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/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_BITMASK_SZ 3
76 #define NFS4_POLL_RETRY_MIN (HZ/10)
77 #define NFS4_POLL_RETRY_MAX (15*HZ)
79 /* file attributes which can be mapped to nfs attributes */
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
96 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
98 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
99 struct nfs4_label *olabel);
100 #ifdef CONFIG_NFS_V4_1
101 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 const struct cred *cred,
103 struct nfs4_slot *slot,
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106 const struct cred *);
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 const struct cred *, bool);
111 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
112 static inline struct nfs4_label *
113 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
132 nfs4_label_release_security(struct nfs4_label *label)
135 security_release_secctx(label->label, label->len);
137 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
140 return server->attr_bitmask;
142 return server->attr_bitmask_nl;
145 static inline struct nfs4_label *
146 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
150 nfs4_label_release_security(struct nfs4_label *label)
153 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154 { return server->attr_bitmask; }
157 /* Prevent leaks of NFSv4 errors into userland */
158 static int nfs4_map_errors(int err)
163 case -NFS4ERR_RESOURCE:
164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
167 case -NFS4ERR_WRONGSEC:
168 case -NFS4ERR_WRONG_CRED:
170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
173 case -NFS4ERR_SHARE_DENIED:
175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
177 case -NFS4ERR_FILE_OPEN:
180 dprintk("%s could not handle NFSv4 error %d\n",
188 * This is our standard bitmap for GETATTR requests.
190 const u32 nfs4_fattr_bitmap[3] = {
192 | FATTR4_WORD0_CHANGE
195 | FATTR4_WORD0_FILEID,
197 | FATTR4_WORD1_NUMLINKS
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
206 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
211 static const u32 nfs4_pnfs_open_bitmap[3] = {
213 | FATTR4_WORD0_CHANGE
216 | FATTR4_WORD0_FILEID,
218 | FATTR4_WORD1_NUMLINKS
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
227 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
232 static const u32 nfs4_open_noattr_bitmap[3] = {
234 | FATTR4_WORD0_FILEID,
237 const u32 nfs4_statfs_bitmap[3] = {
238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
246 const u32 nfs4_pathconf_bitmap[3] = {
248 | FATTR4_WORD0_MAXNAME,
252 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
256 FATTR4_WORD1_TIME_DELTA
257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
259 | FATTR4_WORD2_CLONE_BLKSIZE
262 const u32 nfs4_fs_locations_bitmap[3] = {
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
278 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
281 unsigned long cache_validity;
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
299 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
302 nfs4_bitmap_copy_adjust(dst, src, inode);
305 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
306 struct nfs4_readdir_arg *readdir)
308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
312 readdir->cookie = cookie;
313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
329 start = p = kmap_atomic(*readdir->pages);
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
338 *p++ = xdr_one; /* bitmap length */
339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
351 *p++ = xdr_one; /* bitmap length */
352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
359 kunmap_atomic(start);
362 static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
364 const struct cred *cred)
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
368 ops->test_and_free_expired(server, stateid, cred);
371 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 const struct cred *cred)
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
379 static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
381 const struct cred *cred)
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
389 static long nfs4_update_delay(long *timeout)
393 return NFS4_POLL_RETRY_MAX;
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
403 static int nfs4_delay_killable(long *timeout)
407 freezable_schedule_timeout_killable_unsafe(
408 nfs4_update_delay(timeout));
409 if (!__fatal_signal_pending(current))
414 static int nfs4_delay_interruptible(long *timeout)
418 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
419 if (!signal_pending(current))
421 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
424 static int nfs4_delay(long *timeout, bool interruptible)
427 return nfs4_delay_interruptible(timeout);
428 return nfs4_delay_killable(timeout);
431 static const nfs4_stateid *
432 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
436 switch (stateid->type) {
437 case NFS4_OPEN_STATEID_TYPE:
438 case NFS4_LOCK_STATEID_TYPE:
439 case NFS4_DELEGATION_STATEID_TYPE:
447 /* This is the error handling routine for processes that are allowed
450 static int nfs4_do_handle_exception(struct nfs_server *server,
451 int errorcode, struct nfs4_exception *exception)
453 struct nfs_client *clp = server->nfs_client;
454 struct nfs4_state *state = exception->state;
455 const nfs4_stateid *stateid;
456 struct inode *inode = exception->inode;
459 exception->delay = 0;
460 exception->recovering = 0;
461 exception->retry = 0;
463 stateid = nfs4_recoverable_stateid(exception->stateid);
464 if (stateid == NULL && state != NULL)
465 stateid = nfs4_recoverable_stateid(&state->stateid);
470 case -NFS4ERR_BADHANDLE:
472 if (inode != NULL && S_ISREG(inode->i_mode))
473 pnfs_destroy_layout(NFS_I(inode));
475 case -NFS4ERR_DELEG_REVOKED:
476 case -NFS4ERR_ADMIN_REVOKED:
477 case -NFS4ERR_EXPIRED:
478 case -NFS4ERR_BAD_STATEID:
479 if (inode != NULL && stateid != NULL) {
480 nfs_inode_find_state_and_recover(inode,
482 goto wait_on_recovery;
485 case -NFS4ERR_OPENMODE:
489 err = nfs_async_inode_return_delegation(inode,
492 goto wait_on_recovery;
493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
494 exception->retry = 1;
500 ret = nfs4_schedule_stateid_recovery(server, state);
503 goto wait_on_recovery;
504 case -NFS4ERR_STALE_STATEID:
505 case -NFS4ERR_STALE_CLIENTID:
506 nfs4_schedule_lease_recovery(clp);
507 goto wait_on_recovery;
509 ret = nfs4_schedule_migration_recovery(server);
512 goto wait_on_recovery;
513 case -NFS4ERR_LEASE_MOVED:
514 nfs4_schedule_lease_moved_recovery(clp);
515 goto wait_on_recovery;
516 #if defined(CONFIG_NFS_V4_1)
517 case -NFS4ERR_BADSESSION:
518 case -NFS4ERR_BADSLOT:
519 case -NFS4ERR_BAD_HIGH_SLOT:
520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
521 case -NFS4ERR_DEADSESSION:
522 case -NFS4ERR_SEQ_FALSE_RETRY:
523 case -NFS4ERR_SEQ_MISORDERED:
524 dprintk("%s ERROR: %d Reset session\n", __func__,
526 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
527 goto wait_on_recovery;
528 #endif /* defined(CONFIG_NFS_V4_1) */
529 case -NFS4ERR_FILE_OPEN:
530 if (exception->timeout > HZ) {
531 /* We have retried a decent amount, time to
539 nfs_inc_server_stats(server, NFSIOS_DELAY);
542 case -NFS4ERR_LAYOUTTRYLATER:
543 case -NFS4ERR_RECALLCONFLICT:
544 exception->delay = 1;
547 case -NFS4ERR_RETRY_UNCACHED_REP:
548 case -NFS4ERR_OLD_STATEID:
549 exception->retry = 1;
551 case -NFS4ERR_BADOWNER:
552 /* The following works around a Linux server bug! */
553 case -NFS4ERR_BADNAME:
554 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
555 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
556 exception->retry = 1;
557 printk(KERN_WARNING "NFS: v4 server %s "
558 "does not accept raw "
560 "Reenabling the idmapper.\n",
561 server->nfs_client->cl_hostname);
564 /* We failed to handle the error */
565 return nfs4_map_errors(ret);
567 exception->recovering = 1;
571 /* This is the error handling routine for processes that are allowed
574 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
576 struct nfs_client *clp = server->nfs_client;
579 ret = nfs4_do_handle_exception(server, errorcode, exception);
580 if (exception->delay) {
581 ret = nfs4_delay(&exception->timeout,
582 exception->interruptible);
585 if (exception->recovering) {
586 ret = nfs4_wait_clnt_recover(clp);
587 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
594 exception->retry = 1;
599 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
600 int errorcode, struct nfs4_exception *exception)
602 struct nfs_client *clp = server->nfs_client;
605 ret = nfs4_do_handle_exception(server, errorcode, exception);
606 if (exception->delay) {
607 rpc_delay(task, nfs4_update_delay(&exception->timeout));
610 if (exception->recovering) {
611 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
612 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
613 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
616 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
621 exception->retry = 1;
623 * For NFS4ERR_MOVED, the client transport will need to
624 * be recomputed after migration recovery has completed.
626 if (errorcode == -NFS4ERR_MOVED)
627 rpc_task_release_transport(task);
633 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
634 struct nfs4_state *state, long *timeout)
636 struct nfs4_exception exception = {
640 if (task->tk_status >= 0)
643 exception.timeout = *timeout;
644 task->tk_status = nfs4_async_handle_exception(task, server,
647 if (exception.delay && timeout)
648 *timeout = exception.timeout;
655 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
656 * or 'false' otherwise.
658 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
660 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
661 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
664 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
666 spin_lock(&clp->cl_lock);
667 if (time_before(clp->cl_last_renewal,timestamp))
668 clp->cl_last_renewal = timestamp;
669 spin_unlock(&clp->cl_lock);
672 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
674 struct nfs_client *clp = server->nfs_client;
676 if (!nfs4_has_session(clp))
677 do_renew_lease(clp, timestamp);
680 struct nfs4_call_sync_data {
681 const struct nfs_server *seq_server;
682 struct nfs4_sequence_args *seq_args;
683 struct nfs4_sequence_res *seq_res;
686 void nfs4_init_sequence(struct nfs4_sequence_args *args,
687 struct nfs4_sequence_res *res, int cache_reply,
690 args->sa_slot = NULL;
691 args->sa_cache_this = cache_reply;
692 args->sa_privileged = privileged;
697 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
699 struct nfs4_slot *slot = res->sr_slot;
700 struct nfs4_slot_table *tbl;
703 spin_lock(&tbl->slot_tbl_lock);
704 if (!nfs41_wake_and_assign_slot(tbl, slot))
705 nfs4_free_slot(tbl, slot);
706 spin_unlock(&tbl->slot_tbl_lock);
711 static int nfs40_sequence_done(struct rpc_task *task,
712 struct nfs4_sequence_res *res)
714 if (res->sr_slot != NULL)
715 nfs40_sequence_free_slot(res);
719 #if defined(CONFIG_NFS_V4_1)
721 static void nfs41_release_slot(struct nfs4_slot *slot)
723 struct nfs4_session *session;
724 struct nfs4_slot_table *tbl;
725 bool send_new_highest_used_slotid = false;
730 session = tbl->session;
732 /* Bump the slot sequence number */
737 spin_lock(&tbl->slot_tbl_lock);
738 /* Be nice to the server: try to ensure that the last transmitted
739 * value for highest_user_slotid <= target_highest_slotid
741 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
742 send_new_highest_used_slotid = true;
744 if (nfs41_wake_and_assign_slot(tbl, slot)) {
745 send_new_highest_used_slotid = false;
748 nfs4_free_slot(tbl, slot);
750 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
751 send_new_highest_used_slotid = false;
753 spin_unlock(&tbl->slot_tbl_lock);
754 if (send_new_highest_used_slotid)
755 nfs41_notify_server(session->clp);
756 if (waitqueue_active(&tbl->slot_waitq))
757 wake_up_all(&tbl->slot_waitq);
760 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
762 nfs41_release_slot(res->sr_slot);
766 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
769 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
770 slot->seq_nr_highest_sent = seqnr;
772 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
775 slot->seq_nr_highest_sent = seqnr;
776 slot->seq_nr_last_acked = seqnr;
779 static int nfs41_sequence_process(struct rpc_task *task,
780 struct nfs4_sequence_res *res)
782 struct nfs4_session *session;
783 struct nfs4_slot *slot = res->sr_slot;
784 struct nfs_client *clp;
789 /* don't increment the sequence number if the task wasn't sent */
790 if (!RPC_WAS_SENT(task) || slot->seq_done)
793 session = slot->table->session;
795 trace_nfs4_sequence_done(session, res);
796 /* Check the SEQUENCE operation status */
797 switch (res->sr_status) {
799 /* Mark this sequence number as having been acked */
800 nfs4_slot_sequence_acked(slot, slot->seq_nr);
801 /* Update the slot's sequence and clientid lease timer */
804 do_renew_lease(clp, res->sr_timestamp);
805 /* Check sequence flags */
806 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
808 nfs41_update_target_slotid(slot->table, slot, res);
812 * sr_status remains 1 if an RPC level error occurred.
813 * The server may or may not have processed the sequence
816 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
820 /* The server detected a resend of the RPC call and
821 * returned NFS4ERR_DELAY as per Section 2.10.6.2
824 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
828 nfs4_slot_sequence_acked(slot, slot->seq_nr);
830 case -NFS4ERR_RETRY_UNCACHED_REP:
831 case -NFS4ERR_SEQ_FALSE_RETRY:
833 * The server thinks we tried to replay a request.
834 * Retry the call after bumping the sequence ID.
836 nfs4_slot_sequence_acked(slot, slot->seq_nr);
838 case -NFS4ERR_BADSLOT:
840 * The slot id we used was probably retired. Try again
841 * using a different slot id.
843 if (slot->slot_nr < slot->table->target_highest_slotid)
844 goto session_recover;
846 case -NFS4ERR_SEQ_MISORDERED:
847 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
849 * Were one or more calls using this slot interrupted?
850 * If the server never received the request, then our
851 * transmitted slot sequence number may be too high.
853 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
859 * A retry might be sent while the original request is
860 * still in progress on the replier. The replier SHOULD
861 * deal with the issue by returning NFS4ERR_DELAY as the
862 * reply to SEQUENCE or CB_SEQUENCE operation, but
863 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
865 * Restart the search after a delay.
867 slot->seq_nr = slot->seq_nr_highest_sent;
870 /* Just update the slot sequence no. */
874 /* The session may be reset by one of the error handlers. */
875 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
879 nfs4_schedule_session_recovery(session, res->sr_status);
884 if (rpc_restart_call_prepare(task)) {
885 nfs41_sequence_free_slot(res);
891 if (!rpc_restart_call(task))
893 rpc_delay(task, NFS4_POLL_RETRY_MAX);
897 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
899 if (!nfs41_sequence_process(task, res))
901 if (res->sr_slot != NULL)
902 nfs41_sequence_free_slot(res);
906 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
908 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
910 if (res->sr_slot == NULL)
912 if (res->sr_slot->table->session != NULL)
913 return nfs41_sequence_process(task, res);
914 return nfs40_sequence_done(task, res);
917 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
919 if (res->sr_slot != NULL) {
920 if (res->sr_slot->table->session != NULL)
921 nfs41_sequence_free_slot(res);
923 nfs40_sequence_free_slot(res);
927 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
929 if (res->sr_slot == NULL)
931 if (!res->sr_slot->table->session)
932 return nfs40_sequence_done(task, res);
933 return nfs41_sequence_done(task, res);
935 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
937 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
939 struct nfs4_call_sync_data *data = calldata;
941 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
943 nfs4_setup_sequence(data->seq_server->nfs_client,
944 data->seq_args, data->seq_res, task);
947 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
949 struct nfs4_call_sync_data *data = calldata;
951 nfs41_sequence_done(task, data->seq_res);
954 static const struct rpc_call_ops nfs41_call_sync_ops = {
955 .rpc_call_prepare = nfs41_call_sync_prepare,
956 .rpc_call_done = nfs41_call_sync_done,
959 #else /* !CONFIG_NFS_V4_1 */
961 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
963 return nfs40_sequence_done(task, res);
966 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
968 if (res->sr_slot != NULL)
969 nfs40_sequence_free_slot(res);
972 int nfs4_sequence_done(struct rpc_task *task,
973 struct nfs4_sequence_res *res)
975 return nfs40_sequence_done(task, res);
977 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
979 #endif /* !CONFIG_NFS_V4_1 */
981 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
983 res->sr_timestamp = jiffies;
984 res->sr_status_flags = 0;
989 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
990 struct nfs4_sequence_res *res,
991 struct nfs4_slot *slot)
995 slot->privileged = args->sa_privileged ? 1 : 0;
996 args->sa_slot = slot;
1001 int nfs4_setup_sequence(struct nfs_client *client,
1002 struct nfs4_sequence_args *args,
1003 struct nfs4_sequence_res *res,
1004 struct rpc_task *task)
1006 struct nfs4_session *session = nfs4_get_session(client);
1007 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1008 struct nfs4_slot *slot;
1010 /* slot already allocated? */
1011 if (res->sr_slot != NULL)
1015 tbl = &session->fc_slot_table;
1017 spin_lock(&tbl->slot_tbl_lock);
1018 /* The state manager will wait until the slot table is empty */
1019 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1022 slot = nfs4_alloc_slot(tbl);
1024 if (slot == ERR_PTR(-ENOMEM))
1025 goto out_sleep_timeout;
1028 spin_unlock(&tbl->slot_tbl_lock);
1030 nfs4_sequence_attach_slot(args, res, slot);
1032 trace_nfs4_setup_sequence(session, args);
1034 nfs41_sequence_res_init(res);
1035 rpc_call_start(task);
1038 /* Try again in 1/4 second */
1039 if (args->sa_privileged)
1040 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1041 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1043 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1044 NULL, jiffies + (HZ >> 2));
1045 spin_unlock(&tbl->slot_tbl_lock);
1048 if (args->sa_privileged)
1049 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1050 RPC_PRIORITY_PRIVILEGED);
1052 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1053 spin_unlock(&tbl->slot_tbl_lock);
1056 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1058 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1060 struct nfs4_call_sync_data *data = calldata;
1061 nfs4_setup_sequence(data->seq_server->nfs_client,
1062 data->seq_args, data->seq_res, task);
1065 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1067 struct nfs4_call_sync_data *data = calldata;
1068 nfs4_sequence_done(task, data->seq_res);
1071 static const struct rpc_call_ops nfs40_call_sync_ops = {
1072 .rpc_call_prepare = nfs40_call_sync_prepare,
1073 .rpc_call_done = nfs40_call_sync_done,
1076 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1079 struct rpc_task *task;
1081 task = rpc_run_task(task_setup);
1083 return PTR_ERR(task);
1085 ret = task->tk_status;
1090 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1091 struct nfs_server *server,
1092 struct rpc_message *msg,
1093 struct nfs4_sequence_args *args,
1094 struct nfs4_sequence_res *res)
1096 struct nfs_client *clp = server->nfs_client;
1097 struct nfs4_call_sync_data data = {
1098 .seq_server = server,
1102 struct rpc_task_setup task_setup = {
1105 .callback_ops = clp->cl_mvops->call_sync_ops,
1106 .callback_data = &data
1109 return nfs4_call_sync_custom(&task_setup);
1112 int nfs4_call_sync(struct rpc_clnt *clnt,
1113 struct nfs_server *server,
1114 struct rpc_message *msg,
1115 struct nfs4_sequence_args *args,
1116 struct nfs4_sequence_res *res,
1119 nfs4_init_sequence(args, res, cache_reply, 0);
1120 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1124 nfs4_inc_nlink_locked(struct inode *inode)
1126 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1131 nfs4_dec_nlink_locked(struct inode *inode)
1133 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1138 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1139 unsigned long timestamp, unsigned long cache_validity)
1141 struct nfs_inode *nfsi = NFS_I(dir);
1143 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1144 | NFS_INO_INVALID_MTIME
1145 | NFS_INO_INVALID_DATA
1147 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1148 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1149 nfsi->attrtimeo_timestamp = jiffies;
1151 nfs_force_lookup_revalidate(dir);
1152 if (cinfo->before != inode_peek_iversion_raw(dir))
1153 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1154 NFS_INO_INVALID_ACL;
1156 inode_set_iversion_raw(dir, cinfo->after);
1157 nfsi->read_cache_jiffies = timestamp;
1158 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1159 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1160 nfs_fscache_invalidate(dir);
1164 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1165 unsigned long timestamp, unsigned long cache_validity)
1167 spin_lock(&dir->i_lock);
1168 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1169 spin_unlock(&dir->i_lock);
1172 struct nfs4_open_createattrs {
1173 struct nfs4_label *label;
1174 struct iattr *sattr;
1175 const __u32 verf[2];
1178 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1179 int err, struct nfs4_exception *exception)
1183 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1185 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1186 exception->retry = 1;
1190 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1192 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1195 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1197 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1199 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1203 nfs4_map_atomic_open_share(struct nfs_server *server,
1204 fmode_t fmode, int openflags)
1208 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1210 res = NFS4_SHARE_ACCESS_READ;
1213 res = NFS4_SHARE_ACCESS_WRITE;
1215 case FMODE_READ|FMODE_WRITE:
1216 res = NFS4_SHARE_ACCESS_BOTH;
1218 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1220 /* Want no delegation if we're using O_DIRECT */
1221 if (openflags & O_DIRECT)
1222 res |= NFS4_SHARE_WANT_NO_DELEG;
1227 static enum open_claim_type4
1228 nfs4_map_atomic_open_claim(struct nfs_server *server,
1229 enum open_claim_type4 claim)
1231 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1236 case NFS4_OPEN_CLAIM_FH:
1237 return NFS4_OPEN_CLAIM_NULL;
1238 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1239 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1240 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1241 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1245 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1247 p->o_res.f_attr = &p->f_attr;
1248 p->o_res.f_label = p->f_label;
1249 p->o_res.seqid = p->o_arg.seqid;
1250 p->c_res.seqid = p->c_arg.seqid;
1251 p->o_res.server = p->o_arg.server;
1252 p->o_res.access_request = p->o_arg.access;
1253 nfs_fattr_init(&p->f_attr);
1254 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1257 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1258 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1259 const struct nfs4_open_createattrs *c,
1260 enum open_claim_type4 claim,
1263 struct dentry *parent = dget_parent(dentry);
1264 struct inode *dir = d_inode(parent);
1265 struct nfs_server *server = NFS_SERVER(dir);
1266 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1267 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1268 struct nfs4_opendata *p;
1270 p = kzalloc(sizeof(*p), gfp_mask);
1274 p->f_label = nfs4_label_alloc(server, gfp_mask);
1275 if (IS_ERR(p->f_label))
1278 p->a_label = nfs4_label_alloc(server, gfp_mask);
1279 if (IS_ERR(p->a_label))
1282 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1283 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1284 if (IS_ERR(p->o_arg.seqid))
1285 goto err_free_label;
1286 nfs_sb_active(dentry->d_sb);
1287 p->dentry = dget(dentry);
1290 atomic_inc(&sp->so_count);
1291 p->o_arg.open_flags = flags;
1292 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1293 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1294 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1296 if (flags & O_CREAT) {
1297 p->o_arg.umask = current_umask();
1298 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1299 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1300 p->o_arg.u.attrs = &p->attrs;
1301 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1303 memcpy(p->o_arg.u.verifier.data, c->verf,
1304 sizeof(p->o_arg.u.verifier.data));
1307 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1308 * will return permission denied for all bits until close */
1309 if (!(flags & O_EXCL)) {
1310 /* ask server to check for all possible rights as results
1312 switch (p->o_arg.claim) {
1315 case NFS4_OPEN_CLAIM_NULL:
1316 case NFS4_OPEN_CLAIM_FH:
1317 p->o_arg.access = NFS4_ACCESS_READ |
1318 NFS4_ACCESS_MODIFY |
1319 NFS4_ACCESS_EXTEND |
1320 NFS4_ACCESS_EXECUTE;
1323 p->o_arg.clientid = server->nfs_client->cl_clientid;
1324 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1325 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1326 p->o_arg.name = &dentry->d_name;
1327 p->o_arg.server = server;
1328 p->o_arg.bitmask = nfs4_bitmask(server, label);
1329 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1330 switch (p->o_arg.claim) {
1331 case NFS4_OPEN_CLAIM_NULL:
1332 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1333 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1334 p->o_arg.fh = NFS_FH(dir);
1336 case NFS4_OPEN_CLAIM_PREVIOUS:
1337 case NFS4_OPEN_CLAIM_FH:
1338 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1339 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1340 p->o_arg.fh = NFS_FH(d_inode(dentry));
1342 p->c_arg.fh = &p->o_res.fh;
1343 p->c_arg.stateid = &p->o_res.stateid;
1344 p->c_arg.seqid = p->o_arg.seqid;
1345 nfs4_init_opendata_res(p);
1346 kref_init(&p->kref);
1350 nfs4_label_free(p->a_label);
1352 nfs4_label_free(p->f_label);
1360 static void nfs4_opendata_free(struct kref *kref)
1362 struct nfs4_opendata *p = container_of(kref,
1363 struct nfs4_opendata, kref);
1364 struct super_block *sb = p->dentry->d_sb;
1366 nfs4_lgopen_release(p->lgp);
1367 nfs_free_seqid(p->o_arg.seqid);
1368 nfs4_sequence_free_slot(&p->o_res.seq_res);
1369 if (p->state != NULL)
1370 nfs4_put_open_state(p->state);
1371 nfs4_put_state_owner(p->owner);
1373 nfs4_label_free(p->a_label);
1374 nfs4_label_free(p->f_label);
1378 nfs_sb_deactive(sb);
1379 nfs_fattr_free_names(&p->f_attr);
1380 kfree(p->f_attr.mdsthreshold);
1384 static void nfs4_opendata_put(struct nfs4_opendata *p)
1387 kref_put(&p->kref, nfs4_opendata_free);
1390 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1393 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1394 case FMODE_READ|FMODE_WRITE:
1395 return state->n_rdwr != 0;
1397 return state->n_wronly != 0;
1399 return state->n_rdonly != 0;
1405 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1406 int open_mode, enum open_claim_type4 claim)
1410 if (open_mode & (O_EXCL|O_TRUNC))
1413 case NFS4_OPEN_CLAIM_NULL:
1414 case NFS4_OPEN_CLAIM_FH:
1419 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1421 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1422 && state->n_rdonly != 0;
1425 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1426 && state->n_wronly != 0;
1428 case FMODE_READ|FMODE_WRITE:
1429 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1430 && state->n_rdwr != 0;
1436 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1437 enum open_claim_type4 claim)
1439 if (delegation == NULL)
1441 if ((delegation->type & fmode) != fmode)
1444 case NFS4_OPEN_CLAIM_NULL:
1445 case NFS4_OPEN_CLAIM_FH:
1447 case NFS4_OPEN_CLAIM_PREVIOUS:
1448 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1454 nfs_mark_delegation_referenced(delegation);
1458 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1467 case FMODE_READ|FMODE_WRITE:
1470 nfs4_state_set_mode_locked(state, state->state | fmode);
1473 #ifdef CONFIG_NFS_V4_1
1474 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1476 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1478 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1480 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1484 #endif /* CONFIG_NFS_V4_1 */
1486 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1488 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1489 wake_up_all(&state->waitq);
1492 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1493 const nfs4_stateid *stateid)
1495 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1496 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1498 if (stateid_seqid == state_seqid + 1U ||
1499 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1500 nfs_state_log_update_open_stateid(state);
1502 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1505 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1507 struct nfs_client *clp = state->owner->so_server->nfs_client;
1508 bool need_recover = false;
1510 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1511 need_recover = true;
1512 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1513 need_recover = true;
1514 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1515 need_recover = true;
1517 nfs4_state_mark_reclaim_nograce(clp, state);
1521 * Check for whether or not the caller may update the open stateid
1522 * to the value passed in by stateid.
1524 * Note: This function relies heavily on the server implementing
1525 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1527 * i.e. The stateid seqids have to be initialised to 1, and
1528 * are then incremented on every state transition.
1530 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1531 const nfs4_stateid *stateid)
1533 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1534 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1535 if (stateid->seqid == cpu_to_be32(1))
1536 nfs_state_log_update_open_stateid(state);
1538 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1542 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1543 nfs_state_log_out_of_order_open_stateid(state, stateid);
1549 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1551 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1553 if (state->n_wronly)
1554 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1555 if (state->n_rdonly)
1556 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1558 set_bit(NFS_O_RDWR_STATE, &state->flags);
1559 set_bit(NFS_OPEN_STATE, &state->flags);
1562 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1563 nfs4_stateid *stateid, fmode_t fmode)
1565 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1566 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1568 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1571 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1574 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1575 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1576 clear_bit(NFS_OPEN_STATE, &state->flags);
1578 if (stateid == NULL)
1580 /* Handle OPEN+OPEN_DOWNGRADE races */
1581 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1582 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1583 nfs_resync_open_stateid_locked(state);
1586 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1587 nfs4_stateid_copy(&state->stateid, stateid);
1588 nfs4_stateid_copy(&state->open_stateid, stateid);
1589 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1591 nfs_state_log_update_open_stateid(state);
1594 static void nfs_clear_open_stateid(struct nfs4_state *state,
1595 nfs4_stateid *arg_stateid,
1596 nfs4_stateid *stateid, fmode_t fmode)
1598 write_seqlock(&state->seqlock);
1599 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1600 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1601 nfs_clear_open_stateid_locked(state, stateid, fmode);
1602 write_sequnlock(&state->seqlock);
1603 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1604 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1607 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1608 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1609 __must_hold(&state->owner->so_lock)
1610 __must_hold(&state->seqlock)
1618 if (!nfs_need_update_open_stateid(state, stateid))
1620 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1624 /* Rely on seqids for serialisation with NFSv4.0 */
1625 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1628 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1630 * Ensure we process the state changes in the same order
1631 * in which the server processed them by delaying the
1632 * update of the stateid until we are in sequence.
1634 write_sequnlock(&state->seqlock);
1635 spin_unlock(&state->owner->so_lock);
1637 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1638 if (!signal_pending(current)) {
1639 if (schedule_timeout(5*HZ) == 0)
1645 finish_wait(&state->waitq, &wait);
1647 spin_lock(&state->owner->so_lock);
1648 write_seqlock(&state->seqlock);
1651 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1652 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1653 nfs4_stateid_copy(freeme, &state->open_stateid);
1654 nfs_test_and_clear_all_open_stateid(state);
1657 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1658 nfs4_stateid_copy(&state->stateid, stateid);
1659 nfs4_stateid_copy(&state->open_stateid, stateid);
1660 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1661 nfs_state_log_update_open_stateid(state);
1664 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1665 const nfs4_stateid *open_stateid,
1667 nfs4_stateid *freeme)
1670 * Protect the call to nfs4_state_set_mode_locked and
1671 * serialise the stateid update
1673 write_seqlock(&state->seqlock);
1674 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1677 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1680 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1682 case FMODE_READ|FMODE_WRITE:
1683 set_bit(NFS_O_RDWR_STATE, &state->flags);
1685 set_bit(NFS_OPEN_STATE, &state->flags);
1686 write_sequnlock(&state->seqlock);
1689 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1691 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1692 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1693 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1694 clear_bit(NFS_OPEN_STATE, &state->flags);
1697 static void nfs_state_set_delegation(struct nfs4_state *state,
1698 const nfs4_stateid *deleg_stateid,
1702 * Protect the call to nfs4_state_set_mode_locked and
1703 * serialise the stateid update
1705 write_seqlock(&state->seqlock);
1706 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1707 set_bit(NFS_DELEGATED_STATE, &state->flags);
1708 write_sequnlock(&state->seqlock);
1711 static void nfs_state_clear_delegation(struct nfs4_state *state)
1713 write_seqlock(&state->seqlock);
1714 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1715 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1716 write_sequnlock(&state->seqlock);
1719 static int update_open_stateid(struct nfs4_state *state,
1720 const nfs4_stateid *open_stateid,
1721 const nfs4_stateid *delegation,
1724 struct nfs_server *server = NFS_SERVER(state->inode);
1725 struct nfs_client *clp = server->nfs_client;
1726 struct nfs_inode *nfsi = NFS_I(state->inode);
1727 struct nfs_delegation *deleg_cur;
1728 nfs4_stateid freeme = { };
1731 fmode &= (FMODE_READ|FMODE_WRITE);
1734 spin_lock(&state->owner->so_lock);
1735 if (open_stateid != NULL) {
1736 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1740 deleg_cur = rcu_dereference(nfsi->delegation);
1741 if (deleg_cur == NULL)
1744 spin_lock(&deleg_cur->lock);
1745 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1746 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1747 (deleg_cur->type & fmode) != fmode)
1748 goto no_delegation_unlock;
1750 if (delegation == NULL)
1751 delegation = &deleg_cur->stateid;
1752 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1753 goto no_delegation_unlock;
1755 nfs_mark_delegation_referenced(deleg_cur);
1756 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1758 no_delegation_unlock:
1759 spin_unlock(&deleg_cur->lock);
1762 update_open_stateflags(state, fmode);
1763 spin_unlock(&state->owner->so_lock);
1766 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1767 nfs4_schedule_state_manager(clp);
1768 if (freeme.type != 0)
1769 nfs4_test_and_free_stateid(server, &freeme,
1770 state->owner->so_cred);
1775 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1776 const nfs4_stateid *stateid)
1778 struct nfs4_state *state = lsp->ls_state;
1781 spin_lock(&state->state_lock);
1782 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1784 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1786 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1789 spin_unlock(&state->state_lock);
1793 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1795 struct nfs_delegation *delegation;
1797 fmode &= FMODE_READ|FMODE_WRITE;
1799 delegation = rcu_dereference(NFS_I(inode)->delegation);
1800 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1805 nfs4_inode_return_delegation(inode);
1808 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1810 struct nfs4_state *state = opendata->state;
1811 struct nfs_delegation *delegation;
1812 int open_mode = opendata->o_arg.open_flags;
1813 fmode_t fmode = opendata->o_arg.fmode;
1814 enum open_claim_type4 claim = opendata->o_arg.claim;
1815 nfs4_stateid stateid;
1819 spin_lock(&state->owner->so_lock);
1820 if (can_open_cached(state, fmode, open_mode, claim)) {
1821 update_open_stateflags(state, fmode);
1822 spin_unlock(&state->owner->so_lock);
1823 goto out_return_state;
1825 spin_unlock(&state->owner->so_lock);
1827 delegation = nfs4_get_valid_delegation(state->inode);
1828 if (!can_open_delegated(delegation, fmode, claim)) {
1832 /* Save the delegation */
1833 nfs4_stateid_copy(&stateid, &delegation->stateid);
1835 nfs_release_seqid(opendata->o_arg.seqid);
1836 if (!opendata->is_recover) {
1837 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1843 /* Try to update the stateid using the delegation */
1844 if (update_open_stateid(state, NULL, &stateid, fmode))
1845 goto out_return_state;
1848 return ERR_PTR(ret);
1850 refcount_inc(&state->count);
1855 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1857 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1858 struct nfs_delegation *delegation;
1859 int delegation_flags = 0;
1862 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1864 delegation_flags = delegation->flags;
1866 switch (data->o_arg.claim) {
1869 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1870 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1871 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1872 "returning a delegation for "
1873 "OPEN(CLAIM_DELEGATE_CUR)\n",
1877 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1878 nfs_inode_set_delegation(state->inode,
1879 data->owner->so_cred,
1880 data->o_res.delegation_type,
1881 &data->o_res.delegation,
1882 data->o_res.pagemod_limit);
1884 nfs_inode_reclaim_delegation(state->inode,
1885 data->owner->so_cred,
1886 data->o_res.delegation_type,
1887 &data->o_res.delegation,
1888 data->o_res.pagemod_limit);
1890 if (data->o_res.do_recall)
1891 nfs_async_inode_return_delegation(state->inode,
1892 &data->o_res.delegation);
1896 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1897 * and update the nfs4_state.
1899 static struct nfs4_state *
1900 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1902 struct inode *inode = data->state->inode;
1903 struct nfs4_state *state = data->state;
1906 if (!data->rpc_done) {
1907 if (data->rpc_status)
1908 return ERR_PTR(data->rpc_status);
1909 /* cached opens have already been processed */
1913 ret = nfs_refresh_inode(inode, &data->f_attr);
1915 return ERR_PTR(ret);
1917 if (data->o_res.delegation_type != 0)
1918 nfs4_opendata_check_deleg(data, state);
1920 if (!update_open_stateid(state, &data->o_res.stateid,
1921 NULL, data->o_arg.fmode))
1922 return ERR_PTR(-EAGAIN);
1923 refcount_inc(&state->count);
1928 static struct inode *
1929 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1931 struct inode *inode;
1933 switch (data->o_arg.claim) {
1934 case NFS4_OPEN_CLAIM_NULL:
1935 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1936 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1937 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1938 return ERR_PTR(-EAGAIN);
1939 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1940 &data->f_attr, data->f_label);
1943 inode = d_inode(data->dentry);
1945 nfs_refresh_inode(inode, &data->f_attr);
1950 static struct nfs4_state *
1951 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1953 struct nfs4_state *state;
1954 struct inode *inode;
1956 inode = nfs4_opendata_get_inode(data);
1958 return ERR_CAST(inode);
1959 if (data->state != NULL && data->state->inode == inode) {
1960 state = data->state;
1961 refcount_inc(&state->count);
1963 state = nfs4_get_open_state(inode, data->owner);
1966 state = ERR_PTR(-ENOMEM);
1970 static struct nfs4_state *
1971 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1973 struct nfs4_state *state;
1975 if (!data->rpc_done) {
1976 state = nfs4_try_open_cached(data);
1977 trace_nfs4_cached_open(data->state);
1981 state = nfs4_opendata_find_nfs4_state(data);
1985 if (data->o_res.delegation_type != 0)
1986 nfs4_opendata_check_deleg(data, state);
1987 if (!update_open_stateid(state, &data->o_res.stateid,
1988 NULL, data->o_arg.fmode)) {
1989 nfs4_put_open_state(state);
1990 state = ERR_PTR(-EAGAIN);
1993 nfs_release_seqid(data->o_arg.seqid);
1997 static struct nfs4_state *
1998 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2000 struct nfs4_state *ret;
2002 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2003 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2005 ret = _nfs4_opendata_to_nfs4_state(data);
2006 nfs4_sequence_free_slot(&data->o_res.seq_res);
2010 static struct nfs_open_context *
2011 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2013 struct nfs_inode *nfsi = NFS_I(state->inode);
2014 struct nfs_open_context *ctx;
2017 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2018 if (ctx->state != state)
2020 if ((ctx->mode & mode) != mode)
2022 if (!get_nfs_open_context(ctx))
2028 return ERR_PTR(-ENOENT);
2031 static struct nfs_open_context *
2032 nfs4_state_find_open_context(struct nfs4_state *state)
2034 struct nfs_open_context *ctx;
2036 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2039 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2042 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2045 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2046 struct nfs4_state *state, enum open_claim_type4 claim)
2048 struct nfs4_opendata *opendata;
2050 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2051 NULL, claim, GFP_NOFS);
2052 if (opendata == NULL)
2053 return ERR_PTR(-ENOMEM);
2054 opendata->state = state;
2055 refcount_inc(&state->count);
2059 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2062 struct nfs4_state *newstate;
2065 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2067 opendata->o_arg.open_flags = 0;
2068 opendata->o_arg.fmode = fmode;
2069 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2070 NFS_SB(opendata->dentry->d_sb),
2072 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2073 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2074 nfs4_init_opendata_res(opendata);
2075 ret = _nfs4_recover_proc_open(opendata);
2078 newstate = nfs4_opendata_to_nfs4_state(opendata);
2079 if (IS_ERR(newstate))
2080 return PTR_ERR(newstate);
2081 if (newstate != opendata->state)
2083 nfs4_close_state(newstate, fmode);
2087 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2091 /* memory barrier prior to reading state->n_* */
2093 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2096 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2099 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2103 * We may have performed cached opens for all three recoveries.
2104 * Check if we need to update the current stateid.
2106 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2107 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2108 write_seqlock(&state->seqlock);
2109 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2110 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2111 write_sequnlock(&state->seqlock);
2118 * reclaim state on the server after a reboot.
2120 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2122 struct nfs_delegation *delegation;
2123 struct nfs4_opendata *opendata;
2124 fmode_t delegation_type = 0;
2127 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2128 NFS4_OPEN_CLAIM_PREVIOUS);
2129 if (IS_ERR(opendata))
2130 return PTR_ERR(opendata);
2132 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2133 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2134 delegation_type = delegation->type;
2136 opendata->o_arg.u.delegation_type = delegation_type;
2137 status = nfs4_open_recover(opendata, state);
2138 nfs4_opendata_put(opendata);
2142 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2144 struct nfs_server *server = NFS_SERVER(state->inode);
2145 struct nfs4_exception exception = { };
2148 err = _nfs4_do_open_reclaim(ctx, state);
2149 trace_nfs4_open_reclaim(ctx, 0, err);
2150 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2152 if (err != -NFS4ERR_DELAY)
2154 nfs4_handle_exception(server, err, &exception);
2155 } while (exception.retry);
2159 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2161 struct nfs_open_context *ctx;
2164 ctx = nfs4_state_find_open_context(state);
2167 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2168 nfs_state_clear_open_state_flags(state);
2169 ret = nfs4_do_open_reclaim(ctx, state);
2170 put_nfs_open_context(ctx);
2174 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2178 printk(KERN_ERR "NFS: %s: unhandled error "
2179 "%d.\n", __func__, err);
2186 case -NFS4ERR_BADSESSION:
2187 case -NFS4ERR_BADSLOT:
2188 case -NFS4ERR_BAD_HIGH_SLOT:
2189 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2190 case -NFS4ERR_DEADSESSION:
2191 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2193 case -NFS4ERR_STALE_CLIENTID:
2194 case -NFS4ERR_STALE_STATEID:
2195 /* Don't recall a delegation if it was lost */
2196 nfs4_schedule_lease_recovery(server->nfs_client);
2198 case -NFS4ERR_MOVED:
2199 nfs4_schedule_migration_recovery(server);
2201 case -NFS4ERR_LEASE_MOVED:
2202 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2204 case -NFS4ERR_DELEG_REVOKED:
2205 case -NFS4ERR_ADMIN_REVOKED:
2206 case -NFS4ERR_EXPIRED:
2207 case -NFS4ERR_BAD_STATEID:
2208 case -NFS4ERR_OPENMODE:
2209 nfs_inode_find_state_and_recover(state->inode,
2211 nfs4_schedule_stateid_recovery(server, state);
2213 case -NFS4ERR_DELAY:
2214 case -NFS4ERR_GRACE:
2218 case -NFS4ERR_DENIED:
2220 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2222 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2229 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2230 struct nfs4_state *state, const nfs4_stateid *stateid)
2232 struct nfs_server *server = NFS_SERVER(state->inode);
2233 struct nfs4_opendata *opendata;
2236 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2237 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2238 if (IS_ERR(opendata))
2239 return PTR_ERR(opendata);
2240 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2241 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2242 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2246 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2247 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2251 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2252 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2256 nfs_state_clear_delegation(state);
2258 nfs4_opendata_put(opendata);
2259 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2262 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2264 struct nfs4_opendata *data = calldata;
2266 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2267 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2270 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2272 struct nfs4_opendata *data = calldata;
2274 nfs40_sequence_done(task, &data->c_res.seq_res);
2276 data->rpc_status = task->tk_status;
2277 if (data->rpc_status == 0) {
2278 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2279 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2280 renew_lease(data->o_res.server, data->timestamp);
2281 data->rpc_done = true;
2285 static void nfs4_open_confirm_release(void *calldata)
2287 struct nfs4_opendata *data = calldata;
2288 struct nfs4_state *state = NULL;
2290 /* If this request hasn't been cancelled, do nothing */
2291 if (!data->cancelled)
2293 /* In case of error, no cleanup! */
2294 if (!data->rpc_done)
2296 state = nfs4_opendata_to_nfs4_state(data);
2298 nfs4_close_state(state, data->o_arg.fmode);
2300 nfs4_opendata_put(data);
2303 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2304 .rpc_call_prepare = nfs4_open_confirm_prepare,
2305 .rpc_call_done = nfs4_open_confirm_done,
2306 .rpc_release = nfs4_open_confirm_release,
2310 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2312 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2314 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2315 struct rpc_task *task;
2316 struct rpc_message msg = {
2317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2318 .rpc_argp = &data->c_arg,
2319 .rpc_resp = &data->c_res,
2320 .rpc_cred = data->owner->so_cred,
2322 struct rpc_task_setup task_setup_data = {
2323 .rpc_client = server->client,
2324 .rpc_message = &msg,
2325 .callback_ops = &nfs4_open_confirm_ops,
2326 .callback_data = data,
2327 .workqueue = nfsiod_workqueue,
2328 .flags = RPC_TASK_ASYNC,
2332 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2334 kref_get(&data->kref);
2335 data->rpc_done = false;
2336 data->rpc_status = 0;
2337 data->timestamp = jiffies;
2338 task = rpc_run_task(&task_setup_data);
2340 return PTR_ERR(task);
2341 status = rpc_wait_for_completion_task(task);
2343 data->cancelled = true;
2346 status = data->rpc_status;
2351 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2353 struct nfs4_opendata *data = calldata;
2354 struct nfs4_state_owner *sp = data->owner;
2355 struct nfs_client *clp = sp->so_server->nfs_client;
2356 enum open_claim_type4 claim = data->o_arg.claim;
2358 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2361 * Check if we still need to send an OPEN call, or if we can use
2362 * a delegation instead.
2364 if (data->state != NULL) {
2365 struct nfs_delegation *delegation;
2367 if (can_open_cached(data->state, data->o_arg.fmode,
2368 data->o_arg.open_flags, claim))
2371 delegation = nfs4_get_valid_delegation(data->state->inode);
2372 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2373 goto unlock_no_action;
2376 /* Update client id. */
2377 data->o_arg.clientid = clp->cl_clientid;
2381 case NFS4_OPEN_CLAIM_PREVIOUS:
2382 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2383 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2384 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2386 case NFS4_OPEN_CLAIM_FH:
2387 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2389 data->timestamp = jiffies;
2390 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2391 &data->o_arg.seq_args,
2392 &data->o_res.seq_res,
2394 nfs_release_seqid(data->o_arg.seqid);
2396 /* Set the create mode (note dependency on the session type) */
2397 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2398 if (data->o_arg.open_flags & O_EXCL) {
2399 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2400 if (nfs4_has_persistent_session(clp))
2401 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2402 else if (clp->cl_mvops->minor_version > 0)
2403 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2407 trace_nfs4_cached_open(data->state);
2410 task->tk_action = NULL;
2412 nfs4_sequence_done(task, &data->o_res.seq_res);
2415 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2417 struct nfs4_opendata *data = calldata;
2419 data->rpc_status = task->tk_status;
2421 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2424 if (task->tk_status == 0) {
2425 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2426 switch (data->o_res.f_attr->mode & S_IFMT) {
2430 data->rpc_status = -ELOOP;
2433 data->rpc_status = -EISDIR;
2436 data->rpc_status = -ENOTDIR;
2439 renew_lease(data->o_res.server, data->timestamp);
2440 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2441 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2443 data->rpc_done = true;
2446 static void nfs4_open_release(void *calldata)
2448 struct nfs4_opendata *data = calldata;
2449 struct nfs4_state *state = NULL;
2451 /* If this request hasn't been cancelled, do nothing */
2452 if (!data->cancelled)
2454 /* In case of error, no cleanup! */
2455 if (data->rpc_status != 0 || !data->rpc_done)
2457 /* In case we need an open_confirm, no cleanup! */
2458 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2460 state = nfs4_opendata_to_nfs4_state(data);
2462 nfs4_close_state(state, data->o_arg.fmode);
2464 nfs4_opendata_put(data);
2467 static const struct rpc_call_ops nfs4_open_ops = {
2468 .rpc_call_prepare = nfs4_open_prepare,
2469 .rpc_call_done = nfs4_open_done,
2470 .rpc_release = nfs4_open_release,
2473 static int nfs4_run_open_task(struct nfs4_opendata *data,
2474 struct nfs_open_context *ctx)
2476 struct inode *dir = d_inode(data->dir);
2477 struct nfs_server *server = NFS_SERVER(dir);
2478 struct nfs_openargs *o_arg = &data->o_arg;
2479 struct nfs_openres *o_res = &data->o_res;
2480 struct rpc_task *task;
2481 struct rpc_message msg = {
2482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2485 .rpc_cred = data->owner->so_cred,
2487 struct rpc_task_setup task_setup_data = {
2488 .rpc_client = server->client,
2489 .rpc_message = &msg,
2490 .callback_ops = &nfs4_open_ops,
2491 .callback_data = data,
2492 .workqueue = nfsiod_workqueue,
2493 .flags = RPC_TASK_ASYNC,
2497 kref_get(&data->kref);
2498 data->rpc_done = false;
2499 data->rpc_status = 0;
2500 data->cancelled = false;
2501 data->is_recover = false;
2503 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2504 data->is_recover = true;
2505 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2507 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2508 pnfs_lgopen_prepare(data, ctx);
2510 task = rpc_run_task(&task_setup_data);
2512 return PTR_ERR(task);
2513 status = rpc_wait_for_completion_task(task);
2515 data->cancelled = true;
2518 status = data->rpc_status;
2524 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2526 struct inode *dir = d_inode(data->dir);
2527 struct nfs_openres *o_res = &data->o_res;
2530 status = nfs4_run_open_task(data, NULL);
2531 if (status != 0 || !data->rpc_done)
2534 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2536 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2537 status = _nfs4_proc_open_confirm(data);
2543 * Additional permission checks in order to distinguish between an
2544 * open for read, and an open for execute. This works around the
2545 * fact that NFSv4 OPEN treats read and execute permissions as being
2547 * Note that in the non-execute case, we want to turn off permission
2548 * checking if we just created a new file (POSIX open() semantics).
2550 static int nfs4_opendata_access(const struct cred *cred,
2551 struct nfs4_opendata *opendata,
2552 struct nfs4_state *state, fmode_t fmode,
2555 struct nfs_access_entry cache;
2558 /* access call failed or for some reason the server doesn't
2559 * support any access modes -- defer access call until later */
2560 if (opendata->o_res.access_supported == 0)
2565 * Use openflags to check for exec, because fmode won't
2566 * always have FMODE_EXEC set when file open for exec.
2568 if (openflags & __FMODE_EXEC) {
2569 /* ONLY check for exec rights */
2570 if (S_ISDIR(state->inode->i_mode))
2571 mask = NFS4_ACCESS_LOOKUP;
2573 mask = NFS4_ACCESS_EXECUTE;
2574 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2575 mask = NFS4_ACCESS_READ;
2578 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2579 nfs_access_add_cache(state->inode, &cache);
2581 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2582 if ((mask & ~cache.mask & flags) == 0)
2589 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2591 static int _nfs4_proc_open(struct nfs4_opendata *data,
2592 struct nfs_open_context *ctx)
2594 struct inode *dir = d_inode(data->dir);
2595 struct nfs_server *server = NFS_SERVER(dir);
2596 struct nfs_openargs *o_arg = &data->o_arg;
2597 struct nfs_openres *o_res = &data->o_res;
2600 status = nfs4_run_open_task(data, ctx);
2601 if (!data->rpc_done)
2604 if (status == -NFS4ERR_BADNAME &&
2605 !(o_arg->open_flags & O_CREAT))
2610 nfs_fattr_map_and_free_names(server, &data->f_attr);
2612 if (o_arg->open_flags & O_CREAT) {
2613 if (o_arg->open_flags & O_EXCL)
2614 data->file_created = true;
2615 else if (o_res->cinfo.before != o_res->cinfo.after)
2616 data->file_created = true;
2617 if (data->file_created ||
2618 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2619 update_changeattr(dir, &o_res->cinfo,
2620 o_res->f_attr->time_start, 0);
2622 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2623 server->caps &= ~NFS_CAP_POSIX_LOCK;
2624 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2625 status = _nfs4_proc_open_confirm(data);
2629 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2630 nfs4_sequence_free_slot(&o_res->seq_res);
2631 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2632 o_res->f_label, NULL);
2639 * reclaim state on the server after a network partition.
2640 * Assumes caller holds the appropriate lock
2642 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2644 struct nfs4_opendata *opendata;
2647 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2648 NFS4_OPEN_CLAIM_FH);
2649 if (IS_ERR(opendata))
2650 return PTR_ERR(opendata);
2651 ret = nfs4_open_recover(opendata, state);
2653 d_drop(ctx->dentry);
2654 nfs4_opendata_put(opendata);
2658 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2660 struct nfs_server *server = NFS_SERVER(state->inode);
2661 struct nfs4_exception exception = { };
2665 err = _nfs4_open_expired(ctx, state);
2666 trace_nfs4_open_expired(ctx, 0, err);
2667 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2672 case -NFS4ERR_GRACE:
2673 case -NFS4ERR_DELAY:
2674 nfs4_handle_exception(server, err, &exception);
2677 } while (exception.retry);
2682 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2684 struct nfs_open_context *ctx;
2687 ctx = nfs4_state_find_open_context(state);
2690 ret = nfs4_do_open_expired(ctx, state);
2691 put_nfs_open_context(ctx);
2695 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2696 const nfs4_stateid *stateid)
2698 nfs_remove_bad_delegation(state->inode, stateid);
2699 nfs_state_clear_delegation(state);
2702 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2704 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2705 nfs_finish_clear_delegation_stateid(state, NULL);
2708 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2710 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2711 nfs40_clear_delegation_stateid(state);
2712 nfs_state_clear_open_state_flags(state);
2713 return nfs4_open_expired(sp, state);
2716 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2717 nfs4_stateid *stateid,
2718 const struct cred *cred)
2720 return -NFS4ERR_BAD_STATEID;
2723 #if defined(CONFIG_NFS_V4_1)
2724 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2725 nfs4_stateid *stateid,
2726 const struct cred *cred)
2730 switch (stateid->type) {
2733 case NFS4_INVALID_STATEID_TYPE:
2734 case NFS4_SPECIAL_STATEID_TYPE:
2735 return -NFS4ERR_BAD_STATEID;
2736 case NFS4_REVOKED_STATEID_TYPE:
2740 status = nfs41_test_stateid(server, stateid, cred);
2742 case -NFS4ERR_EXPIRED:
2743 case -NFS4ERR_ADMIN_REVOKED:
2744 case -NFS4ERR_DELEG_REVOKED:
2750 /* Ack the revoked state to the server */
2751 nfs41_free_stateid(server, stateid, cred, true);
2752 return -NFS4ERR_EXPIRED;
2755 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2757 struct nfs_server *server = NFS_SERVER(state->inode);
2758 nfs4_stateid stateid;
2759 struct nfs_delegation *delegation;
2760 const struct cred *cred = NULL;
2761 int status, ret = NFS_OK;
2763 /* Get the delegation credential for use by test/free_stateid */
2765 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2766 if (delegation == NULL) {
2768 nfs_state_clear_delegation(state);
2772 nfs4_stateid_copy(&stateid, &delegation->stateid);
2774 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2775 &delegation->flags)) {
2780 if (delegation->cred)
2781 cred = get_cred(delegation->cred);
2783 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2784 trace_nfs4_test_delegation_stateid(state, NULL, status);
2785 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2786 nfs_finish_clear_delegation_stateid(state, &stateid);
2794 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2798 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2799 nfs4_copy_delegation_stateid(state->inode, state->state,
2801 nfs4_stateid_match_other(&state->stateid, &tmp))
2802 nfs_state_set_delegation(state, &tmp, state->state);
2804 nfs_state_clear_delegation(state);
2808 * nfs41_check_expired_locks - possibly free a lock stateid
2810 * @state: NFSv4 state for an inode
2812 * Returns NFS_OK if recovery for this stateid is now finished.
2813 * Otherwise a negative NFS4ERR value is returned.
2815 static int nfs41_check_expired_locks(struct nfs4_state *state)
2817 int status, ret = NFS_OK;
2818 struct nfs4_lock_state *lsp, *prev = NULL;
2819 struct nfs_server *server = NFS_SERVER(state->inode);
2821 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2824 spin_lock(&state->state_lock);
2825 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2826 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2827 const struct cred *cred = lsp->ls_state->owner->so_cred;
2829 refcount_inc(&lsp->ls_count);
2830 spin_unlock(&state->state_lock);
2832 nfs4_put_lock_state(prev);
2835 status = nfs41_test_and_free_expired_stateid(server,
2838 trace_nfs4_test_lock_stateid(state, lsp, status);
2839 if (status == -NFS4ERR_EXPIRED ||
2840 status == -NFS4ERR_BAD_STATEID) {
2841 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2842 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2843 if (!recover_lost_locks)
2844 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2845 } else if (status != NFS_OK) {
2847 nfs4_put_lock_state(prev);
2850 spin_lock(&state->state_lock);
2853 spin_unlock(&state->state_lock);
2854 nfs4_put_lock_state(prev);
2860 * nfs41_check_open_stateid - possibly free an open stateid
2862 * @state: NFSv4 state for an inode
2864 * Returns NFS_OK if recovery for this stateid is now finished.
2865 * Otherwise a negative NFS4ERR value is returned.
2867 static int nfs41_check_open_stateid(struct nfs4_state *state)
2869 struct nfs_server *server = NFS_SERVER(state->inode);
2870 nfs4_stateid *stateid = &state->open_stateid;
2871 const struct cred *cred = state->owner->so_cred;
2874 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2875 return -NFS4ERR_BAD_STATEID;
2876 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2877 trace_nfs4_test_open_stateid(state, NULL, status);
2878 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2879 nfs_state_clear_open_state_flags(state);
2880 stateid->type = NFS4_INVALID_STATEID_TYPE;
2883 if (nfs_open_stateid_recover_openmode(state))
2884 return -NFS4ERR_OPENMODE;
2888 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2892 status = nfs41_check_delegation_stateid(state);
2893 if (status != NFS_OK)
2895 nfs41_delegation_recover_stateid(state);
2897 status = nfs41_check_expired_locks(state);
2898 if (status != NFS_OK)
2900 status = nfs41_check_open_stateid(state);
2901 if (status != NFS_OK)
2902 status = nfs4_open_expired(sp, state);
2908 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2909 * fields corresponding to attributes that were used to store the verifier.
2910 * Make sure we clobber those fields in the later setattr call
2912 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2913 struct iattr *sattr, struct nfs4_label **label)
2915 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2920 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2921 attrset[i] = opendata->o_res.attrset[i];
2922 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2923 attrset[i] &= ~bitmask[i];
2926 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2927 sattr->ia_valid : 0;
2929 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2930 if (sattr->ia_valid & ATTR_ATIME_SET)
2931 ret |= ATTR_ATIME_SET;
2936 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2937 if (sattr->ia_valid & ATTR_MTIME_SET)
2938 ret |= ATTR_MTIME_SET;
2943 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2948 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2949 int flags, struct nfs_open_context *ctx)
2951 struct nfs4_state_owner *sp = opendata->owner;
2952 struct nfs_server *server = sp->so_server;
2953 struct dentry *dentry;
2954 struct nfs4_state *state;
2955 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2959 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2961 ret = _nfs4_proc_open(opendata, ctx);
2965 state = _nfs4_opendata_to_nfs4_state(opendata);
2966 ret = PTR_ERR(state);
2970 if (server->caps & NFS_CAP_POSIX_LOCK)
2971 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2972 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2973 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2975 dentry = opendata->dentry;
2976 if (d_really_is_negative(dentry)) {
2977 struct dentry *alias;
2979 alias = d_exact_alias(dentry, state->inode);
2981 alias = d_splice_alias(igrab(state->inode), dentry);
2982 /* d_splice_alias() can't fail here - it's a non-directory */
2985 ctx->dentry = dentry = alias;
2987 nfs_set_verifier(dentry,
2988 nfs_save_change_attribute(d_inode(opendata->dir)));
2991 /* Parse layoutget results before we check for access */
2992 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2994 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
2999 if (d_inode(dentry) == state->inode) {
3000 nfs_inode_attach_open_context(ctx);
3001 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3002 nfs4_schedule_stateid_recovery(server, state);
3006 if (!opendata->cancelled)
3007 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3012 * Returns a referenced nfs4_state
3014 static int _nfs4_do_open(struct inode *dir,
3015 struct nfs_open_context *ctx,
3017 const struct nfs4_open_createattrs *c,
3020 struct nfs4_state_owner *sp;
3021 struct nfs4_state *state = NULL;
3022 struct nfs_server *server = NFS_SERVER(dir);
3023 struct nfs4_opendata *opendata;
3024 struct dentry *dentry = ctx->dentry;
3025 const struct cred *cred = ctx->cred;
3026 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3027 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3028 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3029 struct iattr *sattr = c->sattr;
3030 struct nfs4_label *label = c->label;
3031 struct nfs4_label *olabel = NULL;
3034 /* Protect against reboot recovery conflicts */
3036 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3038 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3041 status = nfs4_client_recover_expired_lease(server->nfs_client);
3043 goto err_put_state_owner;
3044 if (d_really_is_positive(dentry))
3045 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3047 if (d_really_is_positive(dentry))
3048 claim = NFS4_OPEN_CLAIM_FH;
3049 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3050 c, claim, GFP_KERNEL);
3051 if (opendata == NULL)
3052 goto err_put_state_owner;
3055 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3056 if (IS_ERR(olabel)) {
3057 status = PTR_ERR(olabel);
3058 goto err_opendata_put;
3062 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3063 if (!opendata->f_attr.mdsthreshold) {
3064 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3065 if (!opendata->f_attr.mdsthreshold)
3066 goto err_free_label;
3068 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3070 if (d_really_is_positive(dentry))
3071 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3073 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3075 goto err_free_label;
3078 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3079 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3080 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3082 * send create attributes which was not set by open
3083 * with an extra setattr.
3085 if (attrs || label) {
3086 unsigned ia_old = sattr->ia_valid;
3088 sattr->ia_valid = attrs;
3089 nfs_fattr_init(opendata->o_res.f_attr);
3090 status = nfs4_do_setattr(state->inode, cred,
3091 opendata->o_res.f_attr, sattr,
3092 ctx, label, olabel);
3094 nfs_setattr_update_inode(state->inode, sattr,
3095 opendata->o_res.f_attr);
3096 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3098 sattr->ia_valid = ia_old;
3101 if (opened && opendata->file_created)
3104 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3105 *ctx_th = opendata->f_attr.mdsthreshold;
3106 opendata->f_attr.mdsthreshold = NULL;
3109 nfs4_label_free(olabel);
3111 nfs4_opendata_put(opendata);
3112 nfs4_put_state_owner(sp);
3115 nfs4_label_free(olabel);
3117 nfs4_opendata_put(opendata);
3118 err_put_state_owner:
3119 nfs4_put_state_owner(sp);
3125 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3126 struct nfs_open_context *ctx,
3128 struct iattr *sattr,
3129 struct nfs4_label *label,
3132 struct nfs_server *server = NFS_SERVER(dir);
3133 struct nfs4_exception exception = {
3134 .interruptible = true,
3136 struct nfs4_state *res;
3137 struct nfs4_open_createattrs c = {
3141 [0] = (__u32)jiffies,
3142 [1] = (__u32)current->pid,
3148 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3150 trace_nfs4_open_file(ctx, flags, status);
3153 /* NOTE: BAD_SEQID means the server and client disagree about the
3154 * book-keeping w.r.t. state-changing operations
3155 * (OPEN/CLOSE/LOCK/LOCKU...)
3156 * It is actually a sign of a bug on the client or on the server.
3158 * If we receive a BAD_SEQID error in the particular case of
3159 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3160 * have unhashed the old state_owner for us, and that we can
3161 * therefore safely retry using a new one. We should still warn
3162 * the user though...
3164 if (status == -NFS4ERR_BAD_SEQID) {
3165 pr_warn_ratelimited("NFS: v4 server %s "
3166 " returned a bad sequence-id error!\n",
3167 NFS_SERVER(dir)->nfs_client->cl_hostname);
3168 exception.retry = 1;
3172 * BAD_STATEID on OPEN means that the server cancelled our
3173 * state before it received the OPEN_CONFIRM.
3174 * Recover by retrying the request as per the discussion
3175 * on Page 181 of RFC3530.
3177 if (status == -NFS4ERR_BAD_STATEID) {
3178 exception.retry = 1;
3181 if (status == -EAGAIN) {
3182 /* We must have found a delegation */
3183 exception.retry = 1;
3186 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3188 res = ERR_PTR(nfs4_handle_exception(server,
3189 status, &exception));
3190 } while (exception.retry);
3194 static int _nfs4_do_setattr(struct inode *inode,
3195 struct nfs_setattrargs *arg,
3196 struct nfs_setattrres *res,
3197 const struct cred *cred,
3198 struct nfs_open_context *ctx)
3200 struct nfs_server *server = NFS_SERVER(inode);
3201 struct rpc_message msg = {
3202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3207 const struct cred *delegation_cred = NULL;
3208 unsigned long timestamp = jiffies;
3212 nfs_fattr_init(res->fattr);
3214 /* Servers should only apply open mode checks for file size changes */
3215 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3219 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3220 /* Use that stateid */
3221 } else if (ctx != NULL && ctx->state) {
3222 struct nfs_lock_context *l_ctx;
3223 if (!nfs4_valid_open_stateid(ctx->state))
3225 l_ctx = nfs_get_lock_context(ctx);
3227 return PTR_ERR(l_ctx);
3228 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3229 &arg->stateid, &delegation_cred);
3230 nfs_put_lock_context(l_ctx);
3235 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3237 if (delegation_cred)
3238 msg.rpc_cred = delegation_cred;
3240 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3242 put_cred(delegation_cred);
3243 if (status == 0 && ctx != NULL)
3244 renew_lease(server, timestamp);
3245 trace_nfs4_setattr(inode, &arg->stateid, status);
3249 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3250 struct nfs_fattr *fattr, struct iattr *sattr,
3251 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3252 struct nfs4_label *olabel)
3254 struct nfs_server *server = NFS_SERVER(inode);
3255 __u32 bitmask[NFS4_BITMASK_SZ];
3256 struct nfs4_state *state = ctx ? ctx->state : NULL;
3257 struct nfs_setattrargs arg = {
3258 .fh = NFS_FH(inode),
3264 struct nfs_setattrres res = {
3269 struct nfs4_exception exception = {
3272 .stateid = &arg.stateid,
3277 nfs4_bitmap_copy_adjust_setattr(bitmask,
3278 nfs4_bitmask(server, olabel),
3281 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3283 case -NFS4ERR_OPENMODE:
3284 if (!(sattr->ia_valid & ATTR_SIZE)) {
3285 pr_warn_once("NFSv4: server %s is incorrectly "
3286 "applying open mode checks to "
3287 "a SETATTR that is not "
3288 "changing file size.\n",
3289 server->nfs_client->cl_hostname);
3291 if (state && !(state->state & FMODE_WRITE)) {
3293 if (sattr->ia_valid & ATTR_OPEN)
3298 err = nfs4_handle_exception(server, err, &exception);
3299 } while (exception.retry);
3305 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3307 if (inode == NULL || !nfs_have_layout(inode))
3310 return pnfs_wait_on_layoutreturn(inode, task);
3314 * Update the seqid of an open stateid
3316 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3317 struct nfs4_state *state)
3324 if (!nfs4_valid_open_stateid(state))
3326 seq = read_seqbegin(&state->seqlock);
3327 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3328 nfs4_stateid_copy(dst, &state->open_stateid);
3329 if (read_seqretry(&state->seqlock, seq))
3333 seqid_open = state->open_stateid.seqid;
3334 if (read_seqretry(&state->seqlock, seq))
3337 dst_seqid = be32_to_cpu(dst->seqid);
3338 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3339 dst->seqid = seqid_open;
3345 * Update the seqid of an open stateid after receiving
3346 * NFS4ERR_OLD_STATEID
3348 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3349 struct nfs4_state *state)
3358 if (!nfs4_valid_open_stateid(state))
3360 seq = read_seqbegin(&state->seqlock);
3361 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3362 if (read_seqretry(&state->seqlock, seq))
3366 seqid_open = state->open_stateid.seqid;
3367 if (read_seqretry(&state->seqlock, seq))
3370 dst_seqid = be32_to_cpu(dst->seqid);
3371 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3372 dst->seqid = cpu_to_be32(dst_seqid + 1);
3374 dst->seqid = seqid_open;
3382 struct nfs4_closedata {
3383 struct inode *inode;
3384 struct nfs4_state *state;
3385 struct nfs_closeargs arg;
3386 struct nfs_closeres res;
3388 struct nfs4_layoutreturn_args arg;
3389 struct nfs4_layoutreturn_res res;
3390 struct nfs4_xdr_opaque_data ld_private;
3394 struct nfs_fattr fattr;
3395 unsigned long timestamp;
3398 static void nfs4_free_closedata(void *data)
3400 struct nfs4_closedata *calldata = data;
3401 struct nfs4_state_owner *sp = calldata->state->owner;
3402 struct super_block *sb = calldata->state->inode->i_sb;
3404 if (calldata->lr.roc)
3405 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3406 calldata->res.lr_ret);
3407 nfs4_put_open_state(calldata->state);
3408 nfs_free_seqid(calldata->arg.seqid);
3409 nfs4_put_state_owner(sp);
3410 nfs_sb_deactive(sb);
3414 static void nfs4_close_done(struct rpc_task *task, void *data)
3416 struct nfs4_closedata *calldata = data;
3417 struct nfs4_state *state = calldata->state;
3418 struct nfs_server *server = NFS_SERVER(calldata->inode);
3419 nfs4_stateid *res_stateid = NULL;
3420 struct nfs4_exception exception = {
3422 .inode = calldata->inode,
3423 .stateid = &calldata->arg.stateid,
3426 dprintk("%s: begin!\n", __func__);
3427 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3429 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3431 /* Handle Layoutreturn errors */
3432 if (pnfs_roc_done(task, calldata->inode,
3433 &calldata->arg.lr_args,
3434 &calldata->res.lr_res,
3435 &calldata->res.lr_ret) == -EAGAIN)
3438 /* hmm. we are done with the inode, and in the process of freeing
3439 * the state_owner. we keep this around to process errors
3441 switch (task->tk_status) {
3443 res_stateid = &calldata->res.stateid;
3444 renew_lease(server, calldata->timestamp);
3446 case -NFS4ERR_ACCESS:
3447 if (calldata->arg.bitmask != NULL) {
3448 calldata->arg.bitmask = NULL;
3449 calldata->res.fattr = NULL;
3454 case -NFS4ERR_OLD_STATEID:
3455 /* Did we race with OPEN? */
3456 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3460 case -NFS4ERR_ADMIN_REVOKED:
3461 case -NFS4ERR_STALE_STATEID:
3462 case -NFS4ERR_EXPIRED:
3463 nfs4_free_revoked_stateid(server,
3464 &calldata->arg.stateid,
3465 task->tk_msg.rpc_cred);
3467 case -NFS4ERR_BAD_STATEID:
3468 if (calldata->arg.fmode == 0)
3472 task->tk_status = nfs4_async_handle_exception(task,
3473 server, task->tk_status, &exception);
3474 if (exception.retry)
3477 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3478 res_stateid, calldata->arg.fmode);
3480 task->tk_status = 0;
3481 nfs_release_seqid(calldata->arg.seqid);
3482 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3483 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3486 task->tk_status = 0;
3487 rpc_restart_call_prepare(task);
3491 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3493 struct nfs4_closedata *calldata = data;
3494 struct nfs4_state *state = calldata->state;
3495 struct inode *inode = calldata->inode;
3496 struct pnfs_layout_hdr *lo;
3497 bool is_rdonly, is_wronly, is_rdwr;
3500 dprintk("%s: begin!\n", __func__);
3501 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3504 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3505 spin_lock(&state->owner->so_lock);
3506 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3507 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3508 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3509 /* Calculate the change in open mode */
3510 calldata->arg.fmode = 0;
3511 if (state->n_rdwr == 0) {
3512 if (state->n_rdonly == 0)
3513 call_close |= is_rdonly;
3515 calldata->arg.fmode |= FMODE_READ;
3516 if (state->n_wronly == 0)
3517 call_close |= is_wronly;
3519 calldata->arg.fmode |= FMODE_WRITE;
3520 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3521 call_close |= is_rdwr;
3523 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3525 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3526 if (!nfs4_valid_open_stateid(state))
3528 spin_unlock(&state->owner->so_lock);
3531 /* Note: exit _without_ calling nfs4_close_done */
3535 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3536 nfs_release_seqid(calldata->arg.seqid);
3540 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3541 if (lo && !pnfs_layout_is_valid(lo)) {
3542 calldata->arg.lr_args = NULL;
3543 calldata->res.lr_res = NULL;
3546 if (calldata->arg.fmode == 0)
3547 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3549 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3550 /* Close-to-open cache consistency revalidation */
3551 if (!nfs4_have_delegation(inode, FMODE_READ))
3552 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3554 calldata->arg.bitmask = NULL;
3557 calldata->arg.share_access =
3558 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3559 calldata->arg.fmode, 0);
3561 if (calldata->res.fattr == NULL)
3562 calldata->arg.bitmask = NULL;
3563 else if (calldata->arg.bitmask == NULL)
3564 calldata->res.fattr = NULL;
3565 calldata->timestamp = jiffies;
3566 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3567 &calldata->arg.seq_args,
3568 &calldata->res.seq_res,
3570 nfs_release_seqid(calldata->arg.seqid);
3571 dprintk("%s: done!\n", __func__);
3574 task->tk_action = NULL;
3576 nfs4_sequence_done(task, &calldata->res.seq_res);
3579 static const struct rpc_call_ops nfs4_close_ops = {
3580 .rpc_call_prepare = nfs4_close_prepare,
3581 .rpc_call_done = nfs4_close_done,
3582 .rpc_release = nfs4_free_closedata,
3586 * It is possible for data to be read/written from a mem-mapped file
3587 * after the sys_close call (which hits the vfs layer as a flush).
3588 * This means that we can't safely call nfsv4 close on a file until
3589 * the inode is cleared. This in turn means that we are not good
3590 * NFSv4 citizens - we do not indicate to the server to update the file's
3591 * share state even when we are done with one of the three share
3592 * stateid's in the inode.
3594 * NOTE: Caller must be holding the sp->so_owner semaphore!
3596 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3598 struct nfs_server *server = NFS_SERVER(state->inode);
3599 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3600 struct nfs4_closedata *calldata;
3601 struct nfs4_state_owner *sp = state->owner;
3602 struct rpc_task *task;
3603 struct rpc_message msg = {
3604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3605 .rpc_cred = state->owner->so_cred,
3607 struct rpc_task_setup task_setup_data = {
3608 .rpc_client = server->client,
3609 .rpc_message = &msg,
3610 .callback_ops = &nfs4_close_ops,
3611 .workqueue = nfsiod_workqueue,
3612 .flags = RPC_TASK_ASYNC,
3614 int status = -ENOMEM;
3616 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3617 &task_setup_data.rpc_client, &msg);
3619 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3620 if (calldata == NULL)
3622 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3623 calldata->inode = state->inode;
3624 calldata->state = state;
3625 calldata->arg.fh = NFS_FH(state->inode);
3626 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3627 goto out_free_calldata;
3628 /* Serialization for the sequence id */
3629 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3630 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3631 if (IS_ERR(calldata->arg.seqid))
3632 goto out_free_calldata;
3633 nfs_fattr_init(&calldata->fattr);
3634 calldata->arg.fmode = 0;
3635 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3636 calldata->res.fattr = &calldata->fattr;
3637 calldata->res.seqid = calldata->arg.seqid;
3638 calldata->res.server = server;
3639 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3640 calldata->lr.roc = pnfs_roc(state->inode,
3641 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3642 if (calldata->lr.roc) {
3643 calldata->arg.lr_args = &calldata->lr.arg;
3644 calldata->res.lr_res = &calldata->lr.res;
3646 nfs_sb_active(calldata->inode->i_sb);
3648 msg.rpc_argp = &calldata->arg;
3649 msg.rpc_resp = &calldata->res;
3650 task_setup_data.callback_data = calldata;
3651 task = rpc_run_task(&task_setup_data);
3653 return PTR_ERR(task);
3656 status = rpc_wait_for_completion_task(task);
3662 nfs4_put_open_state(state);
3663 nfs4_put_state_owner(sp);
3667 static struct inode *
3668 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3669 int open_flags, struct iattr *attr, int *opened)
3671 struct nfs4_state *state;
3672 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3674 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3676 /* Protect against concurrent sillydeletes */
3677 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3679 nfs4_label_release_security(label);
3682 return ERR_CAST(state);
3683 return state->inode;
3686 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3688 if (ctx->state == NULL)
3691 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3693 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3696 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3697 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3698 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3700 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3702 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3703 struct nfs4_server_caps_arg args = {
3707 struct nfs4_server_caps_res res = {};
3708 struct rpc_message msg = {
3709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3716 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3717 FATTR4_WORD0_FH_EXPIRE_TYPE |
3718 FATTR4_WORD0_LINK_SUPPORT |
3719 FATTR4_WORD0_SYMLINK_SUPPORT |
3720 FATTR4_WORD0_ACLSUPPORT;
3722 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3724 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3726 /* Sanity check the server answers */
3727 switch (minorversion) {
3729 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3730 res.attr_bitmask[2] = 0;
3733 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3736 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3738 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3739 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3740 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3741 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3742 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3743 NFS_CAP_CTIME|NFS_CAP_MTIME|
3744 NFS_CAP_SECURITY_LABEL);
3745 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3746 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3747 server->caps |= NFS_CAP_ACLS;
3748 if (res.has_links != 0)
3749 server->caps |= NFS_CAP_HARDLINKS;
3750 if (res.has_symlinks != 0)
3751 server->caps |= NFS_CAP_SYMLINKS;
3752 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3753 server->caps |= NFS_CAP_FILEID;
3754 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3755 server->caps |= NFS_CAP_MODE;
3756 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3757 server->caps |= NFS_CAP_NLINK;
3758 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3759 server->caps |= NFS_CAP_OWNER;
3760 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3761 server->caps |= NFS_CAP_OWNER_GROUP;
3762 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3763 server->caps |= NFS_CAP_ATIME;
3764 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3765 server->caps |= NFS_CAP_CTIME;
3766 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3767 server->caps |= NFS_CAP_MTIME;
3768 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3769 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3770 server->caps |= NFS_CAP_SECURITY_LABEL;
3772 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3773 sizeof(server->attr_bitmask));
3774 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3776 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3777 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3778 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3779 server->cache_consistency_bitmask[2] = 0;
3781 /* Avoid a regression due to buggy server */
3782 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3783 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3784 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3785 sizeof(server->exclcreat_bitmask));
3787 server->acl_bitmask = res.acl_bitmask;
3788 server->fh_expire_type = res.fh_expire_type;
3794 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3796 struct nfs4_exception exception = {
3797 .interruptible = true,
3801 err = nfs4_handle_exception(server,
3802 _nfs4_server_capabilities(server, fhandle),
3804 } while (exception.retry);
3808 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3809 struct nfs_fsinfo *info)
3812 struct nfs4_lookup_root_arg args = {
3815 struct nfs4_lookup_res res = {
3817 .fattr = info->fattr,
3820 struct rpc_message msg = {
3821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3826 bitmask[0] = nfs4_fattr_bitmap[0];
3827 bitmask[1] = nfs4_fattr_bitmap[1];
3829 * Process the label in the upcoming getfattr
3831 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3833 nfs_fattr_init(info->fattr);
3834 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3837 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3838 struct nfs_fsinfo *info)
3840 struct nfs4_exception exception = {
3841 .interruptible = true,
3845 err = _nfs4_lookup_root(server, fhandle, info);
3846 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3849 case -NFS4ERR_WRONGSEC:
3852 err = nfs4_handle_exception(server, err, &exception);
3854 } while (exception.retry);
3859 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3860 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3862 struct rpc_auth_create_args auth_args = {
3863 .pseudoflavor = flavor,
3865 struct rpc_auth *auth;
3867 auth = rpcauth_create(&auth_args, server->client);
3870 return nfs4_lookup_root(server, fhandle, info);
3874 * Retry pseudoroot lookup with various security flavors. We do this when:
3876 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3877 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3879 * Returns zero on success, or a negative NFS4ERR value, or a
3880 * negative errno value.
3882 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3883 struct nfs_fsinfo *info)
3885 /* Per 3530bis 15.33.5 */
3886 static const rpc_authflavor_t flav_array[] = {
3890 RPC_AUTH_UNIX, /* courtesy */
3893 int status = -EPERM;
3896 if (server->auth_info.flavor_len > 0) {
3897 /* try each flavor specified by user */
3898 for (i = 0; i < server->auth_info.flavor_len; i++) {
3899 status = nfs4_lookup_root_sec(server, fhandle, info,
3900 server->auth_info.flavors[i]);
3901 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3906 /* no flavors specified by user, try default list */
3907 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3908 status = nfs4_lookup_root_sec(server, fhandle, info,
3910 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3917 * -EACCES could mean that the user doesn't have correct permissions
3918 * to access the mount. It could also mean that we tried to mount
3919 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3920 * existing mount programs don't handle -EACCES very well so it should
3921 * be mapped to -EPERM instead.
3923 if (status == -EACCES)
3929 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3930 * @server: initialized nfs_server handle
3931 * @fhandle: we fill in the pseudo-fs root file handle
3932 * @info: we fill in an FSINFO struct
3933 * @auth_probe: probe the auth flavours
3935 * Returns zero on success, or a negative errno.
3937 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3938 struct nfs_fsinfo *info,
3944 status = nfs4_lookup_root(server, fhandle, info);
3946 if (auth_probe || status == NFS4ERR_WRONGSEC)
3947 status = server->nfs_client->cl_mvops->find_root_sec(server,
3951 status = nfs4_server_capabilities(server, fhandle);
3953 status = nfs4_do_fsinfo(server, fhandle, info);
3955 return nfs4_map_errors(status);
3958 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3959 struct nfs_fsinfo *info)
3962 struct nfs_fattr *fattr = info->fattr;
3963 struct nfs4_label *label = NULL;
3965 error = nfs4_server_capabilities(server, mntfh);
3967 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3971 label = nfs4_label_alloc(server, GFP_KERNEL);
3973 return PTR_ERR(label);
3975 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3977 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3978 goto err_free_label;
3981 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3982 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3983 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3986 nfs4_label_free(label);
3992 * Get locations and (maybe) other attributes of a referral.
3993 * Note that we'll actually follow the referral later when
3994 * we detect fsid mismatch in inode revalidation
3996 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3997 const struct qstr *name, struct nfs_fattr *fattr,
3998 struct nfs_fh *fhandle)
4000 int status = -ENOMEM;
4001 struct page *page = NULL;
4002 struct nfs4_fs_locations *locations = NULL;
4004 page = alloc_page(GFP_KERNEL);
4007 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4008 if (locations == NULL)
4011 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4016 * If the fsid didn't change, this is a migration event, not a
4017 * referral. Cause us to drop into the exception handler, which
4018 * will kick off migration recovery.
4020 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4021 dprintk("%s: server did not return a different fsid for"
4022 " a referral at %s\n", __func__, name->name);
4023 status = -NFS4ERR_MOVED;
4026 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4027 nfs_fixup_referral_attributes(&locations->fattr);
4029 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4030 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4031 memset(fhandle, 0, sizeof(struct nfs_fh));
4039 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4040 struct nfs_fattr *fattr, struct nfs4_label *label,
4041 struct inode *inode)
4043 __u32 bitmask[NFS4_BITMASK_SZ];
4044 struct nfs4_getattr_arg args = {
4048 struct nfs4_getattr_res res = {
4053 struct rpc_message msg = {
4054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4059 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4061 nfs_fattr_init(fattr);
4062 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4065 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4066 struct nfs_fattr *fattr, struct nfs4_label *label,
4067 struct inode *inode)
4069 struct nfs4_exception exception = {
4070 .interruptible = true,
4074 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4075 trace_nfs4_getattr(server, fhandle, fattr, err);
4076 err = nfs4_handle_exception(server, err,
4078 } while (exception.retry);
4083 * The file is not closed if it is opened due to the a request to change
4084 * the size of the file. The open call will not be needed once the
4085 * VFS layer lookup-intents are implemented.
4087 * Close is called when the inode is destroyed.
4088 * If we haven't opened the file for O_WRONLY, we
4089 * need to in the size_change case to obtain a stateid.
4092 * Because OPEN is always done by name in nfsv4, it is
4093 * possible that we opened a different file by the same
4094 * name. We can recognize this race condition, but we
4095 * can't do anything about it besides returning an error.
4097 * This will be fixed with VFS changes (lookup-intent).
4100 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4101 struct iattr *sattr)
4103 struct inode *inode = d_inode(dentry);
4104 const struct cred *cred = NULL;
4105 struct nfs_open_context *ctx = NULL;
4106 struct nfs4_label *label = NULL;
4109 if (pnfs_ld_layoutret_on_setattr(inode) &&
4110 sattr->ia_valid & ATTR_SIZE &&
4111 sattr->ia_size < i_size_read(inode))
4112 pnfs_commit_and_return_layout(inode);
4114 nfs_fattr_init(fattr);
4116 /* Deal with open(O_TRUNC) */
4117 if (sattr->ia_valid & ATTR_OPEN)
4118 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4120 /* Optimization: if the end result is no change, don't RPC */
4121 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4124 /* Search for an existing open(O_WRITE) file */
4125 if (sattr->ia_valid & ATTR_FILE) {
4127 ctx = nfs_file_open_context(sattr->ia_file);
4132 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4134 return PTR_ERR(label);
4136 /* Return any delegations if we're going to change ACLs */
4137 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4138 nfs4_inode_make_writeable(inode);
4140 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4142 nfs_setattr_update_inode(inode, sattr, fattr);
4143 nfs_setsecurity(inode, fattr, label);
4145 nfs4_label_free(label);
4149 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4150 const struct qstr *name, struct nfs_fh *fhandle,
4151 struct nfs_fattr *fattr, struct nfs4_label *label)
4153 struct nfs_server *server = NFS_SERVER(dir);
4155 struct nfs4_lookup_arg args = {
4156 .bitmask = server->attr_bitmask,
4157 .dir_fh = NFS_FH(dir),
4160 struct nfs4_lookup_res res = {
4166 struct rpc_message msg = {
4167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4172 args.bitmask = nfs4_bitmask(server, label);
4174 nfs_fattr_init(fattr);
4176 dprintk("NFS call lookup %s\n", name->name);
4177 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4178 dprintk("NFS reply lookup: %d\n", status);
4182 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4184 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4185 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4186 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4190 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4191 const struct qstr *name, struct nfs_fh *fhandle,
4192 struct nfs_fattr *fattr, struct nfs4_label *label)
4194 struct nfs4_exception exception = {
4195 .interruptible = true,
4197 struct rpc_clnt *client = *clnt;
4200 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4201 trace_nfs4_lookup(dir, name, err);
4203 case -NFS4ERR_BADNAME:
4206 case -NFS4ERR_MOVED:
4207 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4208 if (err == -NFS4ERR_MOVED)
4209 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4211 case -NFS4ERR_WRONGSEC:
4213 if (client != *clnt)
4215 client = nfs4_negotiate_security(client, dir, name);
4217 return PTR_ERR(client);
4219 exception.retry = 1;
4222 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4224 } while (exception.retry);
4229 else if (client != *clnt)
4230 rpc_shutdown_client(client);
4235 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4236 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4237 struct nfs4_label *label)
4240 struct rpc_clnt *client = NFS_CLIENT(dir);
4242 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4243 if (client != NFS_CLIENT(dir)) {
4244 rpc_shutdown_client(client);
4245 nfs_fixup_secinfo_attributes(fattr);
4251 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4252 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4254 struct rpc_clnt *client = NFS_CLIENT(dir);
4257 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4259 return ERR_PTR(status);
4260 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4263 static int _nfs4_proc_lookupp(struct inode *inode,
4264 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4265 struct nfs4_label *label)
4267 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4268 struct nfs_server *server = NFS_SERVER(inode);
4270 struct nfs4_lookupp_arg args = {
4271 .bitmask = server->attr_bitmask,
4272 .fh = NFS_FH(inode),
4274 struct nfs4_lookupp_res res = {
4280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4286 args.bitmask = nfs4_bitmask(server, label);
4288 nfs_fattr_init(fattr);
4290 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4291 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4293 dprintk("NFS reply lookupp: %d\n", status);
4297 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4298 struct nfs_fattr *fattr, struct nfs4_label *label)
4300 struct nfs4_exception exception = {
4301 .interruptible = true,
4305 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4306 trace_nfs4_lookupp(inode, err);
4307 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4309 } while (exception.retry);
4313 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4315 struct nfs_server *server = NFS_SERVER(inode);
4316 struct nfs4_accessargs args = {
4317 .fh = NFS_FH(inode),
4318 .access = entry->mask,
4320 struct nfs4_accessres res = {
4323 struct rpc_message msg = {
4324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4327 .rpc_cred = entry->cred,
4331 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4332 res.fattr = nfs_alloc_fattr();
4333 if (res.fattr == NULL)
4335 args.bitmask = server->cache_consistency_bitmask;
4337 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4339 nfs_access_set_mask(entry, res.access);
4341 nfs_refresh_inode(inode, res.fattr);
4343 nfs_free_fattr(res.fattr);
4347 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4349 struct nfs4_exception exception = {
4350 .interruptible = true,
4354 err = _nfs4_proc_access(inode, entry);
4355 trace_nfs4_access(inode, err);
4356 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4358 } while (exception.retry);
4363 * TODO: For the time being, we don't try to get any attributes
4364 * along with any of the zero-copy operations READ, READDIR,
4367 * In the case of the first three, we want to put the GETATTR
4368 * after the read-type operation -- this is because it is hard
4369 * to predict the length of a GETATTR response in v4, and thus
4370 * align the READ data correctly. This means that the GETATTR
4371 * may end up partially falling into the page cache, and we should
4372 * shift it into the 'tail' of the xdr_buf before processing.
4373 * To do this efficiently, we need to know the total length
4374 * of data received, which doesn't seem to be available outside
4377 * In the case of WRITE, we also want to put the GETATTR after
4378 * the operation -- in this case because we want to make sure
4379 * we get the post-operation mtime and size.
4381 * Both of these changes to the XDR layer would in fact be quite
4382 * minor, but I decided to leave them for a subsequent patch.
4384 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4385 unsigned int pgbase, unsigned int pglen)
4387 struct nfs4_readlink args = {
4388 .fh = NFS_FH(inode),
4393 struct nfs4_readlink_res res;
4394 struct rpc_message msg = {
4395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4400 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4403 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4404 unsigned int pgbase, unsigned int pglen)
4406 struct nfs4_exception exception = {
4407 .interruptible = true,
4411 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4412 trace_nfs4_readlink(inode, err);
4413 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4415 } while (exception.retry);
4420 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4423 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4426 struct nfs_server *server = NFS_SERVER(dir);
4427 struct nfs4_label l, *ilabel = NULL;
4428 struct nfs_open_context *ctx;
4429 struct nfs4_state *state;
4432 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4434 return PTR_ERR(ctx);
4436 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4438 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4439 sattr->ia_mode &= ~current_umask();
4440 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4441 if (IS_ERR(state)) {
4442 status = PTR_ERR(state);
4446 nfs4_label_release_security(ilabel);
4447 put_nfs_open_context(ctx);
4452 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4454 struct nfs_server *server = NFS_SERVER(dir);
4455 struct nfs_removeargs args = {
4459 struct nfs_removeres res = {
4462 struct rpc_message msg = {
4463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4467 unsigned long timestamp = jiffies;
4470 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4472 spin_lock(&dir->i_lock);
4473 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4474 /* Removing a directory decrements nlink in the parent */
4475 if (ftype == NF4DIR && dir->i_nlink > 2)
4476 nfs4_dec_nlink_locked(dir);
4477 spin_unlock(&dir->i_lock);
4482 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4484 struct nfs4_exception exception = {
4485 .interruptible = true,
4487 struct inode *inode = d_inode(dentry);
4491 if (inode->i_nlink == 1)
4492 nfs4_inode_return_delegation(inode);
4494 nfs4_inode_make_writeable(inode);
4497 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4498 trace_nfs4_remove(dir, &dentry->d_name, err);
4499 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4501 } while (exception.retry);
4505 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4507 struct nfs4_exception exception = {
4508 .interruptible = true,
4513 err = _nfs4_proc_remove(dir, name, NF4DIR);
4514 trace_nfs4_remove(dir, name, err);
4515 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4517 } while (exception.retry);
4521 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4522 struct dentry *dentry,
4523 struct inode *inode)
4525 struct nfs_removeargs *args = msg->rpc_argp;
4526 struct nfs_removeres *res = msg->rpc_resp;
4528 res->server = NFS_SB(dentry->d_sb);
4529 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4530 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4532 nfs_fattr_init(res->dir_attr);
4535 nfs4_inode_return_delegation(inode);
4538 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4540 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4541 &data->args.seq_args,
4546 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4548 struct nfs_unlinkdata *data = task->tk_calldata;
4549 struct nfs_removeres *res = &data->res;
4551 if (!nfs4_sequence_done(task, &res->seq_res))
4553 if (nfs4_async_handle_error(task, res->server, NULL,
4554 &data->timeout) == -EAGAIN)
4556 if (task->tk_status == 0)
4557 update_changeattr(dir, &res->cinfo,
4558 res->dir_attr->time_start, 0);
4562 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4563 struct dentry *old_dentry,
4564 struct dentry *new_dentry)
4566 struct nfs_renameargs *arg = msg->rpc_argp;
4567 struct nfs_renameres *res = msg->rpc_resp;
4568 struct inode *old_inode = d_inode(old_dentry);
4569 struct inode *new_inode = d_inode(new_dentry);
4572 nfs4_inode_make_writeable(old_inode);
4574 nfs4_inode_return_delegation(new_inode);
4575 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4576 res->server = NFS_SB(old_dentry->d_sb);
4577 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4580 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4582 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4583 &data->args.seq_args,
4588 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4589 struct inode *new_dir)
4591 struct nfs_renamedata *data = task->tk_calldata;
4592 struct nfs_renameres *res = &data->res;
4594 if (!nfs4_sequence_done(task, &res->seq_res))
4596 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4599 if (task->tk_status == 0) {
4600 if (new_dir != old_dir) {
4601 /* Note: If we moved a directory, nlink will change */
4602 update_changeattr(old_dir, &res->old_cinfo,
4603 res->old_fattr->time_start,
4604 NFS_INO_INVALID_OTHER);
4605 update_changeattr(new_dir, &res->new_cinfo,
4606 res->new_fattr->time_start,
4607 NFS_INO_INVALID_OTHER);
4609 update_changeattr(old_dir, &res->old_cinfo,
4610 res->old_fattr->time_start,
4616 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4618 struct nfs_server *server = NFS_SERVER(inode);
4619 __u32 bitmask[NFS4_BITMASK_SZ];
4620 struct nfs4_link_arg arg = {
4621 .fh = NFS_FH(inode),
4622 .dir_fh = NFS_FH(dir),
4626 struct nfs4_link_res res = {
4630 struct rpc_message msg = {
4631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4635 int status = -ENOMEM;
4637 res.fattr = nfs_alloc_fattr();
4638 if (res.fattr == NULL)
4641 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4642 if (IS_ERR(res.label)) {
4643 status = PTR_ERR(res.label);
4647 nfs4_inode_make_writeable(inode);
4648 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4650 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4652 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4653 status = nfs_post_op_update_inode(inode, res.fattr);
4655 nfs_setsecurity(inode, res.fattr, res.label);
4659 nfs4_label_free(res.label);
4662 nfs_free_fattr(res.fattr);
4666 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4668 struct nfs4_exception exception = {
4669 .interruptible = true,
4673 err = nfs4_handle_exception(NFS_SERVER(inode),
4674 _nfs4_proc_link(inode, dir, name),
4676 } while (exception.retry);
4680 struct nfs4_createdata {
4681 struct rpc_message msg;
4682 struct nfs4_create_arg arg;
4683 struct nfs4_create_res res;
4685 struct nfs_fattr fattr;
4686 struct nfs4_label *label;
4689 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4690 const struct qstr *name, struct iattr *sattr, u32 ftype)
4692 struct nfs4_createdata *data;
4694 data = kzalloc(sizeof(*data), GFP_KERNEL);
4696 struct nfs_server *server = NFS_SERVER(dir);
4698 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4699 if (IS_ERR(data->label))
4702 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4703 data->msg.rpc_argp = &data->arg;
4704 data->msg.rpc_resp = &data->res;
4705 data->arg.dir_fh = NFS_FH(dir);
4706 data->arg.server = server;
4707 data->arg.name = name;
4708 data->arg.attrs = sattr;
4709 data->arg.ftype = ftype;
4710 data->arg.bitmask = nfs4_bitmask(server, data->label);
4711 data->arg.umask = current_umask();
4712 data->res.server = server;
4713 data->res.fh = &data->fh;
4714 data->res.fattr = &data->fattr;
4715 data->res.label = data->label;
4716 nfs_fattr_init(data->res.fattr);
4724 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4726 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4727 &data->arg.seq_args, &data->res.seq_res, 1);
4729 spin_lock(&dir->i_lock);
4730 update_changeattr_locked(dir, &data->res.dir_cinfo,
4731 data->res.fattr->time_start, 0);
4732 /* Creating a directory bumps nlink in the parent */
4733 if (data->arg.ftype == NF4DIR)
4734 nfs4_inc_nlink_locked(dir);
4735 spin_unlock(&dir->i_lock);
4736 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4741 static void nfs4_free_createdata(struct nfs4_createdata *data)
4743 nfs4_label_free(data->label);
4747 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4748 struct page *page, unsigned int len, struct iattr *sattr,
4749 struct nfs4_label *label)
4751 struct nfs4_createdata *data;
4752 int status = -ENAMETOOLONG;
4754 if (len > NFS4_MAXPATHLEN)
4758 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4762 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4763 data->arg.u.symlink.pages = &page;
4764 data->arg.u.symlink.len = len;
4765 data->arg.label = label;
4767 status = nfs4_do_create(dir, dentry, data);
4769 nfs4_free_createdata(data);
4774 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4775 struct page *page, unsigned int len, struct iattr *sattr)
4777 struct nfs4_exception exception = {
4778 .interruptible = true,
4780 struct nfs4_label l, *label = NULL;
4783 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4786 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4787 trace_nfs4_symlink(dir, &dentry->d_name, err);
4788 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4790 } while (exception.retry);
4792 nfs4_label_release_security(label);
4796 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4797 struct iattr *sattr, struct nfs4_label *label)
4799 struct nfs4_createdata *data;
4800 int status = -ENOMEM;
4802 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4806 data->arg.label = label;
4807 status = nfs4_do_create(dir, dentry, data);
4809 nfs4_free_createdata(data);
4814 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4815 struct iattr *sattr)
4817 struct nfs_server *server = NFS_SERVER(dir);
4818 struct nfs4_exception exception = {
4819 .interruptible = true,
4821 struct nfs4_label l, *label = NULL;
4824 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4826 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4827 sattr->ia_mode &= ~current_umask();
4829 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4830 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4831 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4833 } while (exception.retry);
4834 nfs4_label_release_security(label);
4839 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4840 u64 cookie, struct page **pages, unsigned int count, bool plus)
4842 struct inode *dir = d_inode(dentry);
4843 struct nfs4_readdir_arg args = {
4848 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4851 struct nfs4_readdir_res res;
4852 struct rpc_message msg = {
4853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4860 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4862 (unsigned long long)cookie);
4863 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4864 res.pgbase = args.pgbase;
4865 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4867 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4868 status += args.pgbase;
4871 nfs_invalidate_atime(dir);
4873 dprintk("%s: returns %d\n", __func__, status);
4877 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4878 u64 cookie, struct page **pages, unsigned int count, bool plus)
4880 struct nfs4_exception exception = {
4881 .interruptible = true,
4885 err = _nfs4_proc_readdir(dentry, cred, cookie,
4886 pages, count, plus);
4887 trace_nfs4_readdir(d_inode(dentry), err);
4888 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4890 } while (exception.retry);
4894 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4895 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4897 struct nfs4_createdata *data;
4898 int mode = sattr->ia_mode;
4899 int status = -ENOMEM;
4901 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4906 data->arg.ftype = NF4FIFO;
4907 else if (S_ISBLK(mode)) {
4908 data->arg.ftype = NF4BLK;
4909 data->arg.u.device.specdata1 = MAJOR(rdev);
4910 data->arg.u.device.specdata2 = MINOR(rdev);
4912 else if (S_ISCHR(mode)) {
4913 data->arg.ftype = NF4CHR;
4914 data->arg.u.device.specdata1 = MAJOR(rdev);
4915 data->arg.u.device.specdata2 = MINOR(rdev);
4916 } else if (!S_ISSOCK(mode)) {
4921 data->arg.label = label;
4922 status = nfs4_do_create(dir, dentry, data);
4924 nfs4_free_createdata(data);
4929 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4930 struct iattr *sattr, dev_t rdev)
4932 struct nfs_server *server = NFS_SERVER(dir);
4933 struct nfs4_exception exception = {
4934 .interruptible = true,
4936 struct nfs4_label l, *label = NULL;
4939 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4941 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4942 sattr->ia_mode &= ~current_umask();
4944 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4945 trace_nfs4_mknod(dir, &dentry->d_name, err);
4946 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4948 } while (exception.retry);
4950 nfs4_label_release_security(label);
4955 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4956 struct nfs_fsstat *fsstat)
4958 struct nfs4_statfs_arg args = {
4960 .bitmask = server->attr_bitmask,
4962 struct nfs4_statfs_res res = {
4965 struct rpc_message msg = {
4966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4971 nfs_fattr_init(fsstat->fattr);
4972 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4975 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4977 struct nfs4_exception exception = {
4978 .interruptible = true,
4982 err = nfs4_handle_exception(server,
4983 _nfs4_proc_statfs(server, fhandle, fsstat),
4985 } while (exception.retry);
4989 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4990 struct nfs_fsinfo *fsinfo)
4992 struct nfs4_fsinfo_arg args = {
4994 .bitmask = server->attr_bitmask,
4996 struct nfs4_fsinfo_res res = {
4999 struct rpc_message msg = {
5000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5005 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5008 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5010 struct nfs4_exception exception = {
5011 .interruptible = true,
5013 unsigned long now = jiffies;
5017 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5018 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5020 nfs4_set_lease_period(server->nfs_client,
5021 fsinfo->lease_time * HZ,
5025 err = nfs4_handle_exception(server, err, &exception);
5026 } while (exception.retry);
5030 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5034 nfs_fattr_init(fsinfo->fattr);
5035 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5037 /* block layout checks this! */
5038 server->pnfs_blksize = fsinfo->blksize;
5039 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5045 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5046 struct nfs_pathconf *pathconf)
5048 struct nfs4_pathconf_arg args = {
5050 .bitmask = server->attr_bitmask,
5052 struct nfs4_pathconf_res res = {
5053 .pathconf = pathconf,
5055 struct rpc_message msg = {
5056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5061 /* None of the pathconf attributes are mandatory to implement */
5062 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5063 memset(pathconf, 0, sizeof(*pathconf));
5067 nfs_fattr_init(pathconf->fattr);
5068 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5071 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5072 struct nfs_pathconf *pathconf)
5074 struct nfs4_exception exception = {
5075 .interruptible = true,
5080 err = nfs4_handle_exception(server,
5081 _nfs4_proc_pathconf(server, fhandle, pathconf),
5083 } while (exception.retry);
5087 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5088 const struct nfs_open_context *ctx,
5089 const struct nfs_lock_context *l_ctx,
5092 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5094 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5096 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5097 const struct nfs_open_context *ctx,
5098 const struct nfs_lock_context *l_ctx,
5101 nfs4_stateid current_stateid;
5103 /* If the current stateid represents a lost lock, then exit */
5104 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
5106 return nfs4_stateid_match(stateid, ¤t_stateid);
5109 static bool nfs4_error_stateid_expired(int err)
5112 case -NFS4ERR_DELEG_REVOKED:
5113 case -NFS4ERR_ADMIN_REVOKED:
5114 case -NFS4ERR_BAD_STATEID:
5115 case -NFS4ERR_STALE_STATEID:
5116 case -NFS4ERR_OLD_STATEID:
5117 case -NFS4ERR_OPENMODE:
5118 case -NFS4ERR_EXPIRED:
5124 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5126 struct nfs_server *server = NFS_SERVER(hdr->inode);
5128 trace_nfs4_read(hdr, task->tk_status);
5129 if (task->tk_status < 0) {
5130 struct nfs4_exception exception = {
5131 .inode = hdr->inode,
5132 .state = hdr->args.context->state,
5133 .stateid = &hdr->args.stateid,
5135 task->tk_status = nfs4_async_handle_exception(task,
5136 server, task->tk_status, &exception);
5137 if (exception.retry) {
5138 rpc_restart_call_prepare(task);
5143 if (task->tk_status > 0)
5144 renew_lease(server, hdr->timestamp);
5148 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5149 struct nfs_pgio_args *args)
5152 if (!nfs4_error_stateid_expired(task->tk_status) ||
5153 nfs4_stateid_is_current(&args->stateid,
5158 rpc_restart_call_prepare(task);
5162 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5165 dprintk("--> %s\n", __func__);
5167 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5169 if (nfs4_read_stateid_changed(task, &hdr->args))
5171 if (task->tk_status > 0)
5172 nfs_invalidate_atime(hdr->inode);
5173 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5174 nfs4_read_done_cb(task, hdr);
5177 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5178 struct rpc_message *msg)
5180 hdr->timestamp = jiffies;
5181 if (!hdr->pgio_done_cb)
5182 hdr->pgio_done_cb = nfs4_read_done_cb;
5183 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5184 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5187 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5188 struct nfs_pgio_header *hdr)
5190 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5191 &hdr->args.seq_args,
5195 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5196 hdr->args.lock_context,
5197 hdr->rw_mode) == -EIO)
5199 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5204 static int nfs4_write_done_cb(struct rpc_task *task,
5205 struct nfs_pgio_header *hdr)
5207 struct inode *inode = hdr->inode;
5209 trace_nfs4_write(hdr, task->tk_status);
5210 if (task->tk_status < 0) {
5211 struct nfs4_exception exception = {
5212 .inode = hdr->inode,
5213 .state = hdr->args.context->state,
5214 .stateid = &hdr->args.stateid,
5216 task->tk_status = nfs4_async_handle_exception(task,
5217 NFS_SERVER(inode), task->tk_status,
5219 if (exception.retry) {
5220 rpc_restart_call_prepare(task);
5224 if (task->tk_status >= 0) {
5225 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5226 nfs_writeback_update_inode(hdr);
5231 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5232 struct nfs_pgio_args *args)
5235 if (!nfs4_error_stateid_expired(task->tk_status) ||
5236 nfs4_stateid_is_current(&args->stateid,
5241 rpc_restart_call_prepare(task);
5245 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5247 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5249 if (nfs4_write_stateid_changed(task, &hdr->args))
5251 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5252 nfs4_write_done_cb(task, hdr);
5256 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5258 /* Don't request attributes for pNFS or O_DIRECT writes */
5259 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5261 /* Otherwise, request attributes if and only if we don't hold
5264 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5267 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5268 struct rpc_message *msg,
5269 struct rpc_clnt **clnt)
5271 struct nfs_server *server = NFS_SERVER(hdr->inode);
5273 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5274 hdr->args.bitmask = NULL;
5275 hdr->res.fattr = NULL;
5277 hdr->args.bitmask = server->cache_consistency_bitmask;
5279 if (!hdr->pgio_done_cb)
5280 hdr->pgio_done_cb = nfs4_write_done_cb;
5281 hdr->res.server = server;
5282 hdr->timestamp = jiffies;
5284 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5285 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5286 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5289 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5291 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5292 &data->args.seq_args,
5297 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5299 struct inode *inode = data->inode;
5301 trace_nfs4_commit(data, task->tk_status);
5302 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5303 NULL, NULL) == -EAGAIN) {
5304 rpc_restart_call_prepare(task);
5310 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5312 if (!nfs4_sequence_done(task, &data->res.seq_res))
5314 return data->commit_done_cb(task, data);
5317 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5318 struct rpc_clnt **clnt)
5320 struct nfs_server *server = NFS_SERVER(data->inode);
5322 if (data->commit_done_cb == NULL)
5323 data->commit_done_cb = nfs4_commit_done_cb;
5324 data->res.server = server;
5325 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5326 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5327 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5330 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5331 struct nfs_commitres *res)
5333 struct inode *dst_inode = file_inode(dst);
5334 struct nfs_server *server = NFS_SERVER(dst_inode);
5335 struct rpc_message msg = {
5336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5341 args->fh = NFS_FH(dst_inode);
5342 return nfs4_call_sync(server->client, server, &msg,
5343 &args->seq_args, &res->seq_res, 1);
5346 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5348 struct nfs_commitargs args = {
5352 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5353 struct nfs4_exception exception = { };
5357 status = _nfs4_proc_commit(dst, &args, res);
5358 status = nfs4_handle_exception(dst_server, status, &exception);
5359 } while (exception.retry);
5364 struct nfs4_renewdata {
5365 struct nfs_client *client;
5366 unsigned long timestamp;
5370 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5371 * standalone procedure for queueing an asynchronous RENEW.
5373 static void nfs4_renew_release(void *calldata)
5375 struct nfs4_renewdata *data = calldata;
5376 struct nfs_client *clp = data->client;
5378 if (refcount_read(&clp->cl_count) > 1)
5379 nfs4_schedule_state_renewal(clp);
5380 nfs_put_client(clp);
5384 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5386 struct nfs4_renewdata *data = calldata;
5387 struct nfs_client *clp = data->client;
5388 unsigned long timestamp = data->timestamp;
5390 trace_nfs4_renew_async(clp, task->tk_status);
5391 switch (task->tk_status) {
5394 case -NFS4ERR_LEASE_MOVED:
5395 nfs4_schedule_lease_moved_recovery(clp);
5398 /* Unless we're shutting down, schedule state recovery! */
5399 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5401 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5402 nfs4_schedule_lease_recovery(clp);
5405 nfs4_schedule_path_down_recovery(clp);
5407 do_renew_lease(clp, timestamp);
5410 static const struct rpc_call_ops nfs4_renew_ops = {
5411 .rpc_call_done = nfs4_renew_done,
5412 .rpc_release = nfs4_renew_release,
5415 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5417 struct rpc_message msg = {
5418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5422 struct nfs4_renewdata *data;
5424 if (renew_flags == 0)
5426 if (!refcount_inc_not_zero(&clp->cl_count))
5428 data = kmalloc(sizeof(*data), GFP_NOFS);
5430 nfs_put_client(clp);
5434 data->timestamp = jiffies;
5435 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5436 &nfs4_renew_ops, data);
5439 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5441 struct rpc_message msg = {
5442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5446 unsigned long now = jiffies;
5449 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5452 do_renew_lease(clp, now);
5456 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5458 return server->caps & NFS_CAP_ACLS;
5461 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5462 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5465 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5467 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5468 struct page **pages)
5470 struct page *newpage, **spages;
5476 len = min_t(size_t, PAGE_SIZE, buflen);
5477 newpage = alloc_page(GFP_KERNEL);
5479 if (newpage == NULL)
5481 memcpy(page_address(newpage), buf, len);
5486 } while (buflen != 0);
5492 __free_page(spages[rc-1]);
5496 struct nfs4_cached_acl {
5502 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5504 struct nfs_inode *nfsi = NFS_I(inode);
5506 spin_lock(&inode->i_lock);
5507 kfree(nfsi->nfs4_acl);
5508 nfsi->nfs4_acl = acl;
5509 spin_unlock(&inode->i_lock);
5512 static void nfs4_zap_acl_attr(struct inode *inode)
5514 nfs4_set_cached_acl(inode, NULL);
5517 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5519 struct nfs_inode *nfsi = NFS_I(inode);
5520 struct nfs4_cached_acl *acl;
5523 spin_lock(&inode->i_lock);
5524 acl = nfsi->nfs4_acl;
5527 if (buf == NULL) /* user is just asking for length */
5529 if (acl->cached == 0)
5531 ret = -ERANGE; /* see getxattr(2) man page */
5532 if (acl->len > buflen)
5534 memcpy(buf, acl->data, acl->len);
5538 spin_unlock(&inode->i_lock);
5542 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5544 struct nfs4_cached_acl *acl;
5545 size_t buflen = sizeof(*acl) + acl_len;
5547 if (buflen <= PAGE_SIZE) {
5548 acl = kmalloc(buflen, GFP_KERNEL);
5552 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5554 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5561 nfs4_set_cached_acl(inode, acl);
5565 * The getxattr API returns the required buffer length when called with a
5566 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5567 * the required buf. On a NULL buf, we send a page of data to the server
5568 * guessing that the ACL request can be serviced by a page. If so, we cache
5569 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5570 * the cache. If not so, we throw away the page, and cache the required
5571 * length. The next getxattr call will then produce another round trip to
5572 * the server, this time with the input buf of the required size.
5574 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5576 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5577 struct nfs_getaclargs args = {
5578 .fh = NFS_FH(inode),
5582 struct nfs_getaclres res = {
5585 struct rpc_message msg = {
5586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5590 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5591 int ret = -ENOMEM, i;
5593 if (npages > ARRAY_SIZE(pages))
5596 for (i = 0; i < npages; i++) {
5597 pages[i] = alloc_page(GFP_KERNEL);
5602 /* for decoding across pages */
5603 res.acl_scratch = alloc_page(GFP_KERNEL);
5604 if (!res.acl_scratch)
5607 args.acl_len = npages * PAGE_SIZE;
5609 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5610 __func__, buf, buflen, npages, args.acl_len);
5611 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5612 &msg, &args.seq_args, &res.seq_res, 0);
5616 /* Handle the case where the passed-in buffer is too short */
5617 if (res.acl_flags & NFS4_ACL_TRUNC) {
5618 /* Did the user only issue a request for the acl length? */
5624 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5626 if (res.acl_len > buflen) {
5630 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5635 for (i = 0; i < npages; i++)
5637 __free_page(pages[i]);
5638 if (res.acl_scratch)
5639 __free_page(res.acl_scratch);
5643 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5645 struct nfs4_exception exception = {
5646 .interruptible = true,
5650 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5651 trace_nfs4_get_acl(inode, ret);
5654 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5655 } while (exception.retry);
5659 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5661 struct nfs_server *server = NFS_SERVER(inode);
5664 if (!nfs4_server_supports_acls(server))
5666 ret = nfs_revalidate_inode(server, inode);
5669 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5670 nfs_zap_acl_cache(inode);
5671 ret = nfs4_read_cached_acl(inode, buf, buflen);
5673 /* -ENOENT is returned if there is no ACL or if there is an ACL
5674 * but no cached acl data, just the acl length */
5676 return nfs4_get_acl_uncached(inode, buf, buflen);
5679 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5681 struct nfs_server *server = NFS_SERVER(inode);
5682 struct page *pages[NFS4ACL_MAXPAGES];
5683 struct nfs_setaclargs arg = {
5684 .fh = NFS_FH(inode),
5688 struct nfs_setaclres res;
5689 struct rpc_message msg = {
5690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5694 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5697 if (!nfs4_server_supports_acls(server))
5699 if (npages > ARRAY_SIZE(pages))
5701 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5704 nfs4_inode_make_writeable(inode);
5705 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5708 * Free each page after tx, so the only ref left is
5709 * held by the network stack
5712 put_page(pages[i-1]);
5715 * Acl update can result in inode attribute update.
5716 * so mark the attribute cache invalid.
5718 spin_lock(&inode->i_lock);
5719 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5720 | NFS_INO_INVALID_CTIME
5721 | NFS_INO_REVAL_FORCED;
5722 spin_unlock(&inode->i_lock);
5723 nfs_access_zap_cache(inode);
5724 nfs_zap_acl_cache(inode);
5728 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5730 struct nfs4_exception exception = { };
5733 err = __nfs4_proc_set_acl(inode, buf, buflen);
5734 trace_nfs4_set_acl(inode, err);
5735 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5737 } while (exception.retry);
5741 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5742 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5745 struct nfs_server *server = NFS_SERVER(inode);
5746 struct nfs_fattr fattr;
5747 struct nfs4_label label = {0, 0, buflen, buf};
5749 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5750 struct nfs4_getattr_arg arg = {
5751 .fh = NFS_FH(inode),
5754 struct nfs4_getattr_res res = {
5759 struct rpc_message msg = {
5760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5766 nfs_fattr_init(&fattr);
5768 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5771 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5773 if (buflen < label.len)
5778 static int nfs4_get_security_label(struct inode *inode, void *buf,
5781 struct nfs4_exception exception = {
5782 .interruptible = true,
5786 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5790 err = _nfs4_get_security_label(inode, buf, buflen);
5791 trace_nfs4_get_security_label(inode, err);
5792 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5794 } while (exception.retry);
5798 static int _nfs4_do_set_security_label(struct inode *inode,
5799 struct nfs4_label *ilabel,
5800 struct nfs_fattr *fattr,
5801 struct nfs4_label *olabel)
5804 struct iattr sattr = {0};
5805 struct nfs_server *server = NFS_SERVER(inode);
5806 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5807 struct nfs_setattrargs arg = {
5808 .fh = NFS_FH(inode),
5814 struct nfs_setattrres res = {
5819 struct rpc_message msg = {
5820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5826 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5828 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5830 dprintk("%s failed: %d\n", __func__, status);
5835 static int nfs4_do_set_security_label(struct inode *inode,
5836 struct nfs4_label *ilabel,
5837 struct nfs_fattr *fattr,
5838 struct nfs4_label *olabel)
5840 struct nfs4_exception exception = { };
5844 err = _nfs4_do_set_security_label(inode, ilabel,
5846 trace_nfs4_set_security_label(inode, err);
5847 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5849 } while (exception.retry);
5854 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5856 struct nfs4_label ilabel, *olabel = NULL;
5857 struct nfs_fattr fattr;
5860 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5863 nfs_fattr_init(&fattr);
5867 ilabel.label = (char *)buf;
5868 ilabel.len = buflen;
5870 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5871 if (IS_ERR(olabel)) {
5872 status = -PTR_ERR(olabel);
5876 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5878 nfs_setsecurity(inode, &fattr, olabel);
5880 nfs4_label_free(olabel);
5884 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5887 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5888 nfs4_verifier *bootverf)
5892 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5893 /* An impossible timestamp guarantees this value
5894 * will never match a generated boot time. */
5895 verf[0] = cpu_to_be32(U32_MAX);
5896 verf[1] = cpu_to_be32(U32_MAX);
5898 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5899 u64 ns = ktime_to_ns(nn->boot_time);
5901 verf[0] = cpu_to_be32(ns >> 32);
5902 verf[1] = cpu_to_be32(ns);
5904 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5908 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5913 if (clp->cl_owner_id != NULL)
5918 strlen(clp->cl_rpcclient->cl_nodename) +
5920 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5923 if (nfs4_client_id_uniquifier[0] != '\0')
5924 len += strlen(nfs4_client_id_uniquifier) + 1;
5925 if (len > NFS4_OPAQUE_LIMIT + 1)
5929 * Since this string is allocated at mount time, and held until the
5930 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5931 * about a memory-reclaim deadlock.
5933 str = kmalloc(len, GFP_KERNEL);
5938 if (nfs4_client_id_uniquifier[0] != '\0')
5939 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5940 clp->cl_rpcclient->cl_nodename,
5941 nfs4_client_id_uniquifier,
5942 rpc_peeraddr2str(clp->cl_rpcclient,
5945 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5946 clp->cl_rpcclient->cl_nodename,
5947 rpc_peeraddr2str(clp->cl_rpcclient,
5951 clp->cl_owner_id = str;
5956 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5961 len = 10 + 10 + 1 + 10 + 1 +
5962 strlen(nfs4_client_id_uniquifier) + 1 +
5963 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5965 if (len > NFS4_OPAQUE_LIMIT + 1)
5969 * Since this string is allocated at mount time, and held until the
5970 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5971 * about a memory-reclaim deadlock.
5973 str = kmalloc(len, GFP_KERNEL);
5977 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5978 clp->rpc_ops->version, clp->cl_minorversion,
5979 nfs4_client_id_uniquifier,
5980 clp->cl_rpcclient->cl_nodename);
5981 clp->cl_owner_id = str;
5986 nfs4_init_uniform_client_string(struct nfs_client *clp)
5991 if (clp->cl_owner_id != NULL)
5994 if (nfs4_client_id_uniquifier[0] != '\0')
5995 return nfs4_init_uniquifier_client_string(clp);
5997 len = 10 + 10 + 1 + 10 + 1 +
5998 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6000 if (len > NFS4_OPAQUE_LIMIT + 1)
6004 * Since this string is allocated at mount time, and held until the
6005 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6006 * about a memory-reclaim deadlock.
6008 str = kmalloc(len, GFP_KERNEL);
6012 scnprintf(str, len, "Linux NFSv%u.%u %s",
6013 clp->rpc_ops->version, clp->cl_minorversion,
6014 clp->cl_rpcclient->cl_nodename);
6015 clp->cl_owner_id = str;
6020 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6021 * services. Advertise one based on the address family of the
6025 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6027 if (strchr(clp->cl_ipaddr, ':') != NULL)
6028 return scnprintf(buf, len, "tcp6");
6030 return scnprintf(buf, len, "tcp");
6033 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6035 struct nfs4_setclientid *sc = calldata;
6037 if (task->tk_status == 0)
6038 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6041 static const struct rpc_call_ops nfs4_setclientid_ops = {
6042 .rpc_call_done = nfs4_setclientid_done,
6046 * nfs4_proc_setclientid - Negotiate client ID
6047 * @clp: state data structure
6048 * @program: RPC program for NFSv4 callback service
6049 * @port: IP port number for NFS4 callback service
6050 * @cred: credential to use for this call
6051 * @res: where to place the result
6053 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6055 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6056 unsigned short port, const struct cred *cred,
6057 struct nfs4_setclientid_res *res)
6059 nfs4_verifier sc_verifier;
6060 struct nfs4_setclientid setclientid = {
6061 .sc_verifier = &sc_verifier,
6065 struct rpc_message msg = {
6066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6067 .rpc_argp = &setclientid,
6071 struct rpc_task_setup task_setup_data = {
6072 .rpc_client = clp->cl_rpcclient,
6073 .rpc_message = &msg,
6074 .callback_ops = &nfs4_setclientid_ops,
6075 .callback_data = &setclientid,
6076 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6080 /* nfs_client_id4 */
6081 nfs4_init_boot_verifier(clp, &sc_verifier);
6083 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6084 status = nfs4_init_uniform_client_string(clp);
6086 status = nfs4_init_nonuniform_client_string(clp);
6092 setclientid.sc_netid_len =
6093 nfs4_init_callback_netid(clp,
6094 setclientid.sc_netid,
6095 sizeof(setclientid.sc_netid));
6096 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6097 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6098 clp->cl_ipaddr, port >> 8, port & 255);
6100 dprintk("NFS call setclientid auth=%s, '%s'\n",
6101 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6104 status = nfs4_call_sync_custom(&task_setup_data);
6105 if (setclientid.sc_cred) {
6106 kfree(clp->cl_acceptor);
6107 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6108 put_rpccred(setclientid.sc_cred);
6111 trace_nfs4_setclientid(clp, status);
6112 dprintk("NFS reply setclientid: %d\n", status);
6117 * nfs4_proc_setclientid_confirm - Confirm client ID
6118 * @clp: state data structure
6119 * @arg: result of a previous SETCLIENTID
6120 * @cred: credential to use for this call
6122 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6124 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6125 struct nfs4_setclientid_res *arg,
6126 const struct cred *cred)
6128 struct rpc_message msg = {
6129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6135 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6136 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6138 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6139 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6140 trace_nfs4_setclientid_confirm(clp, status);
6141 dprintk("NFS reply setclientid_confirm: %d\n", status);
6145 struct nfs4_delegreturndata {
6146 struct nfs4_delegreturnargs args;
6147 struct nfs4_delegreturnres res;
6149 nfs4_stateid stateid;
6150 unsigned long timestamp;
6152 struct nfs4_layoutreturn_args arg;
6153 struct nfs4_layoutreturn_res res;
6154 struct nfs4_xdr_opaque_data ld_private;
6158 struct nfs_fattr fattr;
6160 struct inode *inode;
6163 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6165 struct nfs4_delegreturndata *data = calldata;
6166 struct nfs4_exception exception = {
6167 .inode = data->inode,
6168 .stateid = &data->stateid,
6171 if (!nfs4_sequence_done(task, &data->res.seq_res))
6174 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6176 /* Handle Layoutreturn errors */
6177 if (pnfs_roc_done(task, data->inode,
6178 &data->args.lr_args,
6180 &data->res.lr_ret) == -EAGAIN)
6183 switch (task->tk_status) {
6185 renew_lease(data->res.server, data->timestamp);
6187 case -NFS4ERR_ADMIN_REVOKED:
6188 case -NFS4ERR_DELEG_REVOKED:
6189 case -NFS4ERR_EXPIRED:
6190 nfs4_free_revoked_stateid(data->res.server,
6192 task->tk_msg.rpc_cred);
6194 case -NFS4ERR_BAD_STATEID:
6195 case -NFS4ERR_STALE_STATEID:
6196 task->tk_status = 0;
6198 case -NFS4ERR_OLD_STATEID:
6199 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6201 task->tk_status = 0;
6203 case -NFS4ERR_ACCESS:
6204 if (data->args.bitmask) {
6205 data->args.bitmask = NULL;
6206 data->res.fattr = NULL;
6211 task->tk_status = nfs4_async_handle_exception(task,
6212 data->res.server, task->tk_status,
6214 if (exception.retry)
6217 data->rpc_status = task->tk_status;
6220 task->tk_status = 0;
6221 rpc_restart_call_prepare(task);
6224 static void nfs4_delegreturn_release(void *calldata)
6226 struct nfs4_delegreturndata *data = calldata;
6227 struct inode *inode = data->inode;
6231 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6233 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6234 nfs_iput_and_deactive(inode);
6239 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6241 struct nfs4_delegreturndata *d_data;
6242 struct pnfs_layout_hdr *lo;
6244 d_data = (struct nfs4_delegreturndata *)data;
6246 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6249 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6250 if (lo && !pnfs_layout_is_valid(lo)) {
6251 d_data->args.lr_args = NULL;
6252 d_data->res.lr_res = NULL;
6255 nfs4_setup_sequence(d_data->res.server->nfs_client,
6256 &d_data->args.seq_args,
6257 &d_data->res.seq_res,
6261 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6262 .rpc_call_prepare = nfs4_delegreturn_prepare,
6263 .rpc_call_done = nfs4_delegreturn_done,
6264 .rpc_release = nfs4_delegreturn_release,
6267 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6269 struct nfs4_delegreturndata *data;
6270 struct nfs_server *server = NFS_SERVER(inode);
6271 struct rpc_task *task;
6272 struct rpc_message msg = {
6273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6276 struct rpc_task_setup task_setup_data = {
6277 .rpc_client = server->client,
6278 .rpc_message = &msg,
6279 .callback_ops = &nfs4_delegreturn_ops,
6280 .flags = RPC_TASK_ASYNC,
6284 data = kzalloc(sizeof(*data), GFP_NOFS);
6287 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6289 nfs4_state_protect(server->nfs_client,
6290 NFS_SP4_MACH_CRED_CLEANUP,
6291 &task_setup_data.rpc_client, &msg);
6293 data->args.fhandle = &data->fh;
6294 data->args.stateid = &data->stateid;
6295 data->args.bitmask = server->cache_consistency_bitmask;
6296 nfs_copy_fh(&data->fh, NFS_FH(inode));
6297 nfs4_stateid_copy(&data->stateid, stateid);
6298 data->res.fattr = &data->fattr;
6299 data->res.server = server;
6300 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6301 data->lr.arg.ld_private = &data->lr.ld_private;
6302 nfs_fattr_init(data->res.fattr);
6303 data->timestamp = jiffies;
6304 data->rpc_status = 0;
6305 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6306 data->inode = nfs_igrab_and_active(inode);
6309 data->args.lr_args = &data->lr.arg;
6310 data->res.lr_res = &data->lr.res;
6312 } else if (data->lr.roc) {
6313 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6314 data->lr.roc = false;
6317 task_setup_data.callback_data = data;
6318 msg.rpc_argp = &data->args;
6319 msg.rpc_resp = &data->res;
6320 task = rpc_run_task(&task_setup_data);
6322 return PTR_ERR(task);
6325 status = rpc_wait_for_completion_task(task);
6328 status = data->rpc_status;
6334 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6336 struct nfs_server *server = NFS_SERVER(inode);
6337 struct nfs4_exception exception = { };
6340 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6341 trace_nfs4_delegreturn(inode, stateid, err);
6343 case -NFS4ERR_STALE_STATEID:
6344 case -NFS4ERR_EXPIRED:
6348 err = nfs4_handle_exception(server, err, &exception);
6349 } while (exception.retry);
6353 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6355 struct inode *inode = state->inode;
6356 struct nfs_server *server = NFS_SERVER(inode);
6357 struct nfs_client *clp = server->nfs_client;
6358 struct nfs_lockt_args arg = {
6359 .fh = NFS_FH(inode),
6362 struct nfs_lockt_res res = {
6365 struct rpc_message msg = {
6366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6369 .rpc_cred = state->owner->so_cred,
6371 struct nfs4_lock_state *lsp;
6374 arg.lock_owner.clientid = clp->cl_clientid;
6375 status = nfs4_set_lock_state(state, request);
6378 lsp = request->fl_u.nfs4_fl.owner;
6379 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6380 arg.lock_owner.s_dev = server->s_dev;
6381 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6384 request->fl_type = F_UNLCK;
6386 case -NFS4ERR_DENIED:
6389 request->fl_ops->fl_release_private(request);
6390 request->fl_ops = NULL;
6395 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6397 struct nfs4_exception exception = {
6398 .interruptible = true,
6403 err = _nfs4_proc_getlk(state, cmd, request);
6404 trace_nfs4_get_lock(request, state, cmd, err);
6405 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6407 } while (exception.retry);
6412 * Update the seqid of a lock stateid after receiving
6413 * NFS4ERR_OLD_STATEID
6415 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6416 struct nfs4_lock_state *lsp)
6418 struct nfs4_state *state = lsp->ls_state;
6421 spin_lock(&state->state_lock);
6422 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6424 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6425 nfs4_stateid_seqid_inc(dst);
6427 dst->seqid = lsp->ls_stateid.seqid;
6430 spin_unlock(&state->state_lock);
6434 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6435 struct nfs4_lock_state *lsp)
6437 struct nfs4_state *state = lsp->ls_state;
6440 spin_lock(&state->state_lock);
6441 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6442 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6443 spin_unlock(&state->state_lock);
6447 struct nfs4_unlockdata {
6448 struct nfs_locku_args arg;
6449 struct nfs_locku_res res;
6450 struct nfs4_lock_state *lsp;
6451 struct nfs_open_context *ctx;
6452 struct nfs_lock_context *l_ctx;
6453 struct file_lock fl;
6454 struct nfs_server *server;
6455 unsigned long timestamp;
6458 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6459 struct nfs_open_context *ctx,
6460 struct nfs4_lock_state *lsp,
6461 struct nfs_seqid *seqid)
6463 struct nfs4_unlockdata *p;
6464 struct nfs4_state *state = lsp->ls_state;
6465 struct inode *inode = state->inode;
6467 p = kzalloc(sizeof(*p), GFP_NOFS);
6470 p->arg.fh = NFS_FH(inode);
6472 p->arg.seqid = seqid;
6473 p->res.seqid = seqid;
6475 /* Ensure we don't close file until we're done freeing locks! */
6476 p->ctx = get_nfs_open_context(ctx);
6477 p->l_ctx = nfs_get_lock_context(ctx);
6478 locks_init_lock(&p->fl);
6479 locks_copy_lock(&p->fl, fl);
6480 p->server = NFS_SERVER(inode);
6481 spin_lock(&state->state_lock);
6482 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6483 spin_unlock(&state->state_lock);
6487 static void nfs4_locku_release_calldata(void *data)
6489 struct nfs4_unlockdata *calldata = data;
6490 nfs_free_seqid(calldata->arg.seqid);
6491 nfs4_put_lock_state(calldata->lsp);
6492 nfs_put_lock_context(calldata->l_ctx);
6493 put_nfs_open_context(calldata->ctx);
6497 static void nfs4_locku_done(struct rpc_task *task, void *data)
6499 struct nfs4_unlockdata *calldata = data;
6500 struct nfs4_exception exception = {
6501 .inode = calldata->lsp->ls_state->inode,
6502 .stateid = &calldata->arg.stateid,
6505 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6507 switch (task->tk_status) {
6509 renew_lease(calldata->server, calldata->timestamp);
6510 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6511 if (nfs4_update_lock_stateid(calldata->lsp,
6512 &calldata->res.stateid))
6515 case -NFS4ERR_ADMIN_REVOKED:
6516 case -NFS4ERR_EXPIRED:
6517 nfs4_free_revoked_stateid(calldata->server,
6518 &calldata->arg.stateid,
6519 task->tk_msg.rpc_cred);
6521 case -NFS4ERR_BAD_STATEID:
6522 case -NFS4ERR_STALE_STATEID:
6523 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6525 rpc_restart_call_prepare(task);
6527 case -NFS4ERR_OLD_STATEID:
6528 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6530 rpc_restart_call_prepare(task);
6533 task->tk_status = nfs4_async_handle_exception(task,
6534 calldata->server, task->tk_status,
6536 if (exception.retry)
6537 rpc_restart_call_prepare(task);
6539 nfs_release_seqid(calldata->arg.seqid);
6542 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6544 struct nfs4_unlockdata *calldata = data;
6546 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6547 nfs_async_iocounter_wait(task, calldata->l_ctx))
6550 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6552 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6553 /* Note: exit _without_ running nfs4_locku_done */
6556 calldata->timestamp = jiffies;
6557 if (nfs4_setup_sequence(calldata->server->nfs_client,
6558 &calldata->arg.seq_args,
6559 &calldata->res.seq_res,
6561 nfs_release_seqid(calldata->arg.seqid);
6564 task->tk_action = NULL;
6566 nfs4_sequence_done(task, &calldata->res.seq_res);
6569 static const struct rpc_call_ops nfs4_locku_ops = {
6570 .rpc_call_prepare = nfs4_locku_prepare,
6571 .rpc_call_done = nfs4_locku_done,
6572 .rpc_release = nfs4_locku_release_calldata,
6575 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6576 struct nfs_open_context *ctx,
6577 struct nfs4_lock_state *lsp,
6578 struct nfs_seqid *seqid)
6580 struct nfs4_unlockdata *data;
6581 struct rpc_message msg = {
6582 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6583 .rpc_cred = ctx->cred,
6585 struct rpc_task_setup task_setup_data = {
6586 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6587 .rpc_message = &msg,
6588 .callback_ops = &nfs4_locku_ops,
6589 .workqueue = nfsiod_workqueue,
6590 .flags = RPC_TASK_ASYNC,
6593 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6594 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6596 /* Ensure this is an unlock - when canceling a lock, the
6597 * canceled lock is passed in, and it won't be an unlock.
6599 fl->fl_type = F_UNLCK;
6600 if (fl->fl_flags & FL_CLOSE)
6601 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6603 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6605 nfs_free_seqid(seqid);
6606 return ERR_PTR(-ENOMEM);
6609 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6610 msg.rpc_argp = &data->arg;
6611 msg.rpc_resp = &data->res;
6612 task_setup_data.callback_data = data;
6613 return rpc_run_task(&task_setup_data);
6616 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6618 struct inode *inode = state->inode;
6619 struct nfs4_state_owner *sp = state->owner;
6620 struct nfs_inode *nfsi = NFS_I(inode);
6621 struct nfs_seqid *seqid;
6622 struct nfs4_lock_state *lsp;
6623 struct rpc_task *task;
6624 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6626 unsigned char fl_flags = request->fl_flags;
6628 status = nfs4_set_lock_state(state, request);
6629 /* Unlock _before_ we do the RPC call */
6630 request->fl_flags |= FL_EXISTS;
6631 /* Exclude nfs_delegation_claim_locks() */
6632 mutex_lock(&sp->so_delegreturn_mutex);
6633 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6634 down_read(&nfsi->rwsem);
6635 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6636 up_read(&nfsi->rwsem);
6637 mutex_unlock(&sp->so_delegreturn_mutex);
6640 up_read(&nfsi->rwsem);
6641 mutex_unlock(&sp->so_delegreturn_mutex);
6644 /* Is this a delegated lock? */
6645 lsp = request->fl_u.nfs4_fl.owner;
6646 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6648 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6649 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6653 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6654 status = PTR_ERR(task);
6657 status = rpc_wait_for_completion_task(task);
6660 request->fl_flags = fl_flags;
6661 trace_nfs4_unlock(request, state, F_SETLK, status);
6665 struct nfs4_lockdata {
6666 struct nfs_lock_args arg;
6667 struct nfs_lock_res res;
6668 struct nfs4_lock_state *lsp;
6669 struct nfs_open_context *ctx;
6670 struct file_lock fl;
6671 unsigned long timestamp;
6674 struct nfs_server *server;
6677 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6678 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6681 struct nfs4_lockdata *p;
6682 struct inode *inode = lsp->ls_state->inode;
6683 struct nfs_server *server = NFS_SERVER(inode);
6684 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6686 p = kzalloc(sizeof(*p), gfp_mask);
6690 p->arg.fh = NFS_FH(inode);
6692 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6693 if (IS_ERR(p->arg.open_seqid))
6695 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6696 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6697 if (IS_ERR(p->arg.lock_seqid))
6698 goto out_free_seqid;
6699 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6700 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6701 p->arg.lock_owner.s_dev = server->s_dev;
6702 p->res.lock_seqid = p->arg.lock_seqid;
6705 p->ctx = get_nfs_open_context(ctx);
6706 locks_init_lock(&p->fl);
6707 locks_copy_lock(&p->fl, fl);
6710 nfs_free_seqid(p->arg.open_seqid);
6716 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6718 struct nfs4_lockdata *data = calldata;
6719 struct nfs4_state *state = data->lsp->ls_state;
6721 dprintk("%s: begin!\n", __func__);
6722 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6724 /* Do we need to do an open_to_lock_owner? */
6725 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6726 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6727 goto out_release_lock_seqid;
6729 nfs4_stateid_copy(&data->arg.open_stateid,
6730 &state->open_stateid);
6731 data->arg.new_lock_owner = 1;
6732 data->res.open_seqid = data->arg.open_seqid;
6734 data->arg.new_lock_owner = 0;
6735 nfs4_stateid_copy(&data->arg.lock_stateid,
6736 &data->lsp->ls_stateid);
6738 if (!nfs4_valid_open_stateid(state)) {
6739 data->rpc_status = -EBADF;
6740 task->tk_action = NULL;
6741 goto out_release_open_seqid;
6743 data->timestamp = jiffies;
6744 if (nfs4_setup_sequence(data->server->nfs_client,
6745 &data->arg.seq_args,
6749 out_release_open_seqid:
6750 nfs_release_seqid(data->arg.open_seqid);
6751 out_release_lock_seqid:
6752 nfs_release_seqid(data->arg.lock_seqid);
6754 nfs4_sequence_done(task, &data->res.seq_res);
6755 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6758 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6760 struct nfs4_lockdata *data = calldata;
6761 struct nfs4_lock_state *lsp = data->lsp;
6763 dprintk("%s: begin!\n", __func__);
6765 if (!nfs4_sequence_done(task, &data->res.seq_res))
6768 data->rpc_status = task->tk_status;
6769 switch (task->tk_status) {
6771 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6773 if (data->arg.new_lock && !data->cancelled) {
6774 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6775 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6778 if (data->arg.new_lock_owner != 0) {
6779 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6780 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6781 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6782 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6785 case -NFS4ERR_BAD_STATEID:
6786 case -NFS4ERR_OLD_STATEID:
6787 case -NFS4ERR_STALE_STATEID:
6788 case -NFS4ERR_EXPIRED:
6789 if (data->arg.new_lock_owner != 0) {
6790 if (!nfs4_stateid_match(&data->arg.open_stateid,
6791 &lsp->ls_state->open_stateid))
6793 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6798 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6801 if (!data->cancelled)
6802 rpc_restart_call_prepare(task);
6806 static void nfs4_lock_release(void *calldata)
6808 struct nfs4_lockdata *data = calldata;
6810 dprintk("%s: begin!\n", __func__);
6811 nfs_free_seqid(data->arg.open_seqid);
6812 if (data->cancelled && data->rpc_status == 0) {
6813 struct rpc_task *task;
6814 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6815 data->arg.lock_seqid);
6817 rpc_put_task_async(task);
6818 dprintk("%s: cancelling lock!\n", __func__);
6820 nfs_free_seqid(data->arg.lock_seqid);
6821 nfs4_put_lock_state(data->lsp);
6822 put_nfs_open_context(data->ctx);
6824 dprintk("%s: done!\n", __func__);
6827 static const struct rpc_call_ops nfs4_lock_ops = {
6828 .rpc_call_prepare = nfs4_lock_prepare,
6829 .rpc_call_done = nfs4_lock_done,
6830 .rpc_release = nfs4_lock_release,
6833 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6836 case -NFS4ERR_ADMIN_REVOKED:
6837 case -NFS4ERR_EXPIRED:
6838 case -NFS4ERR_BAD_STATEID:
6839 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6840 if (new_lock_owner != 0 ||
6841 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6842 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6844 case -NFS4ERR_STALE_STATEID:
6845 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6846 nfs4_schedule_lease_recovery(server->nfs_client);
6850 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6852 struct nfs4_lockdata *data;
6853 struct rpc_task *task;
6854 struct rpc_message msg = {
6855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6856 .rpc_cred = state->owner->so_cred,
6858 struct rpc_task_setup task_setup_data = {
6859 .rpc_client = NFS_CLIENT(state->inode),
6860 .rpc_message = &msg,
6861 .callback_ops = &nfs4_lock_ops,
6862 .workqueue = nfsiod_workqueue,
6863 .flags = RPC_TASK_ASYNC,
6867 dprintk("%s: begin!\n", __func__);
6868 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6869 fl->fl_u.nfs4_fl.owner,
6870 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6874 data->arg.block = 1;
6875 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6876 recovery_type > NFS_LOCK_NEW);
6877 msg.rpc_argp = &data->arg;
6878 msg.rpc_resp = &data->res;
6879 task_setup_data.callback_data = data;
6880 if (recovery_type > NFS_LOCK_NEW) {
6881 if (recovery_type == NFS_LOCK_RECLAIM)
6882 data->arg.reclaim = NFS_LOCK_RECLAIM;
6884 data->arg.new_lock = 1;
6885 task = rpc_run_task(&task_setup_data);
6887 return PTR_ERR(task);
6888 ret = rpc_wait_for_completion_task(task);
6890 ret = data->rpc_status;
6892 nfs4_handle_setlk_error(data->server, data->lsp,
6893 data->arg.new_lock_owner, ret);
6895 data->cancelled = true;
6897 dprintk("%s: done, ret = %d!\n", __func__, ret);
6898 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6902 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6904 struct nfs_server *server = NFS_SERVER(state->inode);
6905 struct nfs4_exception exception = {
6906 .inode = state->inode,
6911 /* Cache the lock if possible... */
6912 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6914 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6915 if (err != -NFS4ERR_DELAY)
6917 nfs4_handle_exception(server, err, &exception);
6918 } while (exception.retry);
6922 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6924 struct nfs_server *server = NFS_SERVER(state->inode);
6925 struct nfs4_exception exception = {
6926 .inode = state->inode,
6930 err = nfs4_set_lock_state(state, request);
6933 if (!recover_lost_locks) {
6934 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6938 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6940 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6944 case -NFS4ERR_GRACE:
6945 case -NFS4ERR_DELAY:
6946 nfs4_handle_exception(server, err, &exception);
6949 } while (exception.retry);
6954 #if defined(CONFIG_NFS_V4_1)
6955 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6957 struct nfs4_lock_state *lsp;
6960 status = nfs4_set_lock_state(state, request);
6963 lsp = request->fl_u.nfs4_fl.owner;
6964 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6965 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6967 return nfs4_lock_expired(state, request);
6971 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6973 struct nfs_inode *nfsi = NFS_I(state->inode);
6974 struct nfs4_state_owner *sp = state->owner;
6975 unsigned char fl_flags = request->fl_flags;
6978 request->fl_flags |= FL_ACCESS;
6979 status = locks_lock_inode_wait(state->inode, request);
6982 mutex_lock(&sp->so_delegreturn_mutex);
6983 down_read(&nfsi->rwsem);
6984 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6985 /* Yes: cache locks! */
6986 /* ...but avoid races with delegation recall... */
6987 request->fl_flags = fl_flags & ~FL_SLEEP;
6988 status = locks_lock_inode_wait(state->inode, request);
6989 up_read(&nfsi->rwsem);
6990 mutex_unlock(&sp->so_delegreturn_mutex);
6993 up_read(&nfsi->rwsem);
6994 mutex_unlock(&sp->so_delegreturn_mutex);
6995 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6997 request->fl_flags = fl_flags;
7001 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7003 struct nfs4_exception exception = {
7005 .inode = state->inode,
7006 .interruptible = true,
7011 err = _nfs4_proc_setlk(state, cmd, request);
7012 if (err == -NFS4ERR_DENIED)
7014 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7016 } while (exception.retry);
7020 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7021 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7024 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7025 struct file_lock *request)
7027 int status = -ERESTARTSYS;
7028 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7030 while(!signalled()) {
7031 status = nfs4_proc_setlk(state, cmd, request);
7032 if ((status != -EAGAIN) || IS_SETLK(cmd))
7034 freezable_schedule_timeout_interruptible(timeout);
7036 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7037 status = -ERESTARTSYS;
7042 #ifdef CONFIG_NFS_V4_1
7043 struct nfs4_lock_waiter {
7044 struct task_struct *task;
7045 struct inode *inode;
7046 struct nfs_lowner *owner;
7050 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7053 struct nfs4_lock_waiter *waiter = wait->private;
7055 /* NULL key means to wake up everyone */
7057 struct cb_notify_lock_args *cbnl = key;
7058 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7059 *wowner = waiter->owner;
7061 /* Only wake if the callback was for the same owner. */
7062 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7065 /* Make sure it's for the right inode */
7066 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7070 /* override "private" so we can use default_wake_function */
7071 wait->private = waiter->task;
7072 ret = woken_wake_function(wait, mode, flags, key);
7074 list_del_init(&wait->entry);
7075 wait->private = waiter;
7080 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7082 int status = -ERESTARTSYS;
7083 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7084 struct nfs_server *server = NFS_SERVER(state->inode);
7085 struct nfs_client *clp = server->nfs_client;
7086 wait_queue_head_t *q = &clp->cl_lock_waitq;
7087 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7088 .id = lsp->ls_seqid.owner_id,
7089 .s_dev = server->s_dev };
7090 struct nfs4_lock_waiter waiter = { .task = current,
7091 .inode = state->inode,
7093 wait_queue_entry_t wait;
7095 /* Don't bother with waitqueue if we don't expect a callback */
7096 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7097 return nfs4_retry_setlk_simple(state, cmd, request);
7100 wait.private = &waiter;
7101 wait.func = nfs4_wake_lock_waiter;
7103 while(!signalled()) {
7104 add_wait_queue(q, &wait);
7105 status = nfs4_proc_setlk(state, cmd, request);
7106 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7107 finish_wait(q, &wait);
7111 status = -ERESTARTSYS;
7112 freezer_do_not_count();
7113 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7115 finish_wait(q, &wait);
7120 #else /* !CONFIG_NFS_V4_1 */
7122 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7124 return nfs4_retry_setlk_simple(state, cmd, request);
7129 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7131 struct nfs_open_context *ctx;
7132 struct nfs4_state *state;
7135 /* verify open state */
7136 ctx = nfs_file_open_context(filp);
7139 if (IS_GETLK(cmd)) {
7141 return nfs4_proc_getlk(state, F_GETLK, request);
7145 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7148 if (request->fl_type == F_UNLCK) {
7150 return nfs4_proc_unlck(state, cmd, request);
7157 if ((request->fl_flags & FL_POSIX) &&
7158 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7162 * Don't rely on the VFS having checked the file open mode,
7163 * since it won't do this for flock() locks.
7165 switch (request->fl_type) {
7167 if (!(filp->f_mode & FMODE_READ))
7171 if (!(filp->f_mode & FMODE_WRITE))
7175 status = nfs4_set_lock_state(state, request);
7179 return nfs4_retry_setlk(state, cmd, request);
7182 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7184 struct nfs_server *server = NFS_SERVER(state->inode);
7187 err = nfs4_set_lock_state(state, fl);
7190 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7191 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7194 struct nfs_release_lockowner_data {
7195 struct nfs4_lock_state *lsp;
7196 struct nfs_server *server;
7197 struct nfs_release_lockowner_args args;
7198 struct nfs_release_lockowner_res res;
7199 unsigned long timestamp;
7202 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7204 struct nfs_release_lockowner_data *data = calldata;
7205 struct nfs_server *server = data->server;
7206 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7207 &data->res.seq_res, task);
7208 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7209 data->timestamp = jiffies;
7212 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7214 struct nfs_release_lockowner_data *data = calldata;
7215 struct nfs_server *server = data->server;
7217 nfs40_sequence_done(task, &data->res.seq_res);
7219 switch (task->tk_status) {
7221 renew_lease(server, data->timestamp);
7223 case -NFS4ERR_STALE_CLIENTID:
7224 case -NFS4ERR_EXPIRED:
7225 nfs4_schedule_lease_recovery(server->nfs_client);
7227 case -NFS4ERR_LEASE_MOVED:
7228 case -NFS4ERR_DELAY:
7229 if (nfs4_async_handle_error(task, server,
7230 NULL, NULL) == -EAGAIN)
7231 rpc_restart_call_prepare(task);
7235 static void nfs4_release_lockowner_release(void *calldata)
7237 struct nfs_release_lockowner_data *data = calldata;
7238 nfs4_free_lock_state(data->server, data->lsp);
7242 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7243 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7244 .rpc_call_done = nfs4_release_lockowner_done,
7245 .rpc_release = nfs4_release_lockowner_release,
7249 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7251 struct nfs_release_lockowner_data *data;
7252 struct rpc_message msg = {
7253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7256 if (server->nfs_client->cl_mvops->minor_version != 0)
7259 data = kmalloc(sizeof(*data), GFP_NOFS);
7263 data->server = server;
7264 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7265 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7266 data->args.lock_owner.s_dev = server->s_dev;
7268 msg.rpc_argp = &data->args;
7269 msg.rpc_resp = &data->res;
7270 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7271 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7274 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7276 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7277 struct dentry *unused, struct inode *inode,
7278 const char *key, const void *buf,
7279 size_t buflen, int flags)
7281 return nfs4_proc_set_acl(inode, buf, buflen);
7284 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7285 struct dentry *unused, struct inode *inode,
7286 const char *key, void *buf, size_t buflen)
7288 return nfs4_proc_get_acl(inode, buf, buflen);
7291 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7293 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7296 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7298 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7299 struct dentry *unused, struct inode *inode,
7300 const char *key, const void *buf,
7301 size_t buflen, int flags)
7303 if (security_ismaclabel(key))
7304 return nfs4_set_security_label(inode, buf, buflen);
7309 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7310 struct dentry *unused, struct inode *inode,
7311 const char *key, void *buf, size_t buflen)
7313 if (security_ismaclabel(key))
7314 return nfs4_get_security_label(inode, buf, buflen);
7319 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7323 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7324 len = security_inode_listsecurity(inode, list, list_len);
7325 if (list_len && len > list_len)
7331 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7332 .prefix = XATTR_SECURITY_PREFIX,
7333 .get = nfs4_xattr_get_nfs4_label,
7334 .set = nfs4_xattr_set_nfs4_label,
7340 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7348 * nfs_fhget will use either the mounted_on_fileid or the fileid
7350 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7352 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7353 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7354 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7355 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7358 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7359 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7360 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7364 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7365 const struct qstr *name,
7366 struct nfs4_fs_locations *fs_locations,
7369 struct nfs_server *server = NFS_SERVER(dir);
7371 struct nfs4_fs_locations_arg args = {
7372 .dir_fh = NFS_FH(dir),
7377 struct nfs4_fs_locations_res res = {
7378 .fs_locations = fs_locations,
7380 struct rpc_message msg = {
7381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7387 dprintk("%s: start\n", __func__);
7389 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7390 bitmask[1] = nfs4_fattr_bitmap[1];
7392 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7393 * is not supported */
7394 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7395 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7397 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7399 nfs_fattr_init(&fs_locations->fattr);
7400 fs_locations->server = server;
7401 fs_locations->nlocations = 0;
7402 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7403 dprintk("%s: returned status = %d\n", __func__, status);
7407 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7408 const struct qstr *name,
7409 struct nfs4_fs_locations *fs_locations,
7412 struct nfs4_exception exception = {
7413 .interruptible = true,
7417 err = _nfs4_proc_fs_locations(client, dir, name,
7418 fs_locations, page);
7419 trace_nfs4_get_fs_locations(dir, name, err);
7420 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7422 } while (exception.retry);
7427 * This operation also signals the server that this client is
7428 * performing migration recovery. The server can stop returning
7429 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7430 * appended to this compound to identify the client ID which is
7431 * performing recovery.
7433 static int _nfs40_proc_get_locations(struct inode *inode,
7434 struct nfs4_fs_locations *locations,
7435 struct page *page, const struct cred *cred)
7437 struct nfs_server *server = NFS_SERVER(inode);
7438 struct rpc_clnt *clnt = server->client;
7440 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7442 struct nfs4_fs_locations_arg args = {
7443 .clientid = server->nfs_client->cl_clientid,
7444 .fh = NFS_FH(inode),
7447 .migration = 1, /* skip LOOKUP */
7448 .renew = 1, /* append RENEW */
7450 struct nfs4_fs_locations_res res = {
7451 .fs_locations = locations,
7455 struct rpc_message msg = {
7456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7461 unsigned long now = jiffies;
7464 nfs_fattr_init(&locations->fattr);
7465 locations->server = server;
7466 locations->nlocations = 0;
7468 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7469 status = nfs4_call_sync_sequence(clnt, server, &msg,
7470 &args.seq_args, &res.seq_res);
7474 renew_lease(server, now);
7478 #ifdef CONFIG_NFS_V4_1
7481 * This operation also signals the server that this client is
7482 * performing migration recovery. The server can stop asserting
7483 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7484 * performing this operation is identified in the SEQUENCE
7485 * operation in this compound.
7487 * When the client supports GETATTR(fs_locations_info), it can
7488 * be plumbed in here.
7490 static int _nfs41_proc_get_locations(struct inode *inode,
7491 struct nfs4_fs_locations *locations,
7492 struct page *page, const struct cred *cred)
7494 struct nfs_server *server = NFS_SERVER(inode);
7495 struct rpc_clnt *clnt = server->client;
7497 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7499 struct nfs4_fs_locations_arg args = {
7500 .fh = NFS_FH(inode),
7503 .migration = 1, /* skip LOOKUP */
7505 struct nfs4_fs_locations_res res = {
7506 .fs_locations = locations,
7509 struct rpc_message msg = {
7510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7517 nfs_fattr_init(&locations->fattr);
7518 locations->server = server;
7519 locations->nlocations = 0;
7521 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7522 status = nfs4_call_sync_sequence(clnt, server, &msg,
7523 &args.seq_args, &res.seq_res);
7524 if (status == NFS4_OK &&
7525 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7526 status = -NFS4ERR_LEASE_MOVED;
7530 #endif /* CONFIG_NFS_V4_1 */
7533 * nfs4_proc_get_locations - discover locations for a migrated FSID
7534 * @inode: inode on FSID that is migrating
7535 * @locations: result of query
7537 * @cred: credential to use for this operation
7539 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7540 * operation failed, or a negative errno if a local error occurred.
7542 * On success, "locations" is filled in, but if the server has
7543 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7546 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7547 * from this client that require migration recovery.
7549 int nfs4_proc_get_locations(struct inode *inode,
7550 struct nfs4_fs_locations *locations,
7551 struct page *page, const struct cred *cred)
7553 struct nfs_server *server = NFS_SERVER(inode);
7554 struct nfs_client *clp = server->nfs_client;
7555 const struct nfs4_mig_recovery_ops *ops =
7556 clp->cl_mvops->mig_recovery_ops;
7557 struct nfs4_exception exception = {
7558 .interruptible = true,
7562 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7563 (unsigned long long)server->fsid.major,
7564 (unsigned long long)server->fsid.minor,
7566 nfs_display_fhandle(NFS_FH(inode), __func__);
7569 status = ops->get_locations(inode, locations, page, cred);
7570 if (status != -NFS4ERR_DELAY)
7572 nfs4_handle_exception(server, status, &exception);
7573 } while (exception.retry);
7578 * This operation also signals the server that this client is
7579 * performing "lease moved" recovery. The server can stop
7580 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7581 * is appended to this compound to identify the client ID which is
7582 * performing recovery.
7584 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7586 struct nfs_server *server = NFS_SERVER(inode);
7587 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7588 struct rpc_clnt *clnt = server->client;
7589 struct nfs4_fsid_present_arg args = {
7590 .fh = NFS_FH(inode),
7591 .clientid = clp->cl_clientid,
7592 .renew = 1, /* append RENEW */
7594 struct nfs4_fsid_present_res res = {
7597 struct rpc_message msg = {
7598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7603 unsigned long now = jiffies;
7606 res.fh = nfs_alloc_fhandle();
7610 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7611 status = nfs4_call_sync_sequence(clnt, server, &msg,
7612 &args.seq_args, &res.seq_res);
7613 nfs_free_fhandle(res.fh);
7617 do_renew_lease(clp, now);
7621 #ifdef CONFIG_NFS_V4_1
7624 * This operation also signals the server that this client is
7625 * performing "lease moved" recovery. The server can stop asserting
7626 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7627 * this operation is identified in the SEQUENCE operation in this
7630 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7632 struct nfs_server *server = NFS_SERVER(inode);
7633 struct rpc_clnt *clnt = server->client;
7634 struct nfs4_fsid_present_arg args = {
7635 .fh = NFS_FH(inode),
7637 struct nfs4_fsid_present_res res = {
7639 struct rpc_message msg = {
7640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7647 res.fh = nfs_alloc_fhandle();
7651 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7652 status = nfs4_call_sync_sequence(clnt, server, &msg,
7653 &args.seq_args, &res.seq_res);
7654 nfs_free_fhandle(res.fh);
7655 if (status == NFS4_OK &&
7656 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7657 status = -NFS4ERR_LEASE_MOVED;
7661 #endif /* CONFIG_NFS_V4_1 */
7664 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7665 * @inode: inode on FSID to check
7666 * @cred: credential to use for this operation
7668 * Server indicates whether the FSID is present, moved, or not
7669 * recognized. This operation is necessary to clear a LEASE_MOVED
7670 * condition for this client ID.
7672 * Returns NFS4_OK if the FSID is present on this server,
7673 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7674 * NFS4ERR code if some error occurred on the server, or a
7675 * negative errno if a local failure occurred.
7677 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7679 struct nfs_server *server = NFS_SERVER(inode);
7680 struct nfs_client *clp = server->nfs_client;
7681 const struct nfs4_mig_recovery_ops *ops =
7682 clp->cl_mvops->mig_recovery_ops;
7683 struct nfs4_exception exception = {
7684 .interruptible = true,
7688 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7689 (unsigned long long)server->fsid.major,
7690 (unsigned long long)server->fsid.minor,
7692 nfs_display_fhandle(NFS_FH(inode), __func__);
7695 status = ops->fsid_present(inode, cred);
7696 if (status != -NFS4ERR_DELAY)
7698 nfs4_handle_exception(server, status, &exception);
7699 } while (exception.retry);
7704 * If 'use_integrity' is true and the state managment nfs_client
7705 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7706 * and the machine credential as per RFC3530bis and RFC5661 Security
7707 * Considerations sections. Otherwise, just use the user cred with the
7708 * filesystem's rpc_client.
7710 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7713 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7714 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
7715 struct nfs4_secinfo_arg args = {
7716 .dir_fh = NFS_FH(dir),
7719 struct nfs4_secinfo_res res = {
7722 struct rpc_message msg = {
7723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7727 struct nfs4_call_sync_data data = {
7728 .seq_server = NFS_SERVER(dir),
7729 .seq_args = &args.seq_args,
7730 .seq_res = &res.seq_res,
7732 struct rpc_task_setup task_setup = {
7734 .rpc_message = &msg,
7735 .callback_ops = clp->cl_mvops->call_sync_ops,
7736 .callback_data = &data,
7737 .flags = RPC_TASK_NO_ROUND_ROBIN,
7739 const struct cred *cred = NULL;
7741 if (use_integrity) {
7742 clnt = clp->cl_rpcclient;
7743 task_setup.rpc_client = clnt;
7745 cred = nfs4_get_clid_cred(clp);
7746 msg.rpc_cred = cred;
7749 dprintk("NFS call secinfo %s\n", name->name);
7751 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7752 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7753 status = nfs4_call_sync_custom(&task_setup);
7755 dprintk("NFS reply secinfo: %d\n", status);
7761 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7762 struct nfs4_secinfo_flavors *flavors)
7764 struct nfs4_exception exception = {
7765 .interruptible = true,
7769 err = -NFS4ERR_WRONGSEC;
7771 /* try to use integrity protection with machine cred */
7772 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7773 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7776 * if unable to use integrity protection, or SECINFO with
7777 * integrity protection returns NFS4ERR_WRONGSEC (which is
7778 * disallowed by spec, but exists in deployed servers) use
7779 * the current filesystem's rpc_client and the user cred.
7781 if (err == -NFS4ERR_WRONGSEC)
7782 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7784 trace_nfs4_secinfo(dir, name, err);
7785 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7787 } while (exception.retry);
7791 #ifdef CONFIG_NFS_V4_1
7793 * Check the exchange flags returned by the server for invalid flags, having
7794 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7797 static int nfs4_check_cl_exchange_flags(u32 flags)
7799 if (flags & ~EXCHGID4_FLAG_MASK_R)
7801 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7802 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7804 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7808 return -NFS4ERR_INVAL;
7812 nfs41_same_server_scope(struct nfs41_server_scope *a,
7813 struct nfs41_server_scope *b)
7815 if (a->server_scope_sz != b->server_scope_sz)
7817 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7821 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7825 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7826 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7830 * nfs4_proc_bind_one_conn_to_session()
7832 * The 4.1 client currently uses the same TCP connection for the
7833 * fore and backchannel.
7836 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7837 struct rpc_xprt *xprt,
7838 struct nfs_client *clp,
7839 const struct cred *cred)
7842 struct nfs41_bind_conn_to_session_args args = {
7844 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7846 struct nfs41_bind_conn_to_session_res res;
7847 struct rpc_message msg = {
7849 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7854 struct rpc_task_setup task_setup_data = {
7857 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7858 .rpc_message = &msg,
7859 .flags = RPC_TASK_TIMEOUT,
7861 struct rpc_task *task;
7863 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7864 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7865 args.dir = NFS4_CDFC4_FORE;
7867 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7868 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7869 args.dir = NFS4_CDFC4_FORE;
7871 task = rpc_run_task(&task_setup_data);
7872 if (!IS_ERR(task)) {
7873 status = task->tk_status;
7876 status = PTR_ERR(task);
7877 trace_nfs4_bind_conn_to_session(clp, status);
7879 if (memcmp(res.sessionid.data,
7880 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7881 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7884 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7885 dprintk("NFS: %s: Unexpected direction from server\n",
7889 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7890 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7899 struct rpc_bind_conn_calldata {
7900 struct nfs_client *clp;
7901 const struct cred *cred;
7905 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7906 struct rpc_xprt *xprt,
7909 struct rpc_bind_conn_calldata *p = calldata;
7911 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7914 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7916 struct rpc_bind_conn_calldata data = {
7920 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7921 nfs4_proc_bind_conn_to_session_callback, &data);
7925 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7926 * and operations we'd like to see to enable certain features in the allow map
7928 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7929 .how = SP4_MACH_CRED,
7930 .enforce.u.words = {
7931 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7932 1 << (OP_EXCHANGE_ID - 32) |
7933 1 << (OP_CREATE_SESSION - 32) |
7934 1 << (OP_DESTROY_SESSION - 32) |
7935 1 << (OP_DESTROY_CLIENTID - 32)
7938 [0] = 1 << (OP_CLOSE) |
7939 1 << (OP_OPEN_DOWNGRADE) |
7941 1 << (OP_DELEGRETURN) |
7943 [1] = 1 << (OP_SECINFO - 32) |
7944 1 << (OP_SECINFO_NO_NAME - 32) |
7945 1 << (OP_LAYOUTRETURN - 32) |
7946 1 << (OP_TEST_STATEID - 32) |
7947 1 << (OP_FREE_STATEID - 32) |
7948 1 << (OP_WRITE - 32)
7953 * Select the state protection mode for client `clp' given the server results
7954 * from exchange_id in `sp'.
7956 * Returns 0 on success, negative errno otherwise.
7958 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7959 struct nfs41_state_protection *sp)
7961 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7962 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7963 1 << (OP_EXCHANGE_ID - 32) |
7964 1 << (OP_CREATE_SESSION - 32) |
7965 1 << (OP_DESTROY_SESSION - 32) |
7966 1 << (OP_DESTROY_CLIENTID - 32)
7968 unsigned long flags = 0;
7972 if (sp->how == SP4_MACH_CRED) {
7973 /* Print state protect result */
7974 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7975 for (i = 0; i <= LAST_NFS4_OP; i++) {
7976 if (test_bit(i, sp->enforce.u.longs))
7977 dfprintk(MOUNT, " enforce op %d\n", i);
7978 if (test_bit(i, sp->allow.u.longs))
7979 dfprintk(MOUNT, " allow op %d\n", i);
7982 /* make sure nothing is on enforce list that isn't supported */
7983 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7984 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7985 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7992 * Minimal mode - state operations are allowed to use machine
7993 * credential. Note this already happens by default, so the
7994 * client doesn't have to do anything more than the negotiation.
7996 * NOTE: we don't care if EXCHANGE_ID is in the list -
7997 * we're already using the machine cred for exchange_id
7998 * and will never use a different cred.
8000 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8001 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8002 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8003 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8004 dfprintk(MOUNT, "sp4_mach_cred:\n");
8005 dfprintk(MOUNT, " minimal mode enabled\n");
8006 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8008 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8013 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8014 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8015 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8016 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8017 dfprintk(MOUNT, " cleanup mode enabled\n");
8018 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8021 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8022 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8023 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8026 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8027 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8028 dfprintk(MOUNT, " secinfo mode enabled\n");
8029 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8032 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8033 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8034 dfprintk(MOUNT, " stateid mode enabled\n");
8035 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8038 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8039 dfprintk(MOUNT, " write mode enabled\n");
8040 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8043 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8044 dfprintk(MOUNT, " commit mode enabled\n");
8045 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8049 clp->cl_sp4_flags = flags;
8053 struct nfs41_exchange_id_data {
8054 struct nfs41_exchange_id_res res;
8055 struct nfs41_exchange_id_args args;
8058 static void nfs4_exchange_id_release(void *data)
8060 struct nfs41_exchange_id_data *cdata =
8061 (struct nfs41_exchange_id_data *)data;
8063 nfs_put_client(cdata->args.client);
8064 kfree(cdata->res.impl_id);
8065 kfree(cdata->res.server_scope);
8066 kfree(cdata->res.server_owner);
8070 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8071 .rpc_release = nfs4_exchange_id_release,
8075 * _nfs4_proc_exchange_id()
8077 * Wrapper for EXCHANGE_ID operation.
8079 static struct rpc_task *
8080 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8081 u32 sp4_how, struct rpc_xprt *xprt)
8083 struct rpc_message msg = {
8084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8087 struct rpc_task_setup task_setup_data = {
8088 .rpc_client = clp->cl_rpcclient,
8089 .callback_ops = &nfs4_exchange_id_call_ops,
8090 .rpc_message = &msg,
8091 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8093 struct nfs41_exchange_id_data *calldata;
8096 if (!refcount_inc_not_zero(&clp->cl_count))
8097 return ERR_PTR(-EIO);
8100 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8104 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8106 status = nfs4_init_uniform_client_string(clp);
8110 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8113 if (unlikely(calldata->res.server_owner == NULL))
8116 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8118 if (unlikely(calldata->res.server_scope == NULL))
8119 goto out_server_owner;
8121 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8122 if (unlikely(calldata->res.impl_id == NULL))
8123 goto out_server_scope;
8127 calldata->args.state_protect.how = SP4_NONE;
8131 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8141 task_setup_data.rpc_xprt = xprt;
8142 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8143 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8144 sizeof(calldata->args.verifier.data));
8146 calldata->args.client = clp;
8147 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8148 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8149 #ifdef CONFIG_NFS_V4_1_MIGRATION
8150 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8152 msg.rpc_argp = &calldata->args;
8153 msg.rpc_resp = &calldata->res;
8154 task_setup_data.callback_data = calldata;
8156 return rpc_run_task(&task_setup_data);
8159 kfree(calldata->res.impl_id);
8161 kfree(calldata->res.server_scope);
8163 kfree(calldata->res.server_owner);
8167 nfs_put_client(clp);
8168 return ERR_PTR(status);
8172 * _nfs4_proc_exchange_id()
8174 * Wrapper for EXCHANGE_ID operation.
8176 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8179 struct rpc_task *task;
8180 struct nfs41_exchange_id_args *argp;
8181 struct nfs41_exchange_id_res *resp;
8184 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8186 return PTR_ERR(task);
8188 argp = task->tk_msg.rpc_argp;
8189 resp = task->tk_msg.rpc_resp;
8190 status = task->tk_status;
8194 status = nfs4_check_cl_exchange_flags(resp->flags);
8198 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8202 clp->cl_clientid = resp->clientid;
8203 clp->cl_exchange_flags = resp->flags;
8204 clp->cl_seqid = resp->seqid;
8205 /* Client ID is not confirmed */
8206 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8207 clear_bit(NFS4_SESSION_ESTABLISHED,
8208 &clp->cl_session->session_state);
8210 if (clp->cl_serverscope != NULL &&
8211 !nfs41_same_server_scope(clp->cl_serverscope,
8212 resp->server_scope)) {
8213 dprintk("%s: server_scope mismatch detected\n",
8215 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8218 swap(clp->cl_serverowner, resp->server_owner);
8219 swap(clp->cl_serverscope, resp->server_scope);
8220 swap(clp->cl_implid, resp->impl_id);
8222 /* Save the EXCHANGE_ID verifier session trunk tests */
8223 memcpy(clp->cl_confirm.data, argp->verifier.data,
8224 sizeof(clp->cl_confirm.data));
8226 trace_nfs4_exchange_id(clp, status);
8232 * nfs4_proc_exchange_id()
8234 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8236 * Since the clientid has expired, all compounds using sessions
8237 * associated with the stale clientid will be returning
8238 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8239 * be in some phase of session reset.
8241 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8243 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8245 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8248 /* try SP4_MACH_CRED if krb5i/p */
8249 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8250 authflavor == RPC_AUTH_GSS_KRB5P) {
8251 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8257 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8261 * nfs4_test_session_trunk
8263 * This is an add_xprt_test() test function called from
8264 * rpc_clnt_setup_test_and_add_xprt.
8266 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8267 * and is dereferrenced in nfs4_exchange_id_release
8269 * Upon success, add the new transport to the rpc_clnt
8271 * @clnt: struct rpc_clnt to get new transport
8272 * @xprt: the rpc_xprt to test
8273 * @data: call data for _nfs4_proc_exchange_id.
8275 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8278 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8279 struct rpc_task *task;
8284 dprintk("--> %s try %s\n", __func__,
8285 xprt->address_strings[RPC_DISPLAY_ADDR]);
8287 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8289 /* Test connection for session trunking. Async exchange_id call */
8290 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8294 status = task->tk_status;
8296 status = nfs4_detect_session_trunking(adata->clp,
8297 task->tk_msg.rpc_resp, xprt);
8300 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8304 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8306 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8307 const struct cred *cred)
8309 struct rpc_message msg = {
8310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8316 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8317 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8318 trace_nfs4_destroy_clientid(clp, status);
8320 dprintk("NFS: Got error %d from the server %s on "
8321 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8325 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8326 const struct cred *cred)
8331 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8332 ret = _nfs4_proc_destroy_clientid(clp, cred);
8334 case -NFS4ERR_DELAY:
8335 case -NFS4ERR_CLIENTID_BUSY:
8345 int nfs4_destroy_clientid(struct nfs_client *clp)
8347 const struct cred *cred;
8350 if (clp->cl_mvops->minor_version < 1)
8352 if (clp->cl_exchange_flags == 0)
8354 if (clp->cl_preserve_clid)
8356 cred = nfs4_get_clid_cred(clp);
8357 ret = nfs4_proc_destroy_clientid(clp, cred);
8361 case -NFS4ERR_STALE_CLIENTID:
8362 clp->cl_exchange_flags = 0;
8368 #endif /* CONFIG_NFS_V4_1 */
8370 struct nfs4_get_lease_time_data {
8371 struct nfs4_get_lease_time_args *args;
8372 struct nfs4_get_lease_time_res *res;
8373 struct nfs_client *clp;
8376 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8379 struct nfs4_get_lease_time_data *data =
8380 (struct nfs4_get_lease_time_data *)calldata;
8382 dprintk("--> %s\n", __func__);
8383 /* just setup sequence, do not trigger session recovery
8384 since we're invoked within one */
8385 nfs4_setup_sequence(data->clp,
8386 &data->args->la_seq_args,
8387 &data->res->lr_seq_res,
8389 dprintk("<-- %s\n", __func__);
8393 * Called from nfs4_state_manager thread for session setup, so don't recover
8394 * from sequence operation or clientid errors.
8396 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8398 struct nfs4_get_lease_time_data *data =
8399 (struct nfs4_get_lease_time_data *)calldata;
8401 dprintk("--> %s\n", __func__);
8402 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8404 switch (task->tk_status) {
8405 case -NFS4ERR_DELAY:
8406 case -NFS4ERR_GRACE:
8407 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8408 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8409 task->tk_status = 0;
8411 case -NFS4ERR_RETRY_UNCACHED_REP:
8412 rpc_restart_call_prepare(task);
8415 dprintk("<-- %s\n", __func__);
8418 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8419 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8420 .rpc_call_done = nfs4_get_lease_time_done,
8423 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8425 struct nfs4_get_lease_time_args args;
8426 struct nfs4_get_lease_time_res res = {
8427 .lr_fsinfo = fsinfo,
8429 struct nfs4_get_lease_time_data data = {
8434 struct rpc_message msg = {
8435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8439 struct rpc_task_setup task_setup = {
8440 .rpc_client = clp->cl_rpcclient,
8441 .rpc_message = &msg,
8442 .callback_ops = &nfs4_get_lease_time_ops,
8443 .callback_data = &data,
8444 .flags = RPC_TASK_TIMEOUT,
8447 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8448 return nfs4_call_sync_custom(&task_setup);
8451 #ifdef CONFIG_NFS_V4_1
8454 * Initialize the values to be used by the client in CREATE_SESSION
8455 * If nfs4_init_session set the fore channel request and response sizes,
8458 * Set the back channel max_resp_sz_cached to zero to force the client to
8459 * always set csa_cachethis to FALSE because the current implementation
8460 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8462 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8463 struct rpc_clnt *clnt)
8465 unsigned int max_rqst_sz, max_resp_sz;
8466 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8467 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8469 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8470 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8472 /* Fore channel attributes */
8473 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8474 args->fc_attrs.max_resp_sz = max_resp_sz;
8475 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8476 args->fc_attrs.max_reqs = max_session_slots;
8478 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8479 "max_ops=%u max_reqs=%u\n",
8481 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8482 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8484 /* Back channel attributes */
8485 args->bc_attrs.max_rqst_sz = max_bc_payload;
8486 args->bc_attrs.max_resp_sz = max_bc_payload;
8487 args->bc_attrs.max_resp_sz_cached = 0;
8488 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8489 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8490 if (args->bc_attrs.max_reqs > max_bc_slots)
8491 args->bc_attrs.max_reqs = max_bc_slots;
8493 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8494 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8496 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8497 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8498 args->bc_attrs.max_reqs);
8501 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8502 struct nfs41_create_session_res *res)
8504 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8505 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8507 if (rcvd->max_resp_sz > sent->max_resp_sz)
8510 * Our requested max_ops is the minimum we need; we're not
8511 * prepared to break up compounds into smaller pieces than that.
8512 * So, no point even trying to continue if the server won't
8515 if (rcvd->max_ops < sent->max_ops)
8517 if (rcvd->max_reqs == 0)
8519 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8520 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8524 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8525 struct nfs41_create_session_res *res)
8527 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8528 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8530 if (!(res->flags & SESSION4_BACK_CHAN))
8532 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8534 if (rcvd->max_resp_sz < sent->max_resp_sz)
8536 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8538 if (rcvd->max_ops > sent->max_ops)
8540 if (rcvd->max_reqs > sent->max_reqs)
8546 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8547 struct nfs41_create_session_res *res)
8551 ret = nfs4_verify_fore_channel_attrs(args, res);
8554 return nfs4_verify_back_channel_attrs(args, res);
8557 static void nfs4_update_session(struct nfs4_session *session,
8558 struct nfs41_create_session_res *res)
8560 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8561 /* Mark client id and session as being confirmed */
8562 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8563 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8564 session->flags = res->flags;
8565 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8566 if (res->flags & SESSION4_BACK_CHAN)
8567 memcpy(&session->bc_attrs, &res->bc_attrs,
8568 sizeof(session->bc_attrs));
8571 static int _nfs4_proc_create_session(struct nfs_client *clp,
8572 const struct cred *cred)
8574 struct nfs4_session *session = clp->cl_session;
8575 struct nfs41_create_session_args args = {
8577 .clientid = clp->cl_clientid,
8578 .seqid = clp->cl_seqid,
8579 .cb_program = NFS4_CALLBACK,
8581 struct nfs41_create_session_res res;
8583 struct rpc_message msg = {
8584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8591 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8592 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8594 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8595 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8596 trace_nfs4_create_session(clp, status);
8599 case -NFS4ERR_STALE_CLIENTID:
8600 case -NFS4ERR_DELAY:
8609 /* Verify the session's negotiated channel_attrs values */
8610 status = nfs4_verify_channel_attrs(&args, &res);
8611 /* Increment the clientid slot sequence id */
8614 nfs4_update_session(session, &res);
8621 * Issues a CREATE_SESSION operation to the server.
8622 * It is the responsibility of the caller to verify the session is
8623 * expired before calling this routine.
8625 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8629 struct nfs4_session *session = clp->cl_session;
8631 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8633 status = _nfs4_proc_create_session(clp, cred);
8637 /* Init or reset the session slot tables */
8638 status = nfs4_setup_session_slot_tables(session);
8639 dprintk("slot table setup returned %d\n", status);
8643 ptr = (unsigned *)&session->sess_id.data[0];
8644 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8645 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8647 dprintk("<-- %s\n", __func__);
8652 * Issue the over-the-wire RPC DESTROY_SESSION.
8653 * The caller must serialize access to this routine.
8655 int nfs4_proc_destroy_session(struct nfs4_session *session,
8656 const struct cred *cred)
8658 struct rpc_message msg = {
8659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8660 .rpc_argp = session,
8665 dprintk("--> nfs4_proc_destroy_session\n");
8667 /* session is still being setup */
8668 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8671 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8672 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8673 trace_nfs4_destroy_session(session->clp, status);
8676 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8677 "Session has been destroyed regardless...\n", status);
8679 dprintk("<-- nfs4_proc_destroy_session\n");
8684 * Renew the cl_session lease.
8686 struct nfs4_sequence_data {
8687 struct nfs_client *clp;
8688 struct nfs4_sequence_args args;
8689 struct nfs4_sequence_res res;
8692 static void nfs41_sequence_release(void *data)
8694 struct nfs4_sequence_data *calldata = data;
8695 struct nfs_client *clp = calldata->clp;
8697 if (refcount_read(&clp->cl_count) > 1)
8698 nfs4_schedule_state_renewal(clp);
8699 nfs_put_client(clp);
8703 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8705 switch(task->tk_status) {
8706 case -NFS4ERR_DELAY:
8707 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8710 nfs4_schedule_lease_recovery(clp);
8715 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8717 struct nfs4_sequence_data *calldata = data;
8718 struct nfs_client *clp = calldata->clp;
8720 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8723 trace_nfs4_sequence(clp, task->tk_status);
8724 if (task->tk_status < 0) {
8725 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8726 if (refcount_read(&clp->cl_count) == 1)
8729 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8730 rpc_restart_call_prepare(task);
8734 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8736 dprintk("<-- %s\n", __func__);
8739 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8741 struct nfs4_sequence_data *calldata = data;
8742 struct nfs_client *clp = calldata->clp;
8743 struct nfs4_sequence_args *args;
8744 struct nfs4_sequence_res *res;
8746 args = task->tk_msg.rpc_argp;
8747 res = task->tk_msg.rpc_resp;
8749 nfs4_setup_sequence(clp, args, res, task);
8752 static const struct rpc_call_ops nfs41_sequence_ops = {
8753 .rpc_call_done = nfs41_sequence_call_done,
8754 .rpc_call_prepare = nfs41_sequence_prepare,
8755 .rpc_release = nfs41_sequence_release,
8758 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8759 const struct cred *cred,
8760 struct nfs4_slot *slot,
8763 struct nfs4_sequence_data *calldata;
8764 struct rpc_message msg = {
8765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8768 struct rpc_task_setup task_setup_data = {
8769 .rpc_client = clp->cl_rpcclient,
8770 .rpc_message = &msg,
8771 .callback_ops = &nfs41_sequence_ops,
8772 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8774 struct rpc_task *ret;
8776 ret = ERR_PTR(-EIO);
8777 if (!refcount_inc_not_zero(&clp->cl_count))
8780 ret = ERR_PTR(-ENOMEM);
8781 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8782 if (calldata == NULL)
8784 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8785 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8786 msg.rpc_argp = &calldata->args;
8787 msg.rpc_resp = &calldata->res;
8788 calldata->clp = clp;
8789 task_setup_data.callback_data = calldata;
8791 ret = rpc_run_task(&task_setup_data);
8796 nfs_put_client(clp);
8798 nfs41_release_slot(slot);
8802 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8804 struct rpc_task *task;
8807 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8809 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8811 ret = PTR_ERR(task);
8813 rpc_put_task_async(task);
8814 dprintk("<-- %s status=%d\n", __func__, ret);
8818 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8820 struct rpc_task *task;
8823 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8825 ret = PTR_ERR(task);
8828 ret = rpc_wait_for_completion_task(task);
8830 ret = task->tk_status;
8833 dprintk("<-- %s status=%d\n", __func__, ret);
8837 struct nfs4_reclaim_complete_data {
8838 struct nfs_client *clp;
8839 struct nfs41_reclaim_complete_args arg;
8840 struct nfs41_reclaim_complete_res res;
8843 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8845 struct nfs4_reclaim_complete_data *calldata = data;
8847 nfs4_setup_sequence(calldata->clp,
8848 &calldata->arg.seq_args,
8849 &calldata->res.seq_res,
8853 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8855 switch(task->tk_status) {
8857 wake_up_all(&clp->cl_lock_waitq);
8859 case -NFS4ERR_COMPLETE_ALREADY:
8860 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8862 case -NFS4ERR_DELAY:
8863 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8865 case -NFS4ERR_RETRY_UNCACHED_REP:
8867 case -NFS4ERR_BADSESSION:
8868 case -NFS4ERR_DEADSESSION:
8869 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8870 nfs4_schedule_session_recovery(clp->cl_session,
8874 nfs4_schedule_lease_recovery(clp);
8879 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8881 struct nfs4_reclaim_complete_data *calldata = data;
8882 struct nfs_client *clp = calldata->clp;
8883 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8885 dprintk("--> %s\n", __func__);
8886 if (!nfs41_sequence_done(task, res))
8889 trace_nfs4_reclaim_complete(clp, task->tk_status);
8890 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8891 rpc_restart_call_prepare(task);
8894 dprintk("<-- %s\n", __func__);
8897 static void nfs4_free_reclaim_complete_data(void *data)
8899 struct nfs4_reclaim_complete_data *calldata = data;
8904 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8905 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8906 .rpc_call_done = nfs4_reclaim_complete_done,
8907 .rpc_release = nfs4_free_reclaim_complete_data,
8911 * Issue a global reclaim complete.
8913 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8914 const struct cred *cred)
8916 struct nfs4_reclaim_complete_data *calldata;
8917 struct rpc_message msg = {
8918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8921 struct rpc_task_setup task_setup_data = {
8922 .rpc_client = clp->cl_rpcclient,
8923 .rpc_message = &msg,
8924 .callback_ops = &nfs4_reclaim_complete_call_ops,
8925 .flags = RPC_TASK_NO_ROUND_ROBIN,
8927 int status = -ENOMEM;
8929 dprintk("--> %s\n", __func__);
8930 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8931 if (calldata == NULL)
8933 calldata->clp = clp;
8934 calldata->arg.one_fs = 0;
8936 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8937 msg.rpc_argp = &calldata->arg;
8938 msg.rpc_resp = &calldata->res;
8939 task_setup_data.callback_data = calldata;
8940 status = nfs4_call_sync_custom(&task_setup_data);
8942 dprintk("<-- %s status=%d\n", __func__, status);
8947 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8949 struct nfs4_layoutget *lgp = calldata;
8950 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8952 dprintk("--> %s\n", __func__);
8953 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8954 &lgp->res.seq_res, task);
8955 dprintk("<-- %s\n", __func__);
8958 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8960 struct nfs4_layoutget *lgp = calldata;
8962 dprintk("--> %s\n", __func__);
8963 nfs41_sequence_process(task, &lgp->res.seq_res);
8964 dprintk("<-- %s\n", __func__);
8968 nfs4_layoutget_handle_exception(struct rpc_task *task,
8969 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8971 struct inode *inode = lgp->args.inode;
8972 struct nfs_server *server = NFS_SERVER(inode);
8973 struct pnfs_layout_hdr *lo;
8974 int nfs4err = task->tk_status;
8975 int err, status = 0;
8978 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8980 nfs4_sequence_free_slot(&lgp->res.seq_res);
8987 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8988 * on the file. set tk_status to -ENODATA to tell upper layer to
8991 case -NFS4ERR_LAYOUTUNAVAILABLE:
8995 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8996 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8998 case -NFS4ERR_BADLAYOUT:
8999 status = -EOVERFLOW;
9002 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9003 * (or clients) writing to the same RAID stripe except when
9004 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9006 * Treat it like we would RECALLCONFLICT -- we retry for a little
9007 * while, and then eventually give up.
9009 case -NFS4ERR_LAYOUTTRYLATER:
9010 if (lgp->args.minlength == 0) {
9011 status = -EOVERFLOW;
9016 case -NFS4ERR_RECALLCONFLICT:
9017 status = -ERECALLCONFLICT;
9019 case -NFS4ERR_DELEG_REVOKED:
9020 case -NFS4ERR_ADMIN_REVOKED:
9021 case -NFS4ERR_EXPIRED:
9022 case -NFS4ERR_BAD_STATEID:
9023 exception->timeout = 0;
9024 spin_lock(&inode->i_lock);
9025 lo = NFS_I(inode)->layout;
9026 /* If the open stateid was bad, then recover it. */
9027 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9028 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9029 spin_unlock(&inode->i_lock);
9030 exception->state = lgp->args.ctx->state;
9031 exception->stateid = &lgp->args.stateid;
9036 * Mark the bad layout state as invalid, then retry
9038 pnfs_mark_layout_stateid_invalid(lo, &head);
9039 spin_unlock(&inode->i_lock);
9040 nfs_commit_inode(inode, 0);
9041 pnfs_free_lseg_list(&head);
9046 err = nfs4_handle_exception(server, nfs4err, exception);
9048 if (exception->retry)
9054 dprintk("<-- %s\n", __func__);
9058 size_t max_response_pages(struct nfs_server *server)
9060 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9061 return nfs_page_array_len(0, max_resp_sz);
9064 static void nfs4_layoutget_release(void *calldata)
9066 struct nfs4_layoutget *lgp = calldata;
9068 dprintk("--> %s\n", __func__);
9069 nfs4_sequence_free_slot(&lgp->res.seq_res);
9070 pnfs_layoutget_free(lgp);
9071 dprintk("<-- %s\n", __func__);
9074 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9075 .rpc_call_prepare = nfs4_layoutget_prepare,
9076 .rpc_call_done = nfs4_layoutget_done,
9077 .rpc_release = nfs4_layoutget_release,
9080 struct pnfs_layout_segment *
9081 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9083 struct inode *inode = lgp->args.inode;
9084 struct nfs_server *server = NFS_SERVER(inode);
9085 struct rpc_task *task;
9086 struct rpc_message msg = {
9087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9088 .rpc_argp = &lgp->args,
9089 .rpc_resp = &lgp->res,
9090 .rpc_cred = lgp->cred,
9092 struct rpc_task_setup task_setup_data = {
9093 .rpc_client = server->client,
9094 .rpc_message = &msg,
9095 .callback_ops = &nfs4_layoutget_call_ops,
9096 .callback_data = lgp,
9097 .flags = RPC_TASK_ASYNC,
9099 struct pnfs_layout_segment *lseg = NULL;
9100 struct nfs4_exception exception = {
9102 .timeout = *timeout,
9106 dprintk("--> %s\n", __func__);
9108 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9109 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9111 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9113 task = rpc_run_task(&task_setup_data);
9115 return ERR_CAST(task);
9116 status = rpc_wait_for_completion_task(task);
9120 if (task->tk_status < 0) {
9121 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9122 *timeout = exception.timeout;
9123 } else if (lgp->res.layoutp->len == 0) {
9125 *timeout = nfs4_update_delay(&exception.timeout);
9127 lseg = pnfs_layout_process(lgp);
9129 trace_nfs4_layoutget(lgp->args.ctx,
9136 dprintk("<-- %s status=%d\n", __func__, status);
9138 return ERR_PTR(status);
9143 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9145 struct nfs4_layoutreturn *lrp = calldata;
9147 dprintk("--> %s\n", __func__);
9148 nfs4_setup_sequence(lrp->clp,
9149 &lrp->args.seq_args,
9152 if (!pnfs_layout_is_valid(lrp->args.layout))
9156 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9158 struct nfs4_layoutreturn *lrp = calldata;
9159 struct nfs_server *server;
9161 dprintk("--> %s\n", __func__);
9163 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9167 * Was there an RPC level error? Assume the call succeeded,
9168 * and that we need to release the layout
9170 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9171 lrp->res.lrs_present = 0;
9175 server = NFS_SERVER(lrp->args.inode);
9176 switch (task->tk_status) {
9177 case -NFS4ERR_OLD_STATEID:
9178 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9184 task->tk_status = 0;
9188 case -NFS4ERR_DELAY:
9189 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9193 dprintk("<-- %s\n", __func__);
9196 task->tk_status = 0;
9197 nfs4_sequence_free_slot(&lrp->res.seq_res);
9198 rpc_restart_call_prepare(task);
9201 static void nfs4_layoutreturn_release(void *calldata)
9203 struct nfs4_layoutreturn *lrp = calldata;
9204 struct pnfs_layout_hdr *lo = lrp->args.layout;
9206 dprintk("--> %s\n", __func__);
9207 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9208 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9209 nfs4_sequence_free_slot(&lrp->res.seq_res);
9210 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9211 lrp->ld_private.ops->free(&lrp->ld_private);
9212 pnfs_put_layout_hdr(lrp->args.layout);
9213 nfs_iput_and_deactive(lrp->inode);
9215 dprintk("<-- %s\n", __func__);
9218 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9219 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9220 .rpc_call_done = nfs4_layoutreturn_done,
9221 .rpc_release = nfs4_layoutreturn_release,
9224 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9226 struct rpc_task *task;
9227 struct rpc_message msg = {
9228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9229 .rpc_argp = &lrp->args,
9230 .rpc_resp = &lrp->res,
9231 .rpc_cred = lrp->cred,
9233 struct rpc_task_setup task_setup_data = {
9234 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9235 .rpc_message = &msg,
9236 .callback_ops = &nfs4_layoutreturn_call_ops,
9237 .callback_data = lrp,
9241 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9242 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9243 &task_setup_data.rpc_client, &msg);
9245 dprintk("--> %s\n", __func__);
9247 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9249 nfs4_layoutreturn_release(lrp);
9252 task_setup_data.flags |= RPC_TASK_ASYNC;
9254 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9255 task = rpc_run_task(&task_setup_data);
9257 return PTR_ERR(task);
9259 status = task->tk_status;
9260 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9261 dprintk("<-- %s status=%d\n", __func__, status);
9267 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9268 struct pnfs_device *pdev,
9269 const struct cred *cred)
9271 struct nfs4_getdeviceinfo_args args = {
9273 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9274 NOTIFY_DEVICEID4_DELETE,
9276 struct nfs4_getdeviceinfo_res res = {
9279 struct rpc_message msg = {
9280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9287 dprintk("--> %s\n", __func__);
9288 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9289 if (res.notification & ~args.notify_types)
9290 dprintk("%s: unsupported notification\n", __func__);
9291 if (res.notification != args.notify_types)
9294 dprintk("<-- %s status=%d\n", __func__, status);
9299 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9300 struct pnfs_device *pdev,
9301 const struct cred *cred)
9303 struct nfs4_exception exception = { };
9307 err = nfs4_handle_exception(server,
9308 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9310 } while (exception.retry);
9313 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9315 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9317 struct nfs4_layoutcommit_data *data = calldata;
9318 struct nfs_server *server = NFS_SERVER(data->args.inode);
9320 nfs4_setup_sequence(server->nfs_client,
9321 &data->args.seq_args,
9327 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9329 struct nfs4_layoutcommit_data *data = calldata;
9330 struct nfs_server *server = NFS_SERVER(data->args.inode);
9332 if (!nfs41_sequence_done(task, &data->res.seq_res))
9335 switch (task->tk_status) { /* Just ignore these failures */
9336 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9337 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9338 case -NFS4ERR_BADLAYOUT: /* no layout */
9339 case -NFS4ERR_GRACE: /* loca_recalim always false */
9340 task->tk_status = 0;
9344 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9345 rpc_restart_call_prepare(task);
9351 static void nfs4_layoutcommit_release(void *calldata)
9353 struct nfs4_layoutcommit_data *data = calldata;
9355 pnfs_cleanup_layoutcommit(data);
9356 nfs_post_op_update_inode_force_wcc(data->args.inode,
9358 put_cred(data->cred);
9359 nfs_iput_and_deactive(data->inode);
9363 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9364 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9365 .rpc_call_done = nfs4_layoutcommit_done,
9366 .rpc_release = nfs4_layoutcommit_release,
9370 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9372 struct rpc_message msg = {
9373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9374 .rpc_argp = &data->args,
9375 .rpc_resp = &data->res,
9376 .rpc_cred = data->cred,
9378 struct rpc_task_setup task_setup_data = {
9379 .task = &data->task,
9380 .rpc_client = NFS_CLIENT(data->args.inode),
9381 .rpc_message = &msg,
9382 .callback_ops = &nfs4_layoutcommit_ops,
9383 .callback_data = data,
9385 struct rpc_task *task;
9388 dprintk("NFS: initiating layoutcommit call. sync %d "
9389 "lbw: %llu inode %lu\n", sync,
9390 data->args.lastbytewritten,
9391 data->args.inode->i_ino);
9394 data->inode = nfs_igrab_and_active(data->args.inode);
9395 if (data->inode == NULL) {
9396 nfs4_layoutcommit_release(data);
9399 task_setup_data.flags = RPC_TASK_ASYNC;
9401 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9402 task = rpc_run_task(&task_setup_data);
9404 return PTR_ERR(task);
9406 status = task->tk_status;
9407 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9408 dprintk("%s: status %d\n", __func__, status);
9414 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9415 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9418 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9419 struct nfs_fsinfo *info,
9420 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9422 struct nfs41_secinfo_no_name_args args = {
9423 .style = SECINFO_STYLE_CURRENT_FH,
9425 struct nfs4_secinfo_res res = {
9428 struct rpc_message msg = {
9429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9433 struct rpc_clnt *clnt = server->client;
9434 struct nfs4_call_sync_data data = {
9435 .seq_server = server,
9436 .seq_args = &args.seq_args,
9437 .seq_res = &res.seq_res,
9439 struct rpc_task_setup task_setup = {
9440 .rpc_client = server->client,
9441 .rpc_message = &msg,
9442 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9443 .callback_data = &data,
9444 .flags = RPC_TASK_NO_ROUND_ROBIN,
9446 const struct cred *cred = NULL;
9449 if (use_integrity) {
9450 clnt = server->nfs_client->cl_rpcclient;
9451 task_setup.rpc_client = clnt;
9453 cred = nfs4_get_clid_cred(server->nfs_client);
9454 msg.rpc_cred = cred;
9457 dprintk("--> %s\n", __func__);
9458 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9459 status = nfs4_call_sync_custom(&task_setup);
9460 dprintk("<-- %s status=%d\n", __func__, status);
9468 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9469 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9471 struct nfs4_exception exception = {
9472 .interruptible = true,
9476 /* first try using integrity protection */
9477 err = -NFS4ERR_WRONGSEC;
9479 /* try to use integrity protection with machine cred */
9480 if (_nfs4_is_integrity_protected(server->nfs_client))
9481 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9485 * if unable to use integrity protection, or SECINFO with
9486 * integrity protection returns NFS4ERR_WRONGSEC (which is
9487 * disallowed by spec, but exists in deployed servers) use
9488 * the current filesystem's rpc_client and the user cred.
9490 if (err == -NFS4ERR_WRONGSEC)
9491 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9496 case -NFS4ERR_WRONGSEC:
9500 err = nfs4_handle_exception(server, err, &exception);
9502 } while (exception.retry);
9508 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9509 struct nfs_fsinfo *info)
9513 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9514 struct nfs4_secinfo_flavors *flavors;
9515 struct nfs4_secinfo4 *secinfo;
9518 page = alloc_page(GFP_KERNEL);
9524 flavors = page_address(page);
9525 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9528 * Fall back on "guess and check" method if
9529 * the server doesn't support SECINFO_NO_NAME
9531 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9532 err = nfs4_find_root_sec(server, fhandle, info);
9538 for (i = 0; i < flavors->num_flavors; i++) {
9539 secinfo = &flavors->flavors[i];
9541 switch (secinfo->flavor) {
9545 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9546 &secinfo->flavor_info);
9549 flavor = RPC_AUTH_MAXFLAVOR;
9553 if (!nfs_auth_info_match(&server->auth_info, flavor))
9554 flavor = RPC_AUTH_MAXFLAVOR;
9556 if (flavor != RPC_AUTH_MAXFLAVOR) {
9557 err = nfs4_lookup_root_sec(server, fhandle,
9564 if (flavor == RPC_AUTH_MAXFLAVOR)
9575 static int _nfs41_test_stateid(struct nfs_server *server,
9576 nfs4_stateid *stateid,
9577 const struct cred *cred)
9580 struct nfs41_test_stateid_args args = {
9583 struct nfs41_test_stateid_res res;
9584 struct rpc_message msg = {
9585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9590 struct rpc_clnt *rpc_client = server->client;
9592 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9595 dprintk("NFS call test_stateid %p\n", stateid);
9596 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9597 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9598 &args.seq_args, &res.seq_res);
9599 if (status != NFS_OK) {
9600 dprintk("NFS reply test_stateid: failed, %d\n", status);
9603 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9607 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9608 int err, struct nfs4_exception *exception)
9610 exception->retry = 0;
9612 case -NFS4ERR_DELAY:
9613 case -NFS4ERR_RETRY_UNCACHED_REP:
9614 nfs4_handle_exception(server, err, exception);
9616 case -NFS4ERR_BADSESSION:
9617 case -NFS4ERR_BADSLOT:
9618 case -NFS4ERR_BAD_HIGH_SLOT:
9619 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9620 case -NFS4ERR_DEADSESSION:
9621 nfs4_do_handle_exception(server, err, exception);
9626 * nfs41_test_stateid - perform a TEST_STATEID operation
9628 * @server: server / transport on which to perform the operation
9629 * @stateid: state ID to test
9632 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9633 * Otherwise a negative NFS4ERR value is returned if the operation
9634 * failed or the state ID is not currently valid.
9636 static int nfs41_test_stateid(struct nfs_server *server,
9637 nfs4_stateid *stateid,
9638 const struct cred *cred)
9640 struct nfs4_exception exception = {
9641 .interruptible = true,
9645 err = _nfs41_test_stateid(server, stateid, cred);
9646 nfs4_handle_delay_or_session_error(server, err, &exception);
9647 } while (exception.retry);
9651 struct nfs_free_stateid_data {
9652 struct nfs_server *server;
9653 struct nfs41_free_stateid_args args;
9654 struct nfs41_free_stateid_res res;
9657 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9659 struct nfs_free_stateid_data *data = calldata;
9660 nfs4_setup_sequence(data->server->nfs_client,
9661 &data->args.seq_args,
9666 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9668 struct nfs_free_stateid_data *data = calldata;
9670 nfs41_sequence_done(task, &data->res.seq_res);
9672 switch (task->tk_status) {
9673 case -NFS4ERR_DELAY:
9674 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9675 rpc_restart_call_prepare(task);
9679 static void nfs41_free_stateid_release(void *calldata)
9684 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9685 .rpc_call_prepare = nfs41_free_stateid_prepare,
9686 .rpc_call_done = nfs41_free_stateid_done,
9687 .rpc_release = nfs41_free_stateid_release,
9691 * nfs41_free_stateid - perform a FREE_STATEID operation
9693 * @server: server / transport on which to perform the operation
9694 * @stateid: state ID to release
9696 * @privileged: set to true if this call needs to be privileged
9698 * Note: this function is always asynchronous.
9700 static int nfs41_free_stateid(struct nfs_server *server,
9701 const nfs4_stateid *stateid,
9702 const struct cred *cred,
9705 struct rpc_message msg = {
9706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9709 struct rpc_task_setup task_setup = {
9710 .rpc_client = server->client,
9711 .rpc_message = &msg,
9712 .callback_ops = &nfs41_free_stateid_ops,
9713 .flags = RPC_TASK_ASYNC,
9715 struct nfs_free_stateid_data *data;
9716 struct rpc_task *task;
9718 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9719 &task_setup.rpc_client, &msg);
9721 dprintk("NFS call free_stateid %p\n", stateid);
9722 data = kmalloc(sizeof(*data), GFP_NOFS);
9725 data->server = server;
9726 nfs4_stateid_copy(&data->args.stateid, stateid);
9728 task_setup.callback_data = data;
9730 msg.rpc_argp = &data->args;
9731 msg.rpc_resp = &data->res;
9732 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9733 task = rpc_run_task(&task_setup);
9735 return PTR_ERR(task);
9741 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9743 const struct cred *cred = lsp->ls_state->owner->so_cred;
9745 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9746 nfs4_free_lock_state(server, lsp);
9749 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9750 const nfs4_stateid *s2)
9752 if (s1->type != s2->type)
9755 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9758 if (s1->seqid == s2->seqid)
9761 return s1->seqid == 0 || s2->seqid == 0;
9764 #endif /* CONFIG_NFS_V4_1 */
9766 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9767 const nfs4_stateid *s2)
9769 return nfs4_stateid_match(s1, s2);
9773 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9774 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9775 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9776 .recover_open = nfs4_open_reclaim,
9777 .recover_lock = nfs4_lock_reclaim,
9778 .establish_clid = nfs4_init_clientid,
9779 .detect_trunking = nfs40_discover_server_trunking,
9782 #if defined(CONFIG_NFS_V4_1)
9783 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9784 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9785 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9786 .recover_open = nfs4_open_reclaim,
9787 .recover_lock = nfs4_lock_reclaim,
9788 .establish_clid = nfs41_init_clientid,
9789 .reclaim_complete = nfs41_proc_reclaim_complete,
9790 .detect_trunking = nfs41_discover_server_trunking,
9792 #endif /* CONFIG_NFS_V4_1 */
9794 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9795 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9796 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9797 .recover_open = nfs40_open_expired,
9798 .recover_lock = nfs4_lock_expired,
9799 .establish_clid = nfs4_init_clientid,
9802 #if defined(CONFIG_NFS_V4_1)
9803 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9804 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9805 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9806 .recover_open = nfs41_open_expired,
9807 .recover_lock = nfs41_lock_expired,
9808 .establish_clid = nfs41_init_clientid,
9810 #endif /* CONFIG_NFS_V4_1 */
9812 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9813 .sched_state_renewal = nfs4_proc_async_renew,
9814 .get_state_renewal_cred = nfs4_get_renew_cred,
9815 .renew_lease = nfs4_proc_renew,
9818 #if defined(CONFIG_NFS_V4_1)
9819 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9820 .sched_state_renewal = nfs41_proc_async_sequence,
9821 .get_state_renewal_cred = nfs4_get_machine_cred,
9822 .renew_lease = nfs4_proc_sequence,
9826 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9827 .get_locations = _nfs40_proc_get_locations,
9828 .fsid_present = _nfs40_proc_fsid_present,
9831 #if defined(CONFIG_NFS_V4_1)
9832 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9833 .get_locations = _nfs41_proc_get_locations,
9834 .fsid_present = _nfs41_proc_fsid_present,
9836 #endif /* CONFIG_NFS_V4_1 */
9838 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9840 .init_caps = NFS_CAP_READDIRPLUS
9841 | NFS_CAP_ATOMIC_OPEN
9842 | NFS_CAP_POSIX_LOCK,
9843 .init_client = nfs40_init_client,
9844 .shutdown_client = nfs40_shutdown_client,
9845 .match_stateid = nfs4_match_stateid,
9846 .find_root_sec = nfs4_find_root_sec,
9847 .free_lock_state = nfs4_release_lockowner,
9848 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9849 .alloc_seqid = nfs_alloc_seqid,
9850 .call_sync_ops = &nfs40_call_sync_ops,
9851 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9852 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9853 .state_renewal_ops = &nfs40_state_renewal_ops,
9854 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9857 #if defined(CONFIG_NFS_V4_1)
9858 static struct nfs_seqid *
9859 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9864 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9866 .init_caps = NFS_CAP_READDIRPLUS
9867 | NFS_CAP_ATOMIC_OPEN
9868 | NFS_CAP_POSIX_LOCK
9869 | NFS_CAP_STATEID_NFSV41
9870 | NFS_CAP_ATOMIC_OPEN_V1
9872 .init_client = nfs41_init_client,
9873 .shutdown_client = nfs41_shutdown_client,
9874 .match_stateid = nfs41_match_stateid,
9875 .find_root_sec = nfs41_find_root_sec,
9876 .free_lock_state = nfs41_free_lock_state,
9877 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9878 .alloc_seqid = nfs_alloc_no_seqid,
9879 .session_trunk = nfs4_test_session_trunk,
9880 .call_sync_ops = &nfs41_call_sync_ops,
9881 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9882 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9883 .state_renewal_ops = &nfs41_state_renewal_ops,
9884 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9888 #if defined(CONFIG_NFS_V4_2)
9889 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9891 .init_caps = NFS_CAP_READDIRPLUS
9892 | NFS_CAP_ATOMIC_OPEN
9893 | NFS_CAP_POSIX_LOCK
9894 | NFS_CAP_STATEID_NFSV41
9895 | NFS_CAP_ATOMIC_OPEN_V1
9899 | NFS_CAP_OFFLOAD_CANCEL
9900 | NFS_CAP_DEALLOCATE
9902 | NFS_CAP_LAYOUTSTATS
9904 | NFS_CAP_LAYOUTERROR,
9905 .init_client = nfs41_init_client,
9906 .shutdown_client = nfs41_shutdown_client,
9907 .match_stateid = nfs41_match_stateid,
9908 .find_root_sec = nfs41_find_root_sec,
9909 .free_lock_state = nfs41_free_lock_state,
9910 .call_sync_ops = &nfs41_call_sync_ops,
9911 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9912 .alloc_seqid = nfs_alloc_no_seqid,
9913 .session_trunk = nfs4_test_session_trunk,
9914 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9915 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9916 .state_renewal_ops = &nfs41_state_renewal_ops,
9917 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9921 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9922 [0] = &nfs_v4_0_minor_ops,
9923 #if defined(CONFIG_NFS_V4_1)
9924 [1] = &nfs_v4_1_minor_ops,
9926 #if defined(CONFIG_NFS_V4_2)
9927 [2] = &nfs_v4_2_minor_ops,
9931 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9933 ssize_t error, error2;
9935 error = generic_listxattr(dentry, list, size);
9943 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9946 return error + error2;
9949 static const struct inode_operations nfs4_dir_inode_operations = {
9950 .create = nfs_create,
9951 .lookup = nfs_lookup,
9952 .atomic_open = nfs_atomic_open,
9954 .unlink = nfs_unlink,
9955 .symlink = nfs_symlink,
9959 .rename = nfs_rename,
9960 .permission = nfs_permission,
9961 .getattr = nfs_getattr,
9962 .setattr = nfs_setattr,
9963 .listxattr = nfs4_listxattr,
9966 static const struct inode_operations nfs4_file_inode_operations = {
9967 .permission = nfs_permission,
9968 .getattr = nfs_getattr,
9969 .setattr = nfs_setattr,
9970 .listxattr = nfs4_listxattr,
9973 const struct nfs_rpc_ops nfs_v4_clientops = {
9974 .version = 4, /* protocol version */
9975 .dentry_ops = &nfs4_dentry_operations,
9976 .dir_inode_ops = &nfs4_dir_inode_operations,
9977 .file_inode_ops = &nfs4_file_inode_operations,
9978 .file_ops = &nfs4_file_operations,
9979 .getroot = nfs4_proc_get_root,
9980 .submount = nfs4_submount,
9981 .try_mount = nfs4_try_mount,
9982 .getattr = nfs4_proc_getattr,
9983 .setattr = nfs4_proc_setattr,
9984 .lookup = nfs4_proc_lookup,
9985 .lookupp = nfs4_proc_lookupp,
9986 .access = nfs4_proc_access,
9987 .readlink = nfs4_proc_readlink,
9988 .create = nfs4_proc_create,
9989 .remove = nfs4_proc_remove,
9990 .unlink_setup = nfs4_proc_unlink_setup,
9991 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9992 .unlink_done = nfs4_proc_unlink_done,
9993 .rename_setup = nfs4_proc_rename_setup,
9994 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9995 .rename_done = nfs4_proc_rename_done,
9996 .link = nfs4_proc_link,
9997 .symlink = nfs4_proc_symlink,
9998 .mkdir = nfs4_proc_mkdir,
9999 .rmdir = nfs4_proc_rmdir,
10000 .readdir = nfs4_proc_readdir,
10001 .mknod = nfs4_proc_mknod,
10002 .statfs = nfs4_proc_statfs,
10003 .fsinfo = nfs4_proc_fsinfo,
10004 .pathconf = nfs4_proc_pathconf,
10005 .set_capabilities = nfs4_server_capabilities,
10006 .decode_dirent = nfs4_decode_dirent,
10007 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10008 .read_setup = nfs4_proc_read_setup,
10009 .read_done = nfs4_read_done,
10010 .write_setup = nfs4_proc_write_setup,
10011 .write_done = nfs4_write_done,
10012 .commit_setup = nfs4_proc_commit_setup,
10013 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10014 .commit_done = nfs4_commit_done,
10015 .lock = nfs4_proc_lock,
10016 .clear_acl_cache = nfs4_zap_acl_attr,
10017 .close_context = nfs4_close_context,
10018 .open_context = nfs4_atomic_open,
10019 .have_delegation = nfs4_have_delegation,
10020 .alloc_client = nfs4_alloc_client,
10021 .init_client = nfs4_init_client,
10022 .free_client = nfs4_free_client,
10023 .create_server = nfs4_create_server,
10024 .clone_server = nfs_clone_server,
10027 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10028 .name = XATTR_NAME_NFSV4_ACL,
10029 .list = nfs4_xattr_list_nfs4_acl,
10030 .get = nfs4_xattr_get_nfs4_acl,
10031 .set = nfs4_xattr_set_nfs4_acl,
10034 const struct xattr_handler *nfs4_xattr_handlers[] = {
10035 &nfs4_xattr_nfs4_acl_handler,
10036 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10037 &nfs4_xattr_nfs4_label_handler,
10044 * c-basic-offset: 8