4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
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(struct rpc_clnt *clnt, long *timeout)
409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
411 if (fatal_signal_pending(current))
416 /* This is the error handling routine for processes that are allowed
419 static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
422 struct nfs_client *clp = server->nfs_client;
423 struct nfs4_state *state = exception->state;
424 const nfs4_stateid *stateid = exception->stateid;
425 struct inode *inode = exception->inode;
428 exception->delay = 0;
429 exception->recovering = 0;
430 exception->retry = 0;
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
438 case -NFS4ERR_BADHANDLE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
445 case -NFS4ERR_EXPIRED:
446 case -NFS4ERR_BAD_STATEID:
447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
450 goto wait_on_recovery;
453 case -NFS4ERR_OPENMODE:
457 err = nfs_async_inode_return_delegation(inode,
460 goto wait_on_recovery;
461 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
462 exception->retry = 1;
468 ret = nfs4_schedule_stateid_recovery(server, state);
471 goto wait_on_recovery;
472 case -NFS4ERR_STALE_STATEID:
473 case -NFS4ERR_STALE_CLIENTID:
474 nfs4_schedule_lease_recovery(clp);
475 goto wait_on_recovery;
477 ret = nfs4_schedule_migration_recovery(server);
480 goto wait_on_recovery;
481 case -NFS4ERR_LEASE_MOVED:
482 nfs4_schedule_lease_moved_recovery(clp);
483 goto wait_on_recovery;
484 #if defined(CONFIG_NFS_V4_1)
485 case -NFS4ERR_BADSESSION:
486 case -NFS4ERR_BADSLOT:
487 case -NFS4ERR_BAD_HIGH_SLOT:
488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
489 case -NFS4ERR_DEADSESSION:
490 case -NFS4ERR_SEQ_FALSE_RETRY:
491 case -NFS4ERR_SEQ_MISORDERED:
492 dprintk("%s ERROR: %d Reset session\n", __func__,
494 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
495 goto wait_on_recovery;
496 #endif /* defined(CONFIG_NFS_V4_1) */
497 case -NFS4ERR_FILE_OPEN:
498 if (exception->timeout > HZ) {
499 /* We have retried a decent amount, time to
507 nfs_inc_server_stats(server, NFSIOS_DELAY);
510 case -NFS4ERR_LAYOUTTRYLATER:
511 case -NFS4ERR_RECALLCONFLICT:
512 exception->delay = 1;
515 case -NFS4ERR_RETRY_UNCACHED_REP:
516 case -NFS4ERR_OLD_STATEID:
517 exception->retry = 1;
519 case -NFS4ERR_BADOWNER:
520 /* The following works around a Linux server bug! */
521 case -NFS4ERR_BADNAME:
522 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
523 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
524 exception->retry = 1;
525 printk(KERN_WARNING "NFS: v4 server %s "
526 "does not accept raw "
528 "Reenabling the idmapper.\n",
529 server->nfs_client->cl_hostname);
532 /* We failed to handle the error */
533 return nfs4_map_errors(ret);
535 exception->recovering = 1;
539 /* This is the error handling routine for processes that are allowed
542 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
544 struct nfs_client *clp = server->nfs_client;
547 ret = nfs4_do_handle_exception(server, errorcode, exception);
548 if (exception->delay) {
549 ret = nfs4_delay(server->client, &exception->timeout);
552 if (exception->recovering) {
553 ret = nfs4_wait_clnt_recover(clp);
554 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
561 exception->retry = 1;
566 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
567 int errorcode, struct nfs4_exception *exception)
569 struct nfs_client *clp = server->nfs_client;
572 ret = nfs4_do_handle_exception(server, errorcode, exception);
573 if (exception->delay) {
574 rpc_delay(task, nfs4_update_delay(&exception->timeout));
577 if (exception->recovering) {
578 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
579 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
580 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
583 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
588 exception->retry = 1;
590 * For NFS4ERR_MOVED, the client transport will need to
591 * be recomputed after migration recovery has completed.
593 if (errorcode == -NFS4ERR_MOVED)
594 rpc_task_release_transport(task);
600 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
601 struct nfs4_state *state, long *timeout)
603 struct nfs4_exception exception = {
607 if (task->tk_status >= 0)
610 exception.timeout = *timeout;
611 task->tk_status = nfs4_async_handle_exception(task, server,
614 if (exception.delay && timeout)
615 *timeout = exception.timeout;
622 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
623 * or 'false' otherwise.
625 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
627 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
628 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
631 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
633 spin_lock(&clp->cl_lock);
634 if (time_before(clp->cl_last_renewal,timestamp))
635 clp->cl_last_renewal = timestamp;
636 spin_unlock(&clp->cl_lock);
639 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
641 struct nfs_client *clp = server->nfs_client;
643 if (!nfs4_has_session(clp))
644 do_renew_lease(clp, timestamp);
647 struct nfs4_call_sync_data {
648 const struct nfs_server *seq_server;
649 struct nfs4_sequence_args *seq_args;
650 struct nfs4_sequence_res *seq_res;
653 void nfs4_init_sequence(struct nfs4_sequence_args *args,
654 struct nfs4_sequence_res *res, int cache_reply,
657 args->sa_slot = NULL;
658 args->sa_cache_this = cache_reply;
659 args->sa_privileged = privileged;
664 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
666 struct nfs4_slot *slot = res->sr_slot;
667 struct nfs4_slot_table *tbl;
670 spin_lock(&tbl->slot_tbl_lock);
671 if (!nfs41_wake_and_assign_slot(tbl, slot))
672 nfs4_free_slot(tbl, slot);
673 spin_unlock(&tbl->slot_tbl_lock);
678 static int nfs40_sequence_done(struct rpc_task *task,
679 struct nfs4_sequence_res *res)
681 if (res->sr_slot != NULL)
682 nfs40_sequence_free_slot(res);
686 #if defined(CONFIG_NFS_V4_1)
688 static void nfs41_release_slot(struct nfs4_slot *slot)
690 struct nfs4_session *session;
691 struct nfs4_slot_table *tbl;
692 bool send_new_highest_used_slotid = false;
697 session = tbl->session;
699 /* Bump the slot sequence number */
704 spin_lock(&tbl->slot_tbl_lock);
705 /* Be nice to the server: try to ensure that the last transmitted
706 * value for highest_user_slotid <= target_highest_slotid
708 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709 send_new_highest_used_slotid = true;
711 if (nfs41_wake_and_assign_slot(tbl, slot)) {
712 send_new_highest_used_slotid = false;
715 nfs4_free_slot(tbl, slot);
717 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718 send_new_highest_used_slotid = false;
720 spin_unlock(&tbl->slot_tbl_lock);
721 if (send_new_highest_used_slotid)
722 nfs41_notify_server(session->clp);
723 if (waitqueue_active(&tbl->slot_waitq))
724 wake_up_all(&tbl->slot_waitq);
727 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
729 nfs41_release_slot(res->sr_slot);
733 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
736 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
737 slot->seq_nr_highest_sent = seqnr;
739 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
742 slot->seq_nr_highest_sent = seqnr;
743 slot->seq_nr_last_acked = seqnr;
746 static int nfs41_sequence_process(struct rpc_task *task,
747 struct nfs4_sequence_res *res)
749 struct nfs4_session *session;
750 struct nfs4_slot *slot = res->sr_slot;
751 struct nfs_client *clp;
756 /* don't increment the sequence number if the task wasn't sent */
757 if (!RPC_WAS_SENT(task) || slot->seq_done)
760 session = slot->table->session;
762 trace_nfs4_sequence_done(session, res);
763 /* Check the SEQUENCE operation status */
764 switch (res->sr_status) {
766 /* Mark this sequence number as having been acked */
767 nfs4_slot_sequence_acked(slot, slot->seq_nr);
768 /* Update the slot's sequence and clientid lease timer */
771 do_renew_lease(clp, res->sr_timestamp);
772 /* Check sequence flags */
773 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
775 nfs41_update_target_slotid(slot->table, slot, res);
779 * sr_status remains 1 if an RPC level error occurred.
780 * The server may or may not have processed the sequence
783 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
787 /* The server detected a resend of the RPC call and
788 * returned NFS4ERR_DELAY as per Section 2.10.6.2
791 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
795 nfs4_slot_sequence_acked(slot, slot->seq_nr);
797 case -NFS4ERR_RETRY_UNCACHED_REP:
798 case -NFS4ERR_SEQ_FALSE_RETRY:
800 * The server thinks we tried to replay a request.
801 * Retry the call after bumping the sequence ID.
803 nfs4_slot_sequence_acked(slot, slot->seq_nr);
805 case -NFS4ERR_BADSLOT:
807 * The slot id we used was probably retired. Try again
808 * using a different slot id.
810 if (slot->slot_nr < slot->table->target_highest_slotid)
811 goto session_recover;
813 case -NFS4ERR_SEQ_MISORDERED:
814 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
816 * Were one or more calls using this slot interrupted?
817 * If the server never received the request, then our
818 * transmitted slot sequence number may be too high.
820 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
826 * A retry might be sent while the original request is
827 * still in progress on the replier. The replier SHOULD
828 * deal with the issue by returning NFS4ERR_DELAY as the
829 * reply to SEQUENCE or CB_SEQUENCE operation, but
830 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
832 * Restart the search after a delay.
834 slot->seq_nr = slot->seq_nr_highest_sent;
837 /* Just update the slot sequence no. */
841 /* The session may be reset by one of the error handlers. */
842 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
846 nfs4_schedule_session_recovery(session, res->sr_status);
851 if (rpc_restart_call_prepare(task)) {
852 nfs41_sequence_free_slot(res);
858 if (!rpc_restart_call(task))
860 rpc_delay(task, NFS4_POLL_RETRY_MAX);
864 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
866 if (!nfs41_sequence_process(task, res))
868 if (res->sr_slot != NULL)
869 nfs41_sequence_free_slot(res);
873 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
875 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
877 if (res->sr_slot == NULL)
879 if (res->sr_slot->table->session != NULL)
880 return nfs41_sequence_process(task, res);
881 return nfs40_sequence_done(task, res);
884 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
886 if (res->sr_slot != NULL) {
887 if (res->sr_slot->table->session != NULL)
888 nfs41_sequence_free_slot(res);
890 nfs40_sequence_free_slot(res);
894 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
896 if (res->sr_slot == NULL)
898 if (!res->sr_slot->table->session)
899 return nfs40_sequence_done(task, res);
900 return nfs41_sequence_done(task, res);
902 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
904 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
906 struct nfs4_call_sync_data *data = calldata;
908 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
910 nfs4_setup_sequence(data->seq_server->nfs_client,
911 data->seq_args, data->seq_res, task);
914 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
916 struct nfs4_call_sync_data *data = calldata;
918 nfs41_sequence_done(task, data->seq_res);
921 static const struct rpc_call_ops nfs41_call_sync_ops = {
922 .rpc_call_prepare = nfs41_call_sync_prepare,
923 .rpc_call_done = nfs41_call_sync_done,
926 #else /* !CONFIG_NFS_V4_1 */
928 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
930 return nfs40_sequence_done(task, res);
933 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
935 if (res->sr_slot != NULL)
936 nfs40_sequence_free_slot(res);
939 int nfs4_sequence_done(struct rpc_task *task,
940 struct nfs4_sequence_res *res)
942 return nfs40_sequence_done(task, res);
944 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
946 #endif /* !CONFIG_NFS_V4_1 */
948 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
950 res->sr_timestamp = jiffies;
951 res->sr_status_flags = 0;
956 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
957 struct nfs4_sequence_res *res,
958 struct nfs4_slot *slot)
962 slot->privileged = args->sa_privileged ? 1 : 0;
963 args->sa_slot = slot;
968 int nfs4_setup_sequence(struct nfs_client *client,
969 struct nfs4_sequence_args *args,
970 struct nfs4_sequence_res *res,
971 struct rpc_task *task)
973 struct nfs4_session *session = nfs4_get_session(client);
974 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
975 struct nfs4_slot *slot;
977 /* slot already allocated? */
978 if (res->sr_slot != NULL)
982 tbl = &session->fc_slot_table;
983 task->tk_timeout = 0;
986 spin_lock(&tbl->slot_tbl_lock);
987 /* The state manager will wait until the slot table is empty */
988 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
991 slot = nfs4_alloc_slot(tbl);
993 /* Try again in 1/4 second */
994 if (slot == ERR_PTR(-ENOMEM))
995 task->tk_timeout = HZ >> 2;
998 spin_unlock(&tbl->slot_tbl_lock);
1000 nfs4_sequence_attach_slot(args, res, slot);
1002 trace_nfs4_setup_sequence(session, args);
1004 nfs41_sequence_res_init(res);
1005 rpc_call_start(task);
1009 if (args->sa_privileged)
1010 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1011 NULL, RPC_PRIORITY_PRIVILEGED);
1013 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1014 spin_unlock(&tbl->slot_tbl_lock);
1017 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1019 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1021 struct nfs4_call_sync_data *data = calldata;
1022 nfs4_setup_sequence(data->seq_server->nfs_client,
1023 data->seq_args, data->seq_res, task);
1026 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_sequence_done(task, data->seq_res);
1032 static const struct rpc_call_ops nfs40_call_sync_ops = {
1033 .rpc_call_prepare = nfs40_call_sync_prepare,
1034 .rpc_call_done = nfs40_call_sync_done,
1037 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1038 struct nfs_server *server,
1039 struct rpc_message *msg,
1040 struct nfs4_sequence_args *args,
1041 struct nfs4_sequence_res *res)
1044 struct rpc_task *task;
1045 struct nfs_client *clp = server->nfs_client;
1046 struct nfs4_call_sync_data data = {
1047 .seq_server = server,
1051 struct rpc_task_setup task_setup = {
1054 .callback_ops = clp->cl_mvops->call_sync_ops,
1055 .callback_data = &data
1058 task = rpc_run_task(&task_setup);
1060 ret = PTR_ERR(task);
1062 ret = task->tk_status;
1068 int nfs4_call_sync(struct rpc_clnt *clnt,
1069 struct nfs_server *server,
1070 struct rpc_message *msg,
1071 struct nfs4_sequence_args *args,
1072 struct nfs4_sequence_res *res,
1075 nfs4_init_sequence(args, res, cache_reply, 0);
1076 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1080 nfs4_inc_nlink_locked(struct inode *inode)
1082 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1087 nfs4_dec_nlink_locked(struct inode *inode)
1089 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1094 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1095 unsigned long timestamp, unsigned long cache_validity)
1097 struct nfs_inode *nfsi = NFS_I(dir);
1099 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1100 | NFS_INO_INVALID_MTIME
1101 | NFS_INO_INVALID_DATA
1103 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1104 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1105 nfsi->attrtimeo_timestamp = jiffies;
1107 nfs_force_lookup_revalidate(dir);
1108 if (cinfo->before != inode_peek_iversion_raw(dir))
1109 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1110 NFS_INO_INVALID_ACL;
1112 inode_set_iversion_raw(dir, cinfo->after);
1113 nfsi->read_cache_jiffies = timestamp;
1114 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1115 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1116 nfs_fscache_invalidate(dir);
1120 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1121 unsigned long timestamp, unsigned long cache_validity)
1123 spin_lock(&dir->i_lock);
1124 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1125 spin_unlock(&dir->i_lock);
1128 struct nfs4_open_createattrs {
1129 struct nfs4_label *label;
1130 struct iattr *sattr;
1131 const __u32 verf[2];
1134 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1135 int err, struct nfs4_exception *exception)
1139 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1141 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1142 exception->retry = 1;
1147 nfs4_map_atomic_open_share(struct nfs_server *server,
1148 fmode_t fmode, int openflags)
1152 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1154 res = NFS4_SHARE_ACCESS_READ;
1157 res = NFS4_SHARE_ACCESS_WRITE;
1159 case FMODE_READ|FMODE_WRITE:
1160 res = NFS4_SHARE_ACCESS_BOTH;
1162 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1164 /* Want no delegation if we're using O_DIRECT */
1165 if (openflags & O_DIRECT)
1166 res |= NFS4_SHARE_WANT_NO_DELEG;
1171 static enum open_claim_type4
1172 nfs4_map_atomic_open_claim(struct nfs_server *server,
1173 enum open_claim_type4 claim)
1175 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1180 case NFS4_OPEN_CLAIM_FH:
1181 return NFS4_OPEN_CLAIM_NULL;
1182 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1183 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1184 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1185 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1189 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1191 p->o_res.f_attr = &p->f_attr;
1192 p->o_res.f_label = p->f_label;
1193 p->o_res.seqid = p->o_arg.seqid;
1194 p->c_res.seqid = p->c_arg.seqid;
1195 p->o_res.server = p->o_arg.server;
1196 p->o_res.access_request = p->o_arg.access;
1197 nfs_fattr_init(&p->f_attr);
1198 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1201 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1202 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1203 const struct nfs4_open_createattrs *c,
1204 enum open_claim_type4 claim,
1207 struct dentry *parent = dget_parent(dentry);
1208 struct inode *dir = d_inode(parent);
1209 struct nfs_server *server = NFS_SERVER(dir);
1210 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1211 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1212 struct nfs4_opendata *p;
1214 p = kzalloc(sizeof(*p), gfp_mask);
1218 p->f_label = nfs4_label_alloc(server, gfp_mask);
1219 if (IS_ERR(p->f_label))
1222 p->a_label = nfs4_label_alloc(server, gfp_mask);
1223 if (IS_ERR(p->a_label))
1226 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1227 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1228 if (IS_ERR(p->o_arg.seqid))
1229 goto err_free_label;
1230 nfs_sb_active(dentry->d_sb);
1231 p->dentry = dget(dentry);
1234 atomic_inc(&sp->so_count);
1235 p->o_arg.open_flags = flags;
1236 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1237 p->o_arg.umask = current_umask();
1238 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1239 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1241 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1242 * will return permission denied for all bits until close */
1243 if (!(flags & O_EXCL)) {
1244 /* ask server to check for all possible rights as results
1246 switch (p->o_arg.claim) {
1249 case NFS4_OPEN_CLAIM_NULL:
1250 case NFS4_OPEN_CLAIM_FH:
1251 p->o_arg.access = NFS4_ACCESS_READ |
1252 NFS4_ACCESS_MODIFY |
1253 NFS4_ACCESS_EXTEND |
1254 NFS4_ACCESS_EXECUTE;
1257 p->o_arg.clientid = server->nfs_client->cl_clientid;
1258 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1259 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1260 p->o_arg.name = &dentry->d_name;
1261 p->o_arg.server = server;
1262 p->o_arg.bitmask = nfs4_bitmask(server, label);
1263 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1264 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1265 switch (p->o_arg.claim) {
1266 case NFS4_OPEN_CLAIM_NULL:
1267 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1268 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1269 p->o_arg.fh = NFS_FH(dir);
1271 case NFS4_OPEN_CLAIM_PREVIOUS:
1272 case NFS4_OPEN_CLAIM_FH:
1273 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1274 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1275 p->o_arg.fh = NFS_FH(d_inode(dentry));
1277 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
1278 p->o_arg.u.attrs = &p->attrs;
1279 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1281 memcpy(p->o_arg.u.verifier.data, c->verf,
1282 sizeof(p->o_arg.u.verifier.data));
1284 p->c_arg.fh = &p->o_res.fh;
1285 p->c_arg.stateid = &p->o_res.stateid;
1286 p->c_arg.seqid = p->o_arg.seqid;
1287 nfs4_init_opendata_res(p);
1288 kref_init(&p->kref);
1292 nfs4_label_free(p->a_label);
1294 nfs4_label_free(p->f_label);
1302 static void nfs4_opendata_free(struct kref *kref)
1304 struct nfs4_opendata *p = container_of(kref,
1305 struct nfs4_opendata, kref);
1306 struct super_block *sb = p->dentry->d_sb;
1308 nfs4_lgopen_release(p->lgp);
1309 nfs_free_seqid(p->o_arg.seqid);
1310 nfs4_sequence_free_slot(&p->o_res.seq_res);
1311 if (p->state != NULL)
1312 nfs4_put_open_state(p->state);
1313 nfs4_put_state_owner(p->owner);
1315 nfs4_label_free(p->a_label);
1316 nfs4_label_free(p->f_label);
1320 nfs_sb_deactive(sb);
1321 nfs_fattr_free_names(&p->f_attr);
1322 kfree(p->f_attr.mdsthreshold);
1326 static void nfs4_opendata_put(struct nfs4_opendata *p)
1329 kref_put(&p->kref, nfs4_opendata_free);
1332 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1335 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1336 case FMODE_READ|FMODE_WRITE:
1337 return state->n_rdwr != 0;
1339 return state->n_wronly != 0;
1341 return state->n_rdonly != 0;
1347 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1348 int open_mode, enum open_claim_type4 claim)
1352 if (open_mode & (O_EXCL|O_TRUNC))
1355 case NFS4_OPEN_CLAIM_NULL:
1356 case NFS4_OPEN_CLAIM_FH:
1361 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1363 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1364 && state->n_rdonly != 0;
1367 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1368 && state->n_wronly != 0;
1370 case FMODE_READ|FMODE_WRITE:
1371 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1372 && state->n_rdwr != 0;
1378 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1379 enum open_claim_type4 claim)
1381 if (delegation == NULL)
1383 if ((delegation->type & fmode) != fmode)
1385 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1388 case NFS4_OPEN_CLAIM_NULL:
1389 case NFS4_OPEN_CLAIM_FH:
1391 case NFS4_OPEN_CLAIM_PREVIOUS:
1392 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1398 nfs_mark_delegation_referenced(delegation);
1402 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1411 case FMODE_READ|FMODE_WRITE:
1414 nfs4_state_set_mode_locked(state, state->state | fmode);
1417 #ifdef CONFIG_NFS_V4_1
1418 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1420 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1422 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1424 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1428 #endif /* CONFIG_NFS_V4_1 */
1430 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1432 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1433 wake_up_all(&state->waitq);
1436 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1437 const nfs4_stateid *stateid)
1439 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1440 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1442 if (stateid_seqid == state_seqid + 1U ||
1443 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1444 nfs_state_log_update_open_stateid(state);
1446 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1449 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1451 struct nfs_client *clp = state->owner->so_server->nfs_client;
1452 bool need_recover = false;
1454 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1455 need_recover = true;
1456 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1457 need_recover = true;
1458 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1459 need_recover = true;
1461 nfs4_state_mark_reclaim_nograce(clp, state);
1465 * Check for whether or not the caller may update the open stateid
1466 * to the value passed in by stateid.
1468 * Note: This function relies heavily on the server implementing
1469 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1471 * i.e. The stateid seqids have to be initialised to 1, and
1472 * are then incremented on every state transition.
1474 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1475 const nfs4_stateid *stateid)
1477 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1478 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1479 if (stateid->seqid == cpu_to_be32(1))
1480 nfs_state_log_update_open_stateid(state);
1482 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1486 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1487 nfs_state_log_out_of_order_open_stateid(state, stateid);
1493 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1495 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1497 if (state->n_wronly)
1498 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1499 if (state->n_rdonly)
1500 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1502 set_bit(NFS_O_RDWR_STATE, &state->flags);
1503 set_bit(NFS_OPEN_STATE, &state->flags);
1506 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1507 nfs4_stateid *stateid, fmode_t fmode)
1509 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1510 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1512 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1515 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1518 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1519 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1520 clear_bit(NFS_OPEN_STATE, &state->flags);
1522 if (stateid == NULL)
1524 /* Handle OPEN+OPEN_DOWNGRADE races */
1525 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1526 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1527 nfs_resync_open_stateid_locked(state);
1530 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1531 nfs4_stateid_copy(&state->stateid, stateid);
1532 nfs4_stateid_copy(&state->open_stateid, stateid);
1533 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1535 nfs_state_log_update_open_stateid(state);
1538 static void nfs_clear_open_stateid(struct nfs4_state *state,
1539 nfs4_stateid *arg_stateid,
1540 nfs4_stateid *stateid, fmode_t fmode)
1542 write_seqlock(&state->seqlock);
1543 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1544 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1545 nfs_clear_open_stateid_locked(state, stateid, fmode);
1546 write_sequnlock(&state->seqlock);
1547 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1548 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1551 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1552 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1553 __must_hold(&state->owner->so_lock)
1554 __must_hold(&state->seqlock)
1562 if (!nfs_need_update_open_stateid(state, stateid))
1564 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1568 /* Rely on seqids for serialisation with NFSv4.0 */
1569 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1572 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1574 * Ensure we process the state changes in the same order
1575 * in which the server processed them by delaying the
1576 * update of the stateid until we are in sequence.
1578 write_sequnlock(&state->seqlock);
1579 spin_unlock(&state->owner->so_lock);
1581 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1582 if (!signal_pending(current)) {
1583 if (schedule_timeout(5*HZ) == 0)
1589 finish_wait(&state->waitq, &wait);
1591 spin_lock(&state->owner->so_lock);
1592 write_seqlock(&state->seqlock);
1595 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1596 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1597 nfs4_stateid_copy(freeme, &state->open_stateid);
1598 nfs_test_and_clear_all_open_stateid(state);
1601 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1602 nfs4_stateid_copy(&state->stateid, stateid);
1603 nfs4_stateid_copy(&state->open_stateid, stateid);
1604 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1605 nfs_state_log_update_open_stateid(state);
1608 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1609 const nfs4_stateid *open_stateid,
1611 nfs4_stateid *freeme)
1614 * Protect the call to nfs4_state_set_mode_locked and
1615 * serialise the stateid update
1617 write_seqlock(&state->seqlock);
1618 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1621 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1624 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1626 case FMODE_READ|FMODE_WRITE:
1627 set_bit(NFS_O_RDWR_STATE, &state->flags);
1629 set_bit(NFS_OPEN_STATE, &state->flags);
1630 write_sequnlock(&state->seqlock);
1633 static void nfs_state_set_delegation(struct nfs4_state *state,
1634 const nfs4_stateid *deleg_stateid,
1638 * Protect the call to nfs4_state_set_mode_locked and
1639 * serialise the stateid update
1641 write_seqlock(&state->seqlock);
1642 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1643 set_bit(NFS_DELEGATED_STATE, &state->flags);
1644 write_sequnlock(&state->seqlock);
1647 static void nfs_state_clear_delegation(struct nfs4_state *state)
1649 write_seqlock(&state->seqlock);
1650 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1651 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1652 write_sequnlock(&state->seqlock);
1655 static int update_open_stateid(struct nfs4_state *state,
1656 const nfs4_stateid *open_stateid,
1657 const nfs4_stateid *delegation,
1660 struct nfs_server *server = NFS_SERVER(state->inode);
1661 struct nfs_client *clp = server->nfs_client;
1662 struct nfs_inode *nfsi = NFS_I(state->inode);
1663 struct nfs_delegation *deleg_cur;
1664 nfs4_stateid freeme = { };
1667 fmode &= (FMODE_READ|FMODE_WRITE);
1670 spin_lock(&state->owner->so_lock);
1671 if (open_stateid != NULL) {
1672 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1676 deleg_cur = rcu_dereference(nfsi->delegation);
1677 if (deleg_cur == NULL)
1680 spin_lock(&deleg_cur->lock);
1681 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1682 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1683 (deleg_cur->type & fmode) != fmode)
1684 goto no_delegation_unlock;
1686 if (delegation == NULL)
1687 delegation = &deleg_cur->stateid;
1688 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1689 goto no_delegation_unlock;
1691 nfs_mark_delegation_referenced(deleg_cur);
1692 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1694 no_delegation_unlock:
1695 spin_unlock(&deleg_cur->lock);
1698 update_open_stateflags(state, fmode);
1699 spin_unlock(&state->owner->so_lock);
1702 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1703 nfs4_schedule_state_manager(clp);
1704 if (freeme.type != 0)
1705 nfs4_test_and_free_stateid(server, &freeme,
1706 state->owner->so_cred);
1711 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1712 const nfs4_stateid *stateid)
1714 struct nfs4_state *state = lsp->ls_state;
1717 spin_lock(&state->state_lock);
1718 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1720 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1722 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1725 spin_unlock(&state->state_lock);
1729 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1731 struct nfs_delegation *delegation;
1733 fmode &= FMODE_READ|FMODE_WRITE;
1735 delegation = rcu_dereference(NFS_I(inode)->delegation);
1736 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1741 nfs4_inode_return_delegation(inode);
1744 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1746 struct nfs4_state *state = opendata->state;
1747 struct nfs_inode *nfsi = NFS_I(state->inode);
1748 struct nfs_delegation *delegation;
1749 int open_mode = opendata->o_arg.open_flags;
1750 fmode_t fmode = opendata->o_arg.fmode;
1751 enum open_claim_type4 claim = opendata->o_arg.claim;
1752 nfs4_stateid stateid;
1756 spin_lock(&state->owner->so_lock);
1757 if (can_open_cached(state, fmode, open_mode, claim)) {
1758 update_open_stateflags(state, fmode);
1759 spin_unlock(&state->owner->so_lock);
1760 goto out_return_state;
1762 spin_unlock(&state->owner->so_lock);
1764 delegation = rcu_dereference(nfsi->delegation);
1765 if (!can_open_delegated(delegation, fmode, claim)) {
1769 /* Save the delegation */
1770 nfs4_stateid_copy(&stateid, &delegation->stateid);
1772 nfs_release_seqid(opendata->o_arg.seqid);
1773 if (!opendata->is_recover) {
1774 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1780 /* Try to update the stateid using the delegation */
1781 if (update_open_stateid(state, NULL, &stateid, fmode))
1782 goto out_return_state;
1785 return ERR_PTR(ret);
1787 refcount_inc(&state->count);
1792 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1794 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1795 struct nfs_delegation *delegation;
1796 int delegation_flags = 0;
1799 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1801 delegation_flags = delegation->flags;
1803 switch (data->o_arg.claim) {
1806 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1807 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1808 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1809 "returning a delegation for "
1810 "OPEN(CLAIM_DELEGATE_CUR)\n",
1814 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1815 nfs_inode_set_delegation(state->inode,
1816 data->owner->so_cred,
1817 data->o_res.delegation_type,
1818 &data->o_res.delegation,
1819 data->o_res.pagemod_limit);
1821 nfs_inode_reclaim_delegation(state->inode,
1822 data->owner->so_cred,
1823 data->o_res.delegation_type,
1824 &data->o_res.delegation,
1825 data->o_res.pagemod_limit);
1827 if (data->o_res.do_recall)
1828 nfs_async_inode_return_delegation(state->inode,
1829 &data->o_res.delegation);
1833 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1834 * and update the nfs4_state.
1836 static struct nfs4_state *
1837 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1839 struct inode *inode = data->state->inode;
1840 struct nfs4_state *state = data->state;
1843 if (!data->rpc_done) {
1844 if (data->rpc_status)
1845 return ERR_PTR(data->rpc_status);
1846 /* cached opens have already been processed */
1850 ret = nfs_refresh_inode(inode, &data->f_attr);
1852 return ERR_PTR(ret);
1854 if (data->o_res.delegation_type != 0)
1855 nfs4_opendata_check_deleg(data, state);
1857 update_open_stateid(state, &data->o_res.stateid, NULL,
1859 refcount_inc(&state->count);
1864 static struct inode *
1865 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1867 struct inode *inode;
1869 switch (data->o_arg.claim) {
1870 case NFS4_OPEN_CLAIM_NULL:
1871 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1872 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1873 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1874 return ERR_PTR(-EAGAIN);
1875 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1876 &data->f_attr, data->f_label);
1879 inode = d_inode(data->dentry);
1881 nfs_refresh_inode(inode, &data->f_attr);
1886 static struct nfs4_state *
1887 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1889 struct nfs4_state *state;
1890 struct inode *inode;
1892 inode = nfs4_opendata_get_inode(data);
1894 return ERR_CAST(inode);
1895 if (data->state != NULL && data->state->inode == inode) {
1896 state = data->state;
1897 refcount_inc(&state->count);
1899 state = nfs4_get_open_state(inode, data->owner);
1902 state = ERR_PTR(-ENOMEM);
1906 static struct nfs4_state *
1907 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1909 struct nfs4_state *state;
1911 if (!data->rpc_done) {
1912 state = nfs4_try_open_cached(data);
1913 trace_nfs4_cached_open(data->state);
1917 state = nfs4_opendata_find_nfs4_state(data);
1921 if (data->o_res.delegation_type != 0)
1922 nfs4_opendata_check_deleg(data, state);
1923 update_open_stateid(state, &data->o_res.stateid, NULL,
1926 nfs_release_seqid(data->o_arg.seqid);
1930 static struct nfs4_state *
1931 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1933 struct nfs4_state *ret;
1935 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1936 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1938 ret = _nfs4_opendata_to_nfs4_state(data);
1939 nfs4_sequence_free_slot(&data->o_res.seq_res);
1943 static struct nfs_open_context *
1944 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
1946 struct nfs_inode *nfsi = NFS_I(state->inode);
1947 struct nfs_open_context *ctx;
1950 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1951 if (ctx->state != state)
1953 if ((ctx->mode & mode) != mode)
1955 if (!get_nfs_open_context(ctx))
1961 return ERR_PTR(-ENOENT);
1964 static struct nfs_open_context *
1965 nfs4_state_find_open_context(struct nfs4_state *state)
1967 struct nfs_open_context *ctx;
1969 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
1972 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
1975 return nfs4_state_find_open_context_mode(state, FMODE_READ);
1978 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1979 struct nfs4_state *state, enum open_claim_type4 claim)
1981 struct nfs4_opendata *opendata;
1983 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1984 NULL, claim, GFP_NOFS);
1985 if (opendata == NULL)
1986 return ERR_PTR(-ENOMEM);
1987 opendata->state = state;
1988 refcount_inc(&state->count);
1992 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1995 struct nfs4_state *newstate;
1998 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2000 opendata->o_arg.open_flags = 0;
2001 opendata->o_arg.fmode = fmode;
2002 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2003 NFS_SB(opendata->dentry->d_sb),
2005 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2006 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2007 nfs4_init_opendata_res(opendata);
2008 ret = _nfs4_recover_proc_open(opendata);
2011 newstate = nfs4_opendata_to_nfs4_state(opendata);
2012 if (IS_ERR(newstate))
2013 return PTR_ERR(newstate);
2014 if (newstate != opendata->state)
2016 nfs4_close_state(newstate, fmode);
2020 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2024 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2025 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2026 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2027 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2028 /* memory barrier prior to reading state->n_* */
2029 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2030 clear_bit(NFS_OPEN_STATE, &state->flags);
2032 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2035 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2038 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2042 * We may have performed cached opens for all three recoveries.
2043 * Check if we need to update the current stateid.
2045 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2046 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2047 write_seqlock(&state->seqlock);
2048 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2049 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2050 write_sequnlock(&state->seqlock);
2057 * reclaim state on the server after a reboot.
2059 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2061 struct nfs_delegation *delegation;
2062 struct nfs4_opendata *opendata;
2063 fmode_t delegation_type = 0;
2066 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2067 NFS4_OPEN_CLAIM_PREVIOUS);
2068 if (IS_ERR(opendata))
2069 return PTR_ERR(opendata);
2071 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2072 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2073 delegation_type = delegation->type;
2075 opendata->o_arg.u.delegation_type = delegation_type;
2076 status = nfs4_open_recover(opendata, state);
2077 nfs4_opendata_put(opendata);
2081 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2083 struct nfs_server *server = NFS_SERVER(state->inode);
2084 struct nfs4_exception exception = { };
2087 err = _nfs4_do_open_reclaim(ctx, state);
2088 trace_nfs4_open_reclaim(ctx, 0, err);
2089 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2091 if (err != -NFS4ERR_DELAY)
2093 nfs4_handle_exception(server, err, &exception);
2094 } while (exception.retry);
2098 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2100 struct nfs_open_context *ctx;
2103 ctx = nfs4_state_find_open_context(state);
2106 ret = nfs4_do_open_reclaim(ctx, state);
2107 put_nfs_open_context(ctx);
2111 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)
2115 printk(KERN_ERR "NFS: %s: unhandled error "
2116 "%d.\n", __func__, err);
2122 case -NFS4ERR_BADSESSION:
2123 case -NFS4ERR_BADSLOT:
2124 case -NFS4ERR_BAD_HIGH_SLOT:
2125 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2126 case -NFS4ERR_DEADSESSION:
2127 set_bit(NFS_DELEGATED_STATE, &state->flags);
2128 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2130 case -NFS4ERR_STALE_CLIENTID:
2131 case -NFS4ERR_STALE_STATEID:
2132 set_bit(NFS_DELEGATED_STATE, &state->flags);
2133 /* Don't recall a delegation if it was lost */
2134 nfs4_schedule_lease_recovery(server->nfs_client);
2136 case -NFS4ERR_MOVED:
2137 nfs4_schedule_migration_recovery(server);
2139 case -NFS4ERR_LEASE_MOVED:
2140 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2142 case -NFS4ERR_DELEG_REVOKED:
2143 case -NFS4ERR_ADMIN_REVOKED:
2144 case -NFS4ERR_EXPIRED:
2145 case -NFS4ERR_BAD_STATEID:
2146 case -NFS4ERR_OPENMODE:
2147 nfs_inode_find_state_and_recover(state->inode,
2149 nfs4_schedule_stateid_recovery(server, state);
2151 case -NFS4ERR_DELAY:
2152 case -NFS4ERR_GRACE:
2153 set_bit(NFS_DELEGATED_STATE, &state->flags);
2157 case -NFS4ERR_DENIED:
2159 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2161 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2168 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2169 struct nfs4_state *state, const nfs4_stateid *stateid,
2172 struct nfs_server *server = NFS_SERVER(state->inode);
2173 struct nfs4_opendata *opendata;
2176 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2177 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2178 if (IS_ERR(opendata))
2179 return PTR_ERR(opendata);
2180 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2181 nfs_state_clear_delegation(state);
2182 switch (type & (FMODE_READ|FMODE_WRITE)) {
2183 case FMODE_READ|FMODE_WRITE:
2185 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2188 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2193 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2195 nfs4_opendata_put(opendata);
2196 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2199 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2201 struct nfs4_opendata *data = calldata;
2203 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2204 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2207 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2209 struct nfs4_opendata *data = calldata;
2211 nfs40_sequence_done(task, &data->c_res.seq_res);
2213 data->rpc_status = task->tk_status;
2214 if (data->rpc_status == 0) {
2215 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2216 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2217 renew_lease(data->o_res.server, data->timestamp);
2218 data->rpc_done = true;
2222 static void nfs4_open_confirm_release(void *calldata)
2224 struct nfs4_opendata *data = calldata;
2225 struct nfs4_state *state = NULL;
2227 /* If this request hasn't been cancelled, do nothing */
2228 if (!data->cancelled)
2230 /* In case of error, no cleanup! */
2231 if (!data->rpc_done)
2233 state = nfs4_opendata_to_nfs4_state(data);
2235 nfs4_close_state(state, data->o_arg.fmode);
2237 nfs4_opendata_put(data);
2240 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2241 .rpc_call_prepare = nfs4_open_confirm_prepare,
2242 .rpc_call_done = nfs4_open_confirm_done,
2243 .rpc_release = nfs4_open_confirm_release,
2247 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2249 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2251 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2252 struct rpc_task *task;
2253 struct rpc_message msg = {
2254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2255 .rpc_argp = &data->c_arg,
2256 .rpc_resp = &data->c_res,
2257 .rpc_cred = data->owner->so_cred,
2259 struct rpc_task_setup task_setup_data = {
2260 .rpc_client = server->client,
2261 .rpc_message = &msg,
2262 .callback_ops = &nfs4_open_confirm_ops,
2263 .callback_data = data,
2264 .workqueue = nfsiod_workqueue,
2265 .flags = RPC_TASK_ASYNC,
2269 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2271 kref_get(&data->kref);
2272 data->rpc_done = false;
2273 data->rpc_status = 0;
2274 data->timestamp = jiffies;
2275 task = rpc_run_task(&task_setup_data);
2277 return PTR_ERR(task);
2278 status = rpc_wait_for_completion_task(task);
2280 data->cancelled = true;
2283 status = data->rpc_status;
2288 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2290 struct nfs4_opendata *data = calldata;
2291 struct nfs4_state_owner *sp = data->owner;
2292 struct nfs_client *clp = sp->so_server->nfs_client;
2293 enum open_claim_type4 claim = data->o_arg.claim;
2295 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2298 * Check if we still need to send an OPEN call, or if we can use
2299 * a delegation instead.
2301 if (data->state != NULL) {
2302 struct nfs_delegation *delegation;
2304 if (can_open_cached(data->state, data->o_arg.fmode,
2305 data->o_arg.open_flags, claim))
2308 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2309 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2310 goto unlock_no_action;
2313 /* Update client id. */
2314 data->o_arg.clientid = clp->cl_clientid;
2318 case NFS4_OPEN_CLAIM_PREVIOUS:
2319 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2320 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2321 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2323 case NFS4_OPEN_CLAIM_FH:
2324 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2326 data->timestamp = jiffies;
2327 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2328 &data->o_arg.seq_args,
2329 &data->o_res.seq_res,
2331 nfs_release_seqid(data->o_arg.seqid);
2333 /* Set the create mode (note dependency on the session type) */
2334 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2335 if (data->o_arg.open_flags & O_EXCL) {
2336 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2337 if (nfs4_has_persistent_session(clp))
2338 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2339 else if (clp->cl_mvops->minor_version > 0)
2340 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2344 trace_nfs4_cached_open(data->state);
2347 task->tk_action = NULL;
2349 nfs4_sequence_done(task, &data->o_res.seq_res);
2352 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2354 struct nfs4_opendata *data = calldata;
2356 data->rpc_status = task->tk_status;
2358 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2361 if (task->tk_status == 0) {
2362 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2363 switch (data->o_res.f_attr->mode & S_IFMT) {
2367 data->rpc_status = -ELOOP;
2370 data->rpc_status = -EISDIR;
2373 data->rpc_status = -ENOTDIR;
2376 renew_lease(data->o_res.server, data->timestamp);
2377 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2378 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2380 data->rpc_done = true;
2383 static void nfs4_open_release(void *calldata)
2385 struct nfs4_opendata *data = calldata;
2386 struct nfs4_state *state = NULL;
2388 /* If this request hasn't been cancelled, do nothing */
2389 if (!data->cancelled)
2391 /* In case of error, no cleanup! */
2392 if (data->rpc_status != 0 || !data->rpc_done)
2394 /* In case we need an open_confirm, no cleanup! */
2395 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2397 state = nfs4_opendata_to_nfs4_state(data);
2399 nfs4_close_state(state, data->o_arg.fmode);
2401 nfs4_opendata_put(data);
2404 static const struct rpc_call_ops nfs4_open_ops = {
2405 .rpc_call_prepare = nfs4_open_prepare,
2406 .rpc_call_done = nfs4_open_done,
2407 .rpc_release = nfs4_open_release,
2410 static int nfs4_run_open_task(struct nfs4_opendata *data,
2411 struct nfs_open_context *ctx)
2413 struct inode *dir = d_inode(data->dir);
2414 struct nfs_server *server = NFS_SERVER(dir);
2415 struct nfs_openargs *o_arg = &data->o_arg;
2416 struct nfs_openres *o_res = &data->o_res;
2417 struct rpc_task *task;
2418 struct rpc_message msg = {
2419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2422 .rpc_cred = data->owner->so_cred,
2424 struct rpc_task_setup task_setup_data = {
2425 .rpc_client = server->client,
2426 .rpc_message = &msg,
2427 .callback_ops = &nfs4_open_ops,
2428 .callback_data = data,
2429 .workqueue = nfsiod_workqueue,
2430 .flags = RPC_TASK_ASYNC,
2434 kref_get(&data->kref);
2435 data->rpc_done = false;
2436 data->rpc_status = 0;
2437 data->cancelled = false;
2438 data->is_recover = false;
2440 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2441 data->is_recover = true;
2443 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2444 pnfs_lgopen_prepare(data, ctx);
2446 task = rpc_run_task(&task_setup_data);
2448 return PTR_ERR(task);
2449 status = rpc_wait_for_completion_task(task);
2451 data->cancelled = true;
2454 status = data->rpc_status;
2460 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2462 struct inode *dir = d_inode(data->dir);
2463 struct nfs_openres *o_res = &data->o_res;
2466 status = nfs4_run_open_task(data, NULL);
2467 if (status != 0 || !data->rpc_done)
2470 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2472 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2473 status = _nfs4_proc_open_confirm(data);
2479 * Additional permission checks in order to distinguish between an
2480 * open for read, and an open for execute. This works around the
2481 * fact that NFSv4 OPEN treats read and execute permissions as being
2483 * Note that in the non-execute case, we want to turn off permission
2484 * checking if we just created a new file (POSIX open() semantics).
2486 static int nfs4_opendata_access(const struct cred *cred,
2487 struct nfs4_opendata *opendata,
2488 struct nfs4_state *state, fmode_t fmode,
2491 struct nfs_access_entry cache;
2494 /* access call failed or for some reason the server doesn't
2495 * support any access modes -- defer access call until later */
2496 if (opendata->o_res.access_supported == 0)
2501 * Use openflags to check for exec, because fmode won't
2502 * always have FMODE_EXEC set when file open for exec.
2504 if (openflags & __FMODE_EXEC) {
2505 /* ONLY check for exec rights */
2506 if (S_ISDIR(state->inode->i_mode))
2507 mask = NFS4_ACCESS_LOOKUP;
2509 mask = NFS4_ACCESS_EXECUTE;
2510 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2511 mask = NFS4_ACCESS_READ;
2514 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2515 nfs_access_add_cache(state->inode, &cache);
2517 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2518 if ((mask & ~cache.mask & flags) == 0)
2525 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2527 static int _nfs4_proc_open(struct nfs4_opendata *data,
2528 struct nfs_open_context *ctx)
2530 struct inode *dir = d_inode(data->dir);
2531 struct nfs_server *server = NFS_SERVER(dir);
2532 struct nfs_openargs *o_arg = &data->o_arg;
2533 struct nfs_openres *o_res = &data->o_res;
2536 status = nfs4_run_open_task(data, ctx);
2537 if (!data->rpc_done)
2540 if (status == -NFS4ERR_BADNAME &&
2541 !(o_arg->open_flags & O_CREAT))
2546 nfs_fattr_map_and_free_names(server, &data->f_attr);
2548 if (o_arg->open_flags & O_CREAT) {
2549 if (o_arg->open_flags & O_EXCL)
2550 data->file_created = true;
2551 else if (o_res->cinfo.before != o_res->cinfo.after)
2552 data->file_created = true;
2553 if (data->file_created ||
2554 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2555 update_changeattr(dir, &o_res->cinfo,
2556 o_res->f_attr->time_start, 0);
2558 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2559 server->caps &= ~NFS_CAP_POSIX_LOCK;
2560 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2561 status = _nfs4_proc_open_confirm(data);
2565 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2566 nfs4_sequence_free_slot(&o_res->seq_res);
2567 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2568 o_res->f_label, NULL);
2575 * reclaim state on the server after a network partition.
2576 * Assumes caller holds the appropriate lock
2578 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2580 struct nfs4_opendata *opendata;
2583 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2584 NFS4_OPEN_CLAIM_FH);
2585 if (IS_ERR(opendata))
2586 return PTR_ERR(opendata);
2587 ret = nfs4_open_recover(opendata, state);
2589 d_drop(ctx->dentry);
2590 nfs4_opendata_put(opendata);
2594 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2596 struct nfs_server *server = NFS_SERVER(state->inode);
2597 struct nfs4_exception exception = { };
2601 err = _nfs4_open_expired(ctx, state);
2602 trace_nfs4_open_expired(ctx, 0, err);
2603 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2608 case -NFS4ERR_GRACE:
2609 case -NFS4ERR_DELAY:
2610 nfs4_handle_exception(server, err, &exception);
2613 } while (exception.retry);
2618 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2620 struct nfs_open_context *ctx;
2623 ctx = nfs4_state_find_open_context(state);
2626 ret = nfs4_do_open_expired(ctx, state);
2627 put_nfs_open_context(ctx);
2631 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2632 const nfs4_stateid *stateid)
2634 nfs_remove_bad_delegation(state->inode, stateid);
2635 nfs_state_clear_delegation(state);
2638 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2640 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2641 nfs_finish_clear_delegation_stateid(state, NULL);
2644 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2646 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2647 nfs40_clear_delegation_stateid(state);
2648 return nfs4_open_expired(sp, state);
2651 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2652 nfs4_stateid *stateid,
2653 const struct cred *cred)
2655 return -NFS4ERR_BAD_STATEID;
2658 #if defined(CONFIG_NFS_V4_1)
2659 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2660 nfs4_stateid *stateid,
2661 const struct cred *cred)
2665 switch (stateid->type) {
2668 case NFS4_INVALID_STATEID_TYPE:
2669 case NFS4_SPECIAL_STATEID_TYPE:
2670 return -NFS4ERR_BAD_STATEID;
2671 case NFS4_REVOKED_STATEID_TYPE:
2675 status = nfs41_test_stateid(server, stateid, cred);
2677 case -NFS4ERR_EXPIRED:
2678 case -NFS4ERR_ADMIN_REVOKED:
2679 case -NFS4ERR_DELEG_REVOKED:
2685 /* Ack the revoked state to the server */
2686 nfs41_free_stateid(server, stateid, cred, true);
2687 return -NFS4ERR_EXPIRED;
2690 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2692 struct nfs_server *server = NFS_SERVER(state->inode);
2693 nfs4_stateid stateid;
2694 struct nfs_delegation *delegation;
2695 const struct cred *cred = NULL;
2698 /* Get the delegation credential for use by test/free_stateid */
2700 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2701 if (delegation == NULL) {
2703 nfs_state_clear_delegation(state);
2707 nfs4_stateid_copy(&stateid, &delegation->stateid);
2708 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2710 nfs_state_clear_delegation(state);
2714 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2715 &delegation->flags)) {
2720 if (delegation->cred)
2721 cred = get_cred(delegation->cred);
2723 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2724 trace_nfs4_test_delegation_stateid(state, NULL, status);
2725 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2726 nfs_finish_clear_delegation_stateid(state, &stateid);
2728 if (delegation->cred)
2733 * nfs41_check_expired_locks - possibly free a lock stateid
2735 * @state: NFSv4 state for an inode
2737 * Returns NFS_OK if recovery for this stateid is now finished.
2738 * Otherwise a negative NFS4ERR value is returned.
2740 static int nfs41_check_expired_locks(struct nfs4_state *state)
2742 int status, ret = NFS_OK;
2743 struct nfs4_lock_state *lsp, *prev = NULL;
2744 struct nfs_server *server = NFS_SERVER(state->inode);
2746 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2749 spin_lock(&state->state_lock);
2750 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2751 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2752 const struct cred *cred = lsp->ls_state->owner->so_cred;
2754 refcount_inc(&lsp->ls_count);
2755 spin_unlock(&state->state_lock);
2757 nfs4_put_lock_state(prev);
2760 status = nfs41_test_and_free_expired_stateid(server,
2763 trace_nfs4_test_lock_stateid(state, lsp, status);
2764 if (status == -NFS4ERR_EXPIRED ||
2765 status == -NFS4ERR_BAD_STATEID) {
2766 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2767 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2768 if (!recover_lost_locks)
2769 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2770 } else if (status != NFS_OK) {
2772 nfs4_put_lock_state(prev);
2775 spin_lock(&state->state_lock);
2778 spin_unlock(&state->state_lock);
2779 nfs4_put_lock_state(prev);
2785 * nfs41_check_open_stateid - possibly free an open stateid
2787 * @state: NFSv4 state for an inode
2789 * Returns NFS_OK if recovery for this stateid is now finished.
2790 * Otherwise a negative NFS4ERR value is returned.
2792 static int nfs41_check_open_stateid(struct nfs4_state *state)
2794 struct nfs_server *server = NFS_SERVER(state->inode);
2795 nfs4_stateid *stateid = &state->open_stateid;
2796 const struct cred *cred = state->owner->so_cred;
2799 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2801 if (nfs4_have_delegation(state->inode, state->state))
2803 return -NFS4ERR_OPENMODE;
2805 return -NFS4ERR_BAD_STATEID;
2807 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2808 trace_nfs4_test_open_stateid(state, NULL, status);
2809 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2810 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2811 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2812 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2813 clear_bit(NFS_OPEN_STATE, &state->flags);
2814 stateid->type = NFS4_INVALID_STATEID_TYPE;
2817 if (nfs_open_stateid_recover_openmode(state))
2818 return -NFS4ERR_OPENMODE;
2822 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2826 nfs41_check_delegation_stateid(state);
2827 status = nfs41_check_expired_locks(state);
2828 if (status != NFS_OK)
2830 status = nfs41_check_open_stateid(state);
2831 if (status != NFS_OK)
2832 status = nfs4_open_expired(sp, state);
2838 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2839 * fields corresponding to attributes that were used to store the verifier.
2840 * Make sure we clobber those fields in the later setattr call
2842 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2843 struct iattr *sattr, struct nfs4_label **label)
2845 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2850 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2851 attrset[i] = opendata->o_res.attrset[i];
2852 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2853 attrset[i] &= ~bitmask[i];
2856 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2857 sattr->ia_valid : 0;
2859 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2860 if (sattr->ia_valid & ATTR_ATIME_SET)
2861 ret |= ATTR_ATIME_SET;
2866 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2867 if (sattr->ia_valid & ATTR_MTIME_SET)
2868 ret |= ATTR_MTIME_SET;
2873 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2878 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2881 struct nfs_open_context *ctx)
2883 struct nfs4_state_owner *sp = opendata->owner;
2884 struct nfs_server *server = sp->so_server;
2885 struct dentry *dentry;
2886 struct nfs4_state *state;
2890 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2892 ret = _nfs4_proc_open(opendata, ctx);
2896 state = _nfs4_opendata_to_nfs4_state(opendata);
2897 ret = PTR_ERR(state);
2901 if (server->caps & NFS_CAP_POSIX_LOCK)
2902 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2903 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2904 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2906 dentry = opendata->dentry;
2907 if (d_really_is_negative(dentry)) {
2908 struct dentry *alias;
2910 alias = d_exact_alias(dentry, state->inode);
2912 alias = d_splice_alias(igrab(state->inode), dentry);
2913 /* d_splice_alias() can't fail here - it's a non-directory */
2916 ctx->dentry = dentry = alias;
2918 nfs_set_verifier(dentry,
2919 nfs_save_change_attribute(d_inode(opendata->dir)));
2922 /* Parse layoutget results before we check for access */
2923 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2925 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2929 if (d_inode(dentry) == state->inode) {
2930 nfs_inode_attach_open_context(ctx);
2931 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2932 nfs4_schedule_stateid_recovery(server, state);
2936 if (!opendata->cancelled)
2937 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2942 * Returns a referenced nfs4_state
2944 static int _nfs4_do_open(struct inode *dir,
2945 struct nfs_open_context *ctx,
2947 const struct nfs4_open_createattrs *c,
2950 struct nfs4_state_owner *sp;
2951 struct nfs4_state *state = NULL;
2952 struct nfs_server *server = NFS_SERVER(dir);
2953 struct nfs4_opendata *opendata;
2954 struct dentry *dentry = ctx->dentry;
2955 const struct cred *cred = ctx->cred;
2956 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2957 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2958 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2959 struct iattr *sattr = c->sattr;
2960 struct nfs4_label *label = c->label;
2961 struct nfs4_label *olabel = NULL;
2964 /* Protect against reboot recovery conflicts */
2966 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2968 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2971 status = nfs4_client_recover_expired_lease(server->nfs_client);
2973 goto err_put_state_owner;
2974 if (d_really_is_positive(dentry))
2975 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2977 if (d_really_is_positive(dentry))
2978 claim = NFS4_OPEN_CLAIM_FH;
2979 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2980 c, claim, GFP_KERNEL);
2981 if (opendata == NULL)
2982 goto err_put_state_owner;
2985 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2986 if (IS_ERR(olabel)) {
2987 status = PTR_ERR(olabel);
2988 goto err_opendata_put;
2992 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2993 if (!opendata->f_attr.mdsthreshold) {
2994 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2995 if (!opendata->f_attr.mdsthreshold)
2996 goto err_free_label;
2998 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3000 if (d_really_is_positive(dentry))
3001 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3003 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
3005 goto err_free_label;
3008 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3009 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3010 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3012 * send create attributes which was not set by open
3013 * with an extra setattr.
3015 if (attrs || label) {
3016 unsigned ia_old = sattr->ia_valid;
3018 sattr->ia_valid = attrs;
3019 nfs_fattr_init(opendata->o_res.f_attr);
3020 status = nfs4_do_setattr(state->inode, cred,
3021 opendata->o_res.f_attr, sattr,
3022 ctx, label, olabel);
3024 nfs_setattr_update_inode(state->inode, sattr,
3025 opendata->o_res.f_attr);
3026 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3028 sattr->ia_valid = ia_old;
3031 if (opened && opendata->file_created)
3034 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3035 *ctx_th = opendata->f_attr.mdsthreshold;
3036 opendata->f_attr.mdsthreshold = NULL;
3039 nfs4_label_free(olabel);
3041 nfs4_opendata_put(opendata);
3042 nfs4_put_state_owner(sp);
3045 nfs4_label_free(olabel);
3047 nfs4_opendata_put(opendata);
3048 err_put_state_owner:
3049 nfs4_put_state_owner(sp);
3055 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3056 struct nfs_open_context *ctx,
3058 struct iattr *sattr,
3059 struct nfs4_label *label,
3062 struct nfs_server *server = NFS_SERVER(dir);
3063 struct nfs4_exception exception = { };
3064 struct nfs4_state *res;
3065 struct nfs4_open_createattrs c = {
3069 [0] = (__u32)jiffies,
3070 [1] = (__u32)current->pid,
3076 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3078 trace_nfs4_open_file(ctx, flags, status);
3081 /* NOTE: BAD_SEQID means the server and client disagree about the
3082 * book-keeping w.r.t. state-changing operations
3083 * (OPEN/CLOSE/LOCK/LOCKU...)
3084 * It is actually a sign of a bug on the client or on the server.
3086 * If we receive a BAD_SEQID error in the particular case of
3087 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3088 * have unhashed the old state_owner for us, and that we can
3089 * therefore safely retry using a new one. We should still warn
3090 * the user though...
3092 if (status == -NFS4ERR_BAD_SEQID) {
3093 pr_warn_ratelimited("NFS: v4 server %s "
3094 " returned a bad sequence-id error!\n",
3095 NFS_SERVER(dir)->nfs_client->cl_hostname);
3096 exception.retry = 1;
3100 * BAD_STATEID on OPEN means that the server cancelled our
3101 * state before it received the OPEN_CONFIRM.
3102 * Recover by retrying the request as per the discussion
3103 * on Page 181 of RFC3530.
3105 if (status == -NFS4ERR_BAD_STATEID) {
3106 exception.retry = 1;
3109 if (status == -EAGAIN) {
3110 /* We must have found a delegation */
3111 exception.retry = 1;
3114 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3116 res = ERR_PTR(nfs4_handle_exception(server,
3117 status, &exception));
3118 } while (exception.retry);
3122 static int _nfs4_do_setattr(struct inode *inode,
3123 struct nfs_setattrargs *arg,
3124 struct nfs_setattrres *res,
3125 const struct cred *cred,
3126 struct nfs_open_context *ctx)
3128 struct nfs_server *server = NFS_SERVER(inode);
3129 struct rpc_message msg = {
3130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3135 const struct cred *delegation_cred = NULL;
3136 unsigned long timestamp = jiffies;
3140 nfs_fattr_init(res->fattr);
3142 /* Servers should only apply open mode checks for file size changes */
3143 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3147 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3148 /* Use that stateid */
3149 } else if (ctx != NULL) {
3150 struct nfs_lock_context *l_ctx;
3151 if (!nfs4_valid_open_stateid(ctx->state))
3153 l_ctx = nfs_get_lock_context(ctx);
3155 return PTR_ERR(l_ctx);
3156 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3157 &arg->stateid, &delegation_cred);
3158 nfs_put_lock_context(l_ctx);
3163 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3165 if (delegation_cred)
3166 msg.rpc_cred = delegation_cred;
3168 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3170 put_cred(delegation_cred);
3171 if (status == 0 && ctx != NULL)
3172 renew_lease(server, timestamp);
3173 trace_nfs4_setattr(inode, &arg->stateid, status);
3177 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3178 struct nfs_fattr *fattr, struct iattr *sattr,
3179 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3180 struct nfs4_label *olabel)
3182 struct nfs_server *server = NFS_SERVER(inode);
3183 __u32 bitmask[NFS4_BITMASK_SZ];
3184 struct nfs4_state *state = ctx ? ctx->state : NULL;
3185 struct nfs_setattrargs arg = {
3186 .fh = NFS_FH(inode),
3192 struct nfs_setattrres res = {
3197 struct nfs4_exception exception = {
3200 .stateid = &arg.stateid,
3205 nfs4_bitmap_copy_adjust_setattr(bitmask,
3206 nfs4_bitmask(server, olabel),
3209 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3211 case -NFS4ERR_OPENMODE:
3212 if (!(sattr->ia_valid & ATTR_SIZE)) {
3213 pr_warn_once("NFSv4: server %s is incorrectly "
3214 "applying open mode checks to "
3215 "a SETATTR that is not "
3216 "changing file size.\n",
3217 server->nfs_client->cl_hostname);
3219 if (state && !(state->state & FMODE_WRITE)) {
3221 if (sattr->ia_valid & ATTR_OPEN)
3226 err = nfs4_handle_exception(server, err, &exception);
3227 } while (exception.retry);
3233 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3235 if (inode == NULL || !nfs_have_layout(inode))
3238 return pnfs_wait_on_layoutreturn(inode, task);
3241 struct nfs4_closedata {
3242 struct inode *inode;
3243 struct nfs4_state *state;
3244 struct nfs_closeargs arg;
3245 struct nfs_closeres res;
3247 struct nfs4_layoutreturn_args arg;
3248 struct nfs4_layoutreturn_res res;
3249 struct nfs4_xdr_opaque_data ld_private;
3253 struct nfs_fattr fattr;
3254 unsigned long timestamp;
3257 static void nfs4_free_closedata(void *data)
3259 struct nfs4_closedata *calldata = data;
3260 struct nfs4_state_owner *sp = calldata->state->owner;
3261 struct super_block *sb = calldata->state->inode->i_sb;
3263 if (calldata->lr.roc)
3264 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3265 calldata->res.lr_ret);
3266 nfs4_put_open_state(calldata->state);
3267 nfs_free_seqid(calldata->arg.seqid);
3268 nfs4_put_state_owner(sp);
3269 nfs_sb_deactive(sb);
3273 static void nfs4_close_done(struct rpc_task *task, void *data)
3275 struct nfs4_closedata *calldata = data;
3276 struct nfs4_state *state = calldata->state;
3277 struct nfs_server *server = NFS_SERVER(calldata->inode);
3278 nfs4_stateid *res_stateid = NULL;
3279 struct nfs4_exception exception = {
3281 .inode = calldata->inode,
3282 .stateid = &calldata->arg.stateid,
3285 dprintk("%s: begin!\n", __func__);
3286 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3288 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3290 /* Handle Layoutreturn errors */
3291 if (calldata->arg.lr_args && task->tk_status != 0) {
3292 switch (calldata->res.lr_ret) {
3294 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3297 calldata->arg.lr_args = NULL;
3298 calldata->res.lr_res = NULL;
3300 case -NFS4ERR_OLD_STATEID:
3301 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3302 &calldata->arg.lr_args->range,
3306 case -NFS4ERR_ADMIN_REVOKED:
3307 case -NFS4ERR_DELEG_REVOKED:
3308 case -NFS4ERR_EXPIRED:
3309 case -NFS4ERR_BAD_STATEID:
3310 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3311 case -NFS4ERR_WRONG_CRED:
3312 calldata->arg.lr_args = NULL;
3313 calldata->res.lr_res = NULL;
3318 /* hmm. we are done with the inode, and in the process of freeing
3319 * the state_owner. we keep this around to process errors
3321 switch (task->tk_status) {
3323 res_stateid = &calldata->res.stateid;
3324 renew_lease(server, calldata->timestamp);
3326 case -NFS4ERR_ACCESS:
3327 if (calldata->arg.bitmask != NULL) {
3328 calldata->arg.bitmask = NULL;
3329 calldata->res.fattr = NULL;
3334 case -NFS4ERR_OLD_STATEID:
3335 /* Did we race with OPEN? */
3336 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3340 case -NFS4ERR_ADMIN_REVOKED:
3341 case -NFS4ERR_STALE_STATEID:
3342 case -NFS4ERR_EXPIRED:
3343 nfs4_free_revoked_stateid(server,
3344 &calldata->arg.stateid,
3345 task->tk_msg.rpc_cred);
3347 case -NFS4ERR_BAD_STATEID:
3350 task->tk_status = nfs4_async_handle_exception(task,
3351 server, task->tk_status, &exception);
3352 if (exception.retry)
3355 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3356 res_stateid, calldata->arg.fmode);
3358 task->tk_status = 0;
3359 nfs_release_seqid(calldata->arg.seqid);
3360 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3361 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3364 calldata->res.lr_ret = 0;
3366 task->tk_status = 0;
3367 rpc_restart_call_prepare(task);
3371 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3373 struct nfs4_closedata *calldata = data;
3374 struct nfs4_state *state = calldata->state;
3375 struct inode *inode = calldata->inode;
3376 struct pnfs_layout_hdr *lo;
3377 bool is_rdonly, is_wronly, is_rdwr;
3380 dprintk("%s: begin!\n", __func__);
3381 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3384 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3385 spin_lock(&state->owner->so_lock);
3386 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3387 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3388 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3389 /* Calculate the change in open mode */
3390 calldata->arg.fmode = 0;
3391 if (state->n_rdwr == 0) {
3392 if (state->n_rdonly == 0)
3393 call_close |= is_rdonly;
3395 calldata->arg.fmode |= FMODE_READ;
3396 if (state->n_wronly == 0)
3397 call_close |= is_wronly;
3399 calldata->arg.fmode |= FMODE_WRITE;
3400 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3401 call_close |= is_rdwr;
3403 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3405 if (!nfs4_valid_open_stateid(state) ||
3406 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3408 spin_unlock(&state->owner->so_lock);
3411 /* Note: exit _without_ calling nfs4_close_done */
3415 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3416 nfs_release_seqid(calldata->arg.seqid);
3420 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3421 if (lo && !pnfs_layout_is_valid(lo)) {
3422 calldata->arg.lr_args = NULL;
3423 calldata->res.lr_res = NULL;
3426 if (calldata->arg.fmode == 0)
3427 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3429 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3430 /* Close-to-open cache consistency revalidation */
3431 if (!nfs4_have_delegation(inode, FMODE_READ))
3432 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3434 calldata->arg.bitmask = NULL;
3437 calldata->arg.share_access =
3438 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3439 calldata->arg.fmode, 0);
3441 if (calldata->res.fattr == NULL)
3442 calldata->arg.bitmask = NULL;
3443 else if (calldata->arg.bitmask == NULL)
3444 calldata->res.fattr = NULL;
3445 calldata->timestamp = jiffies;
3446 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3447 &calldata->arg.seq_args,
3448 &calldata->res.seq_res,
3450 nfs_release_seqid(calldata->arg.seqid);
3451 dprintk("%s: done!\n", __func__);
3454 task->tk_action = NULL;
3456 nfs4_sequence_done(task, &calldata->res.seq_res);
3459 static const struct rpc_call_ops nfs4_close_ops = {
3460 .rpc_call_prepare = nfs4_close_prepare,
3461 .rpc_call_done = nfs4_close_done,
3462 .rpc_release = nfs4_free_closedata,
3466 * It is possible for data to be read/written from a mem-mapped file
3467 * after the sys_close call (which hits the vfs layer as a flush).
3468 * This means that we can't safely call nfsv4 close on a file until
3469 * the inode is cleared. This in turn means that we are not good
3470 * NFSv4 citizens - we do not indicate to the server to update the file's
3471 * share state even when we are done with one of the three share
3472 * stateid's in the inode.
3474 * NOTE: Caller must be holding the sp->so_owner semaphore!
3476 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3478 struct nfs_server *server = NFS_SERVER(state->inode);
3479 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3480 struct nfs4_closedata *calldata;
3481 struct nfs4_state_owner *sp = state->owner;
3482 struct rpc_task *task;
3483 struct rpc_message msg = {
3484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3485 .rpc_cred = state->owner->so_cred,
3487 struct rpc_task_setup task_setup_data = {
3488 .rpc_client = server->client,
3489 .rpc_message = &msg,
3490 .callback_ops = &nfs4_close_ops,
3491 .workqueue = nfsiod_workqueue,
3492 .flags = RPC_TASK_ASYNC,
3494 int status = -ENOMEM;
3496 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3497 &task_setup_data.rpc_client, &msg);
3499 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3500 if (calldata == NULL)
3502 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3503 calldata->inode = state->inode;
3504 calldata->state = state;
3505 calldata->arg.fh = NFS_FH(state->inode);
3506 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3507 goto out_free_calldata;
3508 /* Serialization for the sequence id */
3509 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3510 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3511 if (IS_ERR(calldata->arg.seqid))
3512 goto out_free_calldata;
3513 nfs_fattr_init(&calldata->fattr);
3514 calldata->arg.fmode = 0;
3515 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3516 calldata->res.fattr = &calldata->fattr;
3517 calldata->res.seqid = calldata->arg.seqid;
3518 calldata->res.server = server;
3519 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3520 calldata->lr.roc = pnfs_roc(state->inode,
3521 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3522 if (calldata->lr.roc) {
3523 calldata->arg.lr_args = &calldata->lr.arg;
3524 calldata->res.lr_res = &calldata->lr.res;
3526 nfs_sb_active(calldata->inode->i_sb);
3528 msg.rpc_argp = &calldata->arg;
3529 msg.rpc_resp = &calldata->res;
3530 task_setup_data.callback_data = calldata;
3531 task = rpc_run_task(&task_setup_data);
3533 return PTR_ERR(task);
3536 status = rpc_wait_for_completion_task(task);
3542 nfs4_put_open_state(state);
3543 nfs4_put_state_owner(sp);
3547 static struct inode *
3548 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3549 int open_flags, struct iattr *attr, int *opened)
3551 struct nfs4_state *state;
3552 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3554 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3556 /* Protect against concurrent sillydeletes */
3557 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3559 nfs4_label_release_security(label);
3562 return ERR_CAST(state);
3563 return state->inode;
3566 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3568 if (ctx->state == NULL)
3571 nfs4_close_sync(ctx->state, ctx->mode);
3573 nfs4_close_state(ctx->state, ctx->mode);
3576 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3577 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3578 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3580 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3582 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3583 struct nfs4_server_caps_arg args = {
3587 struct nfs4_server_caps_res res = {};
3588 struct rpc_message msg = {
3589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3596 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3597 FATTR4_WORD0_FH_EXPIRE_TYPE |
3598 FATTR4_WORD0_LINK_SUPPORT |
3599 FATTR4_WORD0_SYMLINK_SUPPORT |
3600 FATTR4_WORD0_ACLSUPPORT;
3602 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3604 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3606 /* Sanity check the server answers */
3607 switch (minorversion) {
3609 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3610 res.attr_bitmask[2] = 0;
3613 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3616 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3618 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3619 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3620 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3621 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3622 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3623 NFS_CAP_CTIME|NFS_CAP_MTIME|
3624 NFS_CAP_SECURITY_LABEL);
3625 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3626 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3627 server->caps |= NFS_CAP_ACLS;
3628 if (res.has_links != 0)
3629 server->caps |= NFS_CAP_HARDLINKS;
3630 if (res.has_symlinks != 0)
3631 server->caps |= NFS_CAP_SYMLINKS;
3632 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3633 server->caps |= NFS_CAP_FILEID;
3634 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3635 server->caps |= NFS_CAP_MODE;
3636 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3637 server->caps |= NFS_CAP_NLINK;
3638 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3639 server->caps |= NFS_CAP_OWNER;
3640 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3641 server->caps |= NFS_CAP_OWNER_GROUP;
3642 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3643 server->caps |= NFS_CAP_ATIME;
3644 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3645 server->caps |= NFS_CAP_CTIME;
3646 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3647 server->caps |= NFS_CAP_MTIME;
3648 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3649 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3650 server->caps |= NFS_CAP_SECURITY_LABEL;
3652 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3653 sizeof(server->attr_bitmask));
3654 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3656 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3657 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3658 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3659 server->cache_consistency_bitmask[2] = 0;
3661 /* Avoid a regression due to buggy server */
3662 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3663 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3664 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3665 sizeof(server->exclcreat_bitmask));
3667 server->acl_bitmask = res.acl_bitmask;
3668 server->fh_expire_type = res.fh_expire_type;
3674 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3676 struct nfs4_exception exception = { };
3679 err = nfs4_handle_exception(server,
3680 _nfs4_server_capabilities(server, fhandle),
3682 } while (exception.retry);
3686 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3687 struct nfs_fsinfo *info)
3690 struct nfs4_lookup_root_arg args = {
3693 struct nfs4_lookup_res res = {
3695 .fattr = info->fattr,
3698 struct rpc_message msg = {
3699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3704 bitmask[0] = nfs4_fattr_bitmap[0];
3705 bitmask[1] = nfs4_fattr_bitmap[1];
3707 * Process the label in the upcoming getfattr
3709 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3711 nfs_fattr_init(info->fattr);
3712 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3715 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3716 struct nfs_fsinfo *info)
3718 struct nfs4_exception exception = { };
3721 err = _nfs4_lookup_root(server, fhandle, info);
3722 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3725 case -NFS4ERR_WRONGSEC:
3728 err = nfs4_handle_exception(server, err, &exception);
3730 } while (exception.retry);
3735 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3736 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3738 struct rpc_auth_create_args auth_args = {
3739 .pseudoflavor = flavor,
3741 struct rpc_auth *auth;
3743 auth = rpcauth_create(&auth_args, server->client);
3746 return nfs4_lookup_root(server, fhandle, info);
3750 * Retry pseudoroot lookup with various security flavors. We do this when:
3752 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3753 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3755 * Returns zero on success, or a negative NFS4ERR value, or a
3756 * negative errno value.
3758 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3759 struct nfs_fsinfo *info)
3761 /* Per 3530bis 15.33.5 */
3762 static const rpc_authflavor_t flav_array[] = {
3766 RPC_AUTH_UNIX, /* courtesy */
3769 int status = -EPERM;
3772 if (server->auth_info.flavor_len > 0) {
3773 /* try each flavor specified by user */
3774 for (i = 0; i < server->auth_info.flavor_len; i++) {
3775 status = nfs4_lookup_root_sec(server, fhandle, info,
3776 server->auth_info.flavors[i]);
3777 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3782 /* no flavors specified by user, try default list */
3783 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3784 status = nfs4_lookup_root_sec(server, fhandle, info,
3786 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3793 * -EACCES could mean that the user doesn't have correct permissions
3794 * to access the mount. It could also mean that we tried to mount
3795 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3796 * existing mount programs don't handle -EACCES very well so it should
3797 * be mapped to -EPERM instead.
3799 if (status == -EACCES)
3805 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3806 * @server: initialized nfs_server handle
3807 * @fhandle: we fill in the pseudo-fs root file handle
3808 * @info: we fill in an FSINFO struct
3809 * @auth_probe: probe the auth flavours
3811 * Returns zero on success, or a negative errno.
3813 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3814 struct nfs_fsinfo *info,
3820 status = nfs4_lookup_root(server, fhandle, info);
3822 if (auth_probe || status == NFS4ERR_WRONGSEC)
3823 status = server->nfs_client->cl_mvops->find_root_sec(server,
3827 status = nfs4_server_capabilities(server, fhandle);
3829 status = nfs4_do_fsinfo(server, fhandle, info);
3831 return nfs4_map_errors(status);
3834 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3835 struct nfs_fsinfo *info)
3838 struct nfs_fattr *fattr = info->fattr;
3839 struct nfs4_label *label = NULL;
3841 error = nfs4_server_capabilities(server, mntfh);
3843 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3847 label = nfs4_label_alloc(server, GFP_KERNEL);
3849 return PTR_ERR(label);
3851 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3853 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3854 goto err_free_label;
3857 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3858 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3859 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3862 nfs4_label_free(label);
3868 * Get locations and (maybe) other attributes of a referral.
3869 * Note that we'll actually follow the referral later when
3870 * we detect fsid mismatch in inode revalidation
3872 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3873 const struct qstr *name, struct nfs_fattr *fattr,
3874 struct nfs_fh *fhandle)
3876 int status = -ENOMEM;
3877 struct page *page = NULL;
3878 struct nfs4_fs_locations *locations = NULL;
3880 page = alloc_page(GFP_KERNEL);
3883 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3884 if (locations == NULL)
3887 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3892 * If the fsid didn't change, this is a migration event, not a
3893 * referral. Cause us to drop into the exception handler, which
3894 * will kick off migration recovery.
3896 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3897 dprintk("%s: server did not return a different fsid for"
3898 " a referral at %s\n", __func__, name->name);
3899 status = -NFS4ERR_MOVED;
3902 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3903 nfs_fixup_referral_attributes(&locations->fattr);
3905 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3906 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3907 memset(fhandle, 0, sizeof(struct nfs_fh));
3915 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3916 struct nfs_fattr *fattr, struct nfs4_label *label,
3917 struct inode *inode)
3919 __u32 bitmask[NFS4_BITMASK_SZ];
3920 struct nfs4_getattr_arg args = {
3924 struct nfs4_getattr_res res = {
3929 struct rpc_message msg = {
3930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3935 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
3937 nfs_fattr_init(fattr);
3938 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3941 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3942 struct nfs_fattr *fattr, struct nfs4_label *label,
3943 struct inode *inode)
3945 struct nfs4_exception exception = { };
3948 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
3949 trace_nfs4_getattr(server, fhandle, fattr, err);
3950 err = nfs4_handle_exception(server, err,
3952 } while (exception.retry);
3957 * The file is not closed if it is opened due to the a request to change
3958 * the size of the file. The open call will not be needed once the
3959 * VFS layer lookup-intents are implemented.
3961 * Close is called when the inode is destroyed.
3962 * If we haven't opened the file for O_WRONLY, we
3963 * need to in the size_change case to obtain a stateid.
3966 * Because OPEN is always done by name in nfsv4, it is
3967 * possible that we opened a different file by the same
3968 * name. We can recognize this race condition, but we
3969 * can't do anything about it besides returning an error.
3971 * This will be fixed with VFS changes (lookup-intent).
3974 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3975 struct iattr *sattr)
3977 struct inode *inode = d_inode(dentry);
3978 const struct cred *cred = NULL;
3979 struct nfs_open_context *ctx = NULL;
3980 struct nfs4_label *label = NULL;
3983 if (pnfs_ld_layoutret_on_setattr(inode) &&
3984 sattr->ia_valid & ATTR_SIZE &&
3985 sattr->ia_size < i_size_read(inode))
3986 pnfs_commit_and_return_layout(inode);
3988 nfs_fattr_init(fattr);
3990 /* Deal with open(O_TRUNC) */
3991 if (sattr->ia_valid & ATTR_OPEN)
3992 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3994 /* Optimization: if the end result is no change, don't RPC */
3995 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3998 /* Search for an existing open(O_WRITE) file */
3999 if (sattr->ia_valid & ATTR_FILE) {
4001 ctx = nfs_file_open_context(sattr->ia_file);
4006 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4008 return PTR_ERR(label);
4010 /* Return any delegations if we're going to change ACLs */
4011 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4012 nfs4_inode_make_writeable(inode);
4014 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4016 nfs_setattr_update_inode(inode, sattr, fattr);
4017 nfs_setsecurity(inode, fattr, label);
4019 nfs4_label_free(label);
4023 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4024 const struct qstr *name, struct nfs_fh *fhandle,
4025 struct nfs_fattr *fattr, struct nfs4_label *label)
4027 struct nfs_server *server = NFS_SERVER(dir);
4029 struct nfs4_lookup_arg args = {
4030 .bitmask = server->attr_bitmask,
4031 .dir_fh = NFS_FH(dir),
4034 struct nfs4_lookup_res res = {
4040 struct rpc_message msg = {
4041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4046 args.bitmask = nfs4_bitmask(server, label);
4048 nfs_fattr_init(fattr);
4050 dprintk("NFS call lookup %s\n", name->name);
4051 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4052 dprintk("NFS reply lookup: %d\n", status);
4056 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4058 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4059 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4060 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4064 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4065 const struct qstr *name, struct nfs_fh *fhandle,
4066 struct nfs_fattr *fattr, struct nfs4_label *label)
4068 struct nfs4_exception exception = { };
4069 struct rpc_clnt *client = *clnt;
4072 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4073 trace_nfs4_lookup(dir, name, err);
4075 case -NFS4ERR_BADNAME:
4078 case -NFS4ERR_MOVED:
4079 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4080 if (err == -NFS4ERR_MOVED)
4081 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4083 case -NFS4ERR_WRONGSEC:
4085 if (client != *clnt)
4087 client = nfs4_negotiate_security(client, dir, name);
4089 return PTR_ERR(client);
4091 exception.retry = 1;
4094 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4096 } while (exception.retry);
4101 else if (client != *clnt)
4102 rpc_shutdown_client(client);
4107 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4108 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4109 struct nfs4_label *label)
4112 struct rpc_clnt *client = NFS_CLIENT(dir);
4114 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4115 if (client != NFS_CLIENT(dir)) {
4116 rpc_shutdown_client(client);
4117 nfs_fixup_secinfo_attributes(fattr);
4123 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4124 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4126 struct rpc_clnt *client = NFS_CLIENT(dir);
4129 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4131 return ERR_PTR(status);
4132 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4135 static int _nfs4_proc_lookupp(struct inode *inode,
4136 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4137 struct nfs4_label *label)
4139 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4140 struct nfs_server *server = NFS_SERVER(inode);
4142 struct nfs4_lookupp_arg args = {
4143 .bitmask = server->attr_bitmask,
4144 .fh = NFS_FH(inode),
4146 struct nfs4_lookupp_res res = {
4152 struct rpc_message msg = {
4153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4158 args.bitmask = nfs4_bitmask(server, label);
4160 nfs_fattr_init(fattr);
4162 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4163 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4165 dprintk("NFS reply lookupp: %d\n", status);
4169 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4170 struct nfs_fattr *fattr, struct nfs4_label *label)
4172 struct nfs4_exception exception = { };
4175 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4176 trace_nfs4_lookupp(inode, err);
4177 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4179 } while (exception.retry);
4183 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4185 struct nfs_server *server = NFS_SERVER(inode);
4186 struct nfs4_accessargs args = {
4187 .fh = NFS_FH(inode),
4188 .access = entry->mask,
4190 struct nfs4_accessres res = {
4193 struct rpc_message msg = {
4194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4197 .rpc_cred = entry->cred,
4201 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4202 res.fattr = nfs_alloc_fattr();
4203 if (res.fattr == NULL)
4205 args.bitmask = server->cache_consistency_bitmask;
4207 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4209 nfs_access_set_mask(entry, res.access);
4211 nfs_refresh_inode(inode, res.fattr);
4213 nfs_free_fattr(res.fattr);
4217 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4219 struct nfs4_exception exception = { };
4222 err = _nfs4_proc_access(inode, entry);
4223 trace_nfs4_access(inode, err);
4224 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4226 } while (exception.retry);
4231 * TODO: For the time being, we don't try to get any attributes
4232 * along with any of the zero-copy operations READ, READDIR,
4235 * In the case of the first three, we want to put the GETATTR
4236 * after the read-type operation -- this is because it is hard
4237 * to predict the length of a GETATTR response in v4, and thus
4238 * align the READ data correctly. This means that the GETATTR
4239 * may end up partially falling into the page cache, and we should
4240 * shift it into the 'tail' of the xdr_buf before processing.
4241 * To do this efficiently, we need to know the total length
4242 * of data received, which doesn't seem to be available outside
4245 * In the case of WRITE, we also want to put the GETATTR after
4246 * the operation -- in this case because we want to make sure
4247 * we get the post-operation mtime and size.
4249 * Both of these changes to the XDR layer would in fact be quite
4250 * minor, but I decided to leave them for a subsequent patch.
4252 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4253 unsigned int pgbase, unsigned int pglen)
4255 struct nfs4_readlink args = {
4256 .fh = NFS_FH(inode),
4261 struct nfs4_readlink_res res;
4262 struct rpc_message msg = {
4263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4268 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4271 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4272 unsigned int pgbase, unsigned int pglen)
4274 struct nfs4_exception exception = { };
4277 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4278 trace_nfs4_readlink(inode, err);
4279 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4281 } while (exception.retry);
4286 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4289 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4292 struct nfs_server *server = NFS_SERVER(dir);
4293 struct nfs4_label l, *ilabel = NULL;
4294 struct nfs_open_context *ctx;
4295 struct nfs4_state *state;
4298 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4300 return PTR_ERR(ctx);
4302 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4304 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4305 sattr->ia_mode &= ~current_umask();
4306 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4307 if (IS_ERR(state)) {
4308 status = PTR_ERR(state);
4312 nfs4_label_release_security(ilabel);
4313 put_nfs_open_context(ctx);
4318 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4320 struct nfs_server *server = NFS_SERVER(dir);
4321 struct nfs_removeargs args = {
4325 struct nfs_removeres res = {
4328 struct rpc_message msg = {
4329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4333 unsigned long timestamp = jiffies;
4336 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4338 spin_lock(&dir->i_lock);
4339 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4340 /* Removing a directory decrements nlink in the parent */
4341 if (ftype == NF4DIR && dir->i_nlink > 2)
4342 nfs4_dec_nlink_locked(dir);
4343 spin_unlock(&dir->i_lock);
4348 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4350 struct nfs4_exception exception = { };
4351 struct inode *inode = d_inode(dentry);
4355 if (inode->i_nlink == 1)
4356 nfs4_inode_return_delegation(inode);
4358 nfs4_inode_make_writeable(inode);
4361 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4362 trace_nfs4_remove(dir, &dentry->d_name, err);
4363 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4365 } while (exception.retry);
4369 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4371 struct nfs4_exception exception = { };
4375 err = _nfs4_proc_remove(dir, name, NF4DIR);
4376 trace_nfs4_remove(dir, name, err);
4377 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4379 } while (exception.retry);
4383 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4384 struct dentry *dentry,
4385 struct inode *inode)
4387 struct nfs_removeargs *args = msg->rpc_argp;
4388 struct nfs_removeres *res = msg->rpc_resp;
4390 res->server = NFS_SB(dentry->d_sb);
4391 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4392 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4394 nfs_fattr_init(res->dir_attr);
4397 nfs4_inode_return_delegation(inode);
4400 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4402 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4403 &data->args.seq_args,
4408 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4410 struct nfs_unlinkdata *data = task->tk_calldata;
4411 struct nfs_removeres *res = &data->res;
4413 if (!nfs4_sequence_done(task, &res->seq_res))
4415 if (nfs4_async_handle_error(task, res->server, NULL,
4416 &data->timeout) == -EAGAIN)
4418 if (task->tk_status == 0)
4419 update_changeattr(dir, &res->cinfo,
4420 res->dir_attr->time_start, 0);
4424 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4425 struct dentry *old_dentry,
4426 struct dentry *new_dentry)
4428 struct nfs_renameargs *arg = msg->rpc_argp;
4429 struct nfs_renameres *res = msg->rpc_resp;
4430 struct inode *old_inode = d_inode(old_dentry);
4431 struct inode *new_inode = d_inode(new_dentry);
4434 nfs4_inode_make_writeable(old_inode);
4436 nfs4_inode_return_delegation(new_inode);
4437 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4438 res->server = NFS_SB(old_dentry->d_sb);
4439 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4442 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4444 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4445 &data->args.seq_args,
4450 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4451 struct inode *new_dir)
4453 struct nfs_renamedata *data = task->tk_calldata;
4454 struct nfs_renameres *res = &data->res;
4456 if (!nfs4_sequence_done(task, &res->seq_res))
4458 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4461 if (task->tk_status == 0) {
4462 if (new_dir != old_dir) {
4463 /* Note: If we moved a directory, nlink will change */
4464 update_changeattr(old_dir, &res->old_cinfo,
4465 res->old_fattr->time_start,
4466 NFS_INO_INVALID_OTHER);
4467 update_changeattr(new_dir, &res->new_cinfo,
4468 res->new_fattr->time_start,
4469 NFS_INO_INVALID_OTHER);
4471 update_changeattr(old_dir, &res->old_cinfo,
4472 res->old_fattr->time_start,
4478 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4480 struct nfs_server *server = NFS_SERVER(inode);
4481 __u32 bitmask[NFS4_BITMASK_SZ];
4482 struct nfs4_link_arg arg = {
4483 .fh = NFS_FH(inode),
4484 .dir_fh = NFS_FH(dir),
4488 struct nfs4_link_res res = {
4492 struct rpc_message msg = {
4493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4497 int status = -ENOMEM;
4499 res.fattr = nfs_alloc_fattr();
4500 if (res.fattr == NULL)
4503 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4504 if (IS_ERR(res.label)) {
4505 status = PTR_ERR(res.label);
4509 nfs4_inode_make_writeable(inode);
4510 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4512 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4514 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4515 status = nfs_post_op_update_inode(inode, res.fattr);
4517 nfs_setsecurity(inode, res.fattr, res.label);
4521 nfs4_label_free(res.label);
4524 nfs_free_fattr(res.fattr);
4528 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4530 struct nfs4_exception exception = { };
4533 err = nfs4_handle_exception(NFS_SERVER(inode),
4534 _nfs4_proc_link(inode, dir, name),
4536 } while (exception.retry);
4540 struct nfs4_createdata {
4541 struct rpc_message msg;
4542 struct nfs4_create_arg arg;
4543 struct nfs4_create_res res;
4545 struct nfs_fattr fattr;
4546 struct nfs4_label *label;
4549 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4550 const struct qstr *name, struct iattr *sattr, u32 ftype)
4552 struct nfs4_createdata *data;
4554 data = kzalloc(sizeof(*data), GFP_KERNEL);
4556 struct nfs_server *server = NFS_SERVER(dir);
4558 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4559 if (IS_ERR(data->label))
4562 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4563 data->msg.rpc_argp = &data->arg;
4564 data->msg.rpc_resp = &data->res;
4565 data->arg.dir_fh = NFS_FH(dir);
4566 data->arg.server = server;
4567 data->arg.name = name;
4568 data->arg.attrs = sattr;
4569 data->arg.ftype = ftype;
4570 data->arg.bitmask = nfs4_bitmask(server, data->label);
4571 data->arg.umask = current_umask();
4572 data->res.server = server;
4573 data->res.fh = &data->fh;
4574 data->res.fattr = &data->fattr;
4575 data->res.label = data->label;
4576 nfs_fattr_init(data->res.fattr);
4584 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4586 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4587 &data->arg.seq_args, &data->res.seq_res, 1);
4589 spin_lock(&dir->i_lock);
4590 update_changeattr_locked(dir, &data->res.dir_cinfo,
4591 data->res.fattr->time_start, 0);
4592 /* Creating a directory bumps nlink in the parent */
4593 if (data->arg.ftype == NF4DIR)
4594 nfs4_inc_nlink_locked(dir);
4595 spin_unlock(&dir->i_lock);
4596 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4601 static void nfs4_free_createdata(struct nfs4_createdata *data)
4603 nfs4_label_free(data->label);
4607 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4608 struct page *page, unsigned int len, struct iattr *sattr,
4609 struct nfs4_label *label)
4611 struct nfs4_createdata *data;
4612 int status = -ENAMETOOLONG;
4614 if (len > NFS4_MAXPATHLEN)
4618 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4622 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4623 data->arg.u.symlink.pages = &page;
4624 data->arg.u.symlink.len = len;
4625 data->arg.label = label;
4627 status = nfs4_do_create(dir, dentry, data);
4629 nfs4_free_createdata(data);
4634 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4635 struct page *page, unsigned int len, struct iattr *sattr)
4637 struct nfs4_exception exception = { };
4638 struct nfs4_label l, *label = NULL;
4641 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4644 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4645 trace_nfs4_symlink(dir, &dentry->d_name, err);
4646 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4648 } while (exception.retry);
4650 nfs4_label_release_security(label);
4654 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4655 struct iattr *sattr, struct nfs4_label *label)
4657 struct nfs4_createdata *data;
4658 int status = -ENOMEM;
4660 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4664 data->arg.label = label;
4665 status = nfs4_do_create(dir, dentry, data);
4667 nfs4_free_createdata(data);
4672 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4673 struct iattr *sattr)
4675 struct nfs_server *server = NFS_SERVER(dir);
4676 struct nfs4_exception exception = { };
4677 struct nfs4_label l, *label = NULL;
4680 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4682 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4683 sattr->ia_mode &= ~current_umask();
4685 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4686 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4687 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4689 } while (exception.retry);
4690 nfs4_label_release_security(label);
4695 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4696 u64 cookie, struct page **pages, unsigned int count, bool plus)
4698 struct inode *dir = d_inode(dentry);
4699 struct nfs4_readdir_arg args = {
4704 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4707 struct nfs4_readdir_res res;
4708 struct rpc_message msg = {
4709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4716 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4718 (unsigned long long)cookie);
4719 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4720 res.pgbase = args.pgbase;
4721 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4723 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4724 status += args.pgbase;
4727 nfs_invalidate_atime(dir);
4729 dprintk("%s: returns %d\n", __func__, status);
4733 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4734 u64 cookie, struct page **pages, unsigned int count, bool plus)
4736 struct nfs4_exception exception = { };
4739 err = _nfs4_proc_readdir(dentry, cred, cookie,
4740 pages, count, plus);
4741 trace_nfs4_readdir(d_inode(dentry), err);
4742 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4744 } while (exception.retry);
4748 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4749 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4751 struct nfs4_createdata *data;
4752 int mode = sattr->ia_mode;
4753 int status = -ENOMEM;
4755 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4760 data->arg.ftype = NF4FIFO;
4761 else if (S_ISBLK(mode)) {
4762 data->arg.ftype = NF4BLK;
4763 data->arg.u.device.specdata1 = MAJOR(rdev);
4764 data->arg.u.device.specdata2 = MINOR(rdev);
4766 else if (S_ISCHR(mode)) {
4767 data->arg.ftype = NF4CHR;
4768 data->arg.u.device.specdata1 = MAJOR(rdev);
4769 data->arg.u.device.specdata2 = MINOR(rdev);
4770 } else if (!S_ISSOCK(mode)) {
4775 data->arg.label = label;
4776 status = nfs4_do_create(dir, dentry, data);
4778 nfs4_free_createdata(data);
4783 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4784 struct iattr *sattr, dev_t rdev)
4786 struct nfs_server *server = NFS_SERVER(dir);
4787 struct nfs4_exception exception = { };
4788 struct nfs4_label l, *label = NULL;
4791 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4793 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4794 sattr->ia_mode &= ~current_umask();
4796 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4797 trace_nfs4_mknod(dir, &dentry->d_name, err);
4798 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4800 } while (exception.retry);
4802 nfs4_label_release_security(label);
4807 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4808 struct nfs_fsstat *fsstat)
4810 struct nfs4_statfs_arg args = {
4812 .bitmask = server->attr_bitmask,
4814 struct nfs4_statfs_res res = {
4817 struct rpc_message msg = {
4818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4823 nfs_fattr_init(fsstat->fattr);
4824 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4827 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4829 struct nfs4_exception exception = { };
4832 err = nfs4_handle_exception(server,
4833 _nfs4_proc_statfs(server, fhandle, fsstat),
4835 } while (exception.retry);
4839 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4840 struct nfs_fsinfo *fsinfo)
4842 struct nfs4_fsinfo_arg args = {
4844 .bitmask = server->attr_bitmask,
4846 struct nfs4_fsinfo_res res = {
4849 struct rpc_message msg = {
4850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4855 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4858 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4860 struct nfs4_exception exception = { };
4861 unsigned long now = jiffies;
4865 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4866 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4868 nfs4_set_lease_period(server->nfs_client,
4869 fsinfo->lease_time * HZ,
4873 err = nfs4_handle_exception(server, err, &exception);
4874 } while (exception.retry);
4878 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4882 nfs_fattr_init(fsinfo->fattr);
4883 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4885 /* block layout checks this! */
4886 server->pnfs_blksize = fsinfo->blksize;
4887 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4893 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4894 struct nfs_pathconf *pathconf)
4896 struct nfs4_pathconf_arg args = {
4898 .bitmask = server->attr_bitmask,
4900 struct nfs4_pathconf_res res = {
4901 .pathconf = pathconf,
4903 struct rpc_message msg = {
4904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4909 /* None of the pathconf attributes are mandatory to implement */
4910 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4911 memset(pathconf, 0, sizeof(*pathconf));
4915 nfs_fattr_init(pathconf->fattr);
4916 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4919 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4920 struct nfs_pathconf *pathconf)
4922 struct nfs4_exception exception = { };
4926 err = nfs4_handle_exception(server,
4927 _nfs4_proc_pathconf(server, fhandle, pathconf),
4929 } while (exception.retry);
4933 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4934 const struct nfs_open_context *ctx,
4935 const struct nfs_lock_context *l_ctx,
4938 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4940 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4942 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4943 const struct nfs_open_context *ctx,
4944 const struct nfs_lock_context *l_ctx,
4947 nfs4_stateid current_stateid;
4949 /* If the current stateid represents a lost lock, then exit */
4950 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4952 return nfs4_stateid_match(stateid, ¤t_stateid);
4955 static bool nfs4_error_stateid_expired(int err)
4958 case -NFS4ERR_DELEG_REVOKED:
4959 case -NFS4ERR_ADMIN_REVOKED:
4960 case -NFS4ERR_BAD_STATEID:
4961 case -NFS4ERR_STALE_STATEID:
4962 case -NFS4ERR_OLD_STATEID:
4963 case -NFS4ERR_OPENMODE:
4964 case -NFS4ERR_EXPIRED:
4970 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4972 struct nfs_server *server = NFS_SERVER(hdr->inode);
4974 trace_nfs4_read(hdr, task->tk_status);
4975 if (task->tk_status < 0) {
4976 struct nfs4_exception exception = {
4977 .inode = hdr->inode,
4978 .state = hdr->args.context->state,
4979 .stateid = &hdr->args.stateid,
4981 task->tk_status = nfs4_async_handle_exception(task,
4982 server, task->tk_status, &exception);
4983 if (exception.retry) {
4984 rpc_restart_call_prepare(task);
4989 if (task->tk_status > 0)
4990 renew_lease(server, hdr->timestamp);
4994 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4995 struct nfs_pgio_args *args)
4998 if (!nfs4_error_stateid_expired(task->tk_status) ||
4999 nfs4_stateid_is_current(&args->stateid,
5004 rpc_restart_call_prepare(task);
5008 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5011 dprintk("--> %s\n", __func__);
5013 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5015 if (nfs4_read_stateid_changed(task, &hdr->args))
5017 if (task->tk_status > 0)
5018 nfs_invalidate_atime(hdr->inode);
5019 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5020 nfs4_read_done_cb(task, hdr);
5023 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5024 struct rpc_message *msg)
5026 hdr->timestamp = jiffies;
5027 if (!hdr->pgio_done_cb)
5028 hdr->pgio_done_cb = nfs4_read_done_cb;
5029 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5030 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5033 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5034 struct nfs_pgio_header *hdr)
5036 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5037 &hdr->args.seq_args,
5041 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5042 hdr->args.lock_context,
5043 hdr->rw_mode) == -EIO)
5045 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5050 static int nfs4_write_done_cb(struct rpc_task *task,
5051 struct nfs_pgio_header *hdr)
5053 struct inode *inode = hdr->inode;
5055 trace_nfs4_write(hdr, task->tk_status);
5056 if (task->tk_status < 0) {
5057 struct nfs4_exception exception = {
5058 .inode = hdr->inode,
5059 .state = hdr->args.context->state,
5060 .stateid = &hdr->args.stateid,
5062 task->tk_status = nfs4_async_handle_exception(task,
5063 NFS_SERVER(inode), task->tk_status,
5065 if (exception.retry) {
5066 rpc_restart_call_prepare(task);
5070 if (task->tk_status >= 0) {
5071 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5072 nfs_writeback_update_inode(hdr);
5077 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5078 struct nfs_pgio_args *args)
5081 if (!nfs4_error_stateid_expired(task->tk_status) ||
5082 nfs4_stateid_is_current(&args->stateid,
5087 rpc_restart_call_prepare(task);
5091 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5093 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5095 if (nfs4_write_stateid_changed(task, &hdr->args))
5097 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5098 nfs4_write_done_cb(task, hdr);
5102 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5104 /* Don't request attributes for pNFS or O_DIRECT writes */
5105 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5107 /* Otherwise, request attributes if and only if we don't hold
5110 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5113 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5114 struct rpc_message *msg,
5115 struct rpc_clnt **clnt)
5117 struct nfs_server *server = NFS_SERVER(hdr->inode);
5119 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5120 hdr->args.bitmask = NULL;
5121 hdr->res.fattr = NULL;
5123 hdr->args.bitmask = server->cache_consistency_bitmask;
5125 if (!hdr->pgio_done_cb)
5126 hdr->pgio_done_cb = nfs4_write_done_cb;
5127 hdr->res.server = server;
5128 hdr->timestamp = jiffies;
5130 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5131 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5132 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5135 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5137 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5138 &data->args.seq_args,
5143 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5145 struct inode *inode = data->inode;
5147 trace_nfs4_commit(data, task->tk_status);
5148 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5149 NULL, NULL) == -EAGAIN) {
5150 rpc_restart_call_prepare(task);
5156 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5158 if (!nfs4_sequence_done(task, &data->res.seq_res))
5160 return data->commit_done_cb(task, data);
5163 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5164 struct rpc_clnt **clnt)
5166 struct nfs_server *server = NFS_SERVER(data->inode);
5168 if (data->commit_done_cb == NULL)
5169 data->commit_done_cb = nfs4_commit_done_cb;
5170 data->res.server = server;
5171 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5172 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5173 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5176 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5177 struct nfs_commitres *res)
5179 struct inode *dst_inode = file_inode(dst);
5180 struct nfs_server *server = NFS_SERVER(dst_inode);
5181 struct rpc_message msg = {
5182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5187 args->fh = NFS_FH(dst_inode);
5188 return nfs4_call_sync(server->client, server, &msg,
5189 &args->seq_args, &res->seq_res, 1);
5192 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5194 struct nfs_commitargs args = {
5198 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5199 struct nfs4_exception exception = { };
5203 status = _nfs4_proc_commit(dst, &args, res);
5204 status = nfs4_handle_exception(dst_server, status, &exception);
5205 } while (exception.retry);
5210 struct nfs4_renewdata {
5211 struct nfs_client *client;
5212 unsigned long timestamp;
5216 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5217 * standalone procedure for queueing an asynchronous RENEW.
5219 static void nfs4_renew_release(void *calldata)
5221 struct nfs4_renewdata *data = calldata;
5222 struct nfs_client *clp = data->client;
5224 if (refcount_read(&clp->cl_count) > 1)
5225 nfs4_schedule_state_renewal(clp);
5226 nfs_put_client(clp);
5230 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5232 struct nfs4_renewdata *data = calldata;
5233 struct nfs_client *clp = data->client;
5234 unsigned long timestamp = data->timestamp;
5236 trace_nfs4_renew_async(clp, task->tk_status);
5237 switch (task->tk_status) {
5240 case -NFS4ERR_LEASE_MOVED:
5241 nfs4_schedule_lease_moved_recovery(clp);
5244 /* Unless we're shutting down, schedule state recovery! */
5245 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5247 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5248 nfs4_schedule_lease_recovery(clp);
5251 nfs4_schedule_path_down_recovery(clp);
5253 do_renew_lease(clp, timestamp);
5256 static const struct rpc_call_ops nfs4_renew_ops = {
5257 .rpc_call_done = nfs4_renew_done,
5258 .rpc_release = nfs4_renew_release,
5261 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5263 struct rpc_message msg = {
5264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5268 struct nfs4_renewdata *data;
5270 if (renew_flags == 0)
5272 if (!refcount_inc_not_zero(&clp->cl_count))
5274 data = kmalloc(sizeof(*data), GFP_NOFS);
5276 nfs_put_client(clp);
5280 data->timestamp = jiffies;
5281 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5282 &nfs4_renew_ops, data);
5285 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5292 unsigned long now = jiffies;
5295 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5298 do_renew_lease(clp, now);
5302 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5304 return server->caps & NFS_CAP_ACLS;
5307 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5308 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5311 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5313 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5314 struct page **pages)
5316 struct page *newpage, **spages;
5322 len = min_t(size_t, PAGE_SIZE, buflen);
5323 newpage = alloc_page(GFP_KERNEL);
5325 if (newpage == NULL)
5327 memcpy(page_address(newpage), buf, len);
5332 } while (buflen != 0);
5338 __free_page(spages[rc-1]);
5342 struct nfs4_cached_acl {
5348 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5350 struct nfs_inode *nfsi = NFS_I(inode);
5352 spin_lock(&inode->i_lock);
5353 kfree(nfsi->nfs4_acl);
5354 nfsi->nfs4_acl = acl;
5355 spin_unlock(&inode->i_lock);
5358 static void nfs4_zap_acl_attr(struct inode *inode)
5360 nfs4_set_cached_acl(inode, NULL);
5363 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5365 struct nfs_inode *nfsi = NFS_I(inode);
5366 struct nfs4_cached_acl *acl;
5369 spin_lock(&inode->i_lock);
5370 acl = nfsi->nfs4_acl;
5373 if (buf == NULL) /* user is just asking for length */
5375 if (acl->cached == 0)
5377 ret = -ERANGE; /* see getxattr(2) man page */
5378 if (acl->len > buflen)
5380 memcpy(buf, acl->data, acl->len);
5384 spin_unlock(&inode->i_lock);
5388 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5390 struct nfs4_cached_acl *acl;
5391 size_t buflen = sizeof(*acl) + acl_len;
5393 if (buflen <= PAGE_SIZE) {
5394 acl = kmalloc(buflen, GFP_KERNEL);
5398 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5400 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5407 nfs4_set_cached_acl(inode, acl);
5411 * The getxattr API returns the required buffer length when called with a
5412 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5413 * the required buf. On a NULL buf, we send a page of data to the server
5414 * guessing that the ACL request can be serviced by a page. If so, we cache
5415 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5416 * the cache. If not so, we throw away the page, and cache the required
5417 * length. The next getxattr call will then produce another round trip to
5418 * the server, this time with the input buf of the required size.
5420 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5422 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5423 struct nfs_getaclargs args = {
5424 .fh = NFS_FH(inode),
5428 struct nfs_getaclres res = {
5431 struct rpc_message msg = {
5432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5436 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5437 int ret = -ENOMEM, i;
5439 if (npages > ARRAY_SIZE(pages))
5442 for (i = 0; i < npages; i++) {
5443 pages[i] = alloc_page(GFP_KERNEL);
5448 /* for decoding across pages */
5449 res.acl_scratch = alloc_page(GFP_KERNEL);
5450 if (!res.acl_scratch)
5453 args.acl_len = npages * PAGE_SIZE;
5455 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5456 __func__, buf, buflen, npages, args.acl_len);
5457 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5458 &msg, &args.seq_args, &res.seq_res, 0);
5462 /* Handle the case where the passed-in buffer is too short */
5463 if (res.acl_flags & NFS4_ACL_TRUNC) {
5464 /* Did the user only issue a request for the acl length? */
5470 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5472 if (res.acl_len > buflen) {
5476 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5481 for (i = 0; i < npages; i++)
5483 __free_page(pages[i]);
5484 if (res.acl_scratch)
5485 __free_page(res.acl_scratch);
5489 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5491 struct nfs4_exception exception = { };
5494 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5495 trace_nfs4_get_acl(inode, ret);
5498 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5499 } while (exception.retry);
5503 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5505 struct nfs_server *server = NFS_SERVER(inode);
5508 if (!nfs4_server_supports_acls(server))
5510 ret = nfs_revalidate_inode(server, inode);
5513 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5514 nfs_zap_acl_cache(inode);
5515 ret = nfs4_read_cached_acl(inode, buf, buflen);
5517 /* -ENOENT is returned if there is no ACL or if there is an ACL
5518 * but no cached acl data, just the acl length */
5520 return nfs4_get_acl_uncached(inode, buf, buflen);
5523 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5525 struct nfs_server *server = NFS_SERVER(inode);
5526 struct page *pages[NFS4ACL_MAXPAGES];
5527 struct nfs_setaclargs arg = {
5528 .fh = NFS_FH(inode),
5532 struct nfs_setaclres res;
5533 struct rpc_message msg = {
5534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5538 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5541 if (!nfs4_server_supports_acls(server))
5543 if (npages > ARRAY_SIZE(pages))
5545 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5548 nfs4_inode_make_writeable(inode);
5549 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5552 * Free each page after tx, so the only ref left is
5553 * held by the network stack
5556 put_page(pages[i-1]);
5559 * Acl update can result in inode attribute update.
5560 * so mark the attribute cache invalid.
5562 spin_lock(&inode->i_lock);
5563 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5564 | NFS_INO_INVALID_CTIME
5565 | NFS_INO_REVAL_FORCED;
5566 spin_unlock(&inode->i_lock);
5567 nfs_access_zap_cache(inode);
5568 nfs_zap_acl_cache(inode);
5572 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5574 struct nfs4_exception exception = { };
5577 err = __nfs4_proc_set_acl(inode, buf, buflen);
5578 trace_nfs4_set_acl(inode, err);
5579 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5581 } while (exception.retry);
5585 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5586 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5589 struct nfs_server *server = NFS_SERVER(inode);
5590 struct nfs_fattr fattr;
5591 struct nfs4_label label = {0, 0, buflen, buf};
5593 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5594 struct nfs4_getattr_arg arg = {
5595 .fh = NFS_FH(inode),
5598 struct nfs4_getattr_res res = {
5603 struct rpc_message msg = {
5604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5610 nfs_fattr_init(&fattr);
5612 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5615 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5617 if (buflen < label.len)
5622 static int nfs4_get_security_label(struct inode *inode, void *buf,
5625 struct nfs4_exception exception = { };
5628 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5632 err = _nfs4_get_security_label(inode, buf, buflen);
5633 trace_nfs4_get_security_label(inode, err);
5634 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5636 } while (exception.retry);
5640 static int _nfs4_do_set_security_label(struct inode *inode,
5641 struct nfs4_label *ilabel,
5642 struct nfs_fattr *fattr,
5643 struct nfs4_label *olabel)
5646 struct iattr sattr = {0};
5647 struct nfs_server *server = NFS_SERVER(inode);
5648 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5649 struct nfs_setattrargs arg = {
5650 .fh = NFS_FH(inode),
5656 struct nfs_setattrres res = {
5661 struct rpc_message msg = {
5662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5668 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5670 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5672 dprintk("%s failed: %d\n", __func__, status);
5677 static int nfs4_do_set_security_label(struct inode *inode,
5678 struct nfs4_label *ilabel,
5679 struct nfs_fattr *fattr,
5680 struct nfs4_label *olabel)
5682 struct nfs4_exception exception = { };
5686 err = _nfs4_do_set_security_label(inode, ilabel,
5688 trace_nfs4_set_security_label(inode, err);
5689 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5691 } while (exception.retry);
5696 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5698 struct nfs4_label ilabel, *olabel = NULL;
5699 struct nfs_fattr fattr;
5702 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5705 nfs_fattr_init(&fattr);
5709 ilabel.label = (char *)buf;
5710 ilabel.len = buflen;
5712 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5713 if (IS_ERR(olabel)) {
5714 status = -PTR_ERR(olabel);
5718 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5720 nfs_setsecurity(inode, &fattr, olabel);
5722 nfs4_label_free(olabel);
5726 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5729 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5730 nfs4_verifier *bootverf)
5734 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5735 /* An impossible timestamp guarantees this value
5736 * will never match a generated boot time. */
5737 verf[0] = cpu_to_be32(U32_MAX);
5738 verf[1] = cpu_to_be32(U32_MAX);
5740 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5741 u64 ns = ktime_to_ns(nn->boot_time);
5743 verf[0] = cpu_to_be32(ns >> 32);
5744 verf[1] = cpu_to_be32(ns);
5746 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5750 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5755 if (clp->cl_owner_id != NULL)
5760 strlen(clp->cl_rpcclient->cl_nodename) +
5762 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5765 if (nfs4_client_id_uniquifier[0] != '\0')
5766 len += strlen(nfs4_client_id_uniquifier) + 1;
5767 if (len > NFS4_OPAQUE_LIMIT + 1)
5771 * Since this string is allocated at mount time, and held until the
5772 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5773 * about a memory-reclaim deadlock.
5775 str = kmalloc(len, GFP_KERNEL);
5780 if (nfs4_client_id_uniquifier[0] != '\0')
5781 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5782 clp->cl_rpcclient->cl_nodename,
5783 nfs4_client_id_uniquifier,
5784 rpc_peeraddr2str(clp->cl_rpcclient,
5787 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5788 clp->cl_rpcclient->cl_nodename,
5789 rpc_peeraddr2str(clp->cl_rpcclient,
5793 clp->cl_owner_id = str;
5798 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5803 len = 10 + 10 + 1 + 10 + 1 +
5804 strlen(nfs4_client_id_uniquifier) + 1 +
5805 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5807 if (len > NFS4_OPAQUE_LIMIT + 1)
5811 * Since this string is allocated at mount time, and held until the
5812 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5813 * about a memory-reclaim deadlock.
5815 str = kmalloc(len, GFP_KERNEL);
5819 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5820 clp->rpc_ops->version, clp->cl_minorversion,
5821 nfs4_client_id_uniquifier,
5822 clp->cl_rpcclient->cl_nodename);
5823 clp->cl_owner_id = str;
5828 nfs4_init_uniform_client_string(struct nfs_client *clp)
5833 if (clp->cl_owner_id != NULL)
5836 if (nfs4_client_id_uniquifier[0] != '\0')
5837 return nfs4_init_uniquifier_client_string(clp);
5839 len = 10 + 10 + 1 + 10 + 1 +
5840 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5842 if (len > NFS4_OPAQUE_LIMIT + 1)
5846 * Since this string is allocated at mount time, and held until the
5847 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5848 * about a memory-reclaim deadlock.
5850 str = kmalloc(len, GFP_KERNEL);
5854 scnprintf(str, len, "Linux NFSv%u.%u %s",
5855 clp->rpc_ops->version, clp->cl_minorversion,
5856 clp->cl_rpcclient->cl_nodename);
5857 clp->cl_owner_id = str;
5862 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5863 * services. Advertise one based on the address family of the
5867 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5869 if (strchr(clp->cl_ipaddr, ':') != NULL)
5870 return scnprintf(buf, len, "tcp6");
5872 return scnprintf(buf, len, "tcp");
5875 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5877 struct nfs4_setclientid *sc = calldata;
5879 if (task->tk_status == 0)
5880 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5883 static const struct rpc_call_ops nfs4_setclientid_ops = {
5884 .rpc_call_done = nfs4_setclientid_done,
5888 * nfs4_proc_setclientid - Negotiate client ID
5889 * @clp: state data structure
5890 * @program: RPC program for NFSv4 callback service
5891 * @port: IP port number for NFS4 callback service
5892 * @cred: credential to use for this call
5893 * @res: where to place the result
5895 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5897 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5898 unsigned short port, const struct cred *cred,
5899 struct nfs4_setclientid_res *res)
5901 nfs4_verifier sc_verifier;
5902 struct nfs4_setclientid setclientid = {
5903 .sc_verifier = &sc_verifier,
5907 struct rpc_message msg = {
5908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5909 .rpc_argp = &setclientid,
5913 struct rpc_task *task;
5914 struct rpc_task_setup task_setup_data = {
5915 .rpc_client = clp->cl_rpcclient,
5916 .rpc_message = &msg,
5917 .callback_ops = &nfs4_setclientid_ops,
5918 .callback_data = &setclientid,
5919 .flags = RPC_TASK_TIMEOUT,
5923 /* nfs_client_id4 */
5924 nfs4_init_boot_verifier(clp, &sc_verifier);
5926 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5927 status = nfs4_init_uniform_client_string(clp);
5929 status = nfs4_init_nonuniform_client_string(clp);
5935 setclientid.sc_netid_len =
5936 nfs4_init_callback_netid(clp,
5937 setclientid.sc_netid,
5938 sizeof(setclientid.sc_netid));
5939 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5940 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5941 clp->cl_ipaddr, port >> 8, port & 255);
5943 dprintk("NFS call setclientid auth=%s, '%s'\n",
5944 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5946 task = rpc_run_task(&task_setup_data);
5948 status = PTR_ERR(task);
5951 status = task->tk_status;
5952 if (setclientid.sc_cred) {
5953 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5954 put_rpccred(setclientid.sc_cred);
5958 trace_nfs4_setclientid(clp, status);
5959 dprintk("NFS reply setclientid: %d\n", status);
5964 * nfs4_proc_setclientid_confirm - Confirm client ID
5965 * @clp: state data structure
5966 * @arg: result of a previous SETCLIENTID
5967 * @cred: credential to use for this call
5969 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5971 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5972 struct nfs4_setclientid_res *arg,
5973 const struct cred *cred)
5975 struct rpc_message msg = {
5976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5982 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5983 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5985 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5986 trace_nfs4_setclientid_confirm(clp, status);
5987 dprintk("NFS reply setclientid_confirm: %d\n", status);
5991 struct nfs4_delegreturndata {
5992 struct nfs4_delegreturnargs args;
5993 struct nfs4_delegreturnres res;
5995 nfs4_stateid stateid;
5996 unsigned long timestamp;
5998 struct nfs4_layoutreturn_args arg;
5999 struct nfs4_layoutreturn_res res;
6000 struct nfs4_xdr_opaque_data ld_private;
6004 struct nfs_fattr fattr;
6006 struct inode *inode;
6009 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6011 struct nfs4_delegreturndata *data = calldata;
6012 struct nfs4_exception exception = {
6013 .inode = data->inode,
6014 .stateid = &data->stateid,
6017 if (!nfs4_sequence_done(task, &data->res.seq_res))
6020 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6022 /* Handle Layoutreturn errors */
6023 if (data->args.lr_args && task->tk_status != 0) {
6024 switch(data->res.lr_ret) {
6026 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6029 data->args.lr_args = NULL;
6030 data->res.lr_res = NULL;
6032 case -NFS4ERR_OLD_STATEID:
6033 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6034 &data->args.lr_args->range,
6038 case -NFS4ERR_ADMIN_REVOKED:
6039 case -NFS4ERR_DELEG_REVOKED:
6040 case -NFS4ERR_EXPIRED:
6041 case -NFS4ERR_BAD_STATEID:
6042 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6043 case -NFS4ERR_WRONG_CRED:
6044 data->args.lr_args = NULL;
6045 data->res.lr_res = NULL;
6050 switch (task->tk_status) {
6052 renew_lease(data->res.server, data->timestamp);
6054 case -NFS4ERR_ADMIN_REVOKED:
6055 case -NFS4ERR_DELEG_REVOKED:
6056 case -NFS4ERR_EXPIRED:
6057 nfs4_free_revoked_stateid(data->res.server,
6059 task->tk_msg.rpc_cred);
6061 case -NFS4ERR_BAD_STATEID:
6062 case -NFS4ERR_STALE_STATEID:
6063 task->tk_status = 0;
6065 case -NFS4ERR_OLD_STATEID:
6066 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6068 task->tk_status = 0;
6070 case -NFS4ERR_ACCESS:
6071 if (data->args.bitmask) {
6072 data->args.bitmask = NULL;
6073 data->res.fattr = NULL;
6078 task->tk_status = nfs4_async_handle_exception(task,
6079 data->res.server, task->tk_status,
6081 if (exception.retry)
6084 data->rpc_status = task->tk_status;
6087 data->res.lr_ret = 0;
6089 task->tk_status = 0;
6090 rpc_restart_call_prepare(task);
6093 static void nfs4_delegreturn_release(void *calldata)
6095 struct nfs4_delegreturndata *data = calldata;
6096 struct inode *inode = data->inode;
6100 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6102 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6103 nfs_iput_and_deactive(inode);
6108 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6110 struct nfs4_delegreturndata *d_data;
6111 struct pnfs_layout_hdr *lo;
6113 d_data = (struct nfs4_delegreturndata *)data;
6115 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6118 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6119 if (lo && !pnfs_layout_is_valid(lo)) {
6120 d_data->args.lr_args = NULL;
6121 d_data->res.lr_res = NULL;
6124 nfs4_setup_sequence(d_data->res.server->nfs_client,
6125 &d_data->args.seq_args,
6126 &d_data->res.seq_res,
6130 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6131 .rpc_call_prepare = nfs4_delegreturn_prepare,
6132 .rpc_call_done = nfs4_delegreturn_done,
6133 .rpc_release = nfs4_delegreturn_release,
6136 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6138 struct nfs4_delegreturndata *data;
6139 struct nfs_server *server = NFS_SERVER(inode);
6140 struct rpc_task *task;
6141 struct rpc_message msg = {
6142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6145 struct rpc_task_setup task_setup_data = {
6146 .rpc_client = server->client,
6147 .rpc_message = &msg,
6148 .callback_ops = &nfs4_delegreturn_ops,
6149 .flags = RPC_TASK_ASYNC,
6153 data = kzalloc(sizeof(*data), GFP_NOFS);
6156 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6158 nfs4_state_protect(server->nfs_client,
6159 NFS_SP4_MACH_CRED_CLEANUP,
6160 &task_setup_data.rpc_client, &msg);
6162 data->args.fhandle = &data->fh;
6163 data->args.stateid = &data->stateid;
6164 data->args.bitmask = server->cache_consistency_bitmask;
6165 nfs_copy_fh(&data->fh, NFS_FH(inode));
6166 nfs4_stateid_copy(&data->stateid, stateid);
6167 data->res.fattr = &data->fattr;
6168 data->res.server = server;
6169 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6170 data->lr.arg.ld_private = &data->lr.ld_private;
6171 nfs_fattr_init(data->res.fattr);
6172 data->timestamp = jiffies;
6173 data->rpc_status = 0;
6174 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6175 data->inode = nfs_igrab_and_active(inode);
6178 data->args.lr_args = &data->lr.arg;
6179 data->res.lr_res = &data->lr.res;
6181 } else if (data->lr.roc) {
6182 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6183 data->lr.roc = false;
6186 task_setup_data.callback_data = data;
6187 msg.rpc_argp = &data->args;
6188 msg.rpc_resp = &data->res;
6189 task = rpc_run_task(&task_setup_data);
6191 return PTR_ERR(task);
6194 status = rpc_wait_for_completion_task(task);
6197 status = data->rpc_status;
6203 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6205 struct nfs_server *server = NFS_SERVER(inode);
6206 struct nfs4_exception exception = { };
6209 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6210 trace_nfs4_delegreturn(inode, stateid, err);
6212 case -NFS4ERR_STALE_STATEID:
6213 case -NFS4ERR_EXPIRED:
6217 err = nfs4_handle_exception(server, err, &exception);
6218 } while (exception.retry);
6222 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6224 struct inode *inode = state->inode;
6225 struct nfs_server *server = NFS_SERVER(inode);
6226 struct nfs_client *clp = server->nfs_client;
6227 struct nfs_lockt_args arg = {
6228 .fh = NFS_FH(inode),
6231 struct nfs_lockt_res res = {
6234 struct rpc_message msg = {
6235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6238 .rpc_cred = state->owner->so_cred,
6240 struct nfs4_lock_state *lsp;
6243 arg.lock_owner.clientid = clp->cl_clientid;
6244 status = nfs4_set_lock_state(state, request);
6247 lsp = request->fl_u.nfs4_fl.owner;
6248 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6249 arg.lock_owner.s_dev = server->s_dev;
6250 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6253 request->fl_type = F_UNLCK;
6255 case -NFS4ERR_DENIED:
6258 request->fl_ops->fl_release_private(request);
6259 request->fl_ops = NULL;
6264 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6266 struct nfs4_exception exception = { };
6270 err = _nfs4_proc_getlk(state, cmd, request);
6271 trace_nfs4_get_lock(request, state, cmd, err);
6272 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6274 } while (exception.retry);
6278 struct nfs4_unlockdata {
6279 struct nfs_locku_args arg;
6280 struct nfs_locku_res res;
6281 struct nfs4_lock_state *lsp;
6282 struct nfs_open_context *ctx;
6283 struct nfs_lock_context *l_ctx;
6284 struct file_lock fl;
6285 struct nfs_server *server;
6286 unsigned long timestamp;
6289 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6290 struct nfs_open_context *ctx,
6291 struct nfs4_lock_state *lsp,
6292 struct nfs_seqid *seqid)
6294 struct nfs4_unlockdata *p;
6295 struct inode *inode = lsp->ls_state->inode;
6297 p = kzalloc(sizeof(*p), GFP_NOFS);
6300 p->arg.fh = NFS_FH(inode);
6302 p->arg.seqid = seqid;
6303 p->res.seqid = seqid;
6305 /* Ensure we don't close file until we're done freeing locks! */
6306 p->ctx = get_nfs_open_context(ctx);
6307 p->l_ctx = nfs_get_lock_context(ctx);
6308 locks_init_lock(&p->fl);
6309 locks_copy_lock(&p->fl, fl);
6310 p->server = NFS_SERVER(inode);
6314 static void nfs4_locku_release_calldata(void *data)
6316 struct nfs4_unlockdata *calldata = data;
6317 nfs_free_seqid(calldata->arg.seqid);
6318 nfs4_put_lock_state(calldata->lsp);
6319 nfs_put_lock_context(calldata->l_ctx);
6320 put_nfs_open_context(calldata->ctx);
6324 static void nfs4_locku_done(struct rpc_task *task, void *data)
6326 struct nfs4_unlockdata *calldata = data;
6327 struct nfs4_exception exception = {
6328 .inode = calldata->lsp->ls_state->inode,
6329 .stateid = &calldata->arg.stateid,
6332 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6334 switch (task->tk_status) {
6336 renew_lease(calldata->server, calldata->timestamp);
6337 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6338 if (nfs4_update_lock_stateid(calldata->lsp,
6339 &calldata->res.stateid))
6342 case -NFS4ERR_ADMIN_REVOKED:
6343 case -NFS4ERR_EXPIRED:
6344 nfs4_free_revoked_stateid(calldata->server,
6345 &calldata->arg.stateid,
6346 task->tk_msg.rpc_cred);
6348 case -NFS4ERR_BAD_STATEID:
6349 case -NFS4ERR_OLD_STATEID:
6350 case -NFS4ERR_STALE_STATEID:
6351 if (!nfs4_stateid_match(&calldata->arg.stateid,
6352 &calldata->lsp->ls_stateid))
6353 rpc_restart_call_prepare(task);
6356 task->tk_status = nfs4_async_handle_exception(task,
6357 calldata->server, task->tk_status,
6359 if (exception.retry)
6360 rpc_restart_call_prepare(task);
6362 nfs_release_seqid(calldata->arg.seqid);
6365 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6367 struct nfs4_unlockdata *calldata = data;
6369 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6370 nfs_async_iocounter_wait(task, calldata->l_ctx))
6373 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6375 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6376 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6377 /* Note: exit _without_ running nfs4_locku_done */
6380 calldata->timestamp = jiffies;
6381 if (nfs4_setup_sequence(calldata->server->nfs_client,
6382 &calldata->arg.seq_args,
6383 &calldata->res.seq_res,
6385 nfs_release_seqid(calldata->arg.seqid);
6388 task->tk_action = NULL;
6390 nfs4_sequence_done(task, &calldata->res.seq_res);
6393 static const struct rpc_call_ops nfs4_locku_ops = {
6394 .rpc_call_prepare = nfs4_locku_prepare,
6395 .rpc_call_done = nfs4_locku_done,
6396 .rpc_release = nfs4_locku_release_calldata,
6399 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6400 struct nfs_open_context *ctx,
6401 struct nfs4_lock_state *lsp,
6402 struct nfs_seqid *seqid)
6404 struct nfs4_unlockdata *data;
6405 struct rpc_message msg = {
6406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6407 .rpc_cred = ctx->cred,
6409 struct rpc_task_setup task_setup_data = {
6410 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6411 .rpc_message = &msg,
6412 .callback_ops = &nfs4_locku_ops,
6413 .workqueue = nfsiod_workqueue,
6414 .flags = RPC_TASK_ASYNC,
6417 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6418 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6420 /* Ensure this is an unlock - when canceling a lock, the
6421 * canceled lock is passed in, and it won't be an unlock.
6423 fl->fl_type = F_UNLCK;
6424 if (fl->fl_flags & FL_CLOSE)
6425 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6427 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6429 nfs_free_seqid(seqid);
6430 return ERR_PTR(-ENOMEM);
6433 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6434 msg.rpc_argp = &data->arg;
6435 msg.rpc_resp = &data->res;
6436 task_setup_data.callback_data = data;
6437 return rpc_run_task(&task_setup_data);
6440 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6442 struct inode *inode = state->inode;
6443 struct nfs4_state_owner *sp = state->owner;
6444 struct nfs_inode *nfsi = NFS_I(inode);
6445 struct nfs_seqid *seqid;
6446 struct nfs4_lock_state *lsp;
6447 struct rpc_task *task;
6448 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6450 unsigned char fl_flags = request->fl_flags;
6452 status = nfs4_set_lock_state(state, request);
6453 /* Unlock _before_ we do the RPC call */
6454 request->fl_flags |= FL_EXISTS;
6455 /* Exclude nfs_delegation_claim_locks() */
6456 mutex_lock(&sp->so_delegreturn_mutex);
6457 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6458 down_read(&nfsi->rwsem);
6459 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6460 up_read(&nfsi->rwsem);
6461 mutex_unlock(&sp->so_delegreturn_mutex);
6464 up_read(&nfsi->rwsem);
6465 mutex_unlock(&sp->so_delegreturn_mutex);
6468 /* Is this a delegated lock? */
6469 lsp = request->fl_u.nfs4_fl.owner;
6470 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6472 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6473 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6477 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6478 status = PTR_ERR(task);
6481 status = rpc_wait_for_completion_task(task);
6484 request->fl_flags = fl_flags;
6485 trace_nfs4_unlock(request, state, F_SETLK, status);
6489 struct nfs4_lockdata {
6490 struct nfs_lock_args arg;
6491 struct nfs_lock_res res;
6492 struct nfs4_lock_state *lsp;
6493 struct nfs_open_context *ctx;
6494 struct file_lock fl;
6495 unsigned long timestamp;
6498 struct nfs_server *server;
6501 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6502 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6505 struct nfs4_lockdata *p;
6506 struct inode *inode = lsp->ls_state->inode;
6507 struct nfs_server *server = NFS_SERVER(inode);
6508 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6510 p = kzalloc(sizeof(*p), gfp_mask);
6514 p->arg.fh = NFS_FH(inode);
6516 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6517 if (IS_ERR(p->arg.open_seqid))
6519 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6520 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6521 if (IS_ERR(p->arg.lock_seqid))
6522 goto out_free_seqid;
6523 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6524 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6525 p->arg.lock_owner.s_dev = server->s_dev;
6526 p->res.lock_seqid = p->arg.lock_seqid;
6529 p->ctx = get_nfs_open_context(ctx);
6530 locks_init_lock(&p->fl);
6531 locks_copy_lock(&p->fl, fl);
6534 nfs_free_seqid(p->arg.open_seqid);
6540 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6542 struct nfs4_lockdata *data = calldata;
6543 struct nfs4_state *state = data->lsp->ls_state;
6545 dprintk("%s: begin!\n", __func__);
6546 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6548 /* Do we need to do an open_to_lock_owner? */
6549 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6550 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6551 goto out_release_lock_seqid;
6553 nfs4_stateid_copy(&data->arg.open_stateid,
6554 &state->open_stateid);
6555 data->arg.new_lock_owner = 1;
6556 data->res.open_seqid = data->arg.open_seqid;
6558 data->arg.new_lock_owner = 0;
6559 nfs4_stateid_copy(&data->arg.lock_stateid,
6560 &data->lsp->ls_stateid);
6562 if (!nfs4_valid_open_stateid(state)) {
6563 data->rpc_status = -EBADF;
6564 task->tk_action = NULL;
6565 goto out_release_open_seqid;
6567 data->timestamp = jiffies;
6568 if (nfs4_setup_sequence(data->server->nfs_client,
6569 &data->arg.seq_args,
6573 out_release_open_seqid:
6574 nfs_release_seqid(data->arg.open_seqid);
6575 out_release_lock_seqid:
6576 nfs_release_seqid(data->arg.lock_seqid);
6578 nfs4_sequence_done(task, &data->res.seq_res);
6579 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6582 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6584 struct nfs4_lockdata *data = calldata;
6585 struct nfs4_lock_state *lsp = data->lsp;
6587 dprintk("%s: begin!\n", __func__);
6589 if (!nfs4_sequence_done(task, &data->res.seq_res))
6592 data->rpc_status = task->tk_status;
6593 switch (task->tk_status) {
6595 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6597 if (data->arg.new_lock && !data->cancelled) {
6598 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6599 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6602 if (data->arg.new_lock_owner != 0) {
6603 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6604 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6605 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6606 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6609 case -NFS4ERR_BAD_STATEID:
6610 case -NFS4ERR_OLD_STATEID:
6611 case -NFS4ERR_STALE_STATEID:
6612 case -NFS4ERR_EXPIRED:
6613 if (data->arg.new_lock_owner != 0) {
6614 if (!nfs4_stateid_match(&data->arg.open_stateid,
6615 &lsp->ls_state->open_stateid))
6617 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6622 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6625 if (!data->cancelled)
6626 rpc_restart_call_prepare(task);
6630 static void nfs4_lock_release(void *calldata)
6632 struct nfs4_lockdata *data = calldata;
6634 dprintk("%s: begin!\n", __func__);
6635 nfs_free_seqid(data->arg.open_seqid);
6636 if (data->cancelled && data->rpc_status == 0) {
6637 struct rpc_task *task;
6638 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6639 data->arg.lock_seqid);
6641 rpc_put_task_async(task);
6642 dprintk("%s: cancelling lock!\n", __func__);
6644 nfs_free_seqid(data->arg.lock_seqid);
6645 nfs4_put_lock_state(data->lsp);
6646 put_nfs_open_context(data->ctx);
6648 dprintk("%s: done!\n", __func__);
6651 static const struct rpc_call_ops nfs4_lock_ops = {
6652 .rpc_call_prepare = nfs4_lock_prepare,
6653 .rpc_call_done = nfs4_lock_done,
6654 .rpc_release = nfs4_lock_release,
6657 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6660 case -NFS4ERR_ADMIN_REVOKED:
6661 case -NFS4ERR_EXPIRED:
6662 case -NFS4ERR_BAD_STATEID:
6663 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6664 if (new_lock_owner != 0 ||
6665 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6666 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6668 case -NFS4ERR_STALE_STATEID:
6669 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6670 nfs4_schedule_lease_recovery(server->nfs_client);
6674 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6676 struct nfs4_lockdata *data;
6677 struct rpc_task *task;
6678 struct rpc_message msg = {
6679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6680 .rpc_cred = state->owner->so_cred,
6682 struct rpc_task_setup task_setup_data = {
6683 .rpc_client = NFS_CLIENT(state->inode),
6684 .rpc_message = &msg,
6685 .callback_ops = &nfs4_lock_ops,
6686 .workqueue = nfsiod_workqueue,
6687 .flags = RPC_TASK_ASYNC,
6691 dprintk("%s: begin!\n", __func__);
6692 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6693 fl->fl_u.nfs4_fl.owner,
6694 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6698 data->arg.block = 1;
6699 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6700 recovery_type > NFS_LOCK_NEW);
6701 msg.rpc_argp = &data->arg;
6702 msg.rpc_resp = &data->res;
6703 task_setup_data.callback_data = data;
6704 if (recovery_type > NFS_LOCK_NEW) {
6705 if (recovery_type == NFS_LOCK_RECLAIM)
6706 data->arg.reclaim = NFS_LOCK_RECLAIM;
6708 data->arg.new_lock = 1;
6709 task = rpc_run_task(&task_setup_data);
6711 return PTR_ERR(task);
6712 ret = rpc_wait_for_completion_task(task);
6714 ret = data->rpc_status;
6716 nfs4_handle_setlk_error(data->server, data->lsp,
6717 data->arg.new_lock_owner, ret);
6719 data->cancelled = true;
6721 dprintk("%s: done, ret = %d!\n", __func__, ret);
6722 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6726 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6728 struct nfs_server *server = NFS_SERVER(state->inode);
6729 struct nfs4_exception exception = {
6730 .inode = state->inode,
6735 /* Cache the lock if possible... */
6736 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6738 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6739 if (err != -NFS4ERR_DELAY)
6741 nfs4_handle_exception(server, err, &exception);
6742 } while (exception.retry);
6746 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6748 struct nfs_server *server = NFS_SERVER(state->inode);
6749 struct nfs4_exception exception = {
6750 .inode = state->inode,
6754 err = nfs4_set_lock_state(state, request);
6757 if (!recover_lost_locks) {
6758 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6762 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6764 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6768 case -NFS4ERR_GRACE:
6769 case -NFS4ERR_DELAY:
6770 nfs4_handle_exception(server, err, &exception);
6773 } while (exception.retry);
6778 #if defined(CONFIG_NFS_V4_1)
6779 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6781 struct nfs4_lock_state *lsp;
6784 status = nfs4_set_lock_state(state, request);
6787 lsp = request->fl_u.nfs4_fl.owner;
6788 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6789 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6791 return nfs4_lock_expired(state, request);
6795 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6797 struct nfs_inode *nfsi = NFS_I(state->inode);
6798 struct nfs4_state_owner *sp = state->owner;
6799 unsigned char fl_flags = request->fl_flags;
6802 request->fl_flags |= FL_ACCESS;
6803 status = locks_lock_inode_wait(state->inode, request);
6806 mutex_lock(&sp->so_delegreturn_mutex);
6807 down_read(&nfsi->rwsem);
6808 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6809 /* Yes: cache locks! */
6810 /* ...but avoid races with delegation recall... */
6811 request->fl_flags = fl_flags & ~FL_SLEEP;
6812 status = locks_lock_inode_wait(state->inode, request);
6813 up_read(&nfsi->rwsem);
6814 mutex_unlock(&sp->so_delegreturn_mutex);
6817 up_read(&nfsi->rwsem);
6818 mutex_unlock(&sp->so_delegreturn_mutex);
6819 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6821 request->fl_flags = fl_flags;
6825 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6827 struct nfs4_exception exception = {
6829 .inode = state->inode,
6834 err = _nfs4_proc_setlk(state, cmd, request);
6835 if (err == -NFS4ERR_DENIED)
6837 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6839 } while (exception.retry);
6843 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6844 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6847 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6848 struct file_lock *request)
6850 int status = -ERESTARTSYS;
6851 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6853 while(!signalled()) {
6854 status = nfs4_proc_setlk(state, cmd, request);
6855 if ((status != -EAGAIN) || IS_SETLK(cmd))
6857 freezable_schedule_timeout_interruptible(timeout);
6859 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6860 status = -ERESTARTSYS;
6865 #ifdef CONFIG_NFS_V4_1
6866 struct nfs4_lock_waiter {
6867 struct task_struct *task;
6868 struct inode *inode;
6869 struct nfs_lowner *owner;
6874 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6877 struct nfs4_lock_waiter *waiter = wait->private;
6879 /* NULL key means to wake up everyone */
6881 struct cb_notify_lock_args *cbnl = key;
6882 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6883 *wowner = waiter->owner;
6885 /* Only wake if the callback was for the same owner. */
6886 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6889 /* Make sure it's for the right inode */
6890 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6893 waiter->notified = true;
6896 /* override "private" so we can use default_wake_function */
6897 wait->private = waiter->task;
6898 ret = autoremove_wake_function(wait, mode, flags, key);
6899 wait->private = waiter;
6904 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6906 int status = -ERESTARTSYS;
6907 unsigned long flags;
6908 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6909 struct nfs_server *server = NFS_SERVER(state->inode);
6910 struct nfs_client *clp = server->nfs_client;
6911 wait_queue_head_t *q = &clp->cl_lock_waitq;
6912 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6913 .id = lsp->ls_seqid.owner_id,
6914 .s_dev = server->s_dev };
6915 struct nfs4_lock_waiter waiter = { .task = current,
6916 .inode = state->inode,
6918 .notified = false };
6919 wait_queue_entry_t wait;
6921 /* Don't bother with waitqueue if we don't expect a callback */
6922 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6923 return nfs4_retry_setlk_simple(state, cmd, request);
6926 wait.private = &waiter;
6927 wait.func = nfs4_wake_lock_waiter;
6928 add_wait_queue(q, &wait);
6930 while(!signalled()) {
6931 waiter.notified = false;
6932 status = nfs4_proc_setlk(state, cmd, request);
6933 if ((status != -EAGAIN) || IS_SETLK(cmd))
6936 status = -ERESTARTSYS;
6937 spin_lock_irqsave(&q->lock, flags);
6938 if (waiter.notified) {
6939 spin_unlock_irqrestore(&q->lock, flags);
6942 set_current_state(TASK_INTERRUPTIBLE);
6943 spin_unlock_irqrestore(&q->lock, flags);
6945 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6948 finish_wait(q, &wait);
6951 #else /* !CONFIG_NFS_V4_1 */
6953 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6955 return nfs4_retry_setlk_simple(state, cmd, request);
6960 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6962 struct nfs_open_context *ctx;
6963 struct nfs4_state *state;
6966 /* verify open state */
6967 ctx = nfs_file_open_context(filp);
6970 if (IS_GETLK(cmd)) {
6972 return nfs4_proc_getlk(state, F_GETLK, request);
6976 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6979 if (request->fl_type == F_UNLCK) {
6981 return nfs4_proc_unlck(state, cmd, request);
6988 if ((request->fl_flags & FL_POSIX) &&
6989 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6993 * Don't rely on the VFS having checked the file open mode,
6994 * since it won't do this for flock() locks.
6996 switch (request->fl_type) {
6998 if (!(filp->f_mode & FMODE_READ))
7002 if (!(filp->f_mode & FMODE_WRITE))
7006 status = nfs4_set_lock_state(state, request);
7010 return nfs4_retry_setlk(state, cmd, request);
7013 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7015 struct nfs_server *server = NFS_SERVER(state->inode);
7018 err = nfs4_set_lock_state(state, fl);
7021 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7022 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7025 struct nfs_release_lockowner_data {
7026 struct nfs4_lock_state *lsp;
7027 struct nfs_server *server;
7028 struct nfs_release_lockowner_args args;
7029 struct nfs_release_lockowner_res res;
7030 unsigned long timestamp;
7033 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7035 struct nfs_release_lockowner_data *data = calldata;
7036 struct nfs_server *server = data->server;
7037 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7038 &data->res.seq_res, task);
7039 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7040 data->timestamp = jiffies;
7043 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7045 struct nfs_release_lockowner_data *data = calldata;
7046 struct nfs_server *server = data->server;
7048 nfs40_sequence_done(task, &data->res.seq_res);
7050 switch (task->tk_status) {
7052 renew_lease(server, data->timestamp);
7054 case -NFS4ERR_STALE_CLIENTID:
7055 case -NFS4ERR_EXPIRED:
7056 nfs4_schedule_lease_recovery(server->nfs_client);
7058 case -NFS4ERR_LEASE_MOVED:
7059 case -NFS4ERR_DELAY:
7060 if (nfs4_async_handle_error(task, server,
7061 NULL, NULL) == -EAGAIN)
7062 rpc_restart_call_prepare(task);
7066 static void nfs4_release_lockowner_release(void *calldata)
7068 struct nfs_release_lockowner_data *data = calldata;
7069 nfs4_free_lock_state(data->server, data->lsp);
7073 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7074 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7075 .rpc_call_done = nfs4_release_lockowner_done,
7076 .rpc_release = nfs4_release_lockowner_release,
7080 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7082 struct nfs_release_lockowner_data *data;
7083 struct rpc_message msg = {
7084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7087 if (server->nfs_client->cl_mvops->minor_version != 0)
7090 data = kmalloc(sizeof(*data), GFP_NOFS);
7094 data->server = server;
7095 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7096 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7097 data->args.lock_owner.s_dev = server->s_dev;
7099 msg.rpc_argp = &data->args;
7100 msg.rpc_resp = &data->res;
7101 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7102 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7105 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7107 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7108 struct dentry *unused, struct inode *inode,
7109 const char *key, const void *buf,
7110 size_t buflen, int flags)
7112 return nfs4_proc_set_acl(inode, buf, buflen);
7115 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7116 struct dentry *unused, struct inode *inode,
7117 const char *key, void *buf, size_t buflen)
7119 return nfs4_proc_get_acl(inode, buf, buflen);
7122 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7124 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7127 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7129 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7130 struct dentry *unused, struct inode *inode,
7131 const char *key, const void *buf,
7132 size_t buflen, int flags)
7134 if (security_ismaclabel(key))
7135 return nfs4_set_security_label(inode, buf, buflen);
7140 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7141 struct dentry *unused, struct inode *inode,
7142 const char *key, void *buf, size_t buflen)
7144 if (security_ismaclabel(key))
7145 return nfs4_get_security_label(inode, buf, buflen);
7150 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7154 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7155 len = security_inode_listsecurity(inode, list, list_len);
7156 if (list_len && len > list_len)
7162 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7163 .prefix = XATTR_SECURITY_PREFIX,
7164 .get = nfs4_xattr_get_nfs4_label,
7165 .set = nfs4_xattr_set_nfs4_label,
7171 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7179 * nfs_fhget will use either the mounted_on_fileid or the fileid
7181 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7183 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7184 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7185 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7186 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7189 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7190 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7191 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7195 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7196 const struct qstr *name,
7197 struct nfs4_fs_locations *fs_locations,
7200 struct nfs_server *server = NFS_SERVER(dir);
7202 struct nfs4_fs_locations_arg args = {
7203 .dir_fh = NFS_FH(dir),
7208 struct nfs4_fs_locations_res res = {
7209 .fs_locations = fs_locations,
7211 struct rpc_message msg = {
7212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7218 dprintk("%s: start\n", __func__);
7220 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7221 bitmask[1] = nfs4_fattr_bitmap[1];
7223 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7224 * is not supported */
7225 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7226 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7228 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7230 nfs_fattr_init(&fs_locations->fattr);
7231 fs_locations->server = server;
7232 fs_locations->nlocations = 0;
7233 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7234 dprintk("%s: returned status = %d\n", __func__, status);
7238 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7239 const struct qstr *name,
7240 struct nfs4_fs_locations *fs_locations,
7243 struct nfs4_exception exception = { };
7246 err = _nfs4_proc_fs_locations(client, dir, name,
7247 fs_locations, page);
7248 trace_nfs4_get_fs_locations(dir, name, err);
7249 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7251 } while (exception.retry);
7256 * This operation also signals the server that this client is
7257 * performing migration recovery. The server can stop returning
7258 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7259 * appended to this compound to identify the client ID which is
7260 * performing recovery.
7262 static int _nfs40_proc_get_locations(struct inode *inode,
7263 struct nfs4_fs_locations *locations,
7264 struct page *page, const struct cred *cred)
7266 struct nfs_server *server = NFS_SERVER(inode);
7267 struct rpc_clnt *clnt = server->client;
7269 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7271 struct nfs4_fs_locations_arg args = {
7272 .clientid = server->nfs_client->cl_clientid,
7273 .fh = NFS_FH(inode),
7276 .migration = 1, /* skip LOOKUP */
7277 .renew = 1, /* append RENEW */
7279 struct nfs4_fs_locations_res res = {
7280 .fs_locations = locations,
7284 struct rpc_message msg = {
7285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7290 unsigned long now = jiffies;
7293 nfs_fattr_init(&locations->fattr);
7294 locations->server = server;
7295 locations->nlocations = 0;
7297 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7298 status = nfs4_call_sync_sequence(clnt, server, &msg,
7299 &args.seq_args, &res.seq_res);
7303 renew_lease(server, now);
7307 #ifdef CONFIG_NFS_V4_1
7310 * This operation also signals the server that this client is
7311 * performing migration recovery. The server can stop asserting
7312 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7313 * performing this operation is identified in the SEQUENCE
7314 * operation in this compound.
7316 * When the client supports GETATTR(fs_locations_info), it can
7317 * be plumbed in here.
7319 static int _nfs41_proc_get_locations(struct inode *inode,
7320 struct nfs4_fs_locations *locations,
7321 struct page *page, const struct cred *cred)
7323 struct nfs_server *server = NFS_SERVER(inode);
7324 struct rpc_clnt *clnt = server->client;
7326 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7328 struct nfs4_fs_locations_arg args = {
7329 .fh = NFS_FH(inode),
7332 .migration = 1, /* skip LOOKUP */
7334 struct nfs4_fs_locations_res res = {
7335 .fs_locations = locations,
7338 struct rpc_message msg = {
7339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7346 nfs_fattr_init(&locations->fattr);
7347 locations->server = server;
7348 locations->nlocations = 0;
7350 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7351 status = nfs4_call_sync_sequence(clnt, server, &msg,
7352 &args.seq_args, &res.seq_res);
7353 if (status == NFS4_OK &&
7354 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7355 status = -NFS4ERR_LEASE_MOVED;
7359 #endif /* CONFIG_NFS_V4_1 */
7362 * nfs4_proc_get_locations - discover locations for a migrated FSID
7363 * @inode: inode on FSID that is migrating
7364 * @locations: result of query
7366 * @cred: credential to use for this operation
7368 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7369 * operation failed, or a negative errno if a local error occurred.
7371 * On success, "locations" is filled in, but if the server has
7372 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7375 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7376 * from this client that require migration recovery.
7378 int nfs4_proc_get_locations(struct inode *inode,
7379 struct nfs4_fs_locations *locations,
7380 struct page *page, const struct cred *cred)
7382 struct nfs_server *server = NFS_SERVER(inode);
7383 struct nfs_client *clp = server->nfs_client;
7384 const struct nfs4_mig_recovery_ops *ops =
7385 clp->cl_mvops->mig_recovery_ops;
7386 struct nfs4_exception exception = { };
7389 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7390 (unsigned long long)server->fsid.major,
7391 (unsigned long long)server->fsid.minor,
7393 nfs_display_fhandle(NFS_FH(inode), __func__);
7396 status = ops->get_locations(inode, locations, page, cred);
7397 if (status != -NFS4ERR_DELAY)
7399 nfs4_handle_exception(server, status, &exception);
7400 } while (exception.retry);
7405 * This operation also signals the server that this client is
7406 * performing "lease moved" recovery. The server can stop
7407 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7408 * is appended to this compound to identify the client ID which is
7409 * performing recovery.
7411 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7413 struct nfs_server *server = NFS_SERVER(inode);
7414 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7415 struct rpc_clnt *clnt = server->client;
7416 struct nfs4_fsid_present_arg args = {
7417 .fh = NFS_FH(inode),
7418 .clientid = clp->cl_clientid,
7419 .renew = 1, /* append RENEW */
7421 struct nfs4_fsid_present_res res = {
7424 struct rpc_message msg = {
7425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7430 unsigned long now = jiffies;
7433 res.fh = nfs_alloc_fhandle();
7437 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7438 status = nfs4_call_sync_sequence(clnt, server, &msg,
7439 &args.seq_args, &res.seq_res);
7440 nfs_free_fhandle(res.fh);
7444 do_renew_lease(clp, now);
7448 #ifdef CONFIG_NFS_V4_1
7451 * This operation also signals the server that this client is
7452 * performing "lease moved" recovery. The server can stop asserting
7453 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7454 * this operation is identified in the SEQUENCE operation in this
7457 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7459 struct nfs_server *server = NFS_SERVER(inode);
7460 struct rpc_clnt *clnt = server->client;
7461 struct nfs4_fsid_present_arg args = {
7462 .fh = NFS_FH(inode),
7464 struct nfs4_fsid_present_res res = {
7466 struct rpc_message msg = {
7467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7474 res.fh = nfs_alloc_fhandle();
7478 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7479 status = nfs4_call_sync_sequence(clnt, server, &msg,
7480 &args.seq_args, &res.seq_res);
7481 nfs_free_fhandle(res.fh);
7482 if (status == NFS4_OK &&
7483 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7484 status = -NFS4ERR_LEASE_MOVED;
7488 #endif /* CONFIG_NFS_V4_1 */
7491 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7492 * @inode: inode on FSID to check
7493 * @cred: credential to use for this operation
7495 * Server indicates whether the FSID is present, moved, or not
7496 * recognized. This operation is necessary to clear a LEASE_MOVED
7497 * condition for this client ID.
7499 * Returns NFS4_OK if the FSID is present on this server,
7500 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7501 * NFS4ERR code if some error occurred on the server, or a
7502 * negative errno if a local failure occurred.
7504 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7506 struct nfs_server *server = NFS_SERVER(inode);
7507 struct nfs_client *clp = server->nfs_client;
7508 const struct nfs4_mig_recovery_ops *ops =
7509 clp->cl_mvops->mig_recovery_ops;
7510 struct nfs4_exception exception = { };
7513 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7514 (unsigned long long)server->fsid.major,
7515 (unsigned long long)server->fsid.minor,
7517 nfs_display_fhandle(NFS_FH(inode), __func__);
7520 status = ops->fsid_present(inode, cred);
7521 if (status != -NFS4ERR_DELAY)
7523 nfs4_handle_exception(server, status, &exception);
7524 } while (exception.retry);
7529 * If 'use_integrity' is true and the state managment nfs_client
7530 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7531 * and the machine credential as per RFC3530bis and RFC5661 Security
7532 * Considerations sections. Otherwise, just use the user cred with the
7533 * filesystem's rpc_client.
7535 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7538 struct nfs4_secinfo_arg args = {
7539 .dir_fh = NFS_FH(dir),
7542 struct nfs4_secinfo_res res = {
7545 struct rpc_message msg = {
7546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7550 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7551 const struct cred *cred = NULL;
7553 if (use_integrity) {
7554 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7555 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7556 msg.rpc_cred = cred;
7559 dprintk("NFS call secinfo %s\n", name->name);
7561 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7562 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7564 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7566 dprintk("NFS reply secinfo: %d\n", status);
7573 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7574 struct nfs4_secinfo_flavors *flavors)
7576 struct nfs4_exception exception = { };
7579 err = -NFS4ERR_WRONGSEC;
7581 /* try to use integrity protection with machine cred */
7582 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7583 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7586 * if unable to use integrity protection, or SECINFO with
7587 * integrity protection returns NFS4ERR_WRONGSEC (which is
7588 * disallowed by spec, but exists in deployed servers) use
7589 * the current filesystem's rpc_client and the user cred.
7591 if (err == -NFS4ERR_WRONGSEC)
7592 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7594 trace_nfs4_secinfo(dir, name, err);
7595 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7597 } while (exception.retry);
7601 #ifdef CONFIG_NFS_V4_1
7603 * Check the exchange flags returned by the server for invalid flags, having
7604 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7607 static int nfs4_check_cl_exchange_flags(u32 flags)
7609 if (flags & ~EXCHGID4_FLAG_MASK_R)
7611 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7612 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7614 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7618 return -NFS4ERR_INVAL;
7622 nfs41_same_server_scope(struct nfs41_server_scope *a,
7623 struct nfs41_server_scope *b)
7625 if (a->server_scope_sz != b->server_scope_sz)
7627 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7631 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7635 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7636 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7640 * nfs4_proc_bind_one_conn_to_session()
7642 * The 4.1 client currently uses the same TCP connection for the
7643 * fore and backchannel.
7646 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7647 struct rpc_xprt *xprt,
7648 struct nfs_client *clp,
7649 const struct cred *cred)
7652 struct nfs41_bind_conn_to_session_args args = {
7654 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7656 struct nfs41_bind_conn_to_session_res res;
7657 struct rpc_message msg = {
7659 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7664 struct rpc_task_setup task_setup_data = {
7667 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7668 .rpc_message = &msg,
7669 .flags = RPC_TASK_TIMEOUT,
7671 struct rpc_task *task;
7673 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7674 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7675 args.dir = NFS4_CDFC4_FORE;
7677 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7678 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7679 args.dir = NFS4_CDFC4_FORE;
7681 task = rpc_run_task(&task_setup_data);
7682 if (!IS_ERR(task)) {
7683 status = task->tk_status;
7686 status = PTR_ERR(task);
7687 trace_nfs4_bind_conn_to_session(clp, status);
7689 if (memcmp(res.sessionid.data,
7690 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7691 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7694 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7695 dprintk("NFS: %s: Unexpected direction from server\n",
7699 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7700 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7709 struct rpc_bind_conn_calldata {
7710 struct nfs_client *clp;
7711 const struct cred *cred;
7715 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7716 struct rpc_xprt *xprt,
7719 struct rpc_bind_conn_calldata *p = calldata;
7721 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7724 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7726 struct rpc_bind_conn_calldata data = {
7730 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7731 nfs4_proc_bind_conn_to_session_callback, &data);
7735 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7736 * and operations we'd like to see to enable certain features in the allow map
7738 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7739 .how = SP4_MACH_CRED,
7740 .enforce.u.words = {
7741 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7742 1 << (OP_EXCHANGE_ID - 32) |
7743 1 << (OP_CREATE_SESSION - 32) |
7744 1 << (OP_DESTROY_SESSION - 32) |
7745 1 << (OP_DESTROY_CLIENTID - 32)
7748 [0] = 1 << (OP_CLOSE) |
7749 1 << (OP_OPEN_DOWNGRADE) |
7751 1 << (OP_DELEGRETURN) |
7753 [1] = 1 << (OP_SECINFO - 32) |
7754 1 << (OP_SECINFO_NO_NAME - 32) |
7755 1 << (OP_LAYOUTRETURN - 32) |
7756 1 << (OP_TEST_STATEID - 32) |
7757 1 << (OP_FREE_STATEID - 32) |
7758 1 << (OP_WRITE - 32)
7763 * Select the state protection mode for client `clp' given the server results
7764 * from exchange_id in `sp'.
7766 * Returns 0 on success, negative errno otherwise.
7768 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7769 struct nfs41_state_protection *sp)
7771 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7772 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7773 1 << (OP_EXCHANGE_ID - 32) |
7774 1 << (OP_CREATE_SESSION - 32) |
7775 1 << (OP_DESTROY_SESSION - 32) |
7776 1 << (OP_DESTROY_CLIENTID - 32)
7778 unsigned long flags = 0;
7782 if (sp->how == SP4_MACH_CRED) {
7783 /* Print state protect result */
7784 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7785 for (i = 0; i <= LAST_NFS4_OP; i++) {
7786 if (test_bit(i, sp->enforce.u.longs))
7787 dfprintk(MOUNT, " enforce op %d\n", i);
7788 if (test_bit(i, sp->allow.u.longs))
7789 dfprintk(MOUNT, " allow op %d\n", i);
7792 /* make sure nothing is on enforce list that isn't supported */
7793 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7794 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7795 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7802 * Minimal mode - state operations are allowed to use machine
7803 * credential. Note this already happens by default, so the
7804 * client doesn't have to do anything more than the negotiation.
7806 * NOTE: we don't care if EXCHANGE_ID is in the list -
7807 * we're already using the machine cred for exchange_id
7808 * and will never use a different cred.
7810 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7811 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7812 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7813 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7814 dfprintk(MOUNT, "sp4_mach_cred:\n");
7815 dfprintk(MOUNT, " minimal mode enabled\n");
7816 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7818 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7823 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7824 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7825 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7826 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7827 dfprintk(MOUNT, " cleanup mode enabled\n");
7828 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7831 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7832 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7833 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7836 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7837 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7838 dfprintk(MOUNT, " secinfo mode enabled\n");
7839 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7842 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7843 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7844 dfprintk(MOUNT, " stateid mode enabled\n");
7845 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7848 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7849 dfprintk(MOUNT, " write mode enabled\n");
7850 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7853 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7854 dfprintk(MOUNT, " commit mode enabled\n");
7855 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7859 clp->cl_sp4_flags = flags;
7863 struct nfs41_exchange_id_data {
7864 struct nfs41_exchange_id_res res;
7865 struct nfs41_exchange_id_args args;
7868 static void nfs4_exchange_id_release(void *data)
7870 struct nfs41_exchange_id_data *cdata =
7871 (struct nfs41_exchange_id_data *)data;
7873 nfs_put_client(cdata->args.client);
7874 kfree(cdata->res.impl_id);
7875 kfree(cdata->res.server_scope);
7876 kfree(cdata->res.server_owner);
7880 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7881 .rpc_release = nfs4_exchange_id_release,
7885 * _nfs4_proc_exchange_id()
7887 * Wrapper for EXCHANGE_ID operation.
7889 static struct rpc_task *
7890 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
7891 u32 sp4_how, struct rpc_xprt *xprt)
7893 struct rpc_message msg = {
7894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7897 struct rpc_task_setup task_setup_data = {
7898 .rpc_client = clp->cl_rpcclient,
7899 .callback_ops = &nfs4_exchange_id_call_ops,
7900 .rpc_message = &msg,
7901 .flags = RPC_TASK_TIMEOUT,
7903 struct nfs41_exchange_id_data *calldata;
7906 if (!refcount_inc_not_zero(&clp->cl_count))
7907 return ERR_PTR(-EIO);
7910 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7914 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7916 status = nfs4_init_uniform_client_string(clp);
7920 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7923 if (unlikely(calldata->res.server_owner == NULL))
7926 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7928 if (unlikely(calldata->res.server_scope == NULL))
7929 goto out_server_owner;
7931 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7932 if (unlikely(calldata->res.impl_id == NULL))
7933 goto out_server_scope;
7937 calldata->args.state_protect.how = SP4_NONE;
7941 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7951 task_setup_data.rpc_xprt = xprt;
7952 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7953 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7954 sizeof(calldata->args.verifier.data));
7956 calldata->args.client = clp;
7957 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7958 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7959 #ifdef CONFIG_NFS_V4_1_MIGRATION
7960 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7962 msg.rpc_argp = &calldata->args;
7963 msg.rpc_resp = &calldata->res;
7964 task_setup_data.callback_data = calldata;
7966 return rpc_run_task(&task_setup_data);
7969 kfree(calldata->res.impl_id);
7971 kfree(calldata->res.server_scope);
7973 kfree(calldata->res.server_owner);
7977 nfs_put_client(clp);
7978 return ERR_PTR(status);
7982 * _nfs4_proc_exchange_id()
7984 * Wrapper for EXCHANGE_ID operation.
7986 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
7989 struct rpc_task *task;
7990 struct nfs41_exchange_id_args *argp;
7991 struct nfs41_exchange_id_res *resp;
7994 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7996 return PTR_ERR(task);
7998 argp = task->tk_msg.rpc_argp;
7999 resp = task->tk_msg.rpc_resp;
8000 status = task->tk_status;
8004 status = nfs4_check_cl_exchange_flags(resp->flags);
8008 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8012 clp->cl_clientid = resp->clientid;
8013 clp->cl_exchange_flags = resp->flags;
8014 clp->cl_seqid = resp->seqid;
8015 /* Client ID is not confirmed */
8016 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8017 clear_bit(NFS4_SESSION_ESTABLISHED,
8018 &clp->cl_session->session_state);
8020 if (clp->cl_serverscope != NULL &&
8021 !nfs41_same_server_scope(clp->cl_serverscope,
8022 resp->server_scope)) {
8023 dprintk("%s: server_scope mismatch detected\n",
8025 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8028 swap(clp->cl_serverowner, resp->server_owner);
8029 swap(clp->cl_serverscope, resp->server_scope);
8030 swap(clp->cl_implid, resp->impl_id);
8032 /* Save the EXCHANGE_ID verifier session trunk tests */
8033 memcpy(clp->cl_confirm.data, argp->verifier.data,
8034 sizeof(clp->cl_confirm.data));
8036 trace_nfs4_exchange_id(clp, status);
8042 * nfs4_proc_exchange_id()
8044 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8046 * Since the clientid has expired, all compounds using sessions
8047 * associated with the stale clientid will be returning
8048 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8049 * be in some phase of session reset.
8051 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8053 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8055 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8058 /* try SP4_MACH_CRED if krb5i/p */
8059 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8060 authflavor == RPC_AUTH_GSS_KRB5P) {
8061 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8067 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8071 * nfs4_test_session_trunk
8073 * This is an add_xprt_test() test function called from
8074 * rpc_clnt_setup_test_and_add_xprt.
8076 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8077 * and is dereferrenced in nfs4_exchange_id_release
8079 * Upon success, add the new transport to the rpc_clnt
8081 * @clnt: struct rpc_clnt to get new transport
8082 * @xprt: the rpc_xprt to test
8083 * @data: call data for _nfs4_proc_exchange_id.
8085 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8088 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8089 struct rpc_task *task;
8094 dprintk("--> %s try %s\n", __func__,
8095 xprt->address_strings[RPC_DISPLAY_ADDR]);
8097 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8099 /* Test connection for session trunking. Async exchange_id call */
8100 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8104 status = task->tk_status;
8106 status = nfs4_detect_session_trunking(adata->clp,
8107 task->tk_msg.rpc_resp, xprt);
8110 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8114 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8116 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8117 const struct cred *cred)
8119 struct rpc_message msg = {
8120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8126 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8127 trace_nfs4_destroy_clientid(clp, status);
8129 dprintk("NFS: Got error %d from the server %s on "
8130 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8134 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8135 const struct cred *cred)
8140 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8141 ret = _nfs4_proc_destroy_clientid(clp, cred);
8143 case -NFS4ERR_DELAY:
8144 case -NFS4ERR_CLIENTID_BUSY:
8154 int nfs4_destroy_clientid(struct nfs_client *clp)
8156 const struct cred *cred;
8159 if (clp->cl_mvops->minor_version < 1)
8161 if (clp->cl_exchange_flags == 0)
8163 if (clp->cl_preserve_clid)
8165 cred = nfs4_get_clid_cred(clp);
8166 ret = nfs4_proc_destroy_clientid(clp, cred);
8170 case -NFS4ERR_STALE_CLIENTID:
8171 clp->cl_exchange_flags = 0;
8177 struct nfs4_get_lease_time_data {
8178 struct nfs4_get_lease_time_args *args;
8179 struct nfs4_get_lease_time_res *res;
8180 struct nfs_client *clp;
8183 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8186 struct nfs4_get_lease_time_data *data =
8187 (struct nfs4_get_lease_time_data *)calldata;
8189 dprintk("--> %s\n", __func__);
8190 /* just setup sequence, do not trigger session recovery
8191 since we're invoked within one */
8192 nfs4_setup_sequence(data->clp,
8193 &data->args->la_seq_args,
8194 &data->res->lr_seq_res,
8196 dprintk("<-- %s\n", __func__);
8200 * Called from nfs4_state_manager thread for session setup, so don't recover
8201 * from sequence operation or clientid errors.
8203 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8205 struct nfs4_get_lease_time_data *data =
8206 (struct nfs4_get_lease_time_data *)calldata;
8208 dprintk("--> %s\n", __func__);
8209 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8211 switch (task->tk_status) {
8212 case -NFS4ERR_DELAY:
8213 case -NFS4ERR_GRACE:
8214 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8215 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8216 task->tk_status = 0;
8218 case -NFS4ERR_RETRY_UNCACHED_REP:
8219 rpc_restart_call_prepare(task);
8222 dprintk("<-- %s\n", __func__);
8225 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8226 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8227 .rpc_call_done = nfs4_get_lease_time_done,
8230 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8232 struct rpc_task *task;
8233 struct nfs4_get_lease_time_args args;
8234 struct nfs4_get_lease_time_res res = {
8235 .lr_fsinfo = fsinfo,
8237 struct nfs4_get_lease_time_data data = {
8242 struct rpc_message msg = {
8243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8247 struct rpc_task_setup task_setup = {
8248 .rpc_client = clp->cl_rpcclient,
8249 .rpc_message = &msg,
8250 .callback_ops = &nfs4_get_lease_time_ops,
8251 .callback_data = &data,
8252 .flags = RPC_TASK_TIMEOUT,
8256 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8257 task = rpc_run_task(&task_setup);
8260 return PTR_ERR(task);
8262 status = task->tk_status;
8268 * Initialize the values to be used by the client in CREATE_SESSION
8269 * If nfs4_init_session set the fore channel request and response sizes,
8272 * Set the back channel max_resp_sz_cached to zero to force the client to
8273 * always set csa_cachethis to FALSE because the current implementation
8274 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8276 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8277 struct rpc_clnt *clnt)
8279 unsigned int max_rqst_sz, max_resp_sz;
8280 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8282 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8283 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8285 /* Fore channel attributes */
8286 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8287 args->fc_attrs.max_resp_sz = max_resp_sz;
8288 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8289 args->fc_attrs.max_reqs = max_session_slots;
8291 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8292 "max_ops=%u max_reqs=%u\n",
8294 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8295 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8297 /* Back channel attributes */
8298 args->bc_attrs.max_rqst_sz = max_bc_payload;
8299 args->bc_attrs.max_resp_sz = max_bc_payload;
8300 args->bc_attrs.max_resp_sz_cached = 0;
8301 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8302 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8304 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8305 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8307 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8308 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8309 args->bc_attrs.max_reqs);
8312 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8313 struct nfs41_create_session_res *res)
8315 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8316 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8318 if (rcvd->max_resp_sz > sent->max_resp_sz)
8321 * Our requested max_ops is the minimum we need; we're not
8322 * prepared to break up compounds into smaller pieces than that.
8323 * So, no point even trying to continue if the server won't
8326 if (rcvd->max_ops < sent->max_ops)
8328 if (rcvd->max_reqs == 0)
8330 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8331 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8335 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8336 struct nfs41_create_session_res *res)
8338 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8339 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8341 if (!(res->flags & SESSION4_BACK_CHAN))
8343 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8345 if (rcvd->max_resp_sz < sent->max_resp_sz)
8347 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8349 if (rcvd->max_ops > sent->max_ops)
8351 if (rcvd->max_reqs > sent->max_reqs)
8357 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8358 struct nfs41_create_session_res *res)
8362 ret = nfs4_verify_fore_channel_attrs(args, res);
8365 return nfs4_verify_back_channel_attrs(args, res);
8368 static void nfs4_update_session(struct nfs4_session *session,
8369 struct nfs41_create_session_res *res)
8371 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8372 /* Mark client id and session as being confirmed */
8373 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8374 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8375 session->flags = res->flags;
8376 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8377 if (res->flags & SESSION4_BACK_CHAN)
8378 memcpy(&session->bc_attrs, &res->bc_attrs,
8379 sizeof(session->bc_attrs));
8382 static int _nfs4_proc_create_session(struct nfs_client *clp,
8383 const struct cred *cred)
8385 struct nfs4_session *session = clp->cl_session;
8386 struct nfs41_create_session_args args = {
8388 .clientid = clp->cl_clientid,
8389 .seqid = clp->cl_seqid,
8390 .cb_program = NFS4_CALLBACK,
8392 struct nfs41_create_session_res res;
8394 struct rpc_message msg = {
8395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8402 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8403 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8405 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8406 trace_nfs4_create_session(clp, status);
8409 case -NFS4ERR_STALE_CLIENTID:
8410 case -NFS4ERR_DELAY:
8419 /* Verify the session's negotiated channel_attrs values */
8420 status = nfs4_verify_channel_attrs(&args, &res);
8421 /* Increment the clientid slot sequence id */
8424 nfs4_update_session(session, &res);
8431 * Issues a CREATE_SESSION operation to the server.
8432 * It is the responsibility of the caller to verify the session is
8433 * expired before calling this routine.
8435 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8439 struct nfs4_session *session = clp->cl_session;
8441 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8443 status = _nfs4_proc_create_session(clp, cred);
8447 /* Init or reset the session slot tables */
8448 status = nfs4_setup_session_slot_tables(session);
8449 dprintk("slot table setup returned %d\n", status);
8453 ptr = (unsigned *)&session->sess_id.data[0];
8454 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8455 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8457 dprintk("<-- %s\n", __func__);
8462 * Issue the over-the-wire RPC DESTROY_SESSION.
8463 * The caller must serialize access to this routine.
8465 int nfs4_proc_destroy_session(struct nfs4_session *session,
8466 const struct cred *cred)
8468 struct rpc_message msg = {
8469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8470 .rpc_argp = session,
8475 dprintk("--> nfs4_proc_destroy_session\n");
8477 /* session is still being setup */
8478 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8481 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8482 trace_nfs4_destroy_session(session->clp, status);
8485 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8486 "Session has been destroyed regardless...\n", status);
8488 dprintk("<-- nfs4_proc_destroy_session\n");
8493 * Renew the cl_session lease.
8495 struct nfs4_sequence_data {
8496 struct nfs_client *clp;
8497 struct nfs4_sequence_args args;
8498 struct nfs4_sequence_res res;
8501 static void nfs41_sequence_release(void *data)
8503 struct nfs4_sequence_data *calldata = data;
8504 struct nfs_client *clp = calldata->clp;
8506 if (refcount_read(&clp->cl_count) > 1)
8507 nfs4_schedule_state_renewal(clp);
8508 nfs_put_client(clp);
8512 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8514 switch(task->tk_status) {
8515 case -NFS4ERR_DELAY:
8516 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8519 nfs4_schedule_lease_recovery(clp);
8524 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8526 struct nfs4_sequence_data *calldata = data;
8527 struct nfs_client *clp = calldata->clp;
8529 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8532 trace_nfs4_sequence(clp, task->tk_status);
8533 if (task->tk_status < 0) {
8534 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8535 if (refcount_read(&clp->cl_count) == 1)
8538 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8539 rpc_restart_call_prepare(task);
8543 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8545 dprintk("<-- %s\n", __func__);
8548 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8550 struct nfs4_sequence_data *calldata = data;
8551 struct nfs_client *clp = calldata->clp;
8552 struct nfs4_sequence_args *args;
8553 struct nfs4_sequence_res *res;
8555 args = task->tk_msg.rpc_argp;
8556 res = task->tk_msg.rpc_resp;
8558 nfs4_setup_sequence(clp, args, res, task);
8561 static const struct rpc_call_ops nfs41_sequence_ops = {
8562 .rpc_call_done = nfs41_sequence_call_done,
8563 .rpc_call_prepare = nfs41_sequence_prepare,
8564 .rpc_release = nfs41_sequence_release,
8567 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8568 const struct cred *cred,
8569 struct nfs4_slot *slot,
8572 struct nfs4_sequence_data *calldata;
8573 struct rpc_message msg = {
8574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8577 struct rpc_task_setup task_setup_data = {
8578 .rpc_client = clp->cl_rpcclient,
8579 .rpc_message = &msg,
8580 .callback_ops = &nfs41_sequence_ops,
8581 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8583 struct rpc_task *ret;
8585 ret = ERR_PTR(-EIO);
8586 if (!refcount_inc_not_zero(&clp->cl_count))
8589 ret = ERR_PTR(-ENOMEM);
8590 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8591 if (calldata == NULL)
8593 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8594 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8595 msg.rpc_argp = &calldata->args;
8596 msg.rpc_resp = &calldata->res;
8597 calldata->clp = clp;
8598 task_setup_data.callback_data = calldata;
8600 ret = rpc_run_task(&task_setup_data);
8605 nfs_put_client(clp);
8607 nfs41_release_slot(slot);
8611 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8613 struct rpc_task *task;
8616 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8618 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8620 ret = PTR_ERR(task);
8622 rpc_put_task_async(task);
8623 dprintk("<-- %s status=%d\n", __func__, ret);
8627 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8629 struct rpc_task *task;
8632 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8634 ret = PTR_ERR(task);
8637 ret = rpc_wait_for_completion_task(task);
8639 ret = task->tk_status;
8642 dprintk("<-- %s status=%d\n", __func__, ret);
8646 struct nfs4_reclaim_complete_data {
8647 struct nfs_client *clp;
8648 struct nfs41_reclaim_complete_args arg;
8649 struct nfs41_reclaim_complete_res res;
8652 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8654 struct nfs4_reclaim_complete_data *calldata = data;
8656 nfs4_setup_sequence(calldata->clp,
8657 &calldata->arg.seq_args,
8658 &calldata->res.seq_res,
8662 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8664 switch(task->tk_status) {
8666 wake_up_all(&clp->cl_lock_waitq);
8668 case -NFS4ERR_COMPLETE_ALREADY:
8669 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8671 case -NFS4ERR_DELAY:
8672 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8674 case -NFS4ERR_RETRY_UNCACHED_REP:
8676 case -NFS4ERR_BADSESSION:
8677 case -NFS4ERR_DEADSESSION:
8678 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8679 nfs4_schedule_session_recovery(clp->cl_session,
8683 nfs4_schedule_lease_recovery(clp);
8688 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8690 struct nfs4_reclaim_complete_data *calldata = data;
8691 struct nfs_client *clp = calldata->clp;
8692 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8694 dprintk("--> %s\n", __func__);
8695 if (!nfs41_sequence_done(task, res))
8698 trace_nfs4_reclaim_complete(clp, task->tk_status);
8699 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8700 rpc_restart_call_prepare(task);
8703 dprintk("<-- %s\n", __func__);
8706 static void nfs4_free_reclaim_complete_data(void *data)
8708 struct nfs4_reclaim_complete_data *calldata = data;
8713 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8714 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8715 .rpc_call_done = nfs4_reclaim_complete_done,
8716 .rpc_release = nfs4_free_reclaim_complete_data,
8720 * Issue a global reclaim complete.
8722 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8723 const struct cred *cred)
8725 struct nfs4_reclaim_complete_data *calldata;
8726 struct rpc_task *task;
8727 struct rpc_message msg = {
8728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8731 struct rpc_task_setup task_setup_data = {
8732 .rpc_client = clp->cl_rpcclient,
8733 .rpc_message = &msg,
8734 .callback_ops = &nfs4_reclaim_complete_call_ops,
8735 .flags = RPC_TASK_ASYNC,
8737 int status = -ENOMEM;
8739 dprintk("--> %s\n", __func__);
8740 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8741 if (calldata == NULL)
8743 calldata->clp = clp;
8744 calldata->arg.one_fs = 0;
8746 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8747 msg.rpc_argp = &calldata->arg;
8748 msg.rpc_resp = &calldata->res;
8749 task_setup_data.callback_data = calldata;
8750 task = rpc_run_task(&task_setup_data);
8752 status = PTR_ERR(task);
8755 status = rpc_wait_for_completion_task(task);
8757 status = task->tk_status;
8760 dprintk("<-- %s status=%d\n", __func__, status);
8765 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8767 struct nfs4_layoutget *lgp = calldata;
8768 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8770 dprintk("--> %s\n", __func__);
8771 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8772 &lgp->res.seq_res, task);
8773 dprintk("<-- %s\n", __func__);
8776 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8778 struct nfs4_layoutget *lgp = calldata;
8780 dprintk("--> %s\n", __func__);
8781 nfs41_sequence_process(task, &lgp->res.seq_res);
8782 dprintk("<-- %s\n", __func__);
8786 nfs4_layoutget_handle_exception(struct rpc_task *task,
8787 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8789 struct inode *inode = lgp->args.inode;
8790 struct nfs_server *server = NFS_SERVER(inode);
8791 struct pnfs_layout_hdr *lo;
8792 int nfs4err = task->tk_status;
8793 int err, status = 0;
8796 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8798 nfs4_sequence_free_slot(&lgp->res.seq_res);
8805 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8806 * on the file. set tk_status to -ENODATA to tell upper layer to
8809 case -NFS4ERR_LAYOUTUNAVAILABLE:
8813 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8814 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8816 case -NFS4ERR_BADLAYOUT:
8817 status = -EOVERFLOW;
8820 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8821 * (or clients) writing to the same RAID stripe except when
8822 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8824 * Treat it like we would RECALLCONFLICT -- we retry for a little
8825 * while, and then eventually give up.
8827 case -NFS4ERR_LAYOUTTRYLATER:
8828 if (lgp->args.minlength == 0) {
8829 status = -EOVERFLOW;
8834 case -NFS4ERR_RECALLCONFLICT:
8835 status = -ERECALLCONFLICT;
8837 case -NFS4ERR_DELEG_REVOKED:
8838 case -NFS4ERR_ADMIN_REVOKED:
8839 case -NFS4ERR_EXPIRED:
8840 case -NFS4ERR_BAD_STATEID:
8841 exception->timeout = 0;
8842 spin_lock(&inode->i_lock);
8843 lo = NFS_I(inode)->layout;
8844 /* If the open stateid was bad, then recover it. */
8845 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8846 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8847 spin_unlock(&inode->i_lock);
8848 exception->state = lgp->args.ctx->state;
8849 exception->stateid = &lgp->args.stateid;
8854 * Mark the bad layout state as invalid, then retry
8856 pnfs_mark_layout_stateid_invalid(lo, &head);
8857 spin_unlock(&inode->i_lock);
8858 nfs_commit_inode(inode, 0);
8859 pnfs_free_lseg_list(&head);
8864 err = nfs4_handle_exception(server, nfs4err, exception);
8866 if (exception->retry)
8872 dprintk("<-- %s\n", __func__);
8876 size_t max_response_pages(struct nfs_server *server)
8878 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8879 return nfs_page_array_len(0, max_resp_sz);
8882 static void nfs4_layoutget_release(void *calldata)
8884 struct nfs4_layoutget *lgp = calldata;
8886 dprintk("--> %s\n", __func__);
8887 nfs4_sequence_free_slot(&lgp->res.seq_res);
8888 pnfs_layoutget_free(lgp);
8889 dprintk("<-- %s\n", __func__);
8892 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8893 .rpc_call_prepare = nfs4_layoutget_prepare,
8894 .rpc_call_done = nfs4_layoutget_done,
8895 .rpc_release = nfs4_layoutget_release,
8898 struct pnfs_layout_segment *
8899 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
8901 struct inode *inode = lgp->args.inode;
8902 struct nfs_server *server = NFS_SERVER(inode);
8903 struct rpc_task *task;
8904 struct rpc_message msg = {
8905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8906 .rpc_argp = &lgp->args,
8907 .rpc_resp = &lgp->res,
8908 .rpc_cred = lgp->cred,
8910 struct rpc_task_setup task_setup_data = {
8911 .rpc_client = server->client,
8912 .rpc_message = &msg,
8913 .callback_ops = &nfs4_layoutget_call_ops,
8914 .callback_data = lgp,
8915 .flags = RPC_TASK_ASYNC,
8917 struct pnfs_layout_segment *lseg = NULL;
8918 struct nfs4_exception exception = {
8920 .timeout = *timeout,
8924 dprintk("--> %s\n", __func__);
8926 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8927 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8929 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
8931 task = rpc_run_task(&task_setup_data);
8933 return ERR_CAST(task);
8934 status = rpc_wait_for_completion_task(task);
8938 if (task->tk_status < 0) {
8939 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8940 *timeout = exception.timeout;
8941 } else if (lgp->res.layoutp->len == 0) {
8943 *timeout = nfs4_update_delay(&exception.timeout);
8945 lseg = pnfs_layout_process(lgp);
8947 trace_nfs4_layoutget(lgp->args.ctx,
8954 dprintk("<-- %s status=%d\n", __func__, status);
8956 return ERR_PTR(status);
8961 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8963 struct nfs4_layoutreturn *lrp = calldata;
8965 dprintk("--> %s\n", __func__);
8966 nfs4_setup_sequence(lrp->clp,
8967 &lrp->args.seq_args,
8970 if (!pnfs_layout_is_valid(lrp->args.layout))
8974 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8976 struct nfs4_layoutreturn *lrp = calldata;
8977 struct nfs_server *server;
8979 dprintk("--> %s\n", __func__);
8981 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8984 server = NFS_SERVER(lrp->args.inode);
8985 switch (task->tk_status) {
8986 case -NFS4ERR_OLD_STATEID:
8987 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
8993 task->tk_status = 0;
8997 case -NFS4ERR_DELAY:
8998 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9002 dprintk("<-- %s\n", __func__);
9005 task->tk_status = 0;
9006 nfs4_sequence_free_slot(&lrp->res.seq_res);
9007 rpc_restart_call_prepare(task);
9010 static void nfs4_layoutreturn_release(void *calldata)
9012 struct nfs4_layoutreturn *lrp = calldata;
9013 struct pnfs_layout_hdr *lo = lrp->args.layout;
9015 dprintk("--> %s\n", __func__);
9016 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9017 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9018 nfs4_sequence_free_slot(&lrp->res.seq_res);
9019 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9020 lrp->ld_private.ops->free(&lrp->ld_private);
9021 pnfs_put_layout_hdr(lrp->args.layout);
9022 nfs_iput_and_deactive(lrp->inode);
9024 dprintk("<-- %s\n", __func__);
9027 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9028 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9029 .rpc_call_done = nfs4_layoutreturn_done,
9030 .rpc_release = nfs4_layoutreturn_release,
9033 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9035 struct rpc_task *task;
9036 struct rpc_message msg = {
9037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9038 .rpc_argp = &lrp->args,
9039 .rpc_resp = &lrp->res,
9040 .rpc_cred = lrp->cred,
9042 struct rpc_task_setup task_setup_data = {
9043 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9044 .rpc_message = &msg,
9045 .callback_ops = &nfs4_layoutreturn_call_ops,
9046 .callback_data = lrp,
9050 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9051 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9052 &task_setup_data.rpc_client, &msg);
9054 dprintk("--> %s\n", __func__);
9056 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9058 nfs4_layoutreturn_release(lrp);
9061 task_setup_data.flags |= RPC_TASK_ASYNC;
9063 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9064 task = rpc_run_task(&task_setup_data);
9066 return PTR_ERR(task);
9068 status = task->tk_status;
9069 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9070 dprintk("<-- %s status=%d\n", __func__, status);
9076 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9077 struct pnfs_device *pdev,
9078 const struct cred *cred)
9080 struct nfs4_getdeviceinfo_args args = {
9082 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9083 NOTIFY_DEVICEID4_DELETE,
9085 struct nfs4_getdeviceinfo_res res = {
9088 struct rpc_message msg = {
9089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9096 dprintk("--> %s\n", __func__);
9097 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9098 if (res.notification & ~args.notify_types)
9099 dprintk("%s: unsupported notification\n", __func__);
9100 if (res.notification != args.notify_types)
9103 dprintk("<-- %s status=%d\n", __func__, status);
9108 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9109 struct pnfs_device *pdev,
9110 const struct cred *cred)
9112 struct nfs4_exception exception = { };
9116 err = nfs4_handle_exception(server,
9117 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9119 } while (exception.retry);
9122 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9124 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9126 struct nfs4_layoutcommit_data *data = calldata;
9127 struct nfs_server *server = NFS_SERVER(data->args.inode);
9129 nfs4_setup_sequence(server->nfs_client,
9130 &data->args.seq_args,
9136 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9138 struct nfs4_layoutcommit_data *data = calldata;
9139 struct nfs_server *server = NFS_SERVER(data->args.inode);
9141 if (!nfs41_sequence_done(task, &data->res.seq_res))
9144 switch (task->tk_status) { /* Just ignore these failures */
9145 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9146 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9147 case -NFS4ERR_BADLAYOUT: /* no layout */
9148 case -NFS4ERR_GRACE: /* loca_recalim always false */
9149 task->tk_status = 0;
9153 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9154 rpc_restart_call_prepare(task);
9160 static void nfs4_layoutcommit_release(void *calldata)
9162 struct nfs4_layoutcommit_data *data = calldata;
9164 pnfs_cleanup_layoutcommit(data);
9165 nfs_post_op_update_inode_force_wcc(data->args.inode,
9167 put_cred(data->cred);
9168 nfs_iput_and_deactive(data->inode);
9172 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9173 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9174 .rpc_call_done = nfs4_layoutcommit_done,
9175 .rpc_release = nfs4_layoutcommit_release,
9179 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9181 struct rpc_message msg = {
9182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9183 .rpc_argp = &data->args,
9184 .rpc_resp = &data->res,
9185 .rpc_cred = data->cred,
9187 struct rpc_task_setup task_setup_data = {
9188 .task = &data->task,
9189 .rpc_client = NFS_CLIENT(data->args.inode),
9190 .rpc_message = &msg,
9191 .callback_ops = &nfs4_layoutcommit_ops,
9192 .callback_data = data,
9194 struct rpc_task *task;
9197 dprintk("NFS: initiating layoutcommit call. sync %d "
9198 "lbw: %llu inode %lu\n", sync,
9199 data->args.lastbytewritten,
9200 data->args.inode->i_ino);
9203 data->inode = nfs_igrab_and_active(data->args.inode);
9204 if (data->inode == NULL) {
9205 nfs4_layoutcommit_release(data);
9208 task_setup_data.flags = RPC_TASK_ASYNC;
9210 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9211 task = rpc_run_task(&task_setup_data);
9213 return PTR_ERR(task);
9215 status = task->tk_status;
9216 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9217 dprintk("%s: status %d\n", __func__, status);
9223 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9224 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9227 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9228 struct nfs_fsinfo *info,
9229 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9231 struct nfs41_secinfo_no_name_args args = {
9232 .style = SECINFO_STYLE_CURRENT_FH,
9234 struct nfs4_secinfo_res res = {
9237 struct rpc_message msg = {
9238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9242 struct rpc_clnt *clnt = server->client;
9243 const struct cred *cred = NULL;
9246 if (use_integrity) {
9247 clnt = server->nfs_client->cl_rpcclient;
9248 cred = nfs4_get_clid_cred(server->nfs_client);
9249 msg.rpc_cred = cred;
9252 dprintk("--> %s\n", __func__);
9253 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9255 dprintk("<-- %s status=%d\n", __func__, status);
9263 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9264 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9266 struct nfs4_exception exception = { };
9269 /* first try using integrity protection */
9270 err = -NFS4ERR_WRONGSEC;
9272 /* try to use integrity protection with machine cred */
9273 if (_nfs4_is_integrity_protected(server->nfs_client))
9274 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9278 * if unable to use integrity protection, or SECINFO with
9279 * integrity protection returns NFS4ERR_WRONGSEC (which is
9280 * disallowed by spec, but exists in deployed servers) use
9281 * the current filesystem's rpc_client and the user cred.
9283 if (err == -NFS4ERR_WRONGSEC)
9284 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9289 case -NFS4ERR_WRONGSEC:
9293 err = nfs4_handle_exception(server, err, &exception);
9295 } while (exception.retry);
9301 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9302 struct nfs_fsinfo *info)
9306 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9307 struct nfs4_secinfo_flavors *flavors;
9308 struct nfs4_secinfo4 *secinfo;
9311 page = alloc_page(GFP_KERNEL);
9317 flavors = page_address(page);
9318 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9321 * Fall back on "guess and check" method if
9322 * the server doesn't support SECINFO_NO_NAME
9324 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9325 err = nfs4_find_root_sec(server, fhandle, info);
9331 for (i = 0; i < flavors->num_flavors; i++) {
9332 secinfo = &flavors->flavors[i];
9334 switch (secinfo->flavor) {
9338 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9339 &secinfo->flavor_info);
9342 flavor = RPC_AUTH_MAXFLAVOR;
9346 if (!nfs_auth_info_match(&server->auth_info, flavor))
9347 flavor = RPC_AUTH_MAXFLAVOR;
9349 if (flavor != RPC_AUTH_MAXFLAVOR) {
9350 err = nfs4_lookup_root_sec(server, fhandle,
9357 if (flavor == RPC_AUTH_MAXFLAVOR)
9368 static int _nfs41_test_stateid(struct nfs_server *server,
9369 nfs4_stateid *stateid,
9370 const struct cred *cred)
9373 struct nfs41_test_stateid_args args = {
9376 struct nfs41_test_stateid_res res;
9377 struct rpc_message msg = {
9378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9383 struct rpc_clnt *rpc_client = server->client;
9385 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9388 dprintk("NFS call test_stateid %p\n", stateid);
9389 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9390 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9391 &args.seq_args, &res.seq_res);
9392 if (status != NFS_OK) {
9393 dprintk("NFS reply test_stateid: failed, %d\n", status);
9396 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9400 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9401 int err, struct nfs4_exception *exception)
9403 exception->retry = 0;
9405 case -NFS4ERR_DELAY:
9406 case -NFS4ERR_RETRY_UNCACHED_REP:
9407 nfs4_handle_exception(server, err, exception);
9409 case -NFS4ERR_BADSESSION:
9410 case -NFS4ERR_BADSLOT:
9411 case -NFS4ERR_BAD_HIGH_SLOT:
9412 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9413 case -NFS4ERR_DEADSESSION:
9414 nfs4_do_handle_exception(server, err, exception);
9419 * nfs41_test_stateid - perform a TEST_STATEID operation
9421 * @server: server / transport on which to perform the operation
9422 * @stateid: state ID to test
9425 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9426 * Otherwise a negative NFS4ERR value is returned if the operation
9427 * failed or the state ID is not currently valid.
9429 static int nfs41_test_stateid(struct nfs_server *server,
9430 nfs4_stateid *stateid,
9431 const struct cred *cred)
9433 struct nfs4_exception exception = { };
9436 err = _nfs41_test_stateid(server, stateid, cred);
9437 nfs4_handle_delay_or_session_error(server, err, &exception);
9438 } while (exception.retry);
9442 struct nfs_free_stateid_data {
9443 struct nfs_server *server;
9444 struct nfs41_free_stateid_args args;
9445 struct nfs41_free_stateid_res res;
9448 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9450 struct nfs_free_stateid_data *data = calldata;
9451 nfs4_setup_sequence(data->server->nfs_client,
9452 &data->args.seq_args,
9457 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9459 struct nfs_free_stateid_data *data = calldata;
9461 nfs41_sequence_done(task, &data->res.seq_res);
9463 switch (task->tk_status) {
9464 case -NFS4ERR_DELAY:
9465 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9466 rpc_restart_call_prepare(task);
9470 static void nfs41_free_stateid_release(void *calldata)
9475 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9476 .rpc_call_prepare = nfs41_free_stateid_prepare,
9477 .rpc_call_done = nfs41_free_stateid_done,
9478 .rpc_release = nfs41_free_stateid_release,
9482 * nfs41_free_stateid - perform a FREE_STATEID operation
9484 * @server: server / transport on which to perform the operation
9485 * @stateid: state ID to release
9487 * @privileged: set to true if this call needs to be privileged
9489 * Note: this function is always asynchronous.
9491 static int nfs41_free_stateid(struct nfs_server *server,
9492 const nfs4_stateid *stateid,
9493 const struct cred *cred,
9496 struct rpc_message msg = {
9497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9500 struct rpc_task_setup task_setup = {
9501 .rpc_client = server->client,
9502 .rpc_message = &msg,
9503 .callback_ops = &nfs41_free_stateid_ops,
9504 .flags = RPC_TASK_ASYNC,
9506 struct nfs_free_stateid_data *data;
9507 struct rpc_task *task;
9509 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9510 &task_setup.rpc_client, &msg);
9512 dprintk("NFS call free_stateid %p\n", stateid);
9513 data = kmalloc(sizeof(*data), GFP_NOFS);
9516 data->server = server;
9517 nfs4_stateid_copy(&data->args.stateid, stateid);
9519 task_setup.callback_data = data;
9521 msg.rpc_argp = &data->args;
9522 msg.rpc_resp = &data->res;
9523 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9524 task = rpc_run_task(&task_setup);
9526 return PTR_ERR(task);
9532 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9534 const struct cred *cred = lsp->ls_state->owner->so_cred;
9536 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9537 nfs4_free_lock_state(server, lsp);
9540 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9541 const nfs4_stateid *s2)
9543 if (s1->type != s2->type)
9546 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9549 if (s1->seqid == s2->seqid)
9552 return s1->seqid == 0 || s2->seqid == 0;
9555 #endif /* CONFIG_NFS_V4_1 */
9557 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9558 const nfs4_stateid *s2)
9560 return nfs4_stateid_match(s1, s2);
9564 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9565 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9566 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9567 .recover_open = nfs4_open_reclaim,
9568 .recover_lock = nfs4_lock_reclaim,
9569 .establish_clid = nfs4_init_clientid,
9570 .detect_trunking = nfs40_discover_server_trunking,
9573 #if defined(CONFIG_NFS_V4_1)
9574 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9575 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9576 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9577 .recover_open = nfs4_open_reclaim,
9578 .recover_lock = nfs4_lock_reclaim,
9579 .establish_clid = nfs41_init_clientid,
9580 .reclaim_complete = nfs41_proc_reclaim_complete,
9581 .detect_trunking = nfs41_discover_server_trunking,
9583 #endif /* CONFIG_NFS_V4_1 */
9585 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9586 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9587 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9588 .recover_open = nfs40_open_expired,
9589 .recover_lock = nfs4_lock_expired,
9590 .establish_clid = nfs4_init_clientid,
9593 #if defined(CONFIG_NFS_V4_1)
9594 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9595 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9596 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9597 .recover_open = nfs41_open_expired,
9598 .recover_lock = nfs41_lock_expired,
9599 .establish_clid = nfs41_init_clientid,
9601 #endif /* CONFIG_NFS_V4_1 */
9603 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9604 .sched_state_renewal = nfs4_proc_async_renew,
9605 .get_state_renewal_cred = nfs4_get_renew_cred,
9606 .renew_lease = nfs4_proc_renew,
9609 #if defined(CONFIG_NFS_V4_1)
9610 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9611 .sched_state_renewal = nfs41_proc_async_sequence,
9612 .get_state_renewal_cred = nfs4_get_machine_cred,
9613 .renew_lease = nfs4_proc_sequence,
9617 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9618 .get_locations = _nfs40_proc_get_locations,
9619 .fsid_present = _nfs40_proc_fsid_present,
9622 #if defined(CONFIG_NFS_V4_1)
9623 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9624 .get_locations = _nfs41_proc_get_locations,
9625 .fsid_present = _nfs41_proc_fsid_present,
9627 #endif /* CONFIG_NFS_V4_1 */
9629 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9631 .init_caps = NFS_CAP_READDIRPLUS
9632 | NFS_CAP_ATOMIC_OPEN
9633 | NFS_CAP_POSIX_LOCK,
9634 .init_client = nfs40_init_client,
9635 .shutdown_client = nfs40_shutdown_client,
9636 .match_stateid = nfs4_match_stateid,
9637 .find_root_sec = nfs4_find_root_sec,
9638 .free_lock_state = nfs4_release_lockowner,
9639 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9640 .alloc_seqid = nfs_alloc_seqid,
9641 .call_sync_ops = &nfs40_call_sync_ops,
9642 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9643 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9644 .state_renewal_ops = &nfs40_state_renewal_ops,
9645 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9648 #if defined(CONFIG_NFS_V4_1)
9649 static struct nfs_seqid *
9650 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9655 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9657 .init_caps = NFS_CAP_READDIRPLUS
9658 | NFS_CAP_ATOMIC_OPEN
9659 | NFS_CAP_POSIX_LOCK
9660 | NFS_CAP_STATEID_NFSV41
9661 | NFS_CAP_ATOMIC_OPEN_V1
9663 .init_client = nfs41_init_client,
9664 .shutdown_client = nfs41_shutdown_client,
9665 .match_stateid = nfs41_match_stateid,
9666 .find_root_sec = nfs41_find_root_sec,
9667 .free_lock_state = nfs41_free_lock_state,
9668 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9669 .alloc_seqid = nfs_alloc_no_seqid,
9670 .session_trunk = nfs4_test_session_trunk,
9671 .call_sync_ops = &nfs41_call_sync_ops,
9672 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9673 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9674 .state_renewal_ops = &nfs41_state_renewal_ops,
9675 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9679 #if defined(CONFIG_NFS_V4_2)
9680 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9682 .init_caps = NFS_CAP_READDIRPLUS
9683 | NFS_CAP_ATOMIC_OPEN
9684 | NFS_CAP_POSIX_LOCK
9685 | NFS_CAP_STATEID_NFSV41
9686 | NFS_CAP_ATOMIC_OPEN_V1
9690 | NFS_CAP_OFFLOAD_CANCEL
9691 | NFS_CAP_DEALLOCATE
9693 | NFS_CAP_LAYOUTSTATS
9695 | NFS_CAP_LAYOUTERROR,
9696 .init_client = nfs41_init_client,
9697 .shutdown_client = nfs41_shutdown_client,
9698 .match_stateid = nfs41_match_stateid,
9699 .find_root_sec = nfs41_find_root_sec,
9700 .free_lock_state = nfs41_free_lock_state,
9701 .call_sync_ops = &nfs41_call_sync_ops,
9702 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9703 .alloc_seqid = nfs_alloc_no_seqid,
9704 .session_trunk = nfs4_test_session_trunk,
9705 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9706 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9707 .state_renewal_ops = &nfs41_state_renewal_ops,
9708 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9712 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9713 [0] = &nfs_v4_0_minor_ops,
9714 #if defined(CONFIG_NFS_V4_1)
9715 [1] = &nfs_v4_1_minor_ops,
9717 #if defined(CONFIG_NFS_V4_2)
9718 [2] = &nfs_v4_2_minor_ops,
9722 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9724 ssize_t error, error2;
9726 error = generic_listxattr(dentry, list, size);
9734 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9737 return error + error2;
9740 static const struct inode_operations nfs4_dir_inode_operations = {
9741 .create = nfs_create,
9742 .lookup = nfs_lookup,
9743 .atomic_open = nfs_atomic_open,
9745 .unlink = nfs_unlink,
9746 .symlink = nfs_symlink,
9750 .rename = nfs_rename,
9751 .permission = nfs_permission,
9752 .getattr = nfs_getattr,
9753 .setattr = nfs_setattr,
9754 .listxattr = nfs4_listxattr,
9757 static const struct inode_operations nfs4_file_inode_operations = {
9758 .permission = nfs_permission,
9759 .getattr = nfs_getattr,
9760 .setattr = nfs_setattr,
9761 .listxattr = nfs4_listxattr,
9764 const struct nfs_rpc_ops nfs_v4_clientops = {
9765 .version = 4, /* protocol version */
9766 .dentry_ops = &nfs4_dentry_operations,
9767 .dir_inode_ops = &nfs4_dir_inode_operations,
9768 .file_inode_ops = &nfs4_file_inode_operations,
9769 .file_ops = &nfs4_file_operations,
9770 .getroot = nfs4_proc_get_root,
9771 .submount = nfs4_submount,
9772 .try_mount = nfs4_try_mount,
9773 .getattr = nfs4_proc_getattr,
9774 .setattr = nfs4_proc_setattr,
9775 .lookup = nfs4_proc_lookup,
9776 .lookupp = nfs4_proc_lookupp,
9777 .access = nfs4_proc_access,
9778 .readlink = nfs4_proc_readlink,
9779 .create = nfs4_proc_create,
9780 .remove = nfs4_proc_remove,
9781 .unlink_setup = nfs4_proc_unlink_setup,
9782 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9783 .unlink_done = nfs4_proc_unlink_done,
9784 .rename_setup = nfs4_proc_rename_setup,
9785 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9786 .rename_done = nfs4_proc_rename_done,
9787 .link = nfs4_proc_link,
9788 .symlink = nfs4_proc_symlink,
9789 .mkdir = nfs4_proc_mkdir,
9790 .rmdir = nfs4_proc_rmdir,
9791 .readdir = nfs4_proc_readdir,
9792 .mknod = nfs4_proc_mknod,
9793 .statfs = nfs4_proc_statfs,
9794 .fsinfo = nfs4_proc_fsinfo,
9795 .pathconf = nfs4_proc_pathconf,
9796 .set_capabilities = nfs4_server_capabilities,
9797 .decode_dirent = nfs4_decode_dirent,
9798 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9799 .read_setup = nfs4_proc_read_setup,
9800 .read_done = nfs4_read_done,
9801 .write_setup = nfs4_proc_write_setup,
9802 .write_done = nfs4_write_done,
9803 .commit_setup = nfs4_proc_commit_setup,
9804 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9805 .commit_done = nfs4_commit_done,
9806 .lock = nfs4_proc_lock,
9807 .clear_acl_cache = nfs4_zap_acl_attr,
9808 .close_context = nfs4_close_context,
9809 .open_context = nfs4_atomic_open,
9810 .have_delegation = nfs4_have_delegation,
9811 .alloc_client = nfs4_alloc_client,
9812 .init_client = nfs4_init_client,
9813 .free_client = nfs4_free_client,
9814 .create_server = nfs4_create_server,
9815 .clone_server = nfs_clone_server,
9818 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9819 .name = XATTR_NAME_NFSV4_ACL,
9820 .list = nfs4_xattr_list_nfs4_acl,
9821 .get = nfs4_xattr_get_nfs4_acl,
9822 .set = nfs4_xattr_set_nfs4_acl,
9825 const struct xattr_handler *nfs4_xattr_handlers[] = {
9826 &nfs4_xattr_nfs4_acl_handler,
9827 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9828 &nfs4_xattr_nfs4_label_handler,