4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/sunrpc/gss_api.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/sunrpc/bc_xprt.h>
56 #include <linux/xattr.h>
57 #include <linux/utsname.h>
60 #include "delegation.h"
66 #define NFSDBG_FACILITY NFSDBG_PROC
68 #define NFS4_POLL_RETRY_MIN (HZ/10)
69 #define NFS4_POLL_RETRY_MAX (15*HZ)
71 #define NFS4_MAX_LOOP_ON_RECOVER (10)
74 static int _nfs4_proc_open(struct nfs4_opendata *data);
75 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
76 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
77 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
78 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
79 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
80 struct nfs_fattr *fattr, struct iattr *sattr,
81 struct nfs4_state *state);
82 #ifdef CONFIG_NFS_V4_1
83 static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
84 static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
86 /* Prevent leaks of NFSv4 errors into userland */
87 static int nfs4_map_errors(int err)
92 case -NFS4ERR_RESOURCE:
94 case -NFS4ERR_WRONGSEC:
96 case -NFS4ERR_BADOWNER:
97 case -NFS4ERR_BADNAME:
99 case -NFS4ERR_SHARE_DENIED:
102 dprintk("%s could not handle NFSv4 error %d\n",
110 * This is our standard bitmap for GETATTR requests.
112 const u32 nfs4_fattr_bitmap[2] = {
114 | FATTR4_WORD0_CHANGE
117 | FATTR4_WORD0_FILEID,
119 | FATTR4_WORD1_NUMLINKS
121 | FATTR4_WORD1_OWNER_GROUP
122 | FATTR4_WORD1_RAWDEV
123 | FATTR4_WORD1_SPACE_USED
124 | FATTR4_WORD1_TIME_ACCESS
125 | FATTR4_WORD1_TIME_METADATA
126 | FATTR4_WORD1_TIME_MODIFY
129 const u32 nfs4_statfs_bitmap[2] = {
130 FATTR4_WORD0_FILES_AVAIL
131 | FATTR4_WORD0_FILES_FREE
132 | FATTR4_WORD0_FILES_TOTAL,
133 FATTR4_WORD1_SPACE_AVAIL
134 | FATTR4_WORD1_SPACE_FREE
135 | FATTR4_WORD1_SPACE_TOTAL
138 const u32 nfs4_pathconf_bitmap[2] = {
140 | FATTR4_WORD0_MAXNAME,
144 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
145 | FATTR4_WORD0_MAXREAD
146 | FATTR4_WORD0_MAXWRITE
147 | FATTR4_WORD0_LEASE_TIME,
148 FATTR4_WORD1_TIME_DELTA
149 | FATTR4_WORD1_FS_LAYOUT_TYPES,
150 FATTR4_WORD2_LAYOUT_BLKSIZE
153 const u32 nfs4_fs_locations_bitmap[2] = {
155 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID
159 | FATTR4_WORD0_FS_LOCATIONS,
161 | FATTR4_WORD1_NUMLINKS
163 | FATTR4_WORD1_OWNER_GROUP
164 | FATTR4_WORD1_RAWDEV
165 | FATTR4_WORD1_SPACE_USED
166 | FATTR4_WORD1_TIME_ACCESS
167 | FATTR4_WORD1_TIME_METADATA
168 | FATTR4_WORD1_TIME_MODIFY
169 | FATTR4_WORD1_MOUNTED_ON_FILEID
172 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
173 struct nfs4_readdir_arg *readdir)
177 BUG_ON(readdir->count < 80);
179 readdir->cookie = cookie;
180 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
185 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
190 * NFSv4 servers do not return entries for '.' and '..'
191 * Therefore, we fake these entries here. We let '.'
192 * have cookie 0 and '..' have cookie 1. Note that
193 * when talking to the server, we always send cookie 0
196 start = p = kmap_atomic(*readdir->pages, KM_USER0);
199 *p++ = xdr_one; /* next */
200 *p++ = xdr_zero; /* cookie, first word */
201 *p++ = xdr_one; /* cookie, second word */
202 *p++ = xdr_one; /* entry len */
203 memcpy(p, ".\0\0\0", 4); /* entry */
205 *p++ = xdr_one; /* bitmap length */
206 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
207 *p++ = htonl(8); /* attribute buffer length */
208 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
211 *p++ = xdr_one; /* next */
212 *p++ = xdr_zero; /* cookie, first word */
213 *p++ = xdr_two; /* cookie, second word */
214 *p++ = xdr_two; /* entry len */
215 memcpy(p, "..\0\0", 4); /* entry */
217 *p++ = xdr_one; /* bitmap length */
218 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
219 *p++ = htonl(8); /* attribute buffer length */
220 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
222 readdir->pgbase = (char *)p - (char *)start;
223 readdir->count -= readdir->pgbase;
224 kunmap_atomic(start, KM_USER0);
227 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
233 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
234 nfs_wait_bit_killable, TASK_KILLABLE);
238 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
245 *timeout = NFS4_POLL_RETRY_MIN;
246 if (*timeout > NFS4_POLL_RETRY_MAX)
247 *timeout = NFS4_POLL_RETRY_MAX;
248 schedule_timeout_killable(*timeout);
249 if (fatal_signal_pending(current))
255 /* This is the error handling routine for processes that are allowed
258 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
260 struct nfs_client *clp = server->nfs_client;
261 struct nfs4_state *state = exception->state;
262 struct inode *inode = exception->inode;
265 exception->retry = 0;
269 case -NFS4ERR_OPENMODE:
270 if (nfs_have_delegation(inode, FMODE_READ)) {
271 nfs_inode_return_delegation(inode);
272 exception->retry = 1;
277 nfs4_schedule_stateid_recovery(server, state);
278 goto wait_on_recovery;
279 case -NFS4ERR_DELEG_REVOKED:
280 case -NFS4ERR_ADMIN_REVOKED:
281 case -NFS4ERR_BAD_STATEID:
283 nfs_remove_bad_delegation(state->inode);
286 nfs4_schedule_stateid_recovery(server, state);
287 goto wait_on_recovery;
288 case -NFS4ERR_EXPIRED:
290 nfs4_schedule_stateid_recovery(server, state);
291 case -NFS4ERR_STALE_STATEID:
292 case -NFS4ERR_STALE_CLIENTID:
293 nfs4_schedule_lease_recovery(clp);
294 goto wait_on_recovery;
295 #if defined(CONFIG_NFS_V4_1)
296 case -NFS4ERR_BADSESSION:
297 case -NFS4ERR_BADSLOT:
298 case -NFS4ERR_BAD_HIGH_SLOT:
299 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
300 case -NFS4ERR_DEADSESSION:
301 case -NFS4ERR_SEQ_FALSE_RETRY:
302 case -NFS4ERR_SEQ_MISORDERED:
303 dprintk("%s ERROR: %d Reset session\n", __func__,
305 nfs4_schedule_session_recovery(clp->cl_session);
306 goto wait_on_recovery;
307 #endif /* defined(CONFIG_NFS_V4_1) */
308 case -NFS4ERR_FILE_OPEN:
309 if (exception->timeout > HZ) {
310 /* We have retried a decent amount, time to
319 ret = nfs4_delay(server->client, &exception->timeout);
322 case -NFS4ERR_RETRY_UNCACHED_REP:
323 case -NFS4ERR_OLD_STATEID:
324 exception->retry = 1;
326 case -NFS4ERR_BADOWNER:
327 /* The following works around a Linux server bug! */
328 case -NFS4ERR_BADNAME:
329 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
330 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
331 exception->retry = 1;
332 printk(KERN_WARNING "NFS: v4 server %s "
333 "does not accept raw "
335 "Reenabling the idmapper.\n",
336 server->nfs_client->cl_hostname);
339 /* We failed to handle the error */
340 return nfs4_map_errors(ret);
342 ret = nfs4_wait_clnt_recover(clp);
344 exception->retry = 1;
349 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
351 spin_lock(&clp->cl_lock);
352 if (time_before(clp->cl_last_renewal,timestamp))
353 clp->cl_last_renewal = timestamp;
354 spin_unlock(&clp->cl_lock);
357 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
359 do_renew_lease(server->nfs_client, timestamp);
362 #if defined(CONFIG_NFS_V4_1)
365 * nfs4_free_slot - free a slot and efficiently update slot table.
367 * freeing a slot is trivially done by clearing its respective bit
369 * If the freed slotid equals highest_used_slotid we want to update it
370 * so that the server would be able to size down the slot table if needed,
371 * otherwise we know that the highest_used_slotid is still in use.
372 * When updating highest_used_slotid there may be "holes" in the bitmap
373 * so we need to scan down from highest_used_slotid to 0 looking for the now
374 * highest slotid in use.
375 * If none found, highest_used_slotid is set to -1.
377 * Must be called while holding tbl->slot_tbl_lock
380 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
382 int free_slotid = free_slot - tbl->slots;
383 int slotid = free_slotid;
385 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
386 /* clear used bit in bitmap */
387 __clear_bit(slotid, tbl->used_slots);
389 /* update highest_used_slotid when it is freed */
390 if (slotid == tbl->highest_used_slotid) {
391 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
392 if (slotid < tbl->max_slots)
393 tbl->highest_used_slotid = slotid;
395 tbl->highest_used_slotid = -1;
397 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
398 free_slotid, tbl->highest_used_slotid);
402 * Signal state manager thread if session fore channel is drained
404 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
406 struct rpc_task *task;
408 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
409 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
411 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
415 if (ses->fc_slot_table.highest_used_slotid != -1)
418 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
419 complete(&ses->fc_slot_table.complete);
423 * Signal state manager thread if session back channel is drained
425 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
427 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
428 ses->bc_slot_table.highest_used_slotid != -1)
430 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
431 complete(&ses->bc_slot_table.complete);
434 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
436 struct nfs4_slot_table *tbl;
438 tbl = &res->sr_session->fc_slot_table;
440 /* just wake up the next guy waiting since
441 * we may have not consumed a slot after all */
442 dprintk("%s: No slot\n", __func__);
446 spin_lock(&tbl->slot_tbl_lock);
447 nfs4_free_slot(tbl, res->sr_slot);
448 nfs4_check_drain_fc_complete(res->sr_session);
449 spin_unlock(&tbl->slot_tbl_lock);
453 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
455 unsigned long timestamp;
456 struct nfs_client *clp;
459 * sr_status remains 1 if an RPC level error occurred. The server
460 * may or may not have processed the sequence operation..
461 * Proceed as if the server received and processed the sequence
464 if (res->sr_status == 1)
465 res->sr_status = NFS_OK;
467 /* don't increment the sequence number if the task wasn't sent */
468 if (!RPC_WAS_SENT(task))
471 /* Check the SEQUENCE operation status */
472 switch (res->sr_status) {
474 /* Update the slot's sequence and clientid lease timer */
475 ++res->sr_slot->seq_nr;
476 timestamp = res->sr_renewal_time;
477 clp = res->sr_session->clp;
478 do_renew_lease(clp, timestamp);
479 /* Check sequence flags */
480 if (res->sr_status_flags != 0)
481 nfs4_schedule_lease_recovery(clp);
484 /* The server detected a resend of the RPC call and
485 * returned NFS4ERR_DELAY as per Section 2.10.6.2
488 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
490 res->sr_slot - res->sr_session->fc_slot_table.slots,
491 res->sr_slot->seq_nr);
494 /* Just update the slot sequence no. */
495 ++res->sr_slot->seq_nr;
498 /* The session may be reset by one of the error handlers. */
499 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
500 nfs41_sequence_free_slot(res);
503 if (!rpc_restart_call(task))
505 rpc_delay(task, NFS4_POLL_RETRY_MAX);
509 static int nfs4_sequence_done(struct rpc_task *task,
510 struct nfs4_sequence_res *res)
512 if (res->sr_session == NULL)
514 return nfs41_sequence_done(task, res);
518 * nfs4_find_slot - efficiently look for a free slot
520 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
521 * If found, we mark the slot as used, update the highest_used_slotid,
522 * and respectively set up the sequence operation args.
523 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
525 * Note: must be called with under the slot_tbl_lock.
528 nfs4_find_slot(struct nfs4_slot_table *tbl)
531 u8 ret_id = NFS4_MAX_SLOT_TABLE;
532 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
534 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
535 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
537 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
538 if (slotid >= tbl->max_slots)
540 __set_bit(slotid, tbl->used_slots);
541 if (slotid > tbl->highest_used_slotid)
542 tbl->highest_used_slotid = slotid;
545 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
546 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
550 int nfs41_setup_sequence(struct nfs4_session *session,
551 struct nfs4_sequence_args *args,
552 struct nfs4_sequence_res *res,
554 struct rpc_task *task)
556 struct nfs4_slot *slot;
557 struct nfs4_slot_table *tbl;
560 dprintk("--> %s\n", __func__);
561 /* slot already allocated? */
562 if (res->sr_slot != NULL)
565 tbl = &session->fc_slot_table;
567 spin_lock(&tbl->slot_tbl_lock);
568 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
569 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
571 * The state manager will wait until the slot table is empty.
572 * Schedule the reset thread
574 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
575 spin_unlock(&tbl->slot_tbl_lock);
576 dprintk("%s Schedule Session Reset\n", __func__);
580 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
581 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
582 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
583 spin_unlock(&tbl->slot_tbl_lock);
584 dprintk("%s enforce FIFO order\n", __func__);
588 slotid = nfs4_find_slot(tbl);
589 if (slotid == NFS4_MAX_SLOT_TABLE) {
590 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
591 spin_unlock(&tbl->slot_tbl_lock);
592 dprintk("<-- %s: no free slots\n", __func__);
595 spin_unlock(&tbl->slot_tbl_lock);
597 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
598 slot = tbl->slots + slotid;
599 args->sa_session = session;
600 args->sa_slotid = slotid;
601 args->sa_cache_this = cache_reply;
603 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
605 res->sr_session = session;
607 res->sr_renewal_time = jiffies;
608 res->sr_status_flags = 0;
610 * sr_status is only set in decode_sequence, and so will remain
611 * set to 1 if an rpc level failure occurs.
616 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
618 int nfs4_setup_sequence(const struct nfs_server *server,
619 struct nfs4_sequence_args *args,
620 struct nfs4_sequence_res *res,
622 struct rpc_task *task)
624 struct nfs4_session *session = nfs4_get_session(server);
627 if (session == NULL) {
628 args->sa_session = NULL;
629 res->sr_session = NULL;
633 dprintk("--> %s clp %p session %p sr_slot %td\n",
634 __func__, session->clp, session, res->sr_slot ?
635 res->sr_slot - session->fc_slot_table.slots : -1);
637 ret = nfs41_setup_sequence(session, args, res, cache_reply,
640 dprintk("<-- %s status=%d\n", __func__, ret);
644 struct nfs41_call_sync_data {
645 const struct nfs_server *seq_server;
646 struct nfs4_sequence_args *seq_args;
647 struct nfs4_sequence_res *seq_res;
651 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
653 struct nfs41_call_sync_data *data = calldata;
655 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
657 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
658 data->seq_res, data->cache_reply, task))
660 rpc_call_start(task);
663 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
665 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
666 nfs41_call_sync_prepare(task, calldata);
669 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
671 struct nfs41_call_sync_data *data = calldata;
673 nfs41_sequence_done(task, data->seq_res);
676 struct rpc_call_ops nfs41_call_sync_ops = {
677 .rpc_call_prepare = nfs41_call_sync_prepare,
678 .rpc_call_done = nfs41_call_sync_done,
681 struct rpc_call_ops nfs41_call_priv_sync_ops = {
682 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
683 .rpc_call_done = nfs41_call_sync_done,
686 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
687 struct nfs_server *server,
688 struct rpc_message *msg,
689 struct nfs4_sequence_args *args,
690 struct nfs4_sequence_res *res,
695 struct rpc_task *task;
696 struct nfs41_call_sync_data data = {
697 .seq_server = server,
700 .cache_reply = cache_reply,
702 struct rpc_task_setup task_setup = {
705 .callback_ops = &nfs41_call_sync_ops,
706 .callback_data = &data
711 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
712 task = rpc_run_task(&task_setup);
716 ret = task->tk_status;
722 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
723 struct nfs_server *server,
724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
726 struct nfs4_sequence_res *res,
729 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
733 static int nfs4_sequence_done(struct rpc_task *task,
734 struct nfs4_sequence_res *res)
738 #endif /* CONFIG_NFS_V4_1 */
740 int _nfs4_call_sync(struct rpc_clnt *clnt,
741 struct nfs_server *server,
742 struct rpc_message *msg,
743 struct nfs4_sequence_args *args,
744 struct nfs4_sequence_res *res,
747 args->sa_session = res->sr_session = NULL;
748 return rpc_call_sync(clnt, msg, 0);
752 int nfs4_call_sync(struct rpc_clnt *clnt,
753 struct nfs_server *server,
754 struct rpc_message *msg,
755 struct nfs4_sequence_args *args,
756 struct nfs4_sequence_res *res,
759 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
760 args, res, cache_reply);
763 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
765 struct nfs_inode *nfsi = NFS_I(dir);
767 spin_lock(&dir->i_lock);
768 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
769 if (!cinfo->atomic || cinfo->before != dir->i_version)
770 nfs_force_lookup_revalidate(dir);
771 dir->i_version = cinfo->after;
772 spin_unlock(&dir->i_lock);
775 struct nfs4_opendata {
777 struct nfs_openargs o_arg;
778 struct nfs_openres o_res;
779 struct nfs_open_confirmargs c_arg;
780 struct nfs_open_confirmres c_res;
781 struct nfs_fattr f_attr;
782 struct nfs_fattr dir_attr;
784 struct dentry *dentry;
785 struct nfs4_state_owner *owner;
786 struct nfs4_state *state;
788 unsigned long timestamp;
789 unsigned int rpc_done : 1;
795 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
797 p->o_res.f_attr = &p->f_attr;
798 p->o_res.dir_attr = &p->dir_attr;
799 p->o_res.seqid = p->o_arg.seqid;
800 p->c_res.seqid = p->c_arg.seqid;
801 p->o_res.server = p->o_arg.server;
802 nfs_fattr_init(&p->f_attr);
803 nfs_fattr_init(&p->dir_attr);
806 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
807 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
808 const struct iattr *attrs,
811 struct dentry *parent = dget_parent(dentry);
812 struct inode *dir = parent->d_inode;
813 struct nfs_server *server = NFS_SERVER(dir);
814 struct nfs4_opendata *p;
816 p = kzalloc(sizeof(*p), gfp_mask);
819 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
820 if (p->o_arg.seqid == NULL)
822 nfs_sb_active(dentry->d_sb);
823 p->dentry = dget(dentry);
826 atomic_inc(&sp->so_count);
827 p->o_arg.fh = NFS_FH(dir);
828 p->o_arg.open_flags = flags;
829 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
830 p->o_arg.clientid = server->nfs_client->cl_clientid;
831 p->o_arg.id = sp->so_owner_id.id;
832 p->o_arg.name = &dentry->d_name;
833 p->o_arg.server = server;
834 p->o_arg.bitmask = server->attr_bitmask;
835 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
836 if (flags & O_CREAT) {
839 p->o_arg.u.attrs = &p->attrs;
840 memcpy(&p->attrs, attrs, sizeof(p->attrs));
841 s = (u32 *) p->o_arg.u.verifier.data;
845 p->c_arg.fh = &p->o_res.fh;
846 p->c_arg.stateid = &p->o_res.stateid;
847 p->c_arg.seqid = p->o_arg.seqid;
848 nfs4_init_opendata_res(p);
858 static void nfs4_opendata_free(struct kref *kref)
860 struct nfs4_opendata *p = container_of(kref,
861 struct nfs4_opendata, kref);
862 struct super_block *sb = p->dentry->d_sb;
864 nfs_free_seqid(p->o_arg.seqid);
865 if (p->state != NULL)
866 nfs4_put_open_state(p->state);
867 nfs4_put_state_owner(p->owner);
874 static void nfs4_opendata_put(struct nfs4_opendata *p)
877 kref_put(&p->kref, nfs4_opendata_free);
880 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
884 ret = rpc_wait_for_completion_task(task);
888 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
892 if (open_mode & O_EXCL)
894 switch (mode & (FMODE_READ|FMODE_WRITE)) {
896 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
897 && state->n_rdonly != 0;
900 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
901 && state->n_wronly != 0;
903 case FMODE_READ|FMODE_WRITE:
904 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
905 && state->n_rdwr != 0;
911 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
913 if (delegation == NULL)
915 if ((delegation->type & fmode) != fmode)
917 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
919 nfs_mark_delegation_referenced(delegation);
923 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
932 case FMODE_READ|FMODE_WRITE:
935 nfs4_state_set_mode_locked(state, state->state | fmode);
938 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
940 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
941 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
942 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
945 set_bit(NFS_O_RDONLY_STATE, &state->flags);
948 set_bit(NFS_O_WRONLY_STATE, &state->flags);
950 case FMODE_READ|FMODE_WRITE:
951 set_bit(NFS_O_RDWR_STATE, &state->flags);
955 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
957 write_seqlock(&state->seqlock);
958 nfs_set_open_stateid_locked(state, stateid, fmode);
959 write_sequnlock(&state->seqlock);
962 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
965 * Protect the call to nfs4_state_set_mode_locked and
966 * serialise the stateid update
968 write_seqlock(&state->seqlock);
969 if (deleg_stateid != NULL) {
970 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
971 set_bit(NFS_DELEGATED_STATE, &state->flags);
973 if (open_stateid != NULL)
974 nfs_set_open_stateid_locked(state, open_stateid, fmode);
975 write_sequnlock(&state->seqlock);
976 spin_lock(&state->owner->so_lock);
977 update_open_stateflags(state, fmode);
978 spin_unlock(&state->owner->so_lock);
981 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
983 struct nfs_inode *nfsi = NFS_I(state->inode);
984 struct nfs_delegation *deleg_cur;
987 fmode &= (FMODE_READ|FMODE_WRITE);
990 deleg_cur = rcu_dereference(nfsi->delegation);
991 if (deleg_cur == NULL)
994 spin_lock(&deleg_cur->lock);
995 if (nfsi->delegation != deleg_cur ||
996 (deleg_cur->type & fmode) != fmode)
997 goto no_delegation_unlock;
999 if (delegation == NULL)
1000 delegation = &deleg_cur->stateid;
1001 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1002 goto no_delegation_unlock;
1004 nfs_mark_delegation_referenced(deleg_cur);
1005 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1007 no_delegation_unlock:
1008 spin_unlock(&deleg_cur->lock);
1012 if (!ret && open_stateid != NULL) {
1013 __update_open_stateid(state, open_stateid, NULL, fmode);
1021 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1023 struct nfs_delegation *delegation;
1026 delegation = rcu_dereference(NFS_I(inode)->delegation);
1027 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1032 nfs_inode_return_delegation(inode);
1035 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1037 struct nfs4_state *state = opendata->state;
1038 struct nfs_inode *nfsi = NFS_I(state->inode);
1039 struct nfs_delegation *delegation;
1040 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1041 fmode_t fmode = opendata->o_arg.fmode;
1042 nfs4_stateid stateid;
1046 if (can_open_cached(state, fmode, open_mode)) {
1047 spin_lock(&state->owner->so_lock);
1048 if (can_open_cached(state, fmode, open_mode)) {
1049 update_open_stateflags(state, fmode);
1050 spin_unlock(&state->owner->so_lock);
1051 goto out_return_state;
1053 spin_unlock(&state->owner->so_lock);
1056 delegation = rcu_dereference(nfsi->delegation);
1057 if (!can_open_delegated(delegation, fmode)) {
1061 /* Save the delegation */
1062 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1064 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1069 /* Try to update the stateid using the delegation */
1070 if (update_open_stateid(state, NULL, &stateid, fmode))
1071 goto out_return_state;
1074 return ERR_PTR(ret);
1076 atomic_inc(&state->count);
1080 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1082 struct inode *inode;
1083 struct nfs4_state *state = NULL;
1084 struct nfs_delegation *delegation;
1087 if (!data->rpc_done) {
1088 state = nfs4_try_open_cached(data);
1093 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1095 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1096 ret = PTR_ERR(inode);
1100 state = nfs4_get_open_state(inode, data->owner);
1103 if (data->o_res.delegation_type != 0) {
1104 int delegation_flags = 0;
1107 delegation = rcu_dereference(NFS_I(inode)->delegation);
1109 delegation_flags = delegation->flags;
1111 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1112 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1113 "returning a delegation for "
1114 "OPEN(CLAIM_DELEGATE_CUR)\n",
1115 NFS_CLIENT(inode)->cl_server);
1116 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1117 nfs_inode_set_delegation(state->inode,
1118 data->owner->so_cred,
1121 nfs_inode_reclaim_delegation(state->inode,
1122 data->owner->so_cred,
1126 update_open_stateid(state, &data->o_res.stateid, NULL,
1134 return ERR_PTR(ret);
1137 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1139 struct nfs_inode *nfsi = NFS_I(state->inode);
1140 struct nfs_open_context *ctx;
1142 spin_lock(&state->inode->i_lock);
1143 list_for_each_entry(ctx, &nfsi->open_files, list) {
1144 if (ctx->state != state)
1146 get_nfs_open_context(ctx);
1147 spin_unlock(&state->inode->i_lock);
1150 spin_unlock(&state->inode->i_lock);
1151 return ERR_PTR(-ENOENT);
1154 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1156 struct nfs4_opendata *opendata;
1158 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1159 if (opendata == NULL)
1160 return ERR_PTR(-ENOMEM);
1161 opendata->state = state;
1162 atomic_inc(&state->count);
1166 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1168 struct nfs4_state *newstate;
1171 opendata->o_arg.open_flags = 0;
1172 opendata->o_arg.fmode = fmode;
1173 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1174 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1175 nfs4_init_opendata_res(opendata);
1176 ret = _nfs4_recover_proc_open(opendata);
1179 newstate = nfs4_opendata_to_nfs4_state(opendata);
1180 if (IS_ERR(newstate))
1181 return PTR_ERR(newstate);
1182 nfs4_close_state(newstate, fmode);
1187 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1189 struct nfs4_state *newstate;
1192 /* memory barrier prior to reading state->n_* */
1193 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1195 if (state->n_rdwr != 0) {
1196 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1197 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1200 if (newstate != state)
1203 if (state->n_wronly != 0) {
1204 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1205 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1208 if (newstate != state)
1211 if (state->n_rdonly != 0) {
1212 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1213 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1216 if (newstate != state)
1220 * We may have performed cached opens for all three recoveries.
1221 * Check if we need to update the current stateid.
1223 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1224 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1225 write_seqlock(&state->seqlock);
1226 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1227 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1228 write_sequnlock(&state->seqlock);
1235 * reclaim state on the server after a reboot.
1237 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1239 struct nfs_delegation *delegation;
1240 struct nfs4_opendata *opendata;
1241 fmode_t delegation_type = 0;
1244 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1245 if (IS_ERR(opendata))
1246 return PTR_ERR(opendata);
1247 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1248 opendata->o_arg.fh = NFS_FH(state->inode);
1250 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1251 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1252 delegation_type = delegation->type;
1254 opendata->o_arg.u.delegation_type = delegation_type;
1255 status = nfs4_open_recover(opendata, state);
1256 nfs4_opendata_put(opendata);
1260 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1262 struct nfs_server *server = NFS_SERVER(state->inode);
1263 struct nfs4_exception exception = { };
1266 err = _nfs4_do_open_reclaim(ctx, state);
1267 if (err != -NFS4ERR_DELAY)
1269 nfs4_handle_exception(server, err, &exception);
1270 } while (exception.retry);
1274 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1276 struct nfs_open_context *ctx;
1279 ctx = nfs4_state_find_open_context(state);
1281 return PTR_ERR(ctx);
1282 ret = nfs4_do_open_reclaim(ctx, state);
1283 put_nfs_open_context(ctx);
1287 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1289 struct nfs4_opendata *opendata;
1292 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1293 if (IS_ERR(opendata))
1294 return PTR_ERR(opendata);
1295 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1296 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1297 sizeof(opendata->o_arg.u.delegation.data));
1298 ret = nfs4_open_recover(opendata, state);
1299 nfs4_opendata_put(opendata);
1303 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1305 struct nfs4_exception exception = { };
1306 struct nfs_server *server = NFS_SERVER(state->inode);
1309 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1315 case -NFS4ERR_BADSESSION:
1316 case -NFS4ERR_BADSLOT:
1317 case -NFS4ERR_BAD_HIGH_SLOT:
1318 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1319 case -NFS4ERR_DEADSESSION:
1320 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1322 case -NFS4ERR_STALE_CLIENTID:
1323 case -NFS4ERR_STALE_STATEID:
1324 case -NFS4ERR_EXPIRED:
1325 /* Don't recall a delegation if it was lost */
1326 nfs4_schedule_lease_recovery(server->nfs_client);
1330 * The show must go on: exit, but mark the
1331 * stateid as needing recovery.
1333 case -NFS4ERR_DELEG_REVOKED:
1334 case -NFS4ERR_ADMIN_REVOKED:
1335 case -NFS4ERR_BAD_STATEID:
1336 nfs_inode_find_state_and_recover(state->inode,
1338 nfs4_schedule_stateid_recovery(server, state);
1341 * User RPCSEC_GSS context has expired.
1342 * We cannot recover this stateid now, so
1343 * skip it and allow recovery thread to
1350 err = nfs4_handle_exception(server, err, &exception);
1351 } while (exception.retry);
1356 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1358 struct nfs4_opendata *data = calldata;
1360 data->rpc_status = task->tk_status;
1361 if (data->rpc_status == 0) {
1362 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1363 sizeof(data->o_res.stateid.data));
1364 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1365 renew_lease(data->o_res.server, data->timestamp);
1370 static void nfs4_open_confirm_release(void *calldata)
1372 struct nfs4_opendata *data = calldata;
1373 struct nfs4_state *state = NULL;
1375 /* If this request hasn't been cancelled, do nothing */
1376 if (data->cancelled == 0)
1378 /* In case of error, no cleanup! */
1379 if (!data->rpc_done)
1381 state = nfs4_opendata_to_nfs4_state(data);
1383 nfs4_close_state(state, data->o_arg.fmode);
1385 nfs4_opendata_put(data);
1388 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1389 .rpc_call_done = nfs4_open_confirm_done,
1390 .rpc_release = nfs4_open_confirm_release,
1394 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1396 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1398 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1399 struct rpc_task *task;
1400 struct rpc_message msg = {
1401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1402 .rpc_argp = &data->c_arg,
1403 .rpc_resp = &data->c_res,
1404 .rpc_cred = data->owner->so_cred,
1406 struct rpc_task_setup task_setup_data = {
1407 .rpc_client = server->client,
1408 .rpc_message = &msg,
1409 .callback_ops = &nfs4_open_confirm_ops,
1410 .callback_data = data,
1411 .workqueue = nfsiod_workqueue,
1412 .flags = RPC_TASK_ASYNC,
1416 kref_get(&data->kref);
1418 data->rpc_status = 0;
1419 data->timestamp = jiffies;
1420 task = rpc_run_task(&task_setup_data);
1422 return PTR_ERR(task);
1423 status = nfs4_wait_for_completion_rpc_task(task);
1425 data->cancelled = 1;
1428 status = data->rpc_status;
1433 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1435 struct nfs4_opendata *data = calldata;
1436 struct nfs4_state_owner *sp = data->owner;
1438 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1441 * Check if we still need to send an OPEN call, or if we can use
1442 * a delegation instead.
1444 if (data->state != NULL) {
1445 struct nfs_delegation *delegation;
1447 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1450 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1451 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1452 can_open_delegated(delegation, data->o_arg.fmode))
1453 goto unlock_no_action;
1456 /* Update sequence id. */
1457 data->o_arg.id = sp->so_owner_id.id;
1458 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1459 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1460 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1461 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1463 data->timestamp = jiffies;
1464 if (nfs4_setup_sequence(data->o_arg.server,
1465 &data->o_arg.seq_args,
1466 &data->o_res.seq_res,
1468 nfs_release_seqid(data->o_arg.seqid);
1470 rpc_call_start(task);
1475 task->tk_action = NULL;
1479 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1481 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1482 nfs4_open_prepare(task, calldata);
1485 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1487 struct nfs4_opendata *data = calldata;
1489 data->rpc_status = task->tk_status;
1491 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1494 if (task->tk_status == 0) {
1495 switch (data->o_res.f_attr->mode & S_IFMT) {
1499 data->rpc_status = -ELOOP;
1502 data->rpc_status = -EISDIR;
1505 data->rpc_status = -ENOTDIR;
1507 renew_lease(data->o_res.server, data->timestamp);
1508 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1509 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1514 static void nfs4_open_release(void *calldata)
1516 struct nfs4_opendata *data = calldata;
1517 struct nfs4_state *state = NULL;
1519 /* If this request hasn't been cancelled, do nothing */
1520 if (data->cancelled == 0)
1522 /* In case of error, no cleanup! */
1523 if (data->rpc_status != 0 || !data->rpc_done)
1525 /* In case we need an open_confirm, no cleanup! */
1526 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1528 state = nfs4_opendata_to_nfs4_state(data);
1530 nfs4_close_state(state, data->o_arg.fmode);
1532 nfs4_opendata_put(data);
1535 static const struct rpc_call_ops nfs4_open_ops = {
1536 .rpc_call_prepare = nfs4_open_prepare,
1537 .rpc_call_done = nfs4_open_done,
1538 .rpc_release = nfs4_open_release,
1541 static const struct rpc_call_ops nfs4_recover_open_ops = {
1542 .rpc_call_prepare = nfs4_recover_open_prepare,
1543 .rpc_call_done = nfs4_open_done,
1544 .rpc_release = nfs4_open_release,
1547 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1549 struct inode *dir = data->dir->d_inode;
1550 struct nfs_server *server = NFS_SERVER(dir);
1551 struct nfs_openargs *o_arg = &data->o_arg;
1552 struct nfs_openres *o_res = &data->o_res;
1553 struct rpc_task *task;
1554 struct rpc_message msg = {
1555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1558 .rpc_cred = data->owner->so_cred,
1560 struct rpc_task_setup task_setup_data = {
1561 .rpc_client = server->client,
1562 .rpc_message = &msg,
1563 .callback_ops = &nfs4_open_ops,
1564 .callback_data = data,
1565 .workqueue = nfsiod_workqueue,
1566 .flags = RPC_TASK_ASYNC,
1570 kref_get(&data->kref);
1572 data->rpc_status = 0;
1573 data->cancelled = 0;
1575 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1576 task = rpc_run_task(&task_setup_data);
1578 return PTR_ERR(task);
1579 status = nfs4_wait_for_completion_rpc_task(task);
1581 data->cancelled = 1;
1584 status = data->rpc_status;
1590 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1592 struct inode *dir = data->dir->d_inode;
1593 struct nfs_openres *o_res = &data->o_res;
1596 status = nfs4_run_open_task(data, 1);
1597 if (status != 0 || !data->rpc_done)
1600 nfs_refresh_inode(dir, o_res->dir_attr);
1602 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1603 status = _nfs4_proc_open_confirm(data);
1612 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1614 static int _nfs4_proc_open(struct nfs4_opendata *data)
1616 struct inode *dir = data->dir->d_inode;
1617 struct nfs_server *server = NFS_SERVER(dir);
1618 struct nfs_openargs *o_arg = &data->o_arg;
1619 struct nfs_openres *o_res = &data->o_res;
1622 status = nfs4_run_open_task(data, 0);
1623 if (!data->rpc_done)
1626 if (status == -NFS4ERR_BADNAME &&
1627 !(o_arg->open_flags & O_CREAT))
1632 if (o_arg->open_flags & O_CREAT) {
1633 update_changeattr(dir, &o_res->cinfo);
1634 nfs_post_op_update_inode(dir, o_res->dir_attr);
1636 nfs_refresh_inode(dir, o_res->dir_attr);
1637 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1638 server->caps &= ~NFS_CAP_POSIX_LOCK;
1639 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1640 status = _nfs4_proc_open_confirm(data);
1644 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1645 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1649 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1654 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1655 ret = nfs4_wait_clnt_recover(clp);
1658 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1659 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1661 nfs4_schedule_state_manager(clp);
1667 static int nfs4_recover_expired_lease(struct nfs_server *server)
1669 return nfs4_client_recover_expired_lease(server->nfs_client);
1674 * reclaim state on the server after a network partition.
1675 * Assumes caller holds the appropriate lock
1677 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1679 struct nfs4_opendata *opendata;
1682 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1683 if (IS_ERR(opendata))
1684 return PTR_ERR(opendata);
1685 ret = nfs4_open_recover(opendata, state);
1687 d_drop(ctx->dentry);
1688 nfs4_opendata_put(opendata);
1692 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1694 struct nfs_server *server = NFS_SERVER(state->inode);
1695 struct nfs4_exception exception = { };
1699 err = _nfs4_open_expired(ctx, state);
1703 case -NFS4ERR_GRACE:
1704 case -NFS4ERR_DELAY:
1705 nfs4_handle_exception(server, err, &exception);
1708 } while (exception.retry);
1713 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1715 struct nfs_open_context *ctx;
1718 ctx = nfs4_state_find_open_context(state);
1720 return PTR_ERR(ctx);
1721 ret = nfs4_do_open_expired(ctx, state);
1722 put_nfs_open_context(ctx);
1726 #if defined(CONFIG_NFS_V4_1)
1727 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1730 struct nfs_server *server = NFS_SERVER(state->inode);
1732 status = nfs41_test_stateid(server, state);
1733 if (status == NFS_OK)
1735 nfs41_free_stateid(server, state);
1736 return nfs4_open_expired(sp, state);
1741 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1742 * fields corresponding to attributes that were used to store the verifier.
1743 * Make sure we clobber those fields in the later setattr call
1745 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1747 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1748 !(sattr->ia_valid & ATTR_ATIME_SET))
1749 sattr->ia_valid |= ATTR_ATIME;
1751 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1752 !(sattr->ia_valid & ATTR_MTIME_SET))
1753 sattr->ia_valid |= ATTR_MTIME;
1757 * Returns a referenced nfs4_state
1759 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1761 struct nfs4_state_owner *sp;
1762 struct nfs4_state *state = NULL;
1763 struct nfs_server *server = NFS_SERVER(dir);
1764 struct nfs4_opendata *opendata;
1767 /* Protect against reboot recovery conflicts */
1769 if (!(sp = nfs4_get_state_owner(server, cred))) {
1770 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1773 status = nfs4_recover_expired_lease(server);
1775 goto err_put_state_owner;
1776 if (dentry->d_inode != NULL)
1777 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1779 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1780 if (opendata == NULL)
1781 goto err_put_state_owner;
1783 if (dentry->d_inode != NULL)
1784 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1786 status = _nfs4_proc_open(opendata);
1788 goto err_opendata_put;
1790 state = nfs4_opendata_to_nfs4_state(opendata);
1791 status = PTR_ERR(state);
1793 goto err_opendata_put;
1794 if (server->caps & NFS_CAP_POSIX_LOCK)
1795 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1797 if (opendata->o_arg.open_flags & O_EXCL) {
1798 nfs4_exclusive_attrset(opendata, sattr);
1800 nfs_fattr_init(opendata->o_res.f_attr);
1801 status = nfs4_do_setattr(state->inode, cred,
1802 opendata->o_res.f_attr, sattr,
1805 nfs_setattr_update_inode(state->inode, sattr);
1806 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1808 nfs_revalidate_inode(server, state->inode);
1809 nfs4_opendata_put(opendata);
1810 nfs4_put_state_owner(sp);
1814 nfs4_opendata_put(opendata);
1815 err_put_state_owner:
1816 nfs4_put_state_owner(sp);
1823 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1825 struct nfs4_exception exception = { };
1826 struct nfs4_state *res;
1829 fmode &= FMODE_READ|FMODE_WRITE;
1831 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1834 /* NOTE: BAD_SEQID means the server and client disagree about the
1835 * book-keeping w.r.t. state-changing operations
1836 * (OPEN/CLOSE/LOCK/LOCKU...)
1837 * It is actually a sign of a bug on the client or on the server.
1839 * If we receive a BAD_SEQID error in the particular case of
1840 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1841 * have unhashed the old state_owner for us, and that we can
1842 * therefore safely retry using a new one. We should still warn
1843 * the user though...
1845 if (status == -NFS4ERR_BAD_SEQID) {
1846 pr_warn_ratelimited("NFS: v4 server %s "
1847 " returned a bad sequence-id error!\n",
1848 NFS_SERVER(dir)->nfs_client->cl_hostname);
1849 exception.retry = 1;
1853 * BAD_STATEID on OPEN means that the server cancelled our
1854 * state before it received the OPEN_CONFIRM.
1855 * Recover by retrying the request as per the discussion
1856 * on Page 181 of RFC3530.
1858 if (status == -NFS4ERR_BAD_STATEID) {
1859 exception.retry = 1;
1862 if (status == -EAGAIN) {
1863 /* We must have found a delegation */
1864 exception.retry = 1;
1867 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1868 status, &exception));
1869 } while (exception.retry);
1873 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1874 struct nfs_fattr *fattr, struct iattr *sattr,
1875 struct nfs4_state *state)
1877 struct nfs_server *server = NFS_SERVER(inode);
1878 struct nfs_setattrargs arg = {
1879 .fh = NFS_FH(inode),
1882 .bitmask = server->attr_bitmask,
1884 struct nfs_setattrres res = {
1888 struct rpc_message msg = {
1889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1894 unsigned long timestamp = jiffies;
1897 nfs_fattr_init(fattr);
1899 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1900 /* Use that stateid */
1901 } else if (state != NULL) {
1902 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1904 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1906 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1907 if (status == 0 && state != NULL)
1908 renew_lease(server, timestamp);
1912 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1913 struct nfs_fattr *fattr, struct iattr *sattr,
1914 struct nfs4_state *state)
1916 struct nfs_server *server = NFS_SERVER(inode);
1917 struct nfs4_exception exception = {
1923 err = nfs4_handle_exception(server,
1924 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1926 } while (exception.retry);
1930 struct nfs4_closedata {
1931 struct inode *inode;
1932 struct nfs4_state *state;
1933 struct nfs_closeargs arg;
1934 struct nfs_closeres res;
1935 struct nfs_fattr fattr;
1936 unsigned long timestamp;
1941 static void nfs4_free_closedata(void *data)
1943 struct nfs4_closedata *calldata = data;
1944 struct nfs4_state_owner *sp = calldata->state->owner;
1945 struct super_block *sb = calldata->state->inode->i_sb;
1948 pnfs_roc_release(calldata->state->inode);
1949 nfs4_put_open_state(calldata->state);
1950 nfs_free_seqid(calldata->arg.seqid);
1951 nfs4_put_state_owner(sp);
1952 nfs_sb_deactive(sb);
1956 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1959 spin_lock(&state->owner->so_lock);
1960 if (!(fmode & FMODE_READ))
1961 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1962 if (!(fmode & FMODE_WRITE))
1963 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1964 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1965 spin_unlock(&state->owner->so_lock);
1968 static void nfs4_close_done(struct rpc_task *task, void *data)
1970 struct nfs4_closedata *calldata = data;
1971 struct nfs4_state *state = calldata->state;
1972 struct nfs_server *server = NFS_SERVER(calldata->inode);
1974 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1976 /* hmm. we are done with the inode, and in the process of freeing
1977 * the state_owner. we keep this around to process errors
1979 switch (task->tk_status) {
1982 pnfs_roc_set_barrier(state->inode,
1983 calldata->roc_barrier);
1984 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1985 renew_lease(server, calldata->timestamp);
1986 nfs4_close_clear_stateid_flags(state,
1987 calldata->arg.fmode);
1989 case -NFS4ERR_STALE_STATEID:
1990 case -NFS4ERR_OLD_STATEID:
1991 case -NFS4ERR_BAD_STATEID:
1992 case -NFS4ERR_EXPIRED:
1993 if (calldata->arg.fmode == 0)
1996 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1997 rpc_restart_call_prepare(task);
1999 nfs_release_seqid(calldata->arg.seqid);
2000 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2003 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2005 struct nfs4_closedata *calldata = data;
2006 struct nfs4_state *state = calldata->state;
2007 bool is_rdonly, is_wronly, is_rdwr;
2010 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2013 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2014 spin_lock(&state->owner->so_lock);
2015 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2016 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2017 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2018 /* Calculate the current open share mode */
2019 calldata->arg.fmode = 0;
2020 if (is_rdonly || is_rdwr)
2021 calldata->arg.fmode |= FMODE_READ;
2022 if (is_wronly || is_rdwr)
2023 calldata->arg.fmode |= FMODE_WRITE;
2024 /* Calculate the change in open mode */
2025 if (state->n_rdwr == 0) {
2026 if (state->n_rdonly == 0) {
2027 call_close |= is_rdonly || is_rdwr;
2028 calldata->arg.fmode &= ~FMODE_READ;
2030 if (state->n_wronly == 0) {
2031 call_close |= is_wronly || is_rdwr;
2032 calldata->arg.fmode &= ~FMODE_WRITE;
2035 spin_unlock(&state->owner->so_lock);
2038 /* Note: exit _without_ calling nfs4_close_done */
2039 task->tk_action = NULL;
2043 if (calldata->arg.fmode == 0) {
2044 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2045 if (calldata->roc &&
2046 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2047 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2053 nfs_fattr_init(calldata->res.fattr);
2054 calldata->timestamp = jiffies;
2055 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2056 &calldata->arg.seq_args, &calldata->res.seq_res,
2058 nfs_release_seqid(calldata->arg.seqid);
2060 rpc_call_start(task);
2063 static const struct rpc_call_ops nfs4_close_ops = {
2064 .rpc_call_prepare = nfs4_close_prepare,
2065 .rpc_call_done = nfs4_close_done,
2066 .rpc_release = nfs4_free_closedata,
2070 * It is possible for data to be read/written from a mem-mapped file
2071 * after the sys_close call (which hits the vfs layer as a flush).
2072 * This means that we can't safely call nfsv4 close on a file until
2073 * the inode is cleared. This in turn means that we are not good
2074 * NFSv4 citizens - we do not indicate to the server to update the file's
2075 * share state even when we are done with one of the three share
2076 * stateid's in the inode.
2078 * NOTE: Caller must be holding the sp->so_owner semaphore!
2080 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2082 struct nfs_server *server = NFS_SERVER(state->inode);
2083 struct nfs4_closedata *calldata;
2084 struct nfs4_state_owner *sp = state->owner;
2085 struct rpc_task *task;
2086 struct rpc_message msg = {
2087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2088 .rpc_cred = state->owner->so_cred,
2090 struct rpc_task_setup task_setup_data = {
2091 .rpc_client = server->client,
2092 .rpc_message = &msg,
2093 .callback_ops = &nfs4_close_ops,
2094 .workqueue = nfsiod_workqueue,
2095 .flags = RPC_TASK_ASYNC,
2097 int status = -ENOMEM;
2099 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2100 if (calldata == NULL)
2102 calldata->inode = state->inode;
2103 calldata->state = state;
2104 calldata->arg.fh = NFS_FH(state->inode);
2105 calldata->arg.stateid = &state->open_stateid;
2106 /* Serialization for the sequence id */
2107 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2108 if (calldata->arg.seqid == NULL)
2109 goto out_free_calldata;
2110 calldata->arg.fmode = 0;
2111 calldata->arg.bitmask = server->cache_consistency_bitmask;
2112 calldata->res.fattr = &calldata->fattr;
2113 calldata->res.seqid = calldata->arg.seqid;
2114 calldata->res.server = server;
2115 calldata->roc = roc;
2116 nfs_sb_active(calldata->inode->i_sb);
2118 msg.rpc_argp = &calldata->arg;
2119 msg.rpc_resp = &calldata->res;
2120 task_setup_data.callback_data = calldata;
2121 task = rpc_run_task(&task_setup_data);
2123 return PTR_ERR(task);
2126 status = rpc_wait_for_completion_task(task);
2133 pnfs_roc_release(state->inode);
2134 nfs4_put_open_state(state);
2135 nfs4_put_state_owner(sp);
2139 static struct inode *
2140 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2142 struct nfs4_state *state;
2144 /* Protect against concurrent sillydeletes */
2145 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2147 return ERR_CAST(state);
2149 return igrab(state->inode);
2152 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2154 if (ctx->state == NULL)
2157 nfs4_close_sync(ctx->state, ctx->mode);
2159 nfs4_close_state(ctx->state, ctx->mode);
2162 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2164 struct nfs4_server_caps_arg args = {
2167 struct nfs4_server_caps_res res = {};
2168 struct rpc_message msg = {
2169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2175 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2177 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2178 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2179 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2180 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2181 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2182 NFS_CAP_CTIME|NFS_CAP_MTIME);
2183 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2184 server->caps |= NFS_CAP_ACLS;
2185 if (res.has_links != 0)
2186 server->caps |= NFS_CAP_HARDLINKS;
2187 if (res.has_symlinks != 0)
2188 server->caps |= NFS_CAP_SYMLINKS;
2189 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2190 server->caps |= NFS_CAP_FILEID;
2191 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2192 server->caps |= NFS_CAP_MODE;
2193 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2194 server->caps |= NFS_CAP_NLINK;
2195 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2196 server->caps |= NFS_CAP_OWNER;
2197 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2198 server->caps |= NFS_CAP_OWNER_GROUP;
2199 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2200 server->caps |= NFS_CAP_ATIME;
2201 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2202 server->caps |= NFS_CAP_CTIME;
2203 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2204 server->caps |= NFS_CAP_MTIME;
2206 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2207 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2208 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2209 server->acl_bitmask = res.acl_bitmask;
2215 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2217 struct nfs4_exception exception = { };
2220 err = nfs4_handle_exception(server,
2221 _nfs4_server_capabilities(server, fhandle),
2223 } while (exception.retry);
2227 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2228 struct nfs_fsinfo *info)
2230 struct nfs4_lookup_root_arg args = {
2231 .bitmask = nfs4_fattr_bitmap,
2233 struct nfs4_lookup_res res = {
2235 .fattr = info->fattr,
2238 struct rpc_message msg = {
2239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2244 nfs_fattr_init(info->fattr);
2245 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2248 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2249 struct nfs_fsinfo *info)
2251 struct nfs4_exception exception = { };
2254 err = _nfs4_lookup_root(server, fhandle, info);
2257 case -NFS4ERR_WRONGSEC:
2260 err = nfs4_handle_exception(server, err, &exception);
2262 } while (exception.retry);
2267 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2268 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2270 struct rpc_auth *auth;
2273 auth = rpcauth_create(flavor, server->client);
2278 ret = nfs4_lookup_root(server, fhandle, info);
2283 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2284 struct nfs_fsinfo *info)
2286 int i, len, status = 0;
2287 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2289 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2290 flav_array[len] = RPC_AUTH_NULL;
2293 for (i = 0; i < len; i++) {
2294 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2295 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2300 * -EACCESS could mean that the user doesn't have correct permissions
2301 * to access the mount. It could also mean that we tried to mount
2302 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2303 * existing mount programs don't handle -EACCES very well so it should
2304 * be mapped to -EPERM instead.
2306 if (status == -EACCES)
2312 * get the file handle for the "/" directory on the server
2314 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2315 struct nfs_fsinfo *info)
2317 int minor_version = server->nfs_client->cl_minorversion;
2318 int status = nfs4_lookup_root(server, fhandle, info);
2319 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2321 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2322 * by nfs4_map_errors() as this function exits.
2324 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2326 status = nfs4_server_capabilities(server, fhandle);
2328 status = nfs4_do_fsinfo(server, fhandle, info);
2329 return nfs4_map_errors(status);
2332 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2334 * Get locations and (maybe) other attributes of a referral.
2335 * Note that we'll actually follow the referral later when
2336 * we detect fsid mismatch in inode revalidation
2338 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2339 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2341 int status = -ENOMEM;
2342 struct page *page = NULL;
2343 struct nfs4_fs_locations *locations = NULL;
2345 page = alloc_page(GFP_KERNEL);
2348 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2349 if (locations == NULL)
2352 status = nfs4_proc_fs_locations(dir, name, locations, page);
2355 /* Make sure server returned a different fsid for the referral */
2356 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2357 dprintk("%s: server did not return a different fsid for"
2358 " a referral at %s\n", __func__, name->name);
2362 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2363 nfs_fixup_referral_attributes(&locations->fattr);
2365 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2366 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2367 memset(fhandle, 0, sizeof(struct nfs_fh));
2375 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2377 struct nfs4_getattr_arg args = {
2379 .bitmask = server->attr_bitmask,
2381 struct nfs4_getattr_res res = {
2385 struct rpc_message msg = {
2386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2391 nfs_fattr_init(fattr);
2392 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2395 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2397 struct nfs4_exception exception = { };
2400 err = nfs4_handle_exception(server,
2401 _nfs4_proc_getattr(server, fhandle, fattr),
2403 } while (exception.retry);
2408 * The file is not closed if it is opened due to the a request to change
2409 * the size of the file. The open call will not be needed once the
2410 * VFS layer lookup-intents are implemented.
2412 * Close is called when the inode is destroyed.
2413 * If we haven't opened the file for O_WRONLY, we
2414 * need to in the size_change case to obtain a stateid.
2417 * Because OPEN is always done by name in nfsv4, it is
2418 * possible that we opened a different file by the same
2419 * name. We can recognize this race condition, but we
2420 * can't do anything about it besides returning an error.
2422 * This will be fixed with VFS changes (lookup-intent).
2425 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2426 struct iattr *sattr)
2428 struct inode *inode = dentry->d_inode;
2429 struct rpc_cred *cred = NULL;
2430 struct nfs4_state *state = NULL;
2433 if (pnfs_ld_layoutret_on_setattr(inode))
2434 pnfs_return_layout(inode);
2436 nfs_fattr_init(fattr);
2438 /* Search for an existing open(O_WRITE) file */
2439 if (sattr->ia_valid & ATTR_FILE) {
2440 struct nfs_open_context *ctx;
2442 ctx = nfs_file_open_context(sattr->ia_file);
2449 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2451 nfs_setattr_update_inode(inode, sattr);
2455 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2456 const struct qstr *name, struct nfs_fh *fhandle,
2457 struct nfs_fattr *fattr)
2459 struct nfs_server *server = NFS_SERVER(dir);
2461 struct nfs4_lookup_arg args = {
2462 .bitmask = server->attr_bitmask,
2463 .dir_fh = NFS_FH(dir),
2466 struct nfs4_lookup_res res = {
2471 struct rpc_message msg = {
2472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2477 nfs_fattr_init(fattr);
2479 dprintk("NFS call lookup %s\n", name->name);
2480 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2481 dprintk("NFS reply lookup: %d\n", status);
2485 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2487 memset(fh, 0, sizeof(struct nfs_fh));
2488 fattr->fsid.major = 1;
2489 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2490 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2491 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2495 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2496 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2498 struct nfs4_exception exception = { };
2503 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2505 case -NFS4ERR_BADNAME:
2507 case -NFS4ERR_MOVED:
2508 return nfs4_get_referral(dir, name, fattr, fhandle);
2509 case -NFS4ERR_WRONGSEC:
2510 nfs_fixup_secinfo_attributes(fattr, fhandle);
2512 err = nfs4_handle_exception(NFS_SERVER(dir),
2513 status, &exception);
2514 } while (exception.retry);
2518 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2520 struct nfs_server *server = NFS_SERVER(inode);
2521 struct nfs4_accessargs args = {
2522 .fh = NFS_FH(inode),
2523 .bitmask = server->attr_bitmask,
2525 struct nfs4_accessres res = {
2528 struct rpc_message msg = {
2529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2532 .rpc_cred = entry->cred,
2534 int mode = entry->mask;
2538 * Determine which access bits we want to ask for...
2540 if (mode & MAY_READ)
2541 args.access |= NFS4_ACCESS_READ;
2542 if (S_ISDIR(inode->i_mode)) {
2543 if (mode & MAY_WRITE)
2544 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2545 if (mode & MAY_EXEC)
2546 args.access |= NFS4_ACCESS_LOOKUP;
2548 if (mode & MAY_WRITE)
2549 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2550 if (mode & MAY_EXEC)
2551 args.access |= NFS4_ACCESS_EXECUTE;
2554 res.fattr = nfs_alloc_fattr();
2555 if (res.fattr == NULL)
2558 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2561 if (res.access & NFS4_ACCESS_READ)
2562 entry->mask |= MAY_READ;
2563 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2564 entry->mask |= MAY_WRITE;
2565 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2566 entry->mask |= MAY_EXEC;
2567 nfs_refresh_inode(inode, res.fattr);
2569 nfs_free_fattr(res.fattr);
2573 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2575 struct nfs4_exception exception = { };
2578 err = nfs4_handle_exception(NFS_SERVER(inode),
2579 _nfs4_proc_access(inode, entry),
2581 } while (exception.retry);
2586 * TODO: For the time being, we don't try to get any attributes
2587 * along with any of the zero-copy operations READ, READDIR,
2590 * In the case of the first three, we want to put the GETATTR
2591 * after the read-type operation -- this is because it is hard
2592 * to predict the length of a GETATTR response in v4, and thus
2593 * align the READ data correctly. This means that the GETATTR
2594 * may end up partially falling into the page cache, and we should
2595 * shift it into the 'tail' of the xdr_buf before processing.
2596 * To do this efficiently, we need to know the total length
2597 * of data received, which doesn't seem to be available outside
2600 * In the case of WRITE, we also want to put the GETATTR after
2601 * the operation -- in this case because we want to make sure
2602 * we get the post-operation mtime and size. This means that
2603 * we can't use xdr_encode_pages() as written: we need a variant
2604 * of it which would leave room in the 'tail' iovec.
2606 * Both of these changes to the XDR layer would in fact be quite
2607 * minor, but I decided to leave them for a subsequent patch.
2609 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2610 unsigned int pgbase, unsigned int pglen)
2612 struct nfs4_readlink args = {
2613 .fh = NFS_FH(inode),
2618 struct nfs4_readlink_res res;
2619 struct rpc_message msg = {
2620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2625 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2628 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2629 unsigned int pgbase, unsigned int pglen)
2631 struct nfs4_exception exception = { };
2634 err = nfs4_handle_exception(NFS_SERVER(inode),
2635 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2637 } while (exception.retry);
2643 * We will need to arrange for the VFS layer to provide an atomic open.
2644 * Until then, this create/open method is prone to inefficiency and race
2645 * conditions due to the lookup, create, and open VFS calls from sys_open()
2646 * placed on the wire.
2648 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2649 * The file will be opened again in the subsequent VFS open call
2650 * (nfs4_proc_file_open).
2652 * The open for read will just hang around to be used by any process that
2653 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2657 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2658 int flags, struct nfs_open_context *ctx)
2660 struct dentry *de = dentry;
2661 struct nfs4_state *state;
2662 struct rpc_cred *cred = NULL;
2671 sattr->ia_mode &= ~current_umask();
2672 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2674 if (IS_ERR(state)) {
2675 status = PTR_ERR(state);
2678 d_add(dentry, igrab(state->inode));
2679 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2683 nfs4_close_sync(state, fmode);
2688 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2690 struct nfs_server *server = NFS_SERVER(dir);
2691 struct nfs_removeargs args = {
2693 .name.len = name->len,
2694 .name.name = name->name,
2695 .bitmask = server->attr_bitmask,
2697 struct nfs_removeres res = {
2700 struct rpc_message msg = {
2701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2705 int status = -ENOMEM;
2707 res.dir_attr = nfs_alloc_fattr();
2708 if (res.dir_attr == NULL)
2711 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2713 update_changeattr(dir, &res.cinfo);
2714 nfs_post_op_update_inode(dir, res.dir_attr);
2716 nfs_free_fattr(res.dir_attr);
2721 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2723 struct nfs4_exception exception = { };
2726 err = nfs4_handle_exception(NFS_SERVER(dir),
2727 _nfs4_proc_remove(dir, name),
2729 } while (exception.retry);
2733 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2735 struct nfs_server *server = NFS_SERVER(dir);
2736 struct nfs_removeargs *args = msg->rpc_argp;
2737 struct nfs_removeres *res = msg->rpc_resp;
2739 args->bitmask = server->cache_consistency_bitmask;
2740 res->server = server;
2741 res->seq_res.sr_slot = NULL;
2742 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2745 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2747 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2749 if (!nfs4_sequence_done(task, &res->seq_res))
2751 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2753 update_changeattr(dir, &res->cinfo);
2754 nfs_post_op_update_inode(dir, res->dir_attr);
2758 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2760 struct nfs_server *server = NFS_SERVER(dir);
2761 struct nfs_renameargs *arg = msg->rpc_argp;
2762 struct nfs_renameres *res = msg->rpc_resp;
2764 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2765 arg->bitmask = server->attr_bitmask;
2766 res->server = server;
2769 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2770 struct inode *new_dir)
2772 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2774 if (!nfs4_sequence_done(task, &res->seq_res))
2776 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2779 update_changeattr(old_dir, &res->old_cinfo);
2780 nfs_post_op_update_inode(old_dir, res->old_fattr);
2781 update_changeattr(new_dir, &res->new_cinfo);
2782 nfs_post_op_update_inode(new_dir, res->new_fattr);
2786 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2787 struct inode *new_dir, struct qstr *new_name)
2789 struct nfs_server *server = NFS_SERVER(old_dir);
2790 struct nfs_renameargs arg = {
2791 .old_dir = NFS_FH(old_dir),
2792 .new_dir = NFS_FH(new_dir),
2793 .old_name = old_name,
2794 .new_name = new_name,
2795 .bitmask = server->attr_bitmask,
2797 struct nfs_renameres res = {
2800 struct rpc_message msg = {
2801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2805 int status = -ENOMEM;
2807 res.old_fattr = nfs_alloc_fattr();
2808 res.new_fattr = nfs_alloc_fattr();
2809 if (res.old_fattr == NULL || res.new_fattr == NULL)
2812 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2814 update_changeattr(old_dir, &res.old_cinfo);
2815 nfs_post_op_update_inode(old_dir, res.old_fattr);
2816 update_changeattr(new_dir, &res.new_cinfo);
2817 nfs_post_op_update_inode(new_dir, res.new_fattr);
2820 nfs_free_fattr(res.new_fattr);
2821 nfs_free_fattr(res.old_fattr);
2825 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2826 struct inode *new_dir, struct qstr *new_name)
2828 struct nfs4_exception exception = { };
2831 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2832 _nfs4_proc_rename(old_dir, old_name,
2835 } while (exception.retry);
2839 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2841 struct nfs_server *server = NFS_SERVER(inode);
2842 struct nfs4_link_arg arg = {
2843 .fh = NFS_FH(inode),
2844 .dir_fh = NFS_FH(dir),
2846 .bitmask = server->attr_bitmask,
2848 struct nfs4_link_res res = {
2851 struct rpc_message msg = {
2852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2856 int status = -ENOMEM;
2858 res.fattr = nfs_alloc_fattr();
2859 res.dir_attr = nfs_alloc_fattr();
2860 if (res.fattr == NULL || res.dir_attr == NULL)
2863 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2865 update_changeattr(dir, &res.cinfo);
2866 nfs_post_op_update_inode(dir, res.dir_attr);
2867 nfs_post_op_update_inode(inode, res.fattr);
2870 nfs_free_fattr(res.dir_attr);
2871 nfs_free_fattr(res.fattr);
2875 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2877 struct nfs4_exception exception = { };
2880 err = nfs4_handle_exception(NFS_SERVER(inode),
2881 _nfs4_proc_link(inode, dir, name),
2883 } while (exception.retry);
2887 struct nfs4_createdata {
2888 struct rpc_message msg;
2889 struct nfs4_create_arg arg;
2890 struct nfs4_create_res res;
2892 struct nfs_fattr fattr;
2893 struct nfs_fattr dir_fattr;
2896 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2897 struct qstr *name, struct iattr *sattr, u32 ftype)
2899 struct nfs4_createdata *data;
2901 data = kzalloc(sizeof(*data), GFP_KERNEL);
2903 struct nfs_server *server = NFS_SERVER(dir);
2905 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2906 data->msg.rpc_argp = &data->arg;
2907 data->msg.rpc_resp = &data->res;
2908 data->arg.dir_fh = NFS_FH(dir);
2909 data->arg.server = server;
2910 data->arg.name = name;
2911 data->arg.attrs = sattr;
2912 data->arg.ftype = ftype;
2913 data->arg.bitmask = server->attr_bitmask;
2914 data->res.server = server;
2915 data->res.fh = &data->fh;
2916 data->res.fattr = &data->fattr;
2917 data->res.dir_fattr = &data->dir_fattr;
2918 nfs_fattr_init(data->res.fattr);
2919 nfs_fattr_init(data->res.dir_fattr);
2924 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2926 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2927 &data->arg.seq_args, &data->res.seq_res, 1);
2929 update_changeattr(dir, &data->res.dir_cinfo);
2930 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2931 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2936 static void nfs4_free_createdata(struct nfs4_createdata *data)
2941 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2942 struct page *page, unsigned int len, struct iattr *sattr)
2944 struct nfs4_createdata *data;
2945 int status = -ENAMETOOLONG;
2947 if (len > NFS4_MAXPATHLEN)
2951 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2955 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2956 data->arg.u.symlink.pages = &page;
2957 data->arg.u.symlink.len = len;
2959 status = nfs4_do_create(dir, dentry, data);
2961 nfs4_free_createdata(data);
2966 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2967 struct page *page, unsigned int len, struct iattr *sattr)
2969 struct nfs4_exception exception = { };
2972 err = nfs4_handle_exception(NFS_SERVER(dir),
2973 _nfs4_proc_symlink(dir, dentry, page,
2976 } while (exception.retry);
2980 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2981 struct iattr *sattr)
2983 struct nfs4_createdata *data;
2984 int status = -ENOMEM;
2986 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2990 status = nfs4_do_create(dir, dentry, data);
2992 nfs4_free_createdata(data);
2997 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2998 struct iattr *sattr)
3000 struct nfs4_exception exception = { };
3003 sattr->ia_mode &= ~current_umask();
3005 err = nfs4_handle_exception(NFS_SERVER(dir),
3006 _nfs4_proc_mkdir(dir, dentry, sattr),
3008 } while (exception.retry);
3012 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3013 u64 cookie, struct page **pages, unsigned int count, int plus)
3015 struct inode *dir = dentry->d_inode;
3016 struct nfs4_readdir_arg args = {
3021 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3024 struct nfs4_readdir_res res;
3025 struct rpc_message msg = {
3026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3033 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3034 dentry->d_parent->d_name.name,
3035 dentry->d_name.name,
3036 (unsigned long long)cookie);
3037 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3038 res.pgbase = args.pgbase;
3039 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3041 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3042 status += args.pgbase;
3045 nfs_invalidate_atime(dir);
3047 dprintk("%s: returns %d\n", __func__, status);
3051 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3052 u64 cookie, struct page **pages, unsigned int count, int plus)
3054 struct nfs4_exception exception = { };
3057 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3058 _nfs4_proc_readdir(dentry, cred, cookie,
3059 pages, count, plus),
3061 } while (exception.retry);
3065 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3066 struct iattr *sattr, dev_t rdev)
3068 struct nfs4_createdata *data;
3069 int mode = sattr->ia_mode;
3070 int status = -ENOMEM;
3072 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3073 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3075 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3080 data->arg.ftype = NF4FIFO;
3081 else if (S_ISBLK(mode)) {
3082 data->arg.ftype = NF4BLK;
3083 data->arg.u.device.specdata1 = MAJOR(rdev);
3084 data->arg.u.device.specdata2 = MINOR(rdev);
3086 else if (S_ISCHR(mode)) {
3087 data->arg.ftype = NF4CHR;
3088 data->arg.u.device.specdata1 = MAJOR(rdev);
3089 data->arg.u.device.specdata2 = MINOR(rdev);
3092 status = nfs4_do_create(dir, dentry, data);
3094 nfs4_free_createdata(data);
3099 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3100 struct iattr *sattr, dev_t rdev)
3102 struct nfs4_exception exception = { };
3105 sattr->ia_mode &= ~current_umask();
3107 err = nfs4_handle_exception(NFS_SERVER(dir),
3108 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3110 } while (exception.retry);
3114 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3115 struct nfs_fsstat *fsstat)
3117 struct nfs4_statfs_arg args = {
3119 .bitmask = server->attr_bitmask,
3121 struct nfs4_statfs_res res = {
3124 struct rpc_message msg = {
3125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3130 nfs_fattr_init(fsstat->fattr);
3131 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3134 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3136 struct nfs4_exception exception = { };
3139 err = nfs4_handle_exception(server,
3140 _nfs4_proc_statfs(server, fhandle, fsstat),
3142 } while (exception.retry);
3146 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3147 struct nfs_fsinfo *fsinfo)
3149 struct nfs4_fsinfo_arg args = {
3151 .bitmask = server->attr_bitmask,
3153 struct nfs4_fsinfo_res res = {
3156 struct rpc_message msg = {
3157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3162 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3165 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3167 struct nfs4_exception exception = { };
3171 err = nfs4_handle_exception(server,
3172 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3174 } while (exception.retry);
3178 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3180 nfs_fattr_init(fsinfo->fattr);
3181 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3184 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3185 struct nfs_pathconf *pathconf)
3187 struct nfs4_pathconf_arg args = {
3189 .bitmask = server->attr_bitmask,
3191 struct nfs4_pathconf_res res = {
3192 .pathconf = pathconf,
3194 struct rpc_message msg = {
3195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3200 /* None of the pathconf attributes are mandatory to implement */
3201 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3202 memset(pathconf, 0, sizeof(*pathconf));
3206 nfs_fattr_init(pathconf->fattr);
3207 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3210 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3211 struct nfs_pathconf *pathconf)
3213 struct nfs4_exception exception = { };
3217 err = nfs4_handle_exception(server,
3218 _nfs4_proc_pathconf(server, fhandle, pathconf),