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/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 static int _nfs4_proc_open(struct nfs4_opendata *data);
78 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
79 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
80 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
81 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
82 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
84 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
88 #ifdef CONFIG_NFS_V4_1
89 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
95 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
96 static inline struct nfs4_label *
97 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
108 err = security_dentry_init_security(dentry, sattr->ia_mode,
109 &dentry->d_name, (void **)&label->label, &label->len);
116 nfs4_label_release_security(struct nfs4_label *label)
119 security_release_secctx(label->label, label->len);
121 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
124 return server->attr_bitmask;
126 return server->attr_bitmask_nl;
129 static inline struct nfs4_label *
130 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
131 struct iattr *sattr, struct nfs4_label *l)
134 nfs4_label_release_security(struct nfs4_label *label)
137 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138 { return server->attr_bitmask; }
141 /* Prevent leaks of NFSv4 errors into userland */
142 static int nfs4_map_errors(int err)
147 case -NFS4ERR_RESOURCE:
148 case -NFS4ERR_LAYOUTTRYLATER:
149 case -NFS4ERR_RECALLCONFLICT:
151 case -NFS4ERR_WRONGSEC:
152 case -NFS4ERR_WRONG_CRED:
154 case -NFS4ERR_BADOWNER:
155 case -NFS4ERR_BADNAME:
157 case -NFS4ERR_SHARE_DENIED:
159 case -NFS4ERR_MINOR_VERS_MISMATCH:
160 return -EPROTONOSUPPORT;
161 case -NFS4ERR_ACCESS:
163 case -NFS4ERR_FILE_OPEN:
166 dprintk("%s could not handle NFSv4 error %d\n",
174 * This is our standard bitmap for GETATTR requests.
176 const u32 nfs4_fattr_bitmap[3] = {
178 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_FILEID,
183 | FATTR4_WORD1_NUMLINKS
185 | FATTR4_WORD1_OWNER_GROUP
186 | FATTR4_WORD1_RAWDEV
187 | FATTR4_WORD1_SPACE_USED
188 | FATTR4_WORD1_TIME_ACCESS
189 | FATTR4_WORD1_TIME_METADATA
190 | FATTR4_WORD1_TIME_MODIFY,
191 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
196 static const u32 nfs4_pnfs_open_bitmap[3] = {
198 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_FILEID,
203 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
214 static const u32 nfs4_open_noattr_bitmap[3] = {
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
220 const u32 nfs4_statfs_bitmap[3] = {
221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
229 const u32 nfs4_pathconf_bitmap[3] = {
231 | FATTR4_WORD0_MAXNAME,
235 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
239 FATTR4_WORD1_TIME_DELTA
240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
244 const u32 nfs4_fs_locations_bitmap[3] = {
246 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
252 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
263 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
264 struct nfs4_readdir_arg *readdir)
269 readdir->cookie = cookie;
270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
286 start = p = kmap_atomic(*readdir->pages);
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
298 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
310 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
314 kunmap_atomic(start);
317 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
324 *timeout = NFS4_POLL_RETRY_MIN;
325 if (*timeout > NFS4_POLL_RETRY_MAX)
326 *timeout = NFS4_POLL_RETRY_MAX;
327 freezable_schedule_timeout_killable_unsafe(*timeout);
328 if (fatal_signal_pending(current))
334 /* This is the error handling routine for processes that are allowed
337 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
339 struct nfs_client *clp = server->nfs_client;
340 struct nfs4_state *state = exception->state;
341 struct inode *inode = exception->inode;
344 exception->retry = 0;
348 case -NFS4ERR_OPENMODE:
349 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
350 nfs4_inode_return_delegation(inode);
351 exception->retry = 1;
356 ret = nfs4_schedule_stateid_recovery(server, state);
359 goto wait_on_recovery;
360 case -NFS4ERR_DELEG_REVOKED:
361 case -NFS4ERR_ADMIN_REVOKED:
362 case -NFS4ERR_BAD_STATEID:
363 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
364 nfs_remove_bad_delegation(inode);
365 exception->retry = 1;
370 ret = nfs4_schedule_stateid_recovery(server, state);
373 goto wait_on_recovery;
374 case -NFS4ERR_EXPIRED:
376 ret = nfs4_schedule_stateid_recovery(server, state);
380 case -NFS4ERR_STALE_STATEID:
381 case -NFS4ERR_STALE_CLIENTID:
382 nfs4_schedule_lease_recovery(clp);
383 goto wait_on_recovery;
385 ret = nfs4_schedule_migration_recovery(server);
388 goto wait_on_recovery;
389 case -NFS4ERR_LEASE_MOVED:
390 nfs4_schedule_lease_moved_recovery(clp);
391 goto wait_on_recovery;
392 #if defined(CONFIG_NFS_V4_1)
393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
403 goto wait_on_recovery;
404 #endif /* defined(CONFIG_NFS_V4_1) */
405 case -NFS4ERR_FILE_OPEN:
406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
415 ret = nfs4_delay(server->client, &exception->timeout);
418 case -NFS4ERR_RETRY_UNCACHED_REP:
419 case -NFS4ERR_OLD_STATEID:
420 exception->retry = 1;
422 case -NFS4ERR_BADOWNER:
423 /* The following works around a Linux server bug! */
424 case -NFS4ERR_BADNAME:
425 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
426 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
427 exception->retry = 1;
428 printk(KERN_WARNING "NFS: v4 server %s "
429 "does not accept raw "
431 "Reenabling the idmapper.\n",
432 server->nfs_client->cl_hostname);
435 /* We failed to handle the error */
436 return nfs4_map_errors(ret);
438 ret = nfs4_wait_clnt_recover(clp);
439 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
442 exception->retry = 1;
447 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
448 * or 'false' otherwise.
450 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
452 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
454 if (flavor == RPC_AUTH_GSS_KRB5I ||
455 flavor == RPC_AUTH_GSS_KRB5P)
461 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
463 spin_lock(&clp->cl_lock);
464 if (time_before(clp->cl_last_renewal,timestamp))
465 clp->cl_last_renewal = timestamp;
466 spin_unlock(&clp->cl_lock);
469 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
471 do_renew_lease(server->nfs_client, timestamp);
474 struct nfs4_call_sync_data {
475 const struct nfs_server *seq_server;
476 struct nfs4_sequence_args *seq_args;
477 struct nfs4_sequence_res *seq_res;
480 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
481 struct nfs4_sequence_res *res, int cache_reply)
483 args->sa_slot = NULL;
484 args->sa_cache_this = cache_reply;
485 args->sa_privileged = 0;
490 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
492 args->sa_privileged = 1;
495 static int nfs40_setup_sequence(const struct nfs_server *server,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 struct rpc_task *task)
500 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
501 struct nfs4_slot *slot;
503 /* slot already allocated? */
504 if (res->sr_slot != NULL)
507 spin_lock(&tbl->slot_tbl_lock);
508 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
511 slot = nfs4_alloc_slot(tbl);
513 if (slot == ERR_PTR(-ENOMEM))
514 task->tk_timeout = HZ >> 2;
517 spin_unlock(&tbl->slot_tbl_lock);
519 args->sa_slot = slot;
523 rpc_call_start(task);
527 if (args->sa_privileged)
528 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
529 NULL, RPC_PRIORITY_PRIVILEGED);
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
536 static int nfs40_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
539 struct nfs4_slot *slot = res->sr_slot;
540 struct nfs4_slot_table *tbl;
546 spin_lock(&tbl->slot_tbl_lock);
547 if (!nfs41_wake_and_assign_slot(tbl, slot))
548 nfs4_free_slot(tbl, slot);
549 spin_unlock(&tbl->slot_tbl_lock);
556 #if defined(CONFIG_NFS_V4_1)
558 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
560 struct nfs4_session *session;
561 struct nfs4_slot_table *tbl;
562 struct nfs4_slot *slot = res->sr_slot;
563 bool send_new_highest_used_slotid = false;
566 session = tbl->session;
568 spin_lock(&tbl->slot_tbl_lock);
569 /* Be nice to the server: try to ensure that the last transmitted
570 * value for highest_user_slotid <= target_highest_slotid
572 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
573 send_new_highest_used_slotid = true;
575 if (nfs41_wake_and_assign_slot(tbl, slot)) {
576 send_new_highest_used_slotid = false;
579 nfs4_free_slot(tbl, slot);
581 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
582 send_new_highest_used_slotid = false;
584 spin_unlock(&tbl->slot_tbl_lock);
586 if (send_new_highest_used_slotid)
587 nfs41_server_notify_highest_slotid_update(session->clp);
590 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
592 struct nfs4_session *session;
593 struct nfs4_slot *slot = res->sr_slot;
594 struct nfs_client *clp;
595 bool interrupted = false;
600 /* don't increment the sequence number if the task wasn't sent */
601 if (!RPC_WAS_SENT(task))
604 session = slot->table->session;
606 if (slot->interrupted) {
607 slot->interrupted = 0;
611 trace_nfs4_sequence_done(session, res);
612 /* Check the SEQUENCE operation status */
613 switch (res->sr_status) {
615 /* Update the slot's sequence and clientid lease timer */
618 do_renew_lease(clp, res->sr_timestamp);
619 /* Check sequence flags */
620 if (res->sr_status_flags != 0)
621 nfs4_schedule_lease_recovery(clp);
622 nfs41_update_target_slotid(slot->table, slot, res);
626 * sr_status remains 1 if an RPC level error occurred.
627 * The server may or may not have processed the sequence
629 * Mark the slot as having hosted an interrupted RPC call.
631 slot->interrupted = 1;
634 /* The server detected a resend of the RPC call and
635 * returned NFS4ERR_DELAY as per Section 2.10.6.2
638 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
643 case -NFS4ERR_BADSLOT:
645 * The slot id we used was probably retired. Try again
646 * using a different slot id.
649 case -NFS4ERR_SEQ_MISORDERED:
651 * Was the last operation on this sequence interrupted?
652 * If so, retry after bumping the sequence number.
659 * Could this slot have been previously retired?
660 * If so, then the server may be expecting seq_nr = 1!
662 if (slot->seq_nr != 1) {
667 case -NFS4ERR_SEQ_FALSE_RETRY:
671 /* Just update the slot sequence no. */
675 /* The session may be reset by one of the error handlers. */
676 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
677 nfs41_sequence_free_slot(res);
681 if (rpc_restart_call_prepare(task)) {
687 if (!rpc_restart_call(task))
689 rpc_delay(task, NFS4_POLL_RETRY_MAX);
692 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
694 static int nfs4_sequence_done(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
697 if (res->sr_slot == NULL)
699 if (!res->sr_slot->table->session)
700 return nfs40_sequence_done(task, res);
701 return nfs41_sequence_done(task, res);
704 int nfs41_setup_sequence(struct nfs4_session *session,
705 struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res,
707 struct rpc_task *task)
709 struct nfs4_slot *slot;
710 struct nfs4_slot_table *tbl;
712 dprintk("--> %s\n", __func__);
713 /* slot already allocated? */
714 if (res->sr_slot != NULL)
717 tbl = &session->fc_slot_table;
719 task->tk_timeout = 0;
721 spin_lock(&tbl->slot_tbl_lock);
722 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
723 !args->sa_privileged) {
724 /* The state manager will wait until the slot table is empty */
725 dprintk("%s session is draining\n", __func__);
729 slot = nfs4_alloc_slot(tbl);
731 /* If out of memory, try again in 1/4 second */
732 if (slot == ERR_PTR(-ENOMEM))
733 task->tk_timeout = HZ >> 2;
734 dprintk("<-- %s: no free slots\n", __func__);
737 spin_unlock(&tbl->slot_tbl_lock);
739 args->sa_slot = slot;
741 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
742 slot->slot_nr, slot->seq_nr);
745 res->sr_timestamp = jiffies;
746 res->sr_status_flags = 0;
748 * sr_status is only set in decode_sequence, and so will remain
749 * set to 1 if an rpc level failure occurs.
752 trace_nfs4_setup_sequence(session, args);
754 rpc_call_start(task);
757 /* Privileged tasks are queued with top priority */
758 if (args->sa_privileged)
759 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
760 NULL, RPC_PRIORITY_PRIVILEGED);
762 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
763 spin_unlock(&tbl->slot_tbl_lock);
766 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
768 static int nfs4_setup_sequence(const struct nfs_server *server,
769 struct nfs4_sequence_args *args,
770 struct nfs4_sequence_res *res,
771 struct rpc_task *task)
773 struct nfs4_session *session = nfs4_get_session(server);
777 return nfs40_setup_sequence(server, args, res, task);
779 dprintk("--> %s clp %p session %p sr_slot %u\n",
780 __func__, session->clp, session, res->sr_slot ?
781 res->sr_slot->slot_nr : NFS4_NO_SLOT);
783 ret = nfs41_setup_sequence(session, args, res, task);
785 dprintk("<-- %s status=%d\n", __func__, ret);
789 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
791 struct nfs4_call_sync_data *data = calldata;
792 struct nfs4_session *session = nfs4_get_session(data->seq_server);
794 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
796 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
799 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
801 struct nfs4_call_sync_data *data = calldata;
803 nfs41_sequence_done(task, data->seq_res);
806 static const struct rpc_call_ops nfs41_call_sync_ops = {
807 .rpc_call_prepare = nfs41_call_sync_prepare,
808 .rpc_call_done = nfs41_call_sync_done,
811 #else /* !CONFIG_NFS_V4_1 */
813 static int nfs4_setup_sequence(const struct nfs_server *server,
814 struct nfs4_sequence_args *args,
815 struct nfs4_sequence_res *res,
816 struct rpc_task *task)
818 return nfs40_setup_sequence(server, args, res, task);
821 static int nfs4_sequence_done(struct rpc_task *task,
822 struct nfs4_sequence_res *res)
824 return nfs40_sequence_done(task, res);
827 #endif /* !CONFIG_NFS_V4_1 */
829 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
831 struct nfs4_call_sync_data *data = calldata;
832 nfs4_setup_sequence(data->seq_server,
833 data->seq_args, data->seq_res, task);
836 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
838 struct nfs4_call_sync_data *data = calldata;
839 nfs4_sequence_done(task, data->seq_res);
842 static const struct rpc_call_ops nfs40_call_sync_ops = {
843 .rpc_call_prepare = nfs40_call_sync_prepare,
844 .rpc_call_done = nfs40_call_sync_done,
847 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
848 struct nfs_server *server,
849 struct rpc_message *msg,
850 struct nfs4_sequence_args *args,
851 struct nfs4_sequence_res *res)
854 struct rpc_task *task;
855 struct nfs_client *clp = server->nfs_client;
856 struct nfs4_call_sync_data data = {
857 .seq_server = server,
861 struct rpc_task_setup task_setup = {
864 .callback_ops = clp->cl_mvops->call_sync_ops,
865 .callback_data = &data
868 task = rpc_run_task(&task_setup);
872 ret = task->tk_status;
879 int nfs4_call_sync(struct rpc_clnt *clnt,
880 struct nfs_server *server,
881 struct rpc_message *msg,
882 struct nfs4_sequence_args *args,
883 struct nfs4_sequence_res *res,
886 nfs4_init_sequence(args, res, cache_reply);
887 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
890 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
892 struct nfs_inode *nfsi = NFS_I(dir);
894 spin_lock(&dir->i_lock);
895 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
896 if (!cinfo->atomic || cinfo->before != dir->i_version)
897 nfs_force_lookup_revalidate(dir);
898 dir->i_version = cinfo->after;
899 nfs_fscache_invalidate(dir);
900 spin_unlock(&dir->i_lock);
903 struct nfs4_opendata {
905 struct nfs_openargs o_arg;
906 struct nfs_openres o_res;
907 struct nfs_open_confirmargs c_arg;
908 struct nfs_open_confirmres c_res;
909 struct nfs4_string owner_name;
910 struct nfs4_string group_name;
911 struct nfs_fattr f_attr;
912 struct nfs4_label *f_label;
914 struct dentry *dentry;
915 struct nfs4_state_owner *owner;
916 struct nfs4_state *state;
918 unsigned long timestamp;
919 unsigned int rpc_done : 1;
920 unsigned int file_created : 1;
921 unsigned int is_recover : 1;
926 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
927 int err, struct nfs4_exception *exception)
931 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
933 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
934 exception->retry = 1;
938 static enum open_claim_type4
939 nfs4_map_atomic_open_claim(struct nfs_server *server,
940 enum open_claim_type4 claim)
942 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
947 case NFS4_OPEN_CLAIM_FH:
948 return NFS4_OPEN_CLAIM_NULL;
949 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
950 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
951 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
952 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
956 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
958 p->o_res.f_attr = &p->f_attr;
959 p->o_res.f_label = p->f_label;
960 p->o_res.seqid = p->o_arg.seqid;
961 p->c_res.seqid = p->c_arg.seqid;
962 p->o_res.server = p->o_arg.server;
963 p->o_res.access_request = p->o_arg.access;
964 nfs_fattr_init(&p->f_attr);
965 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
968 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
969 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
970 const struct iattr *attrs,
971 struct nfs4_label *label,
972 enum open_claim_type4 claim,
975 struct dentry *parent = dget_parent(dentry);
976 struct inode *dir = parent->d_inode;
977 struct nfs_server *server = NFS_SERVER(dir);
978 struct nfs4_opendata *p;
980 p = kzalloc(sizeof(*p), gfp_mask);
984 p->f_label = nfs4_label_alloc(server, gfp_mask);
985 if (IS_ERR(p->f_label))
988 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
989 if (p->o_arg.seqid == NULL)
991 nfs_sb_active(dentry->d_sb);
992 p->dentry = dget(dentry);
995 atomic_inc(&sp->so_count);
996 p->o_arg.open_flags = flags;
997 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
998 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
999 * will return permission denied for all bits until close */
1000 if (!(flags & O_EXCL)) {
1001 /* ask server to check for all possible rights as results
1003 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1004 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1006 p->o_arg.clientid = server->nfs_client->cl_clientid;
1007 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1008 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1009 p->o_arg.name = &dentry->d_name;
1010 p->o_arg.server = server;
1011 p->o_arg.bitmask = nfs4_bitmask(server, label);
1012 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1013 p->o_arg.label = label;
1014 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1015 switch (p->o_arg.claim) {
1016 case NFS4_OPEN_CLAIM_NULL:
1017 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1018 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1019 p->o_arg.fh = NFS_FH(dir);
1021 case NFS4_OPEN_CLAIM_PREVIOUS:
1022 case NFS4_OPEN_CLAIM_FH:
1023 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1024 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1025 p->o_arg.fh = NFS_FH(dentry->d_inode);
1027 if (attrs != NULL && attrs->ia_valid != 0) {
1030 p->o_arg.u.attrs = &p->attrs;
1031 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1034 verf[1] = current->pid;
1035 memcpy(p->o_arg.u.verifier.data, verf,
1036 sizeof(p->o_arg.u.verifier.data));
1038 p->c_arg.fh = &p->o_res.fh;
1039 p->c_arg.stateid = &p->o_res.stateid;
1040 p->c_arg.seqid = p->o_arg.seqid;
1041 nfs4_init_opendata_res(p);
1042 kref_init(&p->kref);
1046 nfs4_label_free(p->f_label);
1054 static void nfs4_opendata_free(struct kref *kref)
1056 struct nfs4_opendata *p = container_of(kref,
1057 struct nfs4_opendata, kref);
1058 struct super_block *sb = p->dentry->d_sb;
1060 nfs_free_seqid(p->o_arg.seqid);
1061 if (p->state != NULL)
1062 nfs4_put_open_state(p->state);
1063 nfs4_put_state_owner(p->owner);
1065 nfs4_label_free(p->f_label);
1069 nfs_sb_deactive(sb);
1070 nfs_fattr_free_names(&p->f_attr);
1074 static void nfs4_opendata_put(struct nfs4_opendata *p)
1077 kref_put(&p->kref, nfs4_opendata_free);
1080 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1084 ret = rpc_wait_for_completion_task(task);
1088 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1092 if (open_mode & (O_EXCL|O_TRUNC))
1094 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1096 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1097 && state->n_rdonly != 0;
1100 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1101 && state->n_wronly != 0;
1103 case FMODE_READ|FMODE_WRITE:
1104 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1105 && state->n_rdwr != 0;
1111 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1113 if (delegation == NULL)
1115 if ((delegation->type & fmode) != fmode)
1117 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1119 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1121 nfs_mark_delegation_referenced(delegation);
1125 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1134 case FMODE_READ|FMODE_WRITE:
1137 nfs4_state_set_mode_locked(state, state->state | fmode);
1140 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1141 nfs4_stateid *stateid)
1143 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1145 if (!nfs4_stateid_match_other(stateid, &state->open_stateid))
1147 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1152 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1153 nfs4_stateid *stateid, fmode_t fmode)
1155 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1156 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1158 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1161 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1164 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1165 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1166 clear_bit(NFS_OPEN_STATE, &state->flags);
1168 if (stateid == NULL)
1170 if (!nfs_need_update_open_stateid(state, stateid))
1172 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1173 nfs4_stateid_copy(&state->stateid, stateid);
1174 nfs4_stateid_copy(&state->open_stateid, stateid);
1177 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1179 write_seqlock(&state->seqlock);
1180 nfs_clear_open_stateid_locked(state, stateid, fmode);
1181 write_sequnlock(&state->seqlock);
1184 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1188 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1191 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1193 case FMODE_READ|FMODE_WRITE:
1194 set_bit(NFS_O_RDWR_STATE, &state->flags);
1196 if (!nfs_need_update_open_stateid(state, stateid))
1198 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1199 nfs4_stateid_copy(&state->stateid, stateid);
1200 nfs4_stateid_copy(&state->open_stateid, stateid);
1203 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1206 * Protect the call to nfs4_state_set_mode_locked and
1207 * serialise the stateid update
1209 write_seqlock(&state->seqlock);
1210 if (deleg_stateid != NULL) {
1211 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1212 set_bit(NFS_DELEGATED_STATE, &state->flags);
1214 if (open_stateid != NULL)
1215 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1216 write_sequnlock(&state->seqlock);
1217 spin_lock(&state->owner->so_lock);
1218 update_open_stateflags(state, fmode);
1219 spin_unlock(&state->owner->so_lock);
1222 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1224 struct nfs_inode *nfsi = NFS_I(state->inode);
1225 struct nfs_delegation *deleg_cur;
1228 fmode &= (FMODE_READ|FMODE_WRITE);
1231 deleg_cur = rcu_dereference(nfsi->delegation);
1232 if (deleg_cur == NULL)
1235 spin_lock(&deleg_cur->lock);
1236 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1237 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1238 (deleg_cur->type & fmode) != fmode)
1239 goto no_delegation_unlock;
1241 if (delegation == NULL)
1242 delegation = &deleg_cur->stateid;
1243 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1244 goto no_delegation_unlock;
1246 nfs_mark_delegation_referenced(deleg_cur);
1247 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1249 no_delegation_unlock:
1250 spin_unlock(&deleg_cur->lock);
1254 if (!ret && open_stateid != NULL) {
1255 __update_open_stateid(state, open_stateid, NULL, fmode);
1263 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1265 struct nfs_delegation *delegation;
1268 delegation = rcu_dereference(NFS_I(inode)->delegation);
1269 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1274 nfs4_inode_return_delegation(inode);
1277 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1279 struct nfs4_state *state = opendata->state;
1280 struct nfs_inode *nfsi = NFS_I(state->inode);
1281 struct nfs_delegation *delegation;
1282 int open_mode = opendata->o_arg.open_flags;
1283 fmode_t fmode = opendata->o_arg.fmode;
1284 nfs4_stateid stateid;
1288 if (can_open_cached(state, fmode, open_mode)) {
1289 spin_lock(&state->owner->so_lock);
1290 if (can_open_cached(state, fmode, open_mode)) {
1291 update_open_stateflags(state, fmode);
1292 spin_unlock(&state->owner->so_lock);
1293 goto out_return_state;
1295 spin_unlock(&state->owner->so_lock);
1298 delegation = rcu_dereference(nfsi->delegation);
1299 if (!can_open_delegated(delegation, fmode)) {
1303 /* Save the delegation */
1304 nfs4_stateid_copy(&stateid, &delegation->stateid);
1306 nfs_release_seqid(opendata->o_arg.seqid);
1307 if (!opendata->is_recover) {
1308 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1314 /* Try to update the stateid using the delegation */
1315 if (update_open_stateid(state, NULL, &stateid, fmode))
1316 goto out_return_state;
1319 return ERR_PTR(ret);
1321 atomic_inc(&state->count);
1326 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1328 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1329 struct nfs_delegation *delegation;
1330 int delegation_flags = 0;
1333 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1335 delegation_flags = delegation->flags;
1337 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1338 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1339 "returning a delegation for "
1340 "OPEN(CLAIM_DELEGATE_CUR)\n",
1342 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1343 nfs_inode_set_delegation(state->inode,
1344 data->owner->so_cred,
1347 nfs_inode_reclaim_delegation(state->inode,
1348 data->owner->so_cred,
1353 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1354 * and update the nfs4_state.
1356 static struct nfs4_state *
1357 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1359 struct inode *inode = data->state->inode;
1360 struct nfs4_state *state = data->state;
1363 if (!data->rpc_done) {
1364 if (data->rpc_status) {
1365 ret = data->rpc_status;
1368 /* cached opens have already been processed */
1372 ret = nfs_refresh_inode(inode, &data->f_attr);
1376 if (data->o_res.delegation_type != 0)
1377 nfs4_opendata_check_deleg(data, state);
1379 update_open_stateid(state, &data->o_res.stateid, NULL,
1381 atomic_inc(&state->count);
1385 return ERR_PTR(ret);
1389 static struct nfs4_state *
1390 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1392 struct inode *inode;
1393 struct nfs4_state *state = NULL;
1396 if (!data->rpc_done) {
1397 state = nfs4_try_open_cached(data);
1402 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1404 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1405 ret = PTR_ERR(inode);
1409 state = nfs4_get_open_state(inode, data->owner);
1412 if (data->o_res.delegation_type != 0)
1413 nfs4_opendata_check_deleg(data, state);
1414 update_open_stateid(state, &data->o_res.stateid, NULL,
1418 nfs_release_seqid(data->o_arg.seqid);
1423 return ERR_PTR(ret);
1426 static struct nfs4_state *
1427 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1429 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1430 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1431 return _nfs4_opendata_to_nfs4_state(data);
1434 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1436 struct nfs_inode *nfsi = NFS_I(state->inode);
1437 struct nfs_open_context *ctx;
1439 spin_lock(&state->inode->i_lock);
1440 list_for_each_entry(ctx, &nfsi->open_files, list) {
1441 if (ctx->state != state)
1443 get_nfs_open_context(ctx);
1444 spin_unlock(&state->inode->i_lock);
1447 spin_unlock(&state->inode->i_lock);
1448 return ERR_PTR(-ENOENT);
1451 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1452 struct nfs4_state *state, enum open_claim_type4 claim)
1454 struct nfs4_opendata *opendata;
1456 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1457 NULL, NULL, claim, GFP_NOFS);
1458 if (opendata == NULL)
1459 return ERR_PTR(-ENOMEM);
1460 opendata->state = state;
1461 atomic_inc(&state->count);
1465 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1467 struct nfs4_state *newstate;
1470 opendata->o_arg.open_flags = 0;
1471 opendata->o_arg.fmode = fmode;
1472 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1473 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1474 nfs4_init_opendata_res(opendata);
1475 ret = _nfs4_recover_proc_open(opendata);
1478 newstate = nfs4_opendata_to_nfs4_state(opendata);
1479 if (IS_ERR(newstate))
1480 return PTR_ERR(newstate);
1481 nfs4_close_state(newstate, fmode);
1486 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1488 struct nfs4_state *newstate;
1491 /* memory barrier prior to reading state->n_* */
1492 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1493 clear_bit(NFS_OPEN_STATE, &state->flags);
1495 if (state->n_rdwr != 0) {
1496 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1497 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1500 if (newstate != state)
1503 if (state->n_wronly != 0) {
1504 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1505 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1508 if (newstate != state)
1511 if (state->n_rdonly != 0) {
1512 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1513 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1516 if (newstate != state)
1520 * We may have performed cached opens for all three recoveries.
1521 * Check if we need to update the current stateid.
1523 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1524 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1525 write_seqlock(&state->seqlock);
1526 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1527 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1528 write_sequnlock(&state->seqlock);
1535 * reclaim state on the server after a reboot.
1537 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1539 struct nfs_delegation *delegation;
1540 struct nfs4_opendata *opendata;
1541 fmode_t delegation_type = 0;
1544 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1545 NFS4_OPEN_CLAIM_PREVIOUS);
1546 if (IS_ERR(opendata))
1547 return PTR_ERR(opendata);
1549 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1550 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1551 delegation_type = delegation->type;
1553 opendata->o_arg.u.delegation_type = delegation_type;
1554 status = nfs4_open_recover(opendata, state);
1555 nfs4_opendata_put(opendata);
1559 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1561 struct nfs_server *server = NFS_SERVER(state->inode);
1562 struct nfs4_exception exception = { };
1565 err = _nfs4_do_open_reclaim(ctx, state);
1566 trace_nfs4_open_reclaim(ctx, 0, err);
1567 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1569 if (err != -NFS4ERR_DELAY)
1571 nfs4_handle_exception(server, err, &exception);
1572 } while (exception.retry);
1576 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1578 struct nfs_open_context *ctx;
1581 ctx = nfs4_state_find_open_context(state);
1584 ret = nfs4_do_open_reclaim(ctx, state);
1585 put_nfs_open_context(ctx);
1589 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1593 printk(KERN_ERR "NFS: %s: unhandled error "
1594 "%d.\n", __func__, err);
1599 case -NFS4ERR_BADSESSION:
1600 case -NFS4ERR_BADSLOT:
1601 case -NFS4ERR_BAD_HIGH_SLOT:
1602 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1603 case -NFS4ERR_DEADSESSION:
1604 set_bit(NFS_DELEGATED_STATE, &state->flags);
1605 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1607 case -NFS4ERR_STALE_CLIENTID:
1608 case -NFS4ERR_STALE_STATEID:
1609 set_bit(NFS_DELEGATED_STATE, &state->flags);
1610 case -NFS4ERR_EXPIRED:
1611 /* Don't recall a delegation if it was lost */
1612 nfs4_schedule_lease_recovery(server->nfs_client);
1614 case -NFS4ERR_MOVED:
1615 nfs4_schedule_migration_recovery(server);
1617 case -NFS4ERR_LEASE_MOVED:
1618 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1620 case -NFS4ERR_DELEG_REVOKED:
1621 case -NFS4ERR_ADMIN_REVOKED:
1622 case -NFS4ERR_BAD_STATEID:
1623 case -NFS4ERR_OPENMODE:
1624 nfs_inode_find_state_and_recover(state->inode,
1626 nfs4_schedule_stateid_recovery(server, state);
1628 case -NFS4ERR_DELAY:
1629 case -NFS4ERR_GRACE:
1630 set_bit(NFS_DELEGATED_STATE, &state->flags);
1634 case -NFS4ERR_DENIED:
1635 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1641 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1643 struct nfs_server *server = NFS_SERVER(state->inode);
1644 struct nfs4_opendata *opendata;
1647 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1648 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1649 if (IS_ERR(opendata))
1650 return PTR_ERR(opendata);
1651 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1652 err = nfs4_open_recover(opendata, state);
1653 nfs4_opendata_put(opendata);
1654 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1657 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1659 struct nfs4_opendata *data = calldata;
1661 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1662 &data->c_res.seq_res, task);
1665 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1667 struct nfs4_opendata *data = calldata;
1669 nfs40_sequence_done(task, &data->c_res.seq_res);
1671 data->rpc_status = task->tk_status;
1672 if (data->rpc_status == 0) {
1673 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1674 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1675 renew_lease(data->o_res.server, data->timestamp);
1680 static void nfs4_open_confirm_release(void *calldata)
1682 struct nfs4_opendata *data = calldata;
1683 struct nfs4_state *state = NULL;
1685 /* If this request hasn't been cancelled, do nothing */
1686 if (data->cancelled == 0)
1688 /* In case of error, no cleanup! */
1689 if (!data->rpc_done)
1691 state = nfs4_opendata_to_nfs4_state(data);
1693 nfs4_close_state(state, data->o_arg.fmode);
1695 nfs4_opendata_put(data);
1698 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1699 .rpc_call_prepare = nfs4_open_confirm_prepare,
1700 .rpc_call_done = nfs4_open_confirm_done,
1701 .rpc_release = nfs4_open_confirm_release,
1705 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1707 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1709 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1710 struct rpc_task *task;
1711 struct rpc_message msg = {
1712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1713 .rpc_argp = &data->c_arg,
1714 .rpc_resp = &data->c_res,
1715 .rpc_cred = data->owner->so_cred,
1717 struct rpc_task_setup task_setup_data = {
1718 .rpc_client = server->client,
1719 .rpc_message = &msg,
1720 .callback_ops = &nfs4_open_confirm_ops,
1721 .callback_data = data,
1722 .workqueue = nfsiod_workqueue,
1723 .flags = RPC_TASK_ASYNC,
1727 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1728 kref_get(&data->kref);
1730 data->rpc_status = 0;
1731 data->timestamp = jiffies;
1732 task = rpc_run_task(&task_setup_data);
1734 return PTR_ERR(task);
1735 status = nfs4_wait_for_completion_rpc_task(task);
1737 data->cancelled = 1;
1740 status = data->rpc_status;
1745 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1747 struct nfs4_opendata *data = calldata;
1748 struct nfs4_state_owner *sp = data->owner;
1749 struct nfs_client *clp = sp->so_server->nfs_client;
1751 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1754 * Check if we still need to send an OPEN call, or if we can use
1755 * a delegation instead.
1757 if (data->state != NULL) {
1758 struct nfs_delegation *delegation;
1760 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1763 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1764 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1765 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1766 can_open_delegated(delegation, data->o_arg.fmode))
1767 goto unlock_no_action;
1770 /* Update client id. */
1771 data->o_arg.clientid = clp->cl_clientid;
1772 switch (data->o_arg.claim) {
1773 case NFS4_OPEN_CLAIM_PREVIOUS:
1774 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1775 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1776 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1777 case NFS4_OPEN_CLAIM_FH:
1778 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1779 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1781 data->timestamp = jiffies;
1782 if (nfs4_setup_sequence(data->o_arg.server,
1783 &data->o_arg.seq_args,
1784 &data->o_res.seq_res,
1786 nfs_release_seqid(data->o_arg.seqid);
1788 /* Set the create mode (note dependency on the session type) */
1789 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1790 if (data->o_arg.open_flags & O_EXCL) {
1791 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1792 if (nfs4_has_persistent_session(clp))
1793 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1794 else if (clp->cl_mvops->minor_version > 0)
1795 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1801 task->tk_action = NULL;
1803 nfs4_sequence_done(task, &data->o_res.seq_res);
1806 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1808 struct nfs4_opendata *data = calldata;
1810 data->rpc_status = task->tk_status;
1812 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1815 if (task->tk_status == 0) {
1816 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1817 switch (data->o_res.f_attr->mode & S_IFMT) {
1821 data->rpc_status = -ELOOP;
1824 data->rpc_status = -EISDIR;
1827 data->rpc_status = -ENOTDIR;
1830 renew_lease(data->o_res.server, data->timestamp);
1831 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1832 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1837 static void nfs4_open_release(void *calldata)
1839 struct nfs4_opendata *data = calldata;
1840 struct nfs4_state *state = NULL;
1842 /* If this request hasn't been cancelled, do nothing */
1843 if (data->cancelled == 0)
1845 /* In case of error, no cleanup! */
1846 if (data->rpc_status != 0 || !data->rpc_done)
1848 /* In case we need an open_confirm, no cleanup! */
1849 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1851 state = nfs4_opendata_to_nfs4_state(data);
1853 nfs4_close_state(state, data->o_arg.fmode);
1855 nfs4_opendata_put(data);
1858 static const struct rpc_call_ops nfs4_open_ops = {
1859 .rpc_call_prepare = nfs4_open_prepare,
1860 .rpc_call_done = nfs4_open_done,
1861 .rpc_release = nfs4_open_release,
1864 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1866 struct inode *dir = data->dir->d_inode;
1867 struct nfs_server *server = NFS_SERVER(dir);
1868 struct nfs_openargs *o_arg = &data->o_arg;
1869 struct nfs_openres *o_res = &data->o_res;
1870 struct rpc_task *task;
1871 struct rpc_message msg = {
1872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1875 .rpc_cred = data->owner->so_cred,
1877 struct rpc_task_setup task_setup_data = {
1878 .rpc_client = server->client,
1879 .rpc_message = &msg,
1880 .callback_ops = &nfs4_open_ops,
1881 .callback_data = data,
1882 .workqueue = nfsiod_workqueue,
1883 .flags = RPC_TASK_ASYNC,
1887 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1888 kref_get(&data->kref);
1890 data->rpc_status = 0;
1891 data->cancelled = 0;
1892 data->is_recover = 0;
1894 nfs4_set_sequence_privileged(&o_arg->seq_args);
1895 data->is_recover = 1;
1897 task = rpc_run_task(&task_setup_data);
1899 return PTR_ERR(task);
1900 status = nfs4_wait_for_completion_rpc_task(task);
1902 data->cancelled = 1;
1905 status = data->rpc_status;
1911 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1913 struct inode *dir = data->dir->d_inode;
1914 struct nfs_openres *o_res = &data->o_res;
1917 status = nfs4_run_open_task(data, 1);
1918 if (status != 0 || !data->rpc_done)
1921 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1923 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1924 status = _nfs4_proc_open_confirm(data);
1932 static int nfs4_opendata_access(struct rpc_cred *cred,
1933 struct nfs4_opendata *opendata,
1934 struct nfs4_state *state, fmode_t fmode,
1937 struct nfs_access_entry cache;
1940 /* access call failed or for some reason the server doesn't
1941 * support any access modes -- defer access call until later */
1942 if (opendata->o_res.access_supported == 0)
1946 /* don't check MAY_WRITE - a newly created file may not have
1947 * write mode bits, but POSIX allows the creating process to write.
1948 * use openflags to check for exec, because fmode won't
1949 * always have FMODE_EXEC set when file open for exec. */
1950 if (openflags & __FMODE_EXEC) {
1951 /* ONLY check for exec rights */
1953 } else if (fmode & FMODE_READ)
1957 cache.jiffies = jiffies;
1958 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1959 nfs_access_add_cache(state->inode, &cache);
1961 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1964 /* even though OPEN succeeded, access is denied. Close the file */
1965 nfs4_close_state(state, fmode);
1970 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1972 static int _nfs4_proc_open(struct nfs4_opendata *data)
1974 struct inode *dir = data->dir->d_inode;
1975 struct nfs_server *server = NFS_SERVER(dir);
1976 struct nfs_openargs *o_arg = &data->o_arg;
1977 struct nfs_openres *o_res = &data->o_res;
1980 status = nfs4_run_open_task(data, 0);
1981 if (!data->rpc_done)
1984 if (status == -NFS4ERR_BADNAME &&
1985 !(o_arg->open_flags & O_CREAT))
1990 nfs_fattr_map_and_free_names(server, &data->f_attr);
1992 if (o_arg->open_flags & O_CREAT) {
1993 update_changeattr(dir, &o_res->cinfo);
1994 if (o_arg->open_flags & O_EXCL)
1995 data->file_created = 1;
1996 else if (o_res->cinfo.before != o_res->cinfo.after)
1997 data->file_created = 1;
1999 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2000 server->caps &= ~NFS_CAP_POSIX_LOCK;
2001 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2002 status = _nfs4_proc_open_confirm(data);
2006 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2007 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2011 static int nfs4_recover_expired_lease(struct nfs_server *server)
2013 return nfs4_client_recover_expired_lease(server->nfs_client);
2018 * reclaim state on the server after a network partition.
2019 * Assumes caller holds the appropriate lock
2021 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2023 struct nfs4_opendata *opendata;
2026 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2027 NFS4_OPEN_CLAIM_FH);
2028 if (IS_ERR(opendata))
2029 return PTR_ERR(opendata);
2030 ret = nfs4_open_recover(opendata, state);
2032 d_drop(ctx->dentry);
2033 nfs4_opendata_put(opendata);
2037 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2039 struct nfs_server *server = NFS_SERVER(state->inode);
2040 struct nfs4_exception exception = { };
2044 err = _nfs4_open_expired(ctx, state);
2045 trace_nfs4_open_expired(ctx, 0, err);
2046 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2051 case -NFS4ERR_GRACE:
2052 case -NFS4ERR_DELAY:
2053 nfs4_handle_exception(server, err, &exception);
2056 } while (exception.retry);
2061 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2063 struct nfs_open_context *ctx;
2066 ctx = nfs4_state_find_open_context(state);
2069 ret = nfs4_do_open_expired(ctx, state);
2070 put_nfs_open_context(ctx);
2074 #if defined(CONFIG_NFS_V4_1)
2075 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
2077 struct nfs_server *server = NFS_SERVER(state->inode);
2078 nfs4_stateid *stateid = &state->stateid;
2079 struct nfs_delegation *delegation;
2080 struct rpc_cred *cred = NULL;
2081 int status = -NFS4ERR_BAD_STATEID;
2083 /* If a state reset has been done, test_stateid is unneeded */
2084 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2087 /* Get the delegation credential for use by test/free_stateid */
2089 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2090 if (delegation != NULL &&
2091 nfs4_stateid_match(&delegation->stateid, stateid)) {
2092 cred = get_rpccred(delegation->cred);
2094 status = nfs41_test_stateid(server, stateid, cred);
2095 trace_nfs4_test_delegation_stateid(state, NULL, status);
2099 if (status != NFS_OK) {
2100 /* Free the stateid unless the server explicitly
2101 * informs us the stateid is unrecognized. */
2102 if (status != -NFS4ERR_BAD_STATEID)
2103 nfs41_free_stateid(server, stateid, cred);
2104 nfs_remove_bad_delegation(state->inode);
2106 write_seqlock(&state->seqlock);
2107 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2108 write_sequnlock(&state->seqlock);
2109 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2117 * nfs41_check_open_stateid - possibly free an open stateid
2119 * @state: NFSv4 state for an inode
2121 * Returns NFS_OK if recovery for this stateid is now finished.
2122 * Otherwise a negative NFS4ERR value is returned.
2124 static int nfs41_check_open_stateid(struct nfs4_state *state)
2126 struct nfs_server *server = NFS_SERVER(state->inode);
2127 nfs4_stateid *stateid = &state->open_stateid;
2128 struct rpc_cred *cred = state->owner->so_cred;
2131 /* If a state reset has been done, test_stateid is unneeded */
2132 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2133 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2134 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2135 return -NFS4ERR_BAD_STATEID;
2137 status = nfs41_test_stateid(server, stateid, cred);
2138 trace_nfs4_test_open_stateid(state, NULL, status);
2139 if (status != NFS_OK) {
2140 /* Free the stateid unless the server explicitly
2141 * informs us the stateid is unrecognized. */
2142 if (status != -NFS4ERR_BAD_STATEID)
2143 nfs41_free_stateid(server, stateid, cred);
2145 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2146 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2147 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2148 clear_bit(NFS_OPEN_STATE, &state->flags);
2153 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2157 nfs41_clear_delegation_stateid(state);
2158 status = nfs41_check_open_stateid(state);
2159 if (status != NFS_OK)
2160 status = nfs4_open_expired(sp, state);
2166 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2167 * fields corresponding to attributes that were used to store the verifier.
2168 * Make sure we clobber those fields in the later setattr call
2170 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2172 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2173 !(sattr->ia_valid & ATTR_ATIME_SET))
2174 sattr->ia_valid |= ATTR_ATIME;
2176 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2177 !(sattr->ia_valid & ATTR_MTIME_SET))
2178 sattr->ia_valid |= ATTR_MTIME;
2181 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2184 struct nfs_open_context *ctx)
2186 struct nfs4_state_owner *sp = opendata->owner;
2187 struct nfs_server *server = sp->so_server;
2188 struct dentry *dentry;
2189 struct nfs4_state *state;
2193 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2195 ret = _nfs4_proc_open(opendata);
2199 state = nfs4_opendata_to_nfs4_state(opendata);
2200 ret = PTR_ERR(state);
2203 if (server->caps & NFS_CAP_POSIX_LOCK)
2204 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2206 dentry = opendata->dentry;
2207 if (dentry->d_inode == NULL) {
2208 /* FIXME: Is this d_drop() ever needed? */
2210 dentry = d_add_unique(dentry, igrab(state->inode));
2211 if (dentry == NULL) {
2212 dentry = opendata->dentry;
2213 } else if (dentry != ctx->dentry) {
2215 ctx->dentry = dget(dentry);
2217 nfs_set_verifier(dentry,
2218 nfs_save_change_attribute(opendata->dir->d_inode));
2221 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2226 if (dentry->d_inode == state->inode) {
2227 nfs_inode_attach_open_context(ctx);
2228 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2229 nfs4_schedule_stateid_recovery(server, state);
2236 * Returns a referenced nfs4_state
2238 static int _nfs4_do_open(struct inode *dir,
2239 struct nfs_open_context *ctx,
2241 struct iattr *sattr,
2242 struct nfs4_label *label,
2245 struct nfs4_state_owner *sp;
2246 struct nfs4_state *state = NULL;
2247 struct nfs_server *server = NFS_SERVER(dir);
2248 struct nfs4_opendata *opendata;
2249 struct dentry *dentry = ctx->dentry;
2250 struct rpc_cred *cred = ctx->cred;
2251 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2252 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2253 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2254 struct nfs4_label *olabel = NULL;
2257 /* Protect against reboot recovery conflicts */
2259 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2261 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2264 status = nfs4_recover_expired_lease(server);
2266 goto err_put_state_owner;
2267 if (dentry->d_inode != NULL)
2268 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2270 if (dentry->d_inode)
2271 claim = NFS4_OPEN_CLAIM_FH;
2272 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2273 label, claim, GFP_KERNEL);
2274 if (opendata == NULL)
2275 goto err_put_state_owner;
2278 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2279 if (IS_ERR(olabel)) {
2280 status = PTR_ERR(olabel);
2281 goto err_opendata_put;
2285 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2286 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2287 if (!opendata->f_attr.mdsthreshold)
2288 goto err_free_label;
2289 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2291 if (dentry->d_inode != NULL)
2292 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2294 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2296 goto err_free_label;
2299 if ((opendata->o_arg.open_flags & O_EXCL) &&
2300 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2301 nfs4_exclusive_attrset(opendata, sattr);
2303 nfs_fattr_init(opendata->o_res.f_attr);
2304 status = nfs4_do_setattr(state->inode, cred,
2305 opendata->o_res.f_attr, sattr,
2306 state, label, olabel);
2308 nfs_setattr_update_inode(state->inode, sattr);
2309 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2310 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2313 if (opendata->file_created)
2314 *opened |= FILE_CREATED;
2316 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2317 *ctx_th = opendata->f_attr.mdsthreshold;
2319 kfree(opendata->f_attr.mdsthreshold);
2320 opendata->f_attr.mdsthreshold = NULL;
2322 nfs4_label_free(olabel);
2324 nfs4_opendata_put(opendata);
2325 nfs4_put_state_owner(sp);
2328 nfs4_label_free(olabel);
2330 kfree(opendata->f_attr.mdsthreshold);
2331 nfs4_opendata_put(opendata);
2332 err_put_state_owner:
2333 nfs4_put_state_owner(sp);
2339 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2340 struct nfs_open_context *ctx,
2342 struct iattr *sattr,
2343 struct nfs4_label *label,
2346 struct nfs_server *server = NFS_SERVER(dir);
2347 struct nfs4_exception exception = { };
2348 struct nfs4_state *res;
2352 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2354 trace_nfs4_open_file(ctx, flags, status);
2357 /* NOTE: BAD_SEQID means the server and client disagree about the
2358 * book-keeping w.r.t. state-changing operations
2359 * (OPEN/CLOSE/LOCK/LOCKU...)
2360 * It is actually a sign of a bug on the client or on the server.
2362 * If we receive a BAD_SEQID error in the particular case of
2363 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2364 * have unhashed the old state_owner for us, and that we can
2365 * therefore safely retry using a new one. We should still warn
2366 * the user though...
2368 if (status == -NFS4ERR_BAD_SEQID) {
2369 pr_warn_ratelimited("NFS: v4 server %s "
2370 " returned a bad sequence-id error!\n",
2371 NFS_SERVER(dir)->nfs_client->cl_hostname);
2372 exception.retry = 1;
2376 * BAD_STATEID on OPEN means that the server cancelled our
2377 * state before it received the OPEN_CONFIRM.
2378 * Recover by retrying the request as per the discussion
2379 * on Page 181 of RFC3530.
2381 if (status == -NFS4ERR_BAD_STATEID) {
2382 exception.retry = 1;
2385 if (status == -EAGAIN) {
2386 /* We must have found a delegation */
2387 exception.retry = 1;
2390 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2392 res = ERR_PTR(nfs4_handle_exception(server,
2393 status, &exception));
2394 } while (exception.retry);
2398 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2399 struct nfs_fattr *fattr, struct iattr *sattr,
2400 struct nfs4_state *state, struct nfs4_label *ilabel,
2401 struct nfs4_label *olabel)
2403 struct nfs_server *server = NFS_SERVER(inode);
2404 struct nfs_setattrargs arg = {
2405 .fh = NFS_FH(inode),
2408 .bitmask = server->attr_bitmask,
2411 struct nfs_setattrres res = {
2416 struct rpc_message msg = {
2417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2422 unsigned long timestamp = jiffies;
2427 arg.bitmask = nfs4_bitmask(server, ilabel);
2429 arg.bitmask = nfs4_bitmask(server, olabel);
2431 nfs_fattr_init(fattr);
2433 /* Servers should only apply open mode checks for file size changes */
2434 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2435 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2437 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2438 /* Use that stateid */
2439 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
2440 struct nfs_lockowner lockowner = {
2441 .l_owner = current->files,
2442 .l_pid = current->tgid,
2444 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2447 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2449 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2450 if (status == 0 && state != NULL)
2451 renew_lease(server, timestamp);
2455 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2456 struct nfs_fattr *fattr, struct iattr *sattr,
2457 struct nfs4_state *state, struct nfs4_label *ilabel,
2458 struct nfs4_label *olabel)
2460 struct nfs_server *server = NFS_SERVER(inode);
2461 struct nfs4_exception exception = {
2467 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2468 trace_nfs4_setattr(inode, err);
2470 case -NFS4ERR_OPENMODE:
2471 if (!(sattr->ia_valid & ATTR_SIZE)) {
2472 pr_warn_once("NFSv4: server %s is incorrectly "
2473 "applying open mode checks to "
2474 "a SETATTR that is not "
2475 "changing file size.\n",
2476 server->nfs_client->cl_hostname);
2478 if (state && !(state->state & FMODE_WRITE)) {
2480 if (sattr->ia_valid & ATTR_OPEN)
2485 err = nfs4_handle_exception(server, err, &exception);
2486 } while (exception.retry);
2491 struct nfs4_closedata {
2492 struct inode *inode;
2493 struct nfs4_state *state;
2494 struct nfs_closeargs arg;
2495 struct nfs_closeres res;
2496 struct nfs_fattr fattr;
2497 unsigned long timestamp;
2502 static void nfs4_free_closedata(void *data)
2504 struct nfs4_closedata *calldata = data;
2505 struct nfs4_state_owner *sp = calldata->state->owner;
2506 struct super_block *sb = calldata->state->inode->i_sb;
2509 pnfs_roc_release(calldata->state->inode);
2510 nfs4_put_open_state(calldata->state);
2511 nfs_free_seqid(calldata->arg.seqid);
2512 nfs4_put_state_owner(sp);
2513 nfs_sb_deactive(sb);
2517 static void nfs4_close_done(struct rpc_task *task, void *data)
2519 struct nfs4_closedata *calldata = data;
2520 struct nfs4_state *state = calldata->state;
2521 struct nfs_server *server = NFS_SERVER(calldata->inode);
2523 dprintk("%s: begin!\n", __func__);
2524 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2526 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2527 /* hmm. we are done with the inode, and in the process of freeing
2528 * the state_owner. we keep this around to process errors
2530 switch (task->tk_status) {
2533 pnfs_roc_set_barrier(state->inode,
2534 calldata->roc_barrier);
2535 nfs_clear_open_stateid(state, &calldata->res.stateid, 0);
2536 renew_lease(server, calldata->timestamp);
2538 case -NFS4ERR_ADMIN_REVOKED:
2539 case -NFS4ERR_STALE_STATEID:
2540 case -NFS4ERR_OLD_STATEID:
2541 case -NFS4ERR_BAD_STATEID:
2542 case -NFS4ERR_EXPIRED:
2543 if (calldata->arg.fmode == 0)
2546 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
2547 rpc_restart_call_prepare(task);
2551 nfs_clear_open_stateid(state, NULL, calldata->arg.fmode);
2553 nfs_release_seqid(calldata->arg.seqid);
2554 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2555 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2558 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2560 struct nfs4_closedata *calldata = data;
2561 struct nfs4_state *state = calldata->state;
2562 struct inode *inode = calldata->inode;
2565 dprintk("%s: begin!\n", __func__);
2566 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2569 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2570 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2571 spin_lock(&state->owner->so_lock);
2572 /* Calculate the change in open mode */
2573 if (state->n_rdwr == 0) {
2574 if (state->n_rdonly == 0) {
2575 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2576 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2577 calldata->arg.fmode &= ~FMODE_READ;
2579 if (state->n_wronly == 0) {
2580 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2581 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2582 calldata->arg.fmode &= ~FMODE_WRITE;
2585 if (!nfs4_valid_open_stateid(state))
2587 spin_unlock(&state->owner->so_lock);
2590 /* Note: exit _without_ calling nfs4_close_done */
2594 if (calldata->arg.fmode == 0) {
2595 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2596 if (calldata->roc &&
2597 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2598 nfs_release_seqid(calldata->arg.seqid);
2603 nfs_fattr_init(calldata->res.fattr);
2604 calldata->timestamp = jiffies;
2605 if (nfs4_setup_sequence(NFS_SERVER(inode),
2606 &calldata->arg.seq_args,
2607 &calldata->res.seq_res,
2609 nfs_release_seqid(calldata->arg.seqid);
2610 dprintk("%s: done!\n", __func__);
2613 task->tk_action = NULL;
2615 nfs4_sequence_done(task, &calldata->res.seq_res);
2618 static const struct rpc_call_ops nfs4_close_ops = {
2619 .rpc_call_prepare = nfs4_close_prepare,
2620 .rpc_call_done = nfs4_close_done,
2621 .rpc_release = nfs4_free_closedata,
2625 * It is possible for data to be read/written from a mem-mapped file
2626 * after the sys_close call (which hits the vfs layer as a flush).
2627 * This means that we can't safely call nfsv4 close on a file until
2628 * the inode is cleared. This in turn means that we are not good
2629 * NFSv4 citizens - we do not indicate to the server to update the file's
2630 * share state even when we are done with one of the three share
2631 * stateid's in the inode.
2633 * NOTE: Caller must be holding the sp->so_owner semaphore!
2635 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2637 struct nfs_server *server = NFS_SERVER(state->inode);
2638 struct nfs4_closedata *calldata;
2639 struct nfs4_state_owner *sp = state->owner;
2640 struct rpc_task *task;
2641 struct rpc_message msg = {
2642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2643 .rpc_cred = state->owner->so_cred,
2645 struct rpc_task_setup task_setup_data = {
2646 .rpc_client = server->client,
2647 .rpc_message = &msg,
2648 .callback_ops = &nfs4_close_ops,
2649 .workqueue = nfsiod_workqueue,
2650 .flags = RPC_TASK_ASYNC,
2652 int status = -ENOMEM;
2654 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2655 &task_setup_data.rpc_client, &msg);
2657 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2658 if (calldata == NULL)
2660 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2661 calldata->inode = state->inode;
2662 calldata->state = state;
2663 calldata->arg.fh = NFS_FH(state->inode);
2664 calldata->arg.stateid = &state->open_stateid;
2665 /* Serialization for the sequence id */
2666 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2667 if (calldata->arg.seqid == NULL)
2668 goto out_free_calldata;
2669 calldata->arg.fmode = 0;
2670 calldata->arg.bitmask = server->cache_consistency_bitmask;
2671 calldata->res.fattr = &calldata->fattr;
2672 calldata->res.seqid = calldata->arg.seqid;
2673 calldata->res.server = server;
2674 calldata->roc = pnfs_roc(state->inode);
2675 nfs_sb_active(calldata->inode->i_sb);
2677 msg.rpc_argp = &calldata->arg;
2678 msg.rpc_resp = &calldata->res;
2679 task_setup_data.callback_data = calldata;
2680 task = rpc_run_task(&task_setup_data);
2682 return PTR_ERR(task);
2685 status = rpc_wait_for_completion_task(task);
2691 nfs4_put_open_state(state);
2692 nfs4_put_state_owner(sp);
2696 static struct inode *
2697 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2698 int open_flags, struct iattr *attr, int *opened)
2700 struct nfs4_state *state;
2701 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2703 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2705 /* Protect against concurrent sillydeletes */
2706 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2708 nfs4_label_release_security(label);
2711 return ERR_CAST(state);
2712 return state->inode;
2715 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2717 if (ctx->state == NULL)
2720 nfs4_close_sync(ctx->state, ctx->mode);
2722 nfs4_close_state(ctx->state, ctx->mode);
2725 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2726 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2727 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_CHANGE_SECURITY_LABEL - 1UL)
2729 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2731 struct nfs4_server_caps_arg args = {
2734 struct nfs4_server_caps_res res = {};
2735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2742 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2744 /* Sanity check the server answers */
2745 switch (server->nfs_client->cl_minorversion) {
2747 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2748 res.attr_bitmask[2] = 0;
2751 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2754 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2756 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2757 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2758 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2759 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2760 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2761 NFS_CAP_CTIME|NFS_CAP_MTIME|
2762 NFS_CAP_SECURITY_LABEL);
2763 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2764 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2765 server->caps |= NFS_CAP_ACLS;
2766 if (res.has_links != 0)
2767 server->caps |= NFS_CAP_HARDLINKS;
2768 if (res.has_symlinks != 0)
2769 server->caps |= NFS_CAP_SYMLINKS;
2770 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2771 server->caps |= NFS_CAP_FILEID;
2772 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2773 server->caps |= NFS_CAP_MODE;
2774 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2775 server->caps |= NFS_CAP_NLINK;
2776 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2777 server->caps |= NFS_CAP_OWNER;
2778 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2779 server->caps |= NFS_CAP_OWNER_GROUP;
2780 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2781 server->caps |= NFS_CAP_ATIME;
2782 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2783 server->caps |= NFS_CAP_CTIME;
2784 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2785 server->caps |= NFS_CAP_MTIME;
2786 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2787 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2788 server->caps |= NFS_CAP_SECURITY_LABEL;
2790 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2791 sizeof(server->attr_bitmask));
2792 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2794 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2795 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2796 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2797 server->cache_consistency_bitmask[2] = 0;
2798 server->acl_bitmask = res.acl_bitmask;
2799 server->fh_expire_type = res.fh_expire_type;
2805 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2807 struct nfs4_exception exception = { };
2810 err = nfs4_handle_exception(server,
2811 _nfs4_server_capabilities(server, fhandle),
2813 } while (exception.retry);
2817 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2818 struct nfs_fsinfo *info)
2821 struct nfs4_lookup_root_arg args = {
2824 struct nfs4_lookup_res res = {
2826 .fattr = info->fattr,
2829 struct rpc_message msg = {
2830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2835 bitmask[0] = nfs4_fattr_bitmap[0];
2836 bitmask[1] = nfs4_fattr_bitmap[1];
2838 * Process the label in the upcoming getfattr
2840 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2842 nfs_fattr_init(info->fattr);
2843 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2846 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2847 struct nfs_fsinfo *info)
2849 struct nfs4_exception exception = { };
2852 err = _nfs4_lookup_root(server, fhandle, info);
2853 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2856 case -NFS4ERR_WRONGSEC:
2859 err = nfs4_handle_exception(server, err, &exception);
2861 } while (exception.retry);
2866 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2867 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2869 struct rpc_auth_create_args auth_args = {
2870 .pseudoflavor = flavor,
2872 struct rpc_auth *auth;
2875 auth = rpcauth_create(&auth_args, server->client);
2880 ret = nfs4_lookup_root(server, fhandle, info);
2886 * Retry pseudoroot lookup with various security flavors. We do this when:
2888 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2889 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2891 * Returns zero on success, or a negative NFS4ERR value, or a
2892 * negative errno value.
2894 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2895 struct nfs_fsinfo *info)
2897 /* Per 3530bis 15.33.5 */
2898 static const rpc_authflavor_t flav_array[] = {
2902 RPC_AUTH_UNIX, /* courtesy */
2905 int status = -EPERM;
2908 if (server->auth_info.flavor_len > 0) {
2909 /* try each flavor specified by user */
2910 for (i = 0; i < server->auth_info.flavor_len; i++) {
2911 status = nfs4_lookup_root_sec(server, fhandle, info,
2912 server->auth_info.flavors[i]);
2913 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2918 /* no flavors specified by user, try default list */
2919 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2920 status = nfs4_lookup_root_sec(server, fhandle, info,
2922 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2929 * -EACCESS could mean that the user doesn't have correct permissions
2930 * to access the mount. It could also mean that we tried to mount
2931 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2932 * existing mount programs don't handle -EACCES very well so it should
2933 * be mapped to -EPERM instead.
2935 if (status == -EACCES)
2940 static int nfs4_do_find_root_sec(struct nfs_server *server,
2941 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2943 int mv = server->nfs_client->cl_minorversion;
2944 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2948 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2949 * @server: initialized nfs_server handle
2950 * @fhandle: we fill in the pseudo-fs root file handle
2951 * @info: we fill in an FSINFO struct
2952 * @auth_probe: probe the auth flavours
2954 * Returns zero on success, or a negative errno.
2956 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2957 struct nfs_fsinfo *info,
2962 switch (auth_probe) {
2964 status = nfs4_lookup_root(server, fhandle, info);
2965 if (status != -NFS4ERR_WRONGSEC)
2968 status = nfs4_do_find_root_sec(server, fhandle, info);
2972 status = nfs4_server_capabilities(server, fhandle);
2974 status = nfs4_do_fsinfo(server, fhandle, info);
2976 return nfs4_map_errors(status);
2979 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2980 struct nfs_fsinfo *info)
2983 struct nfs_fattr *fattr = info->fattr;
2984 struct nfs4_label *label = NULL;
2986 error = nfs4_server_capabilities(server, mntfh);
2988 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2992 label = nfs4_label_alloc(server, GFP_KERNEL);
2994 return PTR_ERR(label);
2996 error = nfs4_proc_getattr(server, mntfh, fattr, label);
2998 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2999 goto err_free_label;
3002 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3003 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3004 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3007 nfs4_label_free(label);
3013 * Get locations and (maybe) other attributes of a referral.
3014 * Note that we'll actually follow the referral later when
3015 * we detect fsid mismatch in inode revalidation
3017 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3018 const struct qstr *name, struct nfs_fattr *fattr,
3019 struct nfs_fh *fhandle)
3021 int status = -ENOMEM;
3022 struct page *page = NULL;
3023 struct nfs4_fs_locations *locations = NULL;
3025 page = alloc_page(GFP_KERNEL);
3028 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3029 if (locations == NULL)
3032 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3037 * If the fsid didn't change, this is a migration event, not a
3038 * referral. Cause us to drop into the exception handler, which
3039 * will kick off migration recovery.
3041 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3042 dprintk("%s: server did not return a different fsid for"
3043 " a referral at %s\n", __func__, name->name);
3044 status = -NFS4ERR_MOVED;
3047 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3048 nfs_fixup_referral_attributes(&locations->fattr);
3050 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3051 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3052 memset(fhandle, 0, sizeof(struct nfs_fh));
3060 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3061 struct nfs_fattr *fattr, struct nfs4_label *label)
3063 struct nfs4_getattr_arg args = {
3065 .bitmask = server->attr_bitmask,
3067 struct nfs4_getattr_res res = {
3072 struct rpc_message msg = {
3073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3078 args.bitmask = nfs4_bitmask(server, label);
3080 nfs_fattr_init(fattr);
3081 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3084 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3085 struct nfs_fattr *fattr, struct nfs4_label *label)
3087 struct nfs4_exception exception = { };
3090 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3091 trace_nfs4_getattr(server, fhandle, fattr, err);
3092 err = nfs4_handle_exception(server, err,
3094 } while (exception.retry);
3099 * The file is not closed if it is opened due to the a request to change
3100 * the size of the file. The open call will not be needed once the
3101 * VFS layer lookup-intents are implemented.
3103 * Close is called when the inode is destroyed.
3104 * If we haven't opened the file for O_WRONLY, we
3105 * need to in the size_change case to obtain a stateid.
3108 * Because OPEN is always done by name in nfsv4, it is
3109 * possible that we opened a different file by the same
3110 * name. We can recognize this race condition, but we
3111 * can't do anything about it besides returning an error.
3113 * This will be fixed with VFS changes (lookup-intent).
3116 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3117 struct iattr *sattr)
3119 struct inode *inode = dentry->d_inode;
3120 struct rpc_cred *cred = NULL;
3121 struct nfs4_state *state = NULL;
3122 struct nfs4_label *label = NULL;
3125 if (pnfs_ld_layoutret_on_setattr(inode))
3126 pnfs_commit_and_return_layout(inode);
3128 nfs_fattr_init(fattr);
3130 /* Deal with open(O_TRUNC) */
3131 if (sattr->ia_valid & ATTR_OPEN)
3132 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3134 /* Optimization: if the end result is no change, don't RPC */
3135 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3138 /* Search for an existing open(O_WRITE) file */
3139 if (sattr->ia_valid & ATTR_FILE) {
3140 struct nfs_open_context *ctx;
3142 ctx = nfs_file_open_context(sattr->ia_file);
3149 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3151 return PTR_ERR(label);
3153 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3155 nfs_setattr_update_inode(inode, sattr);
3156 nfs_setsecurity(inode, fattr, label);
3158 nfs4_label_free(label);
3162 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3163 const struct qstr *name, struct nfs_fh *fhandle,
3164 struct nfs_fattr *fattr, struct nfs4_label *label)
3166 struct nfs_server *server = NFS_SERVER(dir);
3168 struct nfs4_lookup_arg args = {
3169 .bitmask = server->attr_bitmask,
3170 .dir_fh = NFS_FH(dir),
3173 struct nfs4_lookup_res res = {
3179 struct rpc_message msg = {
3180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3185 args.bitmask = nfs4_bitmask(server, label);
3187 nfs_fattr_init(fattr);
3189 dprintk("NFS call lookup %s\n", name->name);
3190 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3191 dprintk("NFS reply lookup: %d\n", status);
3195 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3197 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3198 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3199 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3203 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3204 struct qstr *name, struct nfs_fh *fhandle,
3205 struct nfs_fattr *fattr, struct nfs4_label *label)
3207 struct nfs4_exception exception = { };
3208 struct rpc_clnt *client = *clnt;
3211 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3212 trace_nfs4_lookup(dir, name, err);
3214 case -NFS4ERR_BADNAME:
3217 case -NFS4ERR_MOVED:
3218 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3220 case -NFS4ERR_WRONGSEC:
3222 if (client != *clnt)
3224 client = nfs4_create_sec_client(client, dir, name);
3226 return PTR_ERR(client);
3228 exception.retry = 1;
3231 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3233 } while (exception.retry);
3238 else if (client != *clnt)
3239 rpc_shutdown_client(client);
3244 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3245 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3246 struct nfs4_label *label)
3249 struct rpc_clnt *client = NFS_CLIENT(dir);
3251 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3252 if (client != NFS_CLIENT(dir)) {
3253 rpc_shutdown_client(client);
3254 nfs_fixup_secinfo_attributes(fattr);
3260 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3261 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3263 struct rpc_clnt *client = NFS_CLIENT(dir);
3266 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3268 return ERR_PTR(status);
3269 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3272 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3274 struct nfs_server *server = NFS_SERVER(inode);
3275 struct nfs4_accessargs args = {
3276 .fh = NFS_FH(inode),
3277 .bitmask = server->cache_consistency_bitmask,
3279 struct nfs4_accessres res = {
3282 struct rpc_message msg = {
3283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3286 .rpc_cred = entry->cred,
3288 int mode = entry->mask;
3292 * Determine which access bits we want to ask for...
3294 if (mode & MAY_READ)
3295 args.access |= NFS4_ACCESS_READ;
3296 if (S_ISDIR(inode->i_mode)) {
3297 if (mode & MAY_WRITE)
3298 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3299 if (mode & MAY_EXEC)
3300 args.access |= NFS4_ACCESS_LOOKUP;
3302 if (mode & MAY_WRITE)
3303 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3304 if (mode & MAY_EXEC)
3305 args.access |= NFS4_ACCESS_EXECUTE;
3308 res.fattr = nfs_alloc_fattr();
3309 if (res.fattr == NULL)
3312 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3314 nfs_access_set_mask(entry, res.access);
3315 nfs_refresh_inode(inode, res.fattr);
3317 nfs_free_fattr(res.fattr);
3321 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3323 struct nfs4_exception exception = { };
3326 err = _nfs4_proc_access(inode, entry);
3327 trace_nfs4_access(inode, err);
3328 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3330 } while (exception.retry);
3335 * TODO: For the time being, we don't try to get any attributes
3336 * along with any of the zero-copy operations READ, READDIR,
3339 * In the case of the first three, we want to put the GETATTR
3340 * after the read-type operation -- this is because it is hard
3341 * to predict the length of a GETATTR response in v4, and thus
3342 * align the READ data correctly. This means that the GETATTR
3343 * may end up partially falling into the page cache, and we should
3344 * shift it into the 'tail' of the xdr_buf before processing.
3345 * To do this efficiently, we need to know the total length
3346 * of data received, which doesn't seem to be available outside
3349 * In the case of WRITE, we also want to put the GETATTR after
3350 * the operation -- in this case because we want to make sure
3351 * we get the post-operation mtime and size.
3353 * Both of these changes to the XDR layer would in fact be quite
3354 * minor, but I decided to leave them for a subsequent patch.
3356 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3357 unsigned int pgbase, unsigned int pglen)
3359 struct nfs4_readlink args = {
3360 .fh = NFS_FH(inode),
3365 struct nfs4_readlink_res res;
3366 struct rpc_message msg = {
3367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3372 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3375 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3376 unsigned int pgbase, unsigned int pglen)
3378 struct nfs4_exception exception = { };
3381 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3382 trace_nfs4_readlink(inode, err);
3383 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3385 } while (exception.retry);
3390 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3393 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3396 struct nfs4_label l, *ilabel = NULL;
3397 struct nfs_open_context *ctx;
3398 struct nfs4_state *state;
3402 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3404 return PTR_ERR(ctx);
3406 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3408 sattr->ia_mode &= ~current_umask();
3409 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3410 if (IS_ERR(state)) {
3411 status = PTR_ERR(state);
3415 nfs4_label_release_security(ilabel);
3416 put_nfs_open_context(ctx);
3420 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3422 struct nfs_server *server = NFS_SERVER(dir);
3423 struct nfs_removeargs args = {
3427 struct nfs_removeres res = {
3430 struct rpc_message msg = {
3431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3437 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3439 update_changeattr(dir, &res.cinfo);
3443 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3445 struct nfs4_exception exception = { };
3448 err = _nfs4_proc_remove(dir, name);
3449 trace_nfs4_remove(dir, name, err);
3450 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3452 } while (exception.retry);
3456 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3458 struct nfs_server *server = NFS_SERVER(dir);
3459 struct nfs_removeargs *args = msg->rpc_argp;
3460 struct nfs_removeres *res = msg->rpc_resp;
3462 res->server = server;
3463 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3464 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3466 nfs_fattr_init(res->dir_attr);
3469 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3471 nfs4_setup_sequence(NFS_SERVER(data->dir),
3472 &data->args.seq_args,
3477 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3479 struct nfs_unlinkdata *data = task->tk_calldata;
3480 struct nfs_removeres *res = &data->res;
3482 if (!nfs4_sequence_done(task, &res->seq_res))
3484 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3486 update_changeattr(dir, &res->cinfo);
3490 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3492 struct nfs_server *server = NFS_SERVER(dir);
3493 struct nfs_renameargs *arg = msg->rpc_argp;
3494 struct nfs_renameres *res = msg->rpc_resp;
3496 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3497 res->server = server;
3498 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3501 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3503 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3504 &data->args.seq_args,
3509 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3510 struct inode *new_dir)
3512 struct nfs_renamedata *data = task->tk_calldata;
3513 struct nfs_renameres *res = &data->res;
3515 if (!nfs4_sequence_done(task, &res->seq_res))
3517 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3520 update_changeattr(old_dir, &res->old_cinfo);
3521 update_changeattr(new_dir, &res->new_cinfo);
3525 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3526 struct inode *new_dir, struct qstr *new_name)
3528 struct nfs_server *server = NFS_SERVER(old_dir);
3529 struct nfs_renameargs arg = {
3530 .old_dir = NFS_FH(old_dir),
3531 .new_dir = NFS_FH(new_dir),
3532 .old_name = old_name,
3533 .new_name = new_name,
3535 struct nfs_renameres res = {
3538 struct rpc_message msg = {
3539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3543 int status = -ENOMEM;
3545 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3547 update_changeattr(old_dir, &res.old_cinfo);
3548 update_changeattr(new_dir, &res.new_cinfo);
3553 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3554 struct inode *new_dir, struct qstr *new_name)
3556 struct nfs4_exception exception = { };
3559 err = _nfs4_proc_rename(old_dir, old_name,
3561 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3562 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
3564 } while (exception.retry);
3568 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3570 struct nfs_server *server = NFS_SERVER(inode);
3571 struct nfs4_link_arg arg = {
3572 .fh = NFS_FH(inode),
3573 .dir_fh = NFS_FH(dir),
3575 .bitmask = server->attr_bitmask,
3577 struct nfs4_link_res res = {
3581 struct rpc_message msg = {
3582 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3586 int status = -ENOMEM;
3588 res.fattr = nfs_alloc_fattr();
3589 if (res.fattr == NULL)
3592 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3593 if (IS_ERR(res.label)) {
3594 status = PTR_ERR(res.label);
3597 arg.bitmask = nfs4_bitmask(server, res.label);
3599 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3601 update_changeattr(dir, &res.cinfo);
3602 status = nfs_post_op_update_inode(inode, res.fattr);
3604 nfs_setsecurity(inode, res.fattr, res.label);
3608 nfs4_label_free(res.label);
3611 nfs_free_fattr(res.fattr);
3615 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3617 struct nfs4_exception exception = { };
3620 err = nfs4_handle_exception(NFS_SERVER(inode),
3621 _nfs4_proc_link(inode, dir, name),
3623 } while (exception.retry);
3627 struct nfs4_createdata {
3628 struct rpc_message msg;
3629 struct nfs4_create_arg arg;
3630 struct nfs4_create_res res;
3632 struct nfs_fattr fattr;
3633 struct nfs4_label *label;
3636 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3637 struct qstr *name, struct iattr *sattr, u32 ftype)
3639 struct nfs4_createdata *data;
3641 data = kzalloc(sizeof(*data), GFP_KERNEL);
3643 struct nfs_server *server = NFS_SERVER(dir);
3645 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3646 if (IS_ERR(data->label))
3649 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3650 data->msg.rpc_argp = &data->arg;
3651 data->msg.rpc_resp = &data->res;
3652 data->arg.dir_fh = NFS_FH(dir);
3653 data->arg.server = server;
3654 data->arg.name = name;
3655 data->arg.attrs = sattr;
3656 data->arg.ftype = ftype;
3657 data->arg.bitmask = nfs4_bitmask(server, data->label);
3658 data->res.server = server;
3659 data->res.fh = &data->fh;
3660 data->res.fattr = &data->fattr;
3661 data->res.label = data->label;
3662 nfs_fattr_init(data->res.fattr);
3670 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3672 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3673 &data->arg.seq_args, &data->res.seq_res, 1);
3675 update_changeattr(dir, &data->res.dir_cinfo);
3676 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3681 static void nfs4_free_createdata(struct nfs4_createdata *data)
3683 nfs4_label_free(data->label);
3687 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3688 struct page *page, unsigned int len, struct iattr *sattr,
3689 struct nfs4_label *label)
3691 struct nfs4_createdata *data;
3692 int status = -ENAMETOOLONG;
3694 if (len > NFS4_MAXPATHLEN)
3698 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3702 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3703 data->arg.u.symlink.pages = &page;
3704 data->arg.u.symlink.len = len;
3705 data->arg.label = label;
3707 status = nfs4_do_create(dir, dentry, data);
3709 nfs4_free_createdata(data);
3714 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3715 struct page *page, unsigned int len, struct iattr *sattr)
3717 struct nfs4_exception exception = { };
3718 struct nfs4_label l, *label = NULL;
3721 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3724 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3725 trace_nfs4_symlink(dir, &dentry->d_name, err);
3726 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3728 } while (exception.retry);
3730 nfs4_label_release_security(label);
3734 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3735 struct iattr *sattr, struct nfs4_label *label)
3737 struct nfs4_createdata *data;
3738 int status = -ENOMEM;
3740 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3744 data->arg.label = label;
3745 status = nfs4_do_create(dir, dentry, data);
3747 nfs4_free_createdata(data);
3752 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3753 struct iattr *sattr)
3755 struct nfs4_exception exception = { };
3756 struct nfs4_label l, *label = NULL;
3759 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3761 sattr->ia_mode &= ~current_umask();
3763 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3764 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3765 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3767 } while (exception.retry);
3768 nfs4_label_release_security(label);
3773 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3774 u64 cookie, struct page **pages, unsigned int count, int plus)
3776 struct inode *dir = dentry->d_inode;
3777 struct nfs4_readdir_arg args = {
3782 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3785 struct nfs4_readdir_res res;
3786 struct rpc_message msg = {
3787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3794 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3796 (unsigned long long)cookie);
3797 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3798 res.pgbase = args.pgbase;
3799 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3801 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3802 status += args.pgbase;
3805 nfs_invalidate_atime(dir);
3807 dprintk("%s: returns %d\n", __func__, status);
3811 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3812 u64 cookie, struct page **pages, unsigned int count, int plus)
3814 struct nfs4_exception exception = { };
3817 err = _nfs4_proc_readdir(dentry, cred, cookie,
3818 pages, count, plus);
3819 trace_nfs4_readdir(dentry->d_inode, err);
3820 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3822 } while (exception.retry);
3826 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3827 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3829 struct nfs4_createdata *data;
3830 int mode = sattr->ia_mode;
3831 int status = -ENOMEM;
3833 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3838 data->arg.ftype = NF4FIFO;
3839 else if (S_ISBLK(mode)) {
3840 data->arg.ftype = NF4BLK;
3841 data->arg.u.device.specdata1 = MAJOR(rdev);
3842 data->arg.u.device.specdata2 = MINOR(rdev);
3844 else if (S_ISCHR(mode)) {
3845 data->arg.ftype = NF4CHR;
3846 data->arg.u.device.specdata1 = MAJOR(rdev);
3847 data->arg.u.device.specdata2 = MINOR(rdev);
3848 } else if (!S_ISSOCK(mode)) {
3853 data->arg.label = label;
3854 status = nfs4_do_create(dir, dentry, data);
3856 nfs4_free_createdata(data);
3861 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3862 struct iattr *sattr, dev_t rdev)
3864 struct nfs4_exception exception = { };
3865 struct nfs4_label l, *label = NULL;
3868 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3870 sattr->ia_mode &= ~current_umask();
3872 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3873 trace_nfs4_mknod(dir, &dentry->d_name, err);
3874 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3876 } while (exception.retry);
3878 nfs4_label_release_security(label);
3883 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3884 struct nfs_fsstat *fsstat)
3886 struct nfs4_statfs_arg args = {
3888 .bitmask = server->attr_bitmask,
3890 struct nfs4_statfs_res res = {
3893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3899 nfs_fattr_init(fsstat->fattr);
3900 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3903 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3905 struct nfs4_exception exception = { };
3908 err = nfs4_handle_exception(server,
3909 _nfs4_proc_statfs(server, fhandle, fsstat),
3911 } while (exception.retry);
3915 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3916 struct nfs_fsinfo *fsinfo)
3918 struct nfs4_fsinfo_arg args = {
3920 .bitmask = server->attr_bitmask,
3922 struct nfs4_fsinfo_res res = {
3925 struct rpc_message msg = {
3926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3931 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3934 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3936 struct nfs4_exception exception = { };
3937 unsigned long now = jiffies;
3941 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3942 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
3944 struct nfs_client *clp = server->nfs_client;
3946 spin_lock(&clp->cl_lock);
3947 clp->cl_lease_time = fsinfo->lease_time * HZ;
3948 clp->cl_last_renewal = now;
3949 spin_unlock(&clp->cl_lock);
3952 err = nfs4_handle_exception(server, err, &exception);
3953 } while (exception.retry);
3957 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3961 nfs_fattr_init(fsinfo->fattr);
3962 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3964 /* block layout checks this! */
3965 server->pnfs_blksize = fsinfo->blksize;
3966 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3972 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3973 struct nfs_pathconf *pathconf)
3975 struct nfs4_pathconf_arg args = {
3977 .bitmask = server->attr_bitmask,
3979 struct nfs4_pathconf_res res = {
3980 .pathconf = pathconf,
3982 struct rpc_message msg = {
3983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3988 /* None of the pathconf attributes are mandatory to implement */
3989 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3990 memset(pathconf, 0, sizeof(*pathconf));
3994 nfs_fattr_init(pathconf->fattr);
3995 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3998 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3999 struct nfs_pathconf *pathconf)
4001 struct nfs4_exception exception = { };
4005 err = nfs4_handle_exception(server,
4006 _nfs4_proc_pathconf(server, fhandle, pathconf),
4008 } while (exception.retry);
4012 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4013 const struct nfs_open_context *ctx,
4014 const struct nfs_lock_context *l_ctx,
4017 const struct nfs_lockowner *lockowner = NULL;
4020 lockowner = &l_ctx->lockowner;
4021 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4023 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4025 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4026 const struct nfs_open_context *ctx,
4027 const struct nfs_lock_context *l_ctx,
4030 nfs4_stateid current_stateid;
4032 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode))
4034 return nfs4_stateid_match(stateid, ¤t_stateid);
4037 static bool nfs4_error_stateid_expired(int err)
4040 case -NFS4ERR_DELEG_REVOKED:
4041 case -NFS4ERR_ADMIN_REVOKED:
4042 case -NFS4ERR_BAD_STATEID:
4043 case -NFS4ERR_STALE_STATEID:
4044 case -NFS4ERR_OLD_STATEID:
4045 case -NFS4ERR_OPENMODE:
4046 case -NFS4ERR_EXPIRED:
4052 void __nfs4_read_done_cb(struct nfs_read_data *data)
4054 nfs_invalidate_atime(data->header->inode);
4057 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
4059 struct nfs_server *server = NFS_SERVER(data->header->inode);
4061 trace_nfs4_read(data, task->tk_status);
4062 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
4063 rpc_restart_call_prepare(task);
4067 __nfs4_read_done_cb(data);
4068 if (task->tk_status > 0)
4069 renew_lease(server, data->timestamp);
4073 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4074 struct nfs_readargs *args)
4077 if (!nfs4_error_stateid_expired(task->tk_status) ||
4078 nfs4_stateid_is_current(&args->stateid,
4083 rpc_restart_call_prepare(task);
4087 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4090 dprintk("--> %s\n", __func__);
4092 if (!nfs4_sequence_done(task, &data->res.seq_res))
4094 if (nfs4_read_stateid_changed(task, &data->args))
4096 return data->read_done_cb ? data->read_done_cb(task, data) :
4097 nfs4_read_done_cb(task, data);
4100 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
4102 data->timestamp = jiffies;
4103 data->read_done_cb = nfs4_read_done_cb;
4104 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4105 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
4108 static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
4110 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4111 &data->args.seq_args,
4115 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4116 data->args.lock_context, FMODE_READ) == -EIO)
4118 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4123 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
4125 struct inode *inode = data->header->inode;
4127 trace_nfs4_write(data, task->tk_status);
4128 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
4129 rpc_restart_call_prepare(task);
4132 if (task->tk_status >= 0) {
4133 renew_lease(NFS_SERVER(inode), data->timestamp);
4134 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4139 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4140 struct nfs_writeargs *args)
4143 if (!nfs4_error_stateid_expired(task->tk_status) ||
4144 nfs4_stateid_is_current(&args->stateid,
4149 rpc_restart_call_prepare(task);
4153 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4155 if (!nfs4_sequence_done(task, &data->res.seq_res))
4157 if (nfs4_write_stateid_changed(task, &data->args))
4159 return data->write_done_cb ? data->write_done_cb(task, data) :
4160 nfs4_write_done_cb(task, data);
4164 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
4166 const struct nfs_pgio_header *hdr = data->header;
4168 /* Don't request attributes for pNFS or O_DIRECT writes */
4169 if (data->ds_clp != NULL || hdr->dreq != NULL)
4171 /* Otherwise, request attributes if and only if we don't hold
4174 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4177 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
4179 struct nfs_server *server = NFS_SERVER(data->header->inode);
4181 if (!nfs4_write_need_cache_consistency_data(data)) {
4182 data->args.bitmask = NULL;
4183 data->res.fattr = NULL;
4185 data->args.bitmask = server->cache_consistency_bitmask;
4187 if (!data->write_done_cb)
4188 data->write_done_cb = nfs4_write_done_cb;
4189 data->res.server = server;
4190 data->timestamp = jiffies;
4192 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4193 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4196 static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4198 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4199 &data->args.seq_args,
4203 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4204 data->args.lock_context, FMODE_WRITE) == -EIO)
4206 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4211 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4213 nfs4_setup_sequence(NFS_SERVER(data->inode),
4214 &data->args.seq_args,
4219 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4221 struct inode *inode = data->inode;
4223 trace_nfs4_commit(data, task->tk_status);
4224 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
4225 rpc_restart_call_prepare(task);
4231 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4233 if (!nfs4_sequence_done(task, &data->res.seq_res))
4235 return data->commit_done_cb(task, data);
4238 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4240 struct nfs_server *server = NFS_SERVER(data->inode);
4242 if (data->commit_done_cb == NULL)
4243 data->commit_done_cb = nfs4_commit_done_cb;
4244 data->res.server = server;
4245 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4246 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4249 struct nfs4_renewdata {
4250 struct nfs_client *client;
4251 unsigned long timestamp;
4255 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4256 * standalone procedure for queueing an asynchronous RENEW.
4258 static void nfs4_renew_release(void *calldata)
4260 struct nfs4_renewdata *data = calldata;
4261 struct nfs_client *clp = data->client;
4263 if (atomic_read(&clp->cl_count) > 1)
4264 nfs4_schedule_state_renewal(clp);
4265 nfs_put_client(clp);
4269 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4271 struct nfs4_renewdata *data = calldata;
4272 struct nfs_client *clp = data->client;
4273 unsigned long timestamp = data->timestamp;
4275 trace_nfs4_renew_async(clp, task->tk_status);
4276 switch (task->tk_status) {
4279 case -NFS4ERR_LEASE_MOVED:
4280 nfs4_schedule_lease_moved_recovery(clp);
4283 /* Unless we're shutting down, schedule state recovery! */
4284 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4286 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4287 nfs4_schedule_lease_recovery(clp);
4290 nfs4_schedule_path_down_recovery(clp);
4292 do_renew_lease(clp, timestamp);
4295 static const struct rpc_call_ops nfs4_renew_ops = {
4296 .rpc_call_done = nfs4_renew_done,
4297 .rpc_release = nfs4_renew_release,
4300 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4302 struct rpc_message msg = {
4303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4307 struct nfs4_renewdata *data;
4309 if (renew_flags == 0)
4311 if (!atomic_inc_not_zero(&clp->cl_count))
4313 data = kmalloc(sizeof(*data), GFP_NOFS);
4317 data->timestamp = jiffies;
4318 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4319 &nfs4_renew_ops, data);
4322 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4324 struct rpc_message msg = {
4325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4329 unsigned long now = jiffies;
4332 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4335 do_renew_lease(clp, now);
4339 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4341 return server->caps & NFS_CAP_ACLS;
4344 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4345 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4348 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4350 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4351 struct page **pages, unsigned int *pgbase)
4353 struct page *newpage, **spages;
4359 len = min_t(size_t, PAGE_SIZE, buflen);
4360 newpage = alloc_page(GFP_KERNEL);
4362 if (newpage == NULL)
4364 memcpy(page_address(newpage), buf, len);
4369 } while (buflen != 0);
4375 __free_page(spages[rc-1]);
4379 struct nfs4_cached_acl {
4385 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4387 struct nfs_inode *nfsi = NFS_I(inode);
4389 spin_lock(&inode->i_lock);
4390 kfree(nfsi->nfs4_acl);
4391 nfsi->nfs4_acl = acl;
4392 spin_unlock(&inode->i_lock);
4395 static void nfs4_zap_acl_attr(struct inode *inode)
4397 nfs4_set_cached_acl(inode, NULL);
4400 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4402 struct nfs_inode *nfsi = NFS_I(inode);
4403 struct nfs4_cached_acl *acl;
4406 spin_lock(&inode->i_lock);
4407 acl = nfsi->nfs4_acl;
4410 if (buf == NULL) /* user is just asking for length */
4412 if (acl->cached == 0)
4414 ret = -ERANGE; /* see getxattr(2) man page */
4415 if (acl->len > buflen)
4417 memcpy(buf, acl->data, acl->len);
4421 spin_unlock(&inode->i_lock);
4425 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4427 struct nfs4_cached_acl *acl;
4428 size_t buflen = sizeof(*acl) + acl_len;
4430 if (buflen <= PAGE_SIZE) {
4431 acl = kmalloc(buflen, GFP_KERNEL);
4435 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4437 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4444 nfs4_set_cached_acl(inode, acl);
4448 * The getxattr API returns the required buffer length when called with a
4449 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4450 * the required buf. On a NULL buf, we send a page of data to the server
4451 * guessing that the ACL request can be serviced by a page. If so, we cache
4452 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4453 * the cache. If not so, we throw away the page, and cache the required
4454 * length. The next getxattr call will then produce another round trip to
4455 * the server, this time with the input buf of the required size.
4457 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4459 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4460 struct nfs_getaclargs args = {
4461 .fh = NFS_FH(inode),
4465 struct nfs_getaclres res = {
4468 struct rpc_message msg = {
4469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4473 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4474 int ret = -ENOMEM, i;
4476 /* As long as we're doing a round trip to the server anyway,
4477 * let's be prepared for a page of acl data. */
4480 if (npages > ARRAY_SIZE(pages))
4483 for (i = 0; i < npages; i++) {
4484 pages[i] = alloc_page(GFP_KERNEL);
4489 /* for decoding across pages */
4490 res.acl_scratch = alloc_page(GFP_KERNEL);
4491 if (!res.acl_scratch)
4494 args.acl_len = npages * PAGE_SIZE;
4495 args.acl_pgbase = 0;
4497 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4498 __func__, buf, buflen, npages, args.acl_len);
4499 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4500 &msg, &args.seq_args, &res.seq_res, 0);
4504 /* Handle the case where the passed-in buffer is too short */
4505 if (res.acl_flags & NFS4_ACL_TRUNC) {
4506 /* Did the user only issue a request for the acl length? */
4512 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4514 if (res.acl_len > buflen) {
4518 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4523 for (i = 0; i < npages; i++)
4525 __free_page(pages[i]);
4526 if (res.acl_scratch)
4527 __free_page(res.acl_scratch);
4531 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4533 struct nfs4_exception exception = { };
4536 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4537 trace_nfs4_get_acl(inode, ret);
4540 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4541 } while (exception.retry);
4545 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4547 struct nfs_server *server = NFS_SERVER(inode);
4550 if (!nfs4_server_supports_acls(server))
4552 ret = nfs_revalidate_inode(server, inode);
4555 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4556 nfs_zap_acl_cache(inode);
4557 ret = nfs4_read_cached_acl(inode, buf, buflen);
4559 /* -ENOENT is returned if there is no ACL or if there is an ACL
4560 * but no cached acl data, just the acl length */
4562 return nfs4_get_acl_uncached(inode, buf, buflen);
4565 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4567 struct nfs_server *server = NFS_SERVER(inode);
4568 struct page *pages[NFS4ACL_MAXPAGES];
4569 struct nfs_setaclargs arg = {
4570 .fh = NFS_FH(inode),
4574 struct nfs_setaclres res;
4575 struct rpc_message msg = {
4576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4580 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4583 if (!nfs4_server_supports_acls(server))
4585 if (npages > ARRAY_SIZE(pages))
4587 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4590 nfs4_inode_return_delegation(inode);
4591 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4594 * Free each page after tx, so the only ref left is
4595 * held by the network stack
4598 put_page(pages[i-1]);
4601 * Acl update can result in inode attribute update.
4602 * so mark the attribute cache invalid.
4604 spin_lock(&inode->i_lock);
4605 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4606 spin_unlock(&inode->i_lock);
4607 nfs_access_zap_cache(inode);
4608 nfs_zap_acl_cache(inode);
4612 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4614 struct nfs4_exception exception = { };
4617 err = __nfs4_proc_set_acl(inode, buf, buflen);
4618 trace_nfs4_set_acl(inode, err);
4619 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4621 } while (exception.retry);
4625 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4626 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4629 struct nfs_server *server = NFS_SERVER(inode);
4630 struct nfs_fattr fattr;
4631 struct nfs4_label label = {0, 0, buflen, buf};
4633 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4634 struct nfs4_getattr_arg arg = {
4635 .fh = NFS_FH(inode),
4638 struct nfs4_getattr_res res = {
4643 struct rpc_message msg = {
4644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4650 nfs_fattr_init(&fattr);
4652 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4655 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4657 if (buflen < label.len)
4662 static int nfs4_get_security_label(struct inode *inode, void *buf,
4665 struct nfs4_exception exception = { };
4668 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4672 err = _nfs4_get_security_label(inode, buf, buflen);
4673 trace_nfs4_get_security_label(inode, err);
4674 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4676 } while (exception.retry);
4680 static int _nfs4_do_set_security_label(struct inode *inode,
4681 struct nfs4_label *ilabel,
4682 struct nfs_fattr *fattr,
4683 struct nfs4_label *olabel)
4686 struct iattr sattr = {0};
4687 struct nfs_server *server = NFS_SERVER(inode);
4688 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4689 struct nfs_setattrargs arg = {
4690 .fh = NFS_FH(inode),
4696 struct nfs_setattrres res = {
4701 struct rpc_message msg = {
4702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4708 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4710 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4712 dprintk("%s failed: %d\n", __func__, status);
4717 static int nfs4_do_set_security_label(struct inode *inode,
4718 struct nfs4_label *ilabel,
4719 struct nfs_fattr *fattr,
4720 struct nfs4_label *olabel)
4722 struct nfs4_exception exception = { };
4726 err = _nfs4_do_set_security_label(inode, ilabel,
4728 trace_nfs4_set_security_label(inode, err);
4729 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4731 } while (exception.retry);
4736 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4738 struct nfs4_label ilabel, *olabel = NULL;
4739 struct nfs_fattr fattr;
4740 struct rpc_cred *cred;
4741 struct inode *inode = dentry->d_inode;
4744 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4747 nfs_fattr_init(&fattr);
4751 ilabel.label = (char *)buf;
4752 ilabel.len = buflen;
4754 cred = rpc_lookup_cred();
4756 return PTR_ERR(cred);
4758 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4759 if (IS_ERR(olabel)) {
4760 status = -PTR_ERR(olabel);
4764 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4766 nfs_setsecurity(inode, &fattr, olabel);
4768 nfs4_label_free(olabel);
4773 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4777 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4779 struct nfs_client *clp = server->nfs_client;
4781 if (task->tk_status >= 0)
4783 switch(task->tk_status) {
4784 case -NFS4ERR_DELEG_REVOKED:
4785 case -NFS4ERR_ADMIN_REVOKED:
4786 case -NFS4ERR_BAD_STATEID:
4789 nfs_remove_bad_delegation(state->inode);
4790 case -NFS4ERR_OPENMODE:
4793 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4794 goto recovery_failed;
4795 goto wait_on_recovery;
4796 case -NFS4ERR_EXPIRED:
4797 if (state != NULL) {
4798 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4799 goto recovery_failed;
4801 case -NFS4ERR_STALE_STATEID:
4802 case -NFS4ERR_STALE_CLIENTID:
4803 nfs4_schedule_lease_recovery(clp);
4804 goto wait_on_recovery;
4805 case -NFS4ERR_MOVED:
4806 if (nfs4_schedule_migration_recovery(server) < 0)
4807 goto recovery_failed;
4808 goto wait_on_recovery;
4809 case -NFS4ERR_LEASE_MOVED:
4810 nfs4_schedule_lease_moved_recovery(clp);
4811 goto wait_on_recovery;
4812 #if defined(CONFIG_NFS_V4_1)
4813 case -NFS4ERR_BADSESSION:
4814 case -NFS4ERR_BADSLOT:
4815 case -NFS4ERR_BAD_HIGH_SLOT:
4816 case -NFS4ERR_DEADSESSION:
4817 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4818 case -NFS4ERR_SEQ_FALSE_RETRY:
4819 case -NFS4ERR_SEQ_MISORDERED:
4820 dprintk("%s ERROR %d, Reset session\n", __func__,
4822 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4823 goto wait_on_recovery;
4824 #endif /* CONFIG_NFS_V4_1 */
4825 case -NFS4ERR_DELAY:
4826 nfs_inc_server_stats(server, NFSIOS_DELAY);
4827 case -NFS4ERR_GRACE:
4828 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4829 case -NFS4ERR_RETRY_UNCACHED_REP:
4830 case -NFS4ERR_OLD_STATEID:
4833 task->tk_status = nfs4_map_errors(task->tk_status);
4836 task->tk_status = -EIO;
4839 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4840 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4841 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4842 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4843 goto recovery_failed;
4845 task->tk_status = 0;
4849 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4850 nfs4_verifier *bootverf)
4854 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4855 /* An impossible timestamp guarantees this value
4856 * will never match a generated boot time. */
4858 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4860 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4861 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4862 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4864 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4868 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4869 char *buf, size_t len)
4871 unsigned int result;
4874 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4876 rpc_peeraddr2str(clp->cl_rpcclient,
4878 rpc_peeraddr2str(clp->cl_rpcclient,
4879 RPC_DISPLAY_PROTO));
4885 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4886 char *buf, size_t len)
4888 const char *nodename = clp->cl_rpcclient->cl_nodename;
4890 if (nfs4_client_id_uniquifier[0] != '\0')
4891 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4892 clp->rpc_ops->version,
4893 clp->cl_minorversion,
4894 nfs4_client_id_uniquifier,
4896 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4897 clp->rpc_ops->version, clp->cl_minorversion,
4902 * nfs4_proc_setclientid - Negotiate client ID
4903 * @clp: state data structure
4904 * @program: RPC program for NFSv4 callback service
4905 * @port: IP port number for NFS4 callback service
4906 * @cred: RPC credential to use for this call
4907 * @res: where to place the result
4909 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4911 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4912 unsigned short port, struct rpc_cred *cred,
4913 struct nfs4_setclientid_res *res)
4915 nfs4_verifier sc_verifier;
4916 struct nfs4_setclientid setclientid = {
4917 .sc_verifier = &sc_verifier,
4919 .sc_cb_ident = clp->cl_cb_ident,
4921 struct rpc_message msg = {
4922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4923 .rpc_argp = &setclientid,
4929 /* nfs_client_id4 */
4930 nfs4_init_boot_verifier(clp, &sc_verifier);
4931 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4932 setclientid.sc_name_len =
4933 nfs4_init_uniform_client_string(clp,
4934 setclientid.sc_name,
4935 sizeof(setclientid.sc_name));
4937 setclientid.sc_name_len =
4938 nfs4_init_nonuniform_client_string(clp,
4939 setclientid.sc_name,
4940 sizeof(setclientid.sc_name));
4943 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4944 sizeof(setclientid.sc_netid), "%s",
4945 rpc_peeraddr2str(clp->cl_rpcclient,
4946 RPC_DISPLAY_NETID));
4948 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4949 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4950 clp->cl_ipaddr, port >> 8, port & 255);
4952 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4953 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4954 setclientid.sc_name_len, setclientid.sc_name);
4955 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4956 trace_nfs4_setclientid(clp, status);
4957 dprintk("NFS reply setclientid: %d\n", status);
4962 * nfs4_proc_setclientid_confirm - Confirm client ID
4963 * @clp: state data structure
4964 * @res: result of a previous SETCLIENTID
4965 * @cred: RPC credential to use for this call
4967 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4969 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4970 struct nfs4_setclientid_res *arg,
4971 struct rpc_cred *cred)
4973 struct rpc_message msg = {
4974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4980 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4981 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4983 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4984 trace_nfs4_setclientid_confirm(clp, status);
4985 dprintk("NFS reply setclientid_confirm: %d\n", status);
4989 struct nfs4_delegreturndata {
4990 struct nfs4_delegreturnargs args;
4991 struct nfs4_delegreturnres res;
4993 nfs4_stateid stateid;
4994 unsigned long timestamp;
4995 struct nfs_fattr fattr;
4999 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5001 struct nfs4_delegreturndata *data = calldata;
5003 if (!nfs4_sequence_done(task, &data->res.seq_res))
5006 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5007 switch (task->tk_status) {
5009 renew_lease(data->res.server, data->timestamp);
5011 case -NFS4ERR_ADMIN_REVOKED:
5012 case -NFS4ERR_DELEG_REVOKED:
5013 case -NFS4ERR_BAD_STATEID:
5014 case -NFS4ERR_OLD_STATEID:
5015 case -NFS4ERR_STALE_STATEID:
5016 case -NFS4ERR_EXPIRED:
5017 task->tk_status = 0;
5020 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5022 rpc_restart_call_prepare(task);
5026 data->rpc_status = task->tk_status;
5029 static void nfs4_delegreturn_release(void *calldata)
5034 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5036 struct nfs4_delegreturndata *d_data;
5038 d_data = (struct nfs4_delegreturndata *)data;
5040 nfs4_setup_sequence(d_data->res.server,
5041 &d_data->args.seq_args,
5042 &d_data->res.seq_res,
5046 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5047 .rpc_call_prepare = nfs4_delegreturn_prepare,
5048 .rpc_call_done = nfs4_delegreturn_done,
5049 .rpc_release = nfs4_delegreturn_release,
5052 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5054 struct nfs4_delegreturndata *data;
5055 struct nfs_server *server = NFS_SERVER(inode);
5056 struct rpc_task *task;
5057 struct rpc_message msg = {
5058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5061 struct rpc_task_setup task_setup_data = {
5062 .rpc_client = server->client,
5063 .rpc_message = &msg,
5064 .callback_ops = &nfs4_delegreturn_ops,
5065 .flags = RPC_TASK_ASYNC,
5069 data = kzalloc(sizeof(*data), GFP_NOFS);
5072 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5073 data->args.fhandle = &data->fh;
5074 data->args.stateid = &data->stateid;
5075 data->args.bitmask = server->cache_consistency_bitmask;
5076 nfs_copy_fh(&data->fh, NFS_FH(inode));
5077 nfs4_stateid_copy(&data->stateid, stateid);
5078 data->res.fattr = &data->fattr;
5079 data->res.server = server;
5080 nfs_fattr_init(data->res.fattr);
5081 data->timestamp = jiffies;
5082 data->rpc_status = 0;
5084 task_setup_data.callback_data = data;
5085 msg.rpc_argp = &data->args;
5086 msg.rpc_resp = &data->res;
5087 task = rpc_run_task(&task_setup_data);
5089 return PTR_ERR(task);
5092 status = nfs4_wait_for_completion_rpc_task(task);
5095 status = data->rpc_status;
5097 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5099 nfs_refresh_inode(inode, &data->fattr);
5105 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5107 struct nfs_server *server = NFS_SERVER(inode);
5108 struct nfs4_exception exception = { };
5111 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5112 trace_nfs4_delegreturn(inode, err);
5114 case -NFS4ERR_STALE_STATEID:
5115 case -NFS4ERR_EXPIRED:
5119 err = nfs4_handle_exception(server, err, &exception);
5120 } while (exception.retry);
5124 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5125 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5128 * sleep, with exponential backoff, and retry the LOCK operation.
5130 static unsigned long
5131 nfs4_set_lock_task_retry(unsigned long timeout)
5133 freezable_schedule_timeout_killable_unsafe(timeout);
5135 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5136 return NFS4_LOCK_MAXTIMEOUT;
5140 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5142 struct inode *inode = state->inode;
5143 struct nfs_server *server = NFS_SERVER(inode);
5144 struct nfs_client *clp = server->nfs_client;
5145 struct nfs_lockt_args arg = {
5146 .fh = NFS_FH(inode),
5149 struct nfs_lockt_res res = {
5152 struct rpc_message msg = {
5153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5156 .rpc_cred = state->owner->so_cred,
5158 struct nfs4_lock_state *lsp;
5161 arg.lock_owner.clientid = clp->cl_clientid;
5162 status = nfs4_set_lock_state(state, request);
5165 lsp = request->fl_u.nfs4_fl.owner;
5166 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5167 arg.lock_owner.s_dev = server->s_dev;
5168 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5171 request->fl_type = F_UNLCK;
5173 case -NFS4ERR_DENIED:
5176 request->fl_ops->fl_release_private(request);
5177 request->fl_ops = NULL;
5182 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5184 struct nfs4_exception exception = { };
5188 err = _nfs4_proc_getlk(state, cmd, request);
5189 trace_nfs4_get_lock(request, state, cmd, err);
5190 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5192 } while (exception.retry);
5196 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5199 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5201 res = posix_lock_file_wait(file, fl);
5204 res = flock_lock_file_wait(file, fl);
5212 struct nfs4_unlockdata {
5213 struct nfs_locku_args arg;
5214 struct nfs_locku_res res;
5215 struct nfs4_lock_state *lsp;
5216 struct nfs_open_context *ctx;
5217 struct file_lock fl;
5218 const struct nfs_server *server;
5219 unsigned long timestamp;
5222 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5223 struct nfs_open_context *ctx,
5224 struct nfs4_lock_state *lsp,
5225 struct nfs_seqid *seqid)
5227 struct nfs4_unlockdata *p;
5228 struct inode *inode = lsp->ls_state->inode;
5230 p = kzalloc(sizeof(*p), GFP_NOFS);
5233 p->arg.fh = NFS_FH(inode);
5235 p->arg.seqid = seqid;
5236 p->res.seqid = seqid;
5237 p->arg.stateid = &lsp->ls_stateid;
5239 atomic_inc(&lsp->ls_count);
5240 /* Ensure we don't close file until we're done freeing locks! */
5241 p->ctx = get_nfs_open_context(ctx);
5242 memcpy(&p->fl, fl, sizeof(p->fl));
5243 p->server = NFS_SERVER(inode);
5247 static void nfs4_locku_release_calldata(void *data)
5249 struct nfs4_unlockdata *calldata = data;
5250 nfs_free_seqid(calldata->arg.seqid);
5251 nfs4_put_lock_state(calldata->lsp);
5252 put_nfs_open_context(calldata->ctx);
5256 static void nfs4_locku_done(struct rpc_task *task, void *data)
5258 struct nfs4_unlockdata *calldata = data;
5260 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5262 switch (task->tk_status) {
5264 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5265 &calldata->res.stateid);
5266 renew_lease(calldata->server, calldata->timestamp);
5268 case -NFS4ERR_BAD_STATEID:
5269 case -NFS4ERR_OLD_STATEID:
5270 case -NFS4ERR_STALE_STATEID:
5271 case -NFS4ERR_EXPIRED:
5274 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
5275 rpc_restart_call_prepare(task);
5277 nfs_release_seqid(calldata->arg.seqid);
5280 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5282 struct nfs4_unlockdata *calldata = data;
5284 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5286 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5287 /* Note: exit _without_ running nfs4_locku_done */
5290 calldata->timestamp = jiffies;
5291 if (nfs4_setup_sequence(calldata->server,
5292 &calldata->arg.seq_args,
5293 &calldata->res.seq_res,
5295 nfs_release_seqid(calldata->arg.seqid);
5298 task->tk_action = NULL;
5300 nfs4_sequence_done(task, &calldata->res.seq_res);
5303 static const struct rpc_call_ops nfs4_locku_ops = {
5304 .rpc_call_prepare = nfs4_locku_prepare,
5305 .rpc_call_done = nfs4_locku_done,
5306 .rpc_release = nfs4_locku_release_calldata,
5309 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5310 struct nfs_open_context *ctx,
5311 struct nfs4_lock_state *lsp,
5312 struct nfs_seqid *seqid)
5314 struct nfs4_unlockdata *data;
5315 struct rpc_message msg = {
5316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5317 .rpc_cred = ctx->cred,
5319 struct rpc_task_setup task_setup_data = {
5320 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5321 .rpc_message = &msg,
5322 .callback_ops = &nfs4_locku_ops,
5323 .workqueue = nfsiod_workqueue,
5324 .flags = RPC_TASK_ASYNC,
5327 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5328 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5330 /* Ensure this is an unlock - when canceling a lock, the
5331 * canceled lock is passed in, and it won't be an unlock.
5333 fl->fl_type = F_UNLCK;
5335 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5337 nfs_free_seqid(seqid);
5338 return ERR_PTR(-ENOMEM);
5341 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5342 msg.rpc_argp = &data->arg;
5343 msg.rpc_resp = &data->res;
5344 task_setup_data.callback_data = data;
5345 return rpc_run_task(&task_setup_data);
5348 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5350 struct inode *inode = state->inode;
5351 struct nfs4_state_owner *sp = state->owner;
5352 struct nfs_inode *nfsi = NFS_I(inode);
5353 struct nfs_seqid *seqid;
5354 struct nfs4_lock_state *lsp;
5355 struct rpc_task *task;
5357 unsigned char fl_flags = request->fl_flags;
5359 status = nfs4_set_lock_state(state, request);
5360 /* Unlock _before_ we do the RPC call */
5361 request->fl_flags |= FL_EXISTS;
5362 /* Exclude nfs_delegation_claim_locks() */
5363 mutex_lock(&sp->so_delegreturn_mutex);
5364 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5365 down_read(&nfsi->rwsem);
5366 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5367 up_read(&nfsi->rwsem);
5368 mutex_unlock(&sp->so_delegreturn_mutex);
5371 up_read(&nfsi->rwsem);
5372 mutex_unlock(&sp->so_delegreturn_mutex);
5375 /* Is this a delegated lock? */
5376 lsp = request->fl_u.nfs4_fl.owner;
5377 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5379 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5383 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5384 status = PTR_ERR(task);
5387 status = nfs4_wait_for_completion_rpc_task(task);
5390 request->fl_flags = fl_flags;
5391 trace_nfs4_unlock(request, state, F_SETLK, status);
5395 struct nfs4_lockdata {
5396 struct nfs_lock_args arg;
5397 struct nfs_lock_res res;
5398 struct nfs4_lock_state *lsp;
5399 struct nfs_open_context *ctx;
5400 struct file_lock fl;
5401 unsigned long timestamp;
5404 struct nfs_server *server;
5407 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5408 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5411 struct nfs4_lockdata *p;
5412 struct inode *inode = lsp->ls_state->inode;
5413 struct nfs_server *server = NFS_SERVER(inode);
5415 p = kzalloc(sizeof(*p), gfp_mask);
5419 p->arg.fh = NFS_FH(inode);
5421 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5422 if (p->arg.open_seqid == NULL)
5424 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5425 if (p->arg.lock_seqid == NULL)
5426 goto out_free_seqid;
5427 p->arg.lock_stateid = &lsp->ls_stateid;
5428 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5429 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5430 p->arg.lock_owner.s_dev = server->s_dev;
5431 p->res.lock_seqid = p->arg.lock_seqid;
5434 atomic_inc(&lsp->ls_count);
5435 p->ctx = get_nfs_open_context(ctx);
5436 memcpy(&p->fl, fl, sizeof(p->fl));
5439 nfs_free_seqid(p->arg.open_seqid);
5445 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5447 struct nfs4_lockdata *data = calldata;
5448 struct nfs4_state *state = data->lsp->ls_state;
5450 dprintk("%s: begin!\n", __func__);
5451 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5453 /* Do we need to do an open_to_lock_owner? */
5454 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5455 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5456 goto out_release_lock_seqid;
5458 data->arg.open_stateid = &state->open_stateid;
5459 data->arg.new_lock_owner = 1;
5460 data->res.open_seqid = data->arg.open_seqid;
5462 data->arg.new_lock_owner = 0;
5463 if (!nfs4_valid_open_stateid(state)) {
5464 data->rpc_status = -EBADF;
5465 task->tk_action = NULL;
5466 goto out_release_open_seqid;
5468 data->timestamp = jiffies;
5469 if (nfs4_setup_sequence(data->server,
5470 &data->arg.seq_args,
5474 out_release_open_seqid:
5475 nfs_release_seqid(data->arg.open_seqid);
5476 out_release_lock_seqid:
5477 nfs_release_seqid(data->arg.lock_seqid);
5479 nfs4_sequence_done(task, &data->res.seq_res);
5480 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5483 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5485 struct nfs4_lockdata *data = calldata;
5487 dprintk("%s: begin!\n", __func__);
5489 if (!nfs4_sequence_done(task, &data->res.seq_res))
5492 data->rpc_status = task->tk_status;
5493 if (data->arg.new_lock_owner != 0) {
5494 if (data->rpc_status == 0)
5495 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5499 if (data->rpc_status == 0) {
5500 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5501 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5502 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5505 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5508 static void nfs4_lock_release(void *calldata)
5510 struct nfs4_lockdata *data = calldata;
5512 dprintk("%s: begin!\n", __func__);
5513 nfs_free_seqid(data->arg.open_seqid);
5514 if (data->cancelled != 0) {
5515 struct rpc_task *task;
5516 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5517 data->arg.lock_seqid);
5519 rpc_put_task_async(task);
5520 dprintk("%s: cancelling lock!\n", __func__);
5522 nfs_free_seqid(data->arg.lock_seqid);
5523 nfs4_put_lock_state(data->lsp);
5524 put_nfs_open_context(data->ctx);
5526 dprintk("%s: done!\n", __func__);
5529 static const struct rpc_call_ops nfs4_lock_ops = {
5530 .rpc_call_prepare = nfs4_lock_prepare,
5531 .rpc_call_done = nfs4_lock_done,
5532 .rpc_release = nfs4_lock_release,
5535 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5538 case -NFS4ERR_ADMIN_REVOKED:
5539 case -NFS4ERR_BAD_STATEID:
5540 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5541 if (new_lock_owner != 0 ||
5542 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5543 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5545 case -NFS4ERR_STALE_STATEID:
5546 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5547 case -NFS4ERR_EXPIRED:
5548 nfs4_schedule_lease_recovery(server->nfs_client);
5552 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5554 struct nfs4_lockdata *data;
5555 struct rpc_task *task;
5556 struct rpc_message msg = {
5557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5558 .rpc_cred = state->owner->so_cred,
5560 struct rpc_task_setup task_setup_data = {
5561 .rpc_client = NFS_CLIENT(state->inode),
5562 .rpc_message = &msg,
5563 .callback_ops = &nfs4_lock_ops,
5564 .workqueue = nfsiod_workqueue,
5565 .flags = RPC_TASK_ASYNC,
5569 dprintk("%s: begin!\n", __func__);
5570 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5571 fl->fl_u.nfs4_fl.owner,
5572 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5576 data->arg.block = 1;
5577 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5578 msg.rpc_argp = &data->arg;
5579 msg.rpc_resp = &data->res;
5580 task_setup_data.callback_data = data;
5581 if (recovery_type > NFS_LOCK_NEW) {
5582 if (recovery_type == NFS_LOCK_RECLAIM)
5583 data->arg.reclaim = NFS_LOCK_RECLAIM;
5584 nfs4_set_sequence_privileged(&data->arg.seq_args);
5586 task = rpc_run_task(&task_setup_data);
5588 return PTR_ERR(task);
5589 ret = nfs4_wait_for_completion_rpc_task(task);
5591 ret = data->rpc_status;
5593 nfs4_handle_setlk_error(data->server, data->lsp,
5594 data->arg.new_lock_owner, ret);
5596 data->cancelled = 1;
5598 dprintk("%s: done, ret = %d!\n", __func__, ret);
5602 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5604 struct nfs_server *server = NFS_SERVER(state->inode);
5605 struct nfs4_exception exception = {
5606 .inode = state->inode,
5611 /* Cache the lock if possible... */
5612 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5614 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5615 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5616 if (err != -NFS4ERR_DELAY)
5618 nfs4_handle_exception(server, err, &exception);
5619 } while (exception.retry);
5623 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5625 struct nfs_server *server = NFS_SERVER(state->inode);
5626 struct nfs4_exception exception = {
5627 .inode = state->inode,
5631 err = nfs4_set_lock_state(state, request);
5634 if (!recover_lost_locks) {
5635 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5639 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5641 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5642 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5646 case -NFS4ERR_GRACE:
5647 case -NFS4ERR_DELAY:
5648 nfs4_handle_exception(server, err, &exception);
5651 } while (exception.retry);
5656 #if defined(CONFIG_NFS_V4_1)
5658 * nfs41_check_expired_locks - possibly free a lock stateid
5660 * @state: NFSv4 state for an inode
5662 * Returns NFS_OK if recovery for this stateid is now finished.
5663 * Otherwise a negative NFS4ERR value is returned.
5665 static int nfs41_check_expired_locks(struct nfs4_state *state)
5667 int status, ret = -NFS4ERR_BAD_STATEID;
5668 struct nfs4_lock_state *lsp;
5669 struct nfs_server *server = NFS_SERVER(state->inode);
5671 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5672 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5673 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5675 status = nfs41_test_stateid(server,
5678 trace_nfs4_test_lock_stateid(state, lsp, status);
5679 if (status != NFS_OK) {
5680 /* Free the stateid unless the server
5681 * informs us the stateid is unrecognized. */
5682 if (status != -NFS4ERR_BAD_STATEID)
5683 nfs41_free_stateid(server,
5686 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5695 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5697 int status = NFS_OK;
5699 if (test_bit(LK_STATE_IN_USE, &state->flags))
5700 status = nfs41_check_expired_locks(state);
5701 if (status != NFS_OK)
5702 status = nfs4_lock_expired(state, request);
5707 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5709 struct nfs4_state_owner *sp = state->owner;
5710 struct nfs_inode *nfsi = NFS_I(state->inode);
5711 unsigned char fl_flags = request->fl_flags;
5713 int status = -ENOLCK;
5715 if ((fl_flags & FL_POSIX) &&
5716 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5718 /* Is this a delegated open? */
5719 status = nfs4_set_lock_state(state, request);
5722 request->fl_flags |= FL_ACCESS;
5723 status = do_vfs_lock(request->fl_file, request);
5726 down_read(&nfsi->rwsem);
5727 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5728 /* Yes: cache locks! */
5729 /* ...but avoid races with delegation recall... */
5730 request->fl_flags = fl_flags & ~FL_SLEEP;
5731 status = do_vfs_lock(request->fl_file, request);
5734 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5735 up_read(&nfsi->rwsem);
5736 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5739 down_read(&nfsi->rwsem);
5740 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5741 status = -NFS4ERR_DELAY;
5744 /* Note: we always want to sleep here! */
5745 request->fl_flags = fl_flags | FL_SLEEP;
5746 if (do_vfs_lock(request->fl_file, request) < 0)
5747 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5748 "manager!\n", __func__);
5750 up_read(&nfsi->rwsem);
5752 request->fl_flags = fl_flags;
5756 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5758 struct nfs4_exception exception = {
5760 .inode = state->inode,
5765 err = _nfs4_proc_setlk(state, cmd, request);
5766 trace_nfs4_set_lock(request, state, cmd, err);
5767 if (err == -NFS4ERR_DENIED)
5769 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5771 } while (exception.retry);
5776 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5778 struct nfs_open_context *ctx;
5779 struct nfs4_state *state;
5780 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5783 /* verify open state */
5784 ctx = nfs_file_open_context(filp);
5787 if (request->fl_start < 0 || request->fl_end < 0)
5790 if (IS_GETLK(cmd)) {
5792 return nfs4_proc_getlk(state, F_GETLK, request);
5796 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5799 if (request->fl_type == F_UNLCK) {
5801 return nfs4_proc_unlck(state, cmd, request);
5808 * Don't rely on the VFS having checked the file open mode,
5809 * since it won't do this for flock() locks.
5811 switch (request->fl_type) {
5813 if (!(filp->f_mode & FMODE_READ))
5817 if (!(filp->f_mode & FMODE_WRITE))
5822 status = nfs4_proc_setlk(state, cmd, request);
5823 if ((status != -EAGAIN) || IS_SETLK(cmd))
5825 timeout = nfs4_set_lock_task_retry(timeout);
5826 status = -ERESTARTSYS;
5829 } while(status < 0);
5833 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5835 struct nfs_server *server = NFS_SERVER(state->inode);
5838 err = nfs4_set_lock_state(state, fl);
5841 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5842 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5845 struct nfs_release_lockowner_data {
5846 struct nfs4_lock_state *lsp;
5847 struct nfs_server *server;
5848 struct nfs_release_lockowner_args args;
5849 struct nfs4_sequence_args seq_args;
5850 struct nfs4_sequence_res seq_res;
5851 unsigned long timestamp;
5854 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5856 struct nfs_release_lockowner_data *data = calldata;
5857 nfs40_setup_sequence(data->server,
5858 &data->seq_args, &data->seq_res, task);
5859 data->timestamp = jiffies;
5862 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5864 struct nfs_release_lockowner_data *data = calldata;
5865 struct nfs_server *server = data->server;
5867 nfs40_sequence_done(task, &data->seq_res);
5869 switch (task->tk_status) {
5871 renew_lease(server, data->timestamp);
5873 case -NFS4ERR_STALE_CLIENTID:
5874 case -NFS4ERR_EXPIRED:
5875 case -NFS4ERR_LEASE_MOVED:
5876 case -NFS4ERR_DELAY:
5877 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5878 rpc_restart_call_prepare(task);
5882 static void nfs4_release_lockowner_release(void *calldata)
5884 struct nfs_release_lockowner_data *data = calldata;
5885 nfs4_free_lock_state(data->server, data->lsp);
5889 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5890 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5891 .rpc_call_done = nfs4_release_lockowner_done,
5892 .rpc_release = nfs4_release_lockowner_release,
5895 static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
5897 struct nfs_release_lockowner_data *data;
5898 struct rpc_message msg = {
5899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5902 if (server->nfs_client->cl_mvops->minor_version != 0)
5905 data = kmalloc(sizeof(*data), GFP_NOFS);
5908 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
5910 data->server = server;
5911 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5912 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5913 data->args.lock_owner.s_dev = server->s_dev;
5915 msg.rpc_argp = &data->args;
5916 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5920 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5922 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5923 const void *buf, size_t buflen,
5924 int flags, int type)
5926 if (strcmp(key, "") != 0)
5929 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5932 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5933 void *buf, size_t buflen, int type)
5935 if (strcmp(key, "") != 0)
5938 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5941 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5942 size_t list_len, const char *name,
5943 size_t name_len, int type)
5945 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5947 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5950 if (list && len <= list_len)
5951 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5955 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5956 static inline int nfs4_server_supports_labels(struct nfs_server *server)
5958 return server->caps & NFS_CAP_SECURITY_LABEL;
5961 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5962 const void *buf, size_t buflen,
5963 int flags, int type)
5965 if (security_ismaclabel(key))
5966 return nfs4_set_security_label(dentry, buf, buflen);
5971 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5972 void *buf, size_t buflen, int type)
5974 if (security_ismaclabel(key))
5975 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5979 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5980 size_t list_len, const char *name,
5981 size_t name_len, int type)
5985 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5986 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5987 if (list && len <= list_len)
5988 security_inode_listsecurity(dentry->d_inode, list, len);
5993 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5994 .prefix = XATTR_SECURITY_PREFIX,
5995 .list = nfs4_xattr_list_nfs4_label,
5996 .get = nfs4_xattr_get_nfs4_label,
5997 .set = nfs4_xattr_set_nfs4_label,
6003 * nfs_fhget will use either the mounted_on_fileid or the fileid
6005 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6007 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6008 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6009 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6010 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6013 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6014 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6015 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6019 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6020 const struct qstr *name,
6021 struct nfs4_fs_locations *fs_locations,
6024 struct nfs_server *server = NFS_SERVER(dir);
6026 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6028 struct nfs4_fs_locations_arg args = {
6029 .dir_fh = NFS_FH(dir),
6034 struct nfs4_fs_locations_res res = {
6035 .fs_locations = fs_locations,
6037 struct rpc_message msg = {
6038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6044 dprintk("%s: start\n", __func__);
6046 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6047 * is not supported */
6048 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6049 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6051 bitmask[0] |= FATTR4_WORD0_FILEID;
6053 nfs_fattr_init(&fs_locations->fattr);
6054 fs_locations->server = server;
6055 fs_locations->nlocations = 0;
6056 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6057 dprintk("%s: returned status = %d\n", __func__, status);
6061 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6062 const struct qstr *name,
6063 struct nfs4_fs_locations *fs_locations,
6066 struct nfs4_exception exception = { };
6069 err = _nfs4_proc_fs_locations(client, dir, name,
6070 fs_locations, page);
6071 trace_nfs4_get_fs_locations(dir, name, err);
6072 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6074 } while (exception.retry);
6079 * This operation also signals the server that this client is
6080 * performing migration recovery. The server can stop returning
6081 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6082 * appended to this compound to identify the client ID which is
6083 * performing recovery.
6085 static int _nfs40_proc_get_locations(struct inode *inode,
6086 struct nfs4_fs_locations *locations,
6087 struct page *page, struct rpc_cred *cred)
6089 struct nfs_server *server = NFS_SERVER(inode);
6090 struct rpc_clnt *clnt = server->client;
6092 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6094 struct nfs4_fs_locations_arg args = {
6095 .clientid = server->nfs_client->cl_clientid,
6096 .fh = NFS_FH(inode),
6099 .migration = 1, /* skip LOOKUP */
6100 .renew = 1, /* append RENEW */
6102 struct nfs4_fs_locations_res res = {
6103 .fs_locations = locations,
6107 struct rpc_message msg = {
6108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6113 unsigned long now = jiffies;
6116 nfs_fattr_init(&locations->fattr);
6117 locations->server = server;
6118 locations->nlocations = 0;
6120 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6121 nfs4_set_sequence_privileged(&args.seq_args);
6122 status = nfs4_call_sync_sequence(clnt, server, &msg,
6123 &args.seq_args, &res.seq_res);
6127 renew_lease(server, now);
6131 #ifdef CONFIG_NFS_V4_1
6134 * This operation also signals the server that this client is
6135 * performing migration recovery. The server can stop asserting
6136 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6137 * performing this operation is identified in the SEQUENCE
6138 * operation in this compound.
6140 * When the client supports GETATTR(fs_locations_info), it can
6141 * be plumbed in here.
6143 static int _nfs41_proc_get_locations(struct inode *inode,
6144 struct nfs4_fs_locations *locations,
6145 struct page *page, struct rpc_cred *cred)
6147 struct nfs_server *server = NFS_SERVER(inode);
6148 struct rpc_clnt *clnt = server->client;
6150 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6152 struct nfs4_fs_locations_arg args = {
6153 .fh = NFS_FH(inode),
6156 .migration = 1, /* skip LOOKUP */
6158 struct nfs4_fs_locations_res res = {
6159 .fs_locations = locations,
6162 struct rpc_message msg = {
6163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6170 nfs_fattr_init(&locations->fattr);
6171 locations->server = server;
6172 locations->nlocations = 0;
6174 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6175 nfs4_set_sequence_privileged(&args.seq_args);
6176 status = nfs4_call_sync_sequence(clnt, server, &msg,
6177 &args.seq_args, &res.seq_res);
6178 if (status == NFS4_OK &&
6179 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6180 status = -NFS4ERR_LEASE_MOVED;
6184 #endif /* CONFIG_NFS_V4_1 */
6187 * nfs4_proc_get_locations - discover locations for a migrated FSID
6188 * @inode: inode on FSID that is migrating
6189 * @locations: result of query
6191 * @cred: credential to use for this operation
6193 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6194 * operation failed, or a negative errno if a local error occurred.
6196 * On success, "locations" is filled in, but if the server has
6197 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6200 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6201 * from this client that require migration recovery.
6203 int nfs4_proc_get_locations(struct inode *inode,
6204 struct nfs4_fs_locations *locations,
6205 struct page *page, struct rpc_cred *cred)
6207 struct nfs_server *server = NFS_SERVER(inode);
6208 struct nfs_client *clp = server->nfs_client;
6209 const struct nfs4_mig_recovery_ops *ops =
6210 clp->cl_mvops->mig_recovery_ops;
6211 struct nfs4_exception exception = { };
6214 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6215 (unsigned long long)server->fsid.major,
6216 (unsigned long long)server->fsid.minor,
6218 nfs_display_fhandle(NFS_FH(inode), __func__);
6221 status = ops->get_locations(inode, locations, page, cred);
6222 if (status != -NFS4ERR_DELAY)
6224 nfs4_handle_exception(server, status, &exception);
6225 } while (exception.retry);
6230 * This operation also signals the server that this client is
6231 * performing "lease moved" recovery. The server can stop
6232 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6233 * is appended to this compound to identify the client ID which is
6234 * performing recovery.
6236 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6238 struct nfs_server *server = NFS_SERVER(inode);
6239 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6240 struct rpc_clnt *clnt = server->client;
6241 struct nfs4_fsid_present_arg args = {
6242 .fh = NFS_FH(inode),
6243 .clientid = clp->cl_clientid,
6244 .renew = 1, /* append RENEW */
6246 struct nfs4_fsid_present_res res = {
6249 struct rpc_message msg = {
6250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6255 unsigned long now = jiffies;
6258 res.fh = nfs_alloc_fhandle();
6262 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6263 nfs4_set_sequence_privileged(&args.seq_args);
6264 status = nfs4_call_sync_sequence(clnt, server, &msg,
6265 &args.seq_args, &res.seq_res);
6266 nfs_free_fhandle(res.fh);
6270 do_renew_lease(clp, now);
6274 #ifdef CONFIG_NFS_V4_1
6277 * This operation also signals the server that this client is
6278 * performing "lease moved" recovery. The server can stop asserting
6279 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6280 * this operation is identified in the SEQUENCE operation in this
6283 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6285 struct nfs_server *server = NFS_SERVER(inode);
6286 struct rpc_clnt *clnt = server->client;
6287 struct nfs4_fsid_present_arg args = {
6288 .fh = NFS_FH(inode),
6290 struct nfs4_fsid_present_res res = {
6292 struct rpc_message msg = {
6293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6300 res.fh = nfs_alloc_fhandle();
6304 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6305 nfs4_set_sequence_privileged(&args.seq_args);
6306 status = nfs4_call_sync_sequence(clnt, server, &msg,
6307 &args.seq_args, &res.seq_res);
6308 nfs_free_fhandle(res.fh);
6309 if (status == NFS4_OK &&
6310 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6311 status = -NFS4ERR_LEASE_MOVED;
6315 #endif /* CONFIG_NFS_V4_1 */
6318 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6319 * @inode: inode on FSID to check
6320 * @cred: credential to use for this operation
6322 * Server indicates whether the FSID is present, moved, or not
6323 * recognized. This operation is necessary to clear a LEASE_MOVED
6324 * condition for this client ID.
6326 * Returns NFS4_OK if the FSID is present on this server,
6327 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6328 * NFS4ERR code if some error occurred on the server, or a
6329 * negative errno if a local failure occurred.
6331 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6333 struct nfs_server *server = NFS_SERVER(inode);
6334 struct nfs_client *clp = server->nfs_client;
6335 const struct nfs4_mig_recovery_ops *ops =
6336 clp->cl_mvops->mig_recovery_ops;
6337 struct nfs4_exception exception = { };
6340 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6341 (unsigned long long)server->fsid.major,
6342 (unsigned long long)server->fsid.minor,
6344 nfs_display_fhandle(NFS_FH(inode), __func__);
6347 status = ops->fsid_present(inode, cred);
6348 if (status != -NFS4ERR_DELAY)
6350 nfs4_handle_exception(server, status, &exception);
6351 } while (exception.retry);
6356 * If 'use_integrity' is true and the state managment nfs_client
6357 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6358 * and the machine credential as per RFC3530bis and RFC5661 Security
6359 * Considerations sections. Otherwise, just use the user cred with the
6360 * filesystem's rpc_client.
6362 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6365 struct nfs4_secinfo_arg args = {
6366 .dir_fh = NFS_FH(dir),
6369 struct nfs4_secinfo_res res = {
6372 struct rpc_message msg = {
6373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6377 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6378 struct rpc_cred *cred = NULL;
6380 if (use_integrity) {
6381 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6382 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6383 msg.rpc_cred = cred;
6386 dprintk("NFS call secinfo %s\n", name->name);
6388 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6389 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6391 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6393 dprintk("NFS reply secinfo: %d\n", status);
6401 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6402 struct nfs4_secinfo_flavors *flavors)
6404 struct nfs4_exception exception = { };
6407 err = -NFS4ERR_WRONGSEC;
6409 /* try to use integrity protection with machine cred */
6410 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6411 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6414 * if unable to use integrity protection, or SECINFO with
6415 * integrity protection returns NFS4ERR_WRONGSEC (which is
6416 * disallowed by spec, but exists in deployed servers) use
6417 * the current filesystem's rpc_client and the user cred.
6419 if (err == -NFS4ERR_WRONGSEC)
6420 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6422 trace_nfs4_secinfo(dir, name, err);
6423 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6425 } while (exception.retry);
6429 #ifdef CONFIG_NFS_V4_1
6431 * Check the exchange flags returned by the server for invalid flags, having
6432 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6435 static int nfs4_check_cl_exchange_flags(u32 flags)
6437 if (flags & ~EXCHGID4_FLAG_MASK_R)
6439 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6440 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6442 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6446 return -NFS4ERR_INVAL;
6450 nfs41_same_server_scope(struct nfs41_server_scope *a,
6451 struct nfs41_server_scope *b)
6453 if (a->server_scope_sz == b->server_scope_sz &&
6454 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6461 * nfs4_proc_bind_conn_to_session()
6463 * The 4.1 client currently uses the same TCP connection for the
6464 * fore and backchannel.
6466 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6469 struct nfs41_bind_conn_to_session_res res;
6470 struct rpc_message msg = {
6472 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6478 dprintk("--> %s\n", __func__);
6480 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6481 if (unlikely(res.session == NULL)) {
6486 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6487 trace_nfs4_bind_conn_to_session(clp, status);
6489 if (memcmp(res.session->sess_id.data,
6490 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6491 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6495 if (res.dir != NFS4_CDFS4_BOTH) {
6496 dprintk("NFS: %s: Unexpected direction from server\n",
6501 if (res.use_conn_in_rdma_mode) {
6502 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6511 dprintk("<-- %s status= %d\n", __func__, status);
6516 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6517 * and operations we'd like to see to enable certain features in the allow map
6519 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6520 .how = SP4_MACH_CRED,
6521 .enforce.u.words = {
6522 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6523 1 << (OP_EXCHANGE_ID - 32) |
6524 1 << (OP_CREATE_SESSION - 32) |
6525 1 << (OP_DESTROY_SESSION - 32) |
6526 1 << (OP_DESTROY_CLIENTID - 32)
6529 [0] = 1 << (OP_CLOSE) |
6532 [1] = 1 << (OP_SECINFO - 32) |
6533 1 << (OP_SECINFO_NO_NAME - 32) |
6534 1 << (OP_TEST_STATEID - 32) |
6535 1 << (OP_FREE_STATEID - 32) |
6536 1 << (OP_WRITE - 32)
6541 * Select the state protection mode for client `clp' given the server results
6542 * from exchange_id in `sp'.
6544 * Returns 0 on success, negative errno otherwise.
6546 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6547 struct nfs41_state_protection *sp)
6549 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6550 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6551 1 << (OP_EXCHANGE_ID - 32) |
6552 1 << (OP_CREATE_SESSION - 32) |
6553 1 << (OP_DESTROY_SESSION - 32) |
6554 1 << (OP_DESTROY_CLIENTID - 32)
6558 if (sp->how == SP4_MACH_CRED) {
6559 /* Print state protect result */
6560 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6561 for (i = 0; i <= LAST_NFS4_OP; i++) {
6562 if (test_bit(i, sp->enforce.u.longs))
6563 dfprintk(MOUNT, " enforce op %d\n", i);
6564 if (test_bit(i, sp->allow.u.longs))
6565 dfprintk(MOUNT, " allow op %d\n", i);
6568 /* make sure nothing is on enforce list that isn't supported */
6569 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6570 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6571 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6577 * Minimal mode - state operations are allowed to use machine
6578 * credential. Note this already happens by default, so the
6579 * client doesn't have to do anything more than the negotiation.
6581 * NOTE: we don't care if EXCHANGE_ID is in the list -
6582 * we're already using the machine cred for exchange_id
6583 * and will never use a different cred.
6585 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6586 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6587 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6588 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6589 dfprintk(MOUNT, "sp4_mach_cred:\n");
6590 dfprintk(MOUNT, " minimal mode enabled\n");
6591 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6593 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6597 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6598 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6599 dfprintk(MOUNT, " cleanup mode enabled\n");
6600 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6603 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6604 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6605 dfprintk(MOUNT, " secinfo mode enabled\n");
6606 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6609 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6610 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6611 dfprintk(MOUNT, " stateid mode enabled\n");
6612 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6615 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6616 dfprintk(MOUNT, " write mode enabled\n");
6617 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6620 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6621 dfprintk(MOUNT, " commit mode enabled\n");
6622 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6630 * _nfs4_proc_exchange_id()
6632 * Wrapper for EXCHANGE_ID operation.
6634 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6637 nfs4_verifier verifier;
6638 struct nfs41_exchange_id_args args = {
6639 .verifier = &verifier,
6641 #ifdef CONFIG_NFS_V4_1_MIGRATION
6642 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6643 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6644 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6646 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6647 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6650 struct nfs41_exchange_id_res res = {
6654 struct rpc_message msg = {
6655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6661 nfs4_init_boot_verifier(clp, &verifier);
6662 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6664 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6665 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6666 args.id_len, args.id);
6668 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6670 if (unlikely(res.server_owner == NULL)) {
6675 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6677 if (unlikely(res.server_scope == NULL)) {
6679 goto out_server_owner;
6682 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6683 if (unlikely(res.impl_id == NULL)) {
6685 goto out_server_scope;
6690 args.state_protect.how = SP4_NONE;
6694 args.state_protect = nfs4_sp4_mach_cred_request;
6701 goto out_server_scope;
6704 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6705 trace_nfs4_exchange_id(clp, status);
6707 status = nfs4_check_cl_exchange_flags(res.flags);
6710 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6713 clp->cl_clientid = res.clientid;
6714 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6715 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6716 clp->cl_seqid = res.seqid;
6718 kfree(clp->cl_serverowner);
6719 clp->cl_serverowner = res.server_owner;
6720 res.server_owner = NULL;
6722 /* use the most recent implementation id */
6723 kfree(clp->cl_implid);
6724 clp->cl_implid = res.impl_id;
6726 if (clp->cl_serverscope != NULL &&
6727 !nfs41_same_server_scope(clp->cl_serverscope,
6728 res.server_scope)) {
6729 dprintk("%s: server_scope mismatch detected\n",
6731 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6732 kfree(clp->cl_serverscope);
6733 clp->cl_serverscope = NULL;
6736 if (clp->cl_serverscope == NULL) {
6737 clp->cl_serverscope = res.server_scope;
6744 kfree(res.server_owner);
6746 kfree(res.server_scope);
6748 if (clp->cl_implid != NULL)
6749 dprintk("NFS reply exchange_id: Server Implementation ID: "
6750 "domain: %s, name: %s, date: %llu,%u\n",
6751 clp->cl_implid->domain, clp->cl_implid->name,
6752 clp->cl_implid->date.seconds,
6753 clp->cl_implid->date.nseconds);
6754 dprintk("NFS reply exchange_id: %d\n", status);
6759 * nfs4_proc_exchange_id()
6761 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6763 * Since the clientid has expired, all compounds using sessions
6764 * associated with the stale clientid will be returning
6765 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6766 * be in some phase of session reset.
6768 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6770 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6772 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6775 /* try SP4_MACH_CRED if krb5i/p */
6776 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6777 authflavor == RPC_AUTH_GSS_KRB5P) {
6778 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6784 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6787 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6788 struct rpc_cred *cred)
6790 struct rpc_message msg = {
6791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6797 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6798 trace_nfs4_destroy_clientid(clp, status);
6800 dprintk("NFS: Got error %d from the server %s on "
6801 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6805 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6806 struct rpc_cred *cred)
6811 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6812 ret = _nfs4_proc_destroy_clientid(clp, cred);
6814 case -NFS4ERR_DELAY:
6815 case -NFS4ERR_CLIENTID_BUSY:
6825 int nfs4_destroy_clientid(struct nfs_client *clp)
6827 struct rpc_cred *cred;
6830 if (clp->cl_mvops->minor_version < 1)
6832 if (clp->cl_exchange_flags == 0)
6834 if (clp->cl_preserve_clid)
6836 cred = nfs4_get_clid_cred(clp);
6837 ret = nfs4_proc_destroy_clientid(clp, cred);
6842 case -NFS4ERR_STALE_CLIENTID:
6843 clp->cl_exchange_flags = 0;
6849 struct nfs4_get_lease_time_data {
6850 struct nfs4_get_lease_time_args *args;
6851 struct nfs4_get_lease_time_res *res;
6852 struct nfs_client *clp;
6855 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6858 struct nfs4_get_lease_time_data *data =
6859 (struct nfs4_get_lease_time_data *)calldata;
6861 dprintk("--> %s\n", __func__);
6862 /* just setup sequence, do not trigger session recovery
6863 since we're invoked within one */
6864 nfs41_setup_sequence(data->clp->cl_session,
6865 &data->args->la_seq_args,
6866 &data->res->lr_seq_res,
6868 dprintk("<-- %s\n", __func__);
6872 * Called from nfs4_state_manager thread for session setup, so don't recover
6873 * from sequence operation or clientid errors.
6875 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6877 struct nfs4_get_lease_time_data *data =
6878 (struct nfs4_get_lease_time_data *)calldata;
6880 dprintk("--> %s\n", __func__);
6881 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6883 switch (task->tk_status) {
6884 case -NFS4ERR_DELAY:
6885 case -NFS4ERR_GRACE:
6886 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6887 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6888 task->tk_status = 0;
6890 case -NFS4ERR_RETRY_UNCACHED_REP:
6891 rpc_restart_call_prepare(task);
6894 dprintk("<-- %s\n", __func__);
6897 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
6898 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6899 .rpc_call_done = nfs4_get_lease_time_done,
6902 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6904 struct rpc_task *task;
6905 struct nfs4_get_lease_time_args args;
6906 struct nfs4_get_lease_time_res res = {
6907 .lr_fsinfo = fsinfo,
6909 struct nfs4_get_lease_time_data data = {
6914 struct rpc_message msg = {
6915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6919 struct rpc_task_setup task_setup = {
6920 .rpc_client = clp->cl_rpcclient,
6921 .rpc_message = &msg,
6922 .callback_ops = &nfs4_get_lease_time_ops,
6923 .callback_data = &data,
6924 .flags = RPC_TASK_TIMEOUT,
6928 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
6929 nfs4_set_sequence_privileged(&args.la_seq_args);
6930 dprintk("--> %s\n", __func__);
6931 task = rpc_run_task(&task_setup);
6934 status = PTR_ERR(task);
6936 status = task->tk_status;
6939 dprintk("<-- %s return %d\n", __func__, status);
6945 * Initialize the values to be used by the client in CREATE_SESSION
6946 * If nfs4_init_session set the fore channel request and response sizes,
6949 * Set the back channel max_resp_sz_cached to zero to force the client to
6950 * always set csa_cachethis to FALSE because the current implementation
6951 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6953 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6955 unsigned int max_rqst_sz, max_resp_sz;
6957 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6958 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6960 /* Fore channel attributes */
6961 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6962 args->fc_attrs.max_resp_sz = max_resp_sz;
6963 args->fc_attrs.max_ops = NFS4_MAX_OPS;
6964 args->fc_attrs.max_reqs = max_session_slots;
6966 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6967 "max_ops=%u max_reqs=%u\n",
6969 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
6970 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
6972 /* Back channel attributes */
6973 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6974 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6975 args->bc_attrs.max_resp_sz_cached = 0;
6976 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6977 args->bc_attrs.max_reqs = 1;
6979 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6980 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6982 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6983 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6984 args->bc_attrs.max_reqs);
6987 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6989 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6990 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6992 if (rcvd->max_resp_sz > sent->max_resp_sz)
6995 * Our requested max_ops is the minimum we need; we're not
6996 * prepared to break up compounds into smaller pieces than that.
6997 * So, no point even trying to continue if the server won't
7000 if (rcvd->max_ops < sent->max_ops)
7002 if (rcvd->max_reqs == 0)
7004 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7005 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7009 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7011 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7012 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
7014 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7016 if (rcvd->max_resp_sz < sent->max_resp_sz)
7018 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7020 /* These would render the backchannel useless: */
7021 if (rcvd->max_ops != sent->max_ops)
7023 if (rcvd->max_reqs != sent->max_reqs)
7028 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7029 struct nfs4_session *session)
7033 ret = nfs4_verify_fore_channel_attrs(args, session);
7036 return nfs4_verify_back_channel_attrs(args, session);
7039 static int _nfs4_proc_create_session(struct nfs_client *clp,
7040 struct rpc_cred *cred)
7042 struct nfs4_session *session = clp->cl_session;
7043 struct nfs41_create_session_args args = {
7045 .cb_program = NFS4_CALLBACK,
7047 struct nfs41_create_session_res res = {
7050 struct rpc_message msg = {
7051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7058 nfs4_init_channel_attrs(&args);
7059 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7061 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7062 trace_nfs4_create_session(clp, status);
7065 /* Verify the session's negotiated channel_attrs values */
7066 status = nfs4_verify_channel_attrs(&args, session);
7067 /* Increment the clientid slot sequence id */
7075 * Issues a CREATE_SESSION operation to the server.
7076 * It is the responsibility of the caller to verify the session is
7077 * expired before calling this routine.
7079 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7083 struct nfs4_session *session = clp->cl_session;
7085 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7087 status = _nfs4_proc_create_session(clp, cred);
7091 /* Init or reset the session slot tables */
7092 status = nfs4_setup_session_slot_tables(session);
7093 dprintk("slot table setup returned %d\n", status);
7097 ptr = (unsigned *)&session->sess_id.data[0];
7098 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7099 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7101 dprintk("<-- %s\n", __func__);
7106 * Issue the over-the-wire RPC DESTROY_SESSION.
7107 * The caller must serialize access to this routine.
7109 int nfs4_proc_destroy_session(struct nfs4_session *session,
7110 struct rpc_cred *cred)
7112 struct rpc_message msg = {
7113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7114 .rpc_argp = session,
7119 dprintk("--> nfs4_proc_destroy_session\n");
7121 /* session is still being setup */
7122 if (session->clp->cl_cons_state != NFS_CS_READY)
7125 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7126 trace_nfs4_destroy_session(session->clp, status);
7129 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7130 "Session has been destroyed regardless...\n", status);
7132 dprintk("<-- nfs4_proc_destroy_session\n");
7137 * Renew the cl_session lease.
7139 struct nfs4_sequence_data {
7140 struct nfs_client *clp;
7141 struct nfs4_sequence_args args;
7142 struct nfs4_sequence_res res;
7145 static void nfs41_sequence_release(void *data)
7147 struct nfs4_sequence_data *calldata = data;
7148 struct nfs_client *clp = calldata->clp;
7150 if (atomic_read(&clp->cl_count) > 1)
7151 nfs4_schedule_state_renewal(clp);
7152 nfs_put_client(clp);
7156 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7158 switch(task->tk_status) {
7159 case -NFS4ERR_DELAY:
7160 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7163 nfs4_schedule_lease_recovery(clp);
7168 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7170 struct nfs4_sequence_data *calldata = data;
7171 struct nfs_client *clp = calldata->clp;
7173 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7176 trace_nfs4_sequence(clp, task->tk_status);
7177 if (task->tk_status < 0) {
7178 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7179 if (atomic_read(&clp->cl_count) == 1)
7182 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7183 rpc_restart_call_prepare(task);
7187 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7189 dprintk("<-- %s\n", __func__);
7192 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7194 struct nfs4_sequence_data *calldata = data;
7195 struct nfs_client *clp = calldata->clp;
7196 struct nfs4_sequence_args *args;
7197 struct nfs4_sequence_res *res;
7199 args = task->tk_msg.rpc_argp;
7200 res = task->tk_msg.rpc_resp;
7202 nfs41_setup_sequence(clp->cl_session, args, res, task);
7205 static const struct rpc_call_ops nfs41_sequence_ops = {
7206 .rpc_call_done = nfs41_sequence_call_done,
7207 .rpc_call_prepare = nfs41_sequence_prepare,
7208 .rpc_release = nfs41_sequence_release,
7211 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7212 struct rpc_cred *cred,
7215 struct nfs4_sequence_data *calldata;
7216 struct rpc_message msg = {
7217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7220 struct rpc_task_setup task_setup_data = {
7221 .rpc_client = clp->cl_rpcclient,
7222 .rpc_message = &msg,
7223 .callback_ops = &nfs41_sequence_ops,
7224 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7227 if (!atomic_inc_not_zero(&clp->cl_count))
7228 return ERR_PTR(-EIO);
7229 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7230 if (calldata == NULL) {
7231 nfs_put_client(clp);
7232 return ERR_PTR(-ENOMEM);
7234 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7236 nfs4_set_sequence_privileged(&calldata->args);
7237 msg.rpc_argp = &calldata->args;
7238 msg.rpc_resp = &calldata->res;
7239 calldata->clp = clp;
7240 task_setup_data.callback_data = calldata;
7242 return rpc_run_task(&task_setup_data);
7245 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7247 struct rpc_task *task;
7250 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7252 task = _nfs41_proc_sequence(clp, cred, false);
7254 ret = PTR_ERR(task);
7256 rpc_put_task_async(task);
7257 dprintk("<-- %s status=%d\n", __func__, ret);
7261 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7263 struct rpc_task *task;
7266 task = _nfs41_proc_sequence(clp, cred, true);
7268 ret = PTR_ERR(task);
7271 ret = rpc_wait_for_completion_task(task);
7273 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7275 if (task->tk_status == 0)
7276 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7277 ret = task->tk_status;
7281 dprintk("<-- %s status=%d\n", __func__, ret);
7285 struct nfs4_reclaim_complete_data {
7286 struct nfs_client *clp;
7287 struct nfs41_reclaim_complete_args arg;
7288 struct nfs41_reclaim_complete_res res;
7291 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7293 struct nfs4_reclaim_complete_data *calldata = data;
7295 nfs41_setup_sequence(calldata->clp->cl_session,
7296 &calldata->arg.seq_args,
7297 &calldata->res.seq_res,
7301 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7303 switch(task->tk_status) {
7305 case -NFS4ERR_COMPLETE_ALREADY:
7306 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7308 case -NFS4ERR_DELAY:
7309 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7311 case -NFS4ERR_RETRY_UNCACHED_REP:
7314 nfs4_schedule_lease_recovery(clp);
7319 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7321 struct nfs4_reclaim_complete_data *calldata = data;
7322 struct nfs_client *clp = calldata->clp;
7323 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7325 dprintk("--> %s\n", __func__);
7326 if (!nfs41_sequence_done(task, res))
7329 trace_nfs4_reclaim_complete(clp, task->tk_status);
7330 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7331 rpc_restart_call_prepare(task);
7334 dprintk("<-- %s\n", __func__);
7337 static void nfs4_free_reclaim_complete_data(void *data)
7339 struct nfs4_reclaim_complete_data *calldata = data;
7344 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7345 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7346 .rpc_call_done = nfs4_reclaim_complete_done,
7347 .rpc_release = nfs4_free_reclaim_complete_data,
7351 * Issue a global reclaim complete.
7353 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7354 struct rpc_cred *cred)
7356 struct nfs4_reclaim_complete_data *calldata;
7357 struct rpc_task *task;
7358 struct rpc_message msg = {
7359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7362 struct rpc_task_setup task_setup_data = {
7363 .rpc_client = clp->cl_rpcclient,
7364 .rpc_message = &msg,
7365 .callback_ops = &nfs4_reclaim_complete_call_ops,
7366 .flags = RPC_TASK_ASYNC,
7368 int status = -ENOMEM;
7370 dprintk("--> %s\n", __func__);
7371 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7372 if (calldata == NULL)
7374 calldata->clp = clp;
7375 calldata->arg.one_fs = 0;
7377 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7378 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7379 msg.rpc_argp = &calldata->arg;
7380 msg.rpc_resp = &calldata->res;
7381 task_setup_data.callback_data = calldata;
7382 task = rpc_run_task(&task_setup_data);
7384 status = PTR_ERR(task);
7387 status = nfs4_wait_for_completion_rpc_task(task);
7389 status = task->tk_status;
7393 dprintk("<-- %s status=%d\n", __func__, status);
7398 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7400 struct nfs4_layoutget *lgp = calldata;
7401 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7402 struct nfs4_session *session = nfs4_get_session(server);
7404 dprintk("--> %s\n", __func__);
7405 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7406 * right now covering the LAYOUTGET we are about to send.
7407 * However, that is not so catastrophic, and there seems
7408 * to be no way to prevent it completely.
7410 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7411 &lgp->res.seq_res, task))
7413 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7414 NFS_I(lgp->args.inode)->layout,
7415 lgp->args.ctx->state)) {
7416 rpc_exit(task, NFS4_OK);
7420 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7422 struct nfs4_layoutget *lgp = calldata;
7423 struct inode *inode = lgp->args.inode;
7424 struct nfs_server *server = NFS_SERVER(inode);
7425 struct pnfs_layout_hdr *lo;
7426 struct nfs4_state *state = NULL;
7427 unsigned long timeo, now, giveup;
7429 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7431 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7434 switch (task->tk_status) {
7438 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7439 * (or clients) writing to the same RAID stripe
7441 case -NFS4ERR_LAYOUTTRYLATER:
7443 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7444 * existing layout before getting a new one).
7446 case -NFS4ERR_RECALLCONFLICT:
7447 timeo = rpc_get_timeout(task->tk_client);
7448 giveup = lgp->args.timestamp + timeo;
7450 if (time_after(giveup, now)) {
7451 unsigned long delay;
7454 * - Not less then NFS4_POLL_RETRY_MIN.
7455 * - One last time a jiffie before we give up
7456 * - exponential backoff (time_now minus start_attempt)
7458 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7459 min((giveup - now - 1),
7460 now - lgp->args.timestamp));
7462 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7464 rpc_delay(task, delay);
7465 task->tk_status = 0;
7466 rpc_restart_call_prepare(task);
7467 goto out; /* Do not call nfs4_async_handle_error() */
7470 case -NFS4ERR_EXPIRED:
7471 case -NFS4ERR_BAD_STATEID:
7472 spin_lock(&inode->i_lock);
7473 lo = NFS_I(inode)->layout;
7474 if (!lo || list_empty(&lo->plh_segs)) {
7475 spin_unlock(&inode->i_lock);
7476 /* If the open stateid was bad, then recover it. */
7477 state = lgp->args.ctx->state;
7481 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7482 spin_unlock(&inode->i_lock);
7483 /* Mark the bad layout state as invalid, then
7484 * retry using the open stateid. */
7485 pnfs_free_lseg_list(&head);
7488 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7489 rpc_restart_call_prepare(task);
7491 dprintk("<-- %s\n", __func__);
7494 static size_t max_response_pages(struct nfs_server *server)
7496 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7497 return nfs_page_array_len(0, max_resp_sz);
7500 static void nfs4_free_pages(struct page **pages, size_t size)
7507 for (i = 0; i < size; i++) {
7510 __free_page(pages[i]);
7515 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7517 struct page **pages;
7520 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7522 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7526 for (i = 0; i < size; i++) {
7527 pages[i] = alloc_page(gfp_flags);
7529 dprintk("%s: failed to allocate page\n", __func__);
7530 nfs4_free_pages(pages, size);
7538 static void nfs4_layoutget_release(void *calldata)
7540 struct nfs4_layoutget *lgp = calldata;
7541 struct inode *inode = lgp->args.inode;
7542 struct nfs_server *server = NFS_SERVER(inode);
7543 size_t max_pages = max_response_pages(server);
7545 dprintk("--> %s\n", __func__);
7546 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7547 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7548 put_nfs_open_context(lgp->args.ctx);
7550 dprintk("<-- %s\n", __func__);
7553 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7554 .rpc_call_prepare = nfs4_layoutget_prepare,
7555 .rpc_call_done = nfs4_layoutget_done,
7556 .rpc_release = nfs4_layoutget_release,
7559 struct pnfs_layout_segment *
7560 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7562 struct inode *inode = lgp->args.inode;
7563 struct nfs_server *server = NFS_SERVER(inode);
7564 size_t max_pages = max_response_pages(server);
7565 struct rpc_task *task;
7566 struct rpc_message msg = {
7567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7568 .rpc_argp = &lgp->args,
7569 .rpc_resp = &lgp->res,
7570 .rpc_cred = lgp->cred,
7572 struct rpc_task_setup task_setup_data = {
7573 .rpc_client = server->client,
7574 .rpc_message = &msg,
7575 .callback_ops = &nfs4_layoutget_call_ops,
7576 .callback_data = lgp,
7577 .flags = RPC_TASK_ASYNC,
7579 struct pnfs_layout_segment *lseg = NULL;
7582 dprintk("--> %s\n", __func__);
7584 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7585 if (!lgp->args.layout.pages) {
7586 nfs4_layoutget_release(lgp);
7587 return ERR_PTR(-ENOMEM);
7589 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7590 lgp->args.timestamp = jiffies;
7592 lgp->res.layoutp = &lgp->args.layout;
7593 lgp->res.seq_res.sr_slot = NULL;
7594 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7596 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7597 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7599 task = rpc_run_task(&task_setup_data);
7601 return ERR_CAST(task);
7602 status = nfs4_wait_for_completion_rpc_task(task);
7604 status = task->tk_status;
7605 trace_nfs4_layoutget(lgp->args.ctx,
7609 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7610 if (status == 0 && lgp->res.layoutp->len)
7611 lseg = pnfs_layout_process(lgp);
7613 dprintk("<-- %s status=%d\n", __func__, status);
7615 return ERR_PTR(status);
7620 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7622 struct nfs4_layoutreturn *lrp = calldata;
7624 dprintk("--> %s\n", __func__);
7625 nfs41_setup_sequence(lrp->clp->cl_session,
7626 &lrp->args.seq_args,
7631 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7633 struct nfs4_layoutreturn *lrp = calldata;
7634 struct nfs_server *server;
7636 dprintk("--> %s\n", __func__);
7638 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7641 server = NFS_SERVER(lrp->args.inode);
7642 switch (task->tk_status) {
7644 task->tk_status = 0;
7647 case -NFS4ERR_DELAY:
7648 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7650 rpc_restart_call_prepare(task);
7653 dprintk("<-- %s\n", __func__);
7656 static void nfs4_layoutreturn_release(void *calldata)
7658 struct nfs4_layoutreturn *lrp = calldata;
7659 struct pnfs_layout_hdr *lo = lrp->args.layout;
7661 dprintk("--> %s\n", __func__);
7662 spin_lock(&lo->plh_inode->i_lock);
7663 if (lrp->res.lrs_present)
7664 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7665 lo->plh_block_lgets--;
7666 spin_unlock(&lo->plh_inode->i_lock);
7667 pnfs_put_layout_hdr(lrp->args.layout);
7669 dprintk("<-- %s\n", __func__);
7672 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7673 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7674 .rpc_call_done = nfs4_layoutreturn_done,
7675 .rpc_release = nfs4_layoutreturn_release,
7678 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7680 struct rpc_task *task;
7681 struct rpc_message msg = {
7682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7683 .rpc_argp = &lrp->args,
7684 .rpc_resp = &lrp->res,
7685 .rpc_cred = lrp->cred,
7687 struct rpc_task_setup task_setup_data = {
7688 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7689 .rpc_message = &msg,
7690 .callback_ops = &nfs4_layoutreturn_call_ops,
7691 .callback_data = lrp,
7695 dprintk("--> %s\n", __func__);
7696 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7697 task = rpc_run_task(&task_setup_data);
7699 return PTR_ERR(task);
7700 status = task->tk_status;
7701 trace_nfs4_layoutreturn(lrp->args.inode, status);
7702 dprintk("<-- %s status=%d\n", __func__, status);
7708 * Retrieve the list of Data Server devices from the MDS.
7710 static int _nfs4_getdevicelist(struct nfs_server *server,
7711 const struct nfs_fh *fh,
7712 struct pnfs_devicelist *devlist)
7714 struct nfs4_getdevicelist_args args = {
7716 .layoutclass = server->pnfs_curr_ld->id,
7718 struct nfs4_getdevicelist_res res = {
7721 struct rpc_message msg = {
7722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7728 dprintk("--> %s\n", __func__);
7729 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7731 dprintk("<-- %s status=%d\n", __func__, status);
7735 int nfs4_proc_getdevicelist(struct nfs_server *server,
7736 const struct nfs_fh *fh,
7737 struct pnfs_devicelist *devlist)
7739 struct nfs4_exception exception = { };
7743 err = nfs4_handle_exception(server,
7744 _nfs4_getdevicelist(server, fh, devlist),
7746 } while (exception.retry);
7748 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7749 err, devlist->num_devs);
7753 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7756 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7757 struct pnfs_device *pdev,
7758 struct rpc_cred *cred)
7760 struct nfs4_getdeviceinfo_args args = {
7763 struct nfs4_getdeviceinfo_res res = {
7766 struct rpc_message msg = {
7767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7774 dprintk("--> %s\n", __func__);
7775 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7776 dprintk("<-- %s status=%d\n", __func__, status);
7781 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7782 struct pnfs_device *pdev,
7783 struct rpc_cred *cred)
7785 struct nfs4_exception exception = { };
7789 err = nfs4_handle_exception(server,
7790 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7792 } while (exception.retry);
7795 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7797 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7799 struct nfs4_layoutcommit_data *data = calldata;
7800 struct nfs_server *server = NFS_SERVER(data->args.inode);
7801 struct nfs4_session *session = nfs4_get_session(server);
7803 nfs41_setup_sequence(session,
7804 &data->args.seq_args,
7810 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7812 struct nfs4_layoutcommit_data *data = calldata;
7813 struct nfs_server *server = NFS_SERVER(data->args.inode);
7815 if (!nfs41_sequence_done(task, &data->res.seq_res))
7818 switch (task->tk_status) { /* Just ignore these failures */
7819 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7820 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7821 case -NFS4ERR_BADLAYOUT: /* no layout */
7822 case -NFS4ERR_GRACE: /* loca_recalim always false */
7823 task->tk_status = 0;
7827 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7828 rpc_restart_call_prepare(task);
7834 static void nfs4_layoutcommit_release(void *calldata)
7836 struct nfs4_layoutcommit_data *data = calldata;
7838 pnfs_cleanup_layoutcommit(data);
7839 nfs_post_op_update_inode_force_wcc(data->args.inode,
7841 put_rpccred(data->cred);
7845 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7846 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7847 .rpc_call_done = nfs4_layoutcommit_done,
7848 .rpc_release = nfs4_layoutcommit_release,
7852 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7854 struct rpc_message msg = {
7855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7856 .rpc_argp = &data->args,
7857 .rpc_resp = &data->res,
7858 .rpc_cred = data->cred,
7860 struct rpc_task_setup task_setup_data = {
7861 .task = &data->task,
7862 .rpc_client = NFS_CLIENT(data->args.inode),
7863 .rpc_message = &msg,
7864 .callback_ops = &nfs4_layoutcommit_ops,
7865 .callback_data = data,
7866 .flags = RPC_TASK_ASYNC,
7868 struct rpc_task *task;
7871 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7872 "lbw: %llu inode %lu\n",
7873 data->task.tk_pid, sync,
7874 data->args.lastbytewritten,
7875 data->args.inode->i_ino);
7877 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7878 task = rpc_run_task(&task_setup_data);
7880 return PTR_ERR(task);
7883 status = nfs4_wait_for_completion_rpc_task(task);
7886 status = task->tk_status;
7887 trace_nfs4_layoutcommit(data->args.inode, status);
7889 dprintk("%s: status %d\n", __func__, status);
7895 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7896 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7899 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7900 struct nfs_fsinfo *info,
7901 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7903 struct nfs41_secinfo_no_name_args args = {
7904 .style = SECINFO_STYLE_CURRENT_FH,
7906 struct nfs4_secinfo_res res = {
7909 struct rpc_message msg = {
7910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7914 struct rpc_clnt *clnt = server->client;
7915 struct rpc_cred *cred = NULL;
7918 if (use_integrity) {
7919 clnt = server->nfs_client->cl_rpcclient;
7920 cred = nfs4_get_clid_cred(server->nfs_client);
7921 msg.rpc_cred = cred;
7924 dprintk("--> %s\n", __func__);
7925 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7927 dprintk("<-- %s status=%d\n", __func__, status);
7936 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7937 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7939 struct nfs4_exception exception = { };
7942 /* first try using integrity protection */
7943 err = -NFS4ERR_WRONGSEC;
7945 /* try to use integrity protection with machine cred */
7946 if (_nfs4_is_integrity_protected(server->nfs_client))
7947 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7951 * if unable to use integrity protection, or SECINFO with
7952 * integrity protection returns NFS4ERR_WRONGSEC (which is
7953 * disallowed by spec, but exists in deployed servers) use
7954 * the current filesystem's rpc_client and the user cred.
7956 if (err == -NFS4ERR_WRONGSEC)
7957 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7962 case -NFS4ERR_WRONGSEC:
7966 err = nfs4_handle_exception(server, err, &exception);
7968 } while (exception.retry);
7974 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7975 struct nfs_fsinfo *info)
7979 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
7980 struct nfs4_secinfo_flavors *flavors;
7981 struct nfs4_secinfo4 *secinfo;
7984 page = alloc_page(GFP_KERNEL);
7990 flavors = page_address(page);
7991 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7994 * Fall back on "guess and check" method if
7995 * the server doesn't support SECINFO_NO_NAME
7997 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
7998 err = nfs4_find_root_sec(server, fhandle, info);
8004 for (i = 0; i < flavors->num_flavors; i++) {
8005 secinfo = &flavors->flavors[i];
8007 switch (secinfo->flavor) {
8011 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8012 &secinfo->flavor_info);
8015 flavor = RPC_AUTH_MAXFLAVOR;
8019 if (!nfs_auth_info_match(&server->auth_info, flavor))
8020 flavor = RPC_AUTH_MAXFLAVOR;
8022 if (flavor != RPC_AUTH_MAXFLAVOR) {
8023 err = nfs4_lookup_root_sec(server, fhandle,
8030 if (flavor == RPC_AUTH_MAXFLAVOR)
8041 static int _nfs41_test_stateid(struct nfs_server *server,
8042 nfs4_stateid *stateid,
8043 struct rpc_cred *cred)
8046 struct nfs41_test_stateid_args args = {
8049 struct nfs41_test_stateid_res res;
8050 struct rpc_message msg = {
8051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8056 struct rpc_clnt *rpc_client = server->client;
8058 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8061 dprintk("NFS call test_stateid %p\n", stateid);
8062 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8063 nfs4_set_sequence_privileged(&args.seq_args);
8064 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8065 &args.seq_args, &res.seq_res);
8066 if (status != NFS_OK) {
8067 dprintk("NFS reply test_stateid: failed, %d\n", status);
8070 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8075 * nfs41_test_stateid - perform a TEST_STATEID operation
8077 * @server: server / transport on which to perform the operation
8078 * @stateid: state ID to test
8081 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8082 * Otherwise a negative NFS4ERR value is returned if the operation
8083 * failed or the state ID is not currently valid.
8085 static int nfs41_test_stateid(struct nfs_server *server,
8086 nfs4_stateid *stateid,
8087 struct rpc_cred *cred)
8089 struct nfs4_exception exception = { };
8092 err = _nfs41_test_stateid(server, stateid, cred);
8093 if (err != -NFS4ERR_DELAY)
8095 nfs4_handle_exception(server, err, &exception);
8096 } while (exception.retry);
8100 struct nfs_free_stateid_data {
8101 struct nfs_server *server;
8102 struct nfs41_free_stateid_args args;
8103 struct nfs41_free_stateid_res res;
8106 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8108 struct nfs_free_stateid_data *data = calldata;
8109 nfs41_setup_sequence(nfs4_get_session(data->server),
8110 &data->args.seq_args,
8115 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8117 struct nfs_free_stateid_data *data = calldata;
8119 nfs41_sequence_done(task, &data->res.seq_res);
8121 switch (task->tk_status) {
8122 case -NFS4ERR_DELAY:
8123 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8124 rpc_restart_call_prepare(task);
8128 static void nfs41_free_stateid_release(void *calldata)
8133 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8134 .rpc_call_prepare = nfs41_free_stateid_prepare,
8135 .rpc_call_done = nfs41_free_stateid_done,
8136 .rpc_release = nfs41_free_stateid_release,
8139 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8140 nfs4_stateid *stateid,
8141 struct rpc_cred *cred,
8144 struct rpc_message msg = {
8145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8148 struct rpc_task_setup task_setup = {
8149 .rpc_client = server->client,
8150 .rpc_message = &msg,
8151 .callback_ops = &nfs41_free_stateid_ops,
8152 .flags = RPC_TASK_ASYNC,
8154 struct nfs_free_stateid_data *data;
8156 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8157 &task_setup.rpc_client, &msg);
8159 dprintk("NFS call free_stateid %p\n", stateid);
8160 data = kmalloc(sizeof(*data), GFP_NOFS);
8162 return ERR_PTR(-ENOMEM);
8163 data->server = server;
8164 nfs4_stateid_copy(&data->args.stateid, stateid);
8166 task_setup.callback_data = data;
8168 msg.rpc_argp = &data->args;
8169 msg.rpc_resp = &data->res;
8170 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8172 nfs4_set_sequence_privileged(&data->args.seq_args);
8174 return rpc_run_task(&task_setup);
8178 * nfs41_free_stateid - perform a FREE_STATEID operation
8180 * @server: server / transport on which to perform the operation
8181 * @stateid: state ID to release
8184 * Returns NFS_OK if the server freed "stateid". Otherwise a
8185 * negative NFS4ERR value is returned.
8187 static int nfs41_free_stateid(struct nfs_server *server,
8188 nfs4_stateid *stateid,
8189 struct rpc_cred *cred)
8191 struct rpc_task *task;
8194 task = _nfs41_free_stateid(server, stateid, cred, true);
8196 return PTR_ERR(task);
8197 ret = rpc_wait_for_completion_task(task);
8199 ret = task->tk_status;
8204 static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8206 struct rpc_task *task;
8207 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8209 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8210 nfs4_free_lock_state(server, lsp);
8212 return PTR_ERR(task);
8217 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8218 const nfs4_stateid *s2)
8220 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8223 if (s1->seqid == s2->seqid)
8225 if (s1->seqid == 0 || s2->seqid == 0)
8231 #endif /* CONFIG_NFS_V4_1 */
8233 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8234 const nfs4_stateid *s2)
8236 return nfs4_stateid_match(s1, s2);
8240 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8241 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8242 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8243 .recover_open = nfs4_open_reclaim,
8244 .recover_lock = nfs4_lock_reclaim,
8245 .establish_clid = nfs4_init_clientid,
8246 .detect_trunking = nfs40_discover_server_trunking,
8249 #if defined(CONFIG_NFS_V4_1)
8250 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8251 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8252 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8253 .recover_open = nfs4_open_reclaim,
8254 .recover_lock = nfs4_lock_reclaim,
8255 .establish_clid = nfs41_init_clientid,
8256 .reclaim_complete = nfs41_proc_reclaim_complete,
8257 .detect_trunking = nfs41_discover_server_trunking,
8259 #endif /* CONFIG_NFS_V4_1 */
8261 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8262 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8263 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8264 .recover_open = nfs4_open_expired,
8265 .recover_lock = nfs4_lock_expired,
8266 .establish_clid = nfs4_init_clientid,
8269 #if defined(CONFIG_NFS_V4_1)
8270 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8271 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8272 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8273 .recover_open = nfs41_open_expired,
8274 .recover_lock = nfs41_lock_expired,
8275 .establish_clid = nfs41_init_clientid,
8277 #endif /* CONFIG_NFS_V4_1 */
8279 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8280 .sched_state_renewal = nfs4_proc_async_renew,
8281 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8282 .renew_lease = nfs4_proc_renew,
8285 #if defined(CONFIG_NFS_V4_1)
8286 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8287 .sched_state_renewal = nfs41_proc_async_sequence,
8288 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8289 .renew_lease = nfs4_proc_sequence,
8293 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8294 .get_locations = _nfs40_proc_get_locations,
8295 .fsid_present = _nfs40_proc_fsid_present,
8298 #if defined(CONFIG_NFS_V4_1)
8299 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8300 .get_locations = _nfs41_proc_get_locations,
8301 .fsid_present = _nfs41_proc_fsid_present,
8303 #endif /* CONFIG_NFS_V4_1 */
8305 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8307 .init_caps = NFS_CAP_READDIRPLUS
8308 | NFS_CAP_ATOMIC_OPEN
8309 | NFS_CAP_CHANGE_ATTR
8310 | NFS_CAP_POSIX_LOCK,
8311 .init_client = nfs40_init_client,
8312 .shutdown_client = nfs40_shutdown_client,
8313 .match_stateid = nfs4_match_stateid,
8314 .find_root_sec = nfs4_find_root_sec,
8315 .free_lock_state = nfs4_release_lockowner,
8316 .call_sync_ops = &nfs40_call_sync_ops,
8317 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8318 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8319 .state_renewal_ops = &nfs40_state_renewal_ops,
8320 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8323 #if defined(CONFIG_NFS_V4_1)
8324 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8326 .init_caps = NFS_CAP_READDIRPLUS
8327 | NFS_CAP_ATOMIC_OPEN
8328 | NFS_CAP_CHANGE_ATTR
8329 | NFS_CAP_POSIX_LOCK
8330 | NFS_CAP_STATEID_NFSV41
8331 | NFS_CAP_ATOMIC_OPEN_V1,
8332 .init_client = nfs41_init_client,
8333 .shutdown_client = nfs41_shutdown_client,
8334 .match_stateid = nfs41_match_stateid,
8335 .find_root_sec = nfs41_find_root_sec,
8336 .free_lock_state = nfs41_free_lock_state,
8337 .call_sync_ops = &nfs41_call_sync_ops,
8338 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8339 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8340 .state_renewal_ops = &nfs41_state_renewal_ops,
8341 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8345 #if defined(CONFIG_NFS_V4_2)
8346 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8348 .init_caps = NFS_CAP_READDIRPLUS
8349 | NFS_CAP_ATOMIC_OPEN
8350 | NFS_CAP_CHANGE_ATTR
8351 | NFS_CAP_POSIX_LOCK
8352 | NFS_CAP_STATEID_NFSV41
8353 | NFS_CAP_ATOMIC_OPEN_V1,
8354 .init_client = nfs41_init_client,
8355 .shutdown_client = nfs41_shutdown_client,
8356 .match_stateid = nfs41_match_stateid,
8357 .find_root_sec = nfs41_find_root_sec,
8358 .free_lock_state = nfs41_free_lock_state,
8359 .call_sync_ops = &nfs41_call_sync_ops,
8360 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8361 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8362 .state_renewal_ops = &nfs41_state_renewal_ops,
8366 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8367 [0] = &nfs_v4_0_minor_ops,
8368 #if defined(CONFIG_NFS_V4_1)
8369 [1] = &nfs_v4_1_minor_ops,
8371 #if defined(CONFIG_NFS_V4_2)
8372 [2] = &nfs_v4_2_minor_ops,
8376 static const struct inode_operations nfs4_dir_inode_operations = {
8377 .create = nfs_create,
8378 .lookup = nfs_lookup,
8379 .atomic_open = nfs_atomic_open,
8381 .unlink = nfs_unlink,
8382 .symlink = nfs_symlink,
8386 .rename = nfs_rename,
8387 .permission = nfs_permission,
8388 .getattr = nfs_getattr,
8389 .setattr = nfs_setattr,
8390 .getxattr = generic_getxattr,
8391 .setxattr = generic_setxattr,
8392 .listxattr = generic_listxattr,
8393 .removexattr = generic_removexattr,
8396 static const struct inode_operations nfs4_file_inode_operations = {
8397 .permission = nfs_permission,
8398 .getattr = nfs_getattr,
8399 .setattr = nfs_setattr,
8400 .getxattr = generic_getxattr,
8401 .setxattr = generic_setxattr,
8402 .listxattr = generic_listxattr,
8403 .removexattr = generic_removexattr,
8406 const struct nfs_rpc_ops nfs_v4_clientops = {
8407 .version = 4, /* protocol version */
8408 .dentry_ops = &nfs4_dentry_operations,
8409 .dir_inode_ops = &nfs4_dir_inode_operations,
8410 .file_inode_ops = &nfs4_file_inode_operations,
8411 .file_ops = &nfs4_file_operations,
8412 .getroot = nfs4_proc_get_root,
8413 .submount = nfs4_submount,
8414 .try_mount = nfs4_try_mount,
8415 .getattr = nfs4_proc_getattr,
8416 .setattr = nfs4_proc_setattr,
8417 .lookup = nfs4_proc_lookup,
8418 .access = nfs4_proc_access,
8419 .readlink = nfs4_proc_readlink,
8420 .create = nfs4_proc_create,
8421 .remove = nfs4_proc_remove,
8422 .unlink_setup = nfs4_proc_unlink_setup,
8423 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8424 .unlink_done = nfs4_proc_unlink_done,
8425 .rename = nfs4_proc_rename,
8426 .rename_setup = nfs4_proc_rename_setup,
8427 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8428 .rename_done = nfs4_proc_rename_done,
8429 .link = nfs4_proc_link,
8430 .symlink = nfs4_proc_symlink,
8431 .mkdir = nfs4_proc_mkdir,
8432 .rmdir = nfs4_proc_remove,
8433 .readdir = nfs4_proc_readdir,
8434 .mknod = nfs4_proc_mknod,
8435 .statfs = nfs4_proc_statfs,
8436 .fsinfo = nfs4_proc_fsinfo,
8437 .pathconf = nfs4_proc_pathconf,
8438 .set_capabilities = nfs4_server_capabilities,
8439 .decode_dirent = nfs4_decode_dirent,
8440 .read_setup = nfs4_proc_read_setup,
8441 .read_pageio_init = pnfs_pageio_init_read,
8442 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
8443 .read_done = nfs4_read_done,
8444 .write_setup = nfs4_proc_write_setup,
8445 .write_pageio_init = pnfs_pageio_init_write,
8446 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
8447 .write_done = nfs4_write_done,
8448 .commit_setup = nfs4_proc_commit_setup,
8449 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8450 .commit_done = nfs4_commit_done,
8451 .lock = nfs4_proc_lock,
8452 .clear_acl_cache = nfs4_zap_acl_attr,
8453 .close_context = nfs4_close_context,
8454 .open_context = nfs4_atomic_open,
8455 .have_delegation = nfs4_have_delegation,
8456 .return_delegation = nfs4_inode_return_delegation,
8457 .alloc_client = nfs4_alloc_client,
8458 .init_client = nfs4_init_client,
8459 .free_client = nfs4_free_client,
8460 .create_server = nfs4_create_server,
8461 .clone_server = nfs_clone_server,
8464 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8465 .prefix = XATTR_NAME_NFSV4_ACL,
8466 .list = nfs4_xattr_list_nfs4_acl,
8467 .get = nfs4_xattr_get_nfs4_acl,
8468 .set = nfs4_xattr_set_nfs4_acl,
8471 const struct xattr_handler *nfs4_xattr_handlers[] = {
8472 &nfs4_xattr_nfs4_acl_handler,
8473 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8474 &nfs4_xattr_nfs4_label_handler,