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);
1071 kfree(p->f_attr.mdsthreshold);
1075 static void nfs4_opendata_put(struct nfs4_opendata *p)
1078 kref_put(&p->kref, nfs4_opendata_free);
1081 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1085 ret = rpc_wait_for_completion_task(task);
1089 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1093 if (open_mode & (O_EXCL|O_TRUNC))
1095 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1097 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1098 && state->n_rdonly != 0;
1101 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1102 && state->n_wronly != 0;
1104 case FMODE_READ|FMODE_WRITE:
1105 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1106 && state->n_rdwr != 0;
1112 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1114 if (delegation == NULL)
1116 if ((delegation->type & fmode) != fmode)
1118 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1120 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1122 nfs_mark_delegation_referenced(delegation);
1126 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1135 case FMODE_READ|FMODE_WRITE:
1138 nfs4_state_set_mode_locked(state, state->state | fmode);
1141 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1143 struct nfs_client *clp = state->owner->so_server->nfs_client;
1144 bool need_recover = false;
1146 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1147 need_recover = true;
1148 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1149 need_recover = true;
1150 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1151 need_recover = true;
1153 nfs4_state_mark_reclaim_nograce(clp, state);
1156 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1157 nfs4_stateid *stateid)
1159 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1161 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1162 nfs_test_and_clear_all_open_stateid(state);
1165 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1170 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1171 nfs4_stateid *stateid, fmode_t fmode)
1173 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1174 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1176 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1179 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1182 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1183 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1184 clear_bit(NFS_OPEN_STATE, &state->flags);
1186 if (stateid == NULL)
1188 if (!nfs_need_update_open_stateid(state, stateid))
1190 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1191 nfs4_stateid_copy(&state->stateid, stateid);
1192 nfs4_stateid_copy(&state->open_stateid, stateid);
1195 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1197 write_seqlock(&state->seqlock);
1198 nfs_clear_open_stateid_locked(state, stateid, fmode);
1199 write_sequnlock(&state->seqlock);
1200 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1201 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1204 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1208 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1211 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1213 case FMODE_READ|FMODE_WRITE:
1214 set_bit(NFS_O_RDWR_STATE, &state->flags);
1216 if (!nfs_need_update_open_stateid(state, stateid))
1218 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1219 nfs4_stateid_copy(&state->stateid, stateid);
1220 nfs4_stateid_copy(&state->open_stateid, stateid);
1223 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1226 * Protect the call to nfs4_state_set_mode_locked and
1227 * serialise the stateid update
1229 write_seqlock(&state->seqlock);
1230 if (deleg_stateid != NULL) {
1231 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1232 set_bit(NFS_DELEGATED_STATE, &state->flags);
1234 if (open_stateid != NULL)
1235 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1236 write_sequnlock(&state->seqlock);
1237 spin_lock(&state->owner->so_lock);
1238 update_open_stateflags(state, fmode);
1239 spin_unlock(&state->owner->so_lock);
1242 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1244 struct nfs_inode *nfsi = NFS_I(state->inode);
1245 struct nfs_delegation *deleg_cur;
1248 fmode &= (FMODE_READ|FMODE_WRITE);
1251 deleg_cur = rcu_dereference(nfsi->delegation);
1252 if (deleg_cur == NULL)
1255 spin_lock(&deleg_cur->lock);
1256 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1257 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1258 (deleg_cur->type & fmode) != fmode)
1259 goto no_delegation_unlock;
1261 if (delegation == NULL)
1262 delegation = &deleg_cur->stateid;
1263 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1264 goto no_delegation_unlock;
1266 nfs_mark_delegation_referenced(deleg_cur);
1267 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1269 no_delegation_unlock:
1270 spin_unlock(&deleg_cur->lock);
1274 if (!ret && open_stateid != NULL) {
1275 __update_open_stateid(state, open_stateid, NULL, fmode);
1278 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1279 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1285 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1287 struct nfs_delegation *delegation;
1290 delegation = rcu_dereference(NFS_I(inode)->delegation);
1291 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1296 nfs4_inode_return_delegation(inode);
1299 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1301 struct nfs4_state *state = opendata->state;
1302 struct nfs_inode *nfsi = NFS_I(state->inode);
1303 struct nfs_delegation *delegation;
1304 int open_mode = opendata->o_arg.open_flags;
1305 fmode_t fmode = opendata->o_arg.fmode;
1306 nfs4_stateid stateid;
1310 if (can_open_cached(state, fmode, open_mode)) {
1311 spin_lock(&state->owner->so_lock);
1312 if (can_open_cached(state, fmode, open_mode)) {
1313 update_open_stateflags(state, fmode);
1314 spin_unlock(&state->owner->so_lock);
1315 goto out_return_state;
1317 spin_unlock(&state->owner->so_lock);
1320 delegation = rcu_dereference(nfsi->delegation);
1321 if (!can_open_delegated(delegation, fmode)) {
1325 /* Save the delegation */
1326 nfs4_stateid_copy(&stateid, &delegation->stateid);
1328 nfs_release_seqid(opendata->o_arg.seqid);
1329 if (!opendata->is_recover) {
1330 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1336 /* Try to update the stateid using the delegation */
1337 if (update_open_stateid(state, NULL, &stateid, fmode))
1338 goto out_return_state;
1341 return ERR_PTR(ret);
1343 atomic_inc(&state->count);
1348 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1350 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1351 struct nfs_delegation *delegation;
1352 int delegation_flags = 0;
1355 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1357 delegation_flags = delegation->flags;
1359 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1360 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1361 "returning a delegation for "
1362 "OPEN(CLAIM_DELEGATE_CUR)\n",
1364 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1365 nfs_inode_set_delegation(state->inode,
1366 data->owner->so_cred,
1369 nfs_inode_reclaim_delegation(state->inode,
1370 data->owner->so_cred,
1375 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1376 * and update the nfs4_state.
1378 static struct nfs4_state *
1379 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1381 struct inode *inode = data->state->inode;
1382 struct nfs4_state *state = data->state;
1385 if (!data->rpc_done) {
1386 if (data->rpc_status) {
1387 ret = data->rpc_status;
1390 /* cached opens have already been processed */
1394 ret = nfs_refresh_inode(inode, &data->f_attr);
1398 if (data->o_res.delegation_type != 0)
1399 nfs4_opendata_check_deleg(data, state);
1401 update_open_stateid(state, &data->o_res.stateid, NULL,
1403 atomic_inc(&state->count);
1407 return ERR_PTR(ret);
1411 static struct nfs4_state *
1412 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1414 struct inode *inode;
1415 struct nfs4_state *state = NULL;
1418 if (!data->rpc_done) {
1419 state = nfs4_try_open_cached(data);
1424 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1426 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1427 ret = PTR_ERR(inode);
1431 state = nfs4_get_open_state(inode, data->owner);
1434 if (data->o_res.delegation_type != 0)
1435 nfs4_opendata_check_deleg(data, state);
1436 update_open_stateid(state, &data->o_res.stateid, NULL,
1440 nfs_release_seqid(data->o_arg.seqid);
1445 return ERR_PTR(ret);
1448 static struct nfs4_state *
1449 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1451 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1452 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1453 return _nfs4_opendata_to_nfs4_state(data);
1456 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1458 struct nfs_inode *nfsi = NFS_I(state->inode);
1459 struct nfs_open_context *ctx;
1461 spin_lock(&state->inode->i_lock);
1462 list_for_each_entry(ctx, &nfsi->open_files, list) {
1463 if (ctx->state != state)
1465 get_nfs_open_context(ctx);
1466 spin_unlock(&state->inode->i_lock);
1469 spin_unlock(&state->inode->i_lock);
1470 return ERR_PTR(-ENOENT);
1473 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1474 struct nfs4_state *state, enum open_claim_type4 claim)
1476 struct nfs4_opendata *opendata;
1478 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1479 NULL, NULL, claim, GFP_NOFS);
1480 if (opendata == NULL)
1481 return ERR_PTR(-ENOMEM);
1482 opendata->state = state;
1483 atomic_inc(&state->count);
1487 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1489 struct nfs4_state *newstate;
1492 opendata->o_arg.open_flags = 0;
1493 opendata->o_arg.fmode = fmode;
1494 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1495 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1496 nfs4_init_opendata_res(opendata);
1497 ret = _nfs4_recover_proc_open(opendata);
1500 newstate = nfs4_opendata_to_nfs4_state(opendata);
1501 if (IS_ERR(newstate))
1502 return PTR_ERR(newstate);
1503 nfs4_close_state(newstate, fmode);
1508 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1510 struct nfs4_state *newstate;
1513 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1514 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1515 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1516 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1517 /* memory barrier prior to reading state->n_* */
1518 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1519 clear_bit(NFS_OPEN_STATE, &state->flags);
1521 if (state->n_rdwr != 0) {
1522 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1525 if (newstate != state)
1528 if (state->n_wronly != 0) {
1529 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1532 if (newstate != state)
1535 if (state->n_rdonly != 0) {
1536 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1539 if (newstate != state)
1543 * We may have performed cached opens for all three recoveries.
1544 * Check if we need to update the current stateid.
1546 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1547 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1548 write_seqlock(&state->seqlock);
1549 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1550 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1551 write_sequnlock(&state->seqlock);
1558 * reclaim state on the server after a reboot.
1560 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1562 struct nfs_delegation *delegation;
1563 struct nfs4_opendata *opendata;
1564 fmode_t delegation_type = 0;
1567 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1568 NFS4_OPEN_CLAIM_PREVIOUS);
1569 if (IS_ERR(opendata))
1570 return PTR_ERR(opendata);
1572 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1573 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1574 delegation_type = delegation->type;
1576 opendata->o_arg.u.delegation_type = delegation_type;
1577 status = nfs4_open_recover(opendata, state);
1578 nfs4_opendata_put(opendata);
1582 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1584 struct nfs_server *server = NFS_SERVER(state->inode);
1585 struct nfs4_exception exception = { };
1588 err = _nfs4_do_open_reclaim(ctx, state);
1589 trace_nfs4_open_reclaim(ctx, 0, err);
1590 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1592 if (err != -NFS4ERR_DELAY)
1594 nfs4_handle_exception(server, err, &exception);
1595 } while (exception.retry);
1599 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1601 struct nfs_open_context *ctx;
1604 ctx = nfs4_state_find_open_context(state);
1607 ret = nfs4_do_open_reclaim(ctx, state);
1608 put_nfs_open_context(ctx);
1612 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1616 printk(KERN_ERR "NFS: %s: unhandled error "
1617 "%d.\n", __func__, err);
1622 case -NFS4ERR_BADSESSION:
1623 case -NFS4ERR_BADSLOT:
1624 case -NFS4ERR_BAD_HIGH_SLOT:
1625 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1626 case -NFS4ERR_DEADSESSION:
1627 set_bit(NFS_DELEGATED_STATE, &state->flags);
1628 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1630 case -NFS4ERR_STALE_CLIENTID:
1631 case -NFS4ERR_STALE_STATEID:
1632 set_bit(NFS_DELEGATED_STATE, &state->flags);
1633 case -NFS4ERR_EXPIRED:
1634 /* Don't recall a delegation if it was lost */
1635 nfs4_schedule_lease_recovery(server->nfs_client);
1637 case -NFS4ERR_MOVED:
1638 nfs4_schedule_migration_recovery(server);
1640 case -NFS4ERR_LEASE_MOVED:
1641 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1643 case -NFS4ERR_DELEG_REVOKED:
1644 case -NFS4ERR_ADMIN_REVOKED:
1645 case -NFS4ERR_BAD_STATEID:
1646 case -NFS4ERR_OPENMODE:
1647 nfs_inode_find_state_and_recover(state->inode,
1649 nfs4_schedule_stateid_recovery(server, state);
1651 case -NFS4ERR_DELAY:
1652 case -NFS4ERR_GRACE:
1653 set_bit(NFS_DELEGATED_STATE, &state->flags);
1657 case -NFS4ERR_DENIED:
1658 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1664 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1666 struct nfs_server *server = NFS_SERVER(state->inode);
1667 struct nfs4_opendata *opendata;
1670 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1671 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1672 if (IS_ERR(opendata))
1673 return PTR_ERR(opendata);
1674 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1675 err = nfs4_open_recover(opendata, state);
1676 nfs4_opendata_put(opendata);
1677 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1680 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1682 struct nfs4_opendata *data = calldata;
1684 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1685 &data->c_res.seq_res, task);
1688 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1690 struct nfs4_opendata *data = calldata;
1692 nfs40_sequence_done(task, &data->c_res.seq_res);
1694 data->rpc_status = task->tk_status;
1695 if (data->rpc_status == 0) {
1696 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1697 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1698 renew_lease(data->o_res.server, data->timestamp);
1703 static void nfs4_open_confirm_release(void *calldata)
1705 struct nfs4_opendata *data = calldata;
1706 struct nfs4_state *state = NULL;
1708 /* If this request hasn't been cancelled, do nothing */
1709 if (data->cancelled == 0)
1711 /* In case of error, no cleanup! */
1712 if (!data->rpc_done)
1714 state = nfs4_opendata_to_nfs4_state(data);
1716 nfs4_close_state(state, data->o_arg.fmode);
1718 nfs4_opendata_put(data);
1721 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1722 .rpc_call_prepare = nfs4_open_confirm_prepare,
1723 .rpc_call_done = nfs4_open_confirm_done,
1724 .rpc_release = nfs4_open_confirm_release,
1728 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1730 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1732 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1733 struct rpc_task *task;
1734 struct rpc_message msg = {
1735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1736 .rpc_argp = &data->c_arg,
1737 .rpc_resp = &data->c_res,
1738 .rpc_cred = data->owner->so_cred,
1740 struct rpc_task_setup task_setup_data = {
1741 .rpc_client = server->client,
1742 .rpc_message = &msg,
1743 .callback_ops = &nfs4_open_confirm_ops,
1744 .callback_data = data,
1745 .workqueue = nfsiod_workqueue,
1746 .flags = RPC_TASK_ASYNC,
1750 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1751 kref_get(&data->kref);
1753 data->rpc_status = 0;
1754 data->timestamp = jiffies;
1755 task = rpc_run_task(&task_setup_data);
1757 return PTR_ERR(task);
1758 status = nfs4_wait_for_completion_rpc_task(task);
1760 data->cancelled = 1;
1763 status = data->rpc_status;
1768 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1770 struct nfs4_opendata *data = calldata;
1771 struct nfs4_state_owner *sp = data->owner;
1772 struct nfs_client *clp = sp->so_server->nfs_client;
1774 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1777 * Check if we still need to send an OPEN call, or if we can use
1778 * a delegation instead.
1780 if (data->state != NULL) {
1781 struct nfs_delegation *delegation;
1783 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1786 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1787 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1788 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1789 can_open_delegated(delegation, data->o_arg.fmode))
1790 goto unlock_no_action;
1793 /* Update client id. */
1794 data->o_arg.clientid = clp->cl_clientid;
1795 switch (data->o_arg.claim) {
1796 case NFS4_OPEN_CLAIM_PREVIOUS:
1797 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1798 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1799 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1800 case NFS4_OPEN_CLAIM_FH:
1801 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1802 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1804 data->timestamp = jiffies;
1805 if (nfs4_setup_sequence(data->o_arg.server,
1806 &data->o_arg.seq_args,
1807 &data->o_res.seq_res,
1809 nfs_release_seqid(data->o_arg.seqid);
1811 /* Set the create mode (note dependency on the session type) */
1812 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1813 if (data->o_arg.open_flags & O_EXCL) {
1814 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1815 if (nfs4_has_persistent_session(clp))
1816 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1817 else if (clp->cl_mvops->minor_version > 0)
1818 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1824 task->tk_action = NULL;
1826 nfs4_sequence_done(task, &data->o_res.seq_res);
1829 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1831 struct nfs4_opendata *data = calldata;
1833 data->rpc_status = task->tk_status;
1835 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1838 if (task->tk_status == 0) {
1839 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1840 switch (data->o_res.f_attr->mode & S_IFMT) {
1844 data->rpc_status = -ELOOP;
1847 data->rpc_status = -EISDIR;
1850 data->rpc_status = -ENOTDIR;
1853 renew_lease(data->o_res.server, data->timestamp);
1854 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1855 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1860 static void nfs4_open_release(void *calldata)
1862 struct nfs4_opendata *data = calldata;
1863 struct nfs4_state *state = NULL;
1865 /* If this request hasn't been cancelled, do nothing */
1866 if (data->cancelled == 0)
1868 /* In case of error, no cleanup! */
1869 if (data->rpc_status != 0 || !data->rpc_done)
1871 /* In case we need an open_confirm, no cleanup! */
1872 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1874 state = nfs4_opendata_to_nfs4_state(data);
1876 nfs4_close_state(state, data->o_arg.fmode);
1878 nfs4_opendata_put(data);
1881 static const struct rpc_call_ops nfs4_open_ops = {
1882 .rpc_call_prepare = nfs4_open_prepare,
1883 .rpc_call_done = nfs4_open_done,
1884 .rpc_release = nfs4_open_release,
1887 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1889 struct inode *dir = data->dir->d_inode;
1890 struct nfs_server *server = NFS_SERVER(dir);
1891 struct nfs_openargs *o_arg = &data->o_arg;
1892 struct nfs_openres *o_res = &data->o_res;
1893 struct rpc_task *task;
1894 struct rpc_message msg = {
1895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1898 .rpc_cred = data->owner->so_cred,
1900 struct rpc_task_setup task_setup_data = {
1901 .rpc_client = server->client,
1902 .rpc_message = &msg,
1903 .callback_ops = &nfs4_open_ops,
1904 .callback_data = data,
1905 .workqueue = nfsiod_workqueue,
1906 .flags = RPC_TASK_ASYNC,
1910 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1911 kref_get(&data->kref);
1913 data->rpc_status = 0;
1914 data->cancelled = 0;
1915 data->is_recover = 0;
1917 nfs4_set_sequence_privileged(&o_arg->seq_args);
1918 data->is_recover = 1;
1920 task = rpc_run_task(&task_setup_data);
1922 return PTR_ERR(task);
1923 status = nfs4_wait_for_completion_rpc_task(task);
1925 data->cancelled = 1;
1928 status = data->rpc_status;
1934 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1936 struct inode *dir = data->dir->d_inode;
1937 struct nfs_openres *o_res = &data->o_res;
1940 status = nfs4_run_open_task(data, 1);
1941 if (status != 0 || !data->rpc_done)
1944 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1946 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1947 status = _nfs4_proc_open_confirm(data);
1956 * Additional permission checks in order to distinguish between an
1957 * open for read, and an open for execute. This works around the
1958 * fact that NFSv4 OPEN treats read and execute permissions as being
1960 * Note that in the non-execute case, we want to turn off permission
1961 * checking if we just created a new file (POSIX open() semantics).
1963 static int nfs4_opendata_access(struct rpc_cred *cred,
1964 struct nfs4_opendata *opendata,
1965 struct nfs4_state *state, fmode_t fmode,
1968 struct nfs_access_entry cache;
1971 /* access call failed or for some reason the server doesn't
1972 * support any access modes -- defer access call until later */
1973 if (opendata->o_res.access_supported == 0)
1978 * Use openflags to check for exec, because fmode won't
1979 * always have FMODE_EXEC set when file open for exec.
1981 if (openflags & __FMODE_EXEC) {
1982 /* ONLY check for exec rights */
1984 } else if ((fmode & FMODE_READ) && !opendata->file_created)
1988 cache.jiffies = jiffies;
1989 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1990 nfs_access_add_cache(state->inode, &cache);
1992 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1995 /* even though OPEN succeeded, access is denied. Close the file */
1996 nfs4_close_state(state, fmode);
2001 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2003 static int _nfs4_proc_open(struct nfs4_opendata *data)
2005 struct inode *dir = data->dir->d_inode;
2006 struct nfs_server *server = NFS_SERVER(dir);
2007 struct nfs_openargs *o_arg = &data->o_arg;
2008 struct nfs_openres *o_res = &data->o_res;
2011 status = nfs4_run_open_task(data, 0);
2012 if (!data->rpc_done)
2015 if (status == -NFS4ERR_BADNAME &&
2016 !(o_arg->open_flags & O_CREAT))
2021 nfs_fattr_map_and_free_names(server, &data->f_attr);
2023 if (o_arg->open_flags & O_CREAT) {
2024 update_changeattr(dir, &o_res->cinfo);
2025 if (o_arg->open_flags & O_EXCL)
2026 data->file_created = 1;
2027 else if (o_res->cinfo.before != o_res->cinfo.after)
2028 data->file_created = 1;
2030 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2031 server->caps &= ~NFS_CAP_POSIX_LOCK;
2032 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2033 status = _nfs4_proc_open_confirm(data);
2037 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2038 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2042 static int nfs4_recover_expired_lease(struct nfs_server *server)
2044 return nfs4_client_recover_expired_lease(server->nfs_client);
2049 * reclaim state on the server after a network partition.
2050 * Assumes caller holds the appropriate lock
2052 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2054 struct nfs4_opendata *opendata;
2057 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2058 NFS4_OPEN_CLAIM_FH);
2059 if (IS_ERR(opendata))
2060 return PTR_ERR(opendata);
2061 ret = nfs4_open_recover(opendata, state);
2063 d_drop(ctx->dentry);
2064 nfs4_opendata_put(opendata);
2068 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2070 struct nfs_server *server = NFS_SERVER(state->inode);
2071 struct nfs4_exception exception = { };
2075 err = _nfs4_open_expired(ctx, state);
2076 trace_nfs4_open_expired(ctx, 0, err);
2077 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2082 case -NFS4ERR_GRACE:
2083 case -NFS4ERR_DELAY:
2084 nfs4_handle_exception(server, err, &exception);
2087 } while (exception.retry);
2092 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2094 struct nfs_open_context *ctx;
2097 ctx = nfs4_state_find_open_context(state);
2100 ret = nfs4_do_open_expired(ctx, state);
2101 put_nfs_open_context(ctx);
2105 #if defined(CONFIG_NFS_V4_1)
2106 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
2108 struct nfs_server *server = NFS_SERVER(state->inode);
2109 nfs4_stateid *stateid = &state->stateid;
2110 struct nfs_delegation *delegation;
2111 struct rpc_cred *cred = NULL;
2112 int status = -NFS4ERR_BAD_STATEID;
2114 /* If a state reset has been done, test_stateid is unneeded */
2115 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2118 /* Get the delegation credential for use by test/free_stateid */
2120 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2121 if (delegation != NULL &&
2122 nfs4_stateid_match(&delegation->stateid, stateid)) {
2123 cred = get_rpccred(delegation->cred);
2125 status = nfs41_test_stateid(server, stateid, cred);
2126 trace_nfs4_test_delegation_stateid(state, NULL, status);
2130 if (status != NFS_OK) {
2131 /* Free the stateid unless the server explicitly
2132 * informs us the stateid is unrecognized. */
2133 if (status != -NFS4ERR_BAD_STATEID)
2134 nfs41_free_stateid(server, stateid, cred);
2135 nfs_remove_bad_delegation(state->inode);
2137 write_seqlock(&state->seqlock);
2138 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2139 write_sequnlock(&state->seqlock);
2140 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2148 * nfs41_check_open_stateid - possibly free an open stateid
2150 * @state: NFSv4 state for an inode
2152 * Returns NFS_OK if recovery for this stateid is now finished.
2153 * Otherwise a negative NFS4ERR value is returned.
2155 static int nfs41_check_open_stateid(struct nfs4_state *state)
2157 struct nfs_server *server = NFS_SERVER(state->inode);
2158 nfs4_stateid *stateid = &state->open_stateid;
2159 struct rpc_cred *cred = state->owner->so_cred;
2162 /* If a state reset has been done, test_stateid is unneeded */
2163 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2164 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2165 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2166 return -NFS4ERR_BAD_STATEID;
2168 status = nfs41_test_stateid(server, stateid, cred);
2169 trace_nfs4_test_open_stateid(state, NULL, status);
2170 if (status != NFS_OK) {
2171 /* Free the stateid unless the server explicitly
2172 * informs us the stateid is unrecognized. */
2173 if (status != -NFS4ERR_BAD_STATEID)
2174 nfs41_free_stateid(server, stateid, cred);
2176 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2177 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2178 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2179 clear_bit(NFS_OPEN_STATE, &state->flags);
2184 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2188 nfs41_clear_delegation_stateid(state);
2189 status = nfs41_check_open_stateid(state);
2190 if (status != NFS_OK)
2191 status = nfs4_open_expired(sp, state);
2197 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2198 * fields corresponding to attributes that were used to store the verifier.
2199 * Make sure we clobber those fields in the later setattr call
2201 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2203 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2204 !(sattr->ia_valid & ATTR_ATIME_SET))
2205 sattr->ia_valid |= ATTR_ATIME;
2207 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2208 !(sattr->ia_valid & ATTR_MTIME_SET))
2209 sattr->ia_valid |= ATTR_MTIME;
2212 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2215 struct nfs_open_context *ctx)
2217 struct nfs4_state_owner *sp = opendata->owner;
2218 struct nfs_server *server = sp->so_server;
2219 struct dentry *dentry;
2220 struct nfs4_state *state;
2224 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2226 ret = _nfs4_proc_open(opendata);
2230 state = nfs4_opendata_to_nfs4_state(opendata);
2231 ret = PTR_ERR(state);
2234 if (server->caps & NFS_CAP_POSIX_LOCK)
2235 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2237 dentry = opendata->dentry;
2238 if (dentry->d_inode == NULL) {
2239 /* FIXME: Is this d_drop() ever needed? */
2241 dentry = d_add_unique(dentry, igrab(state->inode));
2242 if (dentry == NULL) {
2243 dentry = opendata->dentry;
2244 } else if (dentry != ctx->dentry) {
2246 ctx->dentry = dget(dentry);
2248 nfs_set_verifier(dentry,
2249 nfs_save_change_attribute(opendata->dir->d_inode));
2252 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2257 if (dentry->d_inode == state->inode) {
2258 nfs_inode_attach_open_context(ctx);
2259 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2260 nfs4_schedule_stateid_recovery(server, state);
2267 * Returns a referenced nfs4_state
2269 static int _nfs4_do_open(struct inode *dir,
2270 struct nfs_open_context *ctx,
2272 struct iattr *sattr,
2273 struct nfs4_label *label,
2276 struct nfs4_state_owner *sp;
2277 struct nfs4_state *state = NULL;
2278 struct nfs_server *server = NFS_SERVER(dir);
2279 struct nfs4_opendata *opendata;
2280 struct dentry *dentry = ctx->dentry;
2281 struct rpc_cred *cred = ctx->cred;
2282 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2283 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2284 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2285 struct nfs4_label *olabel = NULL;
2288 /* Protect against reboot recovery conflicts */
2290 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2292 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2295 status = nfs4_recover_expired_lease(server);
2297 goto err_put_state_owner;
2298 if (dentry->d_inode != NULL)
2299 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2301 if (dentry->d_inode)
2302 claim = NFS4_OPEN_CLAIM_FH;
2303 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2304 label, claim, GFP_KERNEL);
2305 if (opendata == NULL)
2306 goto err_put_state_owner;
2309 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2310 if (IS_ERR(olabel)) {
2311 status = PTR_ERR(olabel);
2312 goto err_opendata_put;
2316 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2317 if (!opendata->f_attr.mdsthreshold) {
2318 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2319 if (!opendata->f_attr.mdsthreshold)
2320 goto err_free_label;
2322 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2324 if (dentry->d_inode != NULL)
2325 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2327 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2329 goto err_free_label;
2332 if ((opendata->o_arg.open_flags & O_EXCL) &&
2333 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2334 nfs4_exclusive_attrset(opendata, sattr);
2336 nfs_fattr_init(opendata->o_res.f_attr);
2337 status = nfs4_do_setattr(state->inode, cred,
2338 opendata->o_res.f_attr, sattr,
2339 state, label, olabel);
2341 nfs_setattr_update_inode(state->inode, sattr);
2342 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2343 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2346 if (opendata->file_created)
2347 *opened |= FILE_CREATED;
2349 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2350 *ctx_th = opendata->f_attr.mdsthreshold;
2351 opendata->f_attr.mdsthreshold = NULL;
2354 nfs4_label_free(olabel);
2356 nfs4_opendata_put(opendata);
2357 nfs4_put_state_owner(sp);
2360 nfs4_label_free(olabel);
2362 nfs4_opendata_put(opendata);
2363 err_put_state_owner:
2364 nfs4_put_state_owner(sp);
2370 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2371 struct nfs_open_context *ctx,
2373 struct iattr *sattr,
2374 struct nfs4_label *label,
2377 struct nfs_server *server = NFS_SERVER(dir);
2378 struct nfs4_exception exception = { };
2379 struct nfs4_state *res;
2383 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2385 trace_nfs4_open_file(ctx, flags, status);
2388 /* NOTE: BAD_SEQID means the server and client disagree about the
2389 * book-keeping w.r.t. state-changing operations
2390 * (OPEN/CLOSE/LOCK/LOCKU...)
2391 * It is actually a sign of a bug on the client or on the server.
2393 * If we receive a BAD_SEQID error in the particular case of
2394 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2395 * have unhashed the old state_owner for us, and that we can
2396 * therefore safely retry using a new one. We should still warn
2397 * the user though...
2399 if (status == -NFS4ERR_BAD_SEQID) {
2400 pr_warn_ratelimited("NFS: v4 server %s "
2401 " returned a bad sequence-id error!\n",
2402 NFS_SERVER(dir)->nfs_client->cl_hostname);
2403 exception.retry = 1;
2407 * BAD_STATEID on OPEN means that the server cancelled our
2408 * state before it received the OPEN_CONFIRM.
2409 * Recover by retrying the request as per the discussion
2410 * on Page 181 of RFC3530.
2412 if (status == -NFS4ERR_BAD_STATEID) {
2413 exception.retry = 1;
2416 if (status == -EAGAIN) {
2417 /* We must have found a delegation */
2418 exception.retry = 1;
2421 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2423 res = ERR_PTR(nfs4_handle_exception(server,
2424 status, &exception));
2425 } while (exception.retry);
2429 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2430 struct nfs_fattr *fattr, struct iattr *sattr,
2431 struct nfs4_state *state, struct nfs4_label *ilabel,
2432 struct nfs4_label *olabel)
2434 struct nfs_server *server = NFS_SERVER(inode);
2435 struct nfs_setattrargs arg = {
2436 .fh = NFS_FH(inode),
2439 .bitmask = server->attr_bitmask,
2442 struct nfs_setattrres res = {
2447 struct rpc_message msg = {
2448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2453 unsigned long timestamp = jiffies;
2458 arg.bitmask = nfs4_bitmask(server, ilabel);
2460 arg.bitmask = nfs4_bitmask(server, olabel);
2462 nfs_fattr_init(fattr);
2464 /* Servers should only apply open mode checks for file size changes */
2465 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2466 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2468 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2469 /* Use that stateid */
2470 } else if (truncate && state != NULL) {
2471 struct nfs_lockowner lockowner = {
2472 .l_owner = current->files,
2473 .l_pid = current->tgid,
2475 if (!nfs4_valid_open_stateid(state))
2477 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2478 &lockowner) == -EIO)
2481 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2483 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2484 if (status == 0 && state != NULL)
2485 renew_lease(server, timestamp);
2489 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2490 struct nfs_fattr *fattr, struct iattr *sattr,
2491 struct nfs4_state *state, struct nfs4_label *ilabel,
2492 struct nfs4_label *olabel)
2494 struct nfs_server *server = NFS_SERVER(inode);
2495 struct nfs4_exception exception = {
2501 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2502 trace_nfs4_setattr(inode, err);
2504 case -NFS4ERR_OPENMODE:
2505 if (!(sattr->ia_valid & ATTR_SIZE)) {
2506 pr_warn_once("NFSv4: server %s is incorrectly "
2507 "applying open mode checks to "
2508 "a SETATTR that is not "
2509 "changing file size.\n",
2510 server->nfs_client->cl_hostname);
2512 if (state && !(state->state & FMODE_WRITE)) {
2514 if (sattr->ia_valid & ATTR_OPEN)
2519 err = nfs4_handle_exception(server, err, &exception);
2520 } while (exception.retry);
2525 struct nfs4_closedata {
2526 struct inode *inode;
2527 struct nfs4_state *state;
2528 struct nfs_closeargs arg;
2529 struct nfs_closeres res;
2530 struct nfs_fattr fattr;
2531 unsigned long timestamp;
2536 static void nfs4_free_closedata(void *data)
2538 struct nfs4_closedata *calldata = data;
2539 struct nfs4_state_owner *sp = calldata->state->owner;
2540 struct super_block *sb = calldata->state->inode->i_sb;
2543 pnfs_roc_release(calldata->state->inode);
2544 nfs4_put_open_state(calldata->state);
2545 nfs_free_seqid(calldata->arg.seqid);
2546 nfs4_put_state_owner(sp);
2547 nfs_sb_deactive(sb);
2551 static void nfs4_close_done(struct rpc_task *task, void *data)
2553 struct nfs4_closedata *calldata = data;
2554 struct nfs4_state *state = calldata->state;
2555 struct nfs_server *server = NFS_SERVER(calldata->inode);
2557 dprintk("%s: begin!\n", __func__);
2558 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2560 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2561 /* hmm. we are done with the inode, and in the process of freeing
2562 * the state_owner. we keep this around to process errors
2564 switch (task->tk_status) {
2567 pnfs_roc_set_barrier(state->inode,
2568 calldata->roc_barrier);
2569 nfs_clear_open_stateid(state, &calldata->res.stateid, 0);
2570 renew_lease(server, calldata->timestamp);
2572 case -NFS4ERR_ADMIN_REVOKED:
2573 case -NFS4ERR_STALE_STATEID:
2574 case -NFS4ERR_OLD_STATEID:
2575 case -NFS4ERR_BAD_STATEID:
2576 case -NFS4ERR_EXPIRED:
2577 if (calldata->arg.fmode == 0)
2580 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
2581 rpc_restart_call_prepare(task);
2585 nfs_clear_open_stateid(state, NULL, calldata->arg.fmode);
2587 nfs_release_seqid(calldata->arg.seqid);
2588 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2589 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2592 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2594 struct nfs4_closedata *calldata = data;
2595 struct nfs4_state *state = calldata->state;
2596 struct inode *inode = calldata->inode;
2599 dprintk("%s: begin!\n", __func__);
2600 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2603 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2604 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2605 spin_lock(&state->owner->so_lock);
2606 /* Calculate the change in open mode */
2607 if (state->n_rdwr == 0) {
2608 if (state->n_rdonly == 0) {
2609 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2610 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2611 calldata->arg.fmode &= ~FMODE_READ;
2613 if (state->n_wronly == 0) {
2614 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2615 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2616 calldata->arg.fmode &= ~FMODE_WRITE;
2619 if (!nfs4_valid_open_stateid(state))
2621 spin_unlock(&state->owner->so_lock);
2624 /* Note: exit _without_ calling nfs4_close_done */
2628 if (calldata->arg.fmode == 0) {
2629 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2630 if (calldata->roc &&
2631 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2632 nfs_release_seqid(calldata->arg.seqid);
2637 nfs_fattr_init(calldata->res.fattr);
2638 calldata->timestamp = jiffies;
2639 if (nfs4_setup_sequence(NFS_SERVER(inode),
2640 &calldata->arg.seq_args,
2641 &calldata->res.seq_res,
2643 nfs_release_seqid(calldata->arg.seqid);
2644 dprintk("%s: done!\n", __func__);
2647 task->tk_action = NULL;
2649 nfs4_sequence_done(task, &calldata->res.seq_res);
2652 static const struct rpc_call_ops nfs4_close_ops = {
2653 .rpc_call_prepare = nfs4_close_prepare,
2654 .rpc_call_done = nfs4_close_done,
2655 .rpc_release = nfs4_free_closedata,
2659 * It is possible for data to be read/written from a mem-mapped file
2660 * after the sys_close call (which hits the vfs layer as a flush).
2661 * This means that we can't safely call nfsv4 close on a file until
2662 * the inode is cleared. This in turn means that we are not good
2663 * NFSv4 citizens - we do not indicate to the server to update the file's
2664 * share state even when we are done with one of the three share
2665 * stateid's in the inode.
2667 * NOTE: Caller must be holding the sp->so_owner semaphore!
2669 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2671 struct nfs_server *server = NFS_SERVER(state->inode);
2672 struct nfs4_closedata *calldata;
2673 struct nfs4_state_owner *sp = state->owner;
2674 struct rpc_task *task;
2675 struct rpc_message msg = {
2676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2677 .rpc_cred = state->owner->so_cred,
2679 struct rpc_task_setup task_setup_data = {
2680 .rpc_client = server->client,
2681 .rpc_message = &msg,
2682 .callback_ops = &nfs4_close_ops,
2683 .workqueue = nfsiod_workqueue,
2684 .flags = RPC_TASK_ASYNC,
2686 int status = -ENOMEM;
2688 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2689 &task_setup_data.rpc_client, &msg);
2691 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2692 if (calldata == NULL)
2694 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2695 calldata->inode = state->inode;
2696 calldata->state = state;
2697 calldata->arg.fh = NFS_FH(state->inode);
2698 calldata->arg.stateid = &state->open_stateid;
2699 /* Serialization for the sequence id */
2700 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2701 if (calldata->arg.seqid == NULL)
2702 goto out_free_calldata;
2703 calldata->arg.fmode = 0;
2704 calldata->arg.bitmask = server->cache_consistency_bitmask;
2705 calldata->res.fattr = &calldata->fattr;
2706 calldata->res.seqid = calldata->arg.seqid;
2707 calldata->res.server = server;
2708 calldata->roc = pnfs_roc(state->inode);
2709 nfs_sb_active(calldata->inode->i_sb);
2711 msg.rpc_argp = &calldata->arg;
2712 msg.rpc_resp = &calldata->res;
2713 task_setup_data.callback_data = calldata;
2714 task = rpc_run_task(&task_setup_data);
2716 return PTR_ERR(task);
2719 status = rpc_wait_for_completion_task(task);
2725 nfs4_put_open_state(state);
2726 nfs4_put_state_owner(sp);
2730 static struct inode *
2731 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2732 int open_flags, struct iattr *attr, int *opened)
2734 struct nfs4_state *state;
2735 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2737 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2739 /* Protect against concurrent sillydeletes */
2740 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2742 nfs4_label_release_security(label);
2745 return ERR_CAST(state);
2746 return state->inode;
2749 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2751 if (ctx->state == NULL)
2754 nfs4_close_sync(ctx->state, ctx->mode);
2756 nfs4_close_state(ctx->state, ctx->mode);
2759 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2760 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2761 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2763 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2765 struct nfs4_server_caps_arg args = {
2768 struct nfs4_server_caps_res res = {};
2769 struct rpc_message msg = {
2770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2776 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2778 /* Sanity check the server answers */
2779 switch (server->nfs_client->cl_minorversion) {
2781 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2782 res.attr_bitmask[2] = 0;
2785 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2788 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2790 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2791 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2792 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2793 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2794 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2795 NFS_CAP_CTIME|NFS_CAP_MTIME|
2796 NFS_CAP_SECURITY_LABEL);
2797 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2798 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2799 server->caps |= NFS_CAP_ACLS;
2800 if (res.has_links != 0)
2801 server->caps |= NFS_CAP_HARDLINKS;
2802 if (res.has_symlinks != 0)
2803 server->caps |= NFS_CAP_SYMLINKS;
2804 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2805 server->caps |= NFS_CAP_FILEID;
2806 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2807 server->caps |= NFS_CAP_MODE;
2808 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2809 server->caps |= NFS_CAP_NLINK;
2810 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2811 server->caps |= NFS_CAP_OWNER;
2812 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2813 server->caps |= NFS_CAP_OWNER_GROUP;
2814 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2815 server->caps |= NFS_CAP_ATIME;
2816 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2817 server->caps |= NFS_CAP_CTIME;
2818 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2819 server->caps |= NFS_CAP_MTIME;
2820 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2821 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2822 server->caps |= NFS_CAP_SECURITY_LABEL;
2824 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2825 sizeof(server->attr_bitmask));
2826 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2828 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2829 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2830 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2831 server->cache_consistency_bitmask[2] = 0;
2832 server->acl_bitmask = res.acl_bitmask;
2833 server->fh_expire_type = res.fh_expire_type;
2839 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2841 struct nfs4_exception exception = { };
2844 err = nfs4_handle_exception(server,
2845 _nfs4_server_capabilities(server, fhandle),
2847 } while (exception.retry);
2851 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2852 struct nfs_fsinfo *info)
2855 struct nfs4_lookup_root_arg args = {
2858 struct nfs4_lookup_res res = {
2860 .fattr = info->fattr,
2863 struct rpc_message msg = {
2864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2869 bitmask[0] = nfs4_fattr_bitmap[0];
2870 bitmask[1] = nfs4_fattr_bitmap[1];
2872 * Process the label in the upcoming getfattr
2874 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2876 nfs_fattr_init(info->fattr);
2877 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2880 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2881 struct nfs_fsinfo *info)
2883 struct nfs4_exception exception = { };
2886 err = _nfs4_lookup_root(server, fhandle, info);
2887 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2890 case -NFS4ERR_WRONGSEC:
2893 err = nfs4_handle_exception(server, err, &exception);
2895 } while (exception.retry);
2900 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2901 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2903 struct rpc_auth_create_args auth_args = {
2904 .pseudoflavor = flavor,
2906 struct rpc_auth *auth;
2909 auth = rpcauth_create(&auth_args, server->client);
2914 ret = nfs4_lookup_root(server, fhandle, info);
2920 * Retry pseudoroot lookup with various security flavors. We do this when:
2922 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2923 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2925 * Returns zero on success, or a negative NFS4ERR value, or a
2926 * negative errno value.
2928 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2929 struct nfs_fsinfo *info)
2931 /* Per 3530bis 15.33.5 */
2932 static const rpc_authflavor_t flav_array[] = {
2936 RPC_AUTH_UNIX, /* courtesy */
2939 int status = -EPERM;
2942 if (server->auth_info.flavor_len > 0) {
2943 /* try each flavor specified by user */
2944 for (i = 0; i < server->auth_info.flavor_len; i++) {
2945 status = nfs4_lookup_root_sec(server, fhandle, info,
2946 server->auth_info.flavors[i]);
2947 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2952 /* no flavors specified by user, try default list */
2953 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2954 status = nfs4_lookup_root_sec(server, fhandle, info,
2956 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2963 * -EACCESS could mean that the user doesn't have correct permissions
2964 * to access the mount. It could also mean that we tried to mount
2965 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2966 * existing mount programs don't handle -EACCES very well so it should
2967 * be mapped to -EPERM instead.
2969 if (status == -EACCES)
2974 static int nfs4_do_find_root_sec(struct nfs_server *server,
2975 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2977 int mv = server->nfs_client->cl_minorversion;
2978 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2982 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2983 * @server: initialized nfs_server handle
2984 * @fhandle: we fill in the pseudo-fs root file handle
2985 * @info: we fill in an FSINFO struct
2986 * @auth_probe: probe the auth flavours
2988 * Returns zero on success, or a negative errno.
2990 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2991 struct nfs_fsinfo *info,
2996 switch (auth_probe) {
2998 status = nfs4_lookup_root(server, fhandle, info);
2999 if (status != -NFS4ERR_WRONGSEC)
3002 status = nfs4_do_find_root_sec(server, fhandle, info);
3006 status = nfs4_server_capabilities(server, fhandle);
3008 status = nfs4_do_fsinfo(server, fhandle, info);
3010 return nfs4_map_errors(status);
3013 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3014 struct nfs_fsinfo *info)
3017 struct nfs_fattr *fattr = info->fattr;
3018 struct nfs4_label *label = NULL;
3020 error = nfs4_server_capabilities(server, mntfh);
3022 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3026 label = nfs4_label_alloc(server, GFP_KERNEL);
3028 return PTR_ERR(label);
3030 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3032 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3033 goto err_free_label;
3036 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3037 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3038 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3041 nfs4_label_free(label);
3047 * Get locations and (maybe) other attributes of a referral.
3048 * Note that we'll actually follow the referral later when
3049 * we detect fsid mismatch in inode revalidation
3051 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3052 const struct qstr *name, struct nfs_fattr *fattr,
3053 struct nfs_fh *fhandle)
3055 int status = -ENOMEM;
3056 struct page *page = NULL;
3057 struct nfs4_fs_locations *locations = NULL;
3059 page = alloc_page(GFP_KERNEL);
3062 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3063 if (locations == NULL)
3066 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3071 * If the fsid didn't change, this is a migration event, not a
3072 * referral. Cause us to drop into the exception handler, which
3073 * will kick off migration recovery.
3075 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3076 dprintk("%s: server did not return a different fsid for"
3077 " a referral at %s\n", __func__, name->name);
3078 status = -NFS4ERR_MOVED;
3081 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3082 nfs_fixup_referral_attributes(&locations->fattr);
3084 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3085 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3086 memset(fhandle, 0, sizeof(struct nfs_fh));
3094 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3095 struct nfs_fattr *fattr, struct nfs4_label *label)
3097 struct nfs4_getattr_arg args = {
3099 .bitmask = server->attr_bitmask,
3101 struct nfs4_getattr_res res = {
3106 struct rpc_message msg = {
3107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3112 args.bitmask = nfs4_bitmask(server, label);
3114 nfs_fattr_init(fattr);
3115 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3118 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3119 struct nfs_fattr *fattr, struct nfs4_label *label)
3121 struct nfs4_exception exception = { };
3124 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3125 trace_nfs4_getattr(server, fhandle, fattr, err);
3126 err = nfs4_handle_exception(server, err,
3128 } while (exception.retry);
3133 * The file is not closed if it is opened due to the a request to change
3134 * the size of the file. The open call will not be needed once the
3135 * VFS layer lookup-intents are implemented.
3137 * Close is called when the inode is destroyed.
3138 * If we haven't opened the file for O_WRONLY, we
3139 * need to in the size_change case to obtain a stateid.
3142 * Because OPEN is always done by name in nfsv4, it is
3143 * possible that we opened a different file by the same
3144 * name. We can recognize this race condition, but we
3145 * can't do anything about it besides returning an error.
3147 * This will be fixed with VFS changes (lookup-intent).
3150 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3151 struct iattr *sattr)
3153 struct inode *inode = dentry->d_inode;
3154 struct rpc_cred *cred = NULL;
3155 struct nfs4_state *state = NULL;
3156 struct nfs4_label *label = NULL;
3159 if (pnfs_ld_layoutret_on_setattr(inode))
3160 pnfs_commit_and_return_layout(inode);
3162 nfs_fattr_init(fattr);
3164 /* Deal with open(O_TRUNC) */
3165 if (sattr->ia_valid & ATTR_OPEN)
3166 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3168 /* Optimization: if the end result is no change, don't RPC */
3169 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3172 /* Search for an existing open(O_WRITE) file */
3173 if (sattr->ia_valid & ATTR_FILE) {
3174 struct nfs_open_context *ctx;
3176 ctx = nfs_file_open_context(sattr->ia_file);
3183 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3185 return PTR_ERR(label);
3187 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3189 nfs_setattr_update_inode(inode, sattr);
3190 nfs_setsecurity(inode, fattr, label);
3192 nfs4_label_free(label);
3196 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3197 const struct qstr *name, struct nfs_fh *fhandle,
3198 struct nfs_fattr *fattr, struct nfs4_label *label)
3200 struct nfs_server *server = NFS_SERVER(dir);
3202 struct nfs4_lookup_arg args = {
3203 .bitmask = server->attr_bitmask,
3204 .dir_fh = NFS_FH(dir),
3207 struct nfs4_lookup_res res = {
3213 struct rpc_message msg = {
3214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3219 args.bitmask = nfs4_bitmask(server, label);
3221 nfs_fattr_init(fattr);
3223 dprintk("NFS call lookup %s\n", name->name);
3224 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3225 dprintk("NFS reply lookup: %d\n", status);
3229 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3231 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3232 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3233 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3237 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3238 struct qstr *name, struct nfs_fh *fhandle,
3239 struct nfs_fattr *fattr, struct nfs4_label *label)
3241 struct nfs4_exception exception = { };
3242 struct rpc_clnt *client = *clnt;
3245 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3246 trace_nfs4_lookup(dir, name, err);
3248 case -NFS4ERR_BADNAME:
3251 case -NFS4ERR_MOVED:
3252 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3254 case -NFS4ERR_WRONGSEC:
3256 if (client != *clnt)
3258 client = nfs4_negotiate_security(client, dir, name);
3260 return PTR_ERR(client);
3262 exception.retry = 1;
3265 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3267 } while (exception.retry);
3272 else if (client != *clnt)
3273 rpc_shutdown_client(client);
3278 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3279 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3280 struct nfs4_label *label)
3283 struct rpc_clnt *client = NFS_CLIENT(dir);
3285 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3286 if (client != NFS_CLIENT(dir)) {
3287 rpc_shutdown_client(client);
3288 nfs_fixup_secinfo_attributes(fattr);
3294 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3295 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3297 struct rpc_clnt *client = NFS_CLIENT(dir);
3300 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3302 return ERR_PTR(status);
3303 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3306 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3308 struct nfs_server *server = NFS_SERVER(inode);
3309 struct nfs4_accessargs args = {
3310 .fh = NFS_FH(inode),
3311 .bitmask = server->cache_consistency_bitmask,
3313 struct nfs4_accessres res = {
3316 struct rpc_message msg = {
3317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3320 .rpc_cred = entry->cred,
3322 int mode = entry->mask;
3326 * Determine which access bits we want to ask for...
3328 if (mode & MAY_READ)
3329 args.access |= NFS4_ACCESS_READ;
3330 if (S_ISDIR(inode->i_mode)) {
3331 if (mode & MAY_WRITE)
3332 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3333 if (mode & MAY_EXEC)
3334 args.access |= NFS4_ACCESS_LOOKUP;
3336 if (mode & MAY_WRITE)
3337 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3338 if (mode & MAY_EXEC)
3339 args.access |= NFS4_ACCESS_EXECUTE;
3342 res.fattr = nfs_alloc_fattr();
3343 if (res.fattr == NULL)
3346 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3348 nfs_access_set_mask(entry, res.access);
3349 nfs_refresh_inode(inode, res.fattr);
3351 nfs_free_fattr(res.fattr);
3355 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3357 struct nfs4_exception exception = { };
3360 err = _nfs4_proc_access(inode, entry);
3361 trace_nfs4_access(inode, err);
3362 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3364 } while (exception.retry);
3369 * TODO: For the time being, we don't try to get any attributes
3370 * along with any of the zero-copy operations READ, READDIR,
3373 * In the case of the first three, we want to put the GETATTR
3374 * after the read-type operation -- this is because it is hard
3375 * to predict the length of a GETATTR response in v4, and thus
3376 * align the READ data correctly. This means that the GETATTR
3377 * may end up partially falling into the page cache, and we should
3378 * shift it into the 'tail' of the xdr_buf before processing.
3379 * To do this efficiently, we need to know the total length
3380 * of data received, which doesn't seem to be available outside
3383 * In the case of WRITE, we also want to put the GETATTR after
3384 * the operation -- in this case because we want to make sure
3385 * we get the post-operation mtime and size.
3387 * Both of these changes to the XDR layer would in fact be quite
3388 * minor, but I decided to leave them for a subsequent patch.
3390 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3391 unsigned int pgbase, unsigned int pglen)
3393 struct nfs4_readlink args = {
3394 .fh = NFS_FH(inode),
3399 struct nfs4_readlink_res res;
3400 struct rpc_message msg = {
3401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3406 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3409 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3410 unsigned int pgbase, unsigned int pglen)
3412 struct nfs4_exception exception = { };
3415 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3416 trace_nfs4_readlink(inode, err);
3417 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3419 } while (exception.retry);
3424 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3427 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3430 struct nfs4_label l, *ilabel = NULL;
3431 struct nfs_open_context *ctx;
3432 struct nfs4_state *state;
3436 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3438 return PTR_ERR(ctx);
3440 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3442 sattr->ia_mode &= ~current_umask();
3443 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3444 if (IS_ERR(state)) {
3445 status = PTR_ERR(state);
3449 nfs4_label_release_security(ilabel);
3450 put_nfs_open_context(ctx);
3454 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3456 struct nfs_server *server = NFS_SERVER(dir);
3457 struct nfs_removeargs args = {
3461 struct nfs_removeres res = {
3464 struct rpc_message msg = {
3465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3471 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3473 update_changeattr(dir, &res.cinfo);
3477 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3479 struct nfs4_exception exception = { };
3482 err = _nfs4_proc_remove(dir, name);
3483 trace_nfs4_remove(dir, name, err);
3484 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3486 } while (exception.retry);
3490 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3492 struct nfs_server *server = NFS_SERVER(dir);
3493 struct nfs_removeargs *args = msg->rpc_argp;
3494 struct nfs_removeres *res = msg->rpc_resp;
3496 res->server = server;
3497 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3498 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3500 nfs_fattr_init(res->dir_attr);
3503 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3505 nfs4_setup_sequence(NFS_SERVER(data->dir),
3506 &data->args.seq_args,
3511 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3513 struct nfs_unlinkdata *data = task->tk_calldata;
3514 struct nfs_removeres *res = &data->res;
3516 if (!nfs4_sequence_done(task, &res->seq_res))
3518 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3520 update_changeattr(dir, &res->cinfo);
3524 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3526 struct nfs_server *server = NFS_SERVER(dir);
3527 struct nfs_renameargs *arg = msg->rpc_argp;
3528 struct nfs_renameres *res = msg->rpc_resp;
3530 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3531 res->server = server;
3532 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3535 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3537 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3538 &data->args.seq_args,
3543 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3544 struct inode *new_dir)
3546 struct nfs_renamedata *data = task->tk_calldata;
3547 struct nfs_renameres *res = &data->res;
3549 if (!nfs4_sequence_done(task, &res->seq_res))
3551 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3554 update_changeattr(old_dir, &res->old_cinfo);
3555 update_changeattr(new_dir, &res->new_cinfo);
3559 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3561 struct nfs_server *server = NFS_SERVER(inode);
3562 struct nfs4_link_arg arg = {
3563 .fh = NFS_FH(inode),
3564 .dir_fh = NFS_FH(dir),
3566 .bitmask = server->attr_bitmask,
3568 struct nfs4_link_res res = {
3572 struct rpc_message msg = {
3573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3577 int status = -ENOMEM;
3579 res.fattr = nfs_alloc_fattr();
3580 if (res.fattr == NULL)
3583 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3584 if (IS_ERR(res.label)) {
3585 status = PTR_ERR(res.label);
3588 arg.bitmask = nfs4_bitmask(server, res.label);
3590 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3592 update_changeattr(dir, &res.cinfo);
3593 status = nfs_post_op_update_inode(inode, res.fattr);
3595 nfs_setsecurity(inode, res.fattr, res.label);
3599 nfs4_label_free(res.label);
3602 nfs_free_fattr(res.fattr);
3606 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3608 struct nfs4_exception exception = { };
3611 err = nfs4_handle_exception(NFS_SERVER(inode),
3612 _nfs4_proc_link(inode, dir, name),
3614 } while (exception.retry);
3618 struct nfs4_createdata {
3619 struct rpc_message msg;
3620 struct nfs4_create_arg arg;
3621 struct nfs4_create_res res;
3623 struct nfs_fattr fattr;
3624 struct nfs4_label *label;
3627 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3628 struct qstr *name, struct iattr *sattr, u32 ftype)
3630 struct nfs4_createdata *data;
3632 data = kzalloc(sizeof(*data), GFP_KERNEL);
3634 struct nfs_server *server = NFS_SERVER(dir);
3636 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3637 if (IS_ERR(data->label))
3640 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3641 data->msg.rpc_argp = &data->arg;
3642 data->msg.rpc_resp = &data->res;
3643 data->arg.dir_fh = NFS_FH(dir);
3644 data->arg.server = server;
3645 data->arg.name = name;
3646 data->arg.attrs = sattr;
3647 data->arg.ftype = ftype;
3648 data->arg.bitmask = nfs4_bitmask(server, data->label);
3649 data->res.server = server;
3650 data->res.fh = &data->fh;
3651 data->res.fattr = &data->fattr;
3652 data->res.label = data->label;
3653 nfs_fattr_init(data->res.fattr);
3661 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3663 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3664 &data->arg.seq_args, &data->res.seq_res, 1);
3666 update_changeattr(dir, &data->res.dir_cinfo);
3667 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3672 static void nfs4_free_createdata(struct nfs4_createdata *data)
3674 nfs4_label_free(data->label);
3678 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3679 struct page *page, unsigned int len, struct iattr *sattr,
3680 struct nfs4_label *label)
3682 struct nfs4_createdata *data;
3683 int status = -ENAMETOOLONG;
3685 if (len > NFS4_MAXPATHLEN)
3689 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3693 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3694 data->arg.u.symlink.pages = &page;
3695 data->arg.u.symlink.len = len;
3696 data->arg.label = label;
3698 status = nfs4_do_create(dir, dentry, data);
3700 nfs4_free_createdata(data);
3705 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3706 struct page *page, unsigned int len, struct iattr *sattr)
3708 struct nfs4_exception exception = { };
3709 struct nfs4_label l, *label = NULL;
3712 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3715 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3716 trace_nfs4_symlink(dir, &dentry->d_name, err);
3717 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3719 } while (exception.retry);
3721 nfs4_label_release_security(label);
3725 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3726 struct iattr *sattr, struct nfs4_label *label)
3728 struct nfs4_createdata *data;
3729 int status = -ENOMEM;
3731 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3735 data->arg.label = label;
3736 status = nfs4_do_create(dir, dentry, data);
3738 nfs4_free_createdata(data);
3743 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3744 struct iattr *sattr)
3746 struct nfs4_exception exception = { };
3747 struct nfs4_label l, *label = NULL;
3750 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3752 sattr->ia_mode &= ~current_umask();
3754 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3755 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3756 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3758 } while (exception.retry);
3759 nfs4_label_release_security(label);
3764 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3765 u64 cookie, struct page **pages, unsigned int count, int plus)
3767 struct inode *dir = dentry->d_inode;
3768 struct nfs4_readdir_arg args = {
3773 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3776 struct nfs4_readdir_res res;
3777 struct rpc_message msg = {
3778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3785 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3787 (unsigned long long)cookie);
3788 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3789 res.pgbase = args.pgbase;
3790 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3792 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3793 status += args.pgbase;
3796 nfs_invalidate_atime(dir);
3798 dprintk("%s: returns %d\n", __func__, status);
3802 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3803 u64 cookie, struct page **pages, unsigned int count, int plus)
3805 struct nfs4_exception exception = { };
3808 err = _nfs4_proc_readdir(dentry, cred, cookie,
3809 pages, count, plus);
3810 trace_nfs4_readdir(dentry->d_inode, err);
3811 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3813 } while (exception.retry);
3817 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3818 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3820 struct nfs4_createdata *data;
3821 int mode = sattr->ia_mode;
3822 int status = -ENOMEM;
3824 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3829 data->arg.ftype = NF4FIFO;
3830 else if (S_ISBLK(mode)) {
3831 data->arg.ftype = NF4BLK;
3832 data->arg.u.device.specdata1 = MAJOR(rdev);
3833 data->arg.u.device.specdata2 = MINOR(rdev);
3835 else if (S_ISCHR(mode)) {
3836 data->arg.ftype = NF4CHR;
3837 data->arg.u.device.specdata1 = MAJOR(rdev);
3838 data->arg.u.device.specdata2 = MINOR(rdev);
3839 } else if (!S_ISSOCK(mode)) {
3844 data->arg.label = label;
3845 status = nfs4_do_create(dir, dentry, data);
3847 nfs4_free_createdata(data);
3852 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3853 struct iattr *sattr, dev_t rdev)
3855 struct nfs4_exception exception = { };
3856 struct nfs4_label l, *label = NULL;
3859 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3861 sattr->ia_mode &= ~current_umask();
3863 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3864 trace_nfs4_mknod(dir, &dentry->d_name, err);
3865 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3867 } while (exception.retry);
3869 nfs4_label_release_security(label);
3874 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3875 struct nfs_fsstat *fsstat)
3877 struct nfs4_statfs_arg args = {
3879 .bitmask = server->attr_bitmask,
3881 struct nfs4_statfs_res res = {
3884 struct rpc_message msg = {
3885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3890 nfs_fattr_init(fsstat->fattr);
3891 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3894 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3896 struct nfs4_exception exception = { };
3899 err = nfs4_handle_exception(server,
3900 _nfs4_proc_statfs(server, fhandle, fsstat),
3902 } while (exception.retry);
3906 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3907 struct nfs_fsinfo *fsinfo)
3909 struct nfs4_fsinfo_arg args = {
3911 .bitmask = server->attr_bitmask,
3913 struct nfs4_fsinfo_res res = {
3916 struct rpc_message msg = {
3917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3922 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3925 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3927 struct nfs4_exception exception = { };
3928 unsigned long now = jiffies;
3932 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3933 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
3935 struct nfs_client *clp = server->nfs_client;
3937 spin_lock(&clp->cl_lock);
3938 clp->cl_lease_time = fsinfo->lease_time * HZ;
3939 clp->cl_last_renewal = now;
3940 spin_unlock(&clp->cl_lock);
3943 err = nfs4_handle_exception(server, err, &exception);
3944 } while (exception.retry);
3948 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3952 nfs_fattr_init(fsinfo->fattr);
3953 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3955 /* block layout checks this! */
3956 server->pnfs_blksize = fsinfo->blksize;
3957 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3963 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3964 struct nfs_pathconf *pathconf)
3966 struct nfs4_pathconf_arg args = {
3968 .bitmask = server->attr_bitmask,
3970 struct nfs4_pathconf_res res = {
3971 .pathconf = pathconf,
3973 struct rpc_message msg = {
3974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3979 /* None of the pathconf attributes are mandatory to implement */
3980 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3981 memset(pathconf, 0, sizeof(*pathconf));
3985 nfs_fattr_init(pathconf->fattr);
3986 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3989 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3990 struct nfs_pathconf *pathconf)
3992 struct nfs4_exception exception = { };
3996 err = nfs4_handle_exception(server,
3997 _nfs4_proc_pathconf(server, fhandle, pathconf),
3999 } while (exception.retry);
4003 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4004 const struct nfs_open_context *ctx,
4005 const struct nfs_lock_context *l_ctx,
4008 const struct nfs_lockowner *lockowner = NULL;
4011 lockowner = &l_ctx->lockowner;
4012 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4014 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4016 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4017 const struct nfs_open_context *ctx,
4018 const struct nfs_lock_context *l_ctx,
4021 nfs4_stateid current_stateid;
4023 /* If the current stateid represents a lost lock, then exit */
4024 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4026 return nfs4_stateid_match(stateid, ¤t_stateid);
4029 static bool nfs4_error_stateid_expired(int err)
4032 case -NFS4ERR_DELEG_REVOKED:
4033 case -NFS4ERR_ADMIN_REVOKED:
4034 case -NFS4ERR_BAD_STATEID:
4035 case -NFS4ERR_STALE_STATEID:
4036 case -NFS4ERR_OLD_STATEID:
4037 case -NFS4ERR_OPENMODE:
4038 case -NFS4ERR_EXPIRED:
4044 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4046 nfs_invalidate_atime(hdr->inode);
4049 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4051 struct nfs_server *server = NFS_SERVER(hdr->inode);
4053 trace_nfs4_read(hdr, task->tk_status);
4054 if (nfs4_async_handle_error(task, server,
4055 hdr->args.context->state) == -EAGAIN) {
4056 rpc_restart_call_prepare(task);
4060 __nfs4_read_done_cb(hdr);
4061 if (task->tk_status > 0)
4062 renew_lease(server, hdr->timestamp);
4066 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4067 struct nfs_pgio_args *args)
4070 if (!nfs4_error_stateid_expired(task->tk_status) ||
4071 nfs4_stateid_is_current(&args->stateid,
4076 rpc_restart_call_prepare(task);
4080 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4083 dprintk("--> %s\n", __func__);
4085 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4087 if (nfs4_read_stateid_changed(task, &hdr->args))
4089 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4090 nfs4_read_done_cb(task, hdr);
4093 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4094 struct rpc_message *msg)
4096 hdr->timestamp = jiffies;
4097 hdr->pgio_done_cb = nfs4_read_done_cb;
4098 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4099 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4102 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4103 struct nfs_pgio_header *hdr)
4105 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4106 &hdr->args.seq_args,
4110 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4111 hdr->args.lock_context,
4112 hdr->rw_ops->rw_mode) == -EIO)
4114 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4119 static int nfs4_write_done_cb(struct rpc_task *task,
4120 struct nfs_pgio_header *hdr)
4122 struct inode *inode = hdr->inode;
4124 trace_nfs4_write(hdr, task->tk_status);
4125 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4126 hdr->args.context->state) == -EAGAIN) {
4127 rpc_restart_call_prepare(task);
4130 if (task->tk_status >= 0) {
4131 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4132 nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr);
4137 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4138 struct nfs_pgio_args *args)
4141 if (!nfs4_error_stateid_expired(task->tk_status) ||
4142 nfs4_stateid_is_current(&args->stateid,
4147 rpc_restart_call_prepare(task);
4151 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4153 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4155 if (nfs4_write_stateid_changed(task, &hdr->args))
4157 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4158 nfs4_write_done_cb(task, hdr);
4162 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4164 /* Don't request attributes for pNFS or O_DIRECT writes */
4165 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4167 /* Otherwise, request attributes if and only if we don't hold
4170 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4173 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4174 struct rpc_message *msg)
4176 struct nfs_server *server = NFS_SERVER(hdr->inode);
4178 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4179 hdr->args.bitmask = NULL;
4180 hdr->res.fattr = NULL;
4182 hdr->args.bitmask = server->cache_consistency_bitmask;
4184 if (!hdr->pgio_done_cb)
4185 hdr->pgio_done_cb = nfs4_write_done_cb;
4186 hdr->res.server = server;
4187 hdr->timestamp = jiffies;
4189 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4190 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4193 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4195 nfs4_setup_sequence(NFS_SERVER(data->inode),
4196 &data->args.seq_args,
4201 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4203 struct inode *inode = data->inode;
4205 trace_nfs4_commit(data, task->tk_status);
4206 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
4207 rpc_restart_call_prepare(task);
4213 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4215 if (!nfs4_sequence_done(task, &data->res.seq_res))
4217 return data->commit_done_cb(task, data);
4220 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4222 struct nfs_server *server = NFS_SERVER(data->inode);
4224 if (data->commit_done_cb == NULL)
4225 data->commit_done_cb = nfs4_commit_done_cb;
4226 data->res.server = server;
4227 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4228 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4231 struct nfs4_renewdata {
4232 struct nfs_client *client;
4233 unsigned long timestamp;
4237 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4238 * standalone procedure for queueing an asynchronous RENEW.
4240 static void nfs4_renew_release(void *calldata)
4242 struct nfs4_renewdata *data = calldata;
4243 struct nfs_client *clp = data->client;
4245 if (atomic_read(&clp->cl_count) > 1)
4246 nfs4_schedule_state_renewal(clp);
4247 nfs_put_client(clp);
4251 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4253 struct nfs4_renewdata *data = calldata;
4254 struct nfs_client *clp = data->client;
4255 unsigned long timestamp = data->timestamp;
4257 trace_nfs4_renew_async(clp, task->tk_status);
4258 switch (task->tk_status) {
4261 case -NFS4ERR_LEASE_MOVED:
4262 nfs4_schedule_lease_moved_recovery(clp);
4265 /* Unless we're shutting down, schedule state recovery! */
4266 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4268 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4269 nfs4_schedule_lease_recovery(clp);
4272 nfs4_schedule_path_down_recovery(clp);
4274 do_renew_lease(clp, timestamp);
4277 static const struct rpc_call_ops nfs4_renew_ops = {
4278 .rpc_call_done = nfs4_renew_done,
4279 .rpc_release = nfs4_renew_release,
4282 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4284 struct rpc_message msg = {
4285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4289 struct nfs4_renewdata *data;
4291 if (renew_flags == 0)
4293 if (!atomic_inc_not_zero(&clp->cl_count))
4295 data = kmalloc(sizeof(*data), GFP_NOFS);
4299 data->timestamp = jiffies;
4300 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4301 &nfs4_renew_ops, data);
4304 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4306 struct rpc_message msg = {
4307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4311 unsigned long now = jiffies;
4314 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4317 do_renew_lease(clp, now);
4321 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4323 return server->caps & NFS_CAP_ACLS;
4326 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4327 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4330 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4332 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4333 struct page **pages, unsigned int *pgbase)
4335 struct page *newpage, **spages;
4341 len = min_t(size_t, PAGE_SIZE, buflen);
4342 newpage = alloc_page(GFP_KERNEL);
4344 if (newpage == NULL)
4346 memcpy(page_address(newpage), buf, len);
4351 } while (buflen != 0);
4357 __free_page(spages[rc-1]);
4361 struct nfs4_cached_acl {
4367 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4369 struct nfs_inode *nfsi = NFS_I(inode);
4371 spin_lock(&inode->i_lock);
4372 kfree(nfsi->nfs4_acl);
4373 nfsi->nfs4_acl = acl;
4374 spin_unlock(&inode->i_lock);
4377 static void nfs4_zap_acl_attr(struct inode *inode)
4379 nfs4_set_cached_acl(inode, NULL);
4382 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4384 struct nfs_inode *nfsi = NFS_I(inode);
4385 struct nfs4_cached_acl *acl;
4388 spin_lock(&inode->i_lock);
4389 acl = nfsi->nfs4_acl;
4392 if (buf == NULL) /* user is just asking for length */
4394 if (acl->cached == 0)
4396 ret = -ERANGE; /* see getxattr(2) man page */
4397 if (acl->len > buflen)
4399 memcpy(buf, acl->data, acl->len);
4403 spin_unlock(&inode->i_lock);
4407 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4409 struct nfs4_cached_acl *acl;
4410 size_t buflen = sizeof(*acl) + acl_len;
4412 if (buflen <= PAGE_SIZE) {
4413 acl = kmalloc(buflen, GFP_KERNEL);
4417 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4419 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4426 nfs4_set_cached_acl(inode, acl);
4430 * The getxattr API returns the required buffer length when called with a
4431 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4432 * the required buf. On a NULL buf, we send a page of data to the server
4433 * guessing that the ACL request can be serviced by a page. If so, we cache
4434 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4435 * the cache. If not so, we throw away the page, and cache the required
4436 * length. The next getxattr call will then produce another round trip to
4437 * the server, this time with the input buf of the required size.
4439 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4441 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4442 struct nfs_getaclargs args = {
4443 .fh = NFS_FH(inode),
4447 struct nfs_getaclres res = {
4450 struct rpc_message msg = {
4451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4455 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4456 int ret = -ENOMEM, i;
4458 /* As long as we're doing a round trip to the server anyway,
4459 * let's be prepared for a page of acl data. */
4462 if (npages > ARRAY_SIZE(pages))
4465 for (i = 0; i < npages; i++) {
4466 pages[i] = alloc_page(GFP_KERNEL);
4471 /* for decoding across pages */
4472 res.acl_scratch = alloc_page(GFP_KERNEL);
4473 if (!res.acl_scratch)
4476 args.acl_len = npages * PAGE_SIZE;
4477 args.acl_pgbase = 0;
4479 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4480 __func__, buf, buflen, npages, args.acl_len);
4481 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4482 &msg, &args.seq_args, &res.seq_res, 0);
4486 /* Handle the case where the passed-in buffer is too short */
4487 if (res.acl_flags & NFS4_ACL_TRUNC) {
4488 /* Did the user only issue a request for the acl length? */
4494 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4496 if (res.acl_len > buflen) {
4500 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4505 for (i = 0; i < npages; i++)
4507 __free_page(pages[i]);
4508 if (res.acl_scratch)
4509 __free_page(res.acl_scratch);
4513 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4515 struct nfs4_exception exception = { };
4518 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4519 trace_nfs4_get_acl(inode, ret);
4522 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4523 } while (exception.retry);
4527 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4529 struct nfs_server *server = NFS_SERVER(inode);
4532 if (!nfs4_server_supports_acls(server))
4534 ret = nfs_revalidate_inode(server, inode);
4537 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4538 nfs_zap_acl_cache(inode);
4539 ret = nfs4_read_cached_acl(inode, buf, buflen);
4541 /* -ENOENT is returned if there is no ACL or if there is an ACL
4542 * but no cached acl data, just the acl length */
4544 return nfs4_get_acl_uncached(inode, buf, buflen);
4547 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4549 struct nfs_server *server = NFS_SERVER(inode);
4550 struct page *pages[NFS4ACL_MAXPAGES];
4551 struct nfs_setaclargs arg = {
4552 .fh = NFS_FH(inode),
4556 struct nfs_setaclres res;
4557 struct rpc_message msg = {
4558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4562 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4565 if (!nfs4_server_supports_acls(server))
4567 if (npages > ARRAY_SIZE(pages))
4569 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4572 nfs4_inode_return_delegation(inode);
4573 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4576 * Free each page after tx, so the only ref left is
4577 * held by the network stack
4580 put_page(pages[i-1]);
4583 * Acl update can result in inode attribute update.
4584 * so mark the attribute cache invalid.
4586 spin_lock(&inode->i_lock);
4587 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4588 spin_unlock(&inode->i_lock);
4589 nfs_access_zap_cache(inode);
4590 nfs_zap_acl_cache(inode);
4594 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4596 struct nfs4_exception exception = { };
4599 err = __nfs4_proc_set_acl(inode, buf, buflen);
4600 trace_nfs4_set_acl(inode, err);
4601 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4603 } while (exception.retry);
4607 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4608 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4611 struct nfs_server *server = NFS_SERVER(inode);
4612 struct nfs_fattr fattr;
4613 struct nfs4_label label = {0, 0, buflen, buf};
4615 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4616 struct nfs4_getattr_arg arg = {
4617 .fh = NFS_FH(inode),
4620 struct nfs4_getattr_res res = {
4625 struct rpc_message msg = {
4626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4632 nfs_fattr_init(&fattr);
4634 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4637 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4639 if (buflen < label.len)
4644 static int nfs4_get_security_label(struct inode *inode, void *buf,
4647 struct nfs4_exception exception = { };
4650 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4654 err = _nfs4_get_security_label(inode, buf, buflen);
4655 trace_nfs4_get_security_label(inode, err);
4656 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4658 } while (exception.retry);
4662 static int _nfs4_do_set_security_label(struct inode *inode,
4663 struct nfs4_label *ilabel,
4664 struct nfs_fattr *fattr,
4665 struct nfs4_label *olabel)
4668 struct iattr sattr = {0};
4669 struct nfs_server *server = NFS_SERVER(inode);
4670 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4671 struct nfs_setattrargs arg = {
4672 .fh = NFS_FH(inode),
4678 struct nfs_setattrres res = {
4683 struct rpc_message msg = {
4684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4690 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4692 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4694 dprintk("%s failed: %d\n", __func__, status);
4699 static int nfs4_do_set_security_label(struct inode *inode,
4700 struct nfs4_label *ilabel,
4701 struct nfs_fattr *fattr,
4702 struct nfs4_label *olabel)
4704 struct nfs4_exception exception = { };
4708 err = _nfs4_do_set_security_label(inode, ilabel,
4710 trace_nfs4_set_security_label(inode, err);
4711 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4713 } while (exception.retry);
4718 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4720 struct nfs4_label ilabel, *olabel = NULL;
4721 struct nfs_fattr fattr;
4722 struct rpc_cred *cred;
4723 struct inode *inode = dentry->d_inode;
4726 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4729 nfs_fattr_init(&fattr);
4733 ilabel.label = (char *)buf;
4734 ilabel.len = buflen;
4736 cred = rpc_lookup_cred();
4738 return PTR_ERR(cred);
4740 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4741 if (IS_ERR(olabel)) {
4742 status = -PTR_ERR(olabel);
4746 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4748 nfs_setsecurity(inode, &fattr, olabel);
4750 nfs4_label_free(olabel);
4755 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4759 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4761 struct nfs_client *clp = server->nfs_client;
4763 if (task->tk_status >= 0)
4765 switch(task->tk_status) {
4766 case -NFS4ERR_DELEG_REVOKED:
4767 case -NFS4ERR_ADMIN_REVOKED:
4768 case -NFS4ERR_BAD_STATEID:
4771 nfs_remove_bad_delegation(state->inode);
4772 case -NFS4ERR_OPENMODE:
4775 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4776 goto recovery_failed;
4777 goto wait_on_recovery;
4778 case -NFS4ERR_EXPIRED:
4779 if (state != NULL) {
4780 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4781 goto recovery_failed;
4783 case -NFS4ERR_STALE_STATEID:
4784 case -NFS4ERR_STALE_CLIENTID:
4785 nfs4_schedule_lease_recovery(clp);
4786 goto wait_on_recovery;
4787 case -NFS4ERR_MOVED:
4788 if (nfs4_schedule_migration_recovery(server) < 0)
4789 goto recovery_failed;
4790 goto wait_on_recovery;
4791 case -NFS4ERR_LEASE_MOVED:
4792 nfs4_schedule_lease_moved_recovery(clp);
4793 goto wait_on_recovery;
4794 #if defined(CONFIG_NFS_V4_1)
4795 case -NFS4ERR_BADSESSION:
4796 case -NFS4ERR_BADSLOT:
4797 case -NFS4ERR_BAD_HIGH_SLOT:
4798 case -NFS4ERR_DEADSESSION:
4799 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4800 case -NFS4ERR_SEQ_FALSE_RETRY:
4801 case -NFS4ERR_SEQ_MISORDERED:
4802 dprintk("%s ERROR %d, Reset session\n", __func__,
4804 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4805 goto wait_on_recovery;
4806 #endif /* CONFIG_NFS_V4_1 */
4807 case -NFS4ERR_DELAY:
4808 nfs_inc_server_stats(server, NFSIOS_DELAY);
4809 case -NFS4ERR_GRACE:
4810 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4811 case -NFS4ERR_RETRY_UNCACHED_REP:
4812 case -NFS4ERR_OLD_STATEID:
4815 task->tk_status = nfs4_map_errors(task->tk_status);
4818 task->tk_status = -EIO;
4821 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4822 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4823 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4824 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4825 goto recovery_failed;
4827 task->tk_status = 0;
4831 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4832 nfs4_verifier *bootverf)
4836 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4837 /* An impossible timestamp guarantees this value
4838 * will never match a generated boot time. */
4840 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4842 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4843 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4844 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4846 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4850 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4851 char *buf, size_t len)
4853 unsigned int result;
4856 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4858 rpc_peeraddr2str(clp->cl_rpcclient,
4860 rpc_peeraddr2str(clp->cl_rpcclient,
4861 RPC_DISPLAY_PROTO));
4867 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4868 char *buf, size_t len)
4870 const char *nodename = clp->cl_rpcclient->cl_nodename;
4872 if (nfs4_client_id_uniquifier[0] != '\0')
4873 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4874 clp->rpc_ops->version,
4875 clp->cl_minorversion,
4876 nfs4_client_id_uniquifier,
4878 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4879 clp->rpc_ops->version, clp->cl_minorversion,
4884 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4885 * services. Advertise one based on the address family of the
4889 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4891 if (strchr(clp->cl_ipaddr, ':') != NULL)
4892 return scnprintf(buf, len, "tcp6");
4894 return scnprintf(buf, len, "tcp");
4898 * nfs4_proc_setclientid - Negotiate client ID
4899 * @clp: state data structure
4900 * @program: RPC program for NFSv4 callback service
4901 * @port: IP port number for NFS4 callback service
4902 * @cred: RPC credential to use for this call
4903 * @res: where to place the result
4905 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4907 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4908 unsigned short port, struct rpc_cred *cred,
4909 struct nfs4_setclientid_res *res)
4911 nfs4_verifier sc_verifier;
4912 struct nfs4_setclientid setclientid = {
4913 .sc_verifier = &sc_verifier,
4915 .sc_cb_ident = clp->cl_cb_ident,
4917 struct rpc_message msg = {
4918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4919 .rpc_argp = &setclientid,
4925 /* nfs_client_id4 */
4926 nfs4_init_boot_verifier(clp, &sc_verifier);
4927 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4928 setclientid.sc_name_len =
4929 nfs4_init_uniform_client_string(clp,
4930 setclientid.sc_name,
4931 sizeof(setclientid.sc_name));
4933 setclientid.sc_name_len =
4934 nfs4_init_nonuniform_client_string(clp,
4935 setclientid.sc_name,
4936 sizeof(setclientid.sc_name));
4938 setclientid.sc_netid_len =
4939 nfs4_init_callback_netid(clp,
4940 setclientid.sc_netid,
4941 sizeof(setclientid.sc_netid));
4942 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4943 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4944 clp->cl_ipaddr, port >> 8, port & 255);
4946 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4947 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4948 setclientid.sc_name_len, setclientid.sc_name);
4949 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4950 trace_nfs4_setclientid(clp, status);
4951 dprintk("NFS reply setclientid: %d\n", status);
4956 * nfs4_proc_setclientid_confirm - Confirm client ID
4957 * @clp: state data structure
4958 * @res: result of a previous SETCLIENTID
4959 * @cred: RPC credential to use for this call
4961 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4963 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4964 struct nfs4_setclientid_res *arg,
4965 struct rpc_cred *cred)
4967 struct rpc_message msg = {
4968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4974 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4975 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4977 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4978 trace_nfs4_setclientid_confirm(clp, status);
4979 dprintk("NFS reply setclientid_confirm: %d\n", status);
4983 struct nfs4_delegreturndata {
4984 struct nfs4_delegreturnargs args;
4985 struct nfs4_delegreturnres res;
4987 nfs4_stateid stateid;
4988 unsigned long timestamp;
4989 struct nfs_fattr fattr;
4993 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4995 struct nfs4_delegreturndata *data = calldata;
4997 if (!nfs4_sequence_done(task, &data->res.seq_res))
5000 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5001 switch (task->tk_status) {
5003 renew_lease(data->res.server, data->timestamp);
5005 case -NFS4ERR_ADMIN_REVOKED:
5006 case -NFS4ERR_DELEG_REVOKED:
5007 case -NFS4ERR_BAD_STATEID:
5008 case -NFS4ERR_OLD_STATEID:
5009 case -NFS4ERR_STALE_STATEID:
5010 case -NFS4ERR_EXPIRED:
5011 task->tk_status = 0;
5014 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5016 rpc_restart_call_prepare(task);
5020 data->rpc_status = task->tk_status;
5023 static void nfs4_delegreturn_release(void *calldata)
5028 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5030 struct nfs4_delegreturndata *d_data;
5032 d_data = (struct nfs4_delegreturndata *)data;
5034 nfs4_setup_sequence(d_data->res.server,
5035 &d_data->args.seq_args,
5036 &d_data->res.seq_res,
5040 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5041 .rpc_call_prepare = nfs4_delegreturn_prepare,
5042 .rpc_call_done = nfs4_delegreturn_done,
5043 .rpc_release = nfs4_delegreturn_release,
5046 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5048 struct nfs4_delegreturndata *data;
5049 struct nfs_server *server = NFS_SERVER(inode);
5050 struct rpc_task *task;
5051 struct rpc_message msg = {
5052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5055 struct rpc_task_setup task_setup_data = {
5056 .rpc_client = server->client,
5057 .rpc_message = &msg,
5058 .callback_ops = &nfs4_delegreturn_ops,
5059 .flags = RPC_TASK_ASYNC,
5063 data = kzalloc(sizeof(*data), GFP_NOFS);
5066 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5067 data->args.fhandle = &data->fh;
5068 data->args.stateid = &data->stateid;
5069 data->args.bitmask = server->cache_consistency_bitmask;
5070 nfs_copy_fh(&data->fh, NFS_FH(inode));
5071 nfs4_stateid_copy(&data->stateid, stateid);
5072 data->res.fattr = &data->fattr;
5073 data->res.server = server;
5074 nfs_fattr_init(data->res.fattr);
5075 data->timestamp = jiffies;
5076 data->rpc_status = 0;
5078 task_setup_data.callback_data = data;
5079 msg.rpc_argp = &data->args;
5080 msg.rpc_resp = &data->res;
5081 task = rpc_run_task(&task_setup_data);
5083 return PTR_ERR(task);
5086 status = nfs4_wait_for_completion_rpc_task(task);
5089 status = data->rpc_status;
5091 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5093 nfs_refresh_inode(inode, &data->fattr);
5099 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5101 struct nfs_server *server = NFS_SERVER(inode);
5102 struct nfs4_exception exception = { };
5105 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5106 trace_nfs4_delegreturn(inode, err);
5108 case -NFS4ERR_STALE_STATEID:
5109 case -NFS4ERR_EXPIRED:
5113 err = nfs4_handle_exception(server, err, &exception);
5114 } while (exception.retry);
5118 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5119 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5122 * sleep, with exponential backoff, and retry the LOCK operation.
5124 static unsigned long
5125 nfs4_set_lock_task_retry(unsigned long timeout)
5127 freezable_schedule_timeout_killable_unsafe(timeout);
5129 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5130 return NFS4_LOCK_MAXTIMEOUT;
5134 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5136 struct inode *inode = state->inode;
5137 struct nfs_server *server = NFS_SERVER(inode);
5138 struct nfs_client *clp = server->nfs_client;
5139 struct nfs_lockt_args arg = {
5140 .fh = NFS_FH(inode),
5143 struct nfs_lockt_res res = {
5146 struct rpc_message msg = {
5147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5150 .rpc_cred = state->owner->so_cred,
5152 struct nfs4_lock_state *lsp;
5155 arg.lock_owner.clientid = clp->cl_clientid;
5156 status = nfs4_set_lock_state(state, request);
5159 lsp = request->fl_u.nfs4_fl.owner;
5160 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5161 arg.lock_owner.s_dev = server->s_dev;
5162 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5165 request->fl_type = F_UNLCK;
5167 case -NFS4ERR_DENIED:
5170 request->fl_ops->fl_release_private(request);
5171 request->fl_ops = NULL;
5176 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5178 struct nfs4_exception exception = { };
5182 err = _nfs4_proc_getlk(state, cmd, request);
5183 trace_nfs4_get_lock(request, state, cmd, err);
5184 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5186 } while (exception.retry);
5190 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5193 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5195 res = posix_lock_file_wait(file, fl);
5198 res = flock_lock_file_wait(file, fl);
5206 struct nfs4_unlockdata {
5207 struct nfs_locku_args arg;
5208 struct nfs_locku_res res;
5209 struct nfs4_lock_state *lsp;
5210 struct nfs_open_context *ctx;
5211 struct file_lock fl;
5212 const struct nfs_server *server;
5213 unsigned long timestamp;
5216 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5217 struct nfs_open_context *ctx,
5218 struct nfs4_lock_state *lsp,
5219 struct nfs_seqid *seqid)
5221 struct nfs4_unlockdata *p;
5222 struct inode *inode = lsp->ls_state->inode;
5224 p = kzalloc(sizeof(*p), GFP_NOFS);
5227 p->arg.fh = NFS_FH(inode);
5229 p->arg.seqid = seqid;
5230 p->res.seqid = seqid;
5231 p->arg.stateid = &lsp->ls_stateid;
5233 atomic_inc(&lsp->ls_count);
5234 /* Ensure we don't close file until we're done freeing locks! */
5235 p->ctx = get_nfs_open_context(ctx);
5236 memcpy(&p->fl, fl, sizeof(p->fl));
5237 p->server = NFS_SERVER(inode);
5241 static void nfs4_locku_release_calldata(void *data)
5243 struct nfs4_unlockdata *calldata = data;
5244 nfs_free_seqid(calldata->arg.seqid);
5245 nfs4_put_lock_state(calldata->lsp);
5246 put_nfs_open_context(calldata->ctx);
5250 static void nfs4_locku_done(struct rpc_task *task, void *data)
5252 struct nfs4_unlockdata *calldata = data;
5254 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5256 switch (task->tk_status) {
5258 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5259 &calldata->res.stateid);
5260 renew_lease(calldata->server, calldata->timestamp);
5262 case -NFS4ERR_BAD_STATEID:
5263 case -NFS4ERR_OLD_STATEID:
5264 case -NFS4ERR_STALE_STATEID:
5265 case -NFS4ERR_EXPIRED:
5268 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
5269 rpc_restart_call_prepare(task);
5271 nfs_release_seqid(calldata->arg.seqid);
5274 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5276 struct nfs4_unlockdata *calldata = data;
5278 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5280 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5281 /* Note: exit _without_ running nfs4_locku_done */
5284 calldata->timestamp = jiffies;
5285 if (nfs4_setup_sequence(calldata->server,
5286 &calldata->arg.seq_args,
5287 &calldata->res.seq_res,
5289 nfs_release_seqid(calldata->arg.seqid);
5292 task->tk_action = NULL;
5294 nfs4_sequence_done(task, &calldata->res.seq_res);
5297 static const struct rpc_call_ops nfs4_locku_ops = {
5298 .rpc_call_prepare = nfs4_locku_prepare,
5299 .rpc_call_done = nfs4_locku_done,
5300 .rpc_release = nfs4_locku_release_calldata,
5303 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5304 struct nfs_open_context *ctx,
5305 struct nfs4_lock_state *lsp,
5306 struct nfs_seqid *seqid)
5308 struct nfs4_unlockdata *data;
5309 struct rpc_message msg = {
5310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5311 .rpc_cred = ctx->cred,
5313 struct rpc_task_setup task_setup_data = {
5314 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5315 .rpc_message = &msg,
5316 .callback_ops = &nfs4_locku_ops,
5317 .workqueue = nfsiod_workqueue,
5318 .flags = RPC_TASK_ASYNC,
5321 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5322 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5324 /* Ensure this is an unlock - when canceling a lock, the
5325 * canceled lock is passed in, and it won't be an unlock.
5327 fl->fl_type = F_UNLCK;
5329 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5331 nfs_free_seqid(seqid);
5332 return ERR_PTR(-ENOMEM);
5335 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5336 msg.rpc_argp = &data->arg;
5337 msg.rpc_resp = &data->res;
5338 task_setup_data.callback_data = data;
5339 return rpc_run_task(&task_setup_data);
5342 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5344 struct inode *inode = state->inode;
5345 struct nfs4_state_owner *sp = state->owner;
5346 struct nfs_inode *nfsi = NFS_I(inode);
5347 struct nfs_seqid *seqid;
5348 struct nfs4_lock_state *lsp;
5349 struct rpc_task *task;
5351 unsigned char fl_flags = request->fl_flags;
5353 status = nfs4_set_lock_state(state, request);
5354 /* Unlock _before_ we do the RPC call */
5355 request->fl_flags |= FL_EXISTS;
5356 /* Exclude nfs_delegation_claim_locks() */
5357 mutex_lock(&sp->so_delegreturn_mutex);
5358 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5359 down_read(&nfsi->rwsem);
5360 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5361 up_read(&nfsi->rwsem);
5362 mutex_unlock(&sp->so_delegreturn_mutex);
5365 up_read(&nfsi->rwsem);
5366 mutex_unlock(&sp->so_delegreturn_mutex);
5369 /* Is this a delegated lock? */
5370 lsp = request->fl_u.nfs4_fl.owner;
5371 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5373 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5377 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5378 status = PTR_ERR(task);
5381 status = nfs4_wait_for_completion_rpc_task(task);
5384 request->fl_flags = fl_flags;
5385 trace_nfs4_unlock(request, state, F_SETLK, status);
5389 struct nfs4_lockdata {
5390 struct nfs_lock_args arg;
5391 struct nfs_lock_res res;
5392 struct nfs4_lock_state *lsp;
5393 struct nfs_open_context *ctx;
5394 struct file_lock fl;
5395 unsigned long timestamp;
5398 struct nfs_server *server;
5401 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5402 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5405 struct nfs4_lockdata *p;
5406 struct inode *inode = lsp->ls_state->inode;
5407 struct nfs_server *server = NFS_SERVER(inode);
5409 p = kzalloc(sizeof(*p), gfp_mask);
5413 p->arg.fh = NFS_FH(inode);
5415 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5416 if (p->arg.open_seqid == NULL)
5418 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5419 if (p->arg.lock_seqid == NULL)
5420 goto out_free_seqid;
5421 p->arg.lock_stateid = &lsp->ls_stateid;
5422 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5423 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5424 p->arg.lock_owner.s_dev = server->s_dev;
5425 p->res.lock_seqid = p->arg.lock_seqid;
5428 atomic_inc(&lsp->ls_count);
5429 p->ctx = get_nfs_open_context(ctx);
5430 memcpy(&p->fl, fl, sizeof(p->fl));
5433 nfs_free_seqid(p->arg.open_seqid);
5439 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5441 struct nfs4_lockdata *data = calldata;
5442 struct nfs4_state *state = data->lsp->ls_state;
5444 dprintk("%s: begin!\n", __func__);
5445 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5447 /* Do we need to do an open_to_lock_owner? */
5448 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5449 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5450 goto out_release_lock_seqid;
5452 data->arg.open_stateid = &state->open_stateid;
5453 data->arg.new_lock_owner = 1;
5454 data->res.open_seqid = data->arg.open_seqid;
5456 data->arg.new_lock_owner = 0;
5457 if (!nfs4_valid_open_stateid(state)) {
5458 data->rpc_status = -EBADF;
5459 task->tk_action = NULL;
5460 goto out_release_open_seqid;
5462 data->timestamp = jiffies;
5463 if (nfs4_setup_sequence(data->server,
5464 &data->arg.seq_args,
5468 out_release_open_seqid:
5469 nfs_release_seqid(data->arg.open_seqid);
5470 out_release_lock_seqid:
5471 nfs_release_seqid(data->arg.lock_seqid);
5473 nfs4_sequence_done(task, &data->res.seq_res);
5474 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5477 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5479 struct nfs4_lockdata *data = calldata;
5481 dprintk("%s: begin!\n", __func__);
5483 if (!nfs4_sequence_done(task, &data->res.seq_res))
5486 data->rpc_status = task->tk_status;
5487 if (data->arg.new_lock_owner != 0) {
5488 if (data->rpc_status == 0)
5489 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5493 if (data->rpc_status == 0) {
5494 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5495 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5496 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5499 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5502 static void nfs4_lock_release(void *calldata)
5504 struct nfs4_lockdata *data = calldata;
5506 dprintk("%s: begin!\n", __func__);
5507 nfs_free_seqid(data->arg.open_seqid);
5508 if (data->cancelled != 0) {
5509 struct rpc_task *task;
5510 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5511 data->arg.lock_seqid);
5513 rpc_put_task_async(task);
5514 dprintk("%s: cancelling lock!\n", __func__);
5516 nfs_free_seqid(data->arg.lock_seqid);
5517 nfs4_put_lock_state(data->lsp);
5518 put_nfs_open_context(data->ctx);
5520 dprintk("%s: done!\n", __func__);
5523 static const struct rpc_call_ops nfs4_lock_ops = {
5524 .rpc_call_prepare = nfs4_lock_prepare,
5525 .rpc_call_done = nfs4_lock_done,
5526 .rpc_release = nfs4_lock_release,
5529 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5532 case -NFS4ERR_ADMIN_REVOKED:
5533 case -NFS4ERR_BAD_STATEID:
5534 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5535 if (new_lock_owner != 0 ||
5536 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5537 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5539 case -NFS4ERR_STALE_STATEID:
5540 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5541 case -NFS4ERR_EXPIRED:
5542 nfs4_schedule_lease_recovery(server->nfs_client);
5546 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5548 struct nfs4_lockdata *data;
5549 struct rpc_task *task;
5550 struct rpc_message msg = {
5551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5552 .rpc_cred = state->owner->so_cred,
5554 struct rpc_task_setup task_setup_data = {
5555 .rpc_client = NFS_CLIENT(state->inode),
5556 .rpc_message = &msg,
5557 .callback_ops = &nfs4_lock_ops,
5558 .workqueue = nfsiod_workqueue,
5559 .flags = RPC_TASK_ASYNC,
5563 dprintk("%s: begin!\n", __func__);
5564 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5565 fl->fl_u.nfs4_fl.owner,
5566 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5570 data->arg.block = 1;
5571 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5572 msg.rpc_argp = &data->arg;
5573 msg.rpc_resp = &data->res;
5574 task_setup_data.callback_data = data;
5575 if (recovery_type > NFS_LOCK_NEW) {
5576 if (recovery_type == NFS_LOCK_RECLAIM)
5577 data->arg.reclaim = NFS_LOCK_RECLAIM;
5578 nfs4_set_sequence_privileged(&data->arg.seq_args);
5580 task = rpc_run_task(&task_setup_data);
5582 return PTR_ERR(task);
5583 ret = nfs4_wait_for_completion_rpc_task(task);
5585 ret = data->rpc_status;
5587 nfs4_handle_setlk_error(data->server, data->lsp,
5588 data->arg.new_lock_owner, ret);
5590 data->cancelled = 1;
5592 dprintk("%s: done, ret = %d!\n", __func__, ret);
5596 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5598 struct nfs_server *server = NFS_SERVER(state->inode);
5599 struct nfs4_exception exception = {
5600 .inode = state->inode,
5605 /* Cache the lock if possible... */
5606 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5608 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5609 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5610 if (err != -NFS4ERR_DELAY)
5612 nfs4_handle_exception(server, err, &exception);
5613 } while (exception.retry);
5617 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5619 struct nfs_server *server = NFS_SERVER(state->inode);
5620 struct nfs4_exception exception = {
5621 .inode = state->inode,
5625 err = nfs4_set_lock_state(state, request);
5628 if (!recover_lost_locks) {
5629 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5633 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5635 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5636 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5640 case -NFS4ERR_GRACE:
5641 case -NFS4ERR_DELAY:
5642 nfs4_handle_exception(server, err, &exception);
5645 } while (exception.retry);
5650 #if defined(CONFIG_NFS_V4_1)
5652 * nfs41_check_expired_locks - possibly free a lock stateid
5654 * @state: NFSv4 state for an inode
5656 * Returns NFS_OK if recovery for this stateid is now finished.
5657 * Otherwise a negative NFS4ERR value is returned.
5659 static int nfs41_check_expired_locks(struct nfs4_state *state)
5661 int status, ret = -NFS4ERR_BAD_STATEID;
5662 struct nfs4_lock_state *lsp;
5663 struct nfs_server *server = NFS_SERVER(state->inode);
5665 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5666 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5667 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5669 status = nfs41_test_stateid(server,
5672 trace_nfs4_test_lock_stateid(state, lsp, status);
5673 if (status != NFS_OK) {
5674 /* Free the stateid unless the server
5675 * informs us the stateid is unrecognized. */
5676 if (status != -NFS4ERR_BAD_STATEID)
5677 nfs41_free_stateid(server,
5680 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5689 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5691 int status = NFS_OK;
5693 if (test_bit(LK_STATE_IN_USE, &state->flags))
5694 status = nfs41_check_expired_locks(state);
5695 if (status != NFS_OK)
5696 status = nfs4_lock_expired(state, request);
5701 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5703 struct nfs4_state_owner *sp = state->owner;
5704 struct nfs_inode *nfsi = NFS_I(state->inode);
5705 unsigned char fl_flags = request->fl_flags;
5707 int status = -ENOLCK;
5709 if ((fl_flags & FL_POSIX) &&
5710 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5712 /* Is this a delegated open? */
5713 status = nfs4_set_lock_state(state, request);
5716 request->fl_flags |= FL_ACCESS;
5717 status = do_vfs_lock(request->fl_file, request);
5720 down_read(&nfsi->rwsem);
5721 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5722 /* Yes: cache locks! */
5723 /* ...but avoid races with delegation recall... */
5724 request->fl_flags = fl_flags & ~FL_SLEEP;
5725 status = do_vfs_lock(request->fl_file, request);
5728 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5729 up_read(&nfsi->rwsem);
5730 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5733 down_read(&nfsi->rwsem);
5734 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5735 status = -NFS4ERR_DELAY;
5738 /* Note: we always want to sleep here! */
5739 request->fl_flags = fl_flags | FL_SLEEP;
5740 if (do_vfs_lock(request->fl_file, request) < 0)
5741 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5742 "manager!\n", __func__);
5744 up_read(&nfsi->rwsem);
5746 request->fl_flags = fl_flags;
5750 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5752 struct nfs4_exception exception = {
5754 .inode = state->inode,
5759 err = _nfs4_proc_setlk(state, cmd, request);
5760 trace_nfs4_set_lock(request, state, cmd, err);
5761 if (err == -NFS4ERR_DENIED)
5763 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5765 } while (exception.retry);
5770 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5772 struct nfs_open_context *ctx;
5773 struct nfs4_state *state;
5774 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5777 /* verify open state */
5778 ctx = nfs_file_open_context(filp);
5781 if (request->fl_start < 0 || request->fl_end < 0)
5784 if (IS_GETLK(cmd)) {
5786 return nfs4_proc_getlk(state, F_GETLK, request);
5790 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5793 if (request->fl_type == F_UNLCK) {
5795 return nfs4_proc_unlck(state, cmd, request);
5802 * Don't rely on the VFS having checked the file open mode,
5803 * since it won't do this for flock() locks.
5805 switch (request->fl_type) {
5807 if (!(filp->f_mode & FMODE_READ))
5811 if (!(filp->f_mode & FMODE_WRITE))
5816 status = nfs4_proc_setlk(state, cmd, request);
5817 if ((status != -EAGAIN) || IS_SETLK(cmd))
5819 timeout = nfs4_set_lock_task_retry(timeout);
5820 status = -ERESTARTSYS;
5823 } while(status < 0);
5827 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5829 struct nfs_server *server = NFS_SERVER(state->inode);
5832 err = nfs4_set_lock_state(state, fl);
5835 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5836 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5839 struct nfs_release_lockowner_data {
5840 struct nfs4_lock_state *lsp;
5841 struct nfs_server *server;
5842 struct nfs_release_lockowner_args args;
5843 struct nfs_release_lockowner_res res;
5844 unsigned long timestamp;
5847 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5849 struct nfs_release_lockowner_data *data = calldata;
5850 nfs40_setup_sequence(data->server,
5851 &data->args.seq_args, &data->res.seq_res, task);
5852 data->timestamp = jiffies;
5855 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5857 struct nfs_release_lockowner_data *data = calldata;
5858 struct nfs_server *server = data->server;
5860 nfs40_sequence_done(task, &data->res.seq_res);
5862 switch (task->tk_status) {
5864 renew_lease(server, data->timestamp);
5866 case -NFS4ERR_STALE_CLIENTID:
5867 case -NFS4ERR_EXPIRED:
5868 case -NFS4ERR_LEASE_MOVED:
5869 case -NFS4ERR_DELAY:
5870 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5871 rpc_restart_call_prepare(task);
5875 static void nfs4_release_lockowner_release(void *calldata)
5877 struct nfs_release_lockowner_data *data = calldata;
5878 nfs4_free_lock_state(data->server, data->lsp);
5882 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5883 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5884 .rpc_call_done = nfs4_release_lockowner_done,
5885 .rpc_release = nfs4_release_lockowner_release,
5888 static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
5890 struct nfs_release_lockowner_data *data;
5891 struct rpc_message msg = {
5892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5895 if (server->nfs_client->cl_mvops->minor_version != 0)
5898 data = kmalloc(sizeof(*data), GFP_NOFS);
5902 data->server = server;
5903 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5904 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5905 data->args.lock_owner.s_dev = server->s_dev;
5907 msg.rpc_argp = &data->args;
5908 msg.rpc_resp = &data->res;
5909 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
5910 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5914 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5916 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5917 const void *buf, size_t buflen,
5918 int flags, int type)
5920 if (strcmp(key, "") != 0)
5923 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5926 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5927 void *buf, size_t buflen, int type)
5929 if (strcmp(key, "") != 0)
5932 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5935 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5936 size_t list_len, const char *name,
5937 size_t name_len, int type)
5939 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5941 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5944 if (list && len <= list_len)
5945 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5949 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5950 static inline int nfs4_server_supports_labels(struct nfs_server *server)
5952 return server->caps & NFS_CAP_SECURITY_LABEL;
5955 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5956 const void *buf, size_t buflen,
5957 int flags, int type)
5959 if (security_ismaclabel(key))
5960 return nfs4_set_security_label(dentry, buf, buflen);
5965 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5966 void *buf, size_t buflen, int type)
5968 if (security_ismaclabel(key))
5969 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5973 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5974 size_t list_len, const char *name,
5975 size_t name_len, int type)
5979 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5980 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5981 if (list && len <= list_len)
5982 security_inode_listsecurity(dentry->d_inode, list, len);
5987 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5988 .prefix = XATTR_SECURITY_PREFIX,
5989 .list = nfs4_xattr_list_nfs4_label,
5990 .get = nfs4_xattr_get_nfs4_label,
5991 .set = nfs4_xattr_set_nfs4_label,
5997 * nfs_fhget will use either the mounted_on_fileid or the fileid
5999 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6001 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6002 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6003 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6004 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6007 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6008 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6009 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6013 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6014 const struct qstr *name,
6015 struct nfs4_fs_locations *fs_locations,
6018 struct nfs_server *server = NFS_SERVER(dir);
6020 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6022 struct nfs4_fs_locations_arg args = {
6023 .dir_fh = NFS_FH(dir),
6028 struct nfs4_fs_locations_res res = {
6029 .fs_locations = fs_locations,
6031 struct rpc_message msg = {
6032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6038 dprintk("%s: start\n", __func__);
6040 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6041 * is not supported */
6042 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6043 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6045 bitmask[0] |= FATTR4_WORD0_FILEID;
6047 nfs_fattr_init(&fs_locations->fattr);
6048 fs_locations->server = server;
6049 fs_locations->nlocations = 0;
6050 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6051 dprintk("%s: returned status = %d\n", __func__, status);
6055 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6056 const struct qstr *name,
6057 struct nfs4_fs_locations *fs_locations,
6060 struct nfs4_exception exception = { };
6063 err = _nfs4_proc_fs_locations(client, dir, name,
6064 fs_locations, page);
6065 trace_nfs4_get_fs_locations(dir, name, err);
6066 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6068 } while (exception.retry);
6073 * This operation also signals the server that this client is
6074 * performing migration recovery. The server can stop returning
6075 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6076 * appended to this compound to identify the client ID which is
6077 * performing recovery.
6079 static int _nfs40_proc_get_locations(struct inode *inode,
6080 struct nfs4_fs_locations *locations,
6081 struct page *page, struct rpc_cred *cred)
6083 struct nfs_server *server = NFS_SERVER(inode);
6084 struct rpc_clnt *clnt = server->client;
6086 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6088 struct nfs4_fs_locations_arg args = {
6089 .clientid = server->nfs_client->cl_clientid,
6090 .fh = NFS_FH(inode),
6093 .migration = 1, /* skip LOOKUP */
6094 .renew = 1, /* append RENEW */
6096 struct nfs4_fs_locations_res res = {
6097 .fs_locations = locations,
6101 struct rpc_message msg = {
6102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6107 unsigned long now = jiffies;
6110 nfs_fattr_init(&locations->fattr);
6111 locations->server = server;
6112 locations->nlocations = 0;
6114 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6115 nfs4_set_sequence_privileged(&args.seq_args);
6116 status = nfs4_call_sync_sequence(clnt, server, &msg,
6117 &args.seq_args, &res.seq_res);
6121 renew_lease(server, now);
6125 #ifdef CONFIG_NFS_V4_1
6128 * This operation also signals the server that this client is
6129 * performing migration recovery. The server can stop asserting
6130 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6131 * performing this operation is identified in the SEQUENCE
6132 * operation in this compound.
6134 * When the client supports GETATTR(fs_locations_info), it can
6135 * be plumbed in here.
6137 static int _nfs41_proc_get_locations(struct inode *inode,
6138 struct nfs4_fs_locations *locations,
6139 struct page *page, struct rpc_cred *cred)
6141 struct nfs_server *server = NFS_SERVER(inode);
6142 struct rpc_clnt *clnt = server->client;
6144 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6146 struct nfs4_fs_locations_arg args = {
6147 .fh = NFS_FH(inode),
6150 .migration = 1, /* skip LOOKUP */
6152 struct nfs4_fs_locations_res res = {
6153 .fs_locations = locations,
6156 struct rpc_message msg = {
6157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6164 nfs_fattr_init(&locations->fattr);
6165 locations->server = server;
6166 locations->nlocations = 0;
6168 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6169 nfs4_set_sequence_privileged(&args.seq_args);
6170 status = nfs4_call_sync_sequence(clnt, server, &msg,
6171 &args.seq_args, &res.seq_res);
6172 if (status == NFS4_OK &&
6173 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6174 status = -NFS4ERR_LEASE_MOVED;
6178 #endif /* CONFIG_NFS_V4_1 */
6181 * nfs4_proc_get_locations - discover locations for a migrated FSID
6182 * @inode: inode on FSID that is migrating
6183 * @locations: result of query
6185 * @cred: credential to use for this operation
6187 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6188 * operation failed, or a negative errno if a local error occurred.
6190 * On success, "locations" is filled in, but if the server has
6191 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6194 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6195 * from this client that require migration recovery.
6197 int nfs4_proc_get_locations(struct inode *inode,
6198 struct nfs4_fs_locations *locations,
6199 struct page *page, struct rpc_cred *cred)
6201 struct nfs_server *server = NFS_SERVER(inode);
6202 struct nfs_client *clp = server->nfs_client;
6203 const struct nfs4_mig_recovery_ops *ops =
6204 clp->cl_mvops->mig_recovery_ops;
6205 struct nfs4_exception exception = { };
6208 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6209 (unsigned long long)server->fsid.major,
6210 (unsigned long long)server->fsid.minor,
6212 nfs_display_fhandle(NFS_FH(inode), __func__);
6215 status = ops->get_locations(inode, locations, page, cred);
6216 if (status != -NFS4ERR_DELAY)
6218 nfs4_handle_exception(server, status, &exception);
6219 } while (exception.retry);
6224 * This operation also signals the server that this client is
6225 * performing "lease moved" recovery. The server can stop
6226 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6227 * is appended to this compound to identify the client ID which is
6228 * performing recovery.
6230 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6232 struct nfs_server *server = NFS_SERVER(inode);
6233 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6234 struct rpc_clnt *clnt = server->client;
6235 struct nfs4_fsid_present_arg args = {
6236 .fh = NFS_FH(inode),
6237 .clientid = clp->cl_clientid,
6238 .renew = 1, /* append RENEW */
6240 struct nfs4_fsid_present_res res = {
6243 struct rpc_message msg = {
6244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6249 unsigned long now = jiffies;
6252 res.fh = nfs_alloc_fhandle();
6256 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6257 nfs4_set_sequence_privileged(&args.seq_args);
6258 status = nfs4_call_sync_sequence(clnt, server, &msg,
6259 &args.seq_args, &res.seq_res);
6260 nfs_free_fhandle(res.fh);
6264 do_renew_lease(clp, now);
6268 #ifdef CONFIG_NFS_V4_1
6271 * This operation also signals the server that this client is
6272 * performing "lease moved" recovery. The server can stop asserting
6273 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6274 * this operation is identified in the SEQUENCE operation in this
6277 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6279 struct nfs_server *server = NFS_SERVER(inode);
6280 struct rpc_clnt *clnt = server->client;
6281 struct nfs4_fsid_present_arg args = {
6282 .fh = NFS_FH(inode),
6284 struct nfs4_fsid_present_res res = {
6286 struct rpc_message msg = {
6287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6294 res.fh = nfs_alloc_fhandle();
6298 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6299 nfs4_set_sequence_privileged(&args.seq_args);
6300 status = nfs4_call_sync_sequence(clnt, server, &msg,
6301 &args.seq_args, &res.seq_res);
6302 nfs_free_fhandle(res.fh);
6303 if (status == NFS4_OK &&
6304 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6305 status = -NFS4ERR_LEASE_MOVED;
6309 #endif /* CONFIG_NFS_V4_1 */
6312 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6313 * @inode: inode on FSID to check
6314 * @cred: credential to use for this operation
6316 * Server indicates whether the FSID is present, moved, or not
6317 * recognized. This operation is necessary to clear a LEASE_MOVED
6318 * condition for this client ID.
6320 * Returns NFS4_OK if the FSID is present on this server,
6321 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6322 * NFS4ERR code if some error occurred on the server, or a
6323 * negative errno if a local failure occurred.
6325 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6327 struct nfs_server *server = NFS_SERVER(inode);
6328 struct nfs_client *clp = server->nfs_client;
6329 const struct nfs4_mig_recovery_ops *ops =
6330 clp->cl_mvops->mig_recovery_ops;
6331 struct nfs4_exception exception = { };
6334 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6335 (unsigned long long)server->fsid.major,
6336 (unsigned long long)server->fsid.minor,
6338 nfs_display_fhandle(NFS_FH(inode), __func__);
6341 status = ops->fsid_present(inode, cred);
6342 if (status != -NFS4ERR_DELAY)
6344 nfs4_handle_exception(server, status, &exception);
6345 } while (exception.retry);
6350 * If 'use_integrity' is true and the state managment nfs_client
6351 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6352 * and the machine credential as per RFC3530bis and RFC5661 Security
6353 * Considerations sections. Otherwise, just use the user cred with the
6354 * filesystem's rpc_client.
6356 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6359 struct nfs4_secinfo_arg args = {
6360 .dir_fh = NFS_FH(dir),
6363 struct nfs4_secinfo_res res = {
6366 struct rpc_message msg = {
6367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6371 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6372 struct rpc_cred *cred = NULL;
6374 if (use_integrity) {
6375 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6376 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6377 msg.rpc_cred = cred;
6380 dprintk("NFS call secinfo %s\n", name->name);
6382 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6383 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6385 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6387 dprintk("NFS reply secinfo: %d\n", status);
6395 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6396 struct nfs4_secinfo_flavors *flavors)
6398 struct nfs4_exception exception = { };
6401 err = -NFS4ERR_WRONGSEC;
6403 /* try to use integrity protection with machine cred */
6404 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6405 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6408 * if unable to use integrity protection, or SECINFO with
6409 * integrity protection returns NFS4ERR_WRONGSEC (which is
6410 * disallowed by spec, but exists in deployed servers) use
6411 * the current filesystem's rpc_client and the user cred.
6413 if (err == -NFS4ERR_WRONGSEC)
6414 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6416 trace_nfs4_secinfo(dir, name, err);
6417 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6419 } while (exception.retry);
6423 #ifdef CONFIG_NFS_V4_1
6425 * Check the exchange flags returned by the server for invalid flags, having
6426 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6429 static int nfs4_check_cl_exchange_flags(u32 flags)
6431 if (flags & ~EXCHGID4_FLAG_MASK_R)
6433 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6434 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6436 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6440 return -NFS4ERR_INVAL;
6444 nfs41_same_server_scope(struct nfs41_server_scope *a,
6445 struct nfs41_server_scope *b)
6447 if (a->server_scope_sz == b->server_scope_sz &&
6448 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6455 * nfs4_proc_bind_conn_to_session()
6457 * The 4.1 client currently uses the same TCP connection for the
6458 * fore and backchannel.
6460 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6463 struct nfs41_bind_conn_to_session_res res;
6464 struct rpc_message msg = {
6466 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6472 dprintk("--> %s\n", __func__);
6474 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6475 if (unlikely(res.session == NULL)) {
6480 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6481 trace_nfs4_bind_conn_to_session(clp, status);
6483 if (memcmp(res.session->sess_id.data,
6484 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6485 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6489 if (res.dir != NFS4_CDFS4_BOTH) {
6490 dprintk("NFS: %s: Unexpected direction from server\n",
6495 if (res.use_conn_in_rdma_mode) {
6496 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6505 dprintk("<-- %s status= %d\n", __func__, status);
6510 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6511 * and operations we'd like to see to enable certain features in the allow map
6513 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6514 .how = SP4_MACH_CRED,
6515 .enforce.u.words = {
6516 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6517 1 << (OP_EXCHANGE_ID - 32) |
6518 1 << (OP_CREATE_SESSION - 32) |
6519 1 << (OP_DESTROY_SESSION - 32) |
6520 1 << (OP_DESTROY_CLIENTID - 32)
6523 [0] = 1 << (OP_CLOSE) |
6526 [1] = 1 << (OP_SECINFO - 32) |
6527 1 << (OP_SECINFO_NO_NAME - 32) |
6528 1 << (OP_TEST_STATEID - 32) |
6529 1 << (OP_FREE_STATEID - 32) |
6530 1 << (OP_WRITE - 32)
6535 * Select the state protection mode for client `clp' given the server results
6536 * from exchange_id in `sp'.
6538 * Returns 0 on success, negative errno otherwise.
6540 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6541 struct nfs41_state_protection *sp)
6543 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6544 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6545 1 << (OP_EXCHANGE_ID - 32) |
6546 1 << (OP_CREATE_SESSION - 32) |
6547 1 << (OP_DESTROY_SESSION - 32) |
6548 1 << (OP_DESTROY_CLIENTID - 32)
6552 if (sp->how == SP4_MACH_CRED) {
6553 /* Print state protect result */
6554 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6555 for (i = 0; i <= LAST_NFS4_OP; i++) {
6556 if (test_bit(i, sp->enforce.u.longs))
6557 dfprintk(MOUNT, " enforce op %d\n", i);
6558 if (test_bit(i, sp->allow.u.longs))
6559 dfprintk(MOUNT, " allow op %d\n", i);
6562 /* make sure nothing is on enforce list that isn't supported */
6563 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6564 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6565 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6571 * Minimal mode - state operations are allowed to use machine
6572 * credential. Note this already happens by default, so the
6573 * client doesn't have to do anything more than the negotiation.
6575 * NOTE: we don't care if EXCHANGE_ID is in the list -
6576 * we're already using the machine cred for exchange_id
6577 * and will never use a different cred.
6579 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6580 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6581 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6582 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6583 dfprintk(MOUNT, "sp4_mach_cred:\n");
6584 dfprintk(MOUNT, " minimal mode enabled\n");
6585 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6587 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6591 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6592 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6593 dfprintk(MOUNT, " cleanup mode enabled\n");
6594 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6597 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6598 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6599 dfprintk(MOUNT, " secinfo mode enabled\n");
6600 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6603 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6604 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6605 dfprintk(MOUNT, " stateid mode enabled\n");
6606 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6609 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6610 dfprintk(MOUNT, " write mode enabled\n");
6611 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6614 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6615 dfprintk(MOUNT, " commit mode enabled\n");
6616 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6624 * _nfs4_proc_exchange_id()
6626 * Wrapper for EXCHANGE_ID operation.
6628 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6631 nfs4_verifier verifier;
6632 struct nfs41_exchange_id_args args = {
6633 .verifier = &verifier,
6635 #ifdef CONFIG_NFS_V4_1_MIGRATION
6636 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6637 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6638 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6640 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6641 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6644 struct nfs41_exchange_id_res res = {
6648 struct rpc_message msg = {
6649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6655 nfs4_init_boot_verifier(clp, &verifier);
6656 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6658 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6659 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6660 args.id_len, args.id);
6662 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6664 if (unlikely(res.server_owner == NULL)) {
6669 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6671 if (unlikely(res.server_scope == NULL)) {
6673 goto out_server_owner;
6676 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6677 if (unlikely(res.impl_id == NULL)) {
6679 goto out_server_scope;
6684 args.state_protect.how = SP4_NONE;
6688 args.state_protect = nfs4_sp4_mach_cred_request;
6695 goto out_server_scope;
6698 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6699 trace_nfs4_exchange_id(clp, status);
6701 status = nfs4_check_cl_exchange_flags(res.flags);
6704 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6707 clp->cl_clientid = res.clientid;
6708 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6709 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6710 clp->cl_seqid = res.seqid;
6712 kfree(clp->cl_serverowner);
6713 clp->cl_serverowner = res.server_owner;
6714 res.server_owner = NULL;
6716 /* use the most recent implementation id */
6717 kfree(clp->cl_implid);
6718 clp->cl_implid = res.impl_id;
6720 if (clp->cl_serverscope != NULL &&
6721 !nfs41_same_server_scope(clp->cl_serverscope,
6722 res.server_scope)) {
6723 dprintk("%s: server_scope mismatch detected\n",
6725 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6726 kfree(clp->cl_serverscope);
6727 clp->cl_serverscope = NULL;
6730 if (clp->cl_serverscope == NULL) {
6731 clp->cl_serverscope = res.server_scope;
6738 kfree(res.server_owner);
6740 kfree(res.server_scope);
6742 if (clp->cl_implid != NULL)
6743 dprintk("NFS reply exchange_id: Server Implementation ID: "
6744 "domain: %s, name: %s, date: %llu,%u\n",
6745 clp->cl_implid->domain, clp->cl_implid->name,
6746 clp->cl_implid->date.seconds,
6747 clp->cl_implid->date.nseconds);
6748 dprintk("NFS reply exchange_id: %d\n", status);
6753 * nfs4_proc_exchange_id()
6755 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6757 * Since the clientid has expired, all compounds using sessions
6758 * associated with the stale clientid will be returning
6759 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6760 * be in some phase of session reset.
6762 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6764 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6766 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6769 /* try SP4_MACH_CRED if krb5i/p */
6770 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6771 authflavor == RPC_AUTH_GSS_KRB5P) {
6772 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6778 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6781 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6782 struct rpc_cred *cred)
6784 struct rpc_message msg = {
6785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6791 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6792 trace_nfs4_destroy_clientid(clp, status);
6794 dprintk("NFS: Got error %d from the server %s on "
6795 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6799 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6800 struct rpc_cred *cred)
6805 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6806 ret = _nfs4_proc_destroy_clientid(clp, cred);
6808 case -NFS4ERR_DELAY:
6809 case -NFS4ERR_CLIENTID_BUSY:
6819 int nfs4_destroy_clientid(struct nfs_client *clp)
6821 struct rpc_cred *cred;
6824 if (clp->cl_mvops->minor_version < 1)
6826 if (clp->cl_exchange_flags == 0)
6828 if (clp->cl_preserve_clid)
6830 cred = nfs4_get_clid_cred(clp);
6831 ret = nfs4_proc_destroy_clientid(clp, cred);
6836 case -NFS4ERR_STALE_CLIENTID:
6837 clp->cl_exchange_flags = 0;
6843 struct nfs4_get_lease_time_data {
6844 struct nfs4_get_lease_time_args *args;
6845 struct nfs4_get_lease_time_res *res;
6846 struct nfs_client *clp;
6849 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6852 struct nfs4_get_lease_time_data *data =
6853 (struct nfs4_get_lease_time_data *)calldata;
6855 dprintk("--> %s\n", __func__);
6856 /* just setup sequence, do not trigger session recovery
6857 since we're invoked within one */
6858 nfs41_setup_sequence(data->clp->cl_session,
6859 &data->args->la_seq_args,
6860 &data->res->lr_seq_res,
6862 dprintk("<-- %s\n", __func__);
6866 * Called from nfs4_state_manager thread for session setup, so don't recover
6867 * from sequence operation or clientid errors.
6869 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6871 struct nfs4_get_lease_time_data *data =
6872 (struct nfs4_get_lease_time_data *)calldata;
6874 dprintk("--> %s\n", __func__);
6875 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6877 switch (task->tk_status) {
6878 case -NFS4ERR_DELAY:
6879 case -NFS4ERR_GRACE:
6880 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6881 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6882 task->tk_status = 0;
6884 case -NFS4ERR_RETRY_UNCACHED_REP:
6885 rpc_restart_call_prepare(task);
6888 dprintk("<-- %s\n", __func__);
6891 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
6892 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6893 .rpc_call_done = nfs4_get_lease_time_done,
6896 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6898 struct rpc_task *task;
6899 struct nfs4_get_lease_time_args args;
6900 struct nfs4_get_lease_time_res res = {
6901 .lr_fsinfo = fsinfo,
6903 struct nfs4_get_lease_time_data data = {
6908 struct rpc_message msg = {
6909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6913 struct rpc_task_setup task_setup = {
6914 .rpc_client = clp->cl_rpcclient,
6915 .rpc_message = &msg,
6916 .callback_ops = &nfs4_get_lease_time_ops,
6917 .callback_data = &data,
6918 .flags = RPC_TASK_TIMEOUT,
6922 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
6923 nfs4_set_sequence_privileged(&args.la_seq_args);
6924 dprintk("--> %s\n", __func__);
6925 task = rpc_run_task(&task_setup);
6928 status = PTR_ERR(task);
6930 status = task->tk_status;
6933 dprintk("<-- %s return %d\n", __func__, status);
6939 * Initialize the values to be used by the client in CREATE_SESSION
6940 * If nfs4_init_session set the fore channel request and response sizes,
6943 * Set the back channel max_resp_sz_cached to zero to force the client to
6944 * always set csa_cachethis to FALSE because the current implementation
6945 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6947 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6949 unsigned int max_rqst_sz, max_resp_sz;
6951 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6952 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6954 /* Fore channel attributes */
6955 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6956 args->fc_attrs.max_resp_sz = max_resp_sz;
6957 args->fc_attrs.max_ops = NFS4_MAX_OPS;
6958 args->fc_attrs.max_reqs = max_session_slots;
6960 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6961 "max_ops=%u max_reqs=%u\n",
6963 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
6964 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
6966 /* Back channel attributes */
6967 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6968 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6969 args->bc_attrs.max_resp_sz_cached = 0;
6970 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6971 args->bc_attrs.max_reqs = 1;
6973 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6974 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6976 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6977 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6978 args->bc_attrs.max_reqs);
6981 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6983 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6984 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6986 if (rcvd->max_resp_sz > sent->max_resp_sz)
6989 * Our requested max_ops is the minimum we need; we're not
6990 * prepared to break up compounds into smaller pieces than that.
6991 * So, no point even trying to continue if the server won't
6994 if (rcvd->max_ops < sent->max_ops)
6996 if (rcvd->max_reqs == 0)
6998 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6999 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7003 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7005 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7006 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
7008 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7010 if (rcvd->max_resp_sz < sent->max_resp_sz)
7012 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7014 /* These would render the backchannel useless: */
7015 if (rcvd->max_ops != sent->max_ops)
7017 if (rcvd->max_reqs != sent->max_reqs)
7022 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7023 struct nfs4_session *session)
7027 ret = nfs4_verify_fore_channel_attrs(args, session);
7030 return nfs4_verify_back_channel_attrs(args, session);
7033 static int _nfs4_proc_create_session(struct nfs_client *clp,
7034 struct rpc_cred *cred)
7036 struct nfs4_session *session = clp->cl_session;
7037 struct nfs41_create_session_args args = {
7039 .cb_program = NFS4_CALLBACK,
7041 struct nfs41_create_session_res res = {
7044 struct rpc_message msg = {
7045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7052 nfs4_init_channel_attrs(&args);
7053 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7055 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7056 trace_nfs4_create_session(clp, status);
7059 /* Verify the session's negotiated channel_attrs values */
7060 status = nfs4_verify_channel_attrs(&args, session);
7061 /* Increment the clientid slot sequence id */
7069 * Issues a CREATE_SESSION operation to the server.
7070 * It is the responsibility of the caller to verify the session is
7071 * expired before calling this routine.
7073 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7077 struct nfs4_session *session = clp->cl_session;
7079 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7081 status = _nfs4_proc_create_session(clp, cred);
7085 /* Init or reset the session slot tables */
7086 status = nfs4_setup_session_slot_tables(session);
7087 dprintk("slot table setup returned %d\n", status);
7091 ptr = (unsigned *)&session->sess_id.data[0];
7092 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7093 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7095 dprintk("<-- %s\n", __func__);
7100 * Issue the over-the-wire RPC DESTROY_SESSION.
7101 * The caller must serialize access to this routine.
7103 int nfs4_proc_destroy_session(struct nfs4_session *session,
7104 struct rpc_cred *cred)
7106 struct rpc_message msg = {
7107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7108 .rpc_argp = session,
7113 dprintk("--> nfs4_proc_destroy_session\n");
7115 /* session is still being setup */
7116 if (session->clp->cl_cons_state != NFS_CS_READY)
7119 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7120 trace_nfs4_destroy_session(session->clp, status);
7123 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7124 "Session has been destroyed regardless...\n", status);
7126 dprintk("<-- nfs4_proc_destroy_session\n");
7131 * Renew the cl_session lease.
7133 struct nfs4_sequence_data {
7134 struct nfs_client *clp;
7135 struct nfs4_sequence_args args;
7136 struct nfs4_sequence_res res;
7139 static void nfs41_sequence_release(void *data)
7141 struct nfs4_sequence_data *calldata = data;
7142 struct nfs_client *clp = calldata->clp;
7144 if (atomic_read(&clp->cl_count) > 1)
7145 nfs4_schedule_state_renewal(clp);
7146 nfs_put_client(clp);
7150 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7152 switch(task->tk_status) {
7153 case -NFS4ERR_DELAY:
7154 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7157 nfs4_schedule_lease_recovery(clp);
7162 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7164 struct nfs4_sequence_data *calldata = data;
7165 struct nfs_client *clp = calldata->clp;
7167 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7170 trace_nfs4_sequence(clp, task->tk_status);
7171 if (task->tk_status < 0) {
7172 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7173 if (atomic_read(&clp->cl_count) == 1)
7176 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7177 rpc_restart_call_prepare(task);
7181 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7183 dprintk("<-- %s\n", __func__);
7186 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7188 struct nfs4_sequence_data *calldata = data;
7189 struct nfs_client *clp = calldata->clp;
7190 struct nfs4_sequence_args *args;
7191 struct nfs4_sequence_res *res;
7193 args = task->tk_msg.rpc_argp;
7194 res = task->tk_msg.rpc_resp;
7196 nfs41_setup_sequence(clp->cl_session, args, res, task);
7199 static const struct rpc_call_ops nfs41_sequence_ops = {
7200 .rpc_call_done = nfs41_sequence_call_done,
7201 .rpc_call_prepare = nfs41_sequence_prepare,
7202 .rpc_release = nfs41_sequence_release,
7205 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7206 struct rpc_cred *cred,
7209 struct nfs4_sequence_data *calldata;
7210 struct rpc_message msg = {
7211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7214 struct rpc_task_setup task_setup_data = {
7215 .rpc_client = clp->cl_rpcclient,
7216 .rpc_message = &msg,
7217 .callback_ops = &nfs41_sequence_ops,
7218 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7221 if (!atomic_inc_not_zero(&clp->cl_count))
7222 return ERR_PTR(-EIO);
7223 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7224 if (calldata == NULL) {
7225 nfs_put_client(clp);
7226 return ERR_PTR(-ENOMEM);
7228 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7230 nfs4_set_sequence_privileged(&calldata->args);
7231 msg.rpc_argp = &calldata->args;
7232 msg.rpc_resp = &calldata->res;
7233 calldata->clp = clp;
7234 task_setup_data.callback_data = calldata;
7236 return rpc_run_task(&task_setup_data);
7239 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7241 struct rpc_task *task;
7244 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7246 task = _nfs41_proc_sequence(clp, cred, false);
7248 ret = PTR_ERR(task);
7250 rpc_put_task_async(task);
7251 dprintk("<-- %s status=%d\n", __func__, ret);
7255 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7257 struct rpc_task *task;
7260 task = _nfs41_proc_sequence(clp, cred, true);
7262 ret = PTR_ERR(task);
7265 ret = rpc_wait_for_completion_task(task);
7267 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7269 if (task->tk_status == 0)
7270 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7271 ret = task->tk_status;
7275 dprintk("<-- %s status=%d\n", __func__, ret);
7279 struct nfs4_reclaim_complete_data {
7280 struct nfs_client *clp;
7281 struct nfs41_reclaim_complete_args arg;
7282 struct nfs41_reclaim_complete_res res;
7285 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7287 struct nfs4_reclaim_complete_data *calldata = data;
7289 nfs41_setup_sequence(calldata->clp->cl_session,
7290 &calldata->arg.seq_args,
7291 &calldata->res.seq_res,
7295 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7297 switch(task->tk_status) {
7299 case -NFS4ERR_COMPLETE_ALREADY:
7300 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7302 case -NFS4ERR_DELAY:
7303 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7305 case -NFS4ERR_RETRY_UNCACHED_REP:
7308 nfs4_schedule_lease_recovery(clp);
7313 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7315 struct nfs4_reclaim_complete_data *calldata = data;
7316 struct nfs_client *clp = calldata->clp;
7317 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7319 dprintk("--> %s\n", __func__);
7320 if (!nfs41_sequence_done(task, res))
7323 trace_nfs4_reclaim_complete(clp, task->tk_status);
7324 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7325 rpc_restart_call_prepare(task);
7328 dprintk("<-- %s\n", __func__);
7331 static void nfs4_free_reclaim_complete_data(void *data)
7333 struct nfs4_reclaim_complete_data *calldata = data;
7338 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7339 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7340 .rpc_call_done = nfs4_reclaim_complete_done,
7341 .rpc_release = nfs4_free_reclaim_complete_data,
7345 * Issue a global reclaim complete.
7347 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7348 struct rpc_cred *cred)
7350 struct nfs4_reclaim_complete_data *calldata;
7351 struct rpc_task *task;
7352 struct rpc_message msg = {
7353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7356 struct rpc_task_setup task_setup_data = {
7357 .rpc_client = clp->cl_rpcclient,
7358 .rpc_message = &msg,
7359 .callback_ops = &nfs4_reclaim_complete_call_ops,
7360 .flags = RPC_TASK_ASYNC,
7362 int status = -ENOMEM;
7364 dprintk("--> %s\n", __func__);
7365 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7366 if (calldata == NULL)
7368 calldata->clp = clp;
7369 calldata->arg.one_fs = 0;
7371 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7372 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7373 msg.rpc_argp = &calldata->arg;
7374 msg.rpc_resp = &calldata->res;
7375 task_setup_data.callback_data = calldata;
7376 task = rpc_run_task(&task_setup_data);
7378 status = PTR_ERR(task);
7381 status = nfs4_wait_for_completion_rpc_task(task);
7383 status = task->tk_status;
7387 dprintk("<-- %s status=%d\n", __func__, status);
7392 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7394 struct nfs4_layoutget *lgp = calldata;
7395 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7396 struct nfs4_session *session = nfs4_get_session(server);
7398 dprintk("--> %s\n", __func__);
7399 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7400 * right now covering the LAYOUTGET we are about to send.
7401 * However, that is not so catastrophic, and there seems
7402 * to be no way to prevent it completely.
7404 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7405 &lgp->res.seq_res, task))
7407 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7408 NFS_I(lgp->args.inode)->layout,
7409 lgp->args.ctx->state)) {
7410 rpc_exit(task, NFS4_OK);
7414 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7416 struct nfs4_layoutget *lgp = calldata;
7417 struct inode *inode = lgp->args.inode;
7418 struct nfs_server *server = NFS_SERVER(inode);
7419 struct pnfs_layout_hdr *lo;
7420 struct nfs4_state *state = NULL;
7421 unsigned long timeo, now, giveup;
7423 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7425 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7428 switch (task->tk_status) {
7432 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7433 * (or clients) writing to the same RAID stripe
7435 case -NFS4ERR_LAYOUTTRYLATER:
7437 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7438 * existing layout before getting a new one).
7440 case -NFS4ERR_RECALLCONFLICT:
7441 timeo = rpc_get_timeout(task->tk_client);
7442 giveup = lgp->args.timestamp + timeo;
7444 if (time_after(giveup, now)) {
7445 unsigned long delay;
7448 * - Not less then NFS4_POLL_RETRY_MIN.
7449 * - One last time a jiffie before we give up
7450 * - exponential backoff (time_now minus start_attempt)
7452 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7453 min((giveup - now - 1),
7454 now - lgp->args.timestamp));
7456 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7458 rpc_delay(task, delay);
7459 task->tk_status = 0;
7460 rpc_restart_call_prepare(task);
7461 goto out; /* Do not call nfs4_async_handle_error() */
7464 case -NFS4ERR_EXPIRED:
7465 case -NFS4ERR_BAD_STATEID:
7466 spin_lock(&inode->i_lock);
7467 lo = NFS_I(inode)->layout;
7468 if (!lo || list_empty(&lo->plh_segs)) {
7469 spin_unlock(&inode->i_lock);
7470 /* If the open stateid was bad, then recover it. */
7471 state = lgp->args.ctx->state;
7475 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7476 spin_unlock(&inode->i_lock);
7477 /* Mark the bad layout state as invalid, then
7478 * retry using the open stateid. */
7479 pnfs_free_lseg_list(&head);
7482 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7483 rpc_restart_call_prepare(task);
7485 dprintk("<-- %s\n", __func__);
7488 static size_t max_response_pages(struct nfs_server *server)
7490 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7491 return nfs_page_array_len(0, max_resp_sz);
7494 static void nfs4_free_pages(struct page **pages, size_t size)
7501 for (i = 0; i < size; i++) {
7504 __free_page(pages[i]);
7509 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7511 struct page **pages;
7514 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7516 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7520 for (i = 0; i < size; i++) {
7521 pages[i] = alloc_page(gfp_flags);
7523 dprintk("%s: failed to allocate page\n", __func__);
7524 nfs4_free_pages(pages, size);
7532 static void nfs4_layoutget_release(void *calldata)
7534 struct nfs4_layoutget *lgp = calldata;
7535 struct inode *inode = lgp->args.inode;
7536 struct nfs_server *server = NFS_SERVER(inode);
7537 size_t max_pages = max_response_pages(server);
7539 dprintk("--> %s\n", __func__);
7540 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7541 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7542 put_nfs_open_context(lgp->args.ctx);
7544 dprintk("<-- %s\n", __func__);
7547 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7548 .rpc_call_prepare = nfs4_layoutget_prepare,
7549 .rpc_call_done = nfs4_layoutget_done,
7550 .rpc_release = nfs4_layoutget_release,
7553 struct pnfs_layout_segment *
7554 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7556 struct inode *inode = lgp->args.inode;
7557 struct nfs_server *server = NFS_SERVER(inode);
7558 size_t max_pages = max_response_pages(server);
7559 struct rpc_task *task;
7560 struct rpc_message msg = {
7561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7562 .rpc_argp = &lgp->args,
7563 .rpc_resp = &lgp->res,
7564 .rpc_cred = lgp->cred,
7566 struct rpc_task_setup task_setup_data = {
7567 .rpc_client = server->client,
7568 .rpc_message = &msg,
7569 .callback_ops = &nfs4_layoutget_call_ops,
7570 .callback_data = lgp,
7571 .flags = RPC_TASK_ASYNC,
7573 struct pnfs_layout_segment *lseg = NULL;
7576 dprintk("--> %s\n", __func__);
7578 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7579 if (!lgp->args.layout.pages) {
7580 nfs4_layoutget_release(lgp);
7581 return ERR_PTR(-ENOMEM);
7583 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7584 lgp->args.timestamp = jiffies;
7586 lgp->res.layoutp = &lgp->args.layout;
7587 lgp->res.seq_res.sr_slot = NULL;
7588 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7590 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7591 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7593 task = rpc_run_task(&task_setup_data);
7595 return ERR_CAST(task);
7596 status = nfs4_wait_for_completion_rpc_task(task);
7598 status = task->tk_status;
7599 trace_nfs4_layoutget(lgp->args.ctx,
7603 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7604 if (status == 0 && lgp->res.layoutp->len)
7605 lseg = pnfs_layout_process(lgp);
7607 dprintk("<-- %s status=%d\n", __func__, status);
7609 return ERR_PTR(status);
7614 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7616 struct nfs4_layoutreturn *lrp = calldata;
7618 dprintk("--> %s\n", __func__);
7619 nfs41_setup_sequence(lrp->clp->cl_session,
7620 &lrp->args.seq_args,
7625 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7627 struct nfs4_layoutreturn *lrp = calldata;
7628 struct nfs_server *server;
7630 dprintk("--> %s\n", __func__);
7632 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7635 server = NFS_SERVER(lrp->args.inode);
7636 switch (task->tk_status) {
7638 task->tk_status = 0;
7641 case -NFS4ERR_DELAY:
7642 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7644 rpc_restart_call_prepare(task);
7647 dprintk("<-- %s\n", __func__);
7650 static void nfs4_layoutreturn_release(void *calldata)
7652 struct nfs4_layoutreturn *lrp = calldata;
7653 struct pnfs_layout_hdr *lo = lrp->args.layout;
7655 dprintk("--> %s\n", __func__);
7656 spin_lock(&lo->plh_inode->i_lock);
7657 if (lrp->res.lrs_present)
7658 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7659 lo->plh_block_lgets--;
7660 spin_unlock(&lo->plh_inode->i_lock);
7661 pnfs_put_layout_hdr(lrp->args.layout);
7663 dprintk("<-- %s\n", __func__);
7666 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7667 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7668 .rpc_call_done = nfs4_layoutreturn_done,
7669 .rpc_release = nfs4_layoutreturn_release,
7672 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7674 struct rpc_task *task;
7675 struct rpc_message msg = {
7676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7677 .rpc_argp = &lrp->args,
7678 .rpc_resp = &lrp->res,
7679 .rpc_cred = lrp->cred,
7681 struct rpc_task_setup task_setup_data = {
7682 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7683 .rpc_message = &msg,
7684 .callback_ops = &nfs4_layoutreturn_call_ops,
7685 .callback_data = lrp,
7689 dprintk("--> %s\n", __func__);
7690 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7691 task = rpc_run_task(&task_setup_data);
7693 return PTR_ERR(task);
7694 status = task->tk_status;
7695 trace_nfs4_layoutreturn(lrp->args.inode, status);
7696 dprintk("<-- %s status=%d\n", __func__, status);
7702 * Retrieve the list of Data Server devices from the MDS.
7704 static int _nfs4_getdevicelist(struct nfs_server *server,
7705 const struct nfs_fh *fh,
7706 struct pnfs_devicelist *devlist)
7708 struct nfs4_getdevicelist_args args = {
7710 .layoutclass = server->pnfs_curr_ld->id,
7712 struct nfs4_getdevicelist_res res = {
7715 struct rpc_message msg = {
7716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7722 dprintk("--> %s\n", __func__);
7723 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7725 dprintk("<-- %s status=%d\n", __func__, status);
7729 int nfs4_proc_getdevicelist(struct nfs_server *server,
7730 const struct nfs_fh *fh,
7731 struct pnfs_devicelist *devlist)
7733 struct nfs4_exception exception = { };
7737 err = nfs4_handle_exception(server,
7738 _nfs4_getdevicelist(server, fh, devlist),
7740 } while (exception.retry);
7742 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7743 err, devlist->num_devs);
7747 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7750 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7751 struct pnfs_device *pdev,
7752 struct rpc_cred *cred)
7754 struct nfs4_getdeviceinfo_args args = {
7757 struct nfs4_getdeviceinfo_res res = {
7760 struct rpc_message msg = {
7761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7768 dprintk("--> %s\n", __func__);
7769 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7770 dprintk("<-- %s status=%d\n", __func__, status);
7775 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7776 struct pnfs_device *pdev,
7777 struct rpc_cred *cred)
7779 struct nfs4_exception exception = { };
7783 err = nfs4_handle_exception(server,
7784 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7786 } while (exception.retry);
7789 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7791 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7793 struct nfs4_layoutcommit_data *data = calldata;
7794 struct nfs_server *server = NFS_SERVER(data->args.inode);
7795 struct nfs4_session *session = nfs4_get_session(server);
7797 nfs41_setup_sequence(session,
7798 &data->args.seq_args,
7804 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7806 struct nfs4_layoutcommit_data *data = calldata;
7807 struct nfs_server *server = NFS_SERVER(data->args.inode);
7809 if (!nfs41_sequence_done(task, &data->res.seq_res))
7812 switch (task->tk_status) { /* Just ignore these failures */
7813 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7814 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7815 case -NFS4ERR_BADLAYOUT: /* no layout */
7816 case -NFS4ERR_GRACE: /* loca_recalim always false */
7817 task->tk_status = 0;
7821 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7822 rpc_restart_call_prepare(task);
7828 static void nfs4_layoutcommit_release(void *calldata)
7830 struct nfs4_layoutcommit_data *data = calldata;
7832 pnfs_cleanup_layoutcommit(data);
7833 nfs_post_op_update_inode_force_wcc(data->args.inode,
7835 put_rpccred(data->cred);
7839 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7840 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7841 .rpc_call_done = nfs4_layoutcommit_done,
7842 .rpc_release = nfs4_layoutcommit_release,
7846 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7848 struct rpc_message msg = {
7849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7850 .rpc_argp = &data->args,
7851 .rpc_resp = &data->res,
7852 .rpc_cred = data->cred,
7854 struct rpc_task_setup task_setup_data = {
7855 .task = &data->task,
7856 .rpc_client = NFS_CLIENT(data->args.inode),
7857 .rpc_message = &msg,
7858 .callback_ops = &nfs4_layoutcommit_ops,
7859 .callback_data = data,
7860 .flags = RPC_TASK_ASYNC,
7862 struct rpc_task *task;
7865 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7866 "lbw: %llu inode %lu\n",
7867 data->task.tk_pid, sync,
7868 data->args.lastbytewritten,
7869 data->args.inode->i_ino);
7871 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7872 task = rpc_run_task(&task_setup_data);
7874 return PTR_ERR(task);
7877 status = nfs4_wait_for_completion_rpc_task(task);
7880 status = task->tk_status;
7881 trace_nfs4_layoutcommit(data->args.inode, status);
7883 dprintk("%s: status %d\n", __func__, status);
7889 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7890 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7893 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7894 struct nfs_fsinfo *info,
7895 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7897 struct nfs41_secinfo_no_name_args args = {
7898 .style = SECINFO_STYLE_CURRENT_FH,
7900 struct nfs4_secinfo_res res = {
7903 struct rpc_message msg = {
7904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7908 struct rpc_clnt *clnt = server->client;
7909 struct rpc_cred *cred = NULL;
7912 if (use_integrity) {
7913 clnt = server->nfs_client->cl_rpcclient;
7914 cred = nfs4_get_clid_cred(server->nfs_client);
7915 msg.rpc_cred = cred;
7918 dprintk("--> %s\n", __func__);
7919 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7921 dprintk("<-- %s status=%d\n", __func__, status);
7930 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7931 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7933 struct nfs4_exception exception = { };
7936 /* first try using integrity protection */
7937 err = -NFS4ERR_WRONGSEC;
7939 /* try to use integrity protection with machine cred */
7940 if (_nfs4_is_integrity_protected(server->nfs_client))
7941 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7945 * if unable to use integrity protection, or SECINFO with
7946 * integrity protection returns NFS4ERR_WRONGSEC (which is
7947 * disallowed by spec, but exists in deployed servers) use
7948 * the current filesystem's rpc_client and the user cred.
7950 if (err == -NFS4ERR_WRONGSEC)
7951 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7956 case -NFS4ERR_WRONGSEC:
7960 err = nfs4_handle_exception(server, err, &exception);
7962 } while (exception.retry);
7968 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7969 struct nfs_fsinfo *info)
7973 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
7974 struct nfs4_secinfo_flavors *flavors;
7975 struct nfs4_secinfo4 *secinfo;
7978 page = alloc_page(GFP_KERNEL);
7984 flavors = page_address(page);
7985 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7988 * Fall back on "guess and check" method if
7989 * the server doesn't support SECINFO_NO_NAME
7991 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
7992 err = nfs4_find_root_sec(server, fhandle, info);
7998 for (i = 0; i < flavors->num_flavors; i++) {
7999 secinfo = &flavors->flavors[i];
8001 switch (secinfo->flavor) {
8005 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8006 &secinfo->flavor_info);
8009 flavor = RPC_AUTH_MAXFLAVOR;
8013 if (!nfs_auth_info_match(&server->auth_info, flavor))
8014 flavor = RPC_AUTH_MAXFLAVOR;
8016 if (flavor != RPC_AUTH_MAXFLAVOR) {
8017 err = nfs4_lookup_root_sec(server, fhandle,
8024 if (flavor == RPC_AUTH_MAXFLAVOR)
8035 static int _nfs41_test_stateid(struct nfs_server *server,
8036 nfs4_stateid *stateid,
8037 struct rpc_cred *cred)
8040 struct nfs41_test_stateid_args args = {
8043 struct nfs41_test_stateid_res res;
8044 struct rpc_message msg = {
8045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8050 struct rpc_clnt *rpc_client = server->client;
8052 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8055 dprintk("NFS call test_stateid %p\n", stateid);
8056 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8057 nfs4_set_sequence_privileged(&args.seq_args);
8058 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8059 &args.seq_args, &res.seq_res);
8060 if (status != NFS_OK) {
8061 dprintk("NFS reply test_stateid: failed, %d\n", status);
8064 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8069 * nfs41_test_stateid - perform a TEST_STATEID operation
8071 * @server: server / transport on which to perform the operation
8072 * @stateid: state ID to test
8075 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8076 * Otherwise a negative NFS4ERR value is returned if the operation
8077 * failed or the state ID is not currently valid.
8079 static int nfs41_test_stateid(struct nfs_server *server,
8080 nfs4_stateid *stateid,
8081 struct rpc_cred *cred)
8083 struct nfs4_exception exception = { };
8086 err = _nfs41_test_stateid(server, stateid, cred);
8087 if (err != -NFS4ERR_DELAY)
8089 nfs4_handle_exception(server, err, &exception);
8090 } while (exception.retry);
8094 struct nfs_free_stateid_data {
8095 struct nfs_server *server;
8096 struct nfs41_free_stateid_args args;
8097 struct nfs41_free_stateid_res res;
8100 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8102 struct nfs_free_stateid_data *data = calldata;
8103 nfs41_setup_sequence(nfs4_get_session(data->server),
8104 &data->args.seq_args,
8109 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8111 struct nfs_free_stateid_data *data = calldata;
8113 nfs41_sequence_done(task, &data->res.seq_res);
8115 switch (task->tk_status) {
8116 case -NFS4ERR_DELAY:
8117 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8118 rpc_restart_call_prepare(task);
8122 static void nfs41_free_stateid_release(void *calldata)
8127 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8128 .rpc_call_prepare = nfs41_free_stateid_prepare,
8129 .rpc_call_done = nfs41_free_stateid_done,
8130 .rpc_release = nfs41_free_stateid_release,
8133 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8134 nfs4_stateid *stateid,
8135 struct rpc_cred *cred,
8138 struct rpc_message msg = {
8139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8142 struct rpc_task_setup task_setup = {
8143 .rpc_client = server->client,
8144 .rpc_message = &msg,
8145 .callback_ops = &nfs41_free_stateid_ops,
8146 .flags = RPC_TASK_ASYNC,
8148 struct nfs_free_stateid_data *data;
8150 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8151 &task_setup.rpc_client, &msg);
8153 dprintk("NFS call free_stateid %p\n", stateid);
8154 data = kmalloc(sizeof(*data), GFP_NOFS);
8156 return ERR_PTR(-ENOMEM);
8157 data->server = server;
8158 nfs4_stateid_copy(&data->args.stateid, stateid);
8160 task_setup.callback_data = data;
8162 msg.rpc_argp = &data->args;
8163 msg.rpc_resp = &data->res;
8164 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8166 nfs4_set_sequence_privileged(&data->args.seq_args);
8168 return rpc_run_task(&task_setup);
8172 * nfs41_free_stateid - perform a FREE_STATEID operation
8174 * @server: server / transport on which to perform the operation
8175 * @stateid: state ID to release
8178 * Returns NFS_OK if the server freed "stateid". Otherwise a
8179 * negative NFS4ERR value is returned.
8181 static int nfs41_free_stateid(struct nfs_server *server,
8182 nfs4_stateid *stateid,
8183 struct rpc_cred *cred)
8185 struct rpc_task *task;
8188 task = _nfs41_free_stateid(server, stateid, cred, true);
8190 return PTR_ERR(task);
8191 ret = rpc_wait_for_completion_task(task);
8193 ret = task->tk_status;
8198 static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8200 struct rpc_task *task;
8201 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8203 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8204 nfs4_free_lock_state(server, lsp);
8206 return PTR_ERR(task);
8211 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8212 const nfs4_stateid *s2)
8214 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8217 if (s1->seqid == s2->seqid)
8219 if (s1->seqid == 0 || s2->seqid == 0)
8225 #endif /* CONFIG_NFS_V4_1 */
8227 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8228 const nfs4_stateid *s2)
8230 return nfs4_stateid_match(s1, s2);
8234 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8235 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8236 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8237 .recover_open = nfs4_open_reclaim,
8238 .recover_lock = nfs4_lock_reclaim,
8239 .establish_clid = nfs4_init_clientid,
8240 .detect_trunking = nfs40_discover_server_trunking,
8243 #if defined(CONFIG_NFS_V4_1)
8244 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8245 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8246 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8247 .recover_open = nfs4_open_reclaim,
8248 .recover_lock = nfs4_lock_reclaim,
8249 .establish_clid = nfs41_init_clientid,
8250 .reclaim_complete = nfs41_proc_reclaim_complete,
8251 .detect_trunking = nfs41_discover_server_trunking,
8253 #endif /* CONFIG_NFS_V4_1 */
8255 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8256 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8257 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8258 .recover_open = nfs4_open_expired,
8259 .recover_lock = nfs4_lock_expired,
8260 .establish_clid = nfs4_init_clientid,
8263 #if defined(CONFIG_NFS_V4_1)
8264 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8265 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8266 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8267 .recover_open = nfs41_open_expired,
8268 .recover_lock = nfs41_lock_expired,
8269 .establish_clid = nfs41_init_clientid,
8271 #endif /* CONFIG_NFS_V4_1 */
8273 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8274 .sched_state_renewal = nfs4_proc_async_renew,
8275 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8276 .renew_lease = nfs4_proc_renew,
8279 #if defined(CONFIG_NFS_V4_1)
8280 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8281 .sched_state_renewal = nfs41_proc_async_sequence,
8282 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8283 .renew_lease = nfs4_proc_sequence,
8287 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8288 .get_locations = _nfs40_proc_get_locations,
8289 .fsid_present = _nfs40_proc_fsid_present,
8292 #if defined(CONFIG_NFS_V4_1)
8293 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8294 .get_locations = _nfs41_proc_get_locations,
8295 .fsid_present = _nfs41_proc_fsid_present,
8297 #endif /* CONFIG_NFS_V4_1 */
8299 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8301 .init_caps = NFS_CAP_READDIRPLUS
8302 | NFS_CAP_ATOMIC_OPEN
8303 | NFS_CAP_CHANGE_ATTR
8304 | NFS_CAP_POSIX_LOCK,
8305 .init_client = nfs40_init_client,
8306 .shutdown_client = nfs40_shutdown_client,
8307 .match_stateid = nfs4_match_stateid,
8308 .find_root_sec = nfs4_find_root_sec,
8309 .free_lock_state = nfs4_release_lockowner,
8310 .call_sync_ops = &nfs40_call_sync_ops,
8311 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8312 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8313 .state_renewal_ops = &nfs40_state_renewal_ops,
8314 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8317 #if defined(CONFIG_NFS_V4_1)
8318 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8320 .init_caps = NFS_CAP_READDIRPLUS
8321 | NFS_CAP_ATOMIC_OPEN
8322 | NFS_CAP_CHANGE_ATTR
8323 | NFS_CAP_POSIX_LOCK
8324 | NFS_CAP_STATEID_NFSV41
8325 | NFS_CAP_ATOMIC_OPEN_V1,
8326 .init_client = nfs41_init_client,
8327 .shutdown_client = nfs41_shutdown_client,
8328 .match_stateid = nfs41_match_stateid,
8329 .find_root_sec = nfs41_find_root_sec,
8330 .free_lock_state = nfs41_free_lock_state,
8331 .call_sync_ops = &nfs41_call_sync_ops,
8332 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8333 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8334 .state_renewal_ops = &nfs41_state_renewal_ops,
8335 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8339 #if defined(CONFIG_NFS_V4_2)
8340 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8342 .init_caps = NFS_CAP_READDIRPLUS
8343 | NFS_CAP_ATOMIC_OPEN
8344 | NFS_CAP_CHANGE_ATTR
8345 | NFS_CAP_POSIX_LOCK
8346 | NFS_CAP_STATEID_NFSV41
8347 | NFS_CAP_ATOMIC_OPEN_V1,
8348 .init_client = nfs41_init_client,
8349 .shutdown_client = nfs41_shutdown_client,
8350 .match_stateid = nfs41_match_stateid,
8351 .find_root_sec = nfs41_find_root_sec,
8352 .free_lock_state = nfs41_free_lock_state,
8353 .call_sync_ops = &nfs41_call_sync_ops,
8354 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8355 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8356 .state_renewal_ops = &nfs41_state_renewal_ops,
8360 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8361 [0] = &nfs_v4_0_minor_ops,
8362 #if defined(CONFIG_NFS_V4_1)
8363 [1] = &nfs_v4_1_minor_ops,
8365 #if defined(CONFIG_NFS_V4_2)
8366 [2] = &nfs_v4_2_minor_ops,
8370 static const struct inode_operations nfs4_dir_inode_operations = {
8371 .create = nfs_create,
8372 .lookup = nfs_lookup,
8373 .atomic_open = nfs_atomic_open,
8375 .unlink = nfs_unlink,
8376 .symlink = nfs_symlink,
8380 .rename = nfs_rename,
8381 .permission = nfs_permission,
8382 .getattr = nfs_getattr,
8383 .setattr = nfs_setattr,
8384 .getxattr = generic_getxattr,
8385 .setxattr = generic_setxattr,
8386 .listxattr = generic_listxattr,
8387 .removexattr = generic_removexattr,
8390 static const struct inode_operations nfs4_file_inode_operations = {
8391 .permission = nfs_permission,
8392 .getattr = nfs_getattr,
8393 .setattr = nfs_setattr,
8394 .getxattr = generic_getxattr,
8395 .setxattr = generic_setxattr,
8396 .listxattr = generic_listxattr,
8397 .removexattr = generic_removexattr,
8400 const struct nfs_rpc_ops nfs_v4_clientops = {
8401 .version = 4, /* protocol version */
8402 .dentry_ops = &nfs4_dentry_operations,
8403 .dir_inode_ops = &nfs4_dir_inode_operations,
8404 .file_inode_ops = &nfs4_file_inode_operations,
8405 .file_ops = &nfs4_file_operations,
8406 .getroot = nfs4_proc_get_root,
8407 .submount = nfs4_submount,
8408 .try_mount = nfs4_try_mount,
8409 .getattr = nfs4_proc_getattr,
8410 .setattr = nfs4_proc_setattr,
8411 .lookup = nfs4_proc_lookup,
8412 .access = nfs4_proc_access,
8413 .readlink = nfs4_proc_readlink,
8414 .create = nfs4_proc_create,
8415 .remove = nfs4_proc_remove,
8416 .unlink_setup = nfs4_proc_unlink_setup,
8417 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8418 .unlink_done = nfs4_proc_unlink_done,
8419 .rename_setup = nfs4_proc_rename_setup,
8420 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8421 .rename_done = nfs4_proc_rename_done,
8422 .link = nfs4_proc_link,
8423 .symlink = nfs4_proc_symlink,
8424 .mkdir = nfs4_proc_mkdir,
8425 .rmdir = nfs4_proc_remove,
8426 .readdir = nfs4_proc_readdir,
8427 .mknod = nfs4_proc_mknod,
8428 .statfs = nfs4_proc_statfs,
8429 .fsinfo = nfs4_proc_fsinfo,
8430 .pathconf = nfs4_proc_pathconf,
8431 .set_capabilities = nfs4_server_capabilities,
8432 .decode_dirent = nfs4_decode_dirent,
8433 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8434 .read_setup = nfs4_proc_read_setup,
8435 .read_done = nfs4_read_done,
8436 .write_setup = nfs4_proc_write_setup,
8437 .write_done = nfs4_write_done,
8438 .commit_setup = nfs4_proc_commit_setup,
8439 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8440 .commit_done = nfs4_commit_done,
8441 .lock = nfs4_proc_lock,
8442 .clear_acl_cache = nfs4_zap_acl_attr,
8443 .close_context = nfs4_close_context,
8444 .open_context = nfs4_atomic_open,
8445 .have_delegation = nfs4_have_delegation,
8446 .return_delegation = nfs4_inode_return_delegation,
8447 .alloc_client = nfs4_alloc_client,
8448 .init_client = nfs4_init_client,
8449 .free_client = nfs4_free_client,
8450 .create_server = nfs4_create_server,
8451 .clone_server = nfs_clone_server,
8454 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8455 .prefix = XATTR_NAME_NFSV4_ACL,
8456 .list = nfs4_xattr_list_nfs4_acl,
8457 .get = nfs4_xattr_get_nfs4_acl,
8458 .set = nfs4_xattr_set_nfs4_acl,
8461 const struct xattr_handler *nfs4_xattr_handlers[] = {
8462 &nfs4_xattr_nfs4_acl_handler,
8463 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8464 &nfs4_xattr_nfs4_label_handler,