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 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2941 * Returns a referenced nfs4_state
2943 static int _nfs4_do_open(struct inode *dir,
2944 struct nfs_open_context *ctx,
2946 const struct nfs4_open_createattrs *c,
2949 struct nfs4_state_owner *sp;
2950 struct nfs4_state *state = NULL;
2951 struct nfs_server *server = NFS_SERVER(dir);
2952 struct nfs4_opendata *opendata;
2953 struct dentry *dentry = ctx->dentry;
2954 const struct cred *cred = ctx->cred;
2955 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2956 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2957 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2958 struct iattr *sattr = c->sattr;
2959 struct nfs4_label *label = c->label;
2960 struct nfs4_label *olabel = NULL;
2963 /* Protect against reboot recovery conflicts */
2965 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2967 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2970 status = nfs4_client_recover_expired_lease(server->nfs_client);
2972 goto err_put_state_owner;
2973 if (d_really_is_positive(dentry))
2974 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2976 if (d_really_is_positive(dentry))
2977 claim = NFS4_OPEN_CLAIM_FH;
2978 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2979 c, claim, GFP_KERNEL);
2980 if (opendata == NULL)
2981 goto err_put_state_owner;
2984 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2985 if (IS_ERR(olabel)) {
2986 status = PTR_ERR(olabel);
2987 goto err_opendata_put;
2991 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2992 if (!opendata->f_attr.mdsthreshold) {
2993 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2994 if (!opendata->f_attr.mdsthreshold)
2995 goto err_free_label;
2997 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2999 if (d_really_is_positive(dentry))
3000 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3002 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
3004 goto err_free_label;
3007 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3008 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3009 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3011 * send create attributes which was not set by open
3012 * with an extra setattr.
3014 if (attrs || label) {
3015 unsigned ia_old = sattr->ia_valid;
3017 sattr->ia_valid = attrs;
3018 nfs_fattr_init(opendata->o_res.f_attr);
3019 status = nfs4_do_setattr(state->inode, cred,
3020 opendata->o_res.f_attr, sattr,
3021 ctx, label, olabel);
3023 nfs_setattr_update_inode(state->inode, sattr,
3024 opendata->o_res.f_attr);
3025 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3027 sattr->ia_valid = ia_old;
3030 if (opened && opendata->file_created)
3033 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3034 *ctx_th = opendata->f_attr.mdsthreshold;
3035 opendata->f_attr.mdsthreshold = NULL;
3038 nfs4_label_free(olabel);
3040 nfs4_opendata_put(opendata);
3041 nfs4_put_state_owner(sp);
3044 nfs4_label_free(olabel);
3046 nfs4_opendata_put(opendata);
3047 err_put_state_owner:
3048 nfs4_put_state_owner(sp);
3054 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3055 struct nfs_open_context *ctx,
3057 struct iattr *sattr,
3058 struct nfs4_label *label,
3061 struct nfs_server *server = NFS_SERVER(dir);
3062 struct nfs4_exception exception = { };
3063 struct nfs4_state *res;
3064 struct nfs4_open_createattrs c = {
3068 [0] = (__u32)jiffies,
3069 [1] = (__u32)current->pid,
3075 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3077 trace_nfs4_open_file(ctx, flags, status);
3080 /* NOTE: BAD_SEQID means the server and client disagree about the
3081 * book-keeping w.r.t. state-changing operations
3082 * (OPEN/CLOSE/LOCK/LOCKU...)
3083 * It is actually a sign of a bug on the client or on the server.
3085 * If we receive a BAD_SEQID error in the particular case of
3086 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3087 * have unhashed the old state_owner for us, and that we can
3088 * therefore safely retry using a new one. We should still warn
3089 * the user though...
3091 if (status == -NFS4ERR_BAD_SEQID) {
3092 pr_warn_ratelimited("NFS: v4 server %s "
3093 " returned a bad sequence-id error!\n",
3094 NFS_SERVER(dir)->nfs_client->cl_hostname);
3095 exception.retry = 1;
3099 * BAD_STATEID on OPEN means that the server cancelled our
3100 * state before it received the OPEN_CONFIRM.
3101 * Recover by retrying the request as per the discussion
3102 * on Page 181 of RFC3530.
3104 if (status == -NFS4ERR_BAD_STATEID) {
3105 exception.retry = 1;
3108 if (status == -EAGAIN) {
3109 /* We must have found a delegation */
3110 exception.retry = 1;
3113 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3115 res = ERR_PTR(nfs4_handle_exception(server,
3116 status, &exception));
3117 } while (exception.retry);
3121 static int _nfs4_do_setattr(struct inode *inode,
3122 struct nfs_setattrargs *arg,
3123 struct nfs_setattrres *res,
3124 const struct cred *cred,
3125 struct nfs_open_context *ctx)
3127 struct nfs_server *server = NFS_SERVER(inode);
3128 struct rpc_message msg = {
3129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3134 const struct cred *delegation_cred = NULL;
3135 unsigned long timestamp = jiffies;
3139 nfs_fattr_init(res->fattr);
3141 /* Servers should only apply open mode checks for file size changes */
3142 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3146 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3147 /* Use that stateid */
3148 } else if (ctx != NULL) {
3149 struct nfs_lock_context *l_ctx;
3150 if (!nfs4_valid_open_stateid(ctx->state))
3152 l_ctx = nfs_get_lock_context(ctx);
3154 return PTR_ERR(l_ctx);
3155 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3156 &arg->stateid, &delegation_cred);
3157 nfs_put_lock_context(l_ctx);
3162 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3164 if (delegation_cred)
3165 msg.rpc_cred = delegation_cred;
3167 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3169 put_cred(delegation_cred);
3170 if (status == 0 && ctx != NULL)
3171 renew_lease(server, timestamp);
3172 trace_nfs4_setattr(inode, &arg->stateid, status);
3176 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3177 struct nfs_fattr *fattr, struct iattr *sattr,
3178 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3179 struct nfs4_label *olabel)
3181 struct nfs_server *server = NFS_SERVER(inode);
3182 __u32 bitmask[NFS4_BITMASK_SZ];
3183 struct nfs4_state *state = ctx ? ctx->state : NULL;
3184 struct nfs_setattrargs arg = {
3185 .fh = NFS_FH(inode),
3191 struct nfs_setattrres res = {
3196 struct nfs4_exception exception = {
3199 .stateid = &arg.stateid,
3204 nfs4_bitmap_copy_adjust_setattr(bitmask,
3205 nfs4_bitmask(server, olabel),
3208 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3210 case -NFS4ERR_OPENMODE:
3211 if (!(sattr->ia_valid & ATTR_SIZE)) {
3212 pr_warn_once("NFSv4: server %s is incorrectly "
3213 "applying open mode checks to "
3214 "a SETATTR that is not "
3215 "changing file size.\n",
3216 server->nfs_client->cl_hostname);
3218 if (state && !(state->state & FMODE_WRITE)) {
3220 if (sattr->ia_valid & ATTR_OPEN)
3225 err = nfs4_handle_exception(server, err, &exception);
3226 } while (exception.retry);
3232 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3234 if (inode == NULL || !nfs_have_layout(inode))
3237 return pnfs_wait_on_layoutreturn(inode, task);
3240 struct nfs4_closedata {
3241 struct inode *inode;
3242 struct nfs4_state *state;
3243 struct nfs_closeargs arg;
3244 struct nfs_closeres res;
3246 struct nfs4_layoutreturn_args arg;
3247 struct nfs4_layoutreturn_res res;
3248 struct nfs4_xdr_opaque_data ld_private;
3252 struct nfs_fattr fattr;
3253 unsigned long timestamp;
3256 static void nfs4_free_closedata(void *data)
3258 struct nfs4_closedata *calldata = data;
3259 struct nfs4_state_owner *sp = calldata->state->owner;
3260 struct super_block *sb = calldata->state->inode->i_sb;
3262 if (calldata->lr.roc)
3263 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3264 calldata->res.lr_ret);
3265 nfs4_put_open_state(calldata->state);
3266 nfs_free_seqid(calldata->arg.seqid);
3267 nfs4_put_state_owner(sp);
3268 nfs_sb_deactive(sb);
3272 static void nfs4_close_done(struct rpc_task *task, void *data)
3274 struct nfs4_closedata *calldata = data;
3275 struct nfs4_state *state = calldata->state;
3276 struct nfs_server *server = NFS_SERVER(calldata->inode);
3277 nfs4_stateid *res_stateid = NULL;
3278 struct nfs4_exception exception = {
3280 .inode = calldata->inode,
3281 .stateid = &calldata->arg.stateid,
3284 dprintk("%s: begin!\n", __func__);
3285 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3287 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3289 /* Handle Layoutreturn errors */
3290 if (calldata->arg.lr_args && task->tk_status != 0) {
3291 switch (calldata->res.lr_ret) {
3293 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3296 calldata->arg.lr_args = NULL;
3297 calldata->res.lr_res = NULL;
3299 case -NFS4ERR_OLD_STATEID:
3300 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3301 &calldata->arg.lr_args->range,
3305 case -NFS4ERR_ADMIN_REVOKED:
3306 case -NFS4ERR_DELEG_REVOKED:
3307 case -NFS4ERR_EXPIRED:
3308 case -NFS4ERR_BAD_STATEID:
3309 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3310 case -NFS4ERR_WRONG_CRED:
3311 calldata->arg.lr_args = NULL;
3312 calldata->res.lr_res = NULL;
3317 /* hmm. we are done with the inode, and in the process of freeing
3318 * the state_owner. we keep this around to process errors
3320 switch (task->tk_status) {
3322 res_stateid = &calldata->res.stateid;
3323 renew_lease(server, calldata->timestamp);
3325 case -NFS4ERR_ACCESS:
3326 if (calldata->arg.bitmask != NULL) {
3327 calldata->arg.bitmask = NULL;
3328 calldata->res.fattr = NULL;
3333 case -NFS4ERR_OLD_STATEID:
3334 /* Did we race with OPEN? */
3335 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3339 case -NFS4ERR_ADMIN_REVOKED:
3340 case -NFS4ERR_STALE_STATEID:
3341 case -NFS4ERR_EXPIRED:
3342 nfs4_free_revoked_stateid(server,
3343 &calldata->arg.stateid,
3344 task->tk_msg.rpc_cred);
3346 case -NFS4ERR_BAD_STATEID:
3349 task->tk_status = nfs4_async_handle_exception(task,
3350 server, task->tk_status, &exception);
3351 if (exception.retry)
3354 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3355 res_stateid, calldata->arg.fmode);
3357 task->tk_status = 0;
3358 nfs_release_seqid(calldata->arg.seqid);
3359 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3360 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3363 calldata->res.lr_ret = 0;
3365 task->tk_status = 0;
3366 rpc_restart_call_prepare(task);
3370 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3372 struct nfs4_closedata *calldata = data;
3373 struct nfs4_state *state = calldata->state;
3374 struct inode *inode = calldata->inode;
3375 struct pnfs_layout_hdr *lo;
3376 bool is_rdonly, is_wronly, is_rdwr;
3379 dprintk("%s: begin!\n", __func__);
3380 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3383 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3384 spin_lock(&state->owner->so_lock);
3385 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3386 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3387 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3388 /* Calculate the change in open mode */
3389 calldata->arg.fmode = 0;
3390 if (state->n_rdwr == 0) {
3391 if (state->n_rdonly == 0)
3392 call_close |= is_rdonly;
3394 calldata->arg.fmode |= FMODE_READ;
3395 if (state->n_wronly == 0)
3396 call_close |= is_wronly;
3398 calldata->arg.fmode |= FMODE_WRITE;
3399 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3400 call_close |= is_rdwr;
3402 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3404 if (!nfs4_valid_open_stateid(state) ||
3405 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3407 spin_unlock(&state->owner->so_lock);
3410 /* Note: exit _without_ calling nfs4_close_done */
3414 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3415 nfs_release_seqid(calldata->arg.seqid);
3419 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3420 if (lo && !pnfs_layout_is_valid(lo)) {
3421 calldata->arg.lr_args = NULL;
3422 calldata->res.lr_res = NULL;
3425 if (calldata->arg.fmode == 0)
3426 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3428 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3429 /* Close-to-open cache consistency revalidation */
3430 if (!nfs4_have_delegation(inode, FMODE_READ))
3431 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3433 calldata->arg.bitmask = NULL;
3436 calldata->arg.share_access =
3437 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3438 calldata->arg.fmode, 0);
3440 if (calldata->res.fattr == NULL)
3441 calldata->arg.bitmask = NULL;
3442 else if (calldata->arg.bitmask == NULL)
3443 calldata->res.fattr = NULL;
3444 calldata->timestamp = jiffies;
3445 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3446 &calldata->arg.seq_args,
3447 &calldata->res.seq_res,
3449 nfs_release_seqid(calldata->arg.seqid);
3450 dprintk("%s: done!\n", __func__);
3453 task->tk_action = NULL;
3455 nfs4_sequence_done(task, &calldata->res.seq_res);
3458 static const struct rpc_call_ops nfs4_close_ops = {
3459 .rpc_call_prepare = nfs4_close_prepare,
3460 .rpc_call_done = nfs4_close_done,
3461 .rpc_release = nfs4_free_closedata,
3465 * It is possible for data to be read/written from a mem-mapped file
3466 * after the sys_close call (which hits the vfs layer as a flush).
3467 * This means that we can't safely call nfsv4 close on a file until
3468 * the inode is cleared. This in turn means that we are not good
3469 * NFSv4 citizens - we do not indicate to the server to update the file's
3470 * share state even when we are done with one of the three share
3471 * stateid's in the inode.
3473 * NOTE: Caller must be holding the sp->so_owner semaphore!
3475 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3477 struct nfs_server *server = NFS_SERVER(state->inode);
3478 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3479 struct nfs4_closedata *calldata;
3480 struct nfs4_state_owner *sp = state->owner;
3481 struct rpc_task *task;
3482 struct rpc_message msg = {
3483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3484 .rpc_cred = state->owner->so_cred,
3486 struct rpc_task_setup task_setup_data = {
3487 .rpc_client = server->client,
3488 .rpc_message = &msg,
3489 .callback_ops = &nfs4_close_ops,
3490 .workqueue = nfsiod_workqueue,
3491 .flags = RPC_TASK_ASYNC,
3493 int status = -ENOMEM;
3495 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3496 &task_setup_data.rpc_client, &msg);
3498 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3499 if (calldata == NULL)
3501 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3502 calldata->inode = state->inode;
3503 calldata->state = state;
3504 calldata->arg.fh = NFS_FH(state->inode);
3505 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3506 goto out_free_calldata;
3507 /* Serialization for the sequence id */
3508 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3509 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3510 if (IS_ERR(calldata->arg.seqid))
3511 goto out_free_calldata;
3512 nfs_fattr_init(&calldata->fattr);
3513 calldata->arg.fmode = 0;
3514 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3515 calldata->res.fattr = &calldata->fattr;
3516 calldata->res.seqid = calldata->arg.seqid;
3517 calldata->res.server = server;
3518 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3519 calldata->lr.roc = pnfs_roc(state->inode,
3520 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3521 if (calldata->lr.roc) {
3522 calldata->arg.lr_args = &calldata->lr.arg;
3523 calldata->res.lr_res = &calldata->lr.res;
3525 nfs_sb_active(calldata->inode->i_sb);
3527 msg.rpc_argp = &calldata->arg;
3528 msg.rpc_resp = &calldata->res;
3529 task_setup_data.callback_data = calldata;
3530 task = rpc_run_task(&task_setup_data);
3532 return PTR_ERR(task);
3535 status = rpc_wait_for_completion_task(task);
3541 nfs4_put_open_state(state);
3542 nfs4_put_state_owner(sp);
3546 static struct inode *
3547 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3548 int open_flags, struct iattr *attr, int *opened)
3550 struct nfs4_state *state;
3551 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3553 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3555 /* Protect against concurrent sillydeletes */
3556 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3558 nfs4_label_release_security(label);
3561 return ERR_CAST(state);
3562 return state->inode;
3565 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3567 if (ctx->state == NULL)
3570 nfs4_close_sync(ctx->state, ctx->mode);
3572 nfs4_close_state(ctx->state, ctx->mode);
3575 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3576 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3577 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3579 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3581 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3582 struct nfs4_server_caps_arg args = {
3586 struct nfs4_server_caps_res res = {};
3587 struct rpc_message msg = {
3588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3595 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3596 FATTR4_WORD0_FH_EXPIRE_TYPE |
3597 FATTR4_WORD0_LINK_SUPPORT |
3598 FATTR4_WORD0_SYMLINK_SUPPORT |
3599 FATTR4_WORD0_ACLSUPPORT;
3601 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3603 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3605 /* Sanity check the server answers */
3606 switch (minorversion) {
3608 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3609 res.attr_bitmask[2] = 0;
3612 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3615 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3617 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3618 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3619 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3620 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3621 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3622 NFS_CAP_CTIME|NFS_CAP_MTIME|
3623 NFS_CAP_SECURITY_LABEL);
3624 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3625 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3626 server->caps |= NFS_CAP_ACLS;
3627 if (res.has_links != 0)
3628 server->caps |= NFS_CAP_HARDLINKS;
3629 if (res.has_symlinks != 0)
3630 server->caps |= NFS_CAP_SYMLINKS;
3631 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3632 server->caps |= NFS_CAP_FILEID;
3633 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3634 server->caps |= NFS_CAP_MODE;
3635 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3636 server->caps |= NFS_CAP_NLINK;
3637 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3638 server->caps |= NFS_CAP_OWNER;
3639 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3640 server->caps |= NFS_CAP_OWNER_GROUP;
3641 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3642 server->caps |= NFS_CAP_ATIME;
3643 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3644 server->caps |= NFS_CAP_CTIME;
3645 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3646 server->caps |= NFS_CAP_MTIME;
3647 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3648 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3649 server->caps |= NFS_CAP_SECURITY_LABEL;
3651 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3652 sizeof(server->attr_bitmask));
3653 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3655 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3656 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3657 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3658 server->cache_consistency_bitmask[2] = 0;
3660 /* Avoid a regression due to buggy server */
3661 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3662 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3663 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3664 sizeof(server->exclcreat_bitmask));
3666 server->acl_bitmask = res.acl_bitmask;
3667 server->fh_expire_type = res.fh_expire_type;
3673 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3675 struct nfs4_exception exception = { };
3678 err = nfs4_handle_exception(server,
3679 _nfs4_server_capabilities(server, fhandle),
3681 } while (exception.retry);
3685 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3686 struct nfs_fsinfo *info)
3689 struct nfs4_lookup_root_arg args = {
3692 struct nfs4_lookup_res res = {
3694 .fattr = info->fattr,
3697 struct rpc_message msg = {
3698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3703 bitmask[0] = nfs4_fattr_bitmap[0];
3704 bitmask[1] = nfs4_fattr_bitmap[1];
3706 * Process the label in the upcoming getfattr
3708 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3710 nfs_fattr_init(info->fattr);
3711 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3714 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3715 struct nfs_fsinfo *info)
3717 struct nfs4_exception exception = { };
3720 err = _nfs4_lookup_root(server, fhandle, info);
3721 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3724 case -NFS4ERR_WRONGSEC:
3727 err = nfs4_handle_exception(server, err, &exception);
3729 } while (exception.retry);
3734 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3735 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3737 struct rpc_auth_create_args auth_args = {
3738 .pseudoflavor = flavor,
3740 struct rpc_auth *auth;
3742 auth = rpcauth_create(&auth_args, server->client);
3745 return nfs4_lookup_root(server, fhandle, info);
3749 * Retry pseudoroot lookup with various security flavors. We do this when:
3751 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3752 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3754 * Returns zero on success, or a negative NFS4ERR value, or a
3755 * negative errno value.
3757 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3758 struct nfs_fsinfo *info)
3760 /* Per 3530bis 15.33.5 */
3761 static const rpc_authflavor_t flav_array[] = {
3765 RPC_AUTH_UNIX, /* courtesy */
3768 int status = -EPERM;
3771 if (server->auth_info.flavor_len > 0) {
3772 /* try each flavor specified by user */
3773 for (i = 0; i < server->auth_info.flavor_len; i++) {
3774 status = nfs4_lookup_root_sec(server, fhandle, info,
3775 server->auth_info.flavors[i]);
3776 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3781 /* no flavors specified by user, try default list */
3782 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3783 status = nfs4_lookup_root_sec(server, fhandle, info,
3785 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3792 * -EACCES could mean that the user doesn't have correct permissions
3793 * to access the mount. It could also mean that we tried to mount
3794 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3795 * existing mount programs don't handle -EACCES very well so it should
3796 * be mapped to -EPERM instead.
3798 if (status == -EACCES)
3804 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3805 * @server: initialized nfs_server handle
3806 * @fhandle: we fill in the pseudo-fs root file handle
3807 * @info: we fill in an FSINFO struct
3808 * @auth_probe: probe the auth flavours
3810 * Returns zero on success, or a negative errno.
3812 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3813 struct nfs_fsinfo *info,
3819 status = nfs4_lookup_root(server, fhandle, info);
3821 if (auth_probe || status == NFS4ERR_WRONGSEC)
3822 status = server->nfs_client->cl_mvops->find_root_sec(server,
3826 status = nfs4_server_capabilities(server, fhandle);
3828 status = nfs4_do_fsinfo(server, fhandle, info);
3830 return nfs4_map_errors(status);
3833 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3834 struct nfs_fsinfo *info)
3837 struct nfs_fattr *fattr = info->fattr;
3838 struct nfs4_label *label = NULL;
3840 error = nfs4_server_capabilities(server, mntfh);
3842 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3846 label = nfs4_label_alloc(server, GFP_KERNEL);
3848 return PTR_ERR(label);
3850 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3852 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3853 goto err_free_label;
3856 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3857 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3858 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3861 nfs4_label_free(label);
3867 * Get locations and (maybe) other attributes of a referral.
3868 * Note that we'll actually follow the referral later when
3869 * we detect fsid mismatch in inode revalidation
3871 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3872 const struct qstr *name, struct nfs_fattr *fattr,
3873 struct nfs_fh *fhandle)
3875 int status = -ENOMEM;
3876 struct page *page = NULL;
3877 struct nfs4_fs_locations *locations = NULL;
3879 page = alloc_page(GFP_KERNEL);
3882 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3883 if (locations == NULL)
3886 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3891 * If the fsid didn't change, this is a migration event, not a
3892 * referral. Cause us to drop into the exception handler, which
3893 * will kick off migration recovery.
3895 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3896 dprintk("%s: server did not return a different fsid for"
3897 " a referral at %s\n", __func__, name->name);
3898 status = -NFS4ERR_MOVED;
3901 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3902 nfs_fixup_referral_attributes(&locations->fattr);
3904 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3905 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3906 memset(fhandle, 0, sizeof(struct nfs_fh));
3914 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3915 struct nfs_fattr *fattr, struct nfs4_label *label,
3916 struct inode *inode)
3918 __u32 bitmask[NFS4_BITMASK_SZ];
3919 struct nfs4_getattr_arg args = {
3923 struct nfs4_getattr_res res = {
3928 struct rpc_message msg = {
3929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3934 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
3936 nfs_fattr_init(fattr);
3937 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3940 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3941 struct nfs_fattr *fattr, struct nfs4_label *label,
3942 struct inode *inode)
3944 struct nfs4_exception exception = { };
3947 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
3948 trace_nfs4_getattr(server, fhandle, fattr, err);
3949 err = nfs4_handle_exception(server, err,
3951 } while (exception.retry);
3956 * The file is not closed if it is opened due to the a request to change
3957 * the size of the file. The open call will not be needed once the
3958 * VFS layer lookup-intents are implemented.
3960 * Close is called when the inode is destroyed.
3961 * If we haven't opened the file for O_WRONLY, we
3962 * need to in the size_change case to obtain a stateid.
3965 * Because OPEN is always done by name in nfsv4, it is
3966 * possible that we opened a different file by the same
3967 * name. We can recognize this race condition, but we
3968 * can't do anything about it besides returning an error.
3970 * This will be fixed with VFS changes (lookup-intent).
3973 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3974 struct iattr *sattr)
3976 struct inode *inode = d_inode(dentry);
3977 const struct cred *cred = NULL;
3978 struct nfs_open_context *ctx = NULL;
3979 struct nfs4_label *label = NULL;
3982 if (pnfs_ld_layoutret_on_setattr(inode) &&
3983 sattr->ia_valid & ATTR_SIZE &&
3984 sattr->ia_size < i_size_read(inode))
3985 pnfs_commit_and_return_layout(inode);
3987 nfs_fattr_init(fattr);
3989 /* Deal with open(O_TRUNC) */
3990 if (sattr->ia_valid & ATTR_OPEN)
3991 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3993 /* Optimization: if the end result is no change, don't RPC */
3994 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3997 /* Search for an existing open(O_WRITE) file */
3998 if (sattr->ia_valid & ATTR_FILE) {
4000 ctx = nfs_file_open_context(sattr->ia_file);
4005 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4007 return PTR_ERR(label);
4009 /* Return any delegations if we're going to change ACLs */
4010 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4011 nfs4_inode_make_writeable(inode);
4013 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4015 nfs_setattr_update_inode(inode, sattr, fattr);
4016 nfs_setsecurity(inode, fattr, label);
4018 nfs4_label_free(label);
4022 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4023 const struct qstr *name, struct nfs_fh *fhandle,
4024 struct nfs_fattr *fattr, struct nfs4_label *label)
4026 struct nfs_server *server = NFS_SERVER(dir);
4028 struct nfs4_lookup_arg args = {
4029 .bitmask = server->attr_bitmask,
4030 .dir_fh = NFS_FH(dir),
4033 struct nfs4_lookup_res res = {
4039 struct rpc_message msg = {
4040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4045 args.bitmask = nfs4_bitmask(server, label);
4047 nfs_fattr_init(fattr);
4049 dprintk("NFS call lookup %s\n", name->name);
4050 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4051 dprintk("NFS reply lookup: %d\n", status);
4055 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4057 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4058 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4059 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4063 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4064 const struct qstr *name, struct nfs_fh *fhandle,
4065 struct nfs_fattr *fattr, struct nfs4_label *label)
4067 struct nfs4_exception exception = { };
4068 struct rpc_clnt *client = *clnt;
4071 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4072 trace_nfs4_lookup(dir, name, err);
4074 case -NFS4ERR_BADNAME:
4077 case -NFS4ERR_MOVED:
4078 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4079 if (err == -NFS4ERR_MOVED)
4080 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4082 case -NFS4ERR_WRONGSEC:
4084 if (client != *clnt)
4086 client = nfs4_negotiate_security(client, dir, name);
4088 return PTR_ERR(client);
4090 exception.retry = 1;
4093 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4095 } while (exception.retry);
4100 else if (client != *clnt)
4101 rpc_shutdown_client(client);
4106 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4107 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4108 struct nfs4_label *label)
4111 struct rpc_clnt *client = NFS_CLIENT(dir);
4113 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4114 if (client != NFS_CLIENT(dir)) {
4115 rpc_shutdown_client(client);
4116 nfs_fixup_secinfo_attributes(fattr);
4122 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4123 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4125 struct rpc_clnt *client = NFS_CLIENT(dir);
4128 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4130 return ERR_PTR(status);
4131 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4134 static int _nfs4_proc_lookupp(struct inode *inode,
4135 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4136 struct nfs4_label *label)
4138 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4139 struct nfs_server *server = NFS_SERVER(inode);
4141 struct nfs4_lookupp_arg args = {
4142 .bitmask = server->attr_bitmask,
4143 .fh = NFS_FH(inode),
4145 struct nfs4_lookupp_res res = {
4151 struct rpc_message msg = {
4152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4157 args.bitmask = nfs4_bitmask(server, label);
4159 nfs_fattr_init(fattr);
4161 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4162 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4164 dprintk("NFS reply lookupp: %d\n", status);
4168 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4169 struct nfs_fattr *fattr, struct nfs4_label *label)
4171 struct nfs4_exception exception = { };
4174 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4175 trace_nfs4_lookupp(inode, err);
4176 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4178 } while (exception.retry);
4182 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4184 struct nfs_server *server = NFS_SERVER(inode);
4185 struct nfs4_accessargs args = {
4186 .fh = NFS_FH(inode),
4187 .access = entry->mask,
4189 struct nfs4_accessres res = {
4192 struct rpc_message msg = {
4193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4196 .rpc_cred = entry->cred,
4200 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4201 res.fattr = nfs_alloc_fattr();
4202 if (res.fattr == NULL)
4204 args.bitmask = server->cache_consistency_bitmask;
4206 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4208 nfs_access_set_mask(entry, res.access);
4210 nfs_refresh_inode(inode, res.fattr);
4212 nfs_free_fattr(res.fattr);
4216 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4218 struct nfs4_exception exception = { };
4221 err = _nfs4_proc_access(inode, entry);
4222 trace_nfs4_access(inode, err);
4223 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4225 } while (exception.retry);
4230 * TODO: For the time being, we don't try to get any attributes
4231 * along with any of the zero-copy operations READ, READDIR,
4234 * In the case of the first three, we want to put the GETATTR
4235 * after the read-type operation -- this is because it is hard
4236 * to predict the length of a GETATTR response in v4, and thus
4237 * align the READ data correctly. This means that the GETATTR
4238 * may end up partially falling into the page cache, and we should
4239 * shift it into the 'tail' of the xdr_buf before processing.
4240 * To do this efficiently, we need to know the total length
4241 * of data received, which doesn't seem to be available outside
4244 * In the case of WRITE, we also want to put the GETATTR after
4245 * the operation -- in this case because we want to make sure
4246 * we get the post-operation mtime and size.
4248 * Both of these changes to the XDR layer would in fact be quite
4249 * minor, but I decided to leave them for a subsequent patch.
4251 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4252 unsigned int pgbase, unsigned int pglen)
4254 struct nfs4_readlink args = {
4255 .fh = NFS_FH(inode),
4260 struct nfs4_readlink_res res;
4261 struct rpc_message msg = {
4262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4267 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4270 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4271 unsigned int pgbase, unsigned int pglen)
4273 struct nfs4_exception exception = { };
4276 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4277 trace_nfs4_readlink(inode, err);
4278 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4280 } while (exception.retry);
4285 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4288 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4291 struct nfs_server *server = NFS_SERVER(dir);
4292 struct nfs4_label l, *ilabel = NULL;
4293 struct nfs_open_context *ctx;
4294 struct nfs4_state *state;
4297 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4299 return PTR_ERR(ctx);
4301 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4303 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4304 sattr->ia_mode &= ~current_umask();
4305 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4306 if (IS_ERR(state)) {
4307 status = PTR_ERR(state);
4311 nfs4_label_release_security(ilabel);
4312 put_nfs_open_context(ctx);
4317 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4319 struct nfs_server *server = NFS_SERVER(dir);
4320 struct nfs_removeargs args = {
4324 struct nfs_removeres res = {
4327 struct rpc_message msg = {
4328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4332 unsigned long timestamp = jiffies;
4335 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4337 spin_lock(&dir->i_lock);
4338 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4339 /* Removing a directory decrements nlink in the parent */
4340 if (ftype == NF4DIR && dir->i_nlink > 2)
4341 nfs4_dec_nlink_locked(dir);
4342 spin_unlock(&dir->i_lock);
4347 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4349 struct nfs4_exception exception = { };
4350 struct inode *inode = d_inode(dentry);
4354 if (inode->i_nlink == 1)
4355 nfs4_inode_return_delegation(inode);
4357 nfs4_inode_make_writeable(inode);
4360 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4361 trace_nfs4_remove(dir, &dentry->d_name, err);
4362 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4364 } while (exception.retry);
4368 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4370 struct nfs4_exception exception = { };
4374 err = _nfs4_proc_remove(dir, name, NF4DIR);
4375 trace_nfs4_remove(dir, name, err);
4376 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4378 } while (exception.retry);
4382 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4383 struct dentry *dentry,
4384 struct inode *inode)
4386 struct nfs_removeargs *args = msg->rpc_argp;
4387 struct nfs_removeres *res = msg->rpc_resp;
4389 res->server = NFS_SB(dentry->d_sb);
4390 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4391 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4393 nfs_fattr_init(res->dir_attr);
4396 nfs4_inode_return_delegation(inode);
4399 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4401 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4402 &data->args.seq_args,
4407 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4409 struct nfs_unlinkdata *data = task->tk_calldata;
4410 struct nfs_removeres *res = &data->res;
4412 if (!nfs4_sequence_done(task, &res->seq_res))
4414 if (nfs4_async_handle_error(task, res->server, NULL,
4415 &data->timeout) == -EAGAIN)
4417 if (task->tk_status == 0)
4418 update_changeattr(dir, &res->cinfo,
4419 res->dir_attr->time_start, 0);
4423 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4424 struct dentry *old_dentry,
4425 struct dentry *new_dentry)
4427 struct nfs_renameargs *arg = msg->rpc_argp;
4428 struct nfs_renameres *res = msg->rpc_resp;
4429 struct inode *old_inode = d_inode(old_dentry);
4430 struct inode *new_inode = d_inode(new_dentry);
4433 nfs4_inode_make_writeable(old_inode);
4435 nfs4_inode_return_delegation(new_inode);
4436 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4437 res->server = NFS_SB(old_dentry->d_sb);
4438 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4441 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4443 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4444 &data->args.seq_args,
4449 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4450 struct inode *new_dir)
4452 struct nfs_renamedata *data = task->tk_calldata;
4453 struct nfs_renameres *res = &data->res;
4455 if (!nfs4_sequence_done(task, &res->seq_res))
4457 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4460 if (task->tk_status == 0) {
4461 if (new_dir != old_dir) {
4462 /* Note: If we moved a directory, nlink will change */
4463 update_changeattr(old_dir, &res->old_cinfo,
4464 res->old_fattr->time_start,
4465 NFS_INO_INVALID_OTHER);
4466 update_changeattr(new_dir, &res->new_cinfo,
4467 res->new_fattr->time_start,
4468 NFS_INO_INVALID_OTHER);
4470 update_changeattr(old_dir, &res->old_cinfo,
4471 res->old_fattr->time_start,
4477 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4479 struct nfs_server *server = NFS_SERVER(inode);
4480 __u32 bitmask[NFS4_BITMASK_SZ];
4481 struct nfs4_link_arg arg = {
4482 .fh = NFS_FH(inode),
4483 .dir_fh = NFS_FH(dir),
4487 struct nfs4_link_res res = {
4491 struct rpc_message msg = {
4492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4496 int status = -ENOMEM;
4498 res.fattr = nfs_alloc_fattr();
4499 if (res.fattr == NULL)
4502 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4503 if (IS_ERR(res.label)) {
4504 status = PTR_ERR(res.label);
4508 nfs4_inode_make_writeable(inode);
4509 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4511 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4513 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4514 status = nfs_post_op_update_inode(inode, res.fattr);
4516 nfs_setsecurity(inode, res.fattr, res.label);
4520 nfs4_label_free(res.label);
4523 nfs_free_fattr(res.fattr);
4527 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4529 struct nfs4_exception exception = { };
4532 err = nfs4_handle_exception(NFS_SERVER(inode),
4533 _nfs4_proc_link(inode, dir, name),
4535 } while (exception.retry);
4539 struct nfs4_createdata {
4540 struct rpc_message msg;
4541 struct nfs4_create_arg arg;
4542 struct nfs4_create_res res;
4544 struct nfs_fattr fattr;
4545 struct nfs4_label *label;
4548 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4549 const struct qstr *name, struct iattr *sattr, u32 ftype)
4551 struct nfs4_createdata *data;
4553 data = kzalloc(sizeof(*data), GFP_KERNEL);
4555 struct nfs_server *server = NFS_SERVER(dir);
4557 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4558 if (IS_ERR(data->label))
4561 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4562 data->msg.rpc_argp = &data->arg;
4563 data->msg.rpc_resp = &data->res;
4564 data->arg.dir_fh = NFS_FH(dir);
4565 data->arg.server = server;
4566 data->arg.name = name;
4567 data->arg.attrs = sattr;
4568 data->arg.ftype = ftype;
4569 data->arg.bitmask = nfs4_bitmask(server, data->label);
4570 data->arg.umask = current_umask();
4571 data->res.server = server;
4572 data->res.fh = &data->fh;
4573 data->res.fattr = &data->fattr;
4574 data->res.label = data->label;
4575 nfs_fattr_init(data->res.fattr);
4583 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4585 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4586 &data->arg.seq_args, &data->res.seq_res, 1);
4588 spin_lock(&dir->i_lock);
4589 update_changeattr_locked(dir, &data->res.dir_cinfo,
4590 data->res.fattr->time_start, 0);
4591 /* Creating a directory bumps nlink in the parent */
4592 if (data->arg.ftype == NF4DIR)
4593 nfs4_inc_nlink_locked(dir);
4594 spin_unlock(&dir->i_lock);
4595 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4600 static void nfs4_free_createdata(struct nfs4_createdata *data)
4602 nfs4_label_free(data->label);
4606 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4607 struct page *page, unsigned int len, struct iattr *sattr,
4608 struct nfs4_label *label)
4610 struct nfs4_createdata *data;
4611 int status = -ENAMETOOLONG;
4613 if (len > NFS4_MAXPATHLEN)
4617 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4621 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4622 data->arg.u.symlink.pages = &page;
4623 data->arg.u.symlink.len = len;
4624 data->arg.label = label;
4626 status = nfs4_do_create(dir, dentry, data);
4628 nfs4_free_createdata(data);
4633 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4634 struct page *page, unsigned int len, struct iattr *sattr)
4636 struct nfs4_exception exception = { };
4637 struct nfs4_label l, *label = NULL;
4640 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4643 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4644 trace_nfs4_symlink(dir, &dentry->d_name, err);
4645 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4647 } while (exception.retry);
4649 nfs4_label_release_security(label);
4653 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4654 struct iattr *sattr, struct nfs4_label *label)
4656 struct nfs4_createdata *data;
4657 int status = -ENOMEM;
4659 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4663 data->arg.label = label;
4664 status = nfs4_do_create(dir, dentry, data);
4666 nfs4_free_createdata(data);
4671 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4672 struct iattr *sattr)
4674 struct nfs_server *server = NFS_SERVER(dir);
4675 struct nfs4_exception exception = { };
4676 struct nfs4_label l, *label = NULL;
4679 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4681 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4682 sattr->ia_mode &= ~current_umask();
4684 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4685 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4686 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4688 } while (exception.retry);
4689 nfs4_label_release_security(label);
4694 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4695 u64 cookie, struct page **pages, unsigned int count, bool plus)
4697 struct inode *dir = d_inode(dentry);
4698 struct nfs4_readdir_arg args = {
4703 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4706 struct nfs4_readdir_res res;
4707 struct rpc_message msg = {
4708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4715 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4717 (unsigned long long)cookie);
4718 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4719 res.pgbase = args.pgbase;
4720 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4722 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4723 status += args.pgbase;
4726 nfs_invalidate_atime(dir);
4728 dprintk("%s: returns %d\n", __func__, status);
4732 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4733 u64 cookie, struct page **pages, unsigned int count, bool plus)
4735 struct nfs4_exception exception = { };
4738 err = _nfs4_proc_readdir(dentry, cred, cookie,
4739 pages, count, plus);
4740 trace_nfs4_readdir(d_inode(dentry), err);
4741 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4743 } while (exception.retry);
4747 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4748 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4750 struct nfs4_createdata *data;
4751 int mode = sattr->ia_mode;
4752 int status = -ENOMEM;
4754 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4759 data->arg.ftype = NF4FIFO;
4760 else if (S_ISBLK(mode)) {
4761 data->arg.ftype = NF4BLK;
4762 data->arg.u.device.specdata1 = MAJOR(rdev);
4763 data->arg.u.device.specdata2 = MINOR(rdev);
4765 else if (S_ISCHR(mode)) {
4766 data->arg.ftype = NF4CHR;
4767 data->arg.u.device.specdata1 = MAJOR(rdev);
4768 data->arg.u.device.specdata2 = MINOR(rdev);
4769 } else if (!S_ISSOCK(mode)) {
4774 data->arg.label = label;
4775 status = nfs4_do_create(dir, dentry, data);
4777 nfs4_free_createdata(data);
4782 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4783 struct iattr *sattr, dev_t rdev)
4785 struct nfs_server *server = NFS_SERVER(dir);
4786 struct nfs4_exception exception = { };
4787 struct nfs4_label l, *label = NULL;
4790 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4792 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4793 sattr->ia_mode &= ~current_umask();
4795 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4796 trace_nfs4_mknod(dir, &dentry->d_name, err);
4797 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4799 } while (exception.retry);
4801 nfs4_label_release_security(label);
4806 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4807 struct nfs_fsstat *fsstat)
4809 struct nfs4_statfs_arg args = {
4811 .bitmask = server->attr_bitmask,
4813 struct nfs4_statfs_res res = {
4816 struct rpc_message msg = {
4817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4822 nfs_fattr_init(fsstat->fattr);
4823 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4826 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4828 struct nfs4_exception exception = { };
4831 err = nfs4_handle_exception(server,
4832 _nfs4_proc_statfs(server, fhandle, fsstat),
4834 } while (exception.retry);
4838 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4839 struct nfs_fsinfo *fsinfo)
4841 struct nfs4_fsinfo_arg args = {
4843 .bitmask = server->attr_bitmask,
4845 struct nfs4_fsinfo_res res = {
4848 struct rpc_message msg = {
4849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4854 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4857 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4859 struct nfs4_exception exception = { };
4860 unsigned long now = jiffies;
4864 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4865 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4867 nfs4_set_lease_period(server->nfs_client,
4868 fsinfo->lease_time * HZ,
4872 err = nfs4_handle_exception(server, err, &exception);
4873 } while (exception.retry);
4877 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4881 nfs_fattr_init(fsinfo->fattr);
4882 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4884 /* block layout checks this! */
4885 server->pnfs_blksize = fsinfo->blksize;
4886 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4892 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4893 struct nfs_pathconf *pathconf)
4895 struct nfs4_pathconf_arg args = {
4897 .bitmask = server->attr_bitmask,
4899 struct nfs4_pathconf_res res = {
4900 .pathconf = pathconf,
4902 struct rpc_message msg = {
4903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4908 /* None of the pathconf attributes are mandatory to implement */
4909 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4910 memset(pathconf, 0, sizeof(*pathconf));
4914 nfs_fattr_init(pathconf->fattr);
4915 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4918 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4919 struct nfs_pathconf *pathconf)
4921 struct nfs4_exception exception = { };
4925 err = nfs4_handle_exception(server,
4926 _nfs4_proc_pathconf(server, fhandle, pathconf),
4928 } while (exception.retry);
4932 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4933 const struct nfs_open_context *ctx,
4934 const struct nfs_lock_context *l_ctx,
4937 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4939 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4941 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4942 const struct nfs_open_context *ctx,
4943 const struct nfs_lock_context *l_ctx,
4946 nfs4_stateid current_stateid;
4948 /* If the current stateid represents a lost lock, then exit */
4949 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4951 return nfs4_stateid_match(stateid, ¤t_stateid);
4954 static bool nfs4_error_stateid_expired(int err)
4957 case -NFS4ERR_DELEG_REVOKED:
4958 case -NFS4ERR_ADMIN_REVOKED:
4959 case -NFS4ERR_BAD_STATEID:
4960 case -NFS4ERR_STALE_STATEID:
4961 case -NFS4ERR_OLD_STATEID:
4962 case -NFS4ERR_OPENMODE:
4963 case -NFS4ERR_EXPIRED:
4969 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4971 struct nfs_server *server = NFS_SERVER(hdr->inode);
4973 trace_nfs4_read(hdr, task->tk_status);
4974 if (task->tk_status < 0) {
4975 struct nfs4_exception exception = {
4976 .inode = hdr->inode,
4977 .state = hdr->args.context->state,
4978 .stateid = &hdr->args.stateid,
4980 task->tk_status = nfs4_async_handle_exception(task,
4981 server, task->tk_status, &exception);
4982 if (exception.retry) {
4983 rpc_restart_call_prepare(task);
4988 if (task->tk_status > 0)
4989 renew_lease(server, hdr->timestamp);
4993 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4994 struct nfs_pgio_args *args)
4997 if (!nfs4_error_stateid_expired(task->tk_status) ||
4998 nfs4_stateid_is_current(&args->stateid,
5003 rpc_restart_call_prepare(task);
5007 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5010 dprintk("--> %s\n", __func__);
5012 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5014 if (nfs4_read_stateid_changed(task, &hdr->args))
5016 if (task->tk_status > 0)
5017 nfs_invalidate_atime(hdr->inode);
5018 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5019 nfs4_read_done_cb(task, hdr);
5022 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5023 struct rpc_message *msg)
5025 hdr->timestamp = jiffies;
5026 if (!hdr->pgio_done_cb)
5027 hdr->pgio_done_cb = nfs4_read_done_cb;
5028 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5029 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5032 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5033 struct nfs_pgio_header *hdr)
5035 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5036 &hdr->args.seq_args,
5040 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5041 hdr->args.lock_context,
5042 hdr->rw_mode) == -EIO)
5044 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5049 static int nfs4_write_done_cb(struct rpc_task *task,
5050 struct nfs_pgio_header *hdr)
5052 struct inode *inode = hdr->inode;
5054 trace_nfs4_write(hdr, task->tk_status);
5055 if (task->tk_status < 0) {
5056 struct nfs4_exception exception = {
5057 .inode = hdr->inode,
5058 .state = hdr->args.context->state,
5059 .stateid = &hdr->args.stateid,
5061 task->tk_status = nfs4_async_handle_exception(task,
5062 NFS_SERVER(inode), task->tk_status,
5064 if (exception.retry) {
5065 rpc_restart_call_prepare(task);
5069 if (task->tk_status >= 0) {
5070 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5071 nfs_writeback_update_inode(hdr);
5076 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5077 struct nfs_pgio_args *args)
5080 if (!nfs4_error_stateid_expired(task->tk_status) ||
5081 nfs4_stateid_is_current(&args->stateid,
5086 rpc_restart_call_prepare(task);
5090 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5092 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5094 if (nfs4_write_stateid_changed(task, &hdr->args))
5096 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5097 nfs4_write_done_cb(task, hdr);
5101 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5103 /* Don't request attributes for pNFS or O_DIRECT writes */
5104 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5106 /* Otherwise, request attributes if and only if we don't hold
5109 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5112 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5113 struct rpc_message *msg,
5114 struct rpc_clnt **clnt)
5116 struct nfs_server *server = NFS_SERVER(hdr->inode);
5118 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5119 hdr->args.bitmask = NULL;
5120 hdr->res.fattr = NULL;
5122 hdr->args.bitmask = server->cache_consistency_bitmask;
5124 if (!hdr->pgio_done_cb)
5125 hdr->pgio_done_cb = nfs4_write_done_cb;
5126 hdr->res.server = server;
5127 hdr->timestamp = jiffies;
5129 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5130 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5131 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5134 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5136 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5137 &data->args.seq_args,
5142 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5144 struct inode *inode = data->inode;
5146 trace_nfs4_commit(data, task->tk_status);
5147 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5148 NULL, NULL) == -EAGAIN) {
5149 rpc_restart_call_prepare(task);
5155 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5157 if (!nfs4_sequence_done(task, &data->res.seq_res))
5159 return data->commit_done_cb(task, data);
5162 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5163 struct rpc_clnt **clnt)
5165 struct nfs_server *server = NFS_SERVER(data->inode);
5167 if (data->commit_done_cb == NULL)
5168 data->commit_done_cb = nfs4_commit_done_cb;
5169 data->res.server = server;
5170 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5171 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5172 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5175 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5176 struct nfs_commitres *res)
5178 struct inode *dst_inode = file_inode(dst);
5179 struct nfs_server *server = NFS_SERVER(dst_inode);
5180 struct rpc_message msg = {
5181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5186 args->fh = NFS_FH(dst_inode);
5187 return nfs4_call_sync(server->client, server, &msg,
5188 &args->seq_args, &res->seq_res, 1);
5191 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5193 struct nfs_commitargs args = {
5197 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5198 struct nfs4_exception exception = { };
5202 status = _nfs4_proc_commit(dst, &args, res);
5203 status = nfs4_handle_exception(dst_server, status, &exception);
5204 } while (exception.retry);
5209 struct nfs4_renewdata {
5210 struct nfs_client *client;
5211 unsigned long timestamp;
5215 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5216 * standalone procedure for queueing an asynchronous RENEW.
5218 static void nfs4_renew_release(void *calldata)
5220 struct nfs4_renewdata *data = calldata;
5221 struct nfs_client *clp = data->client;
5223 if (refcount_read(&clp->cl_count) > 1)
5224 nfs4_schedule_state_renewal(clp);
5225 nfs_put_client(clp);
5229 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5231 struct nfs4_renewdata *data = calldata;
5232 struct nfs_client *clp = data->client;
5233 unsigned long timestamp = data->timestamp;
5235 trace_nfs4_renew_async(clp, task->tk_status);
5236 switch (task->tk_status) {
5239 case -NFS4ERR_LEASE_MOVED:
5240 nfs4_schedule_lease_moved_recovery(clp);
5243 /* Unless we're shutting down, schedule state recovery! */
5244 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5246 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5247 nfs4_schedule_lease_recovery(clp);
5250 nfs4_schedule_path_down_recovery(clp);
5252 do_renew_lease(clp, timestamp);
5255 static const struct rpc_call_ops nfs4_renew_ops = {
5256 .rpc_call_done = nfs4_renew_done,
5257 .rpc_release = nfs4_renew_release,
5260 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5262 struct rpc_message msg = {
5263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5267 struct nfs4_renewdata *data;
5269 if (renew_flags == 0)
5271 if (!refcount_inc_not_zero(&clp->cl_count))
5273 data = kmalloc(sizeof(*data), GFP_NOFS);
5275 nfs_put_client(clp);
5279 data->timestamp = jiffies;
5280 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5281 &nfs4_renew_ops, data);
5284 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5286 struct rpc_message msg = {
5287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5291 unsigned long now = jiffies;
5294 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5297 do_renew_lease(clp, now);
5301 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5303 return server->caps & NFS_CAP_ACLS;
5306 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5307 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5310 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5312 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5313 struct page **pages)
5315 struct page *newpage, **spages;
5321 len = min_t(size_t, PAGE_SIZE, buflen);
5322 newpage = alloc_page(GFP_KERNEL);
5324 if (newpage == NULL)
5326 memcpy(page_address(newpage), buf, len);
5331 } while (buflen != 0);
5337 __free_page(spages[rc-1]);
5341 struct nfs4_cached_acl {
5347 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5349 struct nfs_inode *nfsi = NFS_I(inode);
5351 spin_lock(&inode->i_lock);
5352 kfree(nfsi->nfs4_acl);
5353 nfsi->nfs4_acl = acl;
5354 spin_unlock(&inode->i_lock);
5357 static void nfs4_zap_acl_attr(struct inode *inode)
5359 nfs4_set_cached_acl(inode, NULL);
5362 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5364 struct nfs_inode *nfsi = NFS_I(inode);
5365 struct nfs4_cached_acl *acl;
5368 spin_lock(&inode->i_lock);
5369 acl = nfsi->nfs4_acl;
5372 if (buf == NULL) /* user is just asking for length */
5374 if (acl->cached == 0)
5376 ret = -ERANGE; /* see getxattr(2) man page */
5377 if (acl->len > buflen)
5379 memcpy(buf, acl->data, acl->len);
5383 spin_unlock(&inode->i_lock);
5387 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5389 struct nfs4_cached_acl *acl;
5390 size_t buflen = sizeof(*acl) + acl_len;
5392 if (buflen <= PAGE_SIZE) {
5393 acl = kmalloc(buflen, GFP_KERNEL);
5397 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5399 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5406 nfs4_set_cached_acl(inode, acl);
5410 * The getxattr API returns the required buffer length when called with a
5411 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5412 * the required buf. On a NULL buf, we send a page of data to the server
5413 * guessing that the ACL request can be serviced by a page. If so, we cache
5414 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5415 * the cache. If not so, we throw away the page, and cache the required
5416 * length. The next getxattr call will then produce another round trip to
5417 * the server, this time with the input buf of the required size.
5419 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5421 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5422 struct nfs_getaclargs args = {
5423 .fh = NFS_FH(inode),
5427 struct nfs_getaclres res = {
5430 struct rpc_message msg = {
5431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5435 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5436 int ret = -ENOMEM, i;
5438 if (npages > ARRAY_SIZE(pages))
5441 for (i = 0; i < npages; i++) {
5442 pages[i] = alloc_page(GFP_KERNEL);
5447 /* for decoding across pages */
5448 res.acl_scratch = alloc_page(GFP_KERNEL);
5449 if (!res.acl_scratch)
5452 args.acl_len = npages * PAGE_SIZE;
5454 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5455 __func__, buf, buflen, npages, args.acl_len);
5456 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5457 &msg, &args.seq_args, &res.seq_res, 0);
5461 /* Handle the case where the passed-in buffer is too short */
5462 if (res.acl_flags & NFS4_ACL_TRUNC) {
5463 /* Did the user only issue a request for the acl length? */
5469 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5471 if (res.acl_len > buflen) {
5475 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5480 for (i = 0; i < npages; i++)
5482 __free_page(pages[i]);
5483 if (res.acl_scratch)
5484 __free_page(res.acl_scratch);
5488 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5490 struct nfs4_exception exception = { };
5493 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5494 trace_nfs4_get_acl(inode, ret);
5497 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5498 } while (exception.retry);
5502 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5504 struct nfs_server *server = NFS_SERVER(inode);
5507 if (!nfs4_server_supports_acls(server))
5509 ret = nfs_revalidate_inode(server, inode);
5512 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5513 nfs_zap_acl_cache(inode);
5514 ret = nfs4_read_cached_acl(inode, buf, buflen);
5516 /* -ENOENT is returned if there is no ACL or if there is an ACL
5517 * but no cached acl data, just the acl length */
5519 return nfs4_get_acl_uncached(inode, buf, buflen);
5522 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5524 struct nfs_server *server = NFS_SERVER(inode);
5525 struct page *pages[NFS4ACL_MAXPAGES];
5526 struct nfs_setaclargs arg = {
5527 .fh = NFS_FH(inode),
5531 struct nfs_setaclres res;
5532 struct rpc_message msg = {
5533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5537 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5540 if (!nfs4_server_supports_acls(server))
5542 if (npages > ARRAY_SIZE(pages))
5544 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5547 nfs4_inode_make_writeable(inode);
5548 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5551 * Free each page after tx, so the only ref left is
5552 * held by the network stack
5555 put_page(pages[i-1]);
5558 * Acl update can result in inode attribute update.
5559 * so mark the attribute cache invalid.
5561 spin_lock(&inode->i_lock);
5562 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5563 | NFS_INO_INVALID_CTIME
5564 | NFS_INO_REVAL_FORCED;
5565 spin_unlock(&inode->i_lock);
5566 nfs_access_zap_cache(inode);
5567 nfs_zap_acl_cache(inode);
5571 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5573 struct nfs4_exception exception = { };
5576 err = __nfs4_proc_set_acl(inode, buf, buflen);
5577 trace_nfs4_set_acl(inode, err);
5578 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5580 } while (exception.retry);
5584 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5585 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5588 struct nfs_server *server = NFS_SERVER(inode);
5589 struct nfs_fattr fattr;
5590 struct nfs4_label label = {0, 0, buflen, buf};
5592 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5593 struct nfs4_getattr_arg arg = {
5594 .fh = NFS_FH(inode),
5597 struct nfs4_getattr_res res = {
5602 struct rpc_message msg = {
5603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5609 nfs_fattr_init(&fattr);
5611 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5614 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5616 if (buflen < label.len)
5621 static int nfs4_get_security_label(struct inode *inode, void *buf,
5624 struct nfs4_exception exception = { };
5627 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5631 err = _nfs4_get_security_label(inode, buf, buflen);
5632 trace_nfs4_get_security_label(inode, err);
5633 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5635 } while (exception.retry);
5639 static int _nfs4_do_set_security_label(struct inode *inode,
5640 struct nfs4_label *ilabel,
5641 struct nfs_fattr *fattr,
5642 struct nfs4_label *olabel)
5645 struct iattr sattr = {0};
5646 struct nfs_server *server = NFS_SERVER(inode);
5647 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5648 struct nfs_setattrargs arg = {
5649 .fh = NFS_FH(inode),
5655 struct nfs_setattrres res = {
5660 struct rpc_message msg = {
5661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5667 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5669 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5671 dprintk("%s failed: %d\n", __func__, status);
5676 static int nfs4_do_set_security_label(struct inode *inode,
5677 struct nfs4_label *ilabel,
5678 struct nfs_fattr *fattr,
5679 struct nfs4_label *olabel)
5681 struct nfs4_exception exception = { };
5685 err = _nfs4_do_set_security_label(inode, ilabel,
5687 trace_nfs4_set_security_label(inode, err);
5688 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5690 } while (exception.retry);
5695 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5697 struct nfs4_label ilabel, *olabel = NULL;
5698 struct nfs_fattr fattr;
5701 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5704 nfs_fattr_init(&fattr);
5708 ilabel.label = (char *)buf;
5709 ilabel.len = buflen;
5711 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5712 if (IS_ERR(olabel)) {
5713 status = -PTR_ERR(olabel);
5717 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5719 nfs_setsecurity(inode, &fattr, olabel);
5721 nfs4_label_free(olabel);
5725 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5728 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5729 nfs4_verifier *bootverf)
5733 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5734 /* An impossible timestamp guarantees this value
5735 * will never match a generated boot time. */
5736 verf[0] = cpu_to_be32(U32_MAX);
5737 verf[1] = cpu_to_be32(U32_MAX);
5739 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5740 u64 ns = ktime_to_ns(nn->boot_time);
5742 verf[0] = cpu_to_be32(ns >> 32);
5743 verf[1] = cpu_to_be32(ns);
5745 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5749 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5754 if (clp->cl_owner_id != NULL)
5759 strlen(clp->cl_rpcclient->cl_nodename) +
5761 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5764 if (nfs4_client_id_uniquifier[0] != '\0')
5765 len += strlen(nfs4_client_id_uniquifier) + 1;
5766 if (len > NFS4_OPAQUE_LIMIT + 1)
5770 * Since this string is allocated at mount time, and held until the
5771 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5772 * about a memory-reclaim deadlock.
5774 str = kmalloc(len, GFP_KERNEL);
5779 if (nfs4_client_id_uniquifier[0] != '\0')
5780 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5781 clp->cl_rpcclient->cl_nodename,
5782 nfs4_client_id_uniquifier,
5783 rpc_peeraddr2str(clp->cl_rpcclient,
5786 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5787 clp->cl_rpcclient->cl_nodename,
5788 rpc_peeraddr2str(clp->cl_rpcclient,
5792 clp->cl_owner_id = str;
5797 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5802 len = 10 + 10 + 1 + 10 + 1 +
5803 strlen(nfs4_client_id_uniquifier) + 1 +
5804 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5806 if (len > NFS4_OPAQUE_LIMIT + 1)
5810 * Since this string is allocated at mount time, and held until the
5811 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5812 * about a memory-reclaim deadlock.
5814 str = kmalloc(len, GFP_KERNEL);
5818 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5819 clp->rpc_ops->version, clp->cl_minorversion,
5820 nfs4_client_id_uniquifier,
5821 clp->cl_rpcclient->cl_nodename);
5822 clp->cl_owner_id = str;
5827 nfs4_init_uniform_client_string(struct nfs_client *clp)
5832 if (clp->cl_owner_id != NULL)
5835 if (nfs4_client_id_uniquifier[0] != '\0')
5836 return nfs4_init_uniquifier_client_string(clp);
5838 len = 10 + 10 + 1 + 10 + 1 +
5839 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5841 if (len > NFS4_OPAQUE_LIMIT + 1)
5845 * Since this string is allocated at mount time, and held until the
5846 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5847 * about a memory-reclaim deadlock.
5849 str = kmalloc(len, GFP_KERNEL);
5853 scnprintf(str, len, "Linux NFSv%u.%u %s",
5854 clp->rpc_ops->version, clp->cl_minorversion,
5855 clp->cl_rpcclient->cl_nodename);
5856 clp->cl_owner_id = str;
5861 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5862 * services. Advertise one based on the address family of the
5866 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5868 if (strchr(clp->cl_ipaddr, ':') != NULL)
5869 return scnprintf(buf, len, "tcp6");
5871 return scnprintf(buf, len, "tcp");
5874 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5876 struct nfs4_setclientid *sc = calldata;
5878 if (task->tk_status == 0)
5879 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5882 static const struct rpc_call_ops nfs4_setclientid_ops = {
5883 .rpc_call_done = nfs4_setclientid_done,
5887 * nfs4_proc_setclientid - Negotiate client ID
5888 * @clp: state data structure
5889 * @program: RPC program for NFSv4 callback service
5890 * @port: IP port number for NFS4 callback service
5891 * @cred: credential to use for this call
5892 * @res: where to place the result
5894 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5896 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5897 unsigned short port, const struct cred *cred,
5898 struct nfs4_setclientid_res *res)
5900 nfs4_verifier sc_verifier;
5901 struct nfs4_setclientid setclientid = {
5902 .sc_verifier = &sc_verifier,
5906 struct rpc_message msg = {
5907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5908 .rpc_argp = &setclientid,
5912 struct rpc_task *task;
5913 struct rpc_task_setup task_setup_data = {
5914 .rpc_client = clp->cl_rpcclient,
5915 .rpc_message = &msg,
5916 .callback_ops = &nfs4_setclientid_ops,
5917 .callback_data = &setclientid,
5918 .flags = RPC_TASK_TIMEOUT,
5922 /* nfs_client_id4 */
5923 nfs4_init_boot_verifier(clp, &sc_verifier);
5925 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5926 status = nfs4_init_uniform_client_string(clp);
5928 status = nfs4_init_nonuniform_client_string(clp);
5934 setclientid.sc_netid_len =
5935 nfs4_init_callback_netid(clp,
5936 setclientid.sc_netid,
5937 sizeof(setclientid.sc_netid));
5938 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5939 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5940 clp->cl_ipaddr, port >> 8, port & 255);
5942 dprintk("NFS call setclientid auth=%s, '%s'\n",
5943 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5945 task = rpc_run_task(&task_setup_data);
5947 status = PTR_ERR(task);
5950 status = task->tk_status;
5951 if (setclientid.sc_cred) {
5952 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5953 put_rpccred(setclientid.sc_cred);
5957 trace_nfs4_setclientid(clp, status);
5958 dprintk("NFS reply setclientid: %d\n", status);
5963 * nfs4_proc_setclientid_confirm - Confirm client ID
5964 * @clp: state data structure
5965 * @arg: result of a previous SETCLIENTID
5966 * @cred: credential to use for this call
5968 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5970 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5971 struct nfs4_setclientid_res *arg,
5972 const struct cred *cred)
5974 struct rpc_message msg = {
5975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5981 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5982 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5984 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5985 trace_nfs4_setclientid_confirm(clp, status);
5986 dprintk("NFS reply setclientid_confirm: %d\n", status);
5990 struct nfs4_delegreturndata {
5991 struct nfs4_delegreturnargs args;
5992 struct nfs4_delegreturnres res;
5994 nfs4_stateid stateid;
5995 unsigned long timestamp;
5997 struct nfs4_layoutreturn_args arg;
5998 struct nfs4_layoutreturn_res res;
5999 struct nfs4_xdr_opaque_data ld_private;
6003 struct nfs_fattr fattr;
6005 struct inode *inode;
6008 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6010 struct nfs4_delegreturndata *data = calldata;
6011 struct nfs4_exception exception = {
6012 .inode = data->inode,
6013 .stateid = &data->stateid,
6016 if (!nfs4_sequence_done(task, &data->res.seq_res))
6019 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6021 /* Handle Layoutreturn errors */
6022 if (data->args.lr_args && task->tk_status != 0) {
6023 switch(data->res.lr_ret) {
6025 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6028 data->args.lr_args = NULL;
6029 data->res.lr_res = NULL;
6031 case -NFS4ERR_OLD_STATEID:
6032 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6033 &data->args.lr_args->range,
6037 case -NFS4ERR_ADMIN_REVOKED:
6038 case -NFS4ERR_DELEG_REVOKED:
6039 case -NFS4ERR_EXPIRED:
6040 case -NFS4ERR_BAD_STATEID:
6041 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6042 case -NFS4ERR_WRONG_CRED:
6043 data->args.lr_args = NULL;
6044 data->res.lr_res = NULL;
6049 switch (task->tk_status) {
6051 renew_lease(data->res.server, data->timestamp);
6053 case -NFS4ERR_ADMIN_REVOKED:
6054 case -NFS4ERR_DELEG_REVOKED:
6055 case -NFS4ERR_EXPIRED:
6056 nfs4_free_revoked_stateid(data->res.server,
6058 task->tk_msg.rpc_cred);
6060 case -NFS4ERR_BAD_STATEID:
6061 case -NFS4ERR_STALE_STATEID:
6062 task->tk_status = 0;
6064 case -NFS4ERR_OLD_STATEID:
6065 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6067 task->tk_status = 0;
6069 case -NFS4ERR_ACCESS:
6070 if (data->args.bitmask) {
6071 data->args.bitmask = NULL;
6072 data->res.fattr = NULL;
6077 task->tk_status = nfs4_async_handle_exception(task,
6078 data->res.server, task->tk_status,
6080 if (exception.retry)
6083 data->rpc_status = task->tk_status;
6086 data->res.lr_ret = 0;
6088 task->tk_status = 0;
6089 rpc_restart_call_prepare(task);
6092 static void nfs4_delegreturn_release(void *calldata)
6094 struct nfs4_delegreturndata *data = calldata;
6095 struct inode *inode = data->inode;
6099 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6101 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6102 nfs_iput_and_deactive(inode);
6107 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6109 struct nfs4_delegreturndata *d_data;
6110 struct pnfs_layout_hdr *lo;
6112 d_data = (struct nfs4_delegreturndata *)data;
6114 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6117 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6118 if (lo && !pnfs_layout_is_valid(lo)) {
6119 d_data->args.lr_args = NULL;
6120 d_data->res.lr_res = NULL;
6123 nfs4_setup_sequence(d_data->res.server->nfs_client,
6124 &d_data->args.seq_args,
6125 &d_data->res.seq_res,
6129 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6130 .rpc_call_prepare = nfs4_delegreturn_prepare,
6131 .rpc_call_done = nfs4_delegreturn_done,
6132 .rpc_release = nfs4_delegreturn_release,
6135 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6137 struct nfs4_delegreturndata *data;
6138 struct nfs_server *server = NFS_SERVER(inode);
6139 struct rpc_task *task;
6140 struct rpc_message msg = {
6141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6144 struct rpc_task_setup task_setup_data = {
6145 .rpc_client = server->client,
6146 .rpc_message = &msg,
6147 .callback_ops = &nfs4_delegreturn_ops,
6148 .flags = RPC_TASK_ASYNC,
6152 data = kzalloc(sizeof(*data), GFP_NOFS);
6155 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6157 nfs4_state_protect(server->nfs_client,
6158 NFS_SP4_MACH_CRED_CLEANUP,
6159 &task_setup_data.rpc_client, &msg);
6161 data->args.fhandle = &data->fh;
6162 data->args.stateid = &data->stateid;
6163 data->args.bitmask = server->cache_consistency_bitmask;
6164 nfs_copy_fh(&data->fh, NFS_FH(inode));
6165 nfs4_stateid_copy(&data->stateid, stateid);
6166 data->res.fattr = &data->fattr;
6167 data->res.server = server;
6168 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6169 data->lr.arg.ld_private = &data->lr.ld_private;
6170 nfs_fattr_init(data->res.fattr);
6171 data->timestamp = jiffies;
6172 data->rpc_status = 0;
6173 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6174 data->inode = nfs_igrab_and_active(inode);
6177 data->args.lr_args = &data->lr.arg;
6178 data->res.lr_res = &data->lr.res;
6180 } else if (data->lr.roc) {
6181 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6182 data->lr.roc = false;
6185 task_setup_data.callback_data = data;
6186 msg.rpc_argp = &data->args;
6187 msg.rpc_resp = &data->res;
6188 task = rpc_run_task(&task_setup_data);
6190 return PTR_ERR(task);
6193 status = rpc_wait_for_completion_task(task);
6196 status = data->rpc_status;
6202 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6204 struct nfs_server *server = NFS_SERVER(inode);
6205 struct nfs4_exception exception = { };
6208 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6209 trace_nfs4_delegreturn(inode, stateid, err);
6211 case -NFS4ERR_STALE_STATEID:
6212 case -NFS4ERR_EXPIRED:
6216 err = nfs4_handle_exception(server, err, &exception);
6217 } while (exception.retry);
6221 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6223 struct inode *inode = state->inode;
6224 struct nfs_server *server = NFS_SERVER(inode);
6225 struct nfs_client *clp = server->nfs_client;
6226 struct nfs_lockt_args arg = {
6227 .fh = NFS_FH(inode),
6230 struct nfs_lockt_res res = {
6233 struct rpc_message msg = {
6234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6237 .rpc_cred = state->owner->so_cred,
6239 struct nfs4_lock_state *lsp;
6242 arg.lock_owner.clientid = clp->cl_clientid;
6243 status = nfs4_set_lock_state(state, request);
6246 lsp = request->fl_u.nfs4_fl.owner;
6247 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6248 arg.lock_owner.s_dev = server->s_dev;
6249 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6252 request->fl_type = F_UNLCK;
6254 case -NFS4ERR_DENIED:
6257 request->fl_ops->fl_release_private(request);
6258 request->fl_ops = NULL;
6263 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6265 struct nfs4_exception exception = { };
6269 err = _nfs4_proc_getlk(state, cmd, request);
6270 trace_nfs4_get_lock(request, state, cmd, err);
6271 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6273 } while (exception.retry);
6277 struct nfs4_unlockdata {
6278 struct nfs_locku_args arg;
6279 struct nfs_locku_res res;
6280 struct nfs4_lock_state *lsp;
6281 struct nfs_open_context *ctx;
6282 struct nfs_lock_context *l_ctx;
6283 struct file_lock fl;
6284 struct nfs_server *server;
6285 unsigned long timestamp;
6288 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6289 struct nfs_open_context *ctx,
6290 struct nfs4_lock_state *lsp,
6291 struct nfs_seqid *seqid)
6293 struct nfs4_unlockdata *p;
6294 struct inode *inode = lsp->ls_state->inode;
6296 p = kzalloc(sizeof(*p), GFP_NOFS);
6299 p->arg.fh = NFS_FH(inode);
6301 p->arg.seqid = seqid;
6302 p->res.seqid = seqid;
6304 refcount_inc(&lsp->ls_count);
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 refcount_inc(&lsp->ls_count);
6530 p->ctx = get_nfs_open_context(ctx);
6531 locks_init_lock(&p->fl);
6532 locks_copy_lock(&p->fl, fl);
6535 nfs_free_seqid(p->arg.open_seqid);
6541 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6543 struct nfs4_lockdata *data = calldata;
6544 struct nfs4_state *state = data->lsp->ls_state;
6546 dprintk("%s: begin!\n", __func__);
6547 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6549 /* Do we need to do an open_to_lock_owner? */
6550 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6551 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6552 goto out_release_lock_seqid;
6554 nfs4_stateid_copy(&data->arg.open_stateid,
6555 &state->open_stateid);
6556 data->arg.new_lock_owner = 1;
6557 data->res.open_seqid = data->arg.open_seqid;
6559 data->arg.new_lock_owner = 0;
6560 nfs4_stateid_copy(&data->arg.lock_stateid,
6561 &data->lsp->ls_stateid);
6563 if (!nfs4_valid_open_stateid(state)) {
6564 data->rpc_status = -EBADF;
6565 task->tk_action = NULL;
6566 goto out_release_open_seqid;
6568 data->timestamp = jiffies;
6569 if (nfs4_setup_sequence(data->server->nfs_client,
6570 &data->arg.seq_args,
6574 out_release_open_seqid:
6575 nfs_release_seqid(data->arg.open_seqid);
6576 out_release_lock_seqid:
6577 nfs_release_seqid(data->arg.lock_seqid);
6579 nfs4_sequence_done(task, &data->res.seq_res);
6580 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6583 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6585 struct nfs4_lockdata *data = calldata;
6586 struct nfs4_lock_state *lsp = data->lsp;
6588 dprintk("%s: begin!\n", __func__);
6590 if (!nfs4_sequence_done(task, &data->res.seq_res))
6593 data->rpc_status = task->tk_status;
6594 switch (task->tk_status) {
6596 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6598 if (data->arg.new_lock && !data->cancelled) {
6599 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6600 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6603 if (data->arg.new_lock_owner != 0) {
6604 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6605 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6606 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6607 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6610 case -NFS4ERR_BAD_STATEID:
6611 case -NFS4ERR_OLD_STATEID:
6612 case -NFS4ERR_STALE_STATEID:
6613 case -NFS4ERR_EXPIRED:
6614 if (data->arg.new_lock_owner != 0) {
6615 if (!nfs4_stateid_match(&data->arg.open_stateid,
6616 &lsp->ls_state->open_stateid))
6618 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6623 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6626 if (!data->cancelled)
6627 rpc_restart_call_prepare(task);
6631 static void nfs4_lock_release(void *calldata)
6633 struct nfs4_lockdata *data = calldata;
6635 dprintk("%s: begin!\n", __func__);
6636 nfs_free_seqid(data->arg.open_seqid);
6637 if (data->cancelled && data->rpc_status == 0) {
6638 struct rpc_task *task;
6639 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6640 data->arg.lock_seqid);
6642 rpc_put_task_async(task);
6643 dprintk("%s: cancelling lock!\n", __func__);
6645 nfs_free_seqid(data->arg.lock_seqid);
6646 nfs4_put_lock_state(data->lsp);
6647 put_nfs_open_context(data->ctx);
6649 dprintk("%s: done!\n", __func__);
6652 static const struct rpc_call_ops nfs4_lock_ops = {
6653 .rpc_call_prepare = nfs4_lock_prepare,
6654 .rpc_call_done = nfs4_lock_done,
6655 .rpc_release = nfs4_lock_release,
6658 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6661 case -NFS4ERR_ADMIN_REVOKED:
6662 case -NFS4ERR_EXPIRED:
6663 case -NFS4ERR_BAD_STATEID:
6664 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6665 if (new_lock_owner != 0 ||
6666 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6667 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6669 case -NFS4ERR_STALE_STATEID:
6670 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6671 nfs4_schedule_lease_recovery(server->nfs_client);
6675 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6677 struct nfs4_lockdata *data;
6678 struct rpc_task *task;
6679 struct rpc_message msg = {
6680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6681 .rpc_cred = state->owner->so_cred,
6683 struct rpc_task_setup task_setup_data = {
6684 .rpc_client = NFS_CLIENT(state->inode),
6685 .rpc_message = &msg,
6686 .callback_ops = &nfs4_lock_ops,
6687 .workqueue = nfsiod_workqueue,
6688 .flags = RPC_TASK_ASYNC,
6692 dprintk("%s: begin!\n", __func__);
6693 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6694 fl->fl_u.nfs4_fl.owner,
6695 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6699 data->arg.block = 1;
6700 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6701 recovery_type > NFS_LOCK_NEW);
6702 msg.rpc_argp = &data->arg;
6703 msg.rpc_resp = &data->res;
6704 task_setup_data.callback_data = data;
6705 if (recovery_type > NFS_LOCK_NEW) {
6706 if (recovery_type == NFS_LOCK_RECLAIM)
6707 data->arg.reclaim = NFS_LOCK_RECLAIM;
6709 data->arg.new_lock = 1;
6710 task = rpc_run_task(&task_setup_data);
6712 return PTR_ERR(task);
6713 ret = rpc_wait_for_completion_task(task);
6715 ret = data->rpc_status;
6717 nfs4_handle_setlk_error(data->server, data->lsp,
6718 data->arg.new_lock_owner, ret);
6720 data->cancelled = true;
6722 dprintk("%s: done, ret = %d!\n", __func__, ret);
6723 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6727 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6729 struct nfs_server *server = NFS_SERVER(state->inode);
6730 struct nfs4_exception exception = {
6731 .inode = state->inode,
6736 /* Cache the lock if possible... */
6737 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6739 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6740 if (err != -NFS4ERR_DELAY)
6742 nfs4_handle_exception(server, err, &exception);
6743 } while (exception.retry);
6747 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6749 struct nfs_server *server = NFS_SERVER(state->inode);
6750 struct nfs4_exception exception = {
6751 .inode = state->inode,
6755 err = nfs4_set_lock_state(state, request);
6758 if (!recover_lost_locks) {
6759 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6763 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6765 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6769 case -NFS4ERR_GRACE:
6770 case -NFS4ERR_DELAY:
6771 nfs4_handle_exception(server, err, &exception);
6774 } while (exception.retry);
6779 #if defined(CONFIG_NFS_V4_1)
6780 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6782 struct nfs4_lock_state *lsp;
6785 status = nfs4_set_lock_state(state, request);
6788 lsp = request->fl_u.nfs4_fl.owner;
6789 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6790 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6792 return nfs4_lock_expired(state, request);
6796 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6798 struct nfs_inode *nfsi = NFS_I(state->inode);
6799 struct nfs4_state_owner *sp = state->owner;
6800 unsigned char fl_flags = request->fl_flags;
6803 request->fl_flags |= FL_ACCESS;
6804 status = locks_lock_inode_wait(state->inode, request);
6807 mutex_lock(&sp->so_delegreturn_mutex);
6808 down_read(&nfsi->rwsem);
6809 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6810 /* Yes: cache locks! */
6811 /* ...but avoid races with delegation recall... */
6812 request->fl_flags = fl_flags & ~FL_SLEEP;
6813 status = locks_lock_inode_wait(state->inode, request);
6814 up_read(&nfsi->rwsem);
6815 mutex_unlock(&sp->so_delegreturn_mutex);
6818 up_read(&nfsi->rwsem);
6819 mutex_unlock(&sp->so_delegreturn_mutex);
6820 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6822 request->fl_flags = fl_flags;
6826 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6828 struct nfs4_exception exception = {
6830 .inode = state->inode,
6835 err = _nfs4_proc_setlk(state, cmd, request);
6836 if (err == -NFS4ERR_DENIED)
6838 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6840 } while (exception.retry);
6844 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6845 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6848 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6849 struct file_lock *request)
6851 int status = -ERESTARTSYS;
6852 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6854 while(!signalled()) {
6855 status = nfs4_proc_setlk(state, cmd, request);
6856 if ((status != -EAGAIN) || IS_SETLK(cmd))
6858 freezable_schedule_timeout_interruptible(timeout);
6860 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6861 status = -ERESTARTSYS;
6866 #ifdef CONFIG_NFS_V4_1
6867 struct nfs4_lock_waiter {
6868 struct task_struct *task;
6869 struct inode *inode;
6870 struct nfs_lowner *owner;
6875 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6878 struct nfs4_lock_waiter *waiter = wait->private;
6880 /* NULL key means to wake up everyone */
6882 struct cb_notify_lock_args *cbnl = key;
6883 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6884 *wowner = waiter->owner;
6886 /* Only wake if the callback was for the same owner. */
6887 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6890 /* Make sure it's for the right inode */
6891 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6894 waiter->notified = true;
6897 /* override "private" so we can use default_wake_function */
6898 wait->private = waiter->task;
6899 ret = autoremove_wake_function(wait, mode, flags, key);
6900 wait->private = waiter;
6905 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6907 int status = -ERESTARTSYS;
6908 unsigned long flags;
6909 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6910 struct nfs_server *server = NFS_SERVER(state->inode);
6911 struct nfs_client *clp = server->nfs_client;
6912 wait_queue_head_t *q = &clp->cl_lock_waitq;
6913 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6914 .id = lsp->ls_seqid.owner_id,
6915 .s_dev = server->s_dev };
6916 struct nfs4_lock_waiter waiter = { .task = current,
6917 .inode = state->inode,
6919 .notified = false };
6920 wait_queue_entry_t wait;
6922 /* Don't bother with waitqueue if we don't expect a callback */
6923 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6924 return nfs4_retry_setlk_simple(state, cmd, request);
6927 wait.private = &waiter;
6928 wait.func = nfs4_wake_lock_waiter;
6929 add_wait_queue(q, &wait);
6931 while(!signalled()) {
6932 waiter.notified = false;
6933 status = nfs4_proc_setlk(state, cmd, request);
6934 if ((status != -EAGAIN) || IS_SETLK(cmd))
6937 status = -ERESTARTSYS;
6938 spin_lock_irqsave(&q->lock, flags);
6939 if (waiter.notified) {
6940 spin_unlock_irqrestore(&q->lock, flags);
6943 set_current_state(TASK_INTERRUPTIBLE);
6944 spin_unlock_irqrestore(&q->lock, flags);
6946 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6949 finish_wait(q, &wait);
6952 #else /* !CONFIG_NFS_V4_1 */
6954 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6956 return nfs4_retry_setlk_simple(state, cmd, request);
6961 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6963 struct nfs_open_context *ctx;
6964 struct nfs4_state *state;
6967 /* verify open state */
6968 ctx = nfs_file_open_context(filp);
6971 if (IS_GETLK(cmd)) {
6973 return nfs4_proc_getlk(state, F_GETLK, request);
6977 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6980 if (request->fl_type == F_UNLCK) {
6982 return nfs4_proc_unlck(state, cmd, request);
6989 if ((request->fl_flags & FL_POSIX) &&
6990 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6994 * Don't rely on the VFS having checked the file open mode,
6995 * since it won't do this for flock() locks.
6997 switch (request->fl_type) {
6999 if (!(filp->f_mode & FMODE_READ))
7003 if (!(filp->f_mode & FMODE_WRITE))
7007 status = nfs4_set_lock_state(state, request);
7011 return nfs4_retry_setlk(state, cmd, request);
7014 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7016 struct nfs_server *server = NFS_SERVER(state->inode);
7019 err = nfs4_set_lock_state(state, fl);
7022 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7023 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7026 struct nfs_release_lockowner_data {
7027 struct nfs4_lock_state *lsp;
7028 struct nfs_server *server;
7029 struct nfs_release_lockowner_args args;
7030 struct nfs_release_lockowner_res res;
7031 unsigned long timestamp;
7034 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7036 struct nfs_release_lockowner_data *data = calldata;
7037 struct nfs_server *server = data->server;
7038 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7039 &data->res.seq_res, task);
7040 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7041 data->timestamp = jiffies;
7044 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7046 struct nfs_release_lockowner_data *data = calldata;
7047 struct nfs_server *server = data->server;
7049 nfs40_sequence_done(task, &data->res.seq_res);
7051 switch (task->tk_status) {
7053 renew_lease(server, data->timestamp);
7055 case -NFS4ERR_STALE_CLIENTID:
7056 case -NFS4ERR_EXPIRED:
7057 nfs4_schedule_lease_recovery(server->nfs_client);
7059 case -NFS4ERR_LEASE_MOVED:
7060 case -NFS4ERR_DELAY:
7061 if (nfs4_async_handle_error(task, server,
7062 NULL, NULL) == -EAGAIN)
7063 rpc_restart_call_prepare(task);
7067 static void nfs4_release_lockowner_release(void *calldata)
7069 struct nfs_release_lockowner_data *data = calldata;
7070 nfs4_free_lock_state(data->server, data->lsp);
7074 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7075 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7076 .rpc_call_done = nfs4_release_lockowner_done,
7077 .rpc_release = nfs4_release_lockowner_release,
7081 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7083 struct nfs_release_lockowner_data *data;
7084 struct rpc_message msg = {
7085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7088 if (server->nfs_client->cl_mvops->minor_version != 0)
7091 data = kmalloc(sizeof(*data), GFP_NOFS);
7095 data->server = server;
7096 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7097 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7098 data->args.lock_owner.s_dev = server->s_dev;
7100 msg.rpc_argp = &data->args;
7101 msg.rpc_resp = &data->res;
7102 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7103 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7106 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7108 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7109 struct dentry *unused, struct inode *inode,
7110 const char *key, const void *buf,
7111 size_t buflen, int flags)
7113 return nfs4_proc_set_acl(inode, buf, buflen);
7116 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7117 struct dentry *unused, struct inode *inode,
7118 const char *key, void *buf, size_t buflen)
7120 return nfs4_proc_get_acl(inode, buf, buflen);
7123 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7125 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7128 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7130 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7131 struct dentry *unused, struct inode *inode,
7132 const char *key, const void *buf,
7133 size_t buflen, int flags)
7135 if (security_ismaclabel(key))
7136 return nfs4_set_security_label(inode, buf, buflen);
7141 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7142 struct dentry *unused, struct inode *inode,
7143 const char *key, void *buf, size_t buflen)
7145 if (security_ismaclabel(key))
7146 return nfs4_get_security_label(inode, buf, buflen);
7151 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7155 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7156 len = security_inode_listsecurity(inode, list, list_len);
7157 if (list_len && len > list_len)
7163 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7164 .prefix = XATTR_SECURITY_PREFIX,
7165 .get = nfs4_xattr_get_nfs4_label,
7166 .set = nfs4_xattr_set_nfs4_label,
7172 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7180 * nfs_fhget will use either the mounted_on_fileid or the fileid
7182 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7184 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7185 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7186 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7187 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7190 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7191 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7192 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7196 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7197 const struct qstr *name,
7198 struct nfs4_fs_locations *fs_locations,
7201 struct nfs_server *server = NFS_SERVER(dir);
7203 struct nfs4_fs_locations_arg args = {
7204 .dir_fh = NFS_FH(dir),
7209 struct nfs4_fs_locations_res res = {
7210 .fs_locations = fs_locations,
7212 struct rpc_message msg = {
7213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7219 dprintk("%s: start\n", __func__);
7221 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7222 bitmask[1] = nfs4_fattr_bitmap[1];
7224 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7225 * is not supported */
7226 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7227 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7229 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7231 nfs_fattr_init(&fs_locations->fattr);
7232 fs_locations->server = server;
7233 fs_locations->nlocations = 0;
7234 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7235 dprintk("%s: returned status = %d\n", __func__, status);
7239 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7240 const struct qstr *name,
7241 struct nfs4_fs_locations *fs_locations,
7244 struct nfs4_exception exception = { };
7247 err = _nfs4_proc_fs_locations(client, dir, name,
7248 fs_locations, page);
7249 trace_nfs4_get_fs_locations(dir, name, err);
7250 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7252 } while (exception.retry);
7257 * This operation also signals the server that this client is
7258 * performing migration recovery. The server can stop returning
7259 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7260 * appended to this compound to identify the client ID which is
7261 * performing recovery.
7263 static int _nfs40_proc_get_locations(struct inode *inode,
7264 struct nfs4_fs_locations *locations,
7265 struct page *page, const struct cred *cred)
7267 struct nfs_server *server = NFS_SERVER(inode);
7268 struct rpc_clnt *clnt = server->client;
7270 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7272 struct nfs4_fs_locations_arg args = {
7273 .clientid = server->nfs_client->cl_clientid,
7274 .fh = NFS_FH(inode),
7277 .migration = 1, /* skip LOOKUP */
7278 .renew = 1, /* append RENEW */
7280 struct nfs4_fs_locations_res res = {
7281 .fs_locations = locations,
7285 struct rpc_message msg = {
7286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7291 unsigned long now = jiffies;
7294 nfs_fattr_init(&locations->fattr);
7295 locations->server = server;
7296 locations->nlocations = 0;
7298 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7299 status = nfs4_call_sync_sequence(clnt, server, &msg,
7300 &args.seq_args, &res.seq_res);
7304 renew_lease(server, now);
7308 #ifdef CONFIG_NFS_V4_1
7311 * This operation also signals the server that this client is
7312 * performing migration recovery. The server can stop asserting
7313 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7314 * performing this operation is identified in the SEQUENCE
7315 * operation in this compound.
7317 * When the client supports GETATTR(fs_locations_info), it can
7318 * be plumbed in here.
7320 static int _nfs41_proc_get_locations(struct inode *inode,
7321 struct nfs4_fs_locations *locations,
7322 struct page *page, const struct cred *cred)
7324 struct nfs_server *server = NFS_SERVER(inode);
7325 struct rpc_clnt *clnt = server->client;
7327 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7329 struct nfs4_fs_locations_arg args = {
7330 .fh = NFS_FH(inode),
7333 .migration = 1, /* skip LOOKUP */
7335 struct nfs4_fs_locations_res res = {
7336 .fs_locations = locations,
7339 struct rpc_message msg = {
7340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7347 nfs_fattr_init(&locations->fattr);
7348 locations->server = server;
7349 locations->nlocations = 0;
7351 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7352 status = nfs4_call_sync_sequence(clnt, server, &msg,
7353 &args.seq_args, &res.seq_res);
7354 if (status == NFS4_OK &&
7355 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7356 status = -NFS4ERR_LEASE_MOVED;
7360 #endif /* CONFIG_NFS_V4_1 */
7363 * nfs4_proc_get_locations - discover locations for a migrated FSID
7364 * @inode: inode on FSID that is migrating
7365 * @locations: result of query
7367 * @cred: credential to use for this operation
7369 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7370 * operation failed, or a negative errno if a local error occurred.
7372 * On success, "locations" is filled in, but if the server has
7373 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7376 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7377 * from this client that require migration recovery.
7379 int nfs4_proc_get_locations(struct inode *inode,
7380 struct nfs4_fs_locations *locations,
7381 struct page *page, const struct cred *cred)
7383 struct nfs_server *server = NFS_SERVER(inode);
7384 struct nfs_client *clp = server->nfs_client;
7385 const struct nfs4_mig_recovery_ops *ops =
7386 clp->cl_mvops->mig_recovery_ops;
7387 struct nfs4_exception exception = { };
7390 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7391 (unsigned long long)server->fsid.major,
7392 (unsigned long long)server->fsid.minor,
7394 nfs_display_fhandle(NFS_FH(inode), __func__);
7397 status = ops->get_locations(inode, locations, page, cred);
7398 if (status != -NFS4ERR_DELAY)
7400 nfs4_handle_exception(server, status, &exception);
7401 } while (exception.retry);
7406 * This operation also signals the server that this client is
7407 * performing "lease moved" recovery. The server can stop
7408 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7409 * is appended to this compound to identify the client ID which is
7410 * performing recovery.
7412 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7414 struct nfs_server *server = NFS_SERVER(inode);
7415 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7416 struct rpc_clnt *clnt = server->client;
7417 struct nfs4_fsid_present_arg args = {
7418 .fh = NFS_FH(inode),
7419 .clientid = clp->cl_clientid,
7420 .renew = 1, /* append RENEW */
7422 struct nfs4_fsid_present_res res = {
7425 struct rpc_message msg = {
7426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7431 unsigned long now = jiffies;
7434 res.fh = nfs_alloc_fhandle();
7438 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7439 status = nfs4_call_sync_sequence(clnt, server, &msg,
7440 &args.seq_args, &res.seq_res);
7441 nfs_free_fhandle(res.fh);
7445 do_renew_lease(clp, now);
7449 #ifdef CONFIG_NFS_V4_1
7452 * This operation also signals the server that this client is
7453 * performing "lease moved" recovery. The server can stop asserting
7454 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7455 * this operation is identified in the SEQUENCE operation in this
7458 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7460 struct nfs_server *server = NFS_SERVER(inode);
7461 struct rpc_clnt *clnt = server->client;
7462 struct nfs4_fsid_present_arg args = {
7463 .fh = NFS_FH(inode),
7465 struct nfs4_fsid_present_res res = {
7467 struct rpc_message msg = {
7468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7475 res.fh = nfs_alloc_fhandle();
7479 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7480 status = nfs4_call_sync_sequence(clnt, server, &msg,
7481 &args.seq_args, &res.seq_res);
7482 nfs_free_fhandle(res.fh);
7483 if (status == NFS4_OK &&
7484 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7485 status = -NFS4ERR_LEASE_MOVED;
7489 #endif /* CONFIG_NFS_V4_1 */
7492 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7493 * @inode: inode on FSID to check
7494 * @cred: credential to use for this operation
7496 * Server indicates whether the FSID is present, moved, or not
7497 * recognized. This operation is necessary to clear a LEASE_MOVED
7498 * condition for this client ID.
7500 * Returns NFS4_OK if the FSID is present on this server,
7501 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7502 * NFS4ERR code if some error occurred on the server, or a
7503 * negative errno if a local failure occurred.
7505 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7507 struct nfs_server *server = NFS_SERVER(inode);
7508 struct nfs_client *clp = server->nfs_client;
7509 const struct nfs4_mig_recovery_ops *ops =
7510 clp->cl_mvops->mig_recovery_ops;
7511 struct nfs4_exception exception = { };
7514 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7515 (unsigned long long)server->fsid.major,
7516 (unsigned long long)server->fsid.minor,
7518 nfs_display_fhandle(NFS_FH(inode), __func__);
7521 status = ops->fsid_present(inode, cred);
7522 if (status != -NFS4ERR_DELAY)
7524 nfs4_handle_exception(server, status, &exception);
7525 } while (exception.retry);
7530 * If 'use_integrity' is true and the state managment nfs_client
7531 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7532 * and the machine credential as per RFC3530bis and RFC5661 Security
7533 * Considerations sections. Otherwise, just use the user cred with the
7534 * filesystem's rpc_client.
7536 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7539 struct nfs4_secinfo_arg args = {
7540 .dir_fh = NFS_FH(dir),
7543 struct nfs4_secinfo_res res = {
7546 struct rpc_message msg = {
7547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7551 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7552 const struct cred *cred = NULL;
7554 if (use_integrity) {
7555 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7556 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7557 msg.rpc_cred = cred;
7560 dprintk("NFS call secinfo %s\n", name->name);
7562 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7563 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7565 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7567 dprintk("NFS reply secinfo: %d\n", status);
7574 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7575 struct nfs4_secinfo_flavors *flavors)
7577 struct nfs4_exception exception = { };
7580 err = -NFS4ERR_WRONGSEC;
7582 /* try to use integrity protection with machine cred */
7583 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7584 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7587 * if unable to use integrity protection, or SECINFO with
7588 * integrity protection returns NFS4ERR_WRONGSEC (which is
7589 * disallowed by spec, but exists in deployed servers) use
7590 * the current filesystem's rpc_client and the user cred.
7592 if (err == -NFS4ERR_WRONGSEC)
7593 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7595 trace_nfs4_secinfo(dir, name, err);
7596 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7598 } while (exception.retry);
7602 #ifdef CONFIG_NFS_V4_1
7604 * Check the exchange flags returned by the server for invalid flags, having
7605 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7608 static int nfs4_check_cl_exchange_flags(u32 flags)
7610 if (flags & ~EXCHGID4_FLAG_MASK_R)
7612 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7613 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7615 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7619 return -NFS4ERR_INVAL;
7623 nfs41_same_server_scope(struct nfs41_server_scope *a,
7624 struct nfs41_server_scope *b)
7626 if (a->server_scope_sz != b->server_scope_sz)
7628 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7632 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7636 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7637 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7641 * nfs4_proc_bind_one_conn_to_session()
7643 * The 4.1 client currently uses the same TCP connection for the
7644 * fore and backchannel.
7647 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7648 struct rpc_xprt *xprt,
7649 struct nfs_client *clp,
7650 const struct cred *cred)
7653 struct nfs41_bind_conn_to_session_args args = {
7655 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7657 struct nfs41_bind_conn_to_session_res res;
7658 struct rpc_message msg = {
7660 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7665 struct rpc_task_setup task_setup_data = {
7668 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7669 .rpc_message = &msg,
7670 .flags = RPC_TASK_TIMEOUT,
7672 struct rpc_task *task;
7674 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7675 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7676 args.dir = NFS4_CDFC4_FORE;
7678 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7679 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7680 args.dir = NFS4_CDFC4_FORE;
7682 task = rpc_run_task(&task_setup_data);
7683 if (!IS_ERR(task)) {
7684 status = task->tk_status;
7687 status = PTR_ERR(task);
7688 trace_nfs4_bind_conn_to_session(clp, status);
7690 if (memcmp(res.sessionid.data,
7691 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7692 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7695 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7696 dprintk("NFS: %s: Unexpected direction from server\n",
7700 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7701 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7710 struct rpc_bind_conn_calldata {
7711 struct nfs_client *clp;
7712 const struct cred *cred;
7716 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7717 struct rpc_xprt *xprt,
7720 struct rpc_bind_conn_calldata *p = calldata;
7722 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7725 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7727 struct rpc_bind_conn_calldata data = {
7731 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7732 nfs4_proc_bind_conn_to_session_callback, &data);
7736 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7737 * and operations we'd like to see to enable certain features in the allow map
7739 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7740 .how = SP4_MACH_CRED,
7741 .enforce.u.words = {
7742 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7743 1 << (OP_EXCHANGE_ID - 32) |
7744 1 << (OP_CREATE_SESSION - 32) |
7745 1 << (OP_DESTROY_SESSION - 32) |
7746 1 << (OP_DESTROY_CLIENTID - 32)
7749 [0] = 1 << (OP_CLOSE) |
7750 1 << (OP_OPEN_DOWNGRADE) |
7752 1 << (OP_DELEGRETURN) |
7754 [1] = 1 << (OP_SECINFO - 32) |
7755 1 << (OP_SECINFO_NO_NAME - 32) |
7756 1 << (OP_LAYOUTRETURN - 32) |
7757 1 << (OP_TEST_STATEID - 32) |
7758 1 << (OP_FREE_STATEID - 32) |
7759 1 << (OP_WRITE - 32)
7764 * Select the state protection mode for client `clp' given the server results
7765 * from exchange_id in `sp'.
7767 * Returns 0 on success, negative errno otherwise.
7769 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7770 struct nfs41_state_protection *sp)
7772 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7773 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7774 1 << (OP_EXCHANGE_ID - 32) |
7775 1 << (OP_CREATE_SESSION - 32) |
7776 1 << (OP_DESTROY_SESSION - 32) |
7777 1 << (OP_DESTROY_CLIENTID - 32)
7779 unsigned long flags = 0;
7783 if (sp->how == SP4_MACH_CRED) {
7784 /* Print state protect result */
7785 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7786 for (i = 0; i <= LAST_NFS4_OP; i++) {
7787 if (test_bit(i, sp->enforce.u.longs))
7788 dfprintk(MOUNT, " enforce op %d\n", i);
7789 if (test_bit(i, sp->allow.u.longs))
7790 dfprintk(MOUNT, " allow op %d\n", i);
7793 /* make sure nothing is on enforce list that isn't supported */
7794 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7795 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7796 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7803 * Minimal mode - state operations are allowed to use machine
7804 * credential. Note this already happens by default, so the
7805 * client doesn't have to do anything more than the negotiation.
7807 * NOTE: we don't care if EXCHANGE_ID is in the list -
7808 * we're already using the machine cred for exchange_id
7809 * and will never use a different cred.
7811 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7812 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7813 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7814 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7815 dfprintk(MOUNT, "sp4_mach_cred:\n");
7816 dfprintk(MOUNT, " minimal mode enabled\n");
7817 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7819 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7824 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7825 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7826 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7827 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7828 dfprintk(MOUNT, " cleanup mode enabled\n");
7829 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7832 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7833 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7834 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7837 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7838 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7839 dfprintk(MOUNT, " secinfo mode enabled\n");
7840 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7843 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7844 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7845 dfprintk(MOUNT, " stateid mode enabled\n");
7846 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7849 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7850 dfprintk(MOUNT, " write mode enabled\n");
7851 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7854 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7855 dfprintk(MOUNT, " commit mode enabled\n");
7856 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7860 clp->cl_sp4_flags = flags;
7864 struct nfs41_exchange_id_data {
7865 struct nfs41_exchange_id_res res;
7866 struct nfs41_exchange_id_args args;
7869 static void nfs4_exchange_id_release(void *data)
7871 struct nfs41_exchange_id_data *cdata =
7872 (struct nfs41_exchange_id_data *)data;
7874 nfs_put_client(cdata->args.client);
7875 kfree(cdata->res.impl_id);
7876 kfree(cdata->res.server_scope);
7877 kfree(cdata->res.server_owner);
7881 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7882 .rpc_release = nfs4_exchange_id_release,
7886 * _nfs4_proc_exchange_id()
7888 * Wrapper for EXCHANGE_ID operation.
7890 static struct rpc_task *
7891 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
7892 u32 sp4_how, struct rpc_xprt *xprt)
7894 struct rpc_message msg = {
7895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7898 struct rpc_task_setup task_setup_data = {
7899 .rpc_client = clp->cl_rpcclient,
7900 .callback_ops = &nfs4_exchange_id_call_ops,
7901 .rpc_message = &msg,
7902 .flags = RPC_TASK_TIMEOUT,
7904 struct nfs41_exchange_id_data *calldata;
7907 if (!refcount_inc_not_zero(&clp->cl_count))
7908 return ERR_PTR(-EIO);
7911 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7915 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7917 status = nfs4_init_uniform_client_string(clp);
7921 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7924 if (unlikely(calldata->res.server_owner == NULL))
7927 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7929 if (unlikely(calldata->res.server_scope == NULL))
7930 goto out_server_owner;
7932 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7933 if (unlikely(calldata->res.impl_id == NULL))
7934 goto out_server_scope;
7938 calldata->args.state_protect.how = SP4_NONE;
7942 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7952 task_setup_data.rpc_xprt = xprt;
7953 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7954 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7955 sizeof(calldata->args.verifier.data));
7957 calldata->args.client = clp;
7958 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7959 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7960 #ifdef CONFIG_NFS_V4_1_MIGRATION
7961 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7963 msg.rpc_argp = &calldata->args;
7964 msg.rpc_resp = &calldata->res;
7965 task_setup_data.callback_data = calldata;
7967 return rpc_run_task(&task_setup_data);
7970 kfree(calldata->res.impl_id);
7972 kfree(calldata->res.server_scope);
7974 kfree(calldata->res.server_owner);
7978 nfs_put_client(clp);
7979 return ERR_PTR(status);
7983 * _nfs4_proc_exchange_id()
7985 * Wrapper for EXCHANGE_ID operation.
7987 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
7990 struct rpc_task *task;
7991 struct nfs41_exchange_id_args *argp;
7992 struct nfs41_exchange_id_res *resp;
7995 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7997 return PTR_ERR(task);
7999 argp = task->tk_msg.rpc_argp;
8000 resp = task->tk_msg.rpc_resp;
8001 status = task->tk_status;
8005 status = nfs4_check_cl_exchange_flags(resp->flags);
8009 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8013 clp->cl_clientid = resp->clientid;
8014 clp->cl_exchange_flags = resp->flags;
8015 clp->cl_seqid = resp->seqid;
8016 /* Client ID is not confirmed */
8017 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8018 clear_bit(NFS4_SESSION_ESTABLISHED,
8019 &clp->cl_session->session_state);
8021 if (clp->cl_serverscope != NULL &&
8022 !nfs41_same_server_scope(clp->cl_serverscope,
8023 resp->server_scope)) {
8024 dprintk("%s: server_scope mismatch detected\n",
8026 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8029 swap(clp->cl_serverowner, resp->server_owner);
8030 swap(clp->cl_serverscope, resp->server_scope);
8031 swap(clp->cl_implid, resp->impl_id);
8033 /* Save the EXCHANGE_ID verifier session trunk tests */
8034 memcpy(clp->cl_confirm.data, argp->verifier.data,
8035 sizeof(clp->cl_confirm.data));
8037 trace_nfs4_exchange_id(clp, status);
8043 * nfs4_proc_exchange_id()
8045 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8047 * Since the clientid has expired, all compounds using sessions
8048 * associated with the stale clientid will be returning
8049 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8050 * be in some phase of session reset.
8052 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8054 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8056 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8059 /* try SP4_MACH_CRED if krb5i/p */
8060 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8061 authflavor == RPC_AUTH_GSS_KRB5P) {
8062 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8068 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8072 * nfs4_test_session_trunk
8074 * This is an add_xprt_test() test function called from
8075 * rpc_clnt_setup_test_and_add_xprt.
8077 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8078 * and is dereferrenced in nfs4_exchange_id_release
8080 * Upon success, add the new transport to the rpc_clnt
8082 * @clnt: struct rpc_clnt to get new transport
8083 * @xprt: the rpc_xprt to test
8084 * @data: call data for _nfs4_proc_exchange_id.
8086 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8089 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8090 struct rpc_task *task;
8095 dprintk("--> %s try %s\n", __func__,
8096 xprt->address_strings[RPC_DISPLAY_ADDR]);
8098 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8100 /* Test connection for session trunking. Async exchange_id call */
8101 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8105 status = task->tk_status;
8107 status = nfs4_detect_session_trunking(adata->clp,
8108 task->tk_msg.rpc_resp, xprt);
8111 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8115 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8117 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8118 const struct cred *cred)
8120 struct rpc_message msg = {
8121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8127 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8128 trace_nfs4_destroy_clientid(clp, status);
8130 dprintk("NFS: Got error %d from the server %s on "
8131 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8135 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8136 const struct cred *cred)
8141 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8142 ret = _nfs4_proc_destroy_clientid(clp, cred);
8144 case -NFS4ERR_DELAY:
8145 case -NFS4ERR_CLIENTID_BUSY:
8155 int nfs4_destroy_clientid(struct nfs_client *clp)
8157 const struct cred *cred;
8160 if (clp->cl_mvops->minor_version < 1)
8162 if (clp->cl_exchange_flags == 0)
8164 if (clp->cl_preserve_clid)
8166 cred = nfs4_get_clid_cred(clp);
8167 ret = nfs4_proc_destroy_clientid(clp, cred);
8171 case -NFS4ERR_STALE_CLIENTID:
8172 clp->cl_exchange_flags = 0;
8178 struct nfs4_get_lease_time_data {
8179 struct nfs4_get_lease_time_args *args;
8180 struct nfs4_get_lease_time_res *res;
8181 struct nfs_client *clp;
8184 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8187 struct nfs4_get_lease_time_data *data =
8188 (struct nfs4_get_lease_time_data *)calldata;
8190 dprintk("--> %s\n", __func__);
8191 /* just setup sequence, do not trigger session recovery
8192 since we're invoked within one */
8193 nfs4_setup_sequence(data->clp,
8194 &data->args->la_seq_args,
8195 &data->res->lr_seq_res,
8197 dprintk("<-- %s\n", __func__);
8201 * Called from nfs4_state_manager thread for session setup, so don't recover
8202 * from sequence operation or clientid errors.
8204 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8206 struct nfs4_get_lease_time_data *data =
8207 (struct nfs4_get_lease_time_data *)calldata;
8209 dprintk("--> %s\n", __func__);
8210 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8212 switch (task->tk_status) {
8213 case -NFS4ERR_DELAY:
8214 case -NFS4ERR_GRACE:
8215 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8216 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8217 task->tk_status = 0;
8219 case -NFS4ERR_RETRY_UNCACHED_REP:
8220 rpc_restart_call_prepare(task);
8223 dprintk("<-- %s\n", __func__);
8226 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8227 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8228 .rpc_call_done = nfs4_get_lease_time_done,
8231 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8233 struct rpc_task *task;
8234 struct nfs4_get_lease_time_args args;
8235 struct nfs4_get_lease_time_res res = {
8236 .lr_fsinfo = fsinfo,
8238 struct nfs4_get_lease_time_data data = {
8243 struct rpc_message msg = {
8244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8248 struct rpc_task_setup task_setup = {
8249 .rpc_client = clp->cl_rpcclient,
8250 .rpc_message = &msg,
8251 .callback_ops = &nfs4_get_lease_time_ops,
8252 .callback_data = &data,
8253 .flags = RPC_TASK_TIMEOUT,
8257 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8258 task = rpc_run_task(&task_setup);
8261 return PTR_ERR(task);
8263 status = task->tk_status;
8269 * Initialize the values to be used by the client in CREATE_SESSION
8270 * If nfs4_init_session set the fore channel request and response sizes,
8273 * Set the back channel max_resp_sz_cached to zero to force the client to
8274 * always set csa_cachethis to FALSE because the current implementation
8275 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8277 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8278 struct rpc_clnt *clnt)
8280 unsigned int max_rqst_sz, max_resp_sz;
8281 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8283 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8284 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8286 /* Fore channel attributes */
8287 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8288 args->fc_attrs.max_resp_sz = max_resp_sz;
8289 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8290 args->fc_attrs.max_reqs = max_session_slots;
8292 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8293 "max_ops=%u max_reqs=%u\n",
8295 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8296 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8298 /* Back channel attributes */
8299 args->bc_attrs.max_rqst_sz = max_bc_payload;
8300 args->bc_attrs.max_resp_sz = max_bc_payload;
8301 args->bc_attrs.max_resp_sz_cached = 0;
8302 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8303 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8305 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8306 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8308 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8309 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8310 args->bc_attrs.max_reqs);
8313 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8314 struct nfs41_create_session_res *res)
8316 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8317 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8319 if (rcvd->max_resp_sz > sent->max_resp_sz)
8322 * Our requested max_ops is the minimum we need; we're not
8323 * prepared to break up compounds into smaller pieces than that.
8324 * So, no point even trying to continue if the server won't
8327 if (rcvd->max_ops < sent->max_ops)
8329 if (rcvd->max_reqs == 0)
8331 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8332 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8336 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8337 struct nfs41_create_session_res *res)
8339 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8340 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8342 if (!(res->flags & SESSION4_BACK_CHAN))
8344 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8346 if (rcvd->max_resp_sz < sent->max_resp_sz)
8348 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8350 if (rcvd->max_ops > sent->max_ops)
8352 if (rcvd->max_reqs > sent->max_reqs)
8358 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8359 struct nfs41_create_session_res *res)
8363 ret = nfs4_verify_fore_channel_attrs(args, res);
8366 return nfs4_verify_back_channel_attrs(args, res);
8369 static void nfs4_update_session(struct nfs4_session *session,
8370 struct nfs41_create_session_res *res)
8372 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8373 /* Mark client id and session as being confirmed */
8374 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8375 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8376 session->flags = res->flags;
8377 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8378 if (res->flags & SESSION4_BACK_CHAN)
8379 memcpy(&session->bc_attrs, &res->bc_attrs,
8380 sizeof(session->bc_attrs));
8383 static int _nfs4_proc_create_session(struct nfs_client *clp,
8384 const struct cred *cred)
8386 struct nfs4_session *session = clp->cl_session;
8387 struct nfs41_create_session_args args = {
8389 .clientid = clp->cl_clientid,
8390 .seqid = clp->cl_seqid,
8391 .cb_program = NFS4_CALLBACK,
8393 struct nfs41_create_session_res res;
8395 struct rpc_message msg = {
8396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8403 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8404 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8406 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8407 trace_nfs4_create_session(clp, status);
8410 case -NFS4ERR_STALE_CLIENTID:
8411 case -NFS4ERR_DELAY:
8420 /* Verify the session's negotiated channel_attrs values */
8421 status = nfs4_verify_channel_attrs(&args, &res);
8422 /* Increment the clientid slot sequence id */
8425 nfs4_update_session(session, &res);
8432 * Issues a CREATE_SESSION operation to the server.
8433 * It is the responsibility of the caller to verify the session is
8434 * expired before calling this routine.
8436 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8440 struct nfs4_session *session = clp->cl_session;
8442 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8444 status = _nfs4_proc_create_session(clp, cred);
8448 /* Init or reset the session slot tables */
8449 status = nfs4_setup_session_slot_tables(session);
8450 dprintk("slot table setup returned %d\n", status);
8454 ptr = (unsigned *)&session->sess_id.data[0];
8455 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8456 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8458 dprintk("<-- %s\n", __func__);
8463 * Issue the over-the-wire RPC DESTROY_SESSION.
8464 * The caller must serialize access to this routine.
8466 int nfs4_proc_destroy_session(struct nfs4_session *session,
8467 const struct cred *cred)
8469 struct rpc_message msg = {
8470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8471 .rpc_argp = session,
8476 dprintk("--> nfs4_proc_destroy_session\n");
8478 /* session is still being setup */
8479 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8482 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8483 trace_nfs4_destroy_session(session->clp, status);
8486 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8487 "Session has been destroyed regardless...\n", status);
8489 dprintk("<-- nfs4_proc_destroy_session\n");
8494 * Renew the cl_session lease.
8496 struct nfs4_sequence_data {
8497 struct nfs_client *clp;
8498 struct nfs4_sequence_args args;
8499 struct nfs4_sequence_res res;
8502 static void nfs41_sequence_release(void *data)
8504 struct nfs4_sequence_data *calldata = data;
8505 struct nfs_client *clp = calldata->clp;
8507 if (refcount_read(&clp->cl_count) > 1)
8508 nfs4_schedule_state_renewal(clp);
8509 nfs_put_client(clp);
8513 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8515 switch(task->tk_status) {
8516 case -NFS4ERR_DELAY:
8517 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8520 nfs4_schedule_lease_recovery(clp);
8525 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8527 struct nfs4_sequence_data *calldata = data;
8528 struct nfs_client *clp = calldata->clp;
8530 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8533 trace_nfs4_sequence(clp, task->tk_status);
8534 if (task->tk_status < 0) {
8535 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8536 if (refcount_read(&clp->cl_count) == 1)
8539 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8540 rpc_restart_call_prepare(task);
8544 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8546 dprintk("<-- %s\n", __func__);
8549 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8551 struct nfs4_sequence_data *calldata = data;
8552 struct nfs_client *clp = calldata->clp;
8553 struct nfs4_sequence_args *args;
8554 struct nfs4_sequence_res *res;
8556 args = task->tk_msg.rpc_argp;
8557 res = task->tk_msg.rpc_resp;
8559 nfs4_setup_sequence(clp, args, res, task);
8562 static const struct rpc_call_ops nfs41_sequence_ops = {
8563 .rpc_call_done = nfs41_sequence_call_done,
8564 .rpc_call_prepare = nfs41_sequence_prepare,
8565 .rpc_release = nfs41_sequence_release,
8568 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8569 const struct cred *cred,
8570 struct nfs4_slot *slot,
8573 struct nfs4_sequence_data *calldata;
8574 struct rpc_message msg = {
8575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8578 struct rpc_task_setup task_setup_data = {
8579 .rpc_client = clp->cl_rpcclient,
8580 .rpc_message = &msg,
8581 .callback_ops = &nfs41_sequence_ops,
8582 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8584 struct rpc_task *ret;
8586 ret = ERR_PTR(-EIO);
8587 if (!refcount_inc_not_zero(&clp->cl_count))
8590 ret = ERR_PTR(-ENOMEM);
8591 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8592 if (calldata == NULL)
8594 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8595 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8596 msg.rpc_argp = &calldata->args;
8597 msg.rpc_resp = &calldata->res;
8598 calldata->clp = clp;
8599 task_setup_data.callback_data = calldata;
8601 ret = rpc_run_task(&task_setup_data);
8606 nfs_put_client(clp);
8608 nfs41_release_slot(slot);
8612 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8614 struct rpc_task *task;
8617 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8619 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8621 ret = PTR_ERR(task);
8623 rpc_put_task_async(task);
8624 dprintk("<-- %s status=%d\n", __func__, ret);
8628 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8630 struct rpc_task *task;
8633 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8635 ret = PTR_ERR(task);
8638 ret = rpc_wait_for_completion_task(task);
8640 ret = task->tk_status;
8643 dprintk("<-- %s status=%d\n", __func__, ret);
8647 struct nfs4_reclaim_complete_data {
8648 struct nfs_client *clp;
8649 struct nfs41_reclaim_complete_args arg;
8650 struct nfs41_reclaim_complete_res res;
8653 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8655 struct nfs4_reclaim_complete_data *calldata = data;
8657 nfs4_setup_sequence(calldata->clp,
8658 &calldata->arg.seq_args,
8659 &calldata->res.seq_res,
8663 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8665 switch(task->tk_status) {
8667 wake_up_all(&clp->cl_lock_waitq);
8669 case -NFS4ERR_COMPLETE_ALREADY:
8670 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8672 case -NFS4ERR_DELAY:
8673 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8675 case -NFS4ERR_RETRY_UNCACHED_REP:
8677 case -NFS4ERR_BADSESSION:
8678 case -NFS4ERR_DEADSESSION:
8679 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8680 nfs4_schedule_session_recovery(clp->cl_session,
8684 nfs4_schedule_lease_recovery(clp);
8689 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8691 struct nfs4_reclaim_complete_data *calldata = data;
8692 struct nfs_client *clp = calldata->clp;
8693 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8695 dprintk("--> %s\n", __func__);
8696 if (!nfs41_sequence_done(task, res))
8699 trace_nfs4_reclaim_complete(clp, task->tk_status);
8700 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8701 rpc_restart_call_prepare(task);
8704 dprintk("<-- %s\n", __func__);
8707 static void nfs4_free_reclaim_complete_data(void *data)
8709 struct nfs4_reclaim_complete_data *calldata = data;
8714 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8715 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8716 .rpc_call_done = nfs4_reclaim_complete_done,
8717 .rpc_release = nfs4_free_reclaim_complete_data,
8721 * Issue a global reclaim complete.
8723 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8724 const struct cred *cred)
8726 struct nfs4_reclaim_complete_data *calldata;
8727 struct rpc_task *task;
8728 struct rpc_message msg = {
8729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8732 struct rpc_task_setup task_setup_data = {
8733 .rpc_client = clp->cl_rpcclient,
8734 .rpc_message = &msg,
8735 .callback_ops = &nfs4_reclaim_complete_call_ops,
8736 .flags = RPC_TASK_ASYNC,
8738 int status = -ENOMEM;
8740 dprintk("--> %s\n", __func__);
8741 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8742 if (calldata == NULL)
8744 calldata->clp = clp;
8745 calldata->arg.one_fs = 0;
8747 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8748 msg.rpc_argp = &calldata->arg;
8749 msg.rpc_resp = &calldata->res;
8750 task_setup_data.callback_data = calldata;
8751 task = rpc_run_task(&task_setup_data);
8753 status = PTR_ERR(task);
8756 status = rpc_wait_for_completion_task(task);
8758 status = task->tk_status;
8761 dprintk("<-- %s status=%d\n", __func__, status);
8766 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8768 struct nfs4_layoutget *lgp = calldata;
8769 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8771 dprintk("--> %s\n", __func__);
8772 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8773 &lgp->res.seq_res, task);
8774 dprintk("<-- %s\n", __func__);
8777 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8779 struct nfs4_layoutget *lgp = calldata;
8781 dprintk("--> %s\n", __func__);
8782 nfs41_sequence_process(task, &lgp->res.seq_res);
8783 dprintk("<-- %s\n", __func__);
8787 nfs4_layoutget_handle_exception(struct rpc_task *task,
8788 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8790 struct inode *inode = lgp->args.inode;
8791 struct nfs_server *server = NFS_SERVER(inode);
8792 struct pnfs_layout_hdr *lo;
8793 int nfs4err = task->tk_status;
8794 int err, status = 0;
8797 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8799 nfs4_sequence_free_slot(&lgp->res.seq_res);
8806 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8807 * on the file. set tk_status to -ENODATA to tell upper layer to
8810 case -NFS4ERR_LAYOUTUNAVAILABLE:
8814 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8815 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8817 case -NFS4ERR_BADLAYOUT:
8818 status = -EOVERFLOW;
8821 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8822 * (or clients) writing to the same RAID stripe except when
8823 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8825 * Treat it like we would RECALLCONFLICT -- we retry for a little
8826 * while, and then eventually give up.
8828 case -NFS4ERR_LAYOUTTRYLATER:
8829 if (lgp->args.minlength == 0) {
8830 status = -EOVERFLOW;
8835 case -NFS4ERR_RECALLCONFLICT:
8836 status = -ERECALLCONFLICT;
8838 case -NFS4ERR_DELEG_REVOKED:
8839 case -NFS4ERR_ADMIN_REVOKED:
8840 case -NFS4ERR_EXPIRED:
8841 case -NFS4ERR_BAD_STATEID:
8842 exception->timeout = 0;
8843 spin_lock(&inode->i_lock);
8844 lo = NFS_I(inode)->layout;
8845 /* If the open stateid was bad, then recover it. */
8846 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8847 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8848 spin_unlock(&inode->i_lock);
8849 exception->state = lgp->args.ctx->state;
8850 exception->stateid = &lgp->args.stateid;
8855 * Mark the bad layout state as invalid, then retry
8857 pnfs_mark_layout_stateid_invalid(lo, &head);
8858 spin_unlock(&inode->i_lock);
8859 nfs_commit_inode(inode, 0);
8860 pnfs_free_lseg_list(&head);
8865 err = nfs4_handle_exception(server, nfs4err, exception);
8867 if (exception->retry)
8873 dprintk("<-- %s\n", __func__);
8877 size_t max_response_pages(struct nfs_server *server)
8879 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8880 return nfs_page_array_len(0, max_resp_sz);
8883 static void nfs4_layoutget_release(void *calldata)
8885 struct nfs4_layoutget *lgp = calldata;
8887 dprintk("--> %s\n", __func__);
8888 nfs4_sequence_free_slot(&lgp->res.seq_res);
8889 pnfs_layoutget_free(lgp);
8890 dprintk("<-- %s\n", __func__);
8893 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8894 .rpc_call_prepare = nfs4_layoutget_prepare,
8895 .rpc_call_done = nfs4_layoutget_done,
8896 .rpc_release = nfs4_layoutget_release,
8899 struct pnfs_layout_segment *
8900 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
8902 struct inode *inode = lgp->args.inode;
8903 struct nfs_server *server = NFS_SERVER(inode);
8904 struct rpc_task *task;
8905 struct rpc_message msg = {
8906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8907 .rpc_argp = &lgp->args,
8908 .rpc_resp = &lgp->res,
8909 .rpc_cred = lgp->cred,
8911 struct rpc_task_setup task_setup_data = {
8912 .rpc_client = server->client,
8913 .rpc_message = &msg,
8914 .callback_ops = &nfs4_layoutget_call_ops,
8915 .callback_data = lgp,
8916 .flags = RPC_TASK_ASYNC,
8918 struct pnfs_layout_segment *lseg = NULL;
8919 struct nfs4_exception exception = {
8921 .timeout = *timeout,
8925 dprintk("--> %s\n", __func__);
8927 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8928 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8930 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
8932 task = rpc_run_task(&task_setup_data);
8934 return ERR_CAST(task);
8935 status = rpc_wait_for_completion_task(task);
8939 if (task->tk_status < 0) {
8940 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8941 *timeout = exception.timeout;
8942 } else if (lgp->res.layoutp->len == 0) {
8944 *timeout = nfs4_update_delay(&exception.timeout);
8946 lseg = pnfs_layout_process(lgp);
8948 trace_nfs4_layoutget(lgp->args.ctx,
8955 dprintk("<-- %s status=%d\n", __func__, status);
8957 return ERR_PTR(status);
8962 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8964 struct nfs4_layoutreturn *lrp = calldata;
8966 dprintk("--> %s\n", __func__);
8967 nfs4_setup_sequence(lrp->clp,
8968 &lrp->args.seq_args,
8971 if (!pnfs_layout_is_valid(lrp->args.layout))
8975 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8977 struct nfs4_layoutreturn *lrp = calldata;
8978 struct nfs_server *server;
8980 dprintk("--> %s\n", __func__);
8982 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8985 server = NFS_SERVER(lrp->args.inode);
8986 switch (task->tk_status) {
8987 case -NFS4ERR_OLD_STATEID:
8988 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
8994 task->tk_status = 0;
8998 case -NFS4ERR_DELAY:
8999 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9003 dprintk("<-- %s\n", __func__);
9006 task->tk_status = 0;
9007 nfs4_sequence_free_slot(&lrp->res.seq_res);
9008 rpc_restart_call_prepare(task);
9011 static void nfs4_layoutreturn_release(void *calldata)
9013 struct nfs4_layoutreturn *lrp = calldata;
9014 struct pnfs_layout_hdr *lo = lrp->args.layout;
9016 dprintk("--> %s\n", __func__);
9017 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9018 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9019 nfs4_sequence_free_slot(&lrp->res.seq_res);
9020 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9021 lrp->ld_private.ops->free(&lrp->ld_private);
9022 pnfs_put_layout_hdr(lrp->args.layout);
9023 nfs_iput_and_deactive(lrp->inode);
9025 dprintk("<-- %s\n", __func__);
9028 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9029 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9030 .rpc_call_done = nfs4_layoutreturn_done,
9031 .rpc_release = nfs4_layoutreturn_release,
9034 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9036 struct rpc_task *task;
9037 struct rpc_message msg = {
9038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9039 .rpc_argp = &lrp->args,
9040 .rpc_resp = &lrp->res,
9041 .rpc_cred = lrp->cred,
9043 struct rpc_task_setup task_setup_data = {
9044 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9045 .rpc_message = &msg,
9046 .callback_ops = &nfs4_layoutreturn_call_ops,
9047 .callback_data = lrp,
9051 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9052 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9053 &task_setup_data.rpc_client, &msg);
9055 dprintk("--> %s\n", __func__);
9057 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9059 nfs4_layoutreturn_release(lrp);
9062 task_setup_data.flags |= RPC_TASK_ASYNC;
9064 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9065 task = rpc_run_task(&task_setup_data);
9067 return PTR_ERR(task);
9069 status = task->tk_status;
9070 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9071 dprintk("<-- %s status=%d\n", __func__, status);
9077 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9078 struct pnfs_device *pdev,
9079 const struct cred *cred)
9081 struct nfs4_getdeviceinfo_args args = {
9083 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9084 NOTIFY_DEVICEID4_DELETE,
9086 struct nfs4_getdeviceinfo_res res = {
9089 struct rpc_message msg = {
9090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9097 dprintk("--> %s\n", __func__);
9098 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9099 if (res.notification & ~args.notify_types)
9100 dprintk("%s: unsupported notification\n", __func__);
9101 if (res.notification != args.notify_types)
9104 dprintk("<-- %s status=%d\n", __func__, status);
9109 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9110 struct pnfs_device *pdev,
9111 const struct cred *cred)
9113 struct nfs4_exception exception = { };
9117 err = nfs4_handle_exception(server,
9118 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9120 } while (exception.retry);
9123 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9125 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9127 struct nfs4_layoutcommit_data *data = calldata;
9128 struct nfs_server *server = NFS_SERVER(data->args.inode);
9130 nfs4_setup_sequence(server->nfs_client,
9131 &data->args.seq_args,
9137 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9139 struct nfs4_layoutcommit_data *data = calldata;
9140 struct nfs_server *server = NFS_SERVER(data->args.inode);
9142 if (!nfs41_sequence_done(task, &data->res.seq_res))
9145 switch (task->tk_status) { /* Just ignore these failures */
9146 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9147 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9148 case -NFS4ERR_BADLAYOUT: /* no layout */
9149 case -NFS4ERR_GRACE: /* loca_recalim always false */
9150 task->tk_status = 0;
9154 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9155 rpc_restart_call_prepare(task);
9161 static void nfs4_layoutcommit_release(void *calldata)
9163 struct nfs4_layoutcommit_data *data = calldata;
9165 pnfs_cleanup_layoutcommit(data);
9166 nfs_post_op_update_inode_force_wcc(data->args.inode,
9168 put_cred(data->cred);
9169 nfs_iput_and_deactive(data->inode);
9173 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9174 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9175 .rpc_call_done = nfs4_layoutcommit_done,
9176 .rpc_release = nfs4_layoutcommit_release,
9180 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9182 struct rpc_message msg = {
9183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9184 .rpc_argp = &data->args,
9185 .rpc_resp = &data->res,
9186 .rpc_cred = data->cred,
9188 struct rpc_task_setup task_setup_data = {
9189 .task = &data->task,
9190 .rpc_client = NFS_CLIENT(data->args.inode),
9191 .rpc_message = &msg,
9192 .callback_ops = &nfs4_layoutcommit_ops,
9193 .callback_data = data,
9195 struct rpc_task *task;
9198 dprintk("NFS: initiating layoutcommit call. sync %d "
9199 "lbw: %llu inode %lu\n", sync,
9200 data->args.lastbytewritten,
9201 data->args.inode->i_ino);
9204 data->inode = nfs_igrab_and_active(data->args.inode);
9205 if (data->inode == NULL) {
9206 nfs4_layoutcommit_release(data);
9209 task_setup_data.flags = RPC_TASK_ASYNC;
9211 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9212 task = rpc_run_task(&task_setup_data);
9214 return PTR_ERR(task);
9216 status = task->tk_status;
9217 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9218 dprintk("%s: status %d\n", __func__, status);
9224 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9225 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9228 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9229 struct nfs_fsinfo *info,
9230 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9232 struct nfs41_secinfo_no_name_args args = {
9233 .style = SECINFO_STYLE_CURRENT_FH,
9235 struct nfs4_secinfo_res res = {
9238 struct rpc_message msg = {
9239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9243 struct rpc_clnt *clnt = server->client;
9244 const struct cred *cred = NULL;
9247 if (use_integrity) {
9248 clnt = server->nfs_client->cl_rpcclient;
9249 cred = nfs4_get_clid_cred(server->nfs_client);
9250 msg.rpc_cred = cred;
9253 dprintk("--> %s\n", __func__);
9254 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9256 dprintk("<-- %s status=%d\n", __func__, status);
9264 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9265 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9267 struct nfs4_exception exception = { };
9270 /* first try using integrity protection */
9271 err = -NFS4ERR_WRONGSEC;
9273 /* try to use integrity protection with machine cred */
9274 if (_nfs4_is_integrity_protected(server->nfs_client))
9275 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9279 * if unable to use integrity protection, or SECINFO with
9280 * integrity protection returns NFS4ERR_WRONGSEC (which is
9281 * disallowed by spec, but exists in deployed servers) use
9282 * the current filesystem's rpc_client and the user cred.
9284 if (err == -NFS4ERR_WRONGSEC)
9285 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9290 case -NFS4ERR_WRONGSEC:
9294 err = nfs4_handle_exception(server, err, &exception);
9296 } while (exception.retry);
9302 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9303 struct nfs_fsinfo *info)
9307 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9308 struct nfs4_secinfo_flavors *flavors;
9309 struct nfs4_secinfo4 *secinfo;
9312 page = alloc_page(GFP_KERNEL);
9318 flavors = page_address(page);
9319 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9322 * Fall back on "guess and check" method if
9323 * the server doesn't support SECINFO_NO_NAME
9325 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9326 err = nfs4_find_root_sec(server, fhandle, info);
9332 for (i = 0; i < flavors->num_flavors; i++) {
9333 secinfo = &flavors->flavors[i];
9335 switch (secinfo->flavor) {
9339 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9340 &secinfo->flavor_info);
9343 flavor = RPC_AUTH_MAXFLAVOR;
9347 if (!nfs_auth_info_match(&server->auth_info, flavor))
9348 flavor = RPC_AUTH_MAXFLAVOR;
9350 if (flavor != RPC_AUTH_MAXFLAVOR) {
9351 err = nfs4_lookup_root_sec(server, fhandle,
9358 if (flavor == RPC_AUTH_MAXFLAVOR)
9369 static int _nfs41_test_stateid(struct nfs_server *server,
9370 nfs4_stateid *stateid,
9371 const struct cred *cred)
9374 struct nfs41_test_stateid_args args = {
9377 struct nfs41_test_stateid_res res;
9378 struct rpc_message msg = {
9379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9384 struct rpc_clnt *rpc_client = server->client;
9386 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9389 dprintk("NFS call test_stateid %p\n", stateid);
9390 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9391 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9392 &args.seq_args, &res.seq_res);
9393 if (status != NFS_OK) {
9394 dprintk("NFS reply test_stateid: failed, %d\n", status);
9397 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9401 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9402 int err, struct nfs4_exception *exception)
9404 exception->retry = 0;
9406 case -NFS4ERR_DELAY:
9407 case -NFS4ERR_RETRY_UNCACHED_REP:
9408 nfs4_handle_exception(server, err, exception);
9410 case -NFS4ERR_BADSESSION:
9411 case -NFS4ERR_BADSLOT:
9412 case -NFS4ERR_BAD_HIGH_SLOT:
9413 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9414 case -NFS4ERR_DEADSESSION:
9415 nfs4_do_handle_exception(server, err, exception);
9420 * nfs41_test_stateid - perform a TEST_STATEID operation
9422 * @server: server / transport on which to perform the operation
9423 * @stateid: state ID to test
9426 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9427 * Otherwise a negative NFS4ERR value is returned if the operation
9428 * failed or the state ID is not currently valid.
9430 static int nfs41_test_stateid(struct nfs_server *server,
9431 nfs4_stateid *stateid,
9432 const struct cred *cred)
9434 struct nfs4_exception exception = { };
9437 err = _nfs41_test_stateid(server, stateid, cred);
9438 nfs4_handle_delay_or_session_error(server, err, &exception);
9439 } while (exception.retry);
9443 struct nfs_free_stateid_data {
9444 struct nfs_server *server;
9445 struct nfs41_free_stateid_args args;
9446 struct nfs41_free_stateid_res res;
9449 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9451 struct nfs_free_stateid_data *data = calldata;
9452 nfs4_setup_sequence(data->server->nfs_client,
9453 &data->args.seq_args,
9458 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9460 struct nfs_free_stateid_data *data = calldata;
9462 nfs41_sequence_done(task, &data->res.seq_res);
9464 switch (task->tk_status) {
9465 case -NFS4ERR_DELAY:
9466 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9467 rpc_restart_call_prepare(task);
9471 static void nfs41_free_stateid_release(void *calldata)
9476 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9477 .rpc_call_prepare = nfs41_free_stateid_prepare,
9478 .rpc_call_done = nfs41_free_stateid_done,
9479 .rpc_release = nfs41_free_stateid_release,
9483 * nfs41_free_stateid - perform a FREE_STATEID operation
9485 * @server: server / transport on which to perform the operation
9486 * @stateid: state ID to release
9488 * @privileged: set to true if this call needs to be privileged
9490 * Note: this function is always asynchronous.
9492 static int nfs41_free_stateid(struct nfs_server *server,
9493 const nfs4_stateid *stateid,
9494 const struct cred *cred,
9497 struct rpc_message msg = {
9498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9501 struct rpc_task_setup task_setup = {
9502 .rpc_client = server->client,
9503 .rpc_message = &msg,
9504 .callback_ops = &nfs41_free_stateid_ops,
9505 .flags = RPC_TASK_ASYNC,
9507 struct nfs_free_stateid_data *data;
9508 struct rpc_task *task;
9510 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9511 &task_setup.rpc_client, &msg);
9513 dprintk("NFS call free_stateid %p\n", stateid);
9514 data = kmalloc(sizeof(*data), GFP_NOFS);
9517 data->server = server;
9518 nfs4_stateid_copy(&data->args.stateid, stateid);
9520 task_setup.callback_data = data;
9522 msg.rpc_argp = &data->args;
9523 msg.rpc_resp = &data->res;
9524 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9525 task = rpc_run_task(&task_setup);
9527 return PTR_ERR(task);
9533 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9535 const struct cred *cred = lsp->ls_state->owner->so_cred;
9537 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9538 nfs4_free_lock_state(server, lsp);
9541 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9542 const nfs4_stateid *s2)
9544 if (s1->type != s2->type)
9547 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9550 if (s1->seqid == s2->seqid)
9553 return s1->seqid == 0 || s2->seqid == 0;
9556 #endif /* CONFIG_NFS_V4_1 */
9558 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9559 const nfs4_stateid *s2)
9561 return nfs4_stateid_match(s1, s2);
9565 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9566 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9567 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9568 .recover_open = nfs4_open_reclaim,
9569 .recover_lock = nfs4_lock_reclaim,
9570 .establish_clid = nfs4_init_clientid,
9571 .detect_trunking = nfs40_discover_server_trunking,
9574 #if defined(CONFIG_NFS_V4_1)
9575 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9576 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9577 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9578 .recover_open = nfs4_open_reclaim,
9579 .recover_lock = nfs4_lock_reclaim,
9580 .establish_clid = nfs41_init_clientid,
9581 .reclaim_complete = nfs41_proc_reclaim_complete,
9582 .detect_trunking = nfs41_discover_server_trunking,
9584 #endif /* CONFIG_NFS_V4_1 */
9586 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9587 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9588 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9589 .recover_open = nfs40_open_expired,
9590 .recover_lock = nfs4_lock_expired,
9591 .establish_clid = nfs4_init_clientid,
9594 #if defined(CONFIG_NFS_V4_1)
9595 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9596 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9597 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9598 .recover_open = nfs41_open_expired,
9599 .recover_lock = nfs41_lock_expired,
9600 .establish_clid = nfs41_init_clientid,
9602 #endif /* CONFIG_NFS_V4_1 */
9604 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9605 .sched_state_renewal = nfs4_proc_async_renew,
9606 .get_state_renewal_cred = nfs4_get_renew_cred,
9607 .renew_lease = nfs4_proc_renew,
9610 #if defined(CONFIG_NFS_V4_1)
9611 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9612 .sched_state_renewal = nfs41_proc_async_sequence,
9613 .get_state_renewal_cred = nfs4_get_machine_cred,
9614 .renew_lease = nfs4_proc_sequence,
9618 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9619 .get_locations = _nfs40_proc_get_locations,
9620 .fsid_present = _nfs40_proc_fsid_present,
9623 #if defined(CONFIG_NFS_V4_1)
9624 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9625 .get_locations = _nfs41_proc_get_locations,
9626 .fsid_present = _nfs41_proc_fsid_present,
9628 #endif /* CONFIG_NFS_V4_1 */
9630 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9632 .init_caps = NFS_CAP_READDIRPLUS
9633 | NFS_CAP_ATOMIC_OPEN
9634 | NFS_CAP_POSIX_LOCK,
9635 .init_client = nfs40_init_client,
9636 .shutdown_client = nfs40_shutdown_client,
9637 .match_stateid = nfs4_match_stateid,
9638 .find_root_sec = nfs4_find_root_sec,
9639 .free_lock_state = nfs4_release_lockowner,
9640 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9641 .alloc_seqid = nfs_alloc_seqid,
9642 .call_sync_ops = &nfs40_call_sync_ops,
9643 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9644 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9645 .state_renewal_ops = &nfs40_state_renewal_ops,
9646 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9649 #if defined(CONFIG_NFS_V4_1)
9650 static struct nfs_seqid *
9651 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9656 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9658 .init_caps = NFS_CAP_READDIRPLUS
9659 | NFS_CAP_ATOMIC_OPEN
9660 | NFS_CAP_POSIX_LOCK
9661 | NFS_CAP_STATEID_NFSV41
9662 | NFS_CAP_ATOMIC_OPEN_V1
9664 .init_client = nfs41_init_client,
9665 .shutdown_client = nfs41_shutdown_client,
9666 .match_stateid = nfs41_match_stateid,
9667 .find_root_sec = nfs41_find_root_sec,
9668 .free_lock_state = nfs41_free_lock_state,
9669 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9670 .alloc_seqid = nfs_alloc_no_seqid,
9671 .session_trunk = nfs4_test_session_trunk,
9672 .call_sync_ops = &nfs41_call_sync_ops,
9673 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9674 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9675 .state_renewal_ops = &nfs41_state_renewal_ops,
9676 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9680 #if defined(CONFIG_NFS_V4_2)
9681 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9683 .init_caps = NFS_CAP_READDIRPLUS
9684 | NFS_CAP_ATOMIC_OPEN
9685 | NFS_CAP_POSIX_LOCK
9686 | NFS_CAP_STATEID_NFSV41
9687 | NFS_CAP_ATOMIC_OPEN_V1
9691 | NFS_CAP_OFFLOAD_CANCEL
9692 | NFS_CAP_DEALLOCATE
9694 | NFS_CAP_LAYOUTSTATS
9696 | NFS_CAP_LAYOUTERROR,
9697 .init_client = nfs41_init_client,
9698 .shutdown_client = nfs41_shutdown_client,
9699 .match_stateid = nfs41_match_stateid,
9700 .find_root_sec = nfs41_find_root_sec,
9701 .free_lock_state = nfs41_free_lock_state,
9702 .call_sync_ops = &nfs41_call_sync_ops,
9703 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9704 .alloc_seqid = nfs_alloc_no_seqid,
9705 .session_trunk = nfs4_test_session_trunk,
9706 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9707 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9708 .state_renewal_ops = &nfs41_state_renewal_ops,
9709 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9713 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9714 [0] = &nfs_v4_0_minor_ops,
9715 #if defined(CONFIG_NFS_V4_1)
9716 [1] = &nfs_v4_1_minor_ops,
9718 #if defined(CONFIG_NFS_V4_2)
9719 [2] = &nfs_v4_2_minor_ops,
9723 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9725 ssize_t error, error2;
9727 error = generic_listxattr(dentry, list, size);
9735 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9738 return error + error2;
9741 static const struct inode_operations nfs4_dir_inode_operations = {
9742 .create = nfs_create,
9743 .lookup = nfs_lookup,
9744 .atomic_open = nfs_atomic_open,
9746 .unlink = nfs_unlink,
9747 .symlink = nfs_symlink,
9751 .rename = nfs_rename,
9752 .permission = nfs_permission,
9753 .getattr = nfs_getattr,
9754 .setattr = nfs_setattr,
9755 .listxattr = nfs4_listxattr,
9758 static const struct inode_operations nfs4_file_inode_operations = {
9759 .permission = nfs_permission,
9760 .getattr = nfs_getattr,
9761 .setattr = nfs_setattr,
9762 .listxattr = nfs4_listxattr,
9765 const struct nfs_rpc_ops nfs_v4_clientops = {
9766 .version = 4, /* protocol version */
9767 .dentry_ops = &nfs4_dentry_operations,
9768 .dir_inode_ops = &nfs4_dir_inode_operations,
9769 .file_inode_ops = &nfs4_file_inode_operations,
9770 .file_ops = &nfs4_file_operations,
9771 .getroot = nfs4_proc_get_root,
9772 .submount = nfs4_submount,
9773 .try_mount = nfs4_try_mount,
9774 .getattr = nfs4_proc_getattr,
9775 .setattr = nfs4_proc_setattr,
9776 .lookup = nfs4_proc_lookup,
9777 .lookupp = nfs4_proc_lookupp,
9778 .access = nfs4_proc_access,
9779 .readlink = nfs4_proc_readlink,
9780 .create = nfs4_proc_create,
9781 .remove = nfs4_proc_remove,
9782 .unlink_setup = nfs4_proc_unlink_setup,
9783 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9784 .unlink_done = nfs4_proc_unlink_done,
9785 .rename_setup = nfs4_proc_rename_setup,
9786 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9787 .rename_done = nfs4_proc_rename_done,
9788 .link = nfs4_proc_link,
9789 .symlink = nfs4_proc_symlink,
9790 .mkdir = nfs4_proc_mkdir,
9791 .rmdir = nfs4_proc_rmdir,
9792 .readdir = nfs4_proc_readdir,
9793 .mknod = nfs4_proc_mknod,
9794 .statfs = nfs4_proc_statfs,
9795 .fsinfo = nfs4_proc_fsinfo,
9796 .pathconf = nfs4_proc_pathconf,
9797 .set_capabilities = nfs4_server_capabilities,
9798 .decode_dirent = nfs4_decode_dirent,
9799 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9800 .read_setup = nfs4_proc_read_setup,
9801 .read_done = nfs4_read_done,
9802 .write_setup = nfs4_proc_write_setup,
9803 .write_done = nfs4_write_done,
9804 .commit_setup = nfs4_proc_commit_setup,
9805 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9806 .commit_done = nfs4_commit_done,
9807 .lock = nfs4_proc_lock,
9808 .clear_acl_cache = nfs4_zap_acl_attr,
9809 .close_context = nfs4_close_context,
9810 .open_context = nfs4_atomic_open,
9811 .have_delegation = nfs4_have_delegation,
9812 .alloc_client = nfs4_alloc_client,
9813 .init_client = nfs4_init_client,
9814 .free_client = nfs4_free_client,
9815 .create_server = nfs4_create_server,
9816 .clone_server = nfs_clone_server,
9819 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9820 .name = XATTR_NAME_NFSV4_ACL,
9821 .list = nfs4_xattr_list_nfs4_acl,
9822 .get = nfs4_xattr_get_nfs4_acl,
9823 .set = nfs4_xattr_set_nfs4_acl,
9826 const struct xattr_handler *nfs4_xattr_handlers[] = {
9827 &nfs4_xattr_nfs4_acl_handler,
9828 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9829 &nfs4_xattr_nfs4_label_handler,