4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 static int _nfs4_proc_open(struct nfs4_opendata *data);
78 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
79 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
80 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
81 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
82 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
84 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
88 #ifdef CONFIG_NFS_V4_1
89 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
95 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
96 static inline struct nfs4_label *
97 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
108 err = security_dentry_init_security(dentry, sattr->ia_mode,
109 &dentry->d_name, (void **)&label->label, &label->len);
116 nfs4_label_release_security(struct nfs4_label *label)
119 security_release_secctx(label->label, label->len);
121 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
124 return server->attr_bitmask;
126 return server->attr_bitmask_nl;
129 static inline struct nfs4_label *
130 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
131 struct iattr *sattr, struct nfs4_label *l)
134 nfs4_label_release_security(struct nfs4_label *label)
137 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138 { return server->attr_bitmask; }
141 /* Prevent leaks of NFSv4 errors into userland */
142 static int nfs4_map_errors(int err)
147 case -NFS4ERR_RESOURCE:
148 case -NFS4ERR_LAYOUTTRYLATER:
149 case -NFS4ERR_RECALLCONFLICT:
151 case -NFS4ERR_WRONGSEC:
152 case -NFS4ERR_WRONG_CRED:
154 case -NFS4ERR_BADOWNER:
155 case -NFS4ERR_BADNAME:
157 case -NFS4ERR_SHARE_DENIED:
159 case -NFS4ERR_MINOR_VERS_MISMATCH:
160 return -EPROTONOSUPPORT;
161 case -NFS4ERR_ACCESS:
163 case -NFS4ERR_FILE_OPEN:
166 dprintk("%s could not handle NFSv4 error %d\n",
174 * This is our standard bitmap for GETATTR requests.
176 const u32 nfs4_fattr_bitmap[3] = {
178 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_FILEID,
183 | FATTR4_WORD1_NUMLINKS
185 | FATTR4_WORD1_OWNER_GROUP
186 | FATTR4_WORD1_RAWDEV
187 | FATTR4_WORD1_SPACE_USED
188 | FATTR4_WORD1_TIME_ACCESS
189 | FATTR4_WORD1_TIME_METADATA
190 | FATTR4_WORD1_TIME_MODIFY,
191 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
196 static const u32 nfs4_pnfs_open_bitmap[3] = {
198 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_FILEID,
203 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
214 static const u32 nfs4_open_noattr_bitmap[3] = {
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
220 const u32 nfs4_statfs_bitmap[3] = {
221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
229 const u32 nfs4_pathconf_bitmap[3] = {
231 | FATTR4_WORD0_MAXNAME,
235 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
239 FATTR4_WORD1_TIME_DELTA
240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
244 const u32 nfs4_fs_locations_bitmap[3] = {
246 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
252 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
263 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
264 struct nfs4_readdir_arg *readdir)
269 readdir->cookie = cookie;
270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
286 start = p = kmap_atomic(*readdir->pages);
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
298 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
310 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
314 kunmap_atomic(start);
317 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
324 *timeout = NFS4_POLL_RETRY_MIN;
325 if (*timeout > NFS4_POLL_RETRY_MAX)
326 *timeout = NFS4_POLL_RETRY_MAX;
327 freezable_schedule_timeout_killable_unsafe(*timeout);
328 if (fatal_signal_pending(current))
334 /* This is the error handling routine for processes that are allowed
337 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
339 struct nfs_client *clp = server->nfs_client;
340 struct nfs4_state *state = exception->state;
341 struct inode *inode = exception->inode;
344 exception->retry = 0;
348 case -NFS4ERR_OPENMODE:
349 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
350 nfs4_inode_return_delegation(inode);
351 exception->retry = 1;
356 ret = nfs4_schedule_stateid_recovery(server, state);
359 goto wait_on_recovery;
360 case -NFS4ERR_DELEG_REVOKED:
361 case -NFS4ERR_ADMIN_REVOKED:
362 case -NFS4ERR_BAD_STATEID:
363 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
364 nfs_remove_bad_delegation(inode);
365 exception->retry = 1;
370 ret = nfs4_schedule_stateid_recovery(server, state);
373 goto wait_on_recovery;
374 case -NFS4ERR_EXPIRED:
376 ret = nfs4_schedule_stateid_recovery(server, state);
380 case -NFS4ERR_STALE_STATEID:
381 case -NFS4ERR_STALE_CLIENTID:
382 nfs4_schedule_lease_recovery(clp);
383 goto wait_on_recovery;
385 ret = nfs4_schedule_migration_recovery(server);
388 goto wait_on_recovery;
389 case -NFS4ERR_LEASE_MOVED:
390 nfs4_schedule_lease_moved_recovery(clp);
391 goto wait_on_recovery;
392 #if defined(CONFIG_NFS_V4_1)
393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
403 goto wait_on_recovery;
404 #endif /* defined(CONFIG_NFS_V4_1) */
405 case -NFS4ERR_FILE_OPEN:
406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
415 ret = nfs4_delay(server->client, &exception->timeout);
418 case -NFS4ERR_RETRY_UNCACHED_REP:
419 case -NFS4ERR_OLD_STATEID:
420 exception->retry = 1;
422 case -NFS4ERR_BADOWNER:
423 /* The following works around a Linux server bug! */
424 case -NFS4ERR_BADNAME:
425 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
426 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
427 exception->retry = 1;
428 printk(KERN_WARNING "NFS: v4 server %s "
429 "does not accept raw "
431 "Reenabling the idmapper.\n",
432 server->nfs_client->cl_hostname);
435 /* We failed to handle the error */
436 return nfs4_map_errors(ret);
438 ret = nfs4_wait_clnt_recover(clp);
439 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
442 exception->retry = 1;
447 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
448 * or 'false' otherwise.
450 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
452 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
454 if (flavor == RPC_AUTH_GSS_KRB5I ||
455 flavor == RPC_AUTH_GSS_KRB5P)
461 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
463 spin_lock(&clp->cl_lock);
464 if (time_before(clp->cl_last_renewal,timestamp))
465 clp->cl_last_renewal = timestamp;
466 spin_unlock(&clp->cl_lock);
469 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
471 do_renew_lease(server->nfs_client, timestamp);
474 struct nfs4_call_sync_data {
475 const struct nfs_server *seq_server;
476 struct nfs4_sequence_args *seq_args;
477 struct nfs4_sequence_res *seq_res;
480 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
481 struct nfs4_sequence_res *res, int cache_reply)
483 args->sa_slot = NULL;
484 args->sa_cache_this = cache_reply;
485 args->sa_privileged = 0;
490 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
492 args->sa_privileged = 1;
495 static int nfs40_setup_sequence(const struct nfs_server *server,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 struct rpc_task *task)
500 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
501 struct nfs4_slot *slot;
503 /* slot already allocated? */
504 if (res->sr_slot != NULL)
507 spin_lock(&tbl->slot_tbl_lock);
508 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
511 slot = nfs4_alloc_slot(tbl);
513 if (slot == ERR_PTR(-ENOMEM))
514 task->tk_timeout = HZ >> 2;
517 spin_unlock(&tbl->slot_tbl_lock);
519 args->sa_slot = slot;
523 rpc_call_start(task);
527 if (args->sa_privileged)
528 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
529 NULL, RPC_PRIORITY_PRIVILEGED);
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
536 static int nfs40_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
539 struct nfs4_slot *slot = res->sr_slot;
540 struct nfs4_slot_table *tbl;
546 spin_lock(&tbl->slot_tbl_lock);
547 if (!nfs41_wake_and_assign_slot(tbl, slot))
548 nfs4_free_slot(tbl, slot);
549 spin_unlock(&tbl->slot_tbl_lock);
556 #if defined(CONFIG_NFS_V4_1)
558 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
560 struct nfs4_session *session;
561 struct nfs4_slot_table *tbl;
562 struct nfs4_slot *slot = res->sr_slot;
563 bool send_new_highest_used_slotid = false;
566 session = tbl->session;
568 spin_lock(&tbl->slot_tbl_lock);
569 /* Be nice to the server: try to ensure that the last transmitted
570 * value for highest_user_slotid <= target_highest_slotid
572 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
573 send_new_highest_used_slotid = true;
575 if (nfs41_wake_and_assign_slot(tbl, slot)) {
576 send_new_highest_used_slotid = false;
579 nfs4_free_slot(tbl, slot);
581 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
582 send_new_highest_used_slotid = false;
584 spin_unlock(&tbl->slot_tbl_lock);
586 if (send_new_highest_used_slotid)
587 nfs41_server_notify_highest_slotid_update(session->clp);
590 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
592 struct nfs4_session *session;
593 struct nfs4_slot *slot = res->sr_slot;
594 struct nfs_client *clp;
595 bool interrupted = false;
600 /* don't increment the sequence number if the task wasn't sent */
601 if (!RPC_WAS_SENT(task))
604 session = slot->table->session;
606 if (slot->interrupted) {
607 slot->interrupted = 0;
611 trace_nfs4_sequence_done(session, res);
612 /* Check the SEQUENCE operation status */
613 switch (res->sr_status) {
615 /* Update the slot's sequence and clientid lease timer */
618 do_renew_lease(clp, res->sr_timestamp);
619 /* Check sequence flags */
620 if (res->sr_status_flags != 0)
621 nfs4_schedule_lease_recovery(clp);
622 nfs41_update_target_slotid(slot->table, slot, res);
626 * sr_status remains 1 if an RPC level error occurred.
627 * The server may or may not have processed the sequence
629 * Mark the slot as having hosted an interrupted RPC call.
631 slot->interrupted = 1;
634 /* The server detected a resend of the RPC call and
635 * returned NFS4ERR_DELAY as per Section 2.10.6.2
638 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
643 case -NFS4ERR_BADSLOT:
645 * The slot id we used was probably retired. Try again
646 * using a different slot id.
649 case -NFS4ERR_SEQ_MISORDERED:
651 * Was the last operation on this sequence interrupted?
652 * If so, retry after bumping the sequence number.
659 * Could this slot have been previously retired?
660 * If so, then the server may be expecting seq_nr = 1!
662 if (slot->seq_nr != 1) {
667 case -NFS4ERR_SEQ_FALSE_RETRY:
671 /* Just update the slot sequence no. */
675 /* The session may be reset by one of the error handlers. */
676 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
677 nfs41_sequence_free_slot(res);
681 if (rpc_restart_call_prepare(task)) {
687 if (!rpc_restart_call(task))
689 rpc_delay(task, NFS4_POLL_RETRY_MAX);
692 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
694 static int nfs4_sequence_done(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
697 if (res->sr_slot == NULL)
699 if (!res->sr_slot->table->session)
700 return nfs40_sequence_done(task, res);
701 return nfs41_sequence_done(task, res);
704 int nfs41_setup_sequence(struct nfs4_session *session,
705 struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res,
707 struct rpc_task *task)
709 struct nfs4_slot *slot;
710 struct nfs4_slot_table *tbl;
712 dprintk("--> %s\n", __func__);
713 /* slot already allocated? */
714 if (res->sr_slot != NULL)
717 tbl = &session->fc_slot_table;
719 task->tk_timeout = 0;
721 spin_lock(&tbl->slot_tbl_lock);
722 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
723 !args->sa_privileged) {
724 /* The state manager will wait until the slot table is empty */
725 dprintk("%s session is draining\n", __func__);
729 slot = nfs4_alloc_slot(tbl);
731 /* If out of memory, try again in 1/4 second */
732 if (slot == ERR_PTR(-ENOMEM))
733 task->tk_timeout = HZ >> 2;
734 dprintk("<-- %s: no free slots\n", __func__);
737 spin_unlock(&tbl->slot_tbl_lock);
739 args->sa_slot = slot;
741 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
742 slot->slot_nr, slot->seq_nr);
745 res->sr_timestamp = jiffies;
746 res->sr_status_flags = 0;
748 * sr_status is only set in decode_sequence, and so will remain
749 * set to 1 if an rpc level failure occurs.
752 trace_nfs4_setup_sequence(session, args);
754 rpc_call_start(task);
757 /* Privileged tasks are queued with top priority */
758 if (args->sa_privileged)
759 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
760 NULL, RPC_PRIORITY_PRIVILEGED);
762 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
763 spin_unlock(&tbl->slot_tbl_lock);
766 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
768 static int nfs4_setup_sequence(const struct nfs_server *server,
769 struct nfs4_sequence_args *args,
770 struct nfs4_sequence_res *res,
771 struct rpc_task *task)
773 struct nfs4_session *session = nfs4_get_session(server);
777 return nfs40_setup_sequence(server, args, res, task);
779 dprintk("--> %s clp %p session %p sr_slot %u\n",
780 __func__, session->clp, session, res->sr_slot ?
781 res->sr_slot->slot_nr : NFS4_NO_SLOT);
783 ret = nfs41_setup_sequence(session, args, res, task);
785 dprintk("<-- %s status=%d\n", __func__, ret);
789 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
791 struct nfs4_call_sync_data *data = calldata;
792 struct nfs4_session *session = nfs4_get_session(data->seq_server);
794 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
796 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
799 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
801 struct nfs4_call_sync_data *data = calldata;
803 nfs41_sequence_done(task, data->seq_res);
806 static const struct rpc_call_ops nfs41_call_sync_ops = {
807 .rpc_call_prepare = nfs41_call_sync_prepare,
808 .rpc_call_done = nfs41_call_sync_done,
811 #else /* !CONFIG_NFS_V4_1 */
813 static int nfs4_setup_sequence(const struct nfs_server *server,
814 struct nfs4_sequence_args *args,
815 struct nfs4_sequence_res *res,
816 struct rpc_task *task)
818 return nfs40_setup_sequence(server, args, res, task);
821 static int nfs4_sequence_done(struct rpc_task *task,
822 struct nfs4_sequence_res *res)
824 return nfs40_sequence_done(task, res);
827 #endif /* !CONFIG_NFS_V4_1 */
829 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
831 struct nfs4_call_sync_data *data = calldata;
832 nfs4_setup_sequence(data->seq_server,
833 data->seq_args, data->seq_res, task);
836 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
838 struct nfs4_call_sync_data *data = calldata;
839 nfs4_sequence_done(task, data->seq_res);
842 static const struct rpc_call_ops nfs40_call_sync_ops = {
843 .rpc_call_prepare = nfs40_call_sync_prepare,
844 .rpc_call_done = nfs40_call_sync_done,
847 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
848 struct nfs_server *server,
849 struct rpc_message *msg,
850 struct nfs4_sequence_args *args,
851 struct nfs4_sequence_res *res)
854 struct rpc_task *task;
855 struct nfs_client *clp = server->nfs_client;
856 struct nfs4_call_sync_data data = {
857 .seq_server = server,
861 struct rpc_task_setup task_setup = {
864 .callback_ops = clp->cl_mvops->call_sync_ops,
865 .callback_data = &data
868 task = rpc_run_task(&task_setup);
872 ret = task->tk_status;
879 int nfs4_call_sync(struct rpc_clnt *clnt,
880 struct nfs_server *server,
881 struct rpc_message *msg,
882 struct nfs4_sequence_args *args,
883 struct nfs4_sequence_res *res,
886 nfs4_init_sequence(args, res, cache_reply);
887 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
890 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
892 struct nfs_inode *nfsi = NFS_I(dir);
894 spin_lock(&dir->i_lock);
895 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
896 if (!cinfo->atomic || cinfo->before != dir->i_version)
897 nfs_force_lookup_revalidate(dir);
898 dir->i_version = cinfo->after;
899 nfs_fscache_invalidate(dir);
900 spin_unlock(&dir->i_lock);
903 struct nfs4_opendata {
905 struct nfs_openargs o_arg;
906 struct nfs_openres o_res;
907 struct nfs_open_confirmargs c_arg;
908 struct nfs_open_confirmres c_res;
909 struct nfs4_string owner_name;
910 struct nfs4_string group_name;
911 struct nfs_fattr f_attr;
912 struct nfs4_label *f_label;
914 struct dentry *dentry;
915 struct nfs4_state_owner *owner;
916 struct nfs4_state *state;
918 unsigned long timestamp;
919 unsigned int rpc_done : 1;
920 unsigned int file_created : 1;
921 unsigned int is_recover : 1;
926 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
927 int err, struct nfs4_exception *exception)
931 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
933 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
934 exception->retry = 1;
938 static enum open_claim_type4
939 nfs4_map_atomic_open_claim(struct nfs_server *server,
940 enum open_claim_type4 claim)
942 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
947 case NFS4_OPEN_CLAIM_FH:
948 return NFS4_OPEN_CLAIM_NULL;
949 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
950 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
951 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
952 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
956 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
958 p->o_res.f_attr = &p->f_attr;
959 p->o_res.f_label = p->f_label;
960 p->o_res.seqid = p->o_arg.seqid;
961 p->c_res.seqid = p->c_arg.seqid;
962 p->o_res.server = p->o_arg.server;
963 p->o_res.access_request = p->o_arg.access;
964 nfs_fattr_init(&p->f_attr);
965 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
968 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
969 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
970 const struct iattr *attrs,
971 struct nfs4_label *label,
972 enum open_claim_type4 claim,
975 struct dentry *parent = dget_parent(dentry);
976 struct inode *dir = parent->d_inode;
977 struct nfs_server *server = NFS_SERVER(dir);
978 struct nfs4_opendata *p;
980 p = kzalloc(sizeof(*p), gfp_mask);
984 p->f_label = nfs4_label_alloc(server, gfp_mask);
985 if (IS_ERR(p->f_label))
988 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
989 if (p->o_arg.seqid == NULL)
991 nfs_sb_active(dentry->d_sb);
992 p->dentry = dget(dentry);
995 atomic_inc(&sp->so_count);
996 p->o_arg.open_flags = flags;
997 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
998 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
999 * will return permission denied for all bits until close */
1000 if (!(flags & O_EXCL)) {
1001 /* ask server to check for all possible rights as results
1003 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1004 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1006 p->o_arg.clientid = server->nfs_client->cl_clientid;
1007 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1008 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1009 p->o_arg.name = &dentry->d_name;
1010 p->o_arg.server = server;
1011 p->o_arg.bitmask = nfs4_bitmask(server, label);
1012 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1013 p->o_arg.label = label;
1014 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1015 switch (p->o_arg.claim) {
1016 case NFS4_OPEN_CLAIM_NULL:
1017 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1018 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1019 p->o_arg.fh = NFS_FH(dir);
1021 case NFS4_OPEN_CLAIM_PREVIOUS:
1022 case NFS4_OPEN_CLAIM_FH:
1023 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1024 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1025 p->o_arg.fh = NFS_FH(dentry->d_inode);
1027 if (attrs != NULL && attrs->ia_valid != 0) {
1030 p->o_arg.u.attrs = &p->attrs;
1031 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1034 verf[1] = current->pid;
1035 memcpy(p->o_arg.u.verifier.data, verf,
1036 sizeof(p->o_arg.u.verifier.data));
1038 p->c_arg.fh = &p->o_res.fh;
1039 p->c_arg.stateid = &p->o_res.stateid;
1040 p->c_arg.seqid = p->o_arg.seqid;
1041 nfs4_init_opendata_res(p);
1042 kref_init(&p->kref);
1046 nfs4_label_free(p->f_label);
1054 static void nfs4_opendata_free(struct kref *kref)
1056 struct nfs4_opendata *p = container_of(kref,
1057 struct nfs4_opendata, kref);
1058 struct super_block *sb = p->dentry->d_sb;
1060 nfs_free_seqid(p->o_arg.seqid);
1061 if (p->state != NULL)
1062 nfs4_put_open_state(p->state);
1063 nfs4_put_state_owner(p->owner);
1065 nfs4_label_free(p->f_label);
1069 nfs_sb_deactive(sb);
1070 nfs_fattr_free_names(&p->f_attr);
1074 static void nfs4_opendata_put(struct nfs4_opendata *p)
1077 kref_put(&p->kref, nfs4_opendata_free);
1080 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1084 ret = rpc_wait_for_completion_task(task);
1088 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1092 if (open_mode & (O_EXCL|O_TRUNC))
1094 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1096 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1097 && state->n_rdonly != 0;
1100 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1101 && state->n_wronly != 0;
1103 case FMODE_READ|FMODE_WRITE:
1104 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1105 && state->n_rdwr != 0;
1111 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1113 if (delegation == NULL)
1115 if ((delegation->type & fmode) != fmode)
1117 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1119 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1121 nfs_mark_delegation_referenced(delegation);
1125 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1134 case FMODE_READ|FMODE_WRITE:
1137 nfs4_state_set_mode_locked(state, state->state | fmode);
1140 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1142 struct nfs_client *clp = state->owner->so_server->nfs_client;
1143 bool need_recover = false;
1145 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1146 need_recover = true;
1147 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1148 need_recover = true;
1149 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1150 need_recover = true;
1152 nfs4_state_mark_reclaim_nograce(clp, state);
1155 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1156 nfs4_stateid *stateid)
1158 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1160 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1161 nfs_test_and_clear_all_open_stateid(state);
1164 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1169 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1170 nfs4_stateid *stateid, fmode_t fmode)
1172 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1173 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1175 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1178 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1181 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1182 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1183 clear_bit(NFS_OPEN_STATE, &state->flags);
1185 if (stateid == NULL)
1187 if (!nfs_need_update_open_stateid(state, stateid))
1189 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1190 nfs4_stateid_copy(&state->stateid, stateid);
1191 nfs4_stateid_copy(&state->open_stateid, stateid);
1194 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1196 write_seqlock(&state->seqlock);
1197 nfs_clear_open_stateid_locked(state, stateid, fmode);
1198 write_sequnlock(&state->seqlock);
1199 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1200 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1203 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1207 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1210 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1212 case FMODE_READ|FMODE_WRITE:
1213 set_bit(NFS_O_RDWR_STATE, &state->flags);
1215 if (!nfs_need_update_open_stateid(state, stateid))
1217 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1218 nfs4_stateid_copy(&state->stateid, stateid);
1219 nfs4_stateid_copy(&state->open_stateid, stateid);
1222 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1225 * Protect the call to nfs4_state_set_mode_locked and
1226 * serialise the stateid update
1228 write_seqlock(&state->seqlock);
1229 if (deleg_stateid != NULL) {
1230 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1231 set_bit(NFS_DELEGATED_STATE, &state->flags);
1233 if (open_stateid != NULL)
1234 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1235 write_sequnlock(&state->seqlock);
1236 spin_lock(&state->owner->so_lock);
1237 update_open_stateflags(state, fmode);
1238 spin_unlock(&state->owner->so_lock);
1241 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1243 struct nfs_inode *nfsi = NFS_I(state->inode);
1244 struct nfs_delegation *deleg_cur;
1247 fmode &= (FMODE_READ|FMODE_WRITE);
1250 deleg_cur = rcu_dereference(nfsi->delegation);
1251 if (deleg_cur == NULL)
1254 spin_lock(&deleg_cur->lock);
1255 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1256 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1257 (deleg_cur->type & fmode) != fmode)
1258 goto no_delegation_unlock;
1260 if (delegation == NULL)
1261 delegation = &deleg_cur->stateid;
1262 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1263 goto no_delegation_unlock;
1265 nfs_mark_delegation_referenced(deleg_cur);
1266 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1268 no_delegation_unlock:
1269 spin_unlock(&deleg_cur->lock);
1273 if (!ret && open_stateid != NULL) {
1274 __update_open_stateid(state, open_stateid, NULL, fmode);
1277 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1278 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1284 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1286 struct nfs_delegation *delegation;
1289 delegation = rcu_dereference(NFS_I(inode)->delegation);
1290 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1295 nfs4_inode_return_delegation(inode);
1298 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1300 struct nfs4_state *state = opendata->state;
1301 struct nfs_inode *nfsi = NFS_I(state->inode);
1302 struct nfs_delegation *delegation;
1303 int open_mode = opendata->o_arg.open_flags;
1304 fmode_t fmode = opendata->o_arg.fmode;
1305 nfs4_stateid stateid;
1309 if (can_open_cached(state, fmode, open_mode)) {
1310 spin_lock(&state->owner->so_lock);
1311 if (can_open_cached(state, fmode, open_mode)) {
1312 update_open_stateflags(state, fmode);
1313 spin_unlock(&state->owner->so_lock);
1314 goto out_return_state;
1316 spin_unlock(&state->owner->so_lock);
1319 delegation = rcu_dereference(nfsi->delegation);
1320 if (!can_open_delegated(delegation, fmode)) {
1324 /* Save the delegation */
1325 nfs4_stateid_copy(&stateid, &delegation->stateid);
1327 nfs_release_seqid(opendata->o_arg.seqid);
1328 if (!opendata->is_recover) {
1329 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1335 /* Try to update the stateid using the delegation */
1336 if (update_open_stateid(state, NULL, &stateid, fmode))
1337 goto out_return_state;
1340 return ERR_PTR(ret);
1342 atomic_inc(&state->count);
1347 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1349 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1350 struct nfs_delegation *delegation;
1351 int delegation_flags = 0;
1354 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1356 delegation_flags = delegation->flags;
1358 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1359 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1360 "returning a delegation for "
1361 "OPEN(CLAIM_DELEGATE_CUR)\n",
1363 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1364 nfs_inode_set_delegation(state->inode,
1365 data->owner->so_cred,
1368 nfs_inode_reclaim_delegation(state->inode,
1369 data->owner->so_cred,
1374 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1375 * and update the nfs4_state.
1377 static struct nfs4_state *
1378 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1380 struct inode *inode = data->state->inode;
1381 struct nfs4_state *state = data->state;
1384 if (!data->rpc_done) {
1385 if (data->rpc_status) {
1386 ret = data->rpc_status;
1389 /* cached opens have already been processed */
1393 ret = nfs_refresh_inode(inode, &data->f_attr);
1397 if (data->o_res.delegation_type != 0)
1398 nfs4_opendata_check_deleg(data, state);
1400 update_open_stateid(state, &data->o_res.stateid, NULL,
1402 atomic_inc(&state->count);
1406 return ERR_PTR(ret);
1410 static struct nfs4_state *
1411 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1413 struct inode *inode;
1414 struct nfs4_state *state = NULL;
1417 if (!data->rpc_done) {
1418 state = nfs4_try_open_cached(data);
1423 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1425 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1426 ret = PTR_ERR(inode);
1430 state = nfs4_get_open_state(inode, data->owner);
1433 if (data->o_res.delegation_type != 0)
1434 nfs4_opendata_check_deleg(data, state);
1435 update_open_stateid(state, &data->o_res.stateid, NULL,
1439 nfs_release_seqid(data->o_arg.seqid);
1444 return ERR_PTR(ret);
1447 static struct nfs4_state *
1448 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1450 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1451 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1452 return _nfs4_opendata_to_nfs4_state(data);
1455 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1457 struct nfs_inode *nfsi = NFS_I(state->inode);
1458 struct nfs_open_context *ctx;
1460 spin_lock(&state->inode->i_lock);
1461 list_for_each_entry(ctx, &nfsi->open_files, list) {
1462 if (ctx->state != state)
1464 get_nfs_open_context(ctx);
1465 spin_unlock(&state->inode->i_lock);
1468 spin_unlock(&state->inode->i_lock);
1469 return ERR_PTR(-ENOENT);
1472 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1473 struct nfs4_state *state, enum open_claim_type4 claim)
1475 struct nfs4_opendata *opendata;
1477 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1478 NULL, NULL, claim, GFP_NOFS);
1479 if (opendata == NULL)
1480 return ERR_PTR(-ENOMEM);
1481 opendata->state = state;
1482 atomic_inc(&state->count);
1486 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1488 struct nfs4_state *newstate;
1491 opendata->o_arg.open_flags = 0;
1492 opendata->o_arg.fmode = fmode;
1493 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1494 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1495 nfs4_init_opendata_res(opendata);
1496 ret = _nfs4_recover_proc_open(opendata);
1499 newstate = nfs4_opendata_to_nfs4_state(opendata);
1500 if (IS_ERR(newstate))
1501 return PTR_ERR(newstate);
1502 nfs4_close_state(newstate, fmode);
1507 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1509 struct nfs4_state *newstate;
1512 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1513 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1514 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1515 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1516 /* memory barrier prior to reading state->n_* */
1517 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1518 clear_bit(NFS_OPEN_STATE, &state->flags);
1520 if (state->n_rdwr != 0) {
1521 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1524 if (newstate != state)
1527 if (state->n_wronly != 0) {
1528 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1531 if (newstate != state)
1534 if (state->n_rdonly != 0) {
1535 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1538 if (newstate != state)
1542 * We may have performed cached opens for all three recoveries.
1543 * Check if we need to update the current stateid.
1545 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1546 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1547 write_seqlock(&state->seqlock);
1548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1549 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1550 write_sequnlock(&state->seqlock);
1557 * reclaim state on the server after a reboot.
1559 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1561 struct nfs_delegation *delegation;
1562 struct nfs4_opendata *opendata;
1563 fmode_t delegation_type = 0;
1566 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1567 NFS4_OPEN_CLAIM_PREVIOUS);
1568 if (IS_ERR(opendata))
1569 return PTR_ERR(opendata);
1571 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1572 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1573 delegation_type = delegation->type;
1575 opendata->o_arg.u.delegation_type = delegation_type;
1576 status = nfs4_open_recover(opendata, state);
1577 nfs4_opendata_put(opendata);
1581 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1583 struct nfs_server *server = NFS_SERVER(state->inode);
1584 struct nfs4_exception exception = { };
1587 err = _nfs4_do_open_reclaim(ctx, state);
1588 trace_nfs4_open_reclaim(ctx, 0, err);
1589 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1591 if (err != -NFS4ERR_DELAY)
1593 nfs4_handle_exception(server, err, &exception);
1594 } while (exception.retry);
1598 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1600 struct nfs_open_context *ctx;
1603 ctx = nfs4_state_find_open_context(state);
1606 ret = nfs4_do_open_reclaim(ctx, state);
1607 put_nfs_open_context(ctx);
1611 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1615 printk(KERN_ERR "NFS: %s: unhandled error "
1616 "%d.\n", __func__, err);
1621 case -NFS4ERR_BADSESSION:
1622 case -NFS4ERR_BADSLOT:
1623 case -NFS4ERR_BAD_HIGH_SLOT:
1624 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1625 case -NFS4ERR_DEADSESSION:
1626 set_bit(NFS_DELEGATED_STATE, &state->flags);
1627 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1629 case -NFS4ERR_STALE_CLIENTID:
1630 case -NFS4ERR_STALE_STATEID:
1631 set_bit(NFS_DELEGATED_STATE, &state->flags);
1632 case -NFS4ERR_EXPIRED:
1633 /* Don't recall a delegation if it was lost */
1634 nfs4_schedule_lease_recovery(server->nfs_client);
1636 case -NFS4ERR_MOVED:
1637 nfs4_schedule_migration_recovery(server);
1639 case -NFS4ERR_LEASE_MOVED:
1640 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1642 case -NFS4ERR_DELEG_REVOKED:
1643 case -NFS4ERR_ADMIN_REVOKED:
1644 case -NFS4ERR_BAD_STATEID:
1645 case -NFS4ERR_OPENMODE:
1646 nfs_inode_find_state_and_recover(state->inode,
1648 nfs4_schedule_stateid_recovery(server, state);
1650 case -NFS4ERR_DELAY:
1651 case -NFS4ERR_GRACE:
1652 set_bit(NFS_DELEGATED_STATE, &state->flags);
1656 case -NFS4ERR_DENIED:
1657 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1663 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1665 struct nfs_server *server = NFS_SERVER(state->inode);
1666 struct nfs4_opendata *opendata;
1669 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1670 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1671 if (IS_ERR(opendata))
1672 return PTR_ERR(opendata);
1673 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1674 err = nfs4_open_recover(opendata, state);
1675 nfs4_opendata_put(opendata);
1676 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1679 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1681 struct nfs4_opendata *data = calldata;
1683 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1684 &data->c_res.seq_res, task);
1687 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1689 struct nfs4_opendata *data = calldata;
1691 nfs40_sequence_done(task, &data->c_res.seq_res);
1693 data->rpc_status = task->tk_status;
1694 if (data->rpc_status == 0) {
1695 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1696 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1697 renew_lease(data->o_res.server, data->timestamp);
1702 static void nfs4_open_confirm_release(void *calldata)
1704 struct nfs4_opendata *data = calldata;
1705 struct nfs4_state *state = NULL;
1707 /* If this request hasn't been cancelled, do nothing */
1708 if (data->cancelled == 0)
1710 /* In case of error, no cleanup! */
1711 if (!data->rpc_done)
1713 state = nfs4_opendata_to_nfs4_state(data);
1715 nfs4_close_state(state, data->o_arg.fmode);
1717 nfs4_opendata_put(data);
1720 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1721 .rpc_call_prepare = nfs4_open_confirm_prepare,
1722 .rpc_call_done = nfs4_open_confirm_done,
1723 .rpc_release = nfs4_open_confirm_release,
1727 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1729 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1731 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1732 struct rpc_task *task;
1733 struct rpc_message msg = {
1734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1735 .rpc_argp = &data->c_arg,
1736 .rpc_resp = &data->c_res,
1737 .rpc_cred = data->owner->so_cred,
1739 struct rpc_task_setup task_setup_data = {
1740 .rpc_client = server->client,
1741 .rpc_message = &msg,
1742 .callback_ops = &nfs4_open_confirm_ops,
1743 .callback_data = data,
1744 .workqueue = nfsiod_workqueue,
1745 .flags = RPC_TASK_ASYNC,
1749 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1750 kref_get(&data->kref);
1752 data->rpc_status = 0;
1753 data->timestamp = jiffies;
1754 task = rpc_run_task(&task_setup_data);
1756 return PTR_ERR(task);
1757 status = nfs4_wait_for_completion_rpc_task(task);
1759 data->cancelled = 1;
1762 status = data->rpc_status;
1767 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1769 struct nfs4_opendata *data = calldata;
1770 struct nfs4_state_owner *sp = data->owner;
1771 struct nfs_client *clp = sp->so_server->nfs_client;
1773 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1776 * Check if we still need to send an OPEN call, or if we can use
1777 * a delegation instead.
1779 if (data->state != NULL) {
1780 struct nfs_delegation *delegation;
1782 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1785 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1786 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1787 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1788 can_open_delegated(delegation, data->o_arg.fmode))
1789 goto unlock_no_action;
1792 /* Update client id. */
1793 data->o_arg.clientid = clp->cl_clientid;
1794 switch (data->o_arg.claim) {
1795 case NFS4_OPEN_CLAIM_PREVIOUS:
1796 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1797 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1798 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1799 case NFS4_OPEN_CLAIM_FH:
1800 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1801 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1803 data->timestamp = jiffies;
1804 if (nfs4_setup_sequence(data->o_arg.server,
1805 &data->o_arg.seq_args,
1806 &data->o_res.seq_res,
1808 nfs_release_seqid(data->o_arg.seqid);
1810 /* Set the create mode (note dependency on the session type) */
1811 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1812 if (data->o_arg.open_flags & O_EXCL) {
1813 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1814 if (nfs4_has_persistent_session(clp))
1815 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1816 else if (clp->cl_mvops->minor_version > 0)
1817 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1823 task->tk_action = NULL;
1825 nfs4_sequence_done(task, &data->o_res.seq_res);
1828 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1830 struct nfs4_opendata *data = calldata;
1832 data->rpc_status = task->tk_status;
1834 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1837 if (task->tk_status == 0) {
1838 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1839 switch (data->o_res.f_attr->mode & S_IFMT) {
1843 data->rpc_status = -ELOOP;
1846 data->rpc_status = -EISDIR;
1849 data->rpc_status = -ENOTDIR;
1852 renew_lease(data->o_res.server, data->timestamp);
1853 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1854 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1859 static void nfs4_open_release(void *calldata)
1861 struct nfs4_opendata *data = calldata;
1862 struct nfs4_state *state = NULL;
1864 /* If this request hasn't been cancelled, do nothing */
1865 if (data->cancelled == 0)
1867 /* In case of error, no cleanup! */
1868 if (data->rpc_status != 0 || !data->rpc_done)
1870 /* In case we need an open_confirm, no cleanup! */
1871 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1873 state = nfs4_opendata_to_nfs4_state(data);
1875 nfs4_close_state(state, data->o_arg.fmode);
1877 nfs4_opendata_put(data);
1880 static const struct rpc_call_ops nfs4_open_ops = {
1881 .rpc_call_prepare = nfs4_open_prepare,
1882 .rpc_call_done = nfs4_open_done,
1883 .rpc_release = nfs4_open_release,
1886 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1888 struct inode *dir = data->dir->d_inode;
1889 struct nfs_server *server = NFS_SERVER(dir);
1890 struct nfs_openargs *o_arg = &data->o_arg;
1891 struct nfs_openres *o_res = &data->o_res;
1892 struct rpc_task *task;
1893 struct rpc_message msg = {
1894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1897 .rpc_cred = data->owner->so_cred,
1899 struct rpc_task_setup task_setup_data = {
1900 .rpc_client = server->client,
1901 .rpc_message = &msg,
1902 .callback_ops = &nfs4_open_ops,
1903 .callback_data = data,
1904 .workqueue = nfsiod_workqueue,
1905 .flags = RPC_TASK_ASYNC,
1909 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1910 kref_get(&data->kref);
1912 data->rpc_status = 0;
1913 data->cancelled = 0;
1914 data->is_recover = 0;
1916 nfs4_set_sequence_privileged(&o_arg->seq_args);
1917 data->is_recover = 1;
1919 task = rpc_run_task(&task_setup_data);
1921 return PTR_ERR(task);
1922 status = nfs4_wait_for_completion_rpc_task(task);
1924 data->cancelled = 1;
1927 status = data->rpc_status;
1933 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1935 struct inode *dir = data->dir->d_inode;
1936 struct nfs_openres *o_res = &data->o_res;
1939 status = nfs4_run_open_task(data, 1);
1940 if (status != 0 || !data->rpc_done)
1943 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1945 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1946 status = _nfs4_proc_open_confirm(data);
1954 static int nfs4_opendata_access(struct rpc_cred *cred,
1955 struct nfs4_opendata *opendata,
1956 struct nfs4_state *state, fmode_t fmode,
1959 struct nfs_access_entry cache;
1962 /* access call failed or for some reason the server doesn't
1963 * support any access modes -- defer access call until later */
1964 if (opendata->o_res.access_supported == 0)
1968 /* don't check MAY_WRITE - a newly created file may not have
1969 * write mode bits, but POSIX allows the creating process to write.
1970 * use openflags to check for exec, because fmode won't
1971 * always have FMODE_EXEC set when file open for exec. */
1972 if (openflags & __FMODE_EXEC) {
1973 /* ONLY check for exec rights */
1975 } else if (fmode & FMODE_READ)
1979 cache.jiffies = jiffies;
1980 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1981 nfs_access_add_cache(state->inode, &cache);
1983 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1986 /* even though OPEN succeeded, access is denied. Close the file */
1987 nfs4_close_state(state, fmode);
1992 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1994 static int _nfs4_proc_open(struct nfs4_opendata *data)
1996 struct inode *dir = data->dir->d_inode;
1997 struct nfs_server *server = NFS_SERVER(dir);
1998 struct nfs_openargs *o_arg = &data->o_arg;
1999 struct nfs_openres *o_res = &data->o_res;
2002 status = nfs4_run_open_task(data, 0);
2003 if (!data->rpc_done)
2006 if (status == -NFS4ERR_BADNAME &&
2007 !(o_arg->open_flags & O_CREAT))
2012 nfs_fattr_map_and_free_names(server, &data->f_attr);
2014 if (o_arg->open_flags & O_CREAT) {
2015 update_changeattr(dir, &o_res->cinfo);
2016 if (o_arg->open_flags & O_EXCL)
2017 data->file_created = 1;
2018 else if (o_res->cinfo.before != o_res->cinfo.after)
2019 data->file_created = 1;
2021 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2022 server->caps &= ~NFS_CAP_POSIX_LOCK;
2023 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2024 status = _nfs4_proc_open_confirm(data);
2028 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2029 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2033 static int nfs4_recover_expired_lease(struct nfs_server *server)
2035 return nfs4_client_recover_expired_lease(server->nfs_client);
2040 * reclaim state on the server after a network partition.
2041 * Assumes caller holds the appropriate lock
2043 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2045 struct nfs4_opendata *opendata;
2048 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2049 NFS4_OPEN_CLAIM_FH);
2050 if (IS_ERR(opendata))
2051 return PTR_ERR(opendata);
2052 ret = nfs4_open_recover(opendata, state);
2054 d_drop(ctx->dentry);
2055 nfs4_opendata_put(opendata);
2059 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2061 struct nfs_server *server = NFS_SERVER(state->inode);
2062 struct nfs4_exception exception = { };
2066 err = _nfs4_open_expired(ctx, state);
2067 trace_nfs4_open_expired(ctx, 0, err);
2068 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2073 case -NFS4ERR_GRACE:
2074 case -NFS4ERR_DELAY:
2075 nfs4_handle_exception(server, err, &exception);
2078 } while (exception.retry);
2083 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2085 struct nfs_open_context *ctx;
2088 ctx = nfs4_state_find_open_context(state);
2091 ret = nfs4_do_open_expired(ctx, state);
2092 put_nfs_open_context(ctx);
2096 #if defined(CONFIG_NFS_V4_1)
2097 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
2099 struct nfs_server *server = NFS_SERVER(state->inode);
2100 nfs4_stateid *stateid = &state->stateid;
2101 struct nfs_delegation *delegation;
2102 struct rpc_cred *cred = NULL;
2103 int status = -NFS4ERR_BAD_STATEID;
2105 /* If a state reset has been done, test_stateid is unneeded */
2106 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2109 /* Get the delegation credential for use by test/free_stateid */
2111 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2112 if (delegation != NULL &&
2113 nfs4_stateid_match(&delegation->stateid, stateid)) {
2114 cred = get_rpccred(delegation->cred);
2116 status = nfs41_test_stateid(server, stateid, cred);
2117 trace_nfs4_test_delegation_stateid(state, NULL, status);
2121 if (status != NFS_OK) {
2122 /* Free the stateid unless the server explicitly
2123 * informs us the stateid is unrecognized. */
2124 if (status != -NFS4ERR_BAD_STATEID)
2125 nfs41_free_stateid(server, stateid, cred);
2126 nfs_remove_bad_delegation(state->inode);
2128 write_seqlock(&state->seqlock);
2129 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2130 write_sequnlock(&state->seqlock);
2131 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2139 * nfs41_check_open_stateid - possibly free an open stateid
2141 * @state: NFSv4 state for an inode
2143 * Returns NFS_OK if recovery for this stateid is now finished.
2144 * Otherwise a negative NFS4ERR value is returned.
2146 static int nfs41_check_open_stateid(struct nfs4_state *state)
2148 struct nfs_server *server = NFS_SERVER(state->inode);
2149 nfs4_stateid *stateid = &state->open_stateid;
2150 struct rpc_cred *cred = state->owner->so_cred;
2153 /* If a state reset has been done, test_stateid is unneeded */
2154 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2155 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2156 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2157 return -NFS4ERR_BAD_STATEID;
2159 status = nfs41_test_stateid(server, stateid, cred);
2160 trace_nfs4_test_open_stateid(state, NULL, status);
2161 if (status != NFS_OK) {
2162 /* Free the stateid unless the server explicitly
2163 * informs us the stateid is unrecognized. */
2164 if (status != -NFS4ERR_BAD_STATEID)
2165 nfs41_free_stateid(server, stateid, cred);
2167 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2168 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2169 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2170 clear_bit(NFS_OPEN_STATE, &state->flags);
2175 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2179 nfs41_clear_delegation_stateid(state);
2180 status = nfs41_check_open_stateid(state);
2181 if (status != NFS_OK)
2182 status = nfs4_open_expired(sp, state);
2188 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2189 * fields corresponding to attributes that were used to store the verifier.
2190 * Make sure we clobber those fields in the later setattr call
2192 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2194 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2195 !(sattr->ia_valid & ATTR_ATIME_SET))
2196 sattr->ia_valid |= ATTR_ATIME;
2198 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2199 !(sattr->ia_valid & ATTR_MTIME_SET))
2200 sattr->ia_valid |= ATTR_MTIME;
2203 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2206 struct nfs_open_context *ctx)
2208 struct nfs4_state_owner *sp = opendata->owner;
2209 struct nfs_server *server = sp->so_server;
2210 struct dentry *dentry;
2211 struct nfs4_state *state;
2215 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2217 ret = _nfs4_proc_open(opendata);
2221 state = nfs4_opendata_to_nfs4_state(opendata);
2222 ret = PTR_ERR(state);
2225 if (server->caps & NFS_CAP_POSIX_LOCK)
2226 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2228 dentry = opendata->dentry;
2229 if (dentry->d_inode == NULL) {
2230 /* FIXME: Is this d_drop() ever needed? */
2232 dentry = d_add_unique(dentry, igrab(state->inode));
2233 if (dentry == NULL) {
2234 dentry = opendata->dentry;
2235 } else if (dentry != ctx->dentry) {
2237 ctx->dentry = dget(dentry);
2239 nfs_set_verifier(dentry,
2240 nfs_save_change_attribute(opendata->dir->d_inode));
2243 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2248 if (dentry->d_inode == state->inode) {
2249 nfs_inode_attach_open_context(ctx);
2250 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2251 nfs4_schedule_stateid_recovery(server, state);
2258 * Returns a referenced nfs4_state
2260 static int _nfs4_do_open(struct inode *dir,
2261 struct nfs_open_context *ctx,
2263 struct iattr *sattr,
2264 struct nfs4_label *label,
2267 struct nfs4_state_owner *sp;
2268 struct nfs4_state *state = NULL;
2269 struct nfs_server *server = NFS_SERVER(dir);
2270 struct nfs4_opendata *opendata;
2271 struct dentry *dentry = ctx->dentry;
2272 struct rpc_cred *cred = ctx->cred;
2273 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2274 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2275 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2276 struct nfs4_label *olabel = NULL;
2279 /* Protect against reboot recovery conflicts */
2281 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2283 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2286 status = nfs4_recover_expired_lease(server);
2288 goto err_put_state_owner;
2289 if (dentry->d_inode != NULL)
2290 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2292 if (dentry->d_inode)
2293 claim = NFS4_OPEN_CLAIM_FH;
2294 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2295 label, claim, GFP_KERNEL);
2296 if (opendata == NULL)
2297 goto err_put_state_owner;
2300 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2301 if (IS_ERR(olabel)) {
2302 status = PTR_ERR(olabel);
2303 goto err_opendata_put;
2307 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2308 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2309 if (!opendata->f_attr.mdsthreshold)
2310 goto err_free_label;
2311 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2313 if (dentry->d_inode != NULL)
2314 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2316 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2318 goto err_free_label;
2321 if ((opendata->o_arg.open_flags & O_EXCL) &&
2322 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2323 nfs4_exclusive_attrset(opendata, sattr);
2325 nfs_fattr_init(opendata->o_res.f_attr);
2326 status = nfs4_do_setattr(state->inode, cred,
2327 opendata->o_res.f_attr, sattr,
2328 state, label, olabel);
2330 nfs_setattr_update_inode(state->inode, sattr);
2331 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2332 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2335 if (opendata->file_created)
2336 *opened |= FILE_CREATED;
2338 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2339 *ctx_th = opendata->f_attr.mdsthreshold;
2341 kfree(opendata->f_attr.mdsthreshold);
2342 opendata->f_attr.mdsthreshold = NULL;
2344 nfs4_label_free(olabel);
2346 nfs4_opendata_put(opendata);
2347 nfs4_put_state_owner(sp);
2350 nfs4_label_free(olabel);
2352 kfree(opendata->f_attr.mdsthreshold);
2353 nfs4_opendata_put(opendata);
2354 err_put_state_owner:
2355 nfs4_put_state_owner(sp);
2361 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2362 struct nfs_open_context *ctx,
2364 struct iattr *sattr,
2365 struct nfs4_label *label,
2368 struct nfs_server *server = NFS_SERVER(dir);
2369 struct nfs4_exception exception = { };
2370 struct nfs4_state *res;
2374 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2376 trace_nfs4_open_file(ctx, flags, status);
2379 /* NOTE: BAD_SEQID means the server and client disagree about the
2380 * book-keeping w.r.t. state-changing operations
2381 * (OPEN/CLOSE/LOCK/LOCKU...)
2382 * It is actually a sign of a bug on the client or on the server.
2384 * If we receive a BAD_SEQID error in the particular case of
2385 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2386 * have unhashed the old state_owner for us, and that we can
2387 * therefore safely retry using a new one. We should still warn
2388 * the user though...
2390 if (status == -NFS4ERR_BAD_SEQID) {
2391 pr_warn_ratelimited("NFS: v4 server %s "
2392 " returned a bad sequence-id error!\n",
2393 NFS_SERVER(dir)->nfs_client->cl_hostname);
2394 exception.retry = 1;
2398 * BAD_STATEID on OPEN means that the server cancelled our
2399 * state before it received the OPEN_CONFIRM.
2400 * Recover by retrying the request as per the discussion
2401 * on Page 181 of RFC3530.
2403 if (status == -NFS4ERR_BAD_STATEID) {
2404 exception.retry = 1;
2407 if (status == -EAGAIN) {
2408 /* We must have found a delegation */
2409 exception.retry = 1;
2412 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2414 res = ERR_PTR(nfs4_handle_exception(server,
2415 status, &exception));
2416 } while (exception.retry);
2420 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2421 struct nfs_fattr *fattr, struct iattr *sattr,
2422 struct nfs4_state *state, struct nfs4_label *ilabel,
2423 struct nfs4_label *olabel)
2425 struct nfs_server *server = NFS_SERVER(inode);
2426 struct nfs_setattrargs arg = {
2427 .fh = NFS_FH(inode),
2430 .bitmask = server->attr_bitmask,
2433 struct nfs_setattrres res = {
2438 struct rpc_message msg = {
2439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2444 unsigned long timestamp = jiffies;
2449 arg.bitmask = nfs4_bitmask(server, ilabel);
2451 arg.bitmask = nfs4_bitmask(server, olabel);
2453 nfs_fattr_init(fattr);
2455 /* Servers should only apply open mode checks for file size changes */
2456 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2457 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2459 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2460 /* Use that stateid */
2461 } else if (truncate && state != NULL) {
2462 struct nfs_lockowner lockowner = {
2463 .l_owner = current->files,
2464 .l_pid = current->tgid,
2466 if (!nfs4_valid_open_stateid(state))
2468 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2469 &lockowner) == -EIO)
2472 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2474 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2475 if (status == 0 && state != NULL)
2476 renew_lease(server, timestamp);
2480 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2481 struct nfs_fattr *fattr, struct iattr *sattr,
2482 struct nfs4_state *state, struct nfs4_label *ilabel,
2483 struct nfs4_label *olabel)
2485 struct nfs_server *server = NFS_SERVER(inode);
2486 struct nfs4_exception exception = {
2492 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2493 trace_nfs4_setattr(inode, err);
2495 case -NFS4ERR_OPENMODE:
2496 if (!(sattr->ia_valid & ATTR_SIZE)) {
2497 pr_warn_once("NFSv4: server %s is incorrectly "
2498 "applying open mode checks to "
2499 "a SETATTR that is not "
2500 "changing file size.\n",
2501 server->nfs_client->cl_hostname);
2503 if (state && !(state->state & FMODE_WRITE)) {
2505 if (sattr->ia_valid & ATTR_OPEN)
2510 err = nfs4_handle_exception(server, err, &exception);
2511 } while (exception.retry);
2516 struct nfs4_closedata {
2517 struct inode *inode;
2518 struct nfs4_state *state;
2519 struct nfs_closeargs arg;
2520 struct nfs_closeres res;
2521 struct nfs_fattr fattr;
2522 unsigned long timestamp;
2527 static void nfs4_free_closedata(void *data)
2529 struct nfs4_closedata *calldata = data;
2530 struct nfs4_state_owner *sp = calldata->state->owner;
2531 struct super_block *sb = calldata->state->inode->i_sb;
2534 pnfs_roc_release(calldata->state->inode);
2535 nfs4_put_open_state(calldata->state);
2536 nfs_free_seqid(calldata->arg.seqid);
2537 nfs4_put_state_owner(sp);
2538 nfs_sb_deactive(sb);
2542 static void nfs4_close_done(struct rpc_task *task, void *data)
2544 struct nfs4_closedata *calldata = data;
2545 struct nfs4_state *state = calldata->state;
2546 struct nfs_server *server = NFS_SERVER(calldata->inode);
2548 dprintk("%s: begin!\n", __func__);
2549 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2551 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2552 /* hmm. we are done with the inode, and in the process of freeing
2553 * the state_owner. we keep this around to process errors
2555 switch (task->tk_status) {
2558 pnfs_roc_set_barrier(state->inode,
2559 calldata->roc_barrier);
2560 nfs_clear_open_stateid(state, &calldata->res.stateid, 0);
2561 renew_lease(server, calldata->timestamp);
2563 case -NFS4ERR_ADMIN_REVOKED:
2564 case -NFS4ERR_STALE_STATEID:
2565 case -NFS4ERR_OLD_STATEID:
2566 case -NFS4ERR_BAD_STATEID:
2567 case -NFS4ERR_EXPIRED:
2568 if (calldata->arg.fmode == 0)
2571 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
2572 rpc_restart_call_prepare(task);
2576 nfs_clear_open_stateid(state, NULL, calldata->arg.fmode);
2578 nfs_release_seqid(calldata->arg.seqid);
2579 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2580 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2583 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2585 struct nfs4_closedata *calldata = data;
2586 struct nfs4_state *state = calldata->state;
2587 struct inode *inode = calldata->inode;
2590 dprintk("%s: begin!\n", __func__);
2591 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2594 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2595 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2596 spin_lock(&state->owner->so_lock);
2597 /* Calculate the change in open mode */
2598 if (state->n_rdwr == 0) {
2599 if (state->n_rdonly == 0) {
2600 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2601 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2602 calldata->arg.fmode &= ~FMODE_READ;
2604 if (state->n_wronly == 0) {
2605 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2606 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2607 calldata->arg.fmode &= ~FMODE_WRITE;
2610 if (!nfs4_valid_open_stateid(state))
2612 spin_unlock(&state->owner->so_lock);
2615 /* Note: exit _without_ calling nfs4_close_done */
2619 if (calldata->arg.fmode == 0) {
2620 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2621 if (calldata->roc &&
2622 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2623 nfs_release_seqid(calldata->arg.seqid);
2628 nfs_fattr_init(calldata->res.fattr);
2629 calldata->timestamp = jiffies;
2630 if (nfs4_setup_sequence(NFS_SERVER(inode),
2631 &calldata->arg.seq_args,
2632 &calldata->res.seq_res,
2634 nfs_release_seqid(calldata->arg.seqid);
2635 dprintk("%s: done!\n", __func__);
2638 task->tk_action = NULL;
2640 nfs4_sequence_done(task, &calldata->res.seq_res);
2643 static const struct rpc_call_ops nfs4_close_ops = {
2644 .rpc_call_prepare = nfs4_close_prepare,
2645 .rpc_call_done = nfs4_close_done,
2646 .rpc_release = nfs4_free_closedata,
2650 * It is possible for data to be read/written from a mem-mapped file
2651 * after the sys_close call (which hits the vfs layer as a flush).
2652 * This means that we can't safely call nfsv4 close on a file until
2653 * the inode is cleared. This in turn means that we are not good
2654 * NFSv4 citizens - we do not indicate to the server to update the file's
2655 * share state even when we are done with one of the three share
2656 * stateid's in the inode.
2658 * NOTE: Caller must be holding the sp->so_owner semaphore!
2660 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2662 struct nfs_server *server = NFS_SERVER(state->inode);
2663 struct nfs4_closedata *calldata;
2664 struct nfs4_state_owner *sp = state->owner;
2665 struct rpc_task *task;
2666 struct rpc_message msg = {
2667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2668 .rpc_cred = state->owner->so_cred,
2670 struct rpc_task_setup task_setup_data = {
2671 .rpc_client = server->client,
2672 .rpc_message = &msg,
2673 .callback_ops = &nfs4_close_ops,
2674 .workqueue = nfsiod_workqueue,
2675 .flags = RPC_TASK_ASYNC,
2677 int status = -ENOMEM;
2679 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2680 &task_setup_data.rpc_client, &msg);
2682 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2683 if (calldata == NULL)
2685 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2686 calldata->inode = state->inode;
2687 calldata->state = state;
2688 calldata->arg.fh = NFS_FH(state->inode);
2689 calldata->arg.stateid = &state->open_stateid;
2690 /* Serialization for the sequence id */
2691 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2692 if (calldata->arg.seqid == NULL)
2693 goto out_free_calldata;
2694 calldata->arg.fmode = 0;
2695 calldata->arg.bitmask = server->cache_consistency_bitmask;
2696 calldata->res.fattr = &calldata->fattr;
2697 calldata->res.seqid = calldata->arg.seqid;
2698 calldata->res.server = server;
2699 calldata->roc = pnfs_roc(state->inode);
2700 nfs_sb_active(calldata->inode->i_sb);
2702 msg.rpc_argp = &calldata->arg;
2703 msg.rpc_resp = &calldata->res;
2704 task_setup_data.callback_data = calldata;
2705 task = rpc_run_task(&task_setup_data);
2707 return PTR_ERR(task);
2710 status = rpc_wait_for_completion_task(task);
2716 nfs4_put_open_state(state);
2717 nfs4_put_state_owner(sp);
2721 static struct inode *
2722 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2723 int open_flags, struct iattr *attr, int *opened)
2725 struct nfs4_state *state;
2726 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2728 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2730 /* Protect against concurrent sillydeletes */
2731 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2733 nfs4_label_release_security(label);
2736 return ERR_CAST(state);
2737 return state->inode;
2740 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2742 if (ctx->state == NULL)
2745 nfs4_close_sync(ctx->state, ctx->mode);
2747 nfs4_close_state(ctx->state, ctx->mode);
2750 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2751 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2752 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_CHANGE_SECURITY_LABEL - 1UL)
2754 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2756 struct nfs4_server_caps_arg args = {
2759 struct nfs4_server_caps_res res = {};
2760 struct rpc_message msg = {
2761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2767 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2769 /* Sanity check the server answers */
2770 switch (server->nfs_client->cl_minorversion) {
2772 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2773 res.attr_bitmask[2] = 0;
2776 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2779 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2781 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2782 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2783 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2784 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2785 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2786 NFS_CAP_CTIME|NFS_CAP_MTIME|
2787 NFS_CAP_SECURITY_LABEL);
2788 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2789 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2790 server->caps |= NFS_CAP_ACLS;
2791 if (res.has_links != 0)
2792 server->caps |= NFS_CAP_HARDLINKS;
2793 if (res.has_symlinks != 0)
2794 server->caps |= NFS_CAP_SYMLINKS;
2795 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2796 server->caps |= NFS_CAP_FILEID;
2797 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2798 server->caps |= NFS_CAP_MODE;
2799 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2800 server->caps |= NFS_CAP_NLINK;
2801 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2802 server->caps |= NFS_CAP_OWNER;
2803 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2804 server->caps |= NFS_CAP_OWNER_GROUP;
2805 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2806 server->caps |= NFS_CAP_ATIME;
2807 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2808 server->caps |= NFS_CAP_CTIME;
2809 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2810 server->caps |= NFS_CAP_MTIME;
2811 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2812 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2813 server->caps |= NFS_CAP_SECURITY_LABEL;
2815 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2816 sizeof(server->attr_bitmask));
2817 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2819 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2820 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2821 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2822 server->cache_consistency_bitmask[2] = 0;
2823 server->acl_bitmask = res.acl_bitmask;
2824 server->fh_expire_type = res.fh_expire_type;
2830 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2832 struct nfs4_exception exception = { };
2835 err = nfs4_handle_exception(server,
2836 _nfs4_server_capabilities(server, fhandle),
2838 } while (exception.retry);
2842 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2843 struct nfs_fsinfo *info)
2846 struct nfs4_lookup_root_arg args = {
2849 struct nfs4_lookup_res res = {
2851 .fattr = info->fattr,
2854 struct rpc_message msg = {
2855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2860 bitmask[0] = nfs4_fattr_bitmap[0];
2861 bitmask[1] = nfs4_fattr_bitmap[1];
2863 * Process the label in the upcoming getfattr
2865 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2867 nfs_fattr_init(info->fattr);
2868 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2871 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2872 struct nfs_fsinfo *info)
2874 struct nfs4_exception exception = { };
2877 err = _nfs4_lookup_root(server, fhandle, info);
2878 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2881 case -NFS4ERR_WRONGSEC:
2884 err = nfs4_handle_exception(server, err, &exception);
2886 } while (exception.retry);
2891 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2892 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2894 struct rpc_auth_create_args auth_args = {
2895 .pseudoflavor = flavor,
2897 struct rpc_auth *auth;
2900 auth = rpcauth_create(&auth_args, server->client);
2905 ret = nfs4_lookup_root(server, fhandle, info);
2911 * Retry pseudoroot lookup with various security flavors. We do this when:
2913 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2914 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2916 * Returns zero on success, or a negative NFS4ERR value, or a
2917 * negative errno value.
2919 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2920 struct nfs_fsinfo *info)
2922 /* Per 3530bis 15.33.5 */
2923 static const rpc_authflavor_t flav_array[] = {
2927 RPC_AUTH_UNIX, /* courtesy */
2930 int status = -EPERM;
2933 if (server->auth_info.flavor_len > 0) {
2934 /* try each flavor specified by user */
2935 for (i = 0; i < server->auth_info.flavor_len; i++) {
2936 status = nfs4_lookup_root_sec(server, fhandle, info,
2937 server->auth_info.flavors[i]);
2938 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2943 /* no flavors specified by user, try default list */
2944 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2945 status = nfs4_lookup_root_sec(server, fhandle, info,
2947 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2954 * -EACCESS could mean that the user doesn't have correct permissions
2955 * to access the mount. It could also mean that we tried to mount
2956 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2957 * existing mount programs don't handle -EACCES very well so it should
2958 * be mapped to -EPERM instead.
2960 if (status == -EACCES)
2965 static int nfs4_do_find_root_sec(struct nfs_server *server,
2966 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2968 int mv = server->nfs_client->cl_minorversion;
2969 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2973 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2974 * @server: initialized nfs_server handle
2975 * @fhandle: we fill in the pseudo-fs root file handle
2976 * @info: we fill in an FSINFO struct
2977 * @auth_probe: probe the auth flavours
2979 * Returns zero on success, or a negative errno.
2981 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2982 struct nfs_fsinfo *info,
2987 switch (auth_probe) {
2989 status = nfs4_lookup_root(server, fhandle, info);
2990 if (status != -NFS4ERR_WRONGSEC)
2993 status = nfs4_do_find_root_sec(server, fhandle, info);
2997 status = nfs4_server_capabilities(server, fhandle);
2999 status = nfs4_do_fsinfo(server, fhandle, info);
3001 return nfs4_map_errors(status);
3004 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3005 struct nfs_fsinfo *info)
3008 struct nfs_fattr *fattr = info->fattr;
3009 struct nfs4_label *label = NULL;
3011 error = nfs4_server_capabilities(server, mntfh);
3013 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3017 label = nfs4_label_alloc(server, GFP_KERNEL);
3019 return PTR_ERR(label);
3021 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3023 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3024 goto err_free_label;
3027 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3028 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3029 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3032 nfs4_label_free(label);
3038 * Get locations and (maybe) other attributes of a referral.
3039 * Note that we'll actually follow the referral later when
3040 * we detect fsid mismatch in inode revalidation
3042 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3043 const struct qstr *name, struct nfs_fattr *fattr,
3044 struct nfs_fh *fhandle)
3046 int status = -ENOMEM;
3047 struct page *page = NULL;
3048 struct nfs4_fs_locations *locations = NULL;
3050 page = alloc_page(GFP_KERNEL);
3053 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3054 if (locations == NULL)
3057 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3062 * If the fsid didn't change, this is a migration event, not a
3063 * referral. Cause us to drop into the exception handler, which
3064 * will kick off migration recovery.
3066 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3067 dprintk("%s: server did not return a different fsid for"
3068 " a referral at %s\n", __func__, name->name);
3069 status = -NFS4ERR_MOVED;
3072 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3073 nfs_fixup_referral_attributes(&locations->fattr);
3075 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3076 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3077 memset(fhandle, 0, sizeof(struct nfs_fh));
3085 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3086 struct nfs_fattr *fattr, struct nfs4_label *label)
3088 struct nfs4_getattr_arg args = {
3090 .bitmask = server->attr_bitmask,
3092 struct nfs4_getattr_res res = {
3097 struct rpc_message msg = {
3098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3103 args.bitmask = nfs4_bitmask(server, label);
3105 nfs_fattr_init(fattr);
3106 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3109 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3110 struct nfs_fattr *fattr, struct nfs4_label *label)
3112 struct nfs4_exception exception = { };
3115 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3116 trace_nfs4_getattr(server, fhandle, fattr, err);
3117 err = nfs4_handle_exception(server, err,
3119 } while (exception.retry);
3124 * The file is not closed if it is opened due to the a request to change
3125 * the size of the file. The open call will not be needed once the
3126 * VFS layer lookup-intents are implemented.
3128 * Close is called when the inode is destroyed.
3129 * If we haven't opened the file for O_WRONLY, we
3130 * need to in the size_change case to obtain a stateid.
3133 * Because OPEN is always done by name in nfsv4, it is
3134 * possible that we opened a different file by the same
3135 * name. We can recognize this race condition, but we
3136 * can't do anything about it besides returning an error.
3138 * This will be fixed with VFS changes (lookup-intent).
3141 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3142 struct iattr *sattr)
3144 struct inode *inode = dentry->d_inode;
3145 struct rpc_cred *cred = NULL;
3146 struct nfs4_state *state = NULL;
3147 struct nfs4_label *label = NULL;
3150 if (pnfs_ld_layoutret_on_setattr(inode))
3151 pnfs_commit_and_return_layout(inode);
3153 nfs_fattr_init(fattr);
3155 /* Deal with open(O_TRUNC) */
3156 if (sattr->ia_valid & ATTR_OPEN)
3157 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3159 /* Optimization: if the end result is no change, don't RPC */
3160 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3163 /* Search for an existing open(O_WRITE) file */
3164 if (sattr->ia_valid & ATTR_FILE) {
3165 struct nfs_open_context *ctx;
3167 ctx = nfs_file_open_context(sattr->ia_file);
3174 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3176 return PTR_ERR(label);
3178 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3180 nfs_setattr_update_inode(inode, sattr);
3181 nfs_setsecurity(inode, fattr, label);
3183 nfs4_label_free(label);
3187 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3188 const struct qstr *name, struct nfs_fh *fhandle,
3189 struct nfs_fattr *fattr, struct nfs4_label *label)
3191 struct nfs_server *server = NFS_SERVER(dir);
3193 struct nfs4_lookup_arg args = {
3194 .bitmask = server->attr_bitmask,
3195 .dir_fh = NFS_FH(dir),
3198 struct nfs4_lookup_res res = {
3204 struct rpc_message msg = {
3205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3210 args.bitmask = nfs4_bitmask(server, label);
3212 nfs_fattr_init(fattr);
3214 dprintk("NFS call lookup %s\n", name->name);
3215 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3216 dprintk("NFS reply lookup: %d\n", status);
3220 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3222 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3223 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3224 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3228 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3229 struct qstr *name, struct nfs_fh *fhandle,
3230 struct nfs_fattr *fattr, struct nfs4_label *label)
3232 struct nfs4_exception exception = { };
3233 struct rpc_clnt *client = *clnt;
3236 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3237 trace_nfs4_lookup(dir, name, err);
3239 case -NFS4ERR_BADNAME:
3242 case -NFS4ERR_MOVED:
3243 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3245 case -NFS4ERR_WRONGSEC:
3247 if (client != *clnt)
3249 client = nfs4_create_sec_client(client, dir, name);
3251 return PTR_ERR(client);
3253 exception.retry = 1;
3256 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3258 } while (exception.retry);
3263 else if (client != *clnt)
3264 rpc_shutdown_client(client);
3269 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3270 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3271 struct nfs4_label *label)
3274 struct rpc_clnt *client = NFS_CLIENT(dir);
3276 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3277 if (client != NFS_CLIENT(dir)) {
3278 rpc_shutdown_client(client);
3279 nfs_fixup_secinfo_attributes(fattr);
3285 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3286 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3288 struct rpc_clnt *client = NFS_CLIENT(dir);
3291 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3293 return ERR_PTR(status);
3294 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3297 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3299 struct nfs_server *server = NFS_SERVER(inode);
3300 struct nfs4_accessargs args = {
3301 .fh = NFS_FH(inode),
3302 .bitmask = server->cache_consistency_bitmask,
3304 struct nfs4_accessres res = {
3307 struct rpc_message msg = {
3308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3311 .rpc_cred = entry->cred,
3313 int mode = entry->mask;
3317 * Determine which access bits we want to ask for...
3319 if (mode & MAY_READ)
3320 args.access |= NFS4_ACCESS_READ;
3321 if (S_ISDIR(inode->i_mode)) {
3322 if (mode & MAY_WRITE)
3323 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3324 if (mode & MAY_EXEC)
3325 args.access |= NFS4_ACCESS_LOOKUP;
3327 if (mode & MAY_WRITE)
3328 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3329 if (mode & MAY_EXEC)
3330 args.access |= NFS4_ACCESS_EXECUTE;
3333 res.fattr = nfs_alloc_fattr();
3334 if (res.fattr == NULL)
3337 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3339 nfs_access_set_mask(entry, res.access);
3340 nfs_refresh_inode(inode, res.fattr);
3342 nfs_free_fattr(res.fattr);
3346 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3348 struct nfs4_exception exception = { };
3351 err = _nfs4_proc_access(inode, entry);
3352 trace_nfs4_access(inode, err);
3353 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3355 } while (exception.retry);
3360 * TODO: For the time being, we don't try to get any attributes
3361 * along with any of the zero-copy operations READ, READDIR,
3364 * In the case of the first three, we want to put the GETATTR
3365 * after the read-type operation -- this is because it is hard
3366 * to predict the length of a GETATTR response in v4, and thus
3367 * align the READ data correctly. This means that the GETATTR
3368 * may end up partially falling into the page cache, and we should
3369 * shift it into the 'tail' of the xdr_buf before processing.
3370 * To do this efficiently, we need to know the total length
3371 * of data received, which doesn't seem to be available outside
3374 * In the case of WRITE, we also want to put the GETATTR after
3375 * the operation -- in this case because we want to make sure
3376 * we get the post-operation mtime and size.
3378 * Both of these changes to the XDR layer would in fact be quite
3379 * minor, but I decided to leave them for a subsequent patch.
3381 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3382 unsigned int pgbase, unsigned int pglen)
3384 struct nfs4_readlink args = {
3385 .fh = NFS_FH(inode),
3390 struct nfs4_readlink_res res;
3391 struct rpc_message msg = {
3392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3397 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3400 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3401 unsigned int pgbase, unsigned int pglen)
3403 struct nfs4_exception exception = { };
3406 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3407 trace_nfs4_readlink(inode, err);
3408 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3410 } while (exception.retry);
3415 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3418 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3421 struct nfs4_label l, *ilabel = NULL;
3422 struct nfs_open_context *ctx;
3423 struct nfs4_state *state;
3427 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3429 return PTR_ERR(ctx);
3431 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3433 sattr->ia_mode &= ~current_umask();
3434 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3435 if (IS_ERR(state)) {
3436 status = PTR_ERR(state);
3440 nfs4_label_release_security(ilabel);
3441 put_nfs_open_context(ctx);
3445 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3447 struct nfs_server *server = NFS_SERVER(dir);
3448 struct nfs_removeargs args = {
3452 struct nfs_removeres res = {
3455 struct rpc_message msg = {
3456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3462 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3464 update_changeattr(dir, &res.cinfo);
3468 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3470 struct nfs4_exception exception = { };
3473 err = _nfs4_proc_remove(dir, name);
3474 trace_nfs4_remove(dir, name, err);
3475 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3477 } while (exception.retry);
3481 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3483 struct nfs_server *server = NFS_SERVER(dir);
3484 struct nfs_removeargs *args = msg->rpc_argp;
3485 struct nfs_removeres *res = msg->rpc_resp;
3487 res->server = server;
3488 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3489 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3491 nfs_fattr_init(res->dir_attr);
3494 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3496 nfs4_setup_sequence(NFS_SERVER(data->dir),
3497 &data->args.seq_args,
3502 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3504 struct nfs_unlinkdata *data = task->tk_calldata;
3505 struct nfs_removeres *res = &data->res;
3507 if (!nfs4_sequence_done(task, &res->seq_res))
3509 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3511 update_changeattr(dir, &res->cinfo);
3515 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3517 struct nfs_server *server = NFS_SERVER(dir);
3518 struct nfs_renameargs *arg = msg->rpc_argp;
3519 struct nfs_renameres *res = msg->rpc_resp;
3521 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3522 res->server = server;
3523 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3526 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3528 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3529 &data->args.seq_args,
3534 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3535 struct inode *new_dir)
3537 struct nfs_renamedata *data = task->tk_calldata;
3538 struct nfs_renameres *res = &data->res;
3540 if (!nfs4_sequence_done(task, &res->seq_res))
3542 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3545 update_changeattr(old_dir, &res->old_cinfo);
3546 update_changeattr(new_dir, &res->new_cinfo);
3550 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3552 struct nfs_server *server = NFS_SERVER(inode);
3553 struct nfs4_link_arg arg = {
3554 .fh = NFS_FH(inode),
3555 .dir_fh = NFS_FH(dir),
3557 .bitmask = server->attr_bitmask,
3559 struct nfs4_link_res res = {
3563 struct rpc_message msg = {
3564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3568 int status = -ENOMEM;
3570 res.fattr = nfs_alloc_fattr();
3571 if (res.fattr == NULL)
3574 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3575 if (IS_ERR(res.label)) {
3576 status = PTR_ERR(res.label);
3579 arg.bitmask = nfs4_bitmask(server, res.label);
3581 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3583 update_changeattr(dir, &res.cinfo);
3584 status = nfs_post_op_update_inode(inode, res.fattr);
3586 nfs_setsecurity(inode, res.fattr, res.label);
3590 nfs4_label_free(res.label);
3593 nfs_free_fattr(res.fattr);
3597 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3599 struct nfs4_exception exception = { };
3602 err = nfs4_handle_exception(NFS_SERVER(inode),
3603 _nfs4_proc_link(inode, dir, name),
3605 } while (exception.retry);
3609 struct nfs4_createdata {
3610 struct rpc_message msg;
3611 struct nfs4_create_arg arg;
3612 struct nfs4_create_res res;
3614 struct nfs_fattr fattr;
3615 struct nfs4_label *label;
3618 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3619 struct qstr *name, struct iattr *sattr, u32 ftype)
3621 struct nfs4_createdata *data;
3623 data = kzalloc(sizeof(*data), GFP_KERNEL);
3625 struct nfs_server *server = NFS_SERVER(dir);
3627 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3628 if (IS_ERR(data->label))
3631 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3632 data->msg.rpc_argp = &data->arg;
3633 data->msg.rpc_resp = &data->res;
3634 data->arg.dir_fh = NFS_FH(dir);
3635 data->arg.server = server;
3636 data->arg.name = name;
3637 data->arg.attrs = sattr;
3638 data->arg.ftype = ftype;
3639 data->arg.bitmask = nfs4_bitmask(server, data->label);
3640 data->res.server = server;
3641 data->res.fh = &data->fh;
3642 data->res.fattr = &data->fattr;
3643 data->res.label = data->label;
3644 nfs_fattr_init(data->res.fattr);
3652 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3654 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3655 &data->arg.seq_args, &data->res.seq_res, 1);
3657 update_changeattr(dir, &data->res.dir_cinfo);
3658 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3663 static void nfs4_free_createdata(struct nfs4_createdata *data)
3665 nfs4_label_free(data->label);
3669 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3670 struct page *page, unsigned int len, struct iattr *sattr,
3671 struct nfs4_label *label)
3673 struct nfs4_createdata *data;
3674 int status = -ENAMETOOLONG;
3676 if (len > NFS4_MAXPATHLEN)
3680 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3684 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3685 data->arg.u.symlink.pages = &page;
3686 data->arg.u.symlink.len = len;
3687 data->arg.label = label;
3689 status = nfs4_do_create(dir, dentry, data);
3691 nfs4_free_createdata(data);
3696 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3697 struct page *page, unsigned int len, struct iattr *sattr)
3699 struct nfs4_exception exception = { };
3700 struct nfs4_label l, *label = NULL;
3703 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3706 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3707 trace_nfs4_symlink(dir, &dentry->d_name, err);
3708 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3710 } while (exception.retry);
3712 nfs4_label_release_security(label);
3716 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3717 struct iattr *sattr, struct nfs4_label *label)
3719 struct nfs4_createdata *data;
3720 int status = -ENOMEM;
3722 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3726 data->arg.label = label;
3727 status = nfs4_do_create(dir, dentry, data);
3729 nfs4_free_createdata(data);
3734 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3735 struct iattr *sattr)
3737 struct nfs4_exception exception = { };
3738 struct nfs4_label l, *label = NULL;
3741 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3743 sattr->ia_mode &= ~current_umask();
3745 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3746 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3747 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3749 } while (exception.retry);
3750 nfs4_label_release_security(label);
3755 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3756 u64 cookie, struct page **pages, unsigned int count, int plus)
3758 struct inode *dir = dentry->d_inode;
3759 struct nfs4_readdir_arg args = {
3764 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3767 struct nfs4_readdir_res res;
3768 struct rpc_message msg = {
3769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3776 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3778 (unsigned long long)cookie);
3779 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3780 res.pgbase = args.pgbase;
3781 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3783 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3784 status += args.pgbase;
3787 nfs_invalidate_atime(dir);
3789 dprintk("%s: returns %d\n", __func__, status);
3793 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3794 u64 cookie, struct page **pages, unsigned int count, int plus)
3796 struct nfs4_exception exception = { };
3799 err = _nfs4_proc_readdir(dentry, cred, cookie,
3800 pages, count, plus);
3801 trace_nfs4_readdir(dentry->d_inode, err);
3802 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3804 } while (exception.retry);
3808 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3809 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3811 struct nfs4_createdata *data;
3812 int mode = sattr->ia_mode;
3813 int status = -ENOMEM;
3815 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3820 data->arg.ftype = NF4FIFO;
3821 else if (S_ISBLK(mode)) {
3822 data->arg.ftype = NF4BLK;
3823 data->arg.u.device.specdata1 = MAJOR(rdev);
3824 data->arg.u.device.specdata2 = MINOR(rdev);
3826 else if (S_ISCHR(mode)) {
3827 data->arg.ftype = NF4CHR;
3828 data->arg.u.device.specdata1 = MAJOR(rdev);
3829 data->arg.u.device.specdata2 = MINOR(rdev);
3830 } else if (!S_ISSOCK(mode)) {
3835 data->arg.label = label;
3836 status = nfs4_do_create(dir, dentry, data);
3838 nfs4_free_createdata(data);
3843 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3844 struct iattr *sattr, dev_t rdev)
3846 struct nfs4_exception exception = { };
3847 struct nfs4_label l, *label = NULL;
3850 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3852 sattr->ia_mode &= ~current_umask();
3854 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3855 trace_nfs4_mknod(dir, &dentry->d_name, err);
3856 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3858 } while (exception.retry);
3860 nfs4_label_release_security(label);
3865 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3866 struct nfs_fsstat *fsstat)
3868 struct nfs4_statfs_arg args = {
3870 .bitmask = server->attr_bitmask,
3872 struct nfs4_statfs_res res = {
3875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3881 nfs_fattr_init(fsstat->fattr);
3882 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3885 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3887 struct nfs4_exception exception = { };
3890 err = nfs4_handle_exception(server,
3891 _nfs4_proc_statfs(server, fhandle, fsstat),
3893 } while (exception.retry);
3897 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3898 struct nfs_fsinfo *fsinfo)
3900 struct nfs4_fsinfo_arg args = {
3902 .bitmask = server->attr_bitmask,
3904 struct nfs4_fsinfo_res res = {
3907 struct rpc_message msg = {
3908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3913 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3916 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3918 struct nfs4_exception exception = { };
3919 unsigned long now = jiffies;
3923 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3924 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
3926 struct nfs_client *clp = server->nfs_client;
3928 spin_lock(&clp->cl_lock);
3929 clp->cl_lease_time = fsinfo->lease_time * HZ;
3930 clp->cl_last_renewal = now;
3931 spin_unlock(&clp->cl_lock);
3934 err = nfs4_handle_exception(server, err, &exception);
3935 } while (exception.retry);
3939 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3943 nfs_fattr_init(fsinfo->fattr);
3944 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3946 /* block layout checks this! */
3947 server->pnfs_blksize = fsinfo->blksize;
3948 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3954 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3955 struct nfs_pathconf *pathconf)
3957 struct nfs4_pathconf_arg args = {
3959 .bitmask = server->attr_bitmask,
3961 struct nfs4_pathconf_res res = {
3962 .pathconf = pathconf,
3964 struct rpc_message msg = {
3965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3970 /* None of the pathconf attributes are mandatory to implement */
3971 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3972 memset(pathconf, 0, sizeof(*pathconf));
3976 nfs_fattr_init(pathconf->fattr);
3977 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3980 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3981 struct nfs_pathconf *pathconf)
3983 struct nfs4_exception exception = { };
3987 err = nfs4_handle_exception(server,
3988 _nfs4_proc_pathconf(server, fhandle, pathconf),
3990 } while (exception.retry);
3994 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
3995 const struct nfs_open_context *ctx,
3996 const struct nfs_lock_context *l_ctx,
3999 const struct nfs_lockowner *lockowner = NULL;
4002 lockowner = &l_ctx->lockowner;
4003 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4005 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4007 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4008 const struct nfs_open_context *ctx,
4009 const struct nfs_lock_context *l_ctx,
4012 nfs4_stateid current_stateid;
4014 /* If the current stateid represents a lost lock, then exit */
4015 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4017 return nfs4_stateid_match(stateid, ¤t_stateid);
4020 static bool nfs4_error_stateid_expired(int err)
4023 case -NFS4ERR_DELEG_REVOKED:
4024 case -NFS4ERR_ADMIN_REVOKED:
4025 case -NFS4ERR_BAD_STATEID:
4026 case -NFS4ERR_STALE_STATEID:
4027 case -NFS4ERR_OLD_STATEID:
4028 case -NFS4ERR_OPENMODE:
4029 case -NFS4ERR_EXPIRED:
4035 void __nfs4_read_done_cb(struct nfs_read_data *data)
4037 nfs_invalidate_atime(data->header->inode);
4040 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
4042 struct nfs_server *server = NFS_SERVER(data->header->inode);
4044 trace_nfs4_read(data, task->tk_status);
4045 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
4046 rpc_restart_call_prepare(task);
4050 __nfs4_read_done_cb(data);
4051 if (task->tk_status > 0)
4052 renew_lease(server, data->timestamp);
4056 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4057 struct nfs_readargs *args)
4060 if (!nfs4_error_stateid_expired(task->tk_status) ||
4061 nfs4_stateid_is_current(&args->stateid,
4066 rpc_restart_call_prepare(task);
4070 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4073 dprintk("--> %s\n", __func__);
4075 if (!nfs4_sequence_done(task, &data->res.seq_res))
4077 if (nfs4_read_stateid_changed(task, &data->args))
4079 return data->read_done_cb ? data->read_done_cb(task, data) :
4080 nfs4_read_done_cb(task, data);
4083 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
4085 data->timestamp = jiffies;
4086 data->read_done_cb = nfs4_read_done_cb;
4087 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4088 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
4091 static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
4093 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4094 &data->args.seq_args,
4098 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4099 data->args.lock_context, FMODE_READ) == -EIO)
4101 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4106 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
4108 struct inode *inode = data->header->inode;
4110 trace_nfs4_write(data, task->tk_status);
4111 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
4112 rpc_restart_call_prepare(task);
4115 if (task->tk_status >= 0) {
4116 renew_lease(NFS_SERVER(inode), data->timestamp);
4117 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4122 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4123 struct nfs_writeargs *args)
4126 if (!nfs4_error_stateid_expired(task->tk_status) ||
4127 nfs4_stateid_is_current(&args->stateid,
4132 rpc_restart_call_prepare(task);
4136 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4138 if (!nfs4_sequence_done(task, &data->res.seq_res))
4140 if (nfs4_write_stateid_changed(task, &data->args))
4142 return data->write_done_cb ? data->write_done_cb(task, data) :
4143 nfs4_write_done_cb(task, data);
4147 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
4149 const struct nfs_pgio_header *hdr = data->header;
4151 /* Don't request attributes for pNFS or O_DIRECT writes */
4152 if (data->ds_clp != NULL || hdr->dreq != NULL)
4154 /* Otherwise, request attributes if and only if we don't hold
4157 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4160 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
4162 struct nfs_server *server = NFS_SERVER(data->header->inode);
4164 if (!nfs4_write_need_cache_consistency_data(data)) {
4165 data->args.bitmask = NULL;
4166 data->res.fattr = NULL;
4168 data->args.bitmask = server->cache_consistency_bitmask;
4170 if (!data->write_done_cb)
4171 data->write_done_cb = nfs4_write_done_cb;
4172 data->res.server = server;
4173 data->timestamp = jiffies;
4175 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4176 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4179 static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4181 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4182 &data->args.seq_args,
4186 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4187 data->args.lock_context, FMODE_WRITE) == -EIO)
4189 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4194 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4196 nfs4_setup_sequence(NFS_SERVER(data->inode),
4197 &data->args.seq_args,
4202 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4204 struct inode *inode = data->inode;
4206 trace_nfs4_commit(data, task->tk_status);
4207 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
4208 rpc_restart_call_prepare(task);
4214 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4216 if (!nfs4_sequence_done(task, &data->res.seq_res))
4218 return data->commit_done_cb(task, data);
4221 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4223 struct nfs_server *server = NFS_SERVER(data->inode);
4225 if (data->commit_done_cb == NULL)
4226 data->commit_done_cb = nfs4_commit_done_cb;
4227 data->res.server = server;
4228 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4229 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4232 struct nfs4_renewdata {
4233 struct nfs_client *client;
4234 unsigned long timestamp;
4238 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4239 * standalone procedure for queueing an asynchronous RENEW.
4241 static void nfs4_renew_release(void *calldata)
4243 struct nfs4_renewdata *data = calldata;
4244 struct nfs_client *clp = data->client;
4246 if (atomic_read(&clp->cl_count) > 1)
4247 nfs4_schedule_state_renewal(clp);
4248 nfs_put_client(clp);
4252 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4254 struct nfs4_renewdata *data = calldata;
4255 struct nfs_client *clp = data->client;
4256 unsigned long timestamp = data->timestamp;
4258 trace_nfs4_renew_async(clp, task->tk_status);
4259 switch (task->tk_status) {
4262 case -NFS4ERR_LEASE_MOVED:
4263 nfs4_schedule_lease_moved_recovery(clp);
4266 /* Unless we're shutting down, schedule state recovery! */
4267 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4269 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4270 nfs4_schedule_lease_recovery(clp);
4273 nfs4_schedule_path_down_recovery(clp);
4275 do_renew_lease(clp, timestamp);
4278 static const struct rpc_call_ops nfs4_renew_ops = {
4279 .rpc_call_done = nfs4_renew_done,
4280 .rpc_release = nfs4_renew_release,
4283 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4285 struct rpc_message msg = {
4286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4290 struct nfs4_renewdata *data;
4292 if (renew_flags == 0)
4294 if (!atomic_inc_not_zero(&clp->cl_count))
4296 data = kmalloc(sizeof(*data), GFP_NOFS);
4300 data->timestamp = jiffies;
4301 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4302 &nfs4_renew_ops, data);
4305 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4307 struct rpc_message msg = {
4308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4312 unsigned long now = jiffies;
4315 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4318 do_renew_lease(clp, now);
4322 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4324 return server->caps & NFS_CAP_ACLS;
4327 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4328 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4331 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4333 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4334 struct page **pages, unsigned int *pgbase)
4336 struct page *newpage, **spages;
4342 len = min_t(size_t, PAGE_SIZE, buflen);
4343 newpage = alloc_page(GFP_KERNEL);
4345 if (newpage == NULL)
4347 memcpy(page_address(newpage), buf, len);
4352 } while (buflen != 0);
4358 __free_page(spages[rc-1]);
4362 struct nfs4_cached_acl {
4368 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4370 struct nfs_inode *nfsi = NFS_I(inode);
4372 spin_lock(&inode->i_lock);
4373 kfree(nfsi->nfs4_acl);
4374 nfsi->nfs4_acl = acl;
4375 spin_unlock(&inode->i_lock);
4378 static void nfs4_zap_acl_attr(struct inode *inode)
4380 nfs4_set_cached_acl(inode, NULL);
4383 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4385 struct nfs_inode *nfsi = NFS_I(inode);
4386 struct nfs4_cached_acl *acl;
4389 spin_lock(&inode->i_lock);
4390 acl = nfsi->nfs4_acl;
4393 if (buf == NULL) /* user is just asking for length */
4395 if (acl->cached == 0)
4397 ret = -ERANGE; /* see getxattr(2) man page */
4398 if (acl->len > buflen)
4400 memcpy(buf, acl->data, acl->len);
4404 spin_unlock(&inode->i_lock);
4408 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4410 struct nfs4_cached_acl *acl;
4411 size_t buflen = sizeof(*acl) + acl_len;
4413 if (buflen <= PAGE_SIZE) {
4414 acl = kmalloc(buflen, GFP_KERNEL);
4418 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4420 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4427 nfs4_set_cached_acl(inode, acl);
4431 * The getxattr API returns the required buffer length when called with a
4432 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4433 * the required buf. On a NULL buf, we send a page of data to the server
4434 * guessing that the ACL request can be serviced by a page. If so, we cache
4435 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4436 * the cache. If not so, we throw away the page, and cache the required
4437 * length. The next getxattr call will then produce another round trip to
4438 * the server, this time with the input buf of the required size.
4440 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4442 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4443 struct nfs_getaclargs args = {
4444 .fh = NFS_FH(inode),
4448 struct nfs_getaclres res = {
4451 struct rpc_message msg = {
4452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4456 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4457 int ret = -ENOMEM, i;
4459 /* As long as we're doing a round trip to the server anyway,
4460 * let's be prepared for a page of acl data. */
4463 if (npages > ARRAY_SIZE(pages))
4466 for (i = 0; i < npages; i++) {
4467 pages[i] = alloc_page(GFP_KERNEL);
4472 /* for decoding across pages */
4473 res.acl_scratch = alloc_page(GFP_KERNEL);
4474 if (!res.acl_scratch)
4477 args.acl_len = npages * PAGE_SIZE;
4478 args.acl_pgbase = 0;
4480 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4481 __func__, buf, buflen, npages, args.acl_len);
4482 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4483 &msg, &args.seq_args, &res.seq_res, 0);
4487 /* Handle the case where the passed-in buffer is too short */
4488 if (res.acl_flags & NFS4_ACL_TRUNC) {
4489 /* Did the user only issue a request for the acl length? */
4495 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4497 if (res.acl_len > buflen) {
4501 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4506 for (i = 0; i < npages; i++)
4508 __free_page(pages[i]);
4509 if (res.acl_scratch)
4510 __free_page(res.acl_scratch);
4514 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4516 struct nfs4_exception exception = { };
4519 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4520 trace_nfs4_get_acl(inode, ret);
4523 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4524 } while (exception.retry);
4528 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4530 struct nfs_server *server = NFS_SERVER(inode);
4533 if (!nfs4_server_supports_acls(server))
4535 ret = nfs_revalidate_inode(server, inode);
4538 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4539 nfs_zap_acl_cache(inode);
4540 ret = nfs4_read_cached_acl(inode, buf, buflen);
4542 /* -ENOENT is returned if there is no ACL or if there is an ACL
4543 * but no cached acl data, just the acl length */
4545 return nfs4_get_acl_uncached(inode, buf, buflen);
4548 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4550 struct nfs_server *server = NFS_SERVER(inode);
4551 struct page *pages[NFS4ACL_MAXPAGES];
4552 struct nfs_setaclargs arg = {
4553 .fh = NFS_FH(inode),
4557 struct nfs_setaclres res;
4558 struct rpc_message msg = {
4559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4563 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4566 if (!nfs4_server_supports_acls(server))
4568 if (npages > ARRAY_SIZE(pages))
4570 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4573 nfs4_inode_return_delegation(inode);
4574 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4577 * Free each page after tx, so the only ref left is
4578 * held by the network stack
4581 put_page(pages[i-1]);
4584 * Acl update can result in inode attribute update.
4585 * so mark the attribute cache invalid.
4587 spin_lock(&inode->i_lock);
4588 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4589 spin_unlock(&inode->i_lock);
4590 nfs_access_zap_cache(inode);
4591 nfs_zap_acl_cache(inode);
4595 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4597 struct nfs4_exception exception = { };
4600 err = __nfs4_proc_set_acl(inode, buf, buflen);
4601 trace_nfs4_set_acl(inode, err);
4602 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4604 } while (exception.retry);
4608 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4609 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4612 struct nfs_server *server = NFS_SERVER(inode);
4613 struct nfs_fattr fattr;
4614 struct nfs4_label label = {0, 0, buflen, buf};
4616 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4617 struct nfs4_getattr_arg arg = {
4618 .fh = NFS_FH(inode),
4621 struct nfs4_getattr_res res = {
4626 struct rpc_message msg = {
4627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4633 nfs_fattr_init(&fattr);
4635 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4638 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4640 if (buflen < label.len)
4645 static int nfs4_get_security_label(struct inode *inode, void *buf,
4648 struct nfs4_exception exception = { };
4651 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4655 err = _nfs4_get_security_label(inode, buf, buflen);
4656 trace_nfs4_get_security_label(inode, err);
4657 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4659 } while (exception.retry);
4663 static int _nfs4_do_set_security_label(struct inode *inode,
4664 struct nfs4_label *ilabel,
4665 struct nfs_fattr *fattr,
4666 struct nfs4_label *olabel)
4669 struct iattr sattr = {0};
4670 struct nfs_server *server = NFS_SERVER(inode);
4671 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4672 struct nfs_setattrargs arg = {
4673 .fh = NFS_FH(inode),
4679 struct nfs_setattrres res = {
4684 struct rpc_message msg = {
4685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4691 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4693 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4695 dprintk("%s failed: %d\n", __func__, status);
4700 static int nfs4_do_set_security_label(struct inode *inode,
4701 struct nfs4_label *ilabel,
4702 struct nfs_fattr *fattr,
4703 struct nfs4_label *olabel)
4705 struct nfs4_exception exception = { };
4709 err = _nfs4_do_set_security_label(inode, ilabel,
4711 trace_nfs4_set_security_label(inode, err);
4712 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4714 } while (exception.retry);
4719 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4721 struct nfs4_label ilabel, *olabel = NULL;
4722 struct nfs_fattr fattr;
4723 struct rpc_cred *cred;
4724 struct inode *inode = dentry->d_inode;
4727 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4730 nfs_fattr_init(&fattr);
4734 ilabel.label = (char *)buf;
4735 ilabel.len = buflen;
4737 cred = rpc_lookup_cred();
4739 return PTR_ERR(cred);
4741 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4742 if (IS_ERR(olabel)) {
4743 status = -PTR_ERR(olabel);
4747 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4749 nfs_setsecurity(inode, &fattr, olabel);
4751 nfs4_label_free(olabel);
4756 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4760 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4762 struct nfs_client *clp = server->nfs_client;
4764 if (task->tk_status >= 0)
4766 switch(task->tk_status) {
4767 case -NFS4ERR_DELEG_REVOKED:
4768 case -NFS4ERR_ADMIN_REVOKED:
4769 case -NFS4ERR_BAD_STATEID:
4772 nfs_remove_bad_delegation(state->inode);
4773 case -NFS4ERR_OPENMODE:
4776 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4777 goto recovery_failed;
4778 goto wait_on_recovery;
4779 case -NFS4ERR_EXPIRED:
4780 if (state != NULL) {
4781 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4782 goto recovery_failed;
4784 case -NFS4ERR_STALE_STATEID:
4785 case -NFS4ERR_STALE_CLIENTID:
4786 nfs4_schedule_lease_recovery(clp);
4787 goto wait_on_recovery;
4788 case -NFS4ERR_MOVED:
4789 if (nfs4_schedule_migration_recovery(server) < 0)
4790 goto recovery_failed;
4791 goto wait_on_recovery;
4792 case -NFS4ERR_LEASE_MOVED:
4793 nfs4_schedule_lease_moved_recovery(clp);
4794 goto wait_on_recovery;
4795 #if defined(CONFIG_NFS_V4_1)
4796 case -NFS4ERR_BADSESSION:
4797 case -NFS4ERR_BADSLOT:
4798 case -NFS4ERR_BAD_HIGH_SLOT:
4799 case -NFS4ERR_DEADSESSION:
4800 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4801 case -NFS4ERR_SEQ_FALSE_RETRY:
4802 case -NFS4ERR_SEQ_MISORDERED:
4803 dprintk("%s ERROR %d, Reset session\n", __func__,
4805 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4806 goto wait_on_recovery;
4807 #endif /* CONFIG_NFS_V4_1 */
4808 case -NFS4ERR_DELAY:
4809 nfs_inc_server_stats(server, NFSIOS_DELAY);
4810 case -NFS4ERR_GRACE:
4811 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4812 case -NFS4ERR_RETRY_UNCACHED_REP:
4813 case -NFS4ERR_OLD_STATEID:
4816 task->tk_status = nfs4_map_errors(task->tk_status);
4819 task->tk_status = -EIO;
4822 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4823 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4824 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4825 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4826 goto recovery_failed;
4828 task->tk_status = 0;
4832 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4833 nfs4_verifier *bootverf)
4837 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4838 /* An impossible timestamp guarantees this value
4839 * will never match a generated boot time. */
4841 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4843 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4844 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4845 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4847 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4851 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4852 char *buf, size_t len)
4854 unsigned int result;
4857 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4859 rpc_peeraddr2str(clp->cl_rpcclient,
4861 rpc_peeraddr2str(clp->cl_rpcclient,
4862 RPC_DISPLAY_PROTO));
4868 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4869 char *buf, size_t len)
4871 const char *nodename = clp->cl_rpcclient->cl_nodename;
4873 if (nfs4_client_id_uniquifier[0] != '\0')
4874 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4875 clp->rpc_ops->version,
4876 clp->cl_minorversion,
4877 nfs4_client_id_uniquifier,
4879 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4880 clp->rpc_ops->version, clp->cl_minorversion,
4885 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4886 * services. Advertise one based on the address family of the
4890 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4892 if (strchr(clp->cl_ipaddr, ':') != NULL)
4893 return scnprintf(buf, len, "tcp6");
4895 return scnprintf(buf, len, "tcp");
4899 * nfs4_proc_setclientid - Negotiate client ID
4900 * @clp: state data structure
4901 * @program: RPC program for NFSv4 callback service
4902 * @port: IP port number for NFS4 callback service
4903 * @cred: RPC credential to use for this call
4904 * @res: where to place the result
4906 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4908 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4909 unsigned short port, struct rpc_cred *cred,
4910 struct nfs4_setclientid_res *res)
4912 nfs4_verifier sc_verifier;
4913 struct nfs4_setclientid setclientid = {
4914 .sc_verifier = &sc_verifier,
4916 .sc_cb_ident = clp->cl_cb_ident,
4918 struct rpc_message msg = {
4919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4920 .rpc_argp = &setclientid,
4926 /* nfs_client_id4 */
4927 nfs4_init_boot_verifier(clp, &sc_verifier);
4928 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4929 setclientid.sc_name_len =
4930 nfs4_init_uniform_client_string(clp,
4931 setclientid.sc_name,
4932 sizeof(setclientid.sc_name));
4934 setclientid.sc_name_len =
4935 nfs4_init_nonuniform_client_string(clp,
4936 setclientid.sc_name,
4937 sizeof(setclientid.sc_name));
4939 setclientid.sc_netid_len =
4940 nfs4_init_callback_netid(clp,
4941 setclientid.sc_netid,
4942 sizeof(setclientid.sc_netid));
4943 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4944 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4945 clp->cl_ipaddr, port >> 8, port & 255);
4947 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4948 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4949 setclientid.sc_name_len, setclientid.sc_name);
4950 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4951 trace_nfs4_setclientid(clp, status);
4952 dprintk("NFS reply setclientid: %d\n", status);
4957 * nfs4_proc_setclientid_confirm - Confirm client ID
4958 * @clp: state data structure
4959 * @res: result of a previous SETCLIENTID
4960 * @cred: RPC credential to use for this call
4962 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4964 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4965 struct nfs4_setclientid_res *arg,
4966 struct rpc_cred *cred)
4968 struct rpc_message msg = {
4969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4975 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4976 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4978 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4979 trace_nfs4_setclientid_confirm(clp, status);
4980 dprintk("NFS reply setclientid_confirm: %d\n", status);
4984 struct nfs4_delegreturndata {
4985 struct nfs4_delegreturnargs args;
4986 struct nfs4_delegreturnres res;
4988 nfs4_stateid stateid;
4989 unsigned long timestamp;
4990 struct nfs_fattr fattr;
4994 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4996 struct nfs4_delegreturndata *data = calldata;
4998 if (!nfs4_sequence_done(task, &data->res.seq_res))
5001 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5002 switch (task->tk_status) {
5004 renew_lease(data->res.server, data->timestamp);
5006 case -NFS4ERR_ADMIN_REVOKED:
5007 case -NFS4ERR_DELEG_REVOKED:
5008 case -NFS4ERR_BAD_STATEID:
5009 case -NFS4ERR_OLD_STATEID:
5010 case -NFS4ERR_STALE_STATEID:
5011 case -NFS4ERR_EXPIRED:
5012 task->tk_status = 0;
5015 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5017 rpc_restart_call_prepare(task);
5021 data->rpc_status = task->tk_status;
5024 static void nfs4_delegreturn_release(void *calldata)
5029 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5031 struct nfs4_delegreturndata *d_data;
5033 d_data = (struct nfs4_delegreturndata *)data;
5035 nfs4_setup_sequence(d_data->res.server,
5036 &d_data->args.seq_args,
5037 &d_data->res.seq_res,
5041 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5042 .rpc_call_prepare = nfs4_delegreturn_prepare,
5043 .rpc_call_done = nfs4_delegreturn_done,
5044 .rpc_release = nfs4_delegreturn_release,
5047 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5049 struct nfs4_delegreturndata *data;
5050 struct nfs_server *server = NFS_SERVER(inode);
5051 struct rpc_task *task;
5052 struct rpc_message msg = {
5053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5056 struct rpc_task_setup task_setup_data = {
5057 .rpc_client = server->client,
5058 .rpc_message = &msg,
5059 .callback_ops = &nfs4_delegreturn_ops,
5060 .flags = RPC_TASK_ASYNC,
5064 data = kzalloc(sizeof(*data), GFP_NOFS);
5067 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5068 data->args.fhandle = &data->fh;
5069 data->args.stateid = &data->stateid;
5070 data->args.bitmask = server->cache_consistency_bitmask;
5071 nfs_copy_fh(&data->fh, NFS_FH(inode));
5072 nfs4_stateid_copy(&data->stateid, stateid);
5073 data->res.fattr = &data->fattr;
5074 data->res.server = server;
5075 nfs_fattr_init(data->res.fattr);
5076 data->timestamp = jiffies;
5077 data->rpc_status = 0;
5079 task_setup_data.callback_data = data;
5080 msg.rpc_argp = &data->args;
5081 msg.rpc_resp = &data->res;
5082 task = rpc_run_task(&task_setup_data);
5084 return PTR_ERR(task);
5087 status = nfs4_wait_for_completion_rpc_task(task);
5090 status = data->rpc_status;
5092 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5094 nfs_refresh_inode(inode, &data->fattr);
5100 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5102 struct nfs_server *server = NFS_SERVER(inode);
5103 struct nfs4_exception exception = { };
5106 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5107 trace_nfs4_delegreturn(inode, err);
5109 case -NFS4ERR_STALE_STATEID:
5110 case -NFS4ERR_EXPIRED:
5114 err = nfs4_handle_exception(server, err, &exception);
5115 } while (exception.retry);
5119 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5120 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5123 * sleep, with exponential backoff, and retry the LOCK operation.
5125 static unsigned long
5126 nfs4_set_lock_task_retry(unsigned long timeout)
5128 freezable_schedule_timeout_killable_unsafe(timeout);
5130 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5131 return NFS4_LOCK_MAXTIMEOUT;
5135 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5137 struct inode *inode = state->inode;
5138 struct nfs_server *server = NFS_SERVER(inode);
5139 struct nfs_client *clp = server->nfs_client;
5140 struct nfs_lockt_args arg = {
5141 .fh = NFS_FH(inode),
5144 struct nfs_lockt_res res = {
5147 struct rpc_message msg = {
5148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5151 .rpc_cred = state->owner->so_cred,
5153 struct nfs4_lock_state *lsp;
5156 arg.lock_owner.clientid = clp->cl_clientid;
5157 status = nfs4_set_lock_state(state, request);
5160 lsp = request->fl_u.nfs4_fl.owner;
5161 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5162 arg.lock_owner.s_dev = server->s_dev;
5163 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5166 request->fl_type = F_UNLCK;
5168 case -NFS4ERR_DENIED:
5171 request->fl_ops->fl_release_private(request);
5172 request->fl_ops = NULL;
5177 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5179 struct nfs4_exception exception = { };
5183 err = _nfs4_proc_getlk(state, cmd, request);
5184 trace_nfs4_get_lock(request, state, cmd, err);
5185 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5187 } while (exception.retry);
5191 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5194 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5196 res = posix_lock_file_wait(file, fl);
5199 res = flock_lock_file_wait(file, fl);
5207 struct nfs4_unlockdata {
5208 struct nfs_locku_args arg;
5209 struct nfs_locku_res res;
5210 struct nfs4_lock_state *lsp;
5211 struct nfs_open_context *ctx;
5212 struct file_lock fl;
5213 const struct nfs_server *server;
5214 unsigned long timestamp;
5217 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5218 struct nfs_open_context *ctx,
5219 struct nfs4_lock_state *lsp,
5220 struct nfs_seqid *seqid)
5222 struct nfs4_unlockdata *p;
5223 struct inode *inode = lsp->ls_state->inode;
5225 p = kzalloc(sizeof(*p), GFP_NOFS);
5228 p->arg.fh = NFS_FH(inode);
5230 p->arg.seqid = seqid;
5231 p->res.seqid = seqid;
5232 p->arg.stateid = &lsp->ls_stateid;
5234 atomic_inc(&lsp->ls_count);
5235 /* Ensure we don't close file until we're done freeing locks! */
5236 p->ctx = get_nfs_open_context(ctx);
5237 memcpy(&p->fl, fl, sizeof(p->fl));
5238 p->server = NFS_SERVER(inode);
5242 static void nfs4_locku_release_calldata(void *data)
5244 struct nfs4_unlockdata *calldata = data;
5245 nfs_free_seqid(calldata->arg.seqid);
5246 nfs4_put_lock_state(calldata->lsp);
5247 put_nfs_open_context(calldata->ctx);
5251 static void nfs4_locku_done(struct rpc_task *task, void *data)
5253 struct nfs4_unlockdata *calldata = data;
5255 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5257 switch (task->tk_status) {
5259 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5260 &calldata->res.stateid);
5261 renew_lease(calldata->server, calldata->timestamp);
5263 case -NFS4ERR_BAD_STATEID:
5264 case -NFS4ERR_OLD_STATEID:
5265 case -NFS4ERR_STALE_STATEID:
5266 case -NFS4ERR_EXPIRED:
5269 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
5270 rpc_restart_call_prepare(task);
5272 nfs_release_seqid(calldata->arg.seqid);
5275 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5277 struct nfs4_unlockdata *calldata = data;
5279 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5281 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5282 /* Note: exit _without_ running nfs4_locku_done */
5285 calldata->timestamp = jiffies;
5286 if (nfs4_setup_sequence(calldata->server,
5287 &calldata->arg.seq_args,
5288 &calldata->res.seq_res,
5290 nfs_release_seqid(calldata->arg.seqid);
5293 task->tk_action = NULL;
5295 nfs4_sequence_done(task, &calldata->res.seq_res);
5298 static const struct rpc_call_ops nfs4_locku_ops = {
5299 .rpc_call_prepare = nfs4_locku_prepare,
5300 .rpc_call_done = nfs4_locku_done,
5301 .rpc_release = nfs4_locku_release_calldata,
5304 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5305 struct nfs_open_context *ctx,
5306 struct nfs4_lock_state *lsp,
5307 struct nfs_seqid *seqid)
5309 struct nfs4_unlockdata *data;
5310 struct rpc_message msg = {
5311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5312 .rpc_cred = ctx->cred,
5314 struct rpc_task_setup task_setup_data = {
5315 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5316 .rpc_message = &msg,
5317 .callback_ops = &nfs4_locku_ops,
5318 .workqueue = nfsiod_workqueue,
5319 .flags = RPC_TASK_ASYNC,
5322 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5323 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5325 /* Ensure this is an unlock - when canceling a lock, the
5326 * canceled lock is passed in, and it won't be an unlock.
5328 fl->fl_type = F_UNLCK;
5330 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5332 nfs_free_seqid(seqid);
5333 return ERR_PTR(-ENOMEM);
5336 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5337 msg.rpc_argp = &data->arg;
5338 msg.rpc_resp = &data->res;
5339 task_setup_data.callback_data = data;
5340 return rpc_run_task(&task_setup_data);
5343 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5345 struct inode *inode = state->inode;
5346 struct nfs4_state_owner *sp = state->owner;
5347 struct nfs_inode *nfsi = NFS_I(inode);
5348 struct nfs_seqid *seqid;
5349 struct nfs4_lock_state *lsp;
5350 struct rpc_task *task;
5352 unsigned char fl_flags = request->fl_flags;
5354 status = nfs4_set_lock_state(state, request);
5355 /* Unlock _before_ we do the RPC call */
5356 request->fl_flags |= FL_EXISTS;
5357 /* Exclude nfs_delegation_claim_locks() */
5358 mutex_lock(&sp->so_delegreturn_mutex);
5359 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5360 down_read(&nfsi->rwsem);
5361 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5362 up_read(&nfsi->rwsem);
5363 mutex_unlock(&sp->so_delegreturn_mutex);
5366 up_read(&nfsi->rwsem);
5367 mutex_unlock(&sp->so_delegreturn_mutex);
5370 /* Is this a delegated lock? */
5371 lsp = request->fl_u.nfs4_fl.owner;
5372 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5374 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5378 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5379 status = PTR_ERR(task);
5382 status = nfs4_wait_for_completion_rpc_task(task);
5385 request->fl_flags = fl_flags;
5386 trace_nfs4_unlock(request, state, F_SETLK, status);
5390 struct nfs4_lockdata {
5391 struct nfs_lock_args arg;
5392 struct nfs_lock_res res;
5393 struct nfs4_lock_state *lsp;
5394 struct nfs_open_context *ctx;
5395 struct file_lock fl;
5396 unsigned long timestamp;
5399 struct nfs_server *server;
5402 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5403 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5406 struct nfs4_lockdata *p;
5407 struct inode *inode = lsp->ls_state->inode;
5408 struct nfs_server *server = NFS_SERVER(inode);
5410 p = kzalloc(sizeof(*p), gfp_mask);
5414 p->arg.fh = NFS_FH(inode);
5416 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5417 if (p->arg.open_seqid == NULL)
5419 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5420 if (p->arg.lock_seqid == NULL)
5421 goto out_free_seqid;
5422 p->arg.lock_stateid = &lsp->ls_stateid;
5423 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5424 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5425 p->arg.lock_owner.s_dev = server->s_dev;
5426 p->res.lock_seqid = p->arg.lock_seqid;
5429 atomic_inc(&lsp->ls_count);
5430 p->ctx = get_nfs_open_context(ctx);
5431 memcpy(&p->fl, fl, sizeof(p->fl));
5434 nfs_free_seqid(p->arg.open_seqid);
5440 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5442 struct nfs4_lockdata *data = calldata;
5443 struct nfs4_state *state = data->lsp->ls_state;
5445 dprintk("%s: begin!\n", __func__);
5446 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5448 /* Do we need to do an open_to_lock_owner? */
5449 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5450 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5451 goto out_release_lock_seqid;
5453 data->arg.open_stateid = &state->open_stateid;
5454 data->arg.new_lock_owner = 1;
5455 data->res.open_seqid = data->arg.open_seqid;
5457 data->arg.new_lock_owner = 0;
5458 if (!nfs4_valid_open_stateid(state)) {
5459 data->rpc_status = -EBADF;
5460 task->tk_action = NULL;
5461 goto out_release_open_seqid;
5463 data->timestamp = jiffies;
5464 if (nfs4_setup_sequence(data->server,
5465 &data->arg.seq_args,
5469 out_release_open_seqid:
5470 nfs_release_seqid(data->arg.open_seqid);
5471 out_release_lock_seqid:
5472 nfs_release_seqid(data->arg.lock_seqid);
5474 nfs4_sequence_done(task, &data->res.seq_res);
5475 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5478 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5480 struct nfs4_lockdata *data = calldata;
5482 dprintk("%s: begin!\n", __func__);
5484 if (!nfs4_sequence_done(task, &data->res.seq_res))
5487 data->rpc_status = task->tk_status;
5488 if (data->arg.new_lock_owner != 0) {
5489 if (data->rpc_status == 0)
5490 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5494 if (data->rpc_status == 0) {
5495 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5496 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5497 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5500 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5503 static void nfs4_lock_release(void *calldata)
5505 struct nfs4_lockdata *data = calldata;
5507 dprintk("%s: begin!\n", __func__);
5508 nfs_free_seqid(data->arg.open_seqid);
5509 if (data->cancelled != 0) {
5510 struct rpc_task *task;
5511 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5512 data->arg.lock_seqid);
5514 rpc_put_task_async(task);
5515 dprintk("%s: cancelling lock!\n", __func__);
5517 nfs_free_seqid(data->arg.lock_seqid);
5518 nfs4_put_lock_state(data->lsp);
5519 put_nfs_open_context(data->ctx);
5521 dprintk("%s: done!\n", __func__);
5524 static const struct rpc_call_ops nfs4_lock_ops = {
5525 .rpc_call_prepare = nfs4_lock_prepare,
5526 .rpc_call_done = nfs4_lock_done,
5527 .rpc_release = nfs4_lock_release,
5530 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5533 case -NFS4ERR_ADMIN_REVOKED:
5534 case -NFS4ERR_BAD_STATEID:
5535 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5536 if (new_lock_owner != 0 ||
5537 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5538 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5540 case -NFS4ERR_STALE_STATEID:
5541 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5542 case -NFS4ERR_EXPIRED:
5543 nfs4_schedule_lease_recovery(server->nfs_client);
5547 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5549 struct nfs4_lockdata *data;
5550 struct rpc_task *task;
5551 struct rpc_message msg = {
5552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5553 .rpc_cred = state->owner->so_cred,
5555 struct rpc_task_setup task_setup_data = {
5556 .rpc_client = NFS_CLIENT(state->inode),
5557 .rpc_message = &msg,
5558 .callback_ops = &nfs4_lock_ops,
5559 .workqueue = nfsiod_workqueue,
5560 .flags = RPC_TASK_ASYNC,
5564 dprintk("%s: begin!\n", __func__);
5565 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5566 fl->fl_u.nfs4_fl.owner,
5567 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5571 data->arg.block = 1;
5572 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5573 msg.rpc_argp = &data->arg;
5574 msg.rpc_resp = &data->res;
5575 task_setup_data.callback_data = data;
5576 if (recovery_type > NFS_LOCK_NEW) {
5577 if (recovery_type == NFS_LOCK_RECLAIM)
5578 data->arg.reclaim = NFS_LOCK_RECLAIM;
5579 nfs4_set_sequence_privileged(&data->arg.seq_args);
5581 task = rpc_run_task(&task_setup_data);
5583 return PTR_ERR(task);
5584 ret = nfs4_wait_for_completion_rpc_task(task);
5586 ret = data->rpc_status;
5588 nfs4_handle_setlk_error(data->server, data->lsp,
5589 data->arg.new_lock_owner, ret);
5591 data->cancelled = 1;
5593 dprintk("%s: done, ret = %d!\n", __func__, ret);
5597 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5599 struct nfs_server *server = NFS_SERVER(state->inode);
5600 struct nfs4_exception exception = {
5601 .inode = state->inode,
5606 /* Cache the lock if possible... */
5607 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5609 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5610 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5611 if (err != -NFS4ERR_DELAY)
5613 nfs4_handle_exception(server, err, &exception);
5614 } while (exception.retry);
5618 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5620 struct nfs_server *server = NFS_SERVER(state->inode);
5621 struct nfs4_exception exception = {
5622 .inode = state->inode,
5626 err = nfs4_set_lock_state(state, request);
5629 if (!recover_lost_locks) {
5630 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5634 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5636 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5637 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5641 case -NFS4ERR_GRACE:
5642 case -NFS4ERR_DELAY:
5643 nfs4_handle_exception(server, err, &exception);
5646 } while (exception.retry);
5651 #if defined(CONFIG_NFS_V4_1)
5653 * nfs41_check_expired_locks - possibly free a lock stateid
5655 * @state: NFSv4 state for an inode
5657 * Returns NFS_OK if recovery for this stateid is now finished.
5658 * Otherwise a negative NFS4ERR value is returned.
5660 static int nfs41_check_expired_locks(struct nfs4_state *state)
5662 int status, ret = -NFS4ERR_BAD_STATEID;
5663 struct nfs4_lock_state *lsp;
5664 struct nfs_server *server = NFS_SERVER(state->inode);
5666 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5667 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5668 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5670 status = nfs41_test_stateid(server,
5673 trace_nfs4_test_lock_stateid(state, lsp, status);
5674 if (status != NFS_OK) {
5675 /* Free the stateid unless the server
5676 * informs us the stateid is unrecognized. */
5677 if (status != -NFS4ERR_BAD_STATEID)
5678 nfs41_free_stateid(server,
5681 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5690 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5692 int status = NFS_OK;
5694 if (test_bit(LK_STATE_IN_USE, &state->flags))
5695 status = nfs41_check_expired_locks(state);
5696 if (status != NFS_OK)
5697 status = nfs4_lock_expired(state, request);
5702 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5704 struct nfs4_state_owner *sp = state->owner;
5705 struct nfs_inode *nfsi = NFS_I(state->inode);
5706 unsigned char fl_flags = request->fl_flags;
5708 int status = -ENOLCK;
5710 if ((fl_flags & FL_POSIX) &&
5711 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5713 /* Is this a delegated open? */
5714 status = nfs4_set_lock_state(state, request);
5717 request->fl_flags |= FL_ACCESS;
5718 status = do_vfs_lock(request->fl_file, request);
5721 down_read(&nfsi->rwsem);
5722 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5723 /* Yes: cache locks! */
5724 /* ...but avoid races with delegation recall... */
5725 request->fl_flags = fl_flags & ~FL_SLEEP;
5726 status = do_vfs_lock(request->fl_file, request);
5729 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5730 up_read(&nfsi->rwsem);
5731 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5734 down_read(&nfsi->rwsem);
5735 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5736 status = -NFS4ERR_DELAY;
5739 /* Note: we always want to sleep here! */
5740 request->fl_flags = fl_flags | FL_SLEEP;
5741 if (do_vfs_lock(request->fl_file, request) < 0)
5742 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5743 "manager!\n", __func__);
5745 up_read(&nfsi->rwsem);
5747 request->fl_flags = fl_flags;
5751 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5753 struct nfs4_exception exception = {
5755 .inode = state->inode,
5760 err = _nfs4_proc_setlk(state, cmd, request);
5761 trace_nfs4_set_lock(request, state, cmd, err);
5762 if (err == -NFS4ERR_DENIED)
5764 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5766 } while (exception.retry);
5771 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5773 struct nfs_open_context *ctx;
5774 struct nfs4_state *state;
5775 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5778 /* verify open state */
5779 ctx = nfs_file_open_context(filp);
5782 if (request->fl_start < 0 || request->fl_end < 0)
5785 if (IS_GETLK(cmd)) {
5787 return nfs4_proc_getlk(state, F_GETLK, request);
5791 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5794 if (request->fl_type == F_UNLCK) {
5796 return nfs4_proc_unlck(state, cmd, request);
5803 * Don't rely on the VFS having checked the file open mode,
5804 * since it won't do this for flock() locks.
5806 switch (request->fl_type) {
5808 if (!(filp->f_mode & FMODE_READ))
5812 if (!(filp->f_mode & FMODE_WRITE))
5817 status = nfs4_proc_setlk(state, cmd, request);
5818 if ((status != -EAGAIN) || IS_SETLK(cmd))
5820 timeout = nfs4_set_lock_task_retry(timeout);
5821 status = -ERESTARTSYS;
5824 } while(status < 0);
5828 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5830 struct nfs_server *server = NFS_SERVER(state->inode);
5833 err = nfs4_set_lock_state(state, fl);
5836 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5837 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5840 struct nfs_release_lockowner_data {
5841 struct nfs4_lock_state *lsp;
5842 struct nfs_server *server;
5843 struct nfs_release_lockowner_args args;
5844 struct nfs_release_lockowner_res res;
5845 unsigned long timestamp;
5848 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5850 struct nfs_release_lockowner_data *data = calldata;
5851 nfs40_setup_sequence(data->server,
5852 &data->args.seq_args, &data->res.seq_res, task);
5853 data->timestamp = jiffies;
5856 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5858 struct nfs_release_lockowner_data *data = calldata;
5859 struct nfs_server *server = data->server;
5861 nfs40_sequence_done(task, &data->res.seq_res);
5863 switch (task->tk_status) {
5865 renew_lease(server, data->timestamp);
5867 case -NFS4ERR_STALE_CLIENTID:
5868 case -NFS4ERR_EXPIRED:
5869 case -NFS4ERR_LEASE_MOVED:
5870 case -NFS4ERR_DELAY:
5871 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5872 rpc_restart_call_prepare(task);
5876 static void nfs4_release_lockowner_release(void *calldata)
5878 struct nfs_release_lockowner_data *data = calldata;
5879 nfs4_free_lock_state(data->server, data->lsp);
5883 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5884 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5885 .rpc_call_done = nfs4_release_lockowner_done,
5886 .rpc_release = nfs4_release_lockowner_release,
5889 static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
5891 struct nfs_release_lockowner_data *data;
5892 struct rpc_message msg = {
5893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5896 if (server->nfs_client->cl_mvops->minor_version != 0)
5899 data = kmalloc(sizeof(*data), GFP_NOFS);
5903 data->server = server;
5904 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5905 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5906 data->args.lock_owner.s_dev = server->s_dev;
5908 msg.rpc_argp = &data->args;
5909 msg.rpc_resp = &data->res;
5910 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
5911 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5915 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5917 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5918 const void *buf, size_t buflen,
5919 int flags, int type)
5921 if (strcmp(key, "") != 0)
5924 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5927 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5928 void *buf, size_t buflen, int type)
5930 if (strcmp(key, "") != 0)
5933 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5936 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5937 size_t list_len, const char *name,
5938 size_t name_len, int type)
5940 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5942 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5945 if (list && len <= list_len)
5946 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5950 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5951 static inline int nfs4_server_supports_labels(struct nfs_server *server)
5953 return server->caps & NFS_CAP_SECURITY_LABEL;
5956 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5957 const void *buf, size_t buflen,
5958 int flags, int type)
5960 if (security_ismaclabel(key))
5961 return nfs4_set_security_label(dentry, buf, buflen);
5966 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5967 void *buf, size_t buflen, int type)
5969 if (security_ismaclabel(key))
5970 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5974 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5975 size_t list_len, const char *name,
5976 size_t name_len, int type)
5980 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5981 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5982 if (list && len <= list_len)
5983 security_inode_listsecurity(dentry->d_inode, list, len);
5988 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5989 .prefix = XATTR_SECURITY_PREFIX,
5990 .list = nfs4_xattr_list_nfs4_label,
5991 .get = nfs4_xattr_get_nfs4_label,
5992 .set = nfs4_xattr_set_nfs4_label,
5998 * nfs_fhget will use either the mounted_on_fileid or the fileid
6000 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6002 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6003 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6004 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6005 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6008 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6009 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6010 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6014 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6015 const struct qstr *name,
6016 struct nfs4_fs_locations *fs_locations,
6019 struct nfs_server *server = NFS_SERVER(dir);
6021 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6023 struct nfs4_fs_locations_arg args = {
6024 .dir_fh = NFS_FH(dir),
6029 struct nfs4_fs_locations_res res = {
6030 .fs_locations = fs_locations,
6032 struct rpc_message msg = {
6033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6039 dprintk("%s: start\n", __func__);
6041 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6042 * is not supported */
6043 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6044 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6046 bitmask[0] |= FATTR4_WORD0_FILEID;
6048 nfs_fattr_init(&fs_locations->fattr);
6049 fs_locations->server = server;
6050 fs_locations->nlocations = 0;
6051 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6052 dprintk("%s: returned status = %d\n", __func__, status);
6056 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6057 const struct qstr *name,
6058 struct nfs4_fs_locations *fs_locations,
6061 struct nfs4_exception exception = { };
6064 err = _nfs4_proc_fs_locations(client, dir, name,
6065 fs_locations, page);
6066 trace_nfs4_get_fs_locations(dir, name, err);
6067 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6069 } while (exception.retry);
6074 * This operation also signals the server that this client is
6075 * performing migration recovery. The server can stop returning
6076 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6077 * appended to this compound to identify the client ID which is
6078 * performing recovery.
6080 static int _nfs40_proc_get_locations(struct inode *inode,
6081 struct nfs4_fs_locations *locations,
6082 struct page *page, struct rpc_cred *cred)
6084 struct nfs_server *server = NFS_SERVER(inode);
6085 struct rpc_clnt *clnt = server->client;
6087 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6089 struct nfs4_fs_locations_arg args = {
6090 .clientid = server->nfs_client->cl_clientid,
6091 .fh = NFS_FH(inode),
6094 .migration = 1, /* skip LOOKUP */
6095 .renew = 1, /* append RENEW */
6097 struct nfs4_fs_locations_res res = {
6098 .fs_locations = locations,
6102 struct rpc_message msg = {
6103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6108 unsigned long now = jiffies;
6111 nfs_fattr_init(&locations->fattr);
6112 locations->server = server;
6113 locations->nlocations = 0;
6115 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6116 nfs4_set_sequence_privileged(&args.seq_args);
6117 status = nfs4_call_sync_sequence(clnt, server, &msg,
6118 &args.seq_args, &res.seq_res);
6122 renew_lease(server, now);
6126 #ifdef CONFIG_NFS_V4_1
6129 * This operation also signals the server that this client is
6130 * performing migration recovery. The server can stop asserting
6131 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6132 * performing this operation is identified in the SEQUENCE
6133 * operation in this compound.
6135 * When the client supports GETATTR(fs_locations_info), it can
6136 * be plumbed in here.
6138 static int _nfs41_proc_get_locations(struct inode *inode,
6139 struct nfs4_fs_locations *locations,
6140 struct page *page, struct rpc_cred *cred)
6142 struct nfs_server *server = NFS_SERVER(inode);
6143 struct rpc_clnt *clnt = server->client;
6145 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6147 struct nfs4_fs_locations_arg args = {
6148 .fh = NFS_FH(inode),
6151 .migration = 1, /* skip LOOKUP */
6153 struct nfs4_fs_locations_res res = {
6154 .fs_locations = locations,
6157 struct rpc_message msg = {
6158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6165 nfs_fattr_init(&locations->fattr);
6166 locations->server = server;
6167 locations->nlocations = 0;
6169 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6170 nfs4_set_sequence_privileged(&args.seq_args);
6171 status = nfs4_call_sync_sequence(clnt, server, &msg,
6172 &args.seq_args, &res.seq_res);
6173 if (status == NFS4_OK &&
6174 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6175 status = -NFS4ERR_LEASE_MOVED;
6179 #endif /* CONFIG_NFS_V4_1 */
6182 * nfs4_proc_get_locations - discover locations for a migrated FSID
6183 * @inode: inode on FSID that is migrating
6184 * @locations: result of query
6186 * @cred: credential to use for this operation
6188 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6189 * operation failed, or a negative errno if a local error occurred.
6191 * On success, "locations" is filled in, but if the server has
6192 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6195 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6196 * from this client that require migration recovery.
6198 int nfs4_proc_get_locations(struct inode *inode,
6199 struct nfs4_fs_locations *locations,
6200 struct page *page, struct rpc_cred *cred)
6202 struct nfs_server *server = NFS_SERVER(inode);
6203 struct nfs_client *clp = server->nfs_client;
6204 const struct nfs4_mig_recovery_ops *ops =
6205 clp->cl_mvops->mig_recovery_ops;
6206 struct nfs4_exception exception = { };
6209 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6210 (unsigned long long)server->fsid.major,
6211 (unsigned long long)server->fsid.minor,
6213 nfs_display_fhandle(NFS_FH(inode), __func__);
6216 status = ops->get_locations(inode, locations, page, cred);
6217 if (status != -NFS4ERR_DELAY)
6219 nfs4_handle_exception(server, status, &exception);
6220 } while (exception.retry);
6225 * This operation also signals the server that this client is
6226 * performing "lease moved" recovery. The server can stop
6227 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6228 * is appended to this compound to identify the client ID which is
6229 * performing recovery.
6231 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6233 struct nfs_server *server = NFS_SERVER(inode);
6234 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6235 struct rpc_clnt *clnt = server->client;
6236 struct nfs4_fsid_present_arg args = {
6237 .fh = NFS_FH(inode),
6238 .clientid = clp->cl_clientid,
6239 .renew = 1, /* append RENEW */
6241 struct nfs4_fsid_present_res res = {
6244 struct rpc_message msg = {
6245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6250 unsigned long now = jiffies;
6253 res.fh = nfs_alloc_fhandle();
6257 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6258 nfs4_set_sequence_privileged(&args.seq_args);
6259 status = nfs4_call_sync_sequence(clnt, server, &msg,
6260 &args.seq_args, &res.seq_res);
6261 nfs_free_fhandle(res.fh);
6265 do_renew_lease(clp, now);
6269 #ifdef CONFIG_NFS_V4_1
6272 * This operation also signals the server that this client is
6273 * performing "lease moved" recovery. The server can stop asserting
6274 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6275 * this operation is identified in the SEQUENCE operation in this
6278 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6280 struct nfs_server *server = NFS_SERVER(inode);
6281 struct rpc_clnt *clnt = server->client;
6282 struct nfs4_fsid_present_arg args = {
6283 .fh = NFS_FH(inode),
6285 struct nfs4_fsid_present_res res = {
6287 struct rpc_message msg = {
6288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6295 res.fh = nfs_alloc_fhandle();
6299 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6300 nfs4_set_sequence_privileged(&args.seq_args);
6301 status = nfs4_call_sync_sequence(clnt, server, &msg,
6302 &args.seq_args, &res.seq_res);
6303 nfs_free_fhandle(res.fh);
6304 if (status == NFS4_OK &&
6305 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6306 status = -NFS4ERR_LEASE_MOVED;
6310 #endif /* CONFIG_NFS_V4_1 */
6313 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6314 * @inode: inode on FSID to check
6315 * @cred: credential to use for this operation
6317 * Server indicates whether the FSID is present, moved, or not
6318 * recognized. This operation is necessary to clear a LEASE_MOVED
6319 * condition for this client ID.
6321 * Returns NFS4_OK if the FSID is present on this server,
6322 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6323 * NFS4ERR code if some error occurred on the server, or a
6324 * negative errno if a local failure occurred.
6326 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6328 struct nfs_server *server = NFS_SERVER(inode);
6329 struct nfs_client *clp = server->nfs_client;
6330 const struct nfs4_mig_recovery_ops *ops =
6331 clp->cl_mvops->mig_recovery_ops;
6332 struct nfs4_exception exception = { };
6335 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6336 (unsigned long long)server->fsid.major,
6337 (unsigned long long)server->fsid.minor,
6339 nfs_display_fhandle(NFS_FH(inode), __func__);
6342 status = ops->fsid_present(inode, cred);
6343 if (status != -NFS4ERR_DELAY)
6345 nfs4_handle_exception(server, status, &exception);
6346 } while (exception.retry);
6351 * If 'use_integrity' is true and the state managment nfs_client
6352 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6353 * and the machine credential as per RFC3530bis and RFC5661 Security
6354 * Considerations sections. Otherwise, just use the user cred with the
6355 * filesystem's rpc_client.
6357 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6360 struct nfs4_secinfo_arg args = {
6361 .dir_fh = NFS_FH(dir),
6364 struct nfs4_secinfo_res res = {
6367 struct rpc_message msg = {
6368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6372 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6373 struct rpc_cred *cred = NULL;
6375 if (use_integrity) {
6376 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6377 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6378 msg.rpc_cred = cred;
6381 dprintk("NFS call secinfo %s\n", name->name);
6383 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6384 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6386 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6388 dprintk("NFS reply secinfo: %d\n", status);
6396 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6397 struct nfs4_secinfo_flavors *flavors)
6399 struct nfs4_exception exception = { };
6402 err = -NFS4ERR_WRONGSEC;
6404 /* try to use integrity protection with machine cred */
6405 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6406 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6409 * if unable to use integrity protection, or SECINFO with
6410 * integrity protection returns NFS4ERR_WRONGSEC (which is
6411 * disallowed by spec, but exists in deployed servers) use
6412 * the current filesystem's rpc_client and the user cred.
6414 if (err == -NFS4ERR_WRONGSEC)
6415 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6417 trace_nfs4_secinfo(dir, name, err);
6418 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6420 } while (exception.retry);
6424 #ifdef CONFIG_NFS_V4_1
6426 * Check the exchange flags returned by the server for invalid flags, having
6427 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6430 static int nfs4_check_cl_exchange_flags(u32 flags)
6432 if (flags & ~EXCHGID4_FLAG_MASK_R)
6434 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6435 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6437 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6441 return -NFS4ERR_INVAL;
6445 nfs41_same_server_scope(struct nfs41_server_scope *a,
6446 struct nfs41_server_scope *b)
6448 if (a->server_scope_sz == b->server_scope_sz &&
6449 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6456 * nfs4_proc_bind_conn_to_session()
6458 * The 4.1 client currently uses the same TCP connection for the
6459 * fore and backchannel.
6461 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6464 struct nfs41_bind_conn_to_session_res res;
6465 struct rpc_message msg = {
6467 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6473 dprintk("--> %s\n", __func__);
6475 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6476 if (unlikely(res.session == NULL)) {
6481 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6482 trace_nfs4_bind_conn_to_session(clp, status);
6484 if (memcmp(res.session->sess_id.data,
6485 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6486 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6490 if (res.dir != NFS4_CDFS4_BOTH) {
6491 dprintk("NFS: %s: Unexpected direction from server\n",
6496 if (res.use_conn_in_rdma_mode) {
6497 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6506 dprintk("<-- %s status= %d\n", __func__, status);
6511 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6512 * and operations we'd like to see to enable certain features in the allow map
6514 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6515 .how = SP4_MACH_CRED,
6516 .enforce.u.words = {
6517 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6518 1 << (OP_EXCHANGE_ID - 32) |
6519 1 << (OP_CREATE_SESSION - 32) |
6520 1 << (OP_DESTROY_SESSION - 32) |
6521 1 << (OP_DESTROY_CLIENTID - 32)
6524 [0] = 1 << (OP_CLOSE) |
6527 [1] = 1 << (OP_SECINFO - 32) |
6528 1 << (OP_SECINFO_NO_NAME - 32) |
6529 1 << (OP_TEST_STATEID - 32) |
6530 1 << (OP_FREE_STATEID - 32) |
6531 1 << (OP_WRITE - 32)
6536 * Select the state protection mode for client `clp' given the server results
6537 * from exchange_id in `sp'.
6539 * Returns 0 on success, negative errno otherwise.
6541 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6542 struct nfs41_state_protection *sp)
6544 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6545 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6546 1 << (OP_EXCHANGE_ID - 32) |
6547 1 << (OP_CREATE_SESSION - 32) |
6548 1 << (OP_DESTROY_SESSION - 32) |
6549 1 << (OP_DESTROY_CLIENTID - 32)
6553 if (sp->how == SP4_MACH_CRED) {
6554 /* Print state protect result */
6555 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6556 for (i = 0; i <= LAST_NFS4_OP; i++) {
6557 if (test_bit(i, sp->enforce.u.longs))
6558 dfprintk(MOUNT, " enforce op %d\n", i);
6559 if (test_bit(i, sp->allow.u.longs))
6560 dfprintk(MOUNT, " allow op %d\n", i);
6563 /* make sure nothing is on enforce list that isn't supported */
6564 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6565 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6566 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6572 * Minimal mode - state operations are allowed to use machine
6573 * credential. Note this already happens by default, so the
6574 * client doesn't have to do anything more than the negotiation.
6576 * NOTE: we don't care if EXCHANGE_ID is in the list -
6577 * we're already using the machine cred for exchange_id
6578 * and will never use a different cred.
6580 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6581 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6582 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6583 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6584 dfprintk(MOUNT, "sp4_mach_cred:\n");
6585 dfprintk(MOUNT, " minimal mode enabled\n");
6586 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6588 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6592 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6593 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6594 dfprintk(MOUNT, " cleanup mode enabled\n");
6595 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6598 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6599 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6600 dfprintk(MOUNT, " secinfo mode enabled\n");
6601 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6604 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6605 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6606 dfprintk(MOUNT, " stateid mode enabled\n");
6607 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6610 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6611 dfprintk(MOUNT, " write mode enabled\n");
6612 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6615 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6616 dfprintk(MOUNT, " commit mode enabled\n");
6617 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6625 * _nfs4_proc_exchange_id()
6627 * Wrapper for EXCHANGE_ID operation.
6629 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6632 nfs4_verifier verifier;
6633 struct nfs41_exchange_id_args args = {
6634 .verifier = &verifier,
6636 #ifdef CONFIG_NFS_V4_1_MIGRATION
6637 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6638 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6639 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6641 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6642 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6645 struct nfs41_exchange_id_res res = {
6649 struct rpc_message msg = {
6650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6656 nfs4_init_boot_verifier(clp, &verifier);
6657 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6659 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6660 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6661 args.id_len, args.id);
6663 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6665 if (unlikely(res.server_owner == NULL)) {
6670 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6672 if (unlikely(res.server_scope == NULL)) {
6674 goto out_server_owner;
6677 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6678 if (unlikely(res.impl_id == NULL)) {
6680 goto out_server_scope;
6685 args.state_protect.how = SP4_NONE;
6689 args.state_protect = nfs4_sp4_mach_cred_request;
6696 goto out_server_scope;
6699 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6700 trace_nfs4_exchange_id(clp, status);
6702 status = nfs4_check_cl_exchange_flags(res.flags);
6705 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6708 clp->cl_clientid = res.clientid;
6709 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6710 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6711 clp->cl_seqid = res.seqid;
6713 kfree(clp->cl_serverowner);
6714 clp->cl_serverowner = res.server_owner;
6715 res.server_owner = NULL;
6717 /* use the most recent implementation id */
6718 kfree(clp->cl_implid);
6719 clp->cl_implid = res.impl_id;
6721 if (clp->cl_serverscope != NULL &&
6722 !nfs41_same_server_scope(clp->cl_serverscope,
6723 res.server_scope)) {
6724 dprintk("%s: server_scope mismatch detected\n",
6726 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6727 kfree(clp->cl_serverscope);
6728 clp->cl_serverscope = NULL;
6731 if (clp->cl_serverscope == NULL) {
6732 clp->cl_serverscope = res.server_scope;
6739 kfree(res.server_owner);
6741 kfree(res.server_scope);
6743 if (clp->cl_implid != NULL)
6744 dprintk("NFS reply exchange_id: Server Implementation ID: "
6745 "domain: %s, name: %s, date: %llu,%u\n",
6746 clp->cl_implid->domain, clp->cl_implid->name,
6747 clp->cl_implid->date.seconds,
6748 clp->cl_implid->date.nseconds);
6749 dprintk("NFS reply exchange_id: %d\n", status);
6754 * nfs4_proc_exchange_id()
6756 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6758 * Since the clientid has expired, all compounds using sessions
6759 * associated with the stale clientid will be returning
6760 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6761 * be in some phase of session reset.
6763 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6765 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6767 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6770 /* try SP4_MACH_CRED if krb5i/p */
6771 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6772 authflavor == RPC_AUTH_GSS_KRB5P) {
6773 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6779 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6782 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6783 struct rpc_cred *cred)
6785 struct rpc_message msg = {
6786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6792 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6793 trace_nfs4_destroy_clientid(clp, status);
6795 dprintk("NFS: Got error %d from the server %s on "
6796 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6800 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6801 struct rpc_cred *cred)
6806 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6807 ret = _nfs4_proc_destroy_clientid(clp, cred);
6809 case -NFS4ERR_DELAY:
6810 case -NFS4ERR_CLIENTID_BUSY:
6820 int nfs4_destroy_clientid(struct nfs_client *clp)
6822 struct rpc_cred *cred;
6825 if (clp->cl_mvops->minor_version < 1)
6827 if (clp->cl_exchange_flags == 0)
6829 if (clp->cl_preserve_clid)
6831 cred = nfs4_get_clid_cred(clp);
6832 ret = nfs4_proc_destroy_clientid(clp, cred);
6837 case -NFS4ERR_STALE_CLIENTID:
6838 clp->cl_exchange_flags = 0;
6844 struct nfs4_get_lease_time_data {
6845 struct nfs4_get_lease_time_args *args;
6846 struct nfs4_get_lease_time_res *res;
6847 struct nfs_client *clp;
6850 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6853 struct nfs4_get_lease_time_data *data =
6854 (struct nfs4_get_lease_time_data *)calldata;
6856 dprintk("--> %s\n", __func__);
6857 /* just setup sequence, do not trigger session recovery
6858 since we're invoked within one */
6859 nfs41_setup_sequence(data->clp->cl_session,
6860 &data->args->la_seq_args,
6861 &data->res->lr_seq_res,
6863 dprintk("<-- %s\n", __func__);
6867 * Called from nfs4_state_manager thread for session setup, so don't recover
6868 * from sequence operation or clientid errors.
6870 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6872 struct nfs4_get_lease_time_data *data =
6873 (struct nfs4_get_lease_time_data *)calldata;
6875 dprintk("--> %s\n", __func__);
6876 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6878 switch (task->tk_status) {
6879 case -NFS4ERR_DELAY:
6880 case -NFS4ERR_GRACE:
6881 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6882 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6883 task->tk_status = 0;
6885 case -NFS4ERR_RETRY_UNCACHED_REP:
6886 rpc_restart_call_prepare(task);
6889 dprintk("<-- %s\n", __func__);
6892 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
6893 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6894 .rpc_call_done = nfs4_get_lease_time_done,
6897 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6899 struct rpc_task *task;
6900 struct nfs4_get_lease_time_args args;
6901 struct nfs4_get_lease_time_res res = {
6902 .lr_fsinfo = fsinfo,
6904 struct nfs4_get_lease_time_data data = {
6909 struct rpc_message msg = {
6910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6914 struct rpc_task_setup task_setup = {
6915 .rpc_client = clp->cl_rpcclient,
6916 .rpc_message = &msg,
6917 .callback_ops = &nfs4_get_lease_time_ops,
6918 .callback_data = &data,
6919 .flags = RPC_TASK_TIMEOUT,
6923 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
6924 nfs4_set_sequence_privileged(&args.la_seq_args);
6925 dprintk("--> %s\n", __func__);
6926 task = rpc_run_task(&task_setup);
6929 status = PTR_ERR(task);
6931 status = task->tk_status;
6934 dprintk("<-- %s return %d\n", __func__, status);
6940 * Initialize the values to be used by the client in CREATE_SESSION
6941 * If nfs4_init_session set the fore channel request and response sizes,
6944 * Set the back channel max_resp_sz_cached to zero to force the client to
6945 * always set csa_cachethis to FALSE because the current implementation
6946 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6948 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6950 unsigned int max_rqst_sz, max_resp_sz;
6952 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6953 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6955 /* Fore channel attributes */
6956 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6957 args->fc_attrs.max_resp_sz = max_resp_sz;
6958 args->fc_attrs.max_ops = NFS4_MAX_OPS;
6959 args->fc_attrs.max_reqs = max_session_slots;
6961 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6962 "max_ops=%u max_reqs=%u\n",
6964 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
6965 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
6967 /* Back channel attributes */
6968 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6969 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6970 args->bc_attrs.max_resp_sz_cached = 0;
6971 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6972 args->bc_attrs.max_reqs = 1;
6974 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6975 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6977 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6978 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6979 args->bc_attrs.max_reqs);
6982 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6984 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6985 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6987 if (rcvd->max_resp_sz > sent->max_resp_sz)
6990 * Our requested max_ops is the minimum we need; we're not
6991 * prepared to break up compounds into smaller pieces than that.
6992 * So, no point even trying to continue if the server won't
6995 if (rcvd->max_ops < sent->max_ops)
6997 if (rcvd->max_reqs == 0)
6999 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7000 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7004 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7006 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7007 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
7009 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7011 if (rcvd->max_resp_sz < sent->max_resp_sz)
7013 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7015 /* These would render the backchannel useless: */
7016 if (rcvd->max_ops != sent->max_ops)
7018 if (rcvd->max_reqs != sent->max_reqs)
7023 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7024 struct nfs4_session *session)
7028 ret = nfs4_verify_fore_channel_attrs(args, session);
7031 return nfs4_verify_back_channel_attrs(args, session);
7034 static int _nfs4_proc_create_session(struct nfs_client *clp,
7035 struct rpc_cred *cred)
7037 struct nfs4_session *session = clp->cl_session;
7038 struct nfs41_create_session_args args = {
7040 .cb_program = NFS4_CALLBACK,
7042 struct nfs41_create_session_res res = {
7045 struct rpc_message msg = {
7046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7053 nfs4_init_channel_attrs(&args);
7054 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7056 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7057 trace_nfs4_create_session(clp, status);
7060 /* Verify the session's negotiated channel_attrs values */
7061 status = nfs4_verify_channel_attrs(&args, session);
7062 /* Increment the clientid slot sequence id */
7070 * Issues a CREATE_SESSION operation to the server.
7071 * It is the responsibility of the caller to verify the session is
7072 * expired before calling this routine.
7074 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7078 struct nfs4_session *session = clp->cl_session;
7080 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7082 status = _nfs4_proc_create_session(clp, cred);
7086 /* Init or reset the session slot tables */
7087 status = nfs4_setup_session_slot_tables(session);
7088 dprintk("slot table setup returned %d\n", status);
7092 ptr = (unsigned *)&session->sess_id.data[0];
7093 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7094 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7096 dprintk("<-- %s\n", __func__);
7101 * Issue the over-the-wire RPC DESTROY_SESSION.
7102 * The caller must serialize access to this routine.
7104 int nfs4_proc_destroy_session(struct nfs4_session *session,
7105 struct rpc_cred *cred)
7107 struct rpc_message msg = {
7108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7109 .rpc_argp = session,
7114 dprintk("--> nfs4_proc_destroy_session\n");
7116 /* session is still being setup */
7117 if (session->clp->cl_cons_state != NFS_CS_READY)
7120 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7121 trace_nfs4_destroy_session(session->clp, status);
7124 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7125 "Session has been destroyed regardless...\n", status);
7127 dprintk("<-- nfs4_proc_destroy_session\n");
7132 * Renew the cl_session lease.
7134 struct nfs4_sequence_data {
7135 struct nfs_client *clp;
7136 struct nfs4_sequence_args args;
7137 struct nfs4_sequence_res res;
7140 static void nfs41_sequence_release(void *data)
7142 struct nfs4_sequence_data *calldata = data;
7143 struct nfs_client *clp = calldata->clp;
7145 if (atomic_read(&clp->cl_count) > 1)
7146 nfs4_schedule_state_renewal(clp);
7147 nfs_put_client(clp);
7151 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7153 switch(task->tk_status) {
7154 case -NFS4ERR_DELAY:
7155 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7158 nfs4_schedule_lease_recovery(clp);
7163 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7165 struct nfs4_sequence_data *calldata = data;
7166 struct nfs_client *clp = calldata->clp;
7168 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7171 trace_nfs4_sequence(clp, task->tk_status);
7172 if (task->tk_status < 0) {
7173 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7174 if (atomic_read(&clp->cl_count) == 1)
7177 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7178 rpc_restart_call_prepare(task);
7182 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7184 dprintk("<-- %s\n", __func__);
7187 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7189 struct nfs4_sequence_data *calldata = data;
7190 struct nfs_client *clp = calldata->clp;
7191 struct nfs4_sequence_args *args;
7192 struct nfs4_sequence_res *res;
7194 args = task->tk_msg.rpc_argp;
7195 res = task->tk_msg.rpc_resp;
7197 nfs41_setup_sequence(clp->cl_session, args, res, task);
7200 static const struct rpc_call_ops nfs41_sequence_ops = {
7201 .rpc_call_done = nfs41_sequence_call_done,
7202 .rpc_call_prepare = nfs41_sequence_prepare,
7203 .rpc_release = nfs41_sequence_release,
7206 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7207 struct rpc_cred *cred,
7210 struct nfs4_sequence_data *calldata;
7211 struct rpc_message msg = {
7212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7215 struct rpc_task_setup task_setup_data = {
7216 .rpc_client = clp->cl_rpcclient,
7217 .rpc_message = &msg,
7218 .callback_ops = &nfs41_sequence_ops,
7219 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7222 if (!atomic_inc_not_zero(&clp->cl_count))
7223 return ERR_PTR(-EIO);
7224 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7225 if (calldata == NULL) {
7226 nfs_put_client(clp);
7227 return ERR_PTR(-ENOMEM);
7229 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7231 nfs4_set_sequence_privileged(&calldata->args);
7232 msg.rpc_argp = &calldata->args;
7233 msg.rpc_resp = &calldata->res;
7234 calldata->clp = clp;
7235 task_setup_data.callback_data = calldata;
7237 return rpc_run_task(&task_setup_data);
7240 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7242 struct rpc_task *task;
7245 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7247 task = _nfs41_proc_sequence(clp, cred, false);
7249 ret = PTR_ERR(task);
7251 rpc_put_task_async(task);
7252 dprintk("<-- %s status=%d\n", __func__, ret);
7256 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7258 struct rpc_task *task;
7261 task = _nfs41_proc_sequence(clp, cred, true);
7263 ret = PTR_ERR(task);
7266 ret = rpc_wait_for_completion_task(task);
7268 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7270 if (task->tk_status == 0)
7271 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7272 ret = task->tk_status;
7276 dprintk("<-- %s status=%d\n", __func__, ret);
7280 struct nfs4_reclaim_complete_data {
7281 struct nfs_client *clp;
7282 struct nfs41_reclaim_complete_args arg;
7283 struct nfs41_reclaim_complete_res res;
7286 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7288 struct nfs4_reclaim_complete_data *calldata = data;
7290 nfs41_setup_sequence(calldata->clp->cl_session,
7291 &calldata->arg.seq_args,
7292 &calldata->res.seq_res,
7296 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7298 switch(task->tk_status) {
7300 case -NFS4ERR_COMPLETE_ALREADY:
7301 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7303 case -NFS4ERR_DELAY:
7304 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7306 case -NFS4ERR_RETRY_UNCACHED_REP:
7309 nfs4_schedule_lease_recovery(clp);
7314 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7316 struct nfs4_reclaim_complete_data *calldata = data;
7317 struct nfs_client *clp = calldata->clp;
7318 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7320 dprintk("--> %s\n", __func__);
7321 if (!nfs41_sequence_done(task, res))
7324 trace_nfs4_reclaim_complete(clp, task->tk_status);
7325 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7326 rpc_restart_call_prepare(task);
7329 dprintk("<-- %s\n", __func__);
7332 static void nfs4_free_reclaim_complete_data(void *data)
7334 struct nfs4_reclaim_complete_data *calldata = data;
7339 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7340 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7341 .rpc_call_done = nfs4_reclaim_complete_done,
7342 .rpc_release = nfs4_free_reclaim_complete_data,
7346 * Issue a global reclaim complete.
7348 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7349 struct rpc_cred *cred)
7351 struct nfs4_reclaim_complete_data *calldata;
7352 struct rpc_task *task;
7353 struct rpc_message msg = {
7354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7357 struct rpc_task_setup task_setup_data = {
7358 .rpc_client = clp->cl_rpcclient,
7359 .rpc_message = &msg,
7360 .callback_ops = &nfs4_reclaim_complete_call_ops,
7361 .flags = RPC_TASK_ASYNC,
7363 int status = -ENOMEM;
7365 dprintk("--> %s\n", __func__);
7366 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7367 if (calldata == NULL)
7369 calldata->clp = clp;
7370 calldata->arg.one_fs = 0;
7372 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7373 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7374 msg.rpc_argp = &calldata->arg;
7375 msg.rpc_resp = &calldata->res;
7376 task_setup_data.callback_data = calldata;
7377 task = rpc_run_task(&task_setup_data);
7379 status = PTR_ERR(task);
7382 status = nfs4_wait_for_completion_rpc_task(task);
7384 status = task->tk_status;
7388 dprintk("<-- %s status=%d\n", __func__, status);
7393 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7395 struct nfs4_layoutget *lgp = calldata;
7396 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7397 struct nfs4_session *session = nfs4_get_session(server);
7399 dprintk("--> %s\n", __func__);
7400 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7401 * right now covering the LAYOUTGET we are about to send.
7402 * However, that is not so catastrophic, and there seems
7403 * to be no way to prevent it completely.
7405 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7406 &lgp->res.seq_res, task))
7408 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7409 NFS_I(lgp->args.inode)->layout,
7410 lgp->args.ctx->state)) {
7411 rpc_exit(task, NFS4_OK);
7415 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7417 struct nfs4_layoutget *lgp = calldata;
7418 struct inode *inode = lgp->args.inode;
7419 struct nfs_server *server = NFS_SERVER(inode);
7420 struct pnfs_layout_hdr *lo;
7421 struct nfs4_state *state = NULL;
7422 unsigned long timeo, now, giveup;
7424 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7426 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7429 switch (task->tk_status) {
7433 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7434 * (or clients) writing to the same RAID stripe
7436 case -NFS4ERR_LAYOUTTRYLATER:
7438 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7439 * existing layout before getting a new one).
7441 case -NFS4ERR_RECALLCONFLICT:
7442 timeo = rpc_get_timeout(task->tk_client);
7443 giveup = lgp->args.timestamp + timeo;
7445 if (time_after(giveup, now)) {
7446 unsigned long delay;
7449 * - Not less then NFS4_POLL_RETRY_MIN.
7450 * - One last time a jiffie before we give up
7451 * - exponential backoff (time_now minus start_attempt)
7453 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7454 min((giveup - now - 1),
7455 now - lgp->args.timestamp));
7457 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7459 rpc_delay(task, delay);
7460 task->tk_status = 0;
7461 rpc_restart_call_prepare(task);
7462 goto out; /* Do not call nfs4_async_handle_error() */
7465 case -NFS4ERR_EXPIRED:
7466 case -NFS4ERR_BAD_STATEID:
7467 spin_lock(&inode->i_lock);
7468 lo = NFS_I(inode)->layout;
7469 if (!lo || list_empty(&lo->plh_segs)) {
7470 spin_unlock(&inode->i_lock);
7471 /* If the open stateid was bad, then recover it. */
7472 state = lgp->args.ctx->state;
7476 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7477 spin_unlock(&inode->i_lock);
7478 /* Mark the bad layout state as invalid, then
7479 * retry using the open stateid. */
7480 pnfs_free_lseg_list(&head);
7483 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7484 rpc_restart_call_prepare(task);
7486 dprintk("<-- %s\n", __func__);
7489 static size_t max_response_pages(struct nfs_server *server)
7491 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7492 return nfs_page_array_len(0, max_resp_sz);
7495 static void nfs4_free_pages(struct page **pages, size_t size)
7502 for (i = 0; i < size; i++) {
7505 __free_page(pages[i]);
7510 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7512 struct page **pages;
7515 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7517 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7521 for (i = 0; i < size; i++) {
7522 pages[i] = alloc_page(gfp_flags);
7524 dprintk("%s: failed to allocate page\n", __func__);
7525 nfs4_free_pages(pages, size);
7533 static void nfs4_layoutget_release(void *calldata)
7535 struct nfs4_layoutget *lgp = calldata;
7536 struct inode *inode = lgp->args.inode;
7537 struct nfs_server *server = NFS_SERVER(inode);
7538 size_t max_pages = max_response_pages(server);
7540 dprintk("--> %s\n", __func__);
7541 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7542 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7543 put_nfs_open_context(lgp->args.ctx);
7545 dprintk("<-- %s\n", __func__);
7548 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7549 .rpc_call_prepare = nfs4_layoutget_prepare,
7550 .rpc_call_done = nfs4_layoutget_done,
7551 .rpc_release = nfs4_layoutget_release,
7554 struct pnfs_layout_segment *
7555 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7557 struct inode *inode = lgp->args.inode;
7558 struct nfs_server *server = NFS_SERVER(inode);
7559 size_t max_pages = max_response_pages(server);
7560 struct rpc_task *task;
7561 struct rpc_message msg = {
7562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7563 .rpc_argp = &lgp->args,
7564 .rpc_resp = &lgp->res,
7565 .rpc_cred = lgp->cred,
7567 struct rpc_task_setup task_setup_data = {
7568 .rpc_client = server->client,
7569 .rpc_message = &msg,
7570 .callback_ops = &nfs4_layoutget_call_ops,
7571 .callback_data = lgp,
7572 .flags = RPC_TASK_ASYNC,
7574 struct pnfs_layout_segment *lseg = NULL;
7577 dprintk("--> %s\n", __func__);
7579 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7580 if (!lgp->args.layout.pages) {
7581 nfs4_layoutget_release(lgp);
7582 return ERR_PTR(-ENOMEM);
7584 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7585 lgp->args.timestamp = jiffies;
7587 lgp->res.layoutp = &lgp->args.layout;
7588 lgp->res.seq_res.sr_slot = NULL;
7589 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7591 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7592 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7594 task = rpc_run_task(&task_setup_data);
7596 return ERR_CAST(task);
7597 status = nfs4_wait_for_completion_rpc_task(task);
7599 status = task->tk_status;
7600 trace_nfs4_layoutget(lgp->args.ctx,
7604 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7605 if (status == 0 && lgp->res.layoutp->len)
7606 lseg = pnfs_layout_process(lgp);
7608 dprintk("<-- %s status=%d\n", __func__, status);
7610 return ERR_PTR(status);
7615 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7617 struct nfs4_layoutreturn *lrp = calldata;
7619 dprintk("--> %s\n", __func__);
7620 nfs41_setup_sequence(lrp->clp->cl_session,
7621 &lrp->args.seq_args,
7626 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7628 struct nfs4_layoutreturn *lrp = calldata;
7629 struct nfs_server *server;
7631 dprintk("--> %s\n", __func__);
7633 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7636 server = NFS_SERVER(lrp->args.inode);
7637 switch (task->tk_status) {
7639 task->tk_status = 0;
7642 case -NFS4ERR_DELAY:
7643 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7645 rpc_restart_call_prepare(task);
7648 dprintk("<-- %s\n", __func__);
7651 static void nfs4_layoutreturn_release(void *calldata)
7653 struct nfs4_layoutreturn *lrp = calldata;
7654 struct pnfs_layout_hdr *lo = lrp->args.layout;
7656 dprintk("--> %s\n", __func__);
7657 spin_lock(&lo->plh_inode->i_lock);
7658 if (lrp->res.lrs_present)
7659 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7660 lo->plh_block_lgets--;
7661 spin_unlock(&lo->plh_inode->i_lock);
7662 pnfs_put_layout_hdr(lrp->args.layout);
7664 dprintk("<-- %s\n", __func__);
7667 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7668 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7669 .rpc_call_done = nfs4_layoutreturn_done,
7670 .rpc_release = nfs4_layoutreturn_release,
7673 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7675 struct rpc_task *task;
7676 struct rpc_message msg = {
7677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7678 .rpc_argp = &lrp->args,
7679 .rpc_resp = &lrp->res,
7680 .rpc_cred = lrp->cred,
7682 struct rpc_task_setup task_setup_data = {
7683 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7684 .rpc_message = &msg,
7685 .callback_ops = &nfs4_layoutreturn_call_ops,
7686 .callback_data = lrp,
7690 dprintk("--> %s\n", __func__);
7691 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7692 task = rpc_run_task(&task_setup_data);
7694 return PTR_ERR(task);
7695 status = task->tk_status;
7696 trace_nfs4_layoutreturn(lrp->args.inode, status);
7697 dprintk("<-- %s status=%d\n", __func__, status);
7703 * Retrieve the list of Data Server devices from the MDS.
7705 static int _nfs4_getdevicelist(struct nfs_server *server,
7706 const struct nfs_fh *fh,
7707 struct pnfs_devicelist *devlist)
7709 struct nfs4_getdevicelist_args args = {
7711 .layoutclass = server->pnfs_curr_ld->id,
7713 struct nfs4_getdevicelist_res res = {
7716 struct rpc_message msg = {
7717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7723 dprintk("--> %s\n", __func__);
7724 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7726 dprintk("<-- %s status=%d\n", __func__, status);
7730 int nfs4_proc_getdevicelist(struct nfs_server *server,
7731 const struct nfs_fh *fh,
7732 struct pnfs_devicelist *devlist)
7734 struct nfs4_exception exception = { };
7738 err = nfs4_handle_exception(server,
7739 _nfs4_getdevicelist(server, fh, devlist),
7741 } while (exception.retry);
7743 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7744 err, devlist->num_devs);
7748 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7751 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7752 struct pnfs_device *pdev,
7753 struct rpc_cred *cred)
7755 struct nfs4_getdeviceinfo_args args = {
7758 struct nfs4_getdeviceinfo_res res = {
7761 struct rpc_message msg = {
7762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7769 dprintk("--> %s\n", __func__);
7770 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7771 dprintk("<-- %s status=%d\n", __func__, status);
7776 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7777 struct pnfs_device *pdev,
7778 struct rpc_cred *cred)
7780 struct nfs4_exception exception = { };
7784 err = nfs4_handle_exception(server,
7785 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7787 } while (exception.retry);
7790 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7792 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7794 struct nfs4_layoutcommit_data *data = calldata;
7795 struct nfs_server *server = NFS_SERVER(data->args.inode);
7796 struct nfs4_session *session = nfs4_get_session(server);
7798 nfs41_setup_sequence(session,
7799 &data->args.seq_args,
7805 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7807 struct nfs4_layoutcommit_data *data = calldata;
7808 struct nfs_server *server = NFS_SERVER(data->args.inode);
7810 if (!nfs41_sequence_done(task, &data->res.seq_res))
7813 switch (task->tk_status) { /* Just ignore these failures */
7814 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7815 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7816 case -NFS4ERR_BADLAYOUT: /* no layout */
7817 case -NFS4ERR_GRACE: /* loca_recalim always false */
7818 task->tk_status = 0;
7822 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7823 rpc_restart_call_prepare(task);
7829 static void nfs4_layoutcommit_release(void *calldata)
7831 struct nfs4_layoutcommit_data *data = calldata;
7833 pnfs_cleanup_layoutcommit(data);
7834 nfs_post_op_update_inode_force_wcc(data->args.inode,
7836 put_rpccred(data->cred);
7840 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7841 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7842 .rpc_call_done = nfs4_layoutcommit_done,
7843 .rpc_release = nfs4_layoutcommit_release,
7847 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7849 struct rpc_message msg = {
7850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7851 .rpc_argp = &data->args,
7852 .rpc_resp = &data->res,
7853 .rpc_cred = data->cred,
7855 struct rpc_task_setup task_setup_data = {
7856 .task = &data->task,
7857 .rpc_client = NFS_CLIENT(data->args.inode),
7858 .rpc_message = &msg,
7859 .callback_ops = &nfs4_layoutcommit_ops,
7860 .callback_data = data,
7861 .flags = RPC_TASK_ASYNC,
7863 struct rpc_task *task;
7866 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7867 "lbw: %llu inode %lu\n",
7868 data->task.tk_pid, sync,
7869 data->args.lastbytewritten,
7870 data->args.inode->i_ino);
7872 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7873 task = rpc_run_task(&task_setup_data);
7875 return PTR_ERR(task);
7878 status = nfs4_wait_for_completion_rpc_task(task);
7881 status = task->tk_status;
7882 trace_nfs4_layoutcommit(data->args.inode, status);
7884 dprintk("%s: status %d\n", __func__, status);
7890 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7891 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7894 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7895 struct nfs_fsinfo *info,
7896 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7898 struct nfs41_secinfo_no_name_args args = {
7899 .style = SECINFO_STYLE_CURRENT_FH,
7901 struct nfs4_secinfo_res res = {
7904 struct rpc_message msg = {
7905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7909 struct rpc_clnt *clnt = server->client;
7910 struct rpc_cred *cred = NULL;
7913 if (use_integrity) {
7914 clnt = server->nfs_client->cl_rpcclient;
7915 cred = nfs4_get_clid_cred(server->nfs_client);
7916 msg.rpc_cred = cred;
7919 dprintk("--> %s\n", __func__);
7920 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7922 dprintk("<-- %s status=%d\n", __func__, status);
7931 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7932 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7934 struct nfs4_exception exception = { };
7937 /* first try using integrity protection */
7938 err = -NFS4ERR_WRONGSEC;
7940 /* try to use integrity protection with machine cred */
7941 if (_nfs4_is_integrity_protected(server->nfs_client))
7942 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7946 * if unable to use integrity protection, or SECINFO with
7947 * integrity protection returns NFS4ERR_WRONGSEC (which is
7948 * disallowed by spec, but exists in deployed servers) use
7949 * the current filesystem's rpc_client and the user cred.
7951 if (err == -NFS4ERR_WRONGSEC)
7952 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7957 case -NFS4ERR_WRONGSEC:
7961 err = nfs4_handle_exception(server, err, &exception);
7963 } while (exception.retry);
7969 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7970 struct nfs_fsinfo *info)
7974 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
7975 struct nfs4_secinfo_flavors *flavors;
7976 struct nfs4_secinfo4 *secinfo;
7979 page = alloc_page(GFP_KERNEL);
7985 flavors = page_address(page);
7986 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7989 * Fall back on "guess and check" method if
7990 * the server doesn't support SECINFO_NO_NAME
7992 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
7993 err = nfs4_find_root_sec(server, fhandle, info);
7999 for (i = 0; i < flavors->num_flavors; i++) {
8000 secinfo = &flavors->flavors[i];
8002 switch (secinfo->flavor) {
8006 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8007 &secinfo->flavor_info);
8010 flavor = RPC_AUTH_MAXFLAVOR;
8014 if (!nfs_auth_info_match(&server->auth_info, flavor))
8015 flavor = RPC_AUTH_MAXFLAVOR;
8017 if (flavor != RPC_AUTH_MAXFLAVOR) {
8018 err = nfs4_lookup_root_sec(server, fhandle,
8025 if (flavor == RPC_AUTH_MAXFLAVOR)
8036 static int _nfs41_test_stateid(struct nfs_server *server,
8037 nfs4_stateid *stateid,
8038 struct rpc_cred *cred)
8041 struct nfs41_test_stateid_args args = {
8044 struct nfs41_test_stateid_res res;
8045 struct rpc_message msg = {
8046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8051 struct rpc_clnt *rpc_client = server->client;
8053 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8056 dprintk("NFS call test_stateid %p\n", stateid);
8057 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8058 nfs4_set_sequence_privileged(&args.seq_args);
8059 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8060 &args.seq_args, &res.seq_res);
8061 if (status != NFS_OK) {
8062 dprintk("NFS reply test_stateid: failed, %d\n", status);
8065 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8070 * nfs41_test_stateid - perform a TEST_STATEID operation
8072 * @server: server / transport on which to perform the operation
8073 * @stateid: state ID to test
8076 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8077 * Otherwise a negative NFS4ERR value is returned if the operation
8078 * failed or the state ID is not currently valid.
8080 static int nfs41_test_stateid(struct nfs_server *server,
8081 nfs4_stateid *stateid,
8082 struct rpc_cred *cred)
8084 struct nfs4_exception exception = { };
8087 err = _nfs41_test_stateid(server, stateid, cred);
8088 if (err != -NFS4ERR_DELAY)
8090 nfs4_handle_exception(server, err, &exception);
8091 } while (exception.retry);
8095 struct nfs_free_stateid_data {
8096 struct nfs_server *server;
8097 struct nfs41_free_stateid_args args;
8098 struct nfs41_free_stateid_res res;
8101 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8103 struct nfs_free_stateid_data *data = calldata;
8104 nfs41_setup_sequence(nfs4_get_session(data->server),
8105 &data->args.seq_args,
8110 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8112 struct nfs_free_stateid_data *data = calldata;
8114 nfs41_sequence_done(task, &data->res.seq_res);
8116 switch (task->tk_status) {
8117 case -NFS4ERR_DELAY:
8118 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8119 rpc_restart_call_prepare(task);
8123 static void nfs41_free_stateid_release(void *calldata)
8128 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8129 .rpc_call_prepare = nfs41_free_stateid_prepare,
8130 .rpc_call_done = nfs41_free_stateid_done,
8131 .rpc_release = nfs41_free_stateid_release,
8134 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8135 nfs4_stateid *stateid,
8136 struct rpc_cred *cred,
8139 struct rpc_message msg = {
8140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8143 struct rpc_task_setup task_setup = {
8144 .rpc_client = server->client,
8145 .rpc_message = &msg,
8146 .callback_ops = &nfs41_free_stateid_ops,
8147 .flags = RPC_TASK_ASYNC,
8149 struct nfs_free_stateid_data *data;
8151 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8152 &task_setup.rpc_client, &msg);
8154 dprintk("NFS call free_stateid %p\n", stateid);
8155 data = kmalloc(sizeof(*data), GFP_NOFS);
8157 return ERR_PTR(-ENOMEM);
8158 data->server = server;
8159 nfs4_stateid_copy(&data->args.stateid, stateid);
8161 task_setup.callback_data = data;
8163 msg.rpc_argp = &data->args;
8164 msg.rpc_resp = &data->res;
8165 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8167 nfs4_set_sequence_privileged(&data->args.seq_args);
8169 return rpc_run_task(&task_setup);
8173 * nfs41_free_stateid - perform a FREE_STATEID operation
8175 * @server: server / transport on which to perform the operation
8176 * @stateid: state ID to release
8179 * Returns NFS_OK if the server freed "stateid". Otherwise a
8180 * negative NFS4ERR value is returned.
8182 static int nfs41_free_stateid(struct nfs_server *server,
8183 nfs4_stateid *stateid,
8184 struct rpc_cred *cred)
8186 struct rpc_task *task;
8189 task = _nfs41_free_stateid(server, stateid, cred, true);
8191 return PTR_ERR(task);
8192 ret = rpc_wait_for_completion_task(task);
8194 ret = task->tk_status;
8199 static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8201 struct rpc_task *task;
8202 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8204 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8205 nfs4_free_lock_state(server, lsp);
8207 return PTR_ERR(task);
8212 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8213 const nfs4_stateid *s2)
8215 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8218 if (s1->seqid == s2->seqid)
8220 if (s1->seqid == 0 || s2->seqid == 0)
8226 #endif /* CONFIG_NFS_V4_1 */
8228 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8229 const nfs4_stateid *s2)
8231 return nfs4_stateid_match(s1, s2);
8235 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8236 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8237 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8238 .recover_open = nfs4_open_reclaim,
8239 .recover_lock = nfs4_lock_reclaim,
8240 .establish_clid = nfs4_init_clientid,
8241 .detect_trunking = nfs40_discover_server_trunking,
8244 #if defined(CONFIG_NFS_V4_1)
8245 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8246 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8247 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8248 .recover_open = nfs4_open_reclaim,
8249 .recover_lock = nfs4_lock_reclaim,
8250 .establish_clid = nfs41_init_clientid,
8251 .reclaim_complete = nfs41_proc_reclaim_complete,
8252 .detect_trunking = nfs41_discover_server_trunking,
8254 #endif /* CONFIG_NFS_V4_1 */
8256 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8257 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8258 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8259 .recover_open = nfs4_open_expired,
8260 .recover_lock = nfs4_lock_expired,
8261 .establish_clid = nfs4_init_clientid,
8264 #if defined(CONFIG_NFS_V4_1)
8265 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8266 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8267 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8268 .recover_open = nfs41_open_expired,
8269 .recover_lock = nfs41_lock_expired,
8270 .establish_clid = nfs41_init_clientid,
8272 #endif /* CONFIG_NFS_V4_1 */
8274 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8275 .sched_state_renewal = nfs4_proc_async_renew,
8276 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8277 .renew_lease = nfs4_proc_renew,
8280 #if defined(CONFIG_NFS_V4_1)
8281 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8282 .sched_state_renewal = nfs41_proc_async_sequence,
8283 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8284 .renew_lease = nfs4_proc_sequence,
8288 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8289 .get_locations = _nfs40_proc_get_locations,
8290 .fsid_present = _nfs40_proc_fsid_present,
8293 #if defined(CONFIG_NFS_V4_1)
8294 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8295 .get_locations = _nfs41_proc_get_locations,
8296 .fsid_present = _nfs41_proc_fsid_present,
8298 #endif /* CONFIG_NFS_V4_1 */
8300 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8302 .init_caps = NFS_CAP_READDIRPLUS
8303 | NFS_CAP_ATOMIC_OPEN
8304 | NFS_CAP_CHANGE_ATTR
8305 | NFS_CAP_POSIX_LOCK,
8306 .init_client = nfs40_init_client,
8307 .shutdown_client = nfs40_shutdown_client,
8308 .match_stateid = nfs4_match_stateid,
8309 .find_root_sec = nfs4_find_root_sec,
8310 .free_lock_state = nfs4_release_lockowner,
8311 .call_sync_ops = &nfs40_call_sync_ops,
8312 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8313 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8314 .state_renewal_ops = &nfs40_state_renewal_ops,
8315 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8318 #if defined(CONFIG_NFS_V4_1)
8319 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8321 .init_caps = NFS_CAP_READDIRPLUS
8322 | NFS_CAP_ATOMIC_OPEN
8323 | NFS_CAP_CHANGE_ATTR
8324 | NFS_CAP_POSIX_LOCK
8325 | NFS_CAP_STATEID_NFSV41
8326 | NFS_CAP_ATOMIC_OPEN_V1,
8327 .init_client = nfs41_init_client,
8328 .shutdown_client = nfs41_shutdown_client,
8329 .match_stateid = nfs41_match_stateid,
8330 .find_root_sec = nfs41_find_root_sec,
8331 .free_lock_state = nfs41_free_lock_state,
8332 .call_sync_ops = &nfs41_call_sync_ops,
8333 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8334 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8335 .state_renewal_ops = &nfs41_state_renewal_ops,
8336 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8340 #if defined(CONFIG_NFS_V4_2)
8341 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8343 .init_caps = NFS_CAP_READDIRPLUS
8344 | NFS_CAP_ATOMIC_OPEN
8345 | NFS_CAP_CHANGE_ATTR
8346 | NFS_CAP_POSIX_LOCK
8347 | NFS_CAP_STATEID_NFSV41
8348 | NFS_CAP_ATOMIC_OPEN_V1,
8349 .init_client = nfs41_init_client,
8350 .shutdown_client = nfs41_shutdown_client,
8351 .match_stateid = nfs41_match_stateid,
8352 .find_root_sec = nfs41_find_root_sec,
8353 .free_lock_state = nfs41_free_lock_state,
8354 .call_sync_ops = &nfs41_call_sync_ops,
8355 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8356 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8357 .state_renewal_ops = &nfs41_state_renewal_ops,
8361 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8362 [0] = &nfs_v4_0_minor_ops,
8363 #if defined(CONFIG_NFS_V4_1)
8364 [1] = &nfs_v4_1_minor_ops,
8366 #if defined(CONFIG_NFS_V4_2)
8367 [2] = &nfs_v4_2_minor_ops,
8371 static const struct inode_operations nfs4_dir_inode_operations = {
8372 .create = nfs_create,
8373 .lookup = nfs_lookup,
8374 .atomic_open = nfs_atomic_open,
8376 .unlink = nfs_unlink,
8377 .symlink = nfs_symlink,
8381 .rename = nfs_rename,
8382 .permission = nfs_permission,
8383 .getattr = nfs_getattr,
8384 .setattr = nfs_setattr,
8385 .getxattr = generic_getxattr,
8386 .setxattr = generic_setxattr,
8387 .listxattr = generic_listxattr,
8388 .removexattr = generic_removexattr,
8391 static const struct inode_operations nfs4_file_inode_operations = {
8392 .permission = nfs_permission,
8393 .getattr = nfs_getattr,
8394 .setattr = nfs_setattr,
8395 .getxattr = generic_getxattr,
8396 .setxattr = generic_setxattr,
8397 .listxattr = generic_listxattr,
8398 .removexattr = generic_removexattr,
8401 const struct nfs_rpc_ops nfs_v4_clientops = {
8402 .version = 4, /* protocol version */
8403 .dentry_ops = &nfs4_dentry_operations,
8404 .dir_inode_ops = &nfs4_dir_inode_operations,
8405 .file_inode_ops = &nfs4_file_inode_operations,
8406 .file_ops = &nfs4_file_operations,
8407 .getroot = nfs4_proc_get_root,
8408 .submount = nfs4_submount,
8409 .try_mount = nfs4_try_mount,
8410 .getattr = nfs4_proc_getattr,
8411 .setattr = nfs4_proc_setattr,
8412 .lookup = nfs4_proc_lookup,
8413 .access = nfs4_proc_access,
8414 .readlink = nfs4_proc_readlink,
8415 .create = nfs4_proc_create,
8416 .remove = nfs4_proc_remove,
8417 .unlink_setup = nfs4_proc_unlink_setup,
8418 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8419 .unlink_done = nfs4_proc_unlink_done,
8420 .rename_setup = nfs4_proc_rename_setup,
8421 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8422 .rename_done = nfs4_proc_rename_done,
8423 .link = nfs4_proc_link,
8424 .symlink = nfs4_proc_symlink,
8425 .mkdir = nfs4_proc_mkdir,
8426 .rmdir = nfs4_proc_remove,
8427 .readdir = nfs4_proc_readdir,
8428 .mknod = nfs4_proc_mknod,
8429 .statfs = nfs4_proc_statfs,
8430 .fsinfo = nfs4_proc_fsinfo,
8431 .pathconf = nfs4_proc_pathconf,
8432 .set_capabilities = nfs4_server_capabilities,
8433 .decode_dirent = nfs4_decode_dirent,
8434 .read_setup = nfs4_proc_read_setup,
8435 .read_pageio_init = pnfs_pageio_init_read,
8436 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
8437 .read_done = nfs4_read_done,
8438 .write_setup = nfs4_proc_write_setup,
8439 .write_pageio_init = pnfs_pageio_init_write,
8440 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
8441 .write_done = nfs4_write_done,
8442 .commit_setup = nfs4_proc_commit_setup,
8443 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8444 .commit_done = nfs4_commit_done,
8445 .lock = nfs4_proc_lock,
8446 .clear_acl_cache = nfs4_zap_acl_attr,
8447 .close_context = nfs4_close_context,
8448 .open_context = nfs4_atomic_open,
8449 .have_delegation = nfs4_have_delegation,
8450 .return_delegation = nfs4_inode_return_delegation,
8451 .alloc_client = nfs4_alloc_client,
8452 .init_client = nfs4_init_client,
8453 .free_client = nfs4_free_client,
8454 .create_server = nfs4_create_server,
8455 .clone_server = nfs_clone_server,
8458 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8459 .prefix = XATTR_NAME_NFSV4_ACL,
8460 .list = nfs4_xattr_list_nfs4_acl,
8461 .get = nfs4_xattr_get_nfs4_acl,
8462 .set = nfs4_xattr_set_nfs4_acl,
8465 const struct xattr_handler *nfs4_xattr_handlers[] = {
8466 &nfs4_xattr_nfs4_acl_handler,
8467 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8468 &nfs4_xattr_nfs4_label_handler,