4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 static int _nfs4_proc_open(struct nfs4_opendata *data);
78 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
79 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
80 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
81 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
82 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
84 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
88 #ifdef CONFIG_NFS_V4_1
89 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
95 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
96 static inline struct nfs4_label *
97 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
108 err = security_dentry_init_security(dentry, sattr->ia_mode,
109 &dentry->d_name, (void **)&label->label, &label->len);
116 nfs4_label_release_security(struct nfs4_label *label)
119 security_release_secctx(label->label, label->len);
121 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
124 return server->attr_bitmask;
126 return server->attr_bitmask_nl;
129 static inline struct nfs4_label *
130 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
131 struct iattr *sattr, struct nfs4_label *l)
134 nfs4_label_release_security(struct nfs4_label *label)
137 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138 { return server->attr_bitmask; }
141 /* Prevent leaks of NFSv4 errors into userland */
142 static int nfs4_map_errors(int err)
147 case -NFS4ERR_RESOURCE:
148 case -NFS4ERR_LAYOUTTRYLATER:
149 case -NFS4ERR_RECALLCONFLICT:
151 case -NFS4ERR_WRONGSEC:
152 case -NFS4ERR_WRONG_CRED:
154 case -NFS4ERR_BADOWNER:
155 case -NFS4ERR_BADNAME:
157 case -NFS4ERR_SHARE_DENIED:
159 case -NFS4ERR_MINOR_VERS_MISMATCH:
160 return -EPROTONOSUPPORT;
161 case -NFS4ERR_ACCESS:
163 case -NFS4ERR_FILE_OPEN:
166 dprintk("%s could not handle NFSv4 error %d\n",
174 * This is our standard bitmap for GETATTR requests.
176 const u32 nfs4_fattr_bitmap[3] = {
178 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_FILEID,
183 | FATTR4_WORD1_NUMLINKS
185 | FATTR4_WORD1_OWNER_GROUP
186 | FATTR4_WORD1_RAWDEV
187 | FATTR4_WORD1_SPACE_USED
188 | FATTR4_WORD1_TIME_ACCESS
189 | FATTR4_WORD1_TIME_METADATA
190 | FATTR4_WORD1_TIME_MODIFY,
191 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
196 static const u32 nfs4_pnfs_open_bitmap[3] = {
198 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_FILEID,
203 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
214 static const u32 nfs4_open_noattr_bitmap[3] = {
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
220 const u32 nfs4_statfs_bitmap[3] = {
221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
229 const u32 nfs4_pathconf_bitmap[3] = {
231 | FATTR4_WORD0_MAXNAME,
235 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
239 FATTR4_WORD1_TIME_DELTA
240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
244 const u32 nfs4_fs_locations_bitmap[3] = {
246 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
252 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
263 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
264 struct nfs4_readdir_arg *readdir)
269 readdir->cookie = cookie;
270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
286 start = p = kmap_atomic(*readdir->pages);
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
298 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
310 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
314 kunmap_atomic(start);
317 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
324 *timeout = NFS4_POLL_RETRY_MIN;
325 if (*timeout > NFS4_POLL_RETRY_MAX)
326 *timeout = NFS4_POLL_RETRY_MAX;
327 freezable_schedule_timeout_killable_unsafe(*timeout);
328 if (fatal_signal_pending(current))
334 /* This is the error handling routine for processes that are allowed
337 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
339 struct nfs_client *clp = server->nfs_client;
340 struct nfs4_state *state = exception->state;
341 struct inode *inode = exception->inode;
344 exception->retry = 0;
348 case -NFS4ERR_OPENMODE:
349 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
350 nfs4_inode_return_delegation(inode);
351 exception->retry = 1;
356 ret = nfs4_schedule_stateid_recovery(server, state);
359 goto wait_on_recovery;
360 case -NFS4ERR_DELEG_REVOKED:
361 case -NFS4ERR_ADMIN_REVOKED:
362 case -NFS4ERR_BAD_STATEID:
363 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
364 nfs_remove_bad_delegation(inode);
365 exception->retry = 1;
370 ret = nfs4_schedule_stateid_recovery(server, state);
373 goto wait_on_recovery;
374 case -NFS4ERR_EXPIRED:
376 ret = nfs4_schedule_stateid_recovery(server, state);
380 case -NFS4ERR_STALE_STATEID:
381 case -NFS4ERR_STALE_CLIENTID:
382 nfs4_schedule_lease_recovery(clp);
383 goto wait_on_recovery;
385 ret = nfs4_schedule_migration_recovery(server);
388 goto wait_on_recovery;
389 case -NFS4ERR_LEASE_MOVED:
390 nfs4_schedule_lease_moved_recovery(clp);
391 goto wait_on_recovery;
392 #if defined(CONFIG_NFS_V4_1)
393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
403 goto wait_on_recovery;
404 #endif /* defined(CONFIG_NFS_V4_1) */
405 case -NFS4ERR_FILE_OPEN:
406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
415 ret = nfs4_delay(server->client, &exception->timeout);
418 case -NFS4ERR_RETRY_UNCACHED_REP:
419 case -NFS4ERR_OLD_STATEID:
420 exception->retry = 1;
422 case -NFS4ERR_BADOWNER:
423 /* The following works around a Linux server bug! */
424 case -NFS4ERR_BADNAME:
425 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
426 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
427 exception->retry = 1;
428 printk(KERN_WARNING "NFS: v4 server %s "
429 "does not accept raw "
431 "Reenabling the idmapper.\n",
432 server->nfs_client->cl_hostname);
435 /* We failed to handle the error */
436 return nfs4_map_errors(ret);
438 ret = nfs4_wait_clnt_recover(clp);
439 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
442 exception->retry = 1;
447 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
448 * or 'false' otherwise.
450 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
452 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
454 if (flavor == RPC_AUTH_GSS_KRB5I ||
455 flavor == RPC_AUTH_GSS_KRB5P)
461 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
463 spin_lock(&clp->cl_lock);
464 if (time_before(clp->cl_last_renewal,timestamp))
465 clp->cl_last_renewal = timestamp;
466 spin_unlock(&clp->cl_lock);
469 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
471 do_renew_lease(server->nfs_client, timestamp);
474 struct nfs4_call_sync_data {
475 const struct nfs_server *seq_server;
476 struct nfs4_sequence_args *seq_args;
477 struct nfs4_sequence_res *seq_res;
480 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
481 struct nfs4_sequence_res *res, int cache_reply)
483 args->sa_slot = NULL;
484 args->sa_cache_this = cache_reply;
485 args->sa_privileged = 0;
490 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
492 args->sa_privileged = 1;
495 static int nfs40_setup_sequence(const struct nfs_server *server,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 struct rpc_task *task)
500 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
501 struct nfs4_slot *slot;
503 /* slot already allocated? */
504 if (res->sr_slot != NULL)
507 spin_lock(&tbl->slot_tbl_lock);
508 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
511 slot = nfs4_alloc_slot(tbl);
513 if (slot == ERR_PTR(-ENOMEM))
514 task->tk_timeout = HZ >> 2;
517 spin_unlock(&tbl->slot_tbl_lock);
519 args->sa_slot = slot;
523 rpc_call_start(task);
527 if (args->sa_privileged)
528 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
529 NULL, RPC_PRIORITY_PRIVILEGED);
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
536 static int nfs40_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
539 struct nfs4_slot *slot = res->sr_slot;
540 struct nfs4_slot_table *tbl;
546 spin_lock(&tbl->slot_tbl_lock);
547 if (!nfs41_wake_and_assign_slot(tbl, slot))
548 nfs4_free_slot(tbl, slot);
549 spin_unlock(&tbl->slot_tbl_lock);
556 #if defined(CONFIG_NFS_V4_1)
558 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
560 struct nfs4_session *session;
561 struct nfs4_slot_table *tbl;
562 struct nfs4_slot *slot = res->sr_slot;
563 bool send_new_highest_used_slotid = false;
566 session = tbl->session;
568 spin_lock(&tbl->slot_tbl_lock);
569 /* Be nice to the server: try to ensure that the last transmitted
570 * value for highest_user_slotid <= target_highest_slotid
572 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
573 send_new_highest_used_slotid = true;
575 if (nfs41_wake_and_assign_slot(tbl, slot)) {
576 send_new_highest_used_slotid = false;
579 nfs4_free_slot(tbl, slot);
581 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
582 send_new_highest_used_slotid = false;
584 spin_unlock(&tbl->slot_tbl_lock);
586 if (send_new_highest_used_slotid)
587 nfs41_server_notify_highest_slotid_update(session->clp);
590 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
592 struct nfs4_session *session;
593 struct nfs4_slot *slot = res->sr_slot;
594 struct nfs_client *clp;
595 bool interrupted = false;
600 /* don't increment the sequence number if the task wasn't sent */
601 if (!RPC_WAS_SENT(task))
604 session = slot->table->session;
606 if (slot->interrupted) {
607 slot->interrupted = 0;
611 trace_nfs4_sequence_done(session, res);
612 /* Check the SEQUENCE operation status */
613 switch (res->sr_status) {
615 /* Update the slot's sequence and clientid lease timer */
618 do_renew_lease(clp, res->sr_timestamp);
619 /* Check sequence flags */
620 if (res->sr_status_flags != 0)
621 nfs4_schedule_lease_recovery(clp);
622 nfs41_update_target_slotid(slot->table, slot, res);
626 * sr_status remains 1 if an RPC level error occurred.
627 * The server may or may not have processed the sequence
629 * Mark the slot as having hosted an interrupted RPC call.
631 slot->interrupted = 1;
634 /* The server detected a resend of the RPC call and
635 * returned NFS4ERR_DELAY as per Section 2.10.6.2
638 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
643 case -NFS4ERR_BADSLOT:
645 * The slot id we used was probably retired. Try again
646 * using a different slot id.
649 case -NFS4ERR_SEQ_MISORDERED:
651 * Was the last operation on this sequence interrupted?
652 * If so, retry after bumping the sequence number.
659 * Could this slot have been previously retired?
660 * If so, then the server may be expecting seq_nr = 1!
662 if (slot->seq_nr != 1) {
667 case -NFS4ERR_SEQ_FALSE_RETRY:
671 /* Just update the slot sequence no. */
675 /* The session may be reset by one of the error handlers. */
676 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
677 nfs41_sequence_free_slot(res);
681 if (rpc_restart_call_prepare(task)) {
687 if (!rpc_restart_call(task))
689 rpc_delay(task, NFS4_POLL_RETRY_MAX);
692 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
694 static int nfs4_sequence_done(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
697 if (res->sr_slot == NULL)
699 if (!res->sr_slot->table->session)
700 return nfs40_sequence_done(task, res);
701 return nfs41_sequence_done(task, res);
704 int nfs41_setup_sequence(struct nfs4_session *session,
705 struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res,
707 struct rpc_task *task)
709 struct nfs4_slot *slot;
710 struct nfs4_slot_table *tbl;
712 dprintk("--> %s\n", __func__);
713 /* slot already allocated? */
714 if (res->sr_slot != NULL)
717 tbl = &session->fc_slot_table;
719 task->tk_timeout = 0;
721 spin_lock(&tbl->slot_tbl_lock);
722 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
723 !args->sa_privileged) {
724 /* The state manager will wait until the slot table is empty */
725 dprintk("%s session is draining\n", __func__);
729 slot = nfs4_alloc_slot(tbl);
731 /* If out of memory, try again in 1/4 second */
732 if (slot == ERR_PTR(-ENOMEM))
733 task->tk_timeout = HZ >> 2;
734 dprintk("<-- %s: no free slots\n", __func__);
737 spin_unlock(&tbl->slot_tbl_lock);
739 args->sa_slot = slot;
741 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
742 slot->slot_nr, slot->seq_nr);
745 res->sr_timestamp = jiffies;
746 res->sr_status_flags = 0;
748 * sr_status is only set in decode_sequence, and so will remain
749 * set to 1 if an rpc level failure occurs.
752 trace_nfs4_setup_sequence(session, args);
754 rpc_call_start(task);
757 /* Privileged tasks are queued with top priority */
758 if (args->sa_privileged)
759 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
760 NULL, RPC_PRIORITY_PRIVILEGED);
762 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
763 spin_unlock(&tbl->slot_tbl_lock);
766 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
768 static int nfs4_setup_sequence(const struct nfs_server *server,
769 struct nfs4_sequence_args *args,
770 struct nfs4_sequence_res *res,
771 struct rpc_task *task)
773 struct nfs4_session *session = nfs4_get_session(server);
777 return nfs40_setup_sequence(server, args, res, task);
779 dprintk("--> %s clp %p session %p sr_slot %u\n",
780 __func__, session->clp, session, res->sr_slot ?
781 res->sr_slot->slot_nr : NFS4_NO_SLOT);
783 ret = nfs41_setup_sequence(session, args, res, task);
785 dprintk("<-- %s status=%d\n", __func__, ret);
789 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
791 struct nfs4_call_sync_data *data = calldata;
792 struct nfs4_session *session = nfs4_get_session(data->seq_server);
794 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
796 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
799 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
801 struct nfs4_call_sync_data *data = calldata;
803 nfs41_sequence_done(task, data->seq_res);
806 static const struct rpc_call_ops nfs41_call_sync_ops = {
807 .rpc_call_prepare = nfs41_call_sync_prepare,
808 .rpc_call_done = nfs41_call_sync_done,
811 #else /* !CONFIG_NFS_V4_1 */
813 static int nfs4_setup_sequence(const struct nfs_server *server,
814 struct nfs4_sequence_args *args,
815 struct nfs4_sequence_res *res,
816 struct rpc_task *task)
818 return nfs40_setup_sequence(server, args, res, task);
821 static int nfs4_sequence_done(struct rpc_task *task,
822 struct nfs4_sequence_res *res)
824 return nfs40_sequence_done(task, res);
827 #endif /* !CONFIG_NFS_V4_1 */
829 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
831 struct nfs4_call_sync_data *data = calldata;
832 nfs4_setup_sequence(data->seq_server,
833 data->seq_args, data->seq_res, task);
836 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
838 struct nfs4_call_sync_data *data = calldata;
839 nfs4_sequence_done(task, data->seq_res);
842 static const struct rpc_call_ops nfs40_call_sync_ops = {
843 .rpc_call_prepare = nfs40_call_sync_prepare,
844 .rpc_call_done = nfs40_call_sync_done,
847 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
848 struct nfs_server *server,
849 struct rpc_message *msg,
850 struct nfs4_sequence_args *args,
851 struct nfs4_sequence_res *res)
854 struct rpc_task *task;
855 struct nfs_client *clp = server->nfs_client;
856 struct nfs4_call_sync_data data = {
857 .seq_server = server,
861 struct rpc_task_setup task_setup = {
864 .callback_ops = clp->cl_mvops->call_sync_ops,
865 .callback_data = &data
868 task = rpc_run_task(&task_setup);
872 ret = task->tk_status;
879 int nfs4_call_sync(struct rpc_clnt *clnt,
880 struct nfs_server *server,
881 struct rpc_message *msg,
882 struct nfs4_sequence_args *args,
883 struct nfs4_sequence_res *res,
886 nfs4_init_sequence(args, res, cache_reply);
887 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
890 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
892 struct nfs_inode *nfsi = NFS_I(dir);
894 spin_lock(&dir->i_lock);
895 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
896 if (!cinfo->atomic || cinfo->before != dir->i_version)
897 nfs_force_lookup_revalidate(dir);
898 dir->i_version = cinfo->after;
899 nfs_fscache_invalidate(dir);
900 spin_unlock(&dir->i_lock);
903 struct nfs4_opendata {
905 struct nfs_openargs o_arg;
906 struct nfs_openres o_res;
907 struct nfs_open_confirmargs c_arg;
908 struct nfs_open_confirmres c_res;
909 struct nfs4_string owner_name;
910 struct nfs4_string group_name;
911 struct nfs_fattr f_attr;
912 struct nfs4_label *f_label;
914 struct dentry *dentry;
915 struct nfs4_state_owner *owner;
916 struct nfs4_state *state;
918 unsigned long timestamp;
919 unsigned int rpc_done : 1;
920 unsigned int file_created : 1;
921 unsigned int is_recover : 1;
926 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
927 int err, struct nfs4_exception *exception)
931 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
933 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
934 exception->retry = 1;
938 static enum open_claim_type4
939 nfs4_map_atomic_open_claim(struct nfs_server *server,
940 enum open_claim_type4 claim)
942 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
947 case NFS4_OPEN_CLAIM_FH:
948 return NFS4_OPEN_CLAIM_NULL;
949 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
950 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
951 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
952 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
956 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
958 p->o_res.f_attr = &p->f_attr;
959 p->o_res.f_label = p->f_label;
960 p->o_res.seqid = p->o_arg.seqid;
961 p->c_res.seqid = p->c_arg.seqid;
962 p->o_res.server = p->o_arg.server;
963 p->o_res.access_request = p->o_arg.access;
964 nfs_fattr_init(&p->f_attr);
965 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
968 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
969 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
970 const struct iattr *attrs,
971 struct nfs4_label *label,
972 enum open_claim_type4 claim,
975 struct dentry *parent = dget_parent(dentry);
976 struct inode *dir = parent->d_inode;
977 struct nfs_server *server = NFS_SERVER(dir);
978 struct nfs4_opendata *p;
980 p = kzalloc(sizeof(*p), gfp_mask);
984 p->f_label = nfs4_label_alloc(server, gfp_mask);
985 if (IS_ERR(p->f_label))
988 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
989 if (p->o_arg.seqid == NULL)
991 nfs_sb_active(dentry->d_sb);
992 p->dentry = dget(dentry);
995 atomic_inc(&sp->so_count);
996 p->o_arg.open_flags = flags;
997 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
998 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
999 * will return permission denied for all bits until close */
1000 if (!(flags & O_EXCL)) {
1001 /* ask server to check for all possible rights as results
1003 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1004 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1006 p->o_arg.clientid = server->nfs_client->cl_clientid;
1007 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1008 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1009 p->o_arg.name = &dentry->d_name;
1010 p->o_arg.server = server;
1011 p->o_arg.bitmask = nfs4_bitmask(server, label);
1012 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1013 p->o_arg.label = label;
1014 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1015 switch (p->o_arg.claim) {
1016 case NFS4_OPEN_CLAIM_NULL:
1017 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1018 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1019 p->o_arg.fh = NFS_FH(dir);
1021 case NFS4_OPEN_CLAIM_PREVIOUS:
1022 case NFS4_OPEN_CLAIM_FH:
1023 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1024 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1025 p->o_arg.fh = NFS_FH(dentry->d_inode);
1027 if (attrs != NULL && attrs->ia_valid != 0) {
1030 p->o_arg.u.attrs = &p->attrs;
1031 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1034 verf[1] = current->pid;
1035 memcpy(p->o_arg.u.verifier.data, verf,
1036 sizeof(p->o_arg.u.verifier.data));
1038 p->c_arg.fh = &p->o_res.fh;
1039 p->c_arg.stateid = &p->o_res.stateid;
1040 p->c_arg.seqid = p->o_arg.seqid;
1041 nfs4_init_opendata_res(p);
1042 kref_init(&p->kref);
1046 nfs4_label_free(p->f_label);
1054 static void nfs4_opendata_free(struct kref *kref)
1056 struct nfs4_opendata *p = container_of(kref,
1057 struct nfs4_opendata, kref);
1058 struct super_block *sb = p->dentry->d_sb;
1060 nfs_free_seqid(p->o_arg.seqid);
1061 if (p->state != NULL)
1062 nfs4_put_open_state(p->state);
1063 nfs4_put_state_owner(p->owner);
1065 nfs4_label_free(p->f_label);
1069 nfs_sb_deactive(sb);
1070 nfs_fattr_free_names(&p->f_attr);
1074 static void nfs4_opendata_put(struct nfs4_opendata *p)
1077 kref_put(&p->kref, nfs4_opendata_free);
1080 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1084 ret = rpc_wait_for_completion_task(task);
1088 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1092 if (open_mode & (O_EXCL|O_TRUNC))
1094 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1096 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1097 && state->n_rdonly != 0;
1100 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1101 && state->n_wronly != 0;
1103 case FMODE_READ|FMODE_WRITE:
1104 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1105 && state->n_rdwr != 0;
1111 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1113 if (delegation == NULL)
1115 if ((delegation->type & fmode) != fmode)
1117 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1119 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1121 nfs_mark_delegation_referenced(delegation);
1125 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1134 case FMODE_READ|FMODE_WRITE:
1137 nfs4_state_set_mode_locked(state, state->state | fmode);
1140 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1141 nfs4_stateid *stateid)
1143 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1145 if (!nfs4_stateid_match_other(stateid, &state->open_stateid))
1147 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1152 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1156 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1159 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1161 case FMODE_READ|FMODE_WRITE:
1162 set_bit(NFS_O_RDWR_STATE, &state->flags);
1164 if (!nfs_need_update_open_stateid(state, stateid))
1166 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1167 nfs4_stateid_copy(&state->stateid, stateid);
1168 nfs4_stateid_copy(&state->open_stateid, stateid);
1171 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1173 write_seqlock(&state->seqlock);
1174 nfs_set_open_stateid_locked(state, stateid, fmode);
1175 write_sequnlock(&state->seqlock);
1178 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1181 * Protect the call to nfs4_state_set_mode_locked and
1182 * serialise the stateid update
1184 write_seqlock(&state->seqlock);
1185 if (deleg_stateid != NULL) {
1186 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1187 set_bit(NFS_DELEGATED_STATE, &state->flags);
1189 if (open_stateid != NULL)
1190 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1191 write_sequnlock(&state->seqlock);
1192 spin_lock(&state->owner->so_lock);
1193 update_open_stateflags(state, fmode);
1194 spin_unlock(&state->owner->so_lock);
1197 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1199 struct nfs_inode *nfsi = NFS_I(state->inode);
1200 struct nfs_delegation *deleg_cur;
1203 fmode &= (FMODE_READ|FMODE_WRITE);
1206 deleg_cur = rcu_dereference(nfsi->delegation);
1207 if (deleg_cur == NULL)
1210 spin_lock(&deleg_cur->lock);
1211 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1212 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1213 (deleg_cur->type & fmode) != fmode)
1214 goto no_delegation_unlock;
1216 if (delegation == NULL)
1217 delegation = &deleg_cur->stateid;
1218 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1219 goto no_delegation_unlock;
1221 nfs_mark_delegation_referenced(deleg_cur);
1222 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1224 no_delegation_unlock:
1225 spin_unlock(&deleg_cur->lock);
1229 if (!ret && open_stateid != NULL) {
1230 __update_open_stateid(state, open_stateid, NULL, fmode);
1238 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1240 struct nfs_delegation *delegation;
1243 delegation = rcu_dereference(NFS_I(inode)->delegation);
1244 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1249 nfs4_inode_return_delegation(inode);
1252 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1254 struct nfs4_state *state = opendata->state;
1255 struct nfs_inode *nfsi = NFS_I(state->inode);
1256 struct nfs_delegation *delegation;
1257 int open_mode = opendata->o_arg.open_flags;
1258 fmode_t fmode = opendata->o_arg.fmode;
1259 nfs4_stateid stateid;
1263 if (can_open_cached(state, fmode, open_mode)) {
1264 spin_lock(&state->owner->so_lock);
1265 if (can_open_cached(state, fmode, open_mode)) {
1266 update_open_stateflags(state, fmode);
1267 spin_unlock(&state->owner->so_lock);
1268 goto out_return_state;
1270 spin_unlock(&state->owner->so_lock);
1273 delegation = rcu_dereference(nfsi->delegation);
1274 if (!can_open_delegated(delegation, fmode)) {
1278 /* Save the delegation */
1279 nfs4_stateid_copy(&stateid, &delegation->stateid);
1281 nfs_release_seqid(opendata->o_arg.seqid);
1282 if (!opendata->is_recover) {
1283 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1289 /* Try to update the stateid using the delegation */
1290 if (update_open_stateid(state, NULL, &stateid, fmode))
1291 goto out_return_state;
1294 return ERR_PTR(ret);
1296 atomic_inc(&state->count);
1301 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1303 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1304 struct nfs_delegation *delegation;
1305 int delegation_flags = 0;
1308 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1310 delegation_flags = delegation->flags;
1312 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1313 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1314 "returning a delegation for "
1315 "OPEN(CLAIM_DELEGATE_CUR)\n",
1317 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1318 nfs_inode_set_delegation(state->inode,
1319 data->owner->so_cred,
1322 nfs_inode_reclaim_delegation(state->inode,
1323 data->owner->so_cred,
1328 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1329 * and update the nfs4_state.
1331 static struct nfs4_state *
1332 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1334 struct inode *inode = data->state->inode;
1335 struct nfs4_state *state = data->state;
1338 if (!data->rpc_done) {
1339 if (data->rpc_status) {
1340 ret = data->rpc_status;
1343 /* cached opens have already been processed */
1347 ret = nfs_refresh_inode(inode, &data->f_attr);
1351 if (data->o_res.delegation_type != 0)
1352 nfs4_opendata_check_deleg(data, state);
1354 update_open_stateid(state, &data->o_res.stateid, NULL,
1356 atomic_inc(&state->count);
1360 return ERR_PTR(ret);
1364 static struct nfs4_state *
1365 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1367 struct inode *inode;
1368 struct nfs4_state *state = NULL;
1371 if (!data->rpc_done) {
1372 state = nfs4_try_open_cached(data);
1377 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1379 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1380 ret = PTR_ERR(inode);
1384 state = nfs4_get_open_state(inode, data->owner);
1387 if (data->o_res.delegation_type != 0)
1388 nfs4_opendata_check_deleg(data, state);
1389 update_open_stateid(state, &data->o_res.stateid, NULL,
1393 nfs_release_seqid(data->o_arg.seqid);
1398 return ERR_PTR(ret);
1401 static struct nfs4_state *
1402 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1404 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1405 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1406 return _nfs4_opendata_to_nfs4_state(data);
1409 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1411 struct nfs_inode *nfsi = NFS_I(state->inode);
1412 struct nfs_open_context *ctx;
1414 spin_lock(&state->inode->i_lock);
1415 list_for_each_entry(ctx, &nfsi->open_files, list) {
1416 if (ctx->state != state)
1418 get_nfs_open_context(ctx);
1419 spin_unlock(&state->inode->i_lock);
1422 spin_unlock(&state->inode->i_lock);
1423 return ERR_PTR(-ENOENT);
1426 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1427 struct nfs4_state *state, enum open_claim_type4 claim)
1429 struct nfs4_opendata *opendata;
1431 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1432 NULL, NULL, claim, GFP_NOFS);
1433 if (opendata == NULL)
1434 return ERR_PTR(-ENOMEM);
1435 opendata->state = state;
1436 atomic_inc(&state->count);
1440 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1442 struct nfs4_state *newstate;
1445 opendata->o_arg.open_flags = 0;
1446 opendata->o_arg.fmode = fmode;
1447 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1448 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1449 nfs4_init_opendata_res(opendata);
1450 ret = _nfs4_recover_proc_open(opendata);
1453 newstate = nfs4_opendata_to_nfs4_state(opendata);
1454 if (IS_ERR(newstate))
1455 return PTR_ERR(newstate);
1456 nfs4_close_state(newstate, fmode);
1461 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1463 struct nfs4_state *newstate;
1466 /* memory barrier prior to reading state->n_* */
1467 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1468 clear_bit(NFS_OPEN_STATE, &state->flags);
1470 if (state->n_rdwr != 0) {
1471 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1472 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1475 if (newstate != state)
1478 if (state->n_wronly != 0) {
1479 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1480 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1483 if (newstate != state)
1486 if (state->n_rdonly != 0) {
1487 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1488 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1491 if (newstate != state)
1495 * We may have performed cached opens for all three recoveries.
1496 * Check if we need to update the current stateid.
1498 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1499 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1500 write_seqlock(&state->seqlock);
1501 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1502 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1503 write_sequnlock(&state->seqlock);
1510 * reclaim state on the server after a reboot.
1512 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1514 struct nfs_delegation *delegation;
1515 struct nfs4_opendata *opendata;
1516 fmode_t delegation_type = 0;
1519 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1520 NFS4_OPEN_CLAIM_PREVIOUS);
1521 if (IS_ERR(opendata))
1522 return PTR_ERR(opendata);
1524 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1525 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1526 delegation_type = delegation->type;
1528 opendata->o_arg.u.delegation_type = delegation_type;
1529 status = nfs4_open_recover(opendata, state);
1530 nfs4_opendata_put(opendata);
1534 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1536 struct nfs_server *server = NFS_SERVER(state->inode);
1537 struct nfs4_exception exception = { };
1540 err = _nfs4_do_open_reclaim(ctx, state);
1541 trace_nfs4_open_reclaim(ctx, 0, err);
1542 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1544 if (err != -NFS4ERR_DELAY)
1546 nfs4_handle_exception(server, err, &exception);
1547 } while (exception.retry);
1551 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1553 struct nfs_open_context *ctx;
1556 ctx = nfs4_state_find_open_context(state);
1559 ret = nfs4_do_open_reclaim(ctx, state);
1560 put_nfs_open_context(ctx);
1564 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1568 printk(KERN_ERR "NFS: %s: unhandled error "
1569 "%d.\n", __func__, err);
1574 case -NFS4ERR_BADSESSION:
1575 case -NFS4ERR_BADSLOT:
1576 case -NFS4ERR_BAD_HIGH_SLOT:
1577 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1578 case -NFS4ERR_DEADSESSION:
1579 set_bit(NFS_DELEGATED_STATE, &state->flags);
1580 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1582 case -NFS4ERR_STALE_CLIENTID:
1583 case -NFS4ERR_STALE_STATEID:
1584 set_bit(NFS_DELEGATED_STATE, &state->flags);
1585 case -NFS4ERR_EXPIRED:
1586 /* Don't recall a delegation if it was lost */
1587 nfs4_schedule_lease_recovery(server->nfs_client);
1589 case -NFS4ERR_MOVED:
1590 nfs4_schedule_migration_recovery(server);
1592 case -NFS4ERR_LEASE_MOVED:
1593 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1595 case -NFS4ERR_DELEG_REVOKED:
1596 case -NFS4ERR_ADMIN_REVOKED:
1597 case -NFS4ERR_BAD_STATEID:
1598 case -NFS4ERR_OPENMODE:
1599 nfs_inode_find_state_and_recover(state->inode,
1601 nfs4_schedule_stateid_recovery(server, state);
1603 case -NFS4ERR_DELAY:
1604 case -NFS4ERR_GRACE:
1605 set_bit(NFS_DELEGATED_STATE, &state->flags);
1609 case -NFS4ERR_DENIED:
1610 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1616 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1618 struct nfs_server *server = NFS_SERVER(state->inode);
1619 struct nfs4_opendata *opendata;
1622 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1623 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1624 if (IS_ERR(opendata))
1625 return PTR_ERR(opendata);
1626 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1627 err = nfs4_open_recover(opendata, state);
1628 nfs4_opendata_put(opendata);
1629 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1632 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1634 struct nfs4_opendata *data = calldata;
1636 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1637 &data->c_res.seq_res, task);
1640 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1642 struct nfs4_opendata *data = calldata;
1644 nfs40_sequence_done(task, &data->c_res.seq_res);
1646 data->rpc_status = task->tk_status;
1647 if (data->rpc_status == 0) {
1648 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1649 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1650 renew_lease(data->o_res.server, data->timestamp);
1655 static void nfs4_open_confirm_release(void *calldata)
1657 struct nfs4_opendata *data = calldata;
1658 struct nfs4_state *state = NULL;
1660 /* If this request hasn't been cancelled, do nothing */
1661 if (data->cancelled == 0)
1663 /* In case of error, no cleanup! */
1664 if (!data->rpc_done)
1666 state = nfs4_opendata_to_nfs4_state(data);
1668 nfs4_close_state(state, data->o_arg.fmode);
1670 nfs4_opendata_put(data);
1673 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1674 .rpc_call_prepare = nfs4_open_confirm_prepare,
1675 .rpc_call_done = nfs4_open_confirm_done,
1676 .rpc_release = nfs4_open_confirm_release,
1680 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1682 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1684 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1685 struct rpc_task *task;
1686 struct rpc_message msg = {
1687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1688 .rpc_argp = &data->c_arg,
1689 .rpc_resp = &data->c_res,
1690 .rpc_cred = data->owner->so_cred,
1692 struct rpc_task_setup task_setup_data = {
1693 .rpc_client = server->client,
1694 .rpc_message = &msg,
1695 .callback_ops = &nfs4_open_confirm_ops,
1696 .callback_data = data,
1697 .workqueue = nfsiod_workqueue,
1698 .flags = RPC_TASK_ASYNC,
1702 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1703 kref_get(&data->kref);
1705 data->rpc_status = 0;
1706 data->timestamp = jiffies;
1707 task = rpc_run_task(&task_setup_data);
1709 return PTR_ERR(task);
1710 status = nfs4_wait_for_completion_rpc_task(task);
1712 data->cancelled = 1;
1715 status = data->rpc_status;
1720 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1722 struct nfs4_opendata *data = calldata;
1723 struct nfs4_state_owner *sp = data->owner;
1724 struct nfs_client *clp = sp->so_server->nfs_client;
1726 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1729 * Check if we still need to send an OPEN call, or if we can use
1730 * a delegation instead.
1732 if (data->state != NULL) {
1733 struct nfs_delegation *delegation;
1735 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1738 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1739 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1740 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1741 can_open_delegated(delegation, data->o_arg.fmode))
1742 goto unlock_no_action;
1745 /* Update client id. */
1746 data->o_arg.clientid = clp->cl_clientid;
1747 switch (data->o_arg.claim) {
1748 case NFS4_OPEN_CLAIM_PREVIOUS:
1749 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1750 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1751 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1752 case NFS4_OPEN_CLAIM_FH:
1753 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1754 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1756 data->timestamp = jiffies;
1757 if (nfs4_setup_sequence(data->o_arg.server,
1758 &data->o_arg.seq_args,
1759 &data->o_res.seq_res,
1761 nfs_release_seqid(data->o_arg.seqid);
1763 /* Set the create mode (note dependency on the session type) */
1764 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1765 if (data->o_arg.open_flags & O_EXCL) {
1766 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1767 if (nfs4_has_persistent_session(clp))
1768 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1769 else if (clp->cl_mvops->minor_version > 0)
1770 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1776 task->tk_action = NULL;
1778 nfs4_sequence_done(task, &data->o_res.seq_res);
1781 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1783 struct nfs4_opendata *data = calldata;
1785 data->rpc_status = task->tk_status;
1787 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1790 if (task->tk_status == 0) {
1791 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1792 switch (data->o_res.f_attr->mode & S_IFMT) {
1796 data->rpc_status = -ELOOP;
1799 data->rpc_status = -EISDIR;
1802 data->rpc_status = -ENOTDIR;
1805 renew_lease(data->o_res.server, data->timestamp);
1806 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1807 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1812 static void nfs4_open_release(void *calldata)
1814 struct nfs4_opendata *data = calldata;
1815 struct nfs4_state *state = NULL;
1817 /* If this request hasn't been cancelled, do nothing */
1818 if (data->cancelled == 0)
1820 /* In case of error, no cleanup! */
1821 if (data->rpc_status != 0 || !data->rpc_done)
1823 /* In case we need an open_confirm, no cleanup! */
1824 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1826 state = nfs4_opendata_to_nfs4_state(data);
1828 nfs4_close_state(state, data->o_arg.fmode);
1830 nfs4_opendata_put(data);
1833 static const struct rpc_call_ops nfs4_open_ops = {
1834 .rpc_call_prepare = nfs4_open_prepare,
1835 .rpc_call_done = nfs4_open_done,
1836 .rpc_release = nfs4_open_release,
1839 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1841 struct inode *dir = data->dir->d_inode;
1842 struct nfs_server *server = NFS_SERVER(dir);
1843 struct nfs_openargs *o_arg = &data->o_arg;
1844 struct nfs_openres *o_res = &data->o_res;
1845 struct rpc_task *task;
1846 struct rpc_message msg = {
1847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1850 .rpc_cred = data->owner->so_cred,
1852 struct rpc_task_setup task_setup_data = {
1853 .rpc_client = server->client,
1854 .rpc_message = &msg,
1855 .callback_ops = &nfs4_open_ops,
1856 .callback_data = data,
1857 .workqueue = nfsiod_workqueue,
1858 .flags = RPC_TASK_ASYNC,
1862 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1863 kref_get(&data->kref);
1865 data->rpc_status = 0;
1866 data->cancelled = 0;
1867 data->is_recover = 0;
1869 nfs4_set_sequence_privileged(&o_arg->seq_args);
1870 data->is_recover = 1;
1872 task = rpc_run_task(&task_setup_data);
1874 return PTR_ERR(task);
1875 status = nfs4_wait_for_completion_rpc_task(task);
1877 data->cancelled = 1;
1880 status = data->rpc_status;
1886 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1888 struct inode *dir = data->dir->d_inode;
1889 struct nfs_openres *o_res = &data->o_res;
1892 status = nfs4_run_open_task(data, 1);
1893 if (status != 0 || !data->rpc_done)
1896 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1898 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1899 status = _nfs4_proc_open_confirm(data);
1907 static int nfs4_opendata_access(struct rpc_cred *cred,
1908 struct nfs4_opendata *opendata,
1909 struct nfs4_state *state, fmode_t fmode,
1912 struct nfs_access_entry cache;
1915 /* access call failed or for some reason the server doesn't
1916 * support any access modes -- defer access call until later */
1917 if (opendata->o_res.access_supported == 0)
1921 /* don't check MAY_WRITE - a newly created file may not have
1922 * write mode bits, but POSIX allows the creating process to write.
1923 * use openflags to check for exec, because fmode won't
1924 * always have FMODE_EXEC set when file open for exec. */
1925 if (openflags & __FMODE_EXEC) {
1926 /* ONLY check for exec rights */
1928 } else if (fmode & FMODE_READ)
1932 cache.jiffies = jiffies;
1933 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1934 nfs_access_add_cache(state->inode, &cache);
1936 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1939 /* even though OPEN succeeded, access is denied. Close the file */
1940 nfs4_close_state(state, fmode);
1945 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1947 static int _nfs4_proc_open(struct nfs4_opendata *data)
1949 struct inode *dir = data->dir->d_inode;
1950 struct nfs_server *server = NFS_SERVER(dir);
1951 struct nfs_openargs *o_arg = &data->o_arg;
1952 struct nfs_openres *o_res = &data->o_res;
1955 status = nfs4_run_open_task(data, 0);
1956 if (!data->rpc_done)
1959 if (status == -NFS4ERR_BADNAME &&
1960 !(o_arg->open_flags & O_CREAT))
1965 nfs_fattr_map_and_free_names(server, &data->f_attr);
1967 if (o_arg->open_flags & O_CREAT) {
1968 update_changeattr(dir, &o_res->cinfo);
1969 if (o_arg->open_flags & O_EXCL)
1970 data->file_created = 1;
1971 else if (o_res->cinfo.before != o_res->cinfo.after)
1972 data->file_created = 1;
1974 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1975 server->caps &= ~NFS_CAP_POSIX_LOCK;
1976 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1977 status = _nfs4_proc_open_confirm(data);
1981 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1982 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
1986 static int nfs4_recover_expired_lease(struct nfs_server *server)
1988 return nfs4_client_recover_expired_lease(server->nfs_client);
1993 * reclaim state on the server after a network partition.
1994 * Assumes caller holds the appropriate lock
1996 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1998 struct nfs4_opendata *opendata;
2001 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2002 NFS4_OPEN_CLAIM_FH);
2003 if (IS_ERR(opendata))
2004 return PTR_ERR(opendata);
2005 ret = nfs4_open_recover(opendata, state);
2007 d_drop(ctx->dentry);
2008 nfs4_opendata_put(opendata);
2012 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2014 struct nfs_server *server = NFS_SERVER(state->inode);
2015 struct nfs4_exception exception = { };
2019 err = _nfs4_open_expired(ctx, state);
2020 trace_nfs4_open_expired(ctx, 0, err);
2021 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2026 case -NFS4ERR_GRACE:
2027 case -NFS4ERR_DELAY:
2028 nfs4_handle_exception(server, err, &exception);
2031 } while (exception.retry);
2036 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2038 struct nfs_open_context *ctx;
2041 ctx = nfs4_state_find_open_context(state);
2044 ret = nfs4_do_open_expired(ctx, state);
2045 put_nfs_open_context(ctx);
2049 #if defined(CONFIG_NFS_V4_1)
2050 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
2052 struct nfs_server *server = NFS_SERVER(state->inode);
2053 nfs4_stateid *stateid = &state->stateid;
2054 struct nfs_delegation *delegation;
2055 struct rpc_cred *cred = NULL;
2056 int status = -NFS4ERR_BAD_STATEID;
2058 /* If a state reset has been done, test_stateid is unneeded */
2059 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2062 /* Get the delegation credential for use by test/free_stateid */
2064 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2065 if (delegation != NULL &&
2066 nfs4_stateid_match(&delegation->stateid, stateid)) {
2067 cred = get_rpccred(delegation->cred);
2069 status = nfs41_test_stateid(server, stateid, cred);
2070 trace_nfs4_test_delegation_stateid(state, NULL, status);
2074 if (status != NFS_OK) {
2075 /* Free the stateid unless the server explicitly
2076 * informs us the stateid is unrecognized. */
2077 if (status != -NFS4ERR_BAD_STATEID)
2078 nfs41_free_stateid(server, stateid, cred);
2079 nfs_remove_bad_delegation(state->inode);
2081 write_seqlock(&state->seqlock);
2082 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2083 write_sequnlock(&state->seqlock);
2084 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2092 * nfs41_check_open_stateid - possibly free an open stateid
2094 * @state: NFSv4 state for an inode
2096 * Returns NFS_OK if recovery for this stateid is now finished.
2097 * Otherwise a negative NFS4ERR value is returned.
2099 static int nfs41_check_open_stateid(struct nfs4_state *state)
2101 struct nfs_server *server = NFS_SERVER(state->inode);
2102 nfs4_stateid *stateid = &state->open_stateid;
2103 struct rpc_cred *cred = state->owner->so_cred;
2106 /* If a state reset has been done, test_stateid is unneeded */
2107 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2108 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2109 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2110 return -NFS4ERR_BAD_STATEID;
2112 status = nfs41_test_stateid(server, stateid, cred);
2113 trace_nfs4_test_open_stateid(state, NULL, status);
2114 if (status != NFS_OK) {
2115 /* Free the stateid unless the server explicitly
2116 * informs us the stateid is unrecognized. */
2117 if (status != -NFS4ERR_BAD_STATEID)
2118 nfs41_free_stateid(server, stateid, cred);
2120 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2121 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2122 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2123 clear_bit(NFS_OPEN_STATE, &state->flags);
2128 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2132 nfs41_clear_delegation_stateid(state);
2133 status = nfs41_check_open_stateid(state);
2134 if (status != NFS_OK)
2135 status = nfs4_open_expired(sp, state);
2141 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2142 * fields corresponding to attributes that were used to store the verifier.
2143 * Make sure we clobber those fields in the later setattr call
2145 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2147 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2148 !(sattr->ia_valid & ATTR_ATIME_SET))
2149 sattr->ia_valid |= ATTR_ATIME;
2151 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2152 !(sattr->ia_valid & ATTR_MTIME_SET))
2153 sattr->ia_valid |= ATTR_MTIME;
2156 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2159 struct nfs_open_context *ctx)
2161 struct nfs4_state_owner *sp = opendata->owner;
2162 struct nfs_server *server = sp->so_server;
2163 struct dentry *dentry;
2164 struct nfs4_state *state;
2168 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2170 ret = _nfs4_proc_open(opendata);
2174 state = nfs4_opendata_to_nfs4_state(opendata);
2175 ret = PTR_ERR(state);
2178 if (server->caps & NFS_CAP_POSIX_LOCK)
2179 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2181 dentry = opendata->dentry;
2182 if (dentry->d_inode == NULL) {
2183 /* FIXME: Is this d_drop() ever needed? */
2185 dentry = d_add_unique(dentry, igrab(state->inode));
2186 if (dentry == NULL) {
2187 dentry = opendata->dentry;
2188 } else if (dentry != ctx->dentry) {
2190 ctx->dentry = dget(dentry);
2192 nfs_set_verifier(dentry,
2193 nfs_save_change_attribute(opendata->dir->d_inode));
2196 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2201 if (dentry->d_inode == state->inode) {
2202 nfs_inode_attach_open_context(ctx);
2203 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2204 nfs4_schedule_stateid_recovery(server, state);
2211 * Returns a referenced nfs4_state
2213 static int _nfs4_do_open(struct inode *dir,
2214 struct nfs_open_context *ctx,
2216 struct iattr *sattr,
2217 struct nfs4_label *label,
2220 struct nfs4_state_owner *sp;
2221 struct nfs4_state *state = NULL;
2222 struct nfs_server *server = NFS_SERVER(dir);
2223 struct nfs4_opendata *opendata;
2224 struct dentry *dentry = ctx->dentry;
2225 struct rpc_cred *cred = ctx->cred;
2226 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2227 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2228 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2229 struct nfs4_label *olabel = NULL;
2232 /* Protect against reboot recovery conflicts */
2234 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2236 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2239 status = nfs4_recover_expired_lease(server);
2241 goto err_put_state_owner;
2242 if (dentry->d_inode != NULL)
2243 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2245 if (dentry->d_inode)
2246 claim = NFS4_OPEN_CLAIM_FH;
2247 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2248 label, claim, GFP_KERNEL);
2249 if (opendata == NULL)
2250 goto err_put_state_owner;
2253 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2254 if (IS_ERR(olabel)) {
2255 status = PTR_ERR(olabel);
2256 goto err_opendata_put;
2260 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2261 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2262 if (!opendata->f_attr.mdsthreshold)
2263 goto err_free_label;
2264 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2266 if (dentry->d_inode != NULL)
2267 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2269 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2271 goto err_free_label;
2274 if ((opendata->o_arg.open_flags & O_EXCL) &&
2275 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2276 nfs4_exclusive_attrset(opendata, sattr);
2278 nfs_fattr_init(opendata->o_res.f_attr);
2279 status = nfs4_do_setattr(state->inode, cred,
2280 opendata->o_res.f_attr, sattr,
2281 state, label, olabel);
2283 nfs_setattr_update_inode(state->inode, sattr);
2284 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2285 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2288 if (opendata->file_created)
2289 *opened |= FILE_CREATED;
2291 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2292 *ctx_th = opendata->f_attr.mdsthreshold;
2294 kfree(opendata->f_attr.mdsthreshold);
2295 opendata->f_attr.mdsthreshold = NULL;
2297 nfs4_label_free(olabel);
2299 nfs4_opendata_put(opendata);
2300 nfs4_put_state_owner(sp);
2303 nfs4_label_free(olabel);
2305 kfree(opendata->f_attr.mdsthreshold);
2306 nfs4_opendata_put(opendata);
2307 err_put_state_owner:
2308 nfs4_put_state_owner(sp);
2314 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2315 struct nfs_open_context *ctx,
2317 struct iattr *sattr,
2318 struct nfs4_label *label,
2321 struct nfs_server *server = NFS_SERVER(dir);
2322 struct nfs4_exception exception = { };
2323 struct nfs4_state *res;
2327 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2329 trace_nfs4_open_file(ctx, flags, status);
2332 /* NOTE: BAD_SEQID means the server and client disagree about the
2333 * book-keeping w.r.t. state-changing operations
2334 * (OPEN/CLOSE/LOCK/LOCKU...)
2335 * It is actually a sign of a bug on the client or on the server.
2337 * If we receive a BAD_SEQID error in the particular case of
2338 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2339 * have unhashed the old state_owner for us, and that we can
2340 * therefore safely retry using a new one. We should still warn
2341 * the user though...
2343 if (status == -NFS4ERR_BAD_SEQID) {
2344 pr_warn_ratelimited("NFS: v4 server %s "
2345 " returned a bad sequence-id error!\n",
2346 NFS_SERVER(dir)->nfs_client->cl_hostname);
2347 exception.retry = 1;
2351 * BAD_STATEID on OPEN means that the server cancelled our
2352 * state before it received the OPEN_CONFIRM.
2353 * Recover by retrying the request as per the discussion
2354 * on Page 181 of RFC3530.
2356 if (status == -NFS4ERR_BAD_STATEID) {
2357 exception.retry = 1;
2360 if (status == -EAGAIN) {
2361 /* We must have found a delegation */
2362 exception.retry = 1;
2365 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2367 res = ERR_PTR(nfs4_handle_exception(server,
2368 status, &exception));
2369 } while (exception.retry);
2373 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2374 struct nfs_fattr *fattr, struct iattr *sattr,
2375 struct nfs4_state *state, struct nfs4_label *ilabel,
2376 struct nfs4_label *olabel)
2378 struct nfs_server *server = NFS_SERVER(inode);
2379 struct nfs_setattrargs arg = {
2380 .fh = NFS_FH(inode),
2383 .bitmask = server->attr_bitmask,
2386 struct nfs_setattrres res = {
2391 struct rpc_message msg = {
2392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2397 unsigned long timestamp = jiffies;
2402 arg.bitmask = nfs4_bitmask(server, ilabel);
2404 arg.bitmask = nfs4_bitmask(server, olabel);
2406 nfs_fattr_init(fattr);
2408 /* Servers should only apply open mode checks for file size changes */
2409 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2410 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2412 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2413 /* Use that stateid */
2414 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
2415 struct nfs_lockowner lockowner = {
2416 .l_owner = current->files,
2417 .l_pid = current->tgid,
2419 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2422 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2424 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2425 if (status == 0 && state != NULL)
2426 renew_lease(server, timestamp);
2430 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2431 struct nfs_fattr *fattr, struct iattr *sattr,
2432 struct nfs4_state *state, struct nfs4_label *ilabel,
2433 struct nfs4_label *olabel)
2435 struct nfs_server *server = NFS_SERVER(inode);
2436 struct nfs4_exception exception = {
2442 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2443 trace_nfs4_setattr(inode, err);
2445 case -NFS4ERR_OPENMODE:
2446 if (!(sattr->ia_valid & ATTR_SIZE)) {
2447 pr_warn_once("NFSv4: server %s is incorrectly "
2448 "applying open mode checks to "
2449 "a SETATTR that is not "
2450 "changing file size.\n",
2451 server->nfs_client->cl_hostname);
2453 if (state && !(state->state & FMODE_WRITE)) {
2455 if (sattr->ia_valid & ATTR_OPEN)
2460 err = nfs4_handle_exception(server, err, &exception);
2461 } while (exception.retry);
2466 struct nfs4_closedata {
2467 struct inode *inode;
2468 struct nfs4_state *state;
2469 struct nfs_closeargs arg;
2470 struct nfs_closeres res;
2471 struct nfs_fattr fattr;
2472 unsigned long timestamp;
2477 static void nfs4_free_closedata(void *data)
2479 struct nfs4_closedata *calldata = data;
2480 struct nfs4_state_owner *sp = calldata->state->owner;
2481 struct super_block *sb = calldata->state->inode->i_sb;
2484 pnfs_roc_release(calldata->state->inode);
2485 nfs4_put_open_state(calldata->state);
2486 nfs_free_seqid(calldata->arg.seqid);
2487 nfs4_put_state_owner(sp);
2488 nfs_sb_deactive(sb);
2492 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2495 spin_lock(&state->owner->so_lock);
2496 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2497 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2499 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2502 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2505 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2506 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2507 clear_bit(NFS_OPEN_STATE, &state->flags);
2509 spin_unlock(&state->owner->so_lock);
2512 static void nfs4_close_done(struct rpc_task *task, void *data)
2514 struct nfs4_closedata *calldata = data;
2515 struct nfs4_state *state = calldata->state;
2516 struct nfs_server *server = NFS_SERVER(calldata->inode);
2518 dprintk("%s: begin!\n", __func__);
2519 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2521 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2522 /* hmm. we are done with the inode, and in the process of freeing
2523 * the state_owner. we keep this around to process errors
2525 switch (task->tk_status) {
2528 pnfs_roc_set_barrier(state->inode,
2529 calldata->roc_barrier);
2530 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2531 renew_lease(server, calldata->timestamp);
2533 case -NFS4ERR_ADMIN_REVOKED:
2534 case -NFS4ERR_STALE_STATEID:
2535 case -NFS4ERR_OLD_STATEID:
2536 case -NFS4ERR_BAD_STATEID:
2537 case -NFS4ERR_EXPIRED:
2538 if (calldata->arg.fmode == 0)
2541 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
2542 rpc_restart_call_prepare(task);
2546 nfs4_close_clear_stateid_flags(state, calldata->arg.fmode);
2548 nfs_release_seqid(calldata->arg.seqid);
2549 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2550 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2553 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2555 struct nfs4_closedata *calldata = data;
2556 struct nfs4_state *state = calldata->state;
2557 struct inode *inode = calldata->inode;
2560 dprintk("%s: begin!\n", __func__);
2561 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2564 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2565 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2566 spin_lock(&state->owner->so_lock);
2567 /* Calculate the change in open mode */
2568 if (state->n_rdwr == 0) {
2569 if (state->n_rdonly == 0) {
2570 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2571 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2572 calldata->arg.fmode &= ~FMODE_READ;
2574 if (state->n_wronly == 0) {
2575 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2576 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2577 calldata->arg.fmode &= ~FMODE_WRITE;
2580 if (!nfs4_valid_open_stateid(state))
2582 spin_unlock(&state->owner->so_lock);
2585 /* Note: exit _without_ calling nfs4_close_done */
2589 if (calldata->arg.fmode == 0) {
2590 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2591 if (calldata->roc &&
2592 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2593 nfs_release_seqid(calldata->arg.seqid);
2598 nfs_fattr_init(calldata->res.fattr);
2599 calldata->timestamp = jiffies;
2600 if (nfs4_setup_sequence(NFS_SERVER(inode),
2601 &calldata->arg.seq_args,
2602 &calldata->res.seq_res,
2604 nfs_release_seqid(calldata->arg.seqid);
2605 dprintk("%s: done!\n", __func__);
2608 task->tk_action = NULL;
2610 nfs4_sequence_done(task, &calldata->res.seq_res);
2613 static const struct rpc_call_ops nfs4_close_ops = {
2614 .rpc_call_prepare = nfs4_close_prepare,
2615 .rpc_call_done = nfs4_close_done,
2616 .rpc_release = nfs4_free_closedata,
2620 * It is possible for data to be read/written from a mem-mapped file
2621 * after the sys_close call (which hits the vfs layer as a flush).
2622 * This means that we can't safely call nfsv4 close on a file until
2623 * the inode is cleared. This in turn means that we are not good
2624 * NFSv4 citizens - we do not indicate to the server to update the file's
2625 * share state even when we are done with one of the three share
2626 * stateid's in the inode.
2628 * NOTE: Caller must be holding the sp->so_owner semaphore!
2630 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2632 struct nfs_server *server = NFS_SERVER(state->inode);
2633 struct nfs4_closedata *calldata;
2634 struct nfs4_state_owner *sp = state->owner;
2635 struct rpc_task *task;
2636 struct rpc_message msg = {
2637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2638 .rpc_cred = state->owner->so_cred,
2640 struct rpc_task_setup task_setup_data = {
2641 .rpc_client = server->client,
2642 .rpc_message = &msg,
2643 .callback_ops = &nfs4_close_ops,
2644 .workqueue = nfsiod_workqueue,
2645 .flags = RPC_TASK_ASYNC,
2647 int status = -ENOMEM;
2649 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2650 &task_setup_data.rpc_client, &msg);
2652 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2653 if (calldata == NULL)
2655 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2656 calldata->inode = state->inode;
2657 calldata->state = state;
2658 calldata->arg.fh = NFS_FH(state->inode);
2659 calldata->arg.stateid = &state->open_stateid;
2660 /* Serialization for the sequence id */
2661 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2662 if (calldata->arg.seqid == NULL)
2663 goto out_free_calldata;
2664 calldata->arg.fmode = 0;
2665 calldata->arg.bitmask = server->cache_consistency_bitmask;
2666 calldata->res.fattr = &calldata->fattr;
2667 calldata->res.seqid = calldata->arg.seqid;
2668 calldata->res.server = server;
2669 calldata->roc = pnfs_roc(state->inode);
2670 nfs_sb_active(calldata->inode->i_sb);
2672 msg.rpc_argp = &calldata->arg;
2673 msg.rpc_resp = &calldata->res;
2674 task_setup_data.callback_data = calldata;
2675 task = rpc_run_task(&task_setup_data);
2677 return PTR_ERR(task);
2680 status = rpc_wait_for_completion_task(task);
2686 nfs4_put_open_state(state);
2687 nfs4_put_state_owner(sp);
2691 static struct inode *
2692 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2693 int open_flags, struct iattr *attr, int *opened)
2695 struct nfs4_state *state;
2696 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2698 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2700 /* Protect against concurrent sillydeletes */
2701 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2703 nfs4_label_release_security(label);
2706 return ERR_CAST(state);
2707 return state->inode;
2710 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2712 if (ctx->state == NULL)
2715 nfs4_close_sync(ctx->state, ctx->mode);
2717 nfs4_close_state(ctx->state, ctx->mode);
2720 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2721 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2722 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_CHANGE_SECURITY_LABEL - 1UL)
2724 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2726 struct nfs4_server_caps_arg args = {
2729 struct nfs4_server_caps_res res = {};
2730 struct rpc_message msg = {
2731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2737 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2739 /* Sanity check the server answers */
2740 switch (server->nfs_client->cl_minorversion) {
2742 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2743 res.attr_bitmask[2] = 0;
2746 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2749 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2751 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2752 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2753 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2754 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2755 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2756 NFS_CAP_CTIME|NFS_CAP_MTIME|
2757 NFS_CAP_SECURITY_LABEL);
2758 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2759 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2760 server->caps |= NFS_CAP_ACLS;
2761 if (res.has_links != 0)
2762 server->caps |= NFS_CAP_HARDLINKS;
2763 if (res.has_symlinks != 0)
2764 server->caps |= NFS_CAP_SYMLINKS;
2765 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2766 server->caps |= NFS_CAP_FILEID;
2767 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2768 server->caps |= NFS_CAP_MODE;
2769 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2770 server->caps |= NFS_CAP_NLINK;
2771 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2772 server->caps |= NFS_CAP_OWNER;
2773 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2774 server->caps |= NFS_CAP_OWNER_GROUP;
2775 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2776 server->caps |= NFS_CAP_ATIME;
2777 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2778 server->caps |= NFS_CAP_CTIME;
2779 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2780 server->caps |= NFS_CAP_MTIME;
2781 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2782 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2783 server->caps |= NFS_CAP_SECURITY_LABEL;
2785 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2786 sizeof(server->attr_bitmask));
2787 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2789 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2790 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2791 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2792 server->cache_consistency_bitmask[2] = 0;
2793 server->acl_bitmask = res.acl_bitmask;
2794 server->fh_expire_type = res.fh_expire_type;
2800 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2802 struct nfs4_exception exception = { };
2805 err = nfs4_handle_exception(server,
2806 _nfs4_server_capabilities(server, fhandle),
2808 } while (exception.retry);
2812 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2813 struct nfs_fsinfo *info)
2816 struct nfs4_lookup_root_arg args = {
2819 struct nfs4_lookup_res res = {
2821 .fattr = info->fattr,
2824 struct rpc_message msg = {
2825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2830 bitmask[0] = nfs4_fattr_bitmap[0];
2831 bitmask[1] = nfs4_fattr_bitmap[1];
2833 * Process the label in the upcoming getfattr
2835 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2837 nfs_fattr_init(info->fattr);
2838 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2841 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2842 struct nfs_fsinfo *info)
2844 struct nfs4_exception exception = { };
2847 err = _nfs4_lookup_root(server, fhandle, info);
2848 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2851 case -NFS4ERR_WRONGSEC:
2854 err = nfs4_handle_exception(server, err, &exception);
2856 } while (exception.retry);
2861 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2862 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2864 struct rpc_auth_create_args auth_args = {
2865 .pseudoflavor = flavor,
2867 struct rpc_auth *auth;
2870 auth = rpcauth_create(&auth_args, server->client);
2875 ret = nfs4_lookup_root(server, fhandle, info);
2881 * Retry pseudoroot lookup with various security flavors. We do this when:
2883 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2884 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2886 * Returns zero on success, or a negative NFS4ERR value, or a
2887 * negative errno value.
2889 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2890 struct nfs_fsinfo *info)
2892 /* Per 3530bis 15.33.5 */
2893 static const rpc_authflavor_t flav_array[] = {
2897 RPC_AUTH_UNIX, /* courtesy */
2900 int status = -EPERM;
2903 if (server->auth_info.flavor_len > 0) {
2904 /* try each flavor specified by user */
2905 for (i = 0; i < server->auth_info.flavor_len; i++) {
2906 status = nfs4_lookup_root_sec(server, fhandle, info,
2907 server->auth_info.flavors[i]);
2908 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2913 /* no flavors specified by user, try default list */
2914 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2915 status = nfs4_lookup_root_sec(server, fhandle, info,
2917 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2924 * -EACCESS could mean that the user doesn't have correct permissions
2925 * to access the mount. It could also mean that we tried to mount
2926 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2927 * existing mount programs don't handle -EACCES very well so it should
2928 * be mapped to -EPERM instead.
2930 if (status == -EACCES)
2935 static int nfs4_do_find_root_sec(struct nfs_server *server,
2936 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2938 int mv = server->nfs_client->cl_minorversion;
2939 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2943 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2944 * @server: initialized nfs_server handle
2945 * @fhandle: we fill in the pseudo-fs root file handle
2946 * @info: we fill in an FSINFO struct
2947 * @auth_probe: probe the auth flavours
2949 * Returns zero on success, or a negative errno.
2951 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2952 struct nfs_fsinfo *info,
2957 switch (auth_probe) {
2959 status = nfs4_lookup_root(server, fhandle, info);
2960 if (status != -NFS4ERR_WRONGSEC)
2963 status = nfs4_do_find_root_sec(server, fhandle, info);
2967 status = nfs4_server_capabilities(server, fhandle);
2969 status = nfs4_do_fsinfo(server, fhandle, info);
2971 return nfs4_map_errors(status);
2974 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2975 struct nfs_fsinfo *info)
2978 struct nfs_fattr *fattr = info->fattr;
2979 struct nfs4_label *label = NULL;
2981 error = nfs4_server_capabilities(server, mntfh);
2983 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2987 label = nfs4_label_alloc(server, GFP_KERNEL);
2989 return PTR_ERR(label);
2991 error = nfs4_proc_getattr(server, mntfh, fattr, label);
2993 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2994 goto err_free_label;
2997 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2998 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2999 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3002 nfs4_label_free(label);
3008 * Get locations and (maybe) other attributes of a referral.
3009 * Note that we'll actually follow the referral later when
3010 * we detect fsid mismatch in inode revalidation
3012 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3013 const struct qstr *name, struct nfs_fattr *fattr,
3014 struct nfs_fh *fhandle)
3016 int status = -ENOMEM;
3017 struct page *page = NULL;
3018 struct nfs4_fs_locations *locations = NULL;
3020 page = alloc_page(GFP_KERNEL);
3023 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3024 if (locations == NULL)
3027 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3032 * If the fsid didn't change, this is a migration event, not a
3033 * referral. Cause us to drop into the exception handler, which
3034 * will kick off migration recovery.
3036 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3037 dprintk("%s: server did not return a different fsid for"
3038 " a referral at %s\n", __func__, name->name);
3039 status = -NFS4ERR_MOVED;
3042 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3043 nfs_fixup_referral_attributes(&locations->fattr);
3045 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3046 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3047 memset(fhandle, 0, sizeof(struct nfs_fh));
3055 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3056 struct nfs_fattr *fattr, struct nfs4_label *label)
3058 struct nfs4_getattr_arg args = {
3060 .bitmask = server->attr_bitmask,
3062 struct nfs4_getattr_res res = {
3067 struct rpc_message msg = {
3068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3073 args.bitmask = nfs4_bitmask(server, label);
3075 nfs_fattr_init(fattr);
3076 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3079 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3080 struct nfs_fattr *fattr, struct nfs4_label *label)
3082 struct nfs4_exception exception = { };
3085 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3086 trace_nfs4_getattr(server, fhandle, fattr, err);
3087 err = nfs4_handle_exception(server, err,
3089 } while (exception.retry);
3094 * The file is not closed if it is opened due to the a request to change
3095 * the size of the file. The open call will not be needed once the
3096 * VFS layer lookup-intents are implemented.
3098 * Close is called when the inode is destroyed.
3099 * If we haven't opened the file for O_WRONLY, we
3100 * need to in the size_change case to obtain a stateid.
3103 * Because OPEN is always done by name in nfsv4, it is
3104 * possible that we opened a different file by the same
3105 * name. We can recognize this race condition, but we
3106 * can't do anything about it besides returning an error.
3108 * This will be fixed with VFS changes (lookup-intent).
3111 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3112 struct iattr *sattr)
3114 struct inode *inode = dentry->d_inode;
3115 struct rpc_cred *cred = NULL;
3116 struct nfs4_state *state = NULL;
3117 struct nfs4_label *label = NULL;
3120 if (pnfs_ld_layoutret_on_setattr(inode))
3121 pnfs_commit_and_return_layout(inode);
3123 nfs_fattr_init(fattr);
3125 /* Deal with open(O_TRUNC) */
3126 if (sattr->ia_valid & ATTR_OPEN)
3127 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3129 /* Optimization: if the end result is no change, don't RPC */
3130 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3133 /* Search for an existing open(O_WRITE) file */
3134 if (sattr->ia_valid & ATTR_FILE) {
3135 struct nfs_open_context *ctx;
3137 ctx = nfs_file_open_context(sattr->ia_file);
3144 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3146 return PTR_ERR(label);
3148 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3150 nfs_setattr_update_inode(inode, sattr);
3151 nfs_setsecurity(inode, fattr, label);
3153 nfs4_label_free(label);
3157 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3158 const struct qstr *name, struct nfs_fh *fhandle,
3159 struct nfs_fattr *fattr, struct nfs4_label *label)
3161 struct nfs_server *server = NFS_SERVER(dir);
3163 struct nfs4_lookup_arg args = {
3164 .bitmask = server->attr_bitmask,
3165 .dir_fh = NFS_FH(dir),
3168 struct nfs4_lookup_res res = {
3174 struct rpc_message msg = {
3175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3180 args.bitmask = nfs4_bitmask(server, label);
3182 nfs_fattr_init(fattr);
3184 dprintk("NFS call lookup %s\n", name->name);
3185 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3186 dprintk("NFS reply lookup: %d\n", status);
3190 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3192 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3193 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3194 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3198 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3199 struct qstr *name, struct nfs_fh *fhandle,
3200 struct nfs_fattr *fattr, struct nfs4_label *label)
3202 struct nfs4_exception exception = { };
3203 struct rpc_clnt *client = *clnt;
3206 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3207 trace_nfs4_lookup(dir, name, err);
3209 case -NFS4ERR_BADNAME:
3212 case -NFS4ERR_MOVED:
3213 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3215 case -NFS4ERR_WRONGSEC:
3217 if (client != *clnt)
3219 client = nfs4_create_sec_client(client, dir, name);
3221 return PTR_ERR(client);
3223 exception.retry = 1;
3226 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3228 } while (exception.retry);
3233 else if (client != *clnt)
3234 rpc_shutdown_client(client);
3239 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3240 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3241 struct nfs4_label *label)
3244 struct rpc_clnt *client = NFS_CLIENT(dir);
3246 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3247 if (client != NFS_CLIENT(dir)) {
3248 rpc_shutdown_client(client);
3249 nfs_fixup_secinfo_attributes(fattr);
3255 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3256 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3258 struct rpc_clnt *client = NFS_CLIENT(dir);
3261 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3263 return ERR_PTR(status);
3264 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3267 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3269 struct nfs_server *server = NFS_SERVER(inode);
3270 struct nfs4_accessargs args = {
3271 .fh = NFS_FH(inode),
3272 .bitmask = server->cache_consistency_bitmask,
3274 struct nfs4_accessres res = {
3277 struct rpc_message msg = {
3278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3281 .rpc_cred = entry->cred,
3283 int mode = entry->mask;
3287 * Determine which access bits we want to ask for...
3289 if (mode & MAY_READ)
3290 args.access |= NFS4_ACCESS_READ;
3291 if (S_ISDIR(inode->i_mode)) {
3292 if (mode & MAY_WRITE)
3293 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3294 if (mode & MAY_EXEC)
3295 args.access |= NFS4_ACCESS_LOOKUP;
3297 if (mode & MAY_WRITE)
3298 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3299 if (mode & MAY_EXEC)
3300 args.access |= NFS4_ACCESS_EXECUTE;
3303 res.fattr = nfs_alloc_fattr();
3304 if (res.fattr == NULL)
3307 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3309 nfs_access_set_mask(entry, res.access);
3310 nfs_refresh_inode(inode, res.fattr);
3312 nfs_free_fattr(res.fattr);
3316 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3318 struct nfs4_exception exception = { };
3321 err = _nfs4_proc_access(inode, entry);
3322 trace_nfs4_access(inode, err);
3323 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3325 } while (exception.retry);
3330 * TODO: For the time being, we don't try to get any attributes
3331 * along with any of the zero-copy operations READ, READDIR,
3334 * In the case of the first three, we want to put the GETATTR
3335 * after the read-type operation -- this is because it is hard
3336 * to predict the length of a GETATTR response in v4, and thus
3337 * align the READ data correctly. This means that the GETATTR
3338 * may end up partially falling into the page cache, and we should
3339 * shift it into the 'tail' of the xdr_buf before processing.
3340 * To do this efficiently, we need to know the total length
3341 * of data received, which doesn't seem to be available outside
3344 * In the case of WRITE, we also want to put the GETATTR after
3345 * the operation -- in this case because we want to make sure
3346 * we get the post-operation mtime and size.
3348 * Both of these changes to the XDR layer would in fact be quite
3349 * minor, but I decided to leave them for a subsequent patch.
3351 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3352 unsigned int pgbase, unsigned int pglen)
3354 struct nfs4_readlink args = {
3355 .fh = NFS_FH(inode),
3360 struct nfs4_readlink_res res;
3361 struct rpc_message msg = {
3362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3367 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3370 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3371 unsigned int pgbase, unsigned int pglen)
3373 struct nfs4_exception exception = { };
3376 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3377 trace_nfs4_readlink(inode, err);
3378 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3380 } while (exception.retry);
3385 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3388 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3391 struct nfs4_label l, *ilabel = NULL;
3392 struct nfs_open_context *ctx;
3393 struct nfs4_state *state;
3397 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3399 return PTR_ERR(ctx);
3401 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3403 sattr->ia_mode &= ~current_umask();
3404 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3405 if (IS_ERR(state)) {
3406 status = PTR_ERR(state);
3410 nfs4_label_release_security(ilabel);
3411 put_nfs_open_context(ctx);
3415 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3417 struct nfs_server *server = NFS_SERVER(dir);
3418 struct nfs_removeargs args = {
3422 struct nfs_removeres res = {
3425 struct rpc_message msg = {
3426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3432 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3434 update_changeattr(dir, &res.cinfo);
3438 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3440 struct nfs4_exception exception = { };
3443 err = _nfs4_proc_remove(dir, name);
3444 trace_nfs4_remove(dir, name, err);
3445 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3447 } while (exception.retry);
3451 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3453 struct nfs_server *server = NFS_SERVER(dir);
3454 struct nfs_removeargs *args = msg->rpc_argp;
3455 struct nfs_removeres *res = msg->rpc_resp;
3457 res->server = server;
3458 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3459 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3461 nfs_fattr_init(res->dir_attr);
3464 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3466 nfs4_setup_sequence(NFS_SERVER(data->dir),
3467 &data->args.seq_args,
3472 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3474 struct nfs_unlinkdata *data = task->tk_calldata;
3475 struct nfs_removeres *res = &data->res;
3477 if (!nfs4_sequence_done(task, &res->seq_res))
3479 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3481 update_changeattr(dir, &res->cinfo);
3485 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3487 struct nfs_server *server = NFS_SERVER(dir);
3488 struct nfs_renameargs *arg = msg->rpc_argp;
3489 struct nfs_renameres *res = msg->rpc_resp;
3491 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3492 res->server = server;
3493 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3496 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3498 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3499 &data->args.seq_args,
3504 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3505 struct inode *new_dir)
3507 struct nfs_renamedata *data = task->tk_calldata;
3508 struct nfs_renameres *res = &data->res;
3510 if (!nfs4_sequence_done(task, &res->seq_res))
3512 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3515 update_changeattr(old_dir, &res->old_cinfo);
3516 update_changeattr(new_dir, &res->new_cinfo);
3520 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3521 struct inode *new_dir, struct qstr *new_name)
3523 struct nfs_server *server = NFS_SERVER(old_dir);
3524 struct nfs_renameargs arg = {
3525 .old_dir = NFS_FH(old_dir),
3526 .new_dir = NFS_FH(new_dir),
3527 .old_name = old_name,
3528 .new_name = new_name,
3530 struct nfs_renameres res = {
3533 struct rpc_message msg = {
3534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3538 int status = -ENOMEM;
3540 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3542 update_changeattr(old_dir, &res.old_cinfo);
3543 update_changeattr(new_dir, &res.new_cinfo);
3548 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3549 struct inode *new_dir, struct qstr *new_name)
3551 struct nfs4_exception exception = { };
3554 err = _nfs4_proc_rename(old_dir, old_name,
3556 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3557 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
3559 } while (exception.retry);
3563 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3565 struct nfs_server *server = NFS_SERVER(inode);
3566 struct nfs4_link_arg arg = {
3567 .fh = NFS_FH(inode),
3568 .dir_fh = NFS_FH(dir),
3570 .bitmask = server->attr_bitmask,
3572 struct nfs4_link_res res = {
3576 struct rpc_message msg = {
3577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3581 int status = -ENOMEM;
3583 res.fattr = nfs_alloc_fattr();
3584 if (res.fattr == NULL)
3587 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3588 if (IS_ERR(res.label)) {
3589 status = PTR_ERR(res.label);
3592 arg.bitmask = nfs4_bitmask(server, res.label);
3594 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3596 update_changeattr(dir, &res.cinfo);
3597 status = nfs_post_op_update_inode(inode, res.fattr);
3599 nfs_setsecurity(inode, res.fattr, res.label);
3603 nfs4_label_free(res.label);
3606 nfs_free_fattr(res.fattr);
3610 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3612 struct nfs4_exception exception = { };
3615 err = nfs4_handle_exception(NFS_SERVER(inode),
3616 _nfs4_proc_link(inode, dir, name),
3618 } while (exception.retry);
3622 struct nfs4_createdata {
3623 struct rpc_message msg;
3624 struct nfs4_create_arg arg;
3625 struct nfs4_create_res res;
3627 struct nfs_fattr fattr;
3628 struct nfs4_label *label;
3631 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3632 struct qstr *name, struct iattr *sattr, u32 ftype)
3634 struct nfs4_createdata *data;
3636 data = kzalloc(sizeof(*data), GFP_KERNEL);
3638 struct nfs_server *server = NFS_SERVER(dir);
3640 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3641 if (IS_ERR(data->label))
3644 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3645 data->msg.rpc_argp = &data->arg;
3646 data->msg.rpc_resp = &data->res;
3647 data->arg.dir_fh = NFS_FH(dir);
3648 data->arg.server = server;
3649 data->arg.name = name;
3650 data->arg.attrs = sattr;
3651 data->arg.ftype = ftype;
3652 data->arg.bitmask = nfs4_bitmask(server, data->label);
3653 data->res.server = server;
3654 data->res.fh = &data->fh;
3655 data->res.fattr = &data->fattr;
3656 data->res.label = data->label;
3657 nfs_fattr_init(data->res.fattr);
3665 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3667 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3668 &data->arg.seq_args, &data->res.seq_res, 1);
3670 update_changeattr(dir, &data->res.dir_cinfo);
3671 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3676 static void nfs4_free_createdata(struct nfs4_createdata *data)
3678 nfs4_label_free(data->label);
3682 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3683 struct page *page, unsigned int len, struct iattr *sattr,
3684 struct nfs4_label *label)
3686 struct nfs4_createdata *data;
3687 int status = -ENAMETOOLONG;
3689 if (len > NFS4_MAXPATHLEN)
3693 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3697 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3698 data->arg.u.symlink.pages = &page;
3699 data->arg.u.symlink.len = len;
3700 data->arg.label = label;
3702 status = nfs4_do_create(dir, dentry, data);
3704 nfs4_free_createdata(data);
3709 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3710 struct page *page, unsigned int len, struct iattr *sattr)
3712 struct nfs4_exception exception = { };
3713 struct nfs4_label l, *label = NULL;
3716 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3719 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3720 trace_nfs4_symlink(dir, &dentry->d_name, err);
3721 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3723 } while (exception.retry);
3725 nfs4_label_release_security(label);
3729 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3730 struct iattr *sattr, struct nfs4_label *label)
3732 struct nfs4_createdata *data;
3733 int status = -ENOMEM;
3735 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3739 data->arg.label = label;
3740 status = nfs4_do_create(dir, dentry, data);
3742 nfs4_free_createdata(data);
3747 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3748 struct iattr *sattr)
3750 struct nfs4_exception exception = { };
3751 struct nfs4_label l, *label = NULL;
3754 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3756 sattr->ia_mode &= ~current_umask();
3758 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3759 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3760 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3762 } while (exception.retry);
3763 nfs4_label_release_security(label);
3768 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3769 u64 cookie, struct page **pages, unsigned int count, int plus)
3771 struct inode *dir = dentry->d_inode;
3772 struct nfs4_readdir_arg args = {
3777 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3780 struct nfs4_readdir_res res;
3781 struct rpc_message msg = {
3782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3789 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3791 (unsigned long long)cookie);
3792 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3793 res.pgbase = args.pgbase;
3794 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3796 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3797 status += args.pgbase;
3800 nfs_invalidate_atime(dir);
3802 dprintk("%s: returns %d\n", __func__, status);
3806 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3807 u64 cookie, struct page **pages, unsigned int count, int plus)
3809 struct nfs4_exception exception = { };
3812 err = _nfs4_proc_readdir(dentry, cred, cookie,
3813 pages, count, plus);
3814 trace_nfs4_readdir(dentry->d_inode, err);
3815 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3817 } while (exception.retry);
3821 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3822 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3824 struct nfs4_createdata *data;
3825 int mode = sattr->ia_mode;
3826 int status = -ENOMEM;
3828 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3833 data->arg.ftype = NF4FIFO;
3834 else if (S_ISBLK(mode)) {
3835 data->arg.ftype = NF4BLK;
3836 data->arg.u.device.specdata1 = MAJOR(rdev);
3837 data->arg.u.device.specdata2 = MINOR(rdev);
3839 else if (S_ISCHR(mode)) {
3840 data->arg.ftype = NF4CHR;
3841 data->arg.u.device.specdata1 = MAJOR(rdev);
3842 data->arg.u.device.specdata2 = MINOR(rdev);
3843 } else if (!S_ISSOCK(mode)) {
3848 data->arg.label = label;
3849 status = nfs4_do_create(dir, dentry, data);
3851 nfs4_free_createdata(data);
3856 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3857 struct iattr *sattr, dev_t rdev)
3859 struct nfs4_exception exception = { };
3860 struct nfs4_label l, *label = NULL;
3863 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3865 sattr->ia_mode &= ~current_umask();
3867 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3868 trace_nfs4_mknod(dir, &dentry->d_name, err);
3869 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3871 } while (exception.retry);
3873 nfs4_label_release_security(label);
3878 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3879 struct nfs_fsstat *fsstat)
3881 struct nfs4_statfs_arg args = {
3883 .bitmask = server->attr_bitmask,
3885 struct nfs4_statfs_res res = {
3888 struct rpc_message msg = {
3889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3894 nfs_fattr_init(fsstat->fattr);
3895 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3898 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3900 struct nfs4_exception exception = { };
3903 err = nfs4_handle_exception(server,
3904 _nfs4_proc_statfs(server, fhandle, fsstat),
3906 } while (exception.retry);
3910 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3911 struct nfs_fsinfo *fsinfo)
3913 struct nfs4_fsinfo_arg args = {
3915 .bitmask = server->attr_bitmask,
3917 struct nfs4_fsinfo_res res = {
3920 struct rpc_message msg = {
3921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3926 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3929 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3931 struct nfs4_exception exception = { };
3932 unsigned long now = jiffies;
3936 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3937 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
3939 struct nfs_client *clp = server->nfs_client;
3941 spin_lock(&clp->cl_lock);
3942 clp->cl_lease_time = fsinfo->lease_time * HZ;
3943 clp->cl_last_renewal = now;
3944 spin_unlock(&clp->cl_lock);
3947 err = nfs4_handle_exception(server, err, &exception);
3948 } while (exception.retry);
3952 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3956 nfs_fattr_init(fsinfo->fattr);
3957 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3959 /* block layout checks this! */
3960 server->pnfs_blksize = fsinfo->blksize;
3961 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3967 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3968 struct nfs_pathconf *pathconf)
3970 struct nfs4_pathconf_arg args = {
3972 .bitmask = server->attr_bitmask,
3974 struct nfs4_pathconf_res res = {
3975 .pathconf = pathconf,
3977 struct rpc_message msg = {
3978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3983 /* None of the pathconf attributes are mandatory to implement */
3984 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3985 memset(pathconf, 0, sizeof(*pathconf));
3989 nfs_fattr_init(pathconf->fattr);
3990 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3993 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3994 struct nfs_pathconf *pathconf)
3996 struct nfs4_exception exception = { };
4000 err = nfs4_handle_exception(server,
4001 _nfs4_proc_pathconf(server, fhandle, pathconf),
4003 } while (exception.retry);
4007 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4008 const struct nfs_open_context *ctx,
4009 const struct nfs_lock_context *l_ctx,
4012 const struct nfs_lockowner *lockowner = NULL;
4015 lockowner = &l_ctx->lockowner;
4016 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4018 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4020 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4021 const struct nfs_open_context *ctx,
4022 const struct nfs_lock_context *l_ctx,
4025 nfs4_stateid current_stateid;
4027 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode))
4029 return nfs4_stateid_match(stateid, ¤t_stateid);
4032 static bool nfs4_error_stateid_expired(int err)
4035 case -NFS4ERR_DELEG_REVOKED:
4036 case -NFS4ERR_ADMIN_REVOKED:
4037 case -NFS4ERR_BAD_STATEID:
4038 case -NFS4ERR_STALE_STATEID:
4039 case -NFS4ERR_OLD_STATEID:
4040 case -NFS4ERR_OPENMODE:
4041 case -NFS4ERR_EXPIRED:
4047 void __nfs4_read_done_cb(struct nfs_read_data *data)
4049 nfs_invalidate_atime(data->header->inode);
4052 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
4054 struct nfs_server *server = NFS_SERVER(data->header->inode);
4056 trace_nfs4_read(data, task->tk_status);
4057 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
4058 rpc_restart_call_prepare(task);
4062 __nfs4_read_done_cb(data);
4063 if (task->tk_status > 0)
4064 renew_lease(server, data->timestamp);
4068 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4069 struct nfs_readargs *args)
4072 if (!nfs4_error_stateid_expired(task->tk_status) ||
4073 nfs4_stateid_is_current(&args->stateid,
4078 rpc_restart_call_prepare(task);
4082 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4085 dprintk("--> %s\n", __func__);
4087 if (!nfs4_sequence_done(task, &data->res.seq_res))
4089 if (nfs4_read_stateid_changed(task, &data->args))
4091 return data->read_done_cb ? data->read_done_cb(task, data) :
4092 nfs4_read_done_cb(task, data);
4095 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
4097 data->timestamp = jiffies;
4098 data->read_done_cb = nfs4_read_done_cb;
4099 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4100 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
4103 static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
4105 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4106 &data->args.seq_args,
4110 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4111 data->args.lock_context, FMODE_READ) == -EIO)
4113 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4118 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
4120 struct inode *inode = data->header->inode;
4122 trace_nfs4_write(data, task->tk_status);
4123 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
4124 rpc_restart_call_prepare(task);
4127 if (task->tk_status >= 0) {
4128 renew_lease(NFS_SERVER(inode), data->timestamp);
4129 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4134 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4135 struct nfs_writeargs *args)
4138 if (!nfs4_error_stateid_expired(task->tk_status) ||
4139 nfs4_stateid_is_current(&args->stateid,
4144 rpc_restart_call_prepare(task);
4148 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4150 if (!nfs4_sequence_done(task, &data->res.seq_res))
4152 if (nfs4_write_stateid_changed(task, &data->args))
4154 return data->write_done_cb ? data->write_done_cb(task, data) :
4155 nfs4_write_done_cb(task, data);
4159 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
4161 const struct nfs_pgio_header *hdr = data->header;
4163 /* Don't request attributes for pNFS or O_DIRECT writes */
4164 if (data->ds_clp != NULL || hdr->dreq != NULL)
4166 /* Otherwise, request attributes if and only if we don't hold
4169 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4172 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
4174 struct nfs_server *server = NFS_SERVER(data->header->inode);
4176 if (!nfs4_write_need_cache_consistency_data(data)) {
4177 data->args.bitmask = NULL;
4178 data->res.fattr = NULL;
4180 data->args.bitmask = server->cache_consistency_bitmask;
4182 if (!data->write_done_cb)
4183 data->write_done_cb = nfs4_write_done_cb;
4184 data->res.server = server;
4185 data->timestamp = jiffies;
4187 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4188 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4191 static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4193 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4194 &data->args.seq_args,
4198 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4199 data->args.lock_context, FMODE_WRITE) == -EIO)
4201 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4206 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4208 nfs4_setup_sequence(NFS_SERVER(data->inode),
4209 &data->args.seq_args,
4214 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4216 struct inode *inode = data->inode;
4218 trace_nfs4_commit(data, task->tk_status);
4219 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
4220 rpc_restart_call_prepare(task);
4226 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4228 if (!nfs4_sequence_done(task, &data->res.seq_res))
4230 return data->commit_done_cb(task, data);
4233 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4235 struct nfs_server *server = NFS_SERVER(data->inode);
4237 if (data->commit_done_cb == NULL)
4238 data->commit_done_cb = nfs4_commit_done_cb;
4239 data->res.server = server;
4240 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4241 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4244 struct nfs4_renewdata {
4245 struct nfs_client *client;
4246 unsigned long timestamp;
4250 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4251 * standalone procedure for queueing an asynchronous RENEW.
4253 static void nfs4_renew_release(void *calldata)
4255 struct nfs4_renewdata *data = calldata;
4256 struct nfs_client *clp = data->client;
4258 if (atomic_read(&clp->cl_count) > 1)
4259 nfs4_schedule_state_renewal(clp);
4260 nfs_put_client(clp);
4264 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4266 struct nfs4_renewdata *data = calldata;
4267 struct nfs_client *clp = data->client;
4268 unsigned long timestamp = data->timestamp;
4270 trace_nfs4_renew_async(clp, task->tk_status);
4271 switch (task->tk_status) {
4274 case -NFS4ERR_LEASE_MOVED:
4275 nfs4_schedule_lease_moved_recovery(clp);
4278 /* Unless we're shutting down, schedule state recovery! */
4279 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4281 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4282 nfs4_schedule_lease_recovery(clp);
4285 nfs4_schedule_path_down_recovery(clp);
4287 do_renew_lease(clp, timestamp);
4290 static const struct rpc_call_ops nfs4_renew_ops = {
4291 .rpc_call_done = nfs4_renew_done,
4292 .rpc_release = nfs4_renew_release,
4295 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4297 struct rpc_message msg = {
4298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4302 struct nfs4_renewdata *data;
4304 if (renew_flags == 0)
4306 if (!atomic_inc_not_zero(&clp->cl_count))
4308 data = kmalloc(sizeof(*data), GFP_NOFS);
4312 data->timestamp = jiffies;
4313 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4314 &nfs4_renew_ops, data);
4317 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4319 struct rpc_message msg = {
4320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4324 unsigned long now = jiffies;
4327 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4330 do_renew_lease(clp, now);
4334 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4336 return server->caps & NFS_CAP_ACLS;
4339 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4340 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4343 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4345 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4346 struct page **pages, unsigned int *pgbase)
4348 struct page *newpage, **spages;
4354 len = min_t(size_t, PAGE_SIZE, buflen);
4355 newpage = alloc_page(GFP_KERNEL);
4357 if (newpage == NULL)
4359 memcpy(page_address(newpage), buf, len);
4364 } while (buflen != 0);
4370 __free_page(spages[rc-1]);
4374 struct nfs4_cached_acl {
4380 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4382 struct nfs_inode *nfsi = NFS_I(inode);
4384 spin_lock(&inode->i_lock);
4385 kfree(nfsi->nfs4_acl);
4386 nfsi->nfs4_acl = acl;
4387 spin_unlock(&inode->i_lock);
4390 static void nfs4_zap_acl_attr(struct inode *inode)
4392 nfs4_set_cached_acl(inode, NULL);
4395 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4397 struct nfs_inode *nfsi = NFS_I(inode);
4398 struct nfs4_cached_acl *acl;
4401 spin_lock(&inode->i_lock);
4402 acl = nfsi->nfs4_acl;
4405 if (buf == NULL) /* user is just asking for length */
4407 if (acl->cached == 0)
4409 ret = -ERANGE; /* see getxattr(2) man page */
4410 if (acl->len > buflen)
4412 memcpy(buf, acl->data, acl->len);
4416 spin_unlock(&inode->i_lock);
4420 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4422 struct nfs4_cached_acl *acl;
4423 size_t buflen = sizeof(*acl) + acl_len;
4425 if (buflen <= PAGE_SIZE) {
4426 acl = kmalloc(buflen, GFP_KERNEL);
4430 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4432 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4439 nfs4_set_cached_acl(inode, acl);
4443 * The getxattr API returns the required buffer length when called with a
4444 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4445 * the required buf. On a NULL buf, we send a page of data to the server
4446 * guessing that the ACL request can be serviced by a page. If so, we cache
4447 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4448 * the cache. If not so, we throw away the page, and cache the required
4449 * length. The next getxattr call will then produce another round trip to
4450 * the server, this time with the input buf of the required size.
4452 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4454 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4455 struct nfs_getaclargs args = {
4456 .fh = NFS_FH(inode),
4460 struct nfs_getaclres res = {
4463 struct rpc_message msg = {
4464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4468 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4469 int ret = -ENOMEM, i;
4471 /* As long as we're doing a round trip to the server anyway,
4472 * let's be prepared for a page of acl data. */
4475 if (npages > ARRAY_SIZE(pages))
4478 for (i = 0; i < npages; i++) {
4479 pages[i] = alloc_page(GFP_KERNEL);
4484 /* for decoding across pages */
4485 res.acl_scratch = alloc_page(GFP_KERNEL);
4486 if (!res.acl_scratch)
4489 args.acl_len = npages * PAGE_SIZE;
4490 args.acl_pgbase = 0;
4492 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4493 __func__, buf, buflen, npages, args.acl_len);
4494 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4495 &msg, &args.seq_args, &res.seq_res, 0);
4499 /* Handle the case where the passed-in buffer is too short */
4500 if (res.acl_flags & NFS4_ACL_TRUNC) {
4501 /* Did the user only issue a request for the acl length? */
4507 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4509 if (res.acl_len > buflen) {
4513 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4518 for (i = 0; i < npages; i++)
4520 __free_page(pages[i]);
4521 if (res.acl_scratch)
4522 __free_page(res.acl_scratch);
4526 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4528 struct nfs4_exception exception = { };
4531 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4532 trace_nfs4_get_acl(inode, ret);
4535 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4536 } while (exception.retry);
4540 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4542 struct nfs_server *server = NFS_SERVER(inode);
4545 if (!nfs4_server_supports_acls(server))
4547 ret = nfs_revalidate_inode(server, inode);
4550 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4551 nfs_zap_acl_cache(inode);
4552 ret = nfs4_read_cached_acl(inode, buf, buflen);
4554 /* -ENOENT is returned if there is no ACL or if there is an ACL
4555 * but no cached acl data, just the acl length */
4557 return nfs4_get_acl_uncached(inode, buf, buflen);
4560 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4562 struct nfs_server *server = NFS_SERVER(inode);
4563 struct page *pages[NFS4ACL_MAXPAGES];
4564 struct nfs_setaclargs arg = {
4565 .fh = NFS_FH(inode),
4569 struct nfs_setaclres res;
4570 struct rpc_message msg = {
4571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4575 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4578 if (!nfs4_server_supports_acls(server))
4580 if (npages > ARRAY_SIZE(pages))
4582 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4585 nfs4_inode_return_delegation(inode);
4586 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4589 * Free each page after tx, so the only ref left is
4590 * held by the network stack
4593 put_page(pages[i-1]);
4596 * Acl update can result in inode attribute update.
4597 * so mark the attribute cache invalid.
4599 spin_lock(&inode->i_lock);
4600 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4601 spin_unlock(&inode->i_lock);
4602 nfs_access_zap_cache(inode);
4603 nfs_zap_acl_cache(inode);
4607 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4609 struct nfs4_exception exception = { };
4612 err = __nfs4_proc_set_acl(inode, buf, buflen);
4613 trace_nfs4_set_acl(inode, err);
4614 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4616 } while (exception.retry);
4620 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4621 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4624 struct nfs_server *server = NFS_SERVER(inode);
4625 struct nfs_fattr fattr;
4626 struct nfs4_label label = {0, 0, buflen, buf};
4628 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4629 struct nfs4_getattr_arg arg = {
4630 .fh = NFS_FH(inode),
4633 struct nfs4_getattr_res res = {
4638 struct rpc_message msg = {
4639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4645 nfs_fattr_init(&fattr);
4647 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4650 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4652 if (buflen < label.len)
4657 static int nfs4_get_security_label(struct inode *inode, void *buf,
4660 struct nfs4_exception exception = { };
4663 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4667 err = _nfs4_get_security_label(inode, buf, buflen);
4668 trace_nfs4_get_security_label(inode, err);
4669 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4671 } while (exception.retry);
4675 static int _nfs4_do_set_security_label(struct inode *inode,
4676 struct nfs4_label *ilabel,
4677 struct nfs_fattr *fattr,
4678 struct nfs4_label *olabel)
4681 struct iattr sattr = {0};
4682 struct nfs_server *server = NFS_SERVER(inode);
4683 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4684 struct nfs_setattrargs arg = {
4685 .fh = NFS_FH(inode),
4691 struct nfs_setattrres res = {
4696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4703 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4705 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4707 dprintk("%s failed: %d\n", __func__, status);
4712 static int nfs4_do_set_security_label(struct inode *inode,
4713 struct nfs4_label *ilabel,
4714 struct nfs_fattr *fattr,
4715 struct nfs4_label *olabel)
4717 struct nfs4_exception exception = { };
4721 err = _nfs4_do_set_security_label(inode, ilabel,
4723 trace_nfs4_set_security_label(inode, err);
4724 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4726 } while (exception.retry);
4731 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4733 struct nfs4_label ilabel, *olabel = NULL;
4734 struct nfs_fattr fattr;
4735 struct rpc_cred *cred;
4736 struct inode *inode = dentry->d_inode;
4739 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4742 nfs_fattr_init(&fattr);
4746 ilabel.label = (char *)buf;
4747 ilabel.len = buflen;
4749 cred = rpc_lookup_cred();
4751 return PTR_ERR(cred);
4753 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4754 if (IS_ERR(olabel)) {
4755 status = -PTR_ERR(olabel);
4759 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4761 nfs_setsecurity(inode, &fattr, olabel);
4763 nfs4_label_free(olabel);
4768 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4772 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4774 struct nfs_client *clp = server->nfs_client;
4776 if (task->tk_status >= 0)
4778 switch(task->tk_status) {
4779 case -NFS4ERR_DELEG_REVOKED:
4780 case -NFS4ERR_ADMIN_REVOKED:
4781 case -NFS4ERR_BAD_STATEID:
4784 nfs_remove_bad_delegation(state->inode);
4785 case -NFS4ERR_OPENMODE:
4788 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4789 goto recovery_failed;
4790 goto wait_on_recovery;
4791 case -NFS4ERR_EXPIRED:
4792 if (state != NULL) {
4793 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4794 goto recovery_failed;
4796 case -NFS4ERR_STALE_STATEID:
4797 case -NFS4ERR_STALE_CLIENTID:
4798 nfs4_schedule_lease_recovery(clp);
4799 goto wait_on_recovery;
4800 case -NFS4ERR_MOVED:
4801 if (nfs4_schedule_migration_recovery(server) < 0)
4802 goto recovery_failed;
4803 goto wait_on_recovery;
4804 case -NFS4ERR_LEASE_MOVED:
4805 nfs4_schedule_lease_moved_recovery(clp);
4806 goto wait_on_recovery;
4807 #if defined(CONFIG_NFS_V4_1)
4808 case -NFS4ERR_BADSESSION:
4809 case -NFS4ERR_BADSLOT:
4810 case -NFS4ERR_BAD_HIGH_SLOT:
4811 case -NFS4ERR_DEADSESSION:
4812 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4813 case -NFS4ERR_SEQ_FALSE_RETRY:
4814 case -NFS4ERR_SEQ_MISORDERED:
4815 dprintk("%s ERROR %d, Reset session\n", __func__,
4817 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4818 goto wait_on_recovery;
4819 #endif /* CONFIG_NFS_V4_1 */
4820 case -NFS4ERR_DELAY:
4821 nfs_inc_server_stats(server, NFSIOS_DELAY);
4822 case -NFS4ERR_GRACE:
4823 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4824 case -NFS4ERR_RETRY_UNCACHED_REP:
4825 case -NFS4ERR_OLD_STATEID:
4828 task->tk_status = nfs4_map_errors(task->tk_status);
4831 task->tk_status = -EIO;
4834 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4835 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4836 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4837 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4838 goto recovery_failed;
4840 task->tk_status = 0;
4844 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4845 nfs4_verifier *bootverf)
4849 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4850 /* An impossible timestamp guarantees this value
4851 * will never match a generated boot time. */
4853 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4855 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4856 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4857 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4859 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4863 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4864 char *buf, size_t len)
4866 unsigned int result;
4869 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4871 rpc_peeraddr2str(clp->cl_rpcclient,
4873 rpc_peeraddr2str(clp->cl_rpcclient,
4874 RPC_DISPLAY_PROTO));
4880 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4881 char *buf, size_t len)
4883 const char *nodename = clp->cl_rpcclient->cl_nodename;
4885 if (nfs4_client_id_uniquifier[0] != '\0')
4886 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4887 clp->rpc_ops->version,
4888 clp->cl_minorversion,
4889 nfs4_client_id_uniquifier,
4891 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4892 clp->rpc_ops->version, clp->cl_minorversion,
4897 * nfs4_proc_setclientid - Negotiate client ID
4898 * @clp: state data structure
4899 * @program: RPC program for NFSv4 callback service
4900 * @port: IP port number for NFS4 callback service
4901 * @cred: RPC credential to use for this call
4902 * @res: where to place the result
4904 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4906 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4907 unsigned short port, struct rpc_cred *cred,
4908 struct nfs4_setclientid_res *res)
4910 nfs4_verifier sc_verifier;
4911 struct nfs4_setclientid setclientid = {
4912 .sc_verifier = &sc_verifier,
4914 .sc_cb_ident = clp->cl_cb_ident,
4916 struct rpc_message msg = {
4917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4918 .rpc_argp = &setclientid,
4924 /* nfs_client_id4 */
4925 nfs4_init_boot_verifier(clp, &sc_verifier);
4926 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4927 setclientid.sc_name_len =
4928 nfs4_init_uniform_client_string(clp,
4929 setclientid.sc_name,
4930 sizeof(setclientid.sc_name));
4932 setclientid.sc_name_len =
4933 nfs4_init_nonuniform_client_string(clp,
4934 setclientid.sc_name,
4935 sizeof(setclientid.sc_name));
4938 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4939 sizeof(setclientid.sc_netid), "%s",
4940 rpc_peeraddr2str(clp->cl_rpcclient,
4941 RPC_DISPLAY_NETID));
4943 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4944 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4945 clp->cl_ipaddr, port >> 8, port & 255);
4947 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4948 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4949 setclientid.sc_name_len, setclientid.sc_name);
4950 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4951 trace_nfs4_setclientid(clp, status);
4952 dprintk("NFS reply setclientid: %d\n", status);
4957 * nfs4_proc_setclientid_confirm - Confirm client ID
4958 * @clp: state data structure
4959 * @res: result of a previous SETCLIENTID
4960 * @cred: RPC credential to use for this call
4962 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4964 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4965 struct nfs4_setclientid_res *arg,
4966 struct rpc_cred *cred)
4968 struct rpc_message msg = {
4969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4975 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4976 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4978 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4979 trace_nfs4_setclientid_confirm(clp, status);
4980 dprintk("NFS reply setclientid_confirm: %d\n", status);
4984 struct nfs4_delegreturndata {
4985 struct nfs4_delegreturnargs args;
4986 struct nfs4_delegreturnres res;
4988 nfs4_stateid stateid;
4989 unsigned long timestamp;
4990 struct nfs_fattr fattr;
4994 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4996 struct nfs4_delegreturndata *data = calldata;
4998 if (!nfs4_sequence_done(task, &data->res.seq_res))
5001 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5002 switch (task->tk_status) {
5004 renew_lease(data->res.server, data->timestamp);
5006 case -NFS4ERR_ADMIN_REVOKED:
5007 case -NFS4ERR_DELEG_REVOKED:
5008 case -NFS4ERR_BAD_STATEID:
5009 case -NFS4ERR_OLD_STATEID:
5010 case -NFS4ERR_STALE_STATEID:
5011 case -NFS4ERR_EXPIRED:
5012 task->tk_status = 0;
5015 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5017 rpc_restart_call_prepare(task);
5021 data->rpc_status = task->tk_status;
5024 static void nfs4_delegreturn_release(void *calldata)
5029 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5031 struct nfs4_delegreturndata *d_data;
5033 d_data = (struct nfs4_delegreturndata *)data;
5035 nfs4_setup_sequence(d_data->res.server,
5036 &d_data->args.seq_args,
5037 &d_data->res.seq_res,
5041 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5042 .rpc_call_prepare = nfs4_delegreturn_prepare,
5043 .rpc_call_done = nfs4_delegreturn_done,
5044 .rpc_release = nfs4_delegreturn_release,
5047 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5049 struct nfs4_delegreturndata *data;
5050 struct nfs_server *server = NFS_SERVER(inode);
5051 struct rpc_task *task;
5052 struct rpc_message msg = {
5053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5056 struct rpc_task_setup task_setup_data = {
5057 .rpc_client = server->client,
5058 .rpc_message = &msg,
5059 .callback_ops = &nfs4_delegreturn_ops,
5060 .flags = RPC_TASK_ASYNC,
5064 data = kzalloc(sizeof(*data), GFP_NOFS);
5067 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5068 data->args.fhandle = &data->fh;
5069 data->args.stateid = &data->stateid;
5070 data->args.bitmask = server->cache_consistency_bitmask;
5071 nfs_copy_fh(&data->fh, NFS_FH(inode));
5072 nfs4_stateid_copy(&data->stateid, stateid);
5073 data->res.fattr = &data->fattr;
5074 data->res.server = server;
5075 nfs_fattr_init(data->res.fattr);
5076 data->timestamp = jiffies;
5077 data->rpc_status = 0;
5079 task_setup_data.callback_data = data;
5080 msg.rpc_argp = &data->args;
5081 msg.rpc_resp = &data->res;
5082 task = rpc_run_task(&task_setup_data);
5084 return PTR_ERR(task);
5087 status = nfs4_wait_for_completion_rpc_task(task);
5090 status = data->rpc_status;
5092 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5094 nfs_refresh_inode(inode, &data->fattr);
5100 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5102 struct nfs_server *server = NFS_SERVER(inode);
5103 struct nfs4_exception exception = { };
5106 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5107 trace_nfs4_delegreturn(inode, err);
5109 case -NFS4ERR_STALE_STATEID:
5110 case -NFS4ERR_EXPIRED:
5114 err = nfs4_handle_exception(server, err, &exception);
5115 } while (exception.retry);
5119 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5120 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5123 * sleep, with exponential backoff, and retry the LOCK operation.
5125 static unsigned long
5126 nfs4_set_lock_task_retry(unsigned long timeout)
5128 freezable_schedule_timeout_killable_unsafe(timeout);
5130 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5131 return NFS4_LOCK_MAXTIMEOUT;
5135 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5137 struct inode *inode = state->inode;
5138 struct nfs_server *server = NFS_SERVER(inode);
5139 struct nfs_client *clp = server->nfs_client;
5140 struct nfs_lockt_args arg = {
5141 .fh = NFS_FH(inode),
5144 struct nfs_lockt_res res = {
5147 struct rpc_message msg = {
5148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5151 .rpc_cred = state->owner->so_cred,
5153 struct nfs4_lock_state *lsp;
5156 arg.lock_owner.clientid = clp->cl_clientid;
5157 status = nfs4_set_lock_state(state, request);
5160 lsp = request->fl_u.nfs4_fl.owner;
5161 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5162 arg.lock_owner.s_dev = server->s_dev;
5163 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5166 request->fl_type = F_UNLCK;
5168 case -NFS4ERR_DENIED:
5171 request->fl_ops->fl_release_private(request);
5172 request->fl_ops = NULL;
5177 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5179 struct nfs4_exception exception = { };
5183 err = _nfs4_proc_getlk(state, cmd, request);
5184 trace_nfs4_get_lock(request, state, cmd, err);
5185 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5187 } while (exception.retry);
5191 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5194 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5196 res = posix_lock_file_wait(file, fl);
5199 res = flock_lock_file_wait(file, fl);
5207 struct nfs4_unlockdata {
5208 struct nfs_locku_args arg;
5209 struct nfs_locku_res res;
5210 struct nfs4_lock_state *lsp;
5211 struct nfs_open_context *ctx;
5212 struct file_lock fl;
5213 const struct nfs_server *server;
5214 unsigned long timestamp;
5217 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5218 struct nfs_open_context *ctx,
5219 struct nfs4_lock_state *lsp,
5220 struct nfs_seqid *seqid)
5222 struct nfs4_unlockdata *p;
5223 struct inode *inode = lsp->ls_state->inode;
5225 p = kzalloc(sizeof(*p), GFP_NOFS);
5228 p->arg.fh = NFS_FH(inode);
5230 p->arg.seqid = seqid;
5231 p->res.seqid = seqid;
5232 p->arg.stateid = &lsp->ls_stateid;
5234 atomic_inc(&lsp->ls_count);
5235 /* Ensure we don't close file until we're done freeing locks! */
5236 p->ctx = get_nfs_open_context(ctx);
5237 memcpy(&p->fl, fl, sizeof(p->fl));
5238 p->server = NFS_SERVER(inode);
5242 static void nfs4_locku_release_calldata(void *data)
5244 struct nfs4_unlockdata *calldata = data;
5245 nfs_free_seqid(calldata->arg.seqid);
5246 nfs4_put_lock_state(calldata->lsp);
5247 put_nfs_open_context(calldata->ctx);
5251 static void nfs4_locku_done(struct rpc_task *task, void *data)
5253 struct nfs4_unlockdata *calldata = data;
5255 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5257 switch (task->tk_status) {
5259 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5260 &calldata->res.stateid);
5261 renew_lease(calldata->server, calldata->timestamp);
5263 case -NFS4ERR_BAD_STATEID:
5264 case -NFS4ERR_OLD_STATEID:
5265 case -NFS4ERR_STALE_STATEID:
5266 case -NFS4ERR_EXPIRED:
5269 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
5270 rpc_restart_call_prepare(task);
5272 nfs_release_seqid(calldata->arg.seqid);
5275 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5277 struct nfs4_unlockdata *calldata = data;
5279 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5281 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5282 /* Note: exit _without_ running nfs4_locku_done */
5285 calldata->timestamp = jiffies;
5286 if (nfs4_setup_sequence(calldata->server,
5287 &calldata->arg.seq_args,
5288 &calldata->res.seq_res,
5290 nfs_release_seqid(calldata->arg.seqid);
5293 task->tk_action = NULL;
5295 nfs4_sequence_done(task, &calldata->res.seq_res);
5298 static const struct rpc_call_ops nfs4_locku_ops = {
5299 .rpc_call_prepare = nfs4_locku_prepare,
5300 .rpc_call_done = nfs4_locku_done,
5301 .rpc_release = nfs4_locku_release_calldata,
5304 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5305 struct nfs_open_context *ctx,
5306 struct nfs4_lock_state *lsp,
5307 struct nfs_seqid *seqid)
5309 struct nfs4_unlockdata *data;
5310 struct rpc_message msg = {
5311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5312 .rpc_cred = ctx->cred,
5314 struct rpc_task_setup task_setup_data = {
5315 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5316 .rpc_message = &msg,
5317 .callback_ops = &nfs4_locku_ops,
5318 .workqueue = nfsiod_workqueue,
5319 .flags = RPC_TASK_ASYNC,
5322 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5323 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5325 /* Ensure this is an unlock - when canceling a lock, the
5326 * canceled lock is passed in, and it won't be an unlock.
5328 fl->fl_type = F_UNLCK;
5330 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5332 nfs_free_seqid(seqid);
5333 return ERR_PTR(-ENOMEM);
5336 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5337 msg.rpc_argp = &data->arg;
5338 msg.rpc_resp = &data->res;
5339 task_setup_data.callback_data = data;
5340 return rpc_run_task(&task_setup_data);
5343 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5345 struct inode *inode = state->inode;
5346 struct nfs4_state_owner *sp = state->owner;
5347 struct nfs_inode *nfsi = NFS_I(inode);
5348 struct nfs_seqid *seqid;
5349 struct nfs4_lock_state *lsp;
5350 struct rpc_task *task;
5352 unsigned char fl_flags = request->fl_flags;
5354 status = nfs4_set_lock_state(state, request);
5355 /* Unlock _before_ we do the RPC call */
5356 request->fl_flags |= FL_EXISTS;
5357 /* Exclude nfs_delegation_claim_locks() */
5358 mutex_lock(&sp->so_delegreturn_mutex);
5359 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5360 down_read(&nfsi->rwsem);
5361 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5362 up_read(&nfsi->rwsem);
5363 mutex_unlock(&sp->so_delegreturn_mutex);
5366 up_read(&nfsi->rwsem);
5367 mutex_unlock(&sp->so_delegreturn_mutex);
5370 /* Is this a delegated lock? */
5371 lsp = request->fl_u.nfs4_fl.owner;
5372 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5374 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5378 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5379 status = PTR_ERR(task);
5382 status = nfs4_wait_for_completion_rpc_task(task);
5385 request->fl_flags = fl_flags;
5386 trace_nfs4_unlock(request, state, F_SETLK, status);
5390 struct nfs4_lockdata {
5391 struct nfs_lock_args arg;
5392 struct nfs_lock_res res;
5393 struct nfs4_lock_state *lsp;
5394 struct nfs_open_context *ctx;
5395 struct file_lock fl;
5396 unsigned long timestamp;
5399 struct nfs_server *server;
5402 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5403 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5406 struct nfs4_lockdata *p;
5407 struct inode *inode = lsp->ls_state->inode;
5408 struct nfs_server *server = NFS_SERVER(inode);
5410 p = kzalloc(sizeof(*p), gfp_mask);
5414 p->arg.fh = NFS_FH(inode);
5416 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5417 if (p->arg.open_seqid == NULL)
5419 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5420 if (p->arg.lock_seqid == NULL)
5421 goto out_free_seqid;
5422 p->arg.lock_stateid = &lsp->ls_stateid;
5423 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5424 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5425 p->arg.lock_owner.s_dev = server->s_dev;
5426 p->res.lock_seqid = p->arg.lock_seqid;
5429 atomic_inc(&lsp->ls_count);
5430 p->ctx = get_nfs_open_context(ctx);
5431 memcpy(&p->fl, fl, sizeof(p->fl));
5434 nfs_free_seqid(p->arg.open_seqid);
5440 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5442 struct nfs4_lockdata *data = calldata;
5443 struct nfs4_state *state = data->lsp->ls_state;
5445 dprintk("%s: begin!\n", __func__);
5446 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5448 /* Do we need to do an open_to_lock_owner? */
5449 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5450 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5451 goto out_release_lock_seqid;
5453 data->arg.open_stateid = &state->open_stateid;
5454 data->arg.new_lock_owner = 1;
5455 data->res.open_seqid = data->arg.open_seqid;
5457 data->arg.new_lock_owner = 0;
5458 if (!nfs4_valid_open_stateid(state)) {
5459 data->rpc_status = -EBADF;
5460 task->tk_action = NULL;
5461 goto out_release_open_seqid;
5463 data->timestamp = jiffies;
5464 if (nfs4_setup_sequence(data->server,
5465 &data->arg.seq_args,
5469 out_release_open_seqid:
5470 nfs_release_seqid(data->arg.open_seqid);
5471 out_release_lock_seqid:
5472 nfs_release_seqid(data->arg.lock_seqid);
5474 nfs4_sequence_done(task, &data->res.seq_res);
5475 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5478 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5480 struct nfs4_lockdata *data = calldata;
5482 dprintk("%s: begin!\n", __func__);
5484 if (!nfs4_sequence_done(task, &data->res.seq_res))
5487 data->rpc_status = task->tk_status;
5488 if (data->arg.new_lock_owner != 0) {
5489 if (data->rpc_status == 0)
5490 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5494 if (data->rpc_status == 0) {
5495 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5496 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5497 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5500 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5503 static void nfs4_lock_release(void *calldata)
5505 struct nfs4_lockdata *data = calldata;
5507 dprintk("%s: begin!\n", __func__);
5508 nfs_free_seqid(data->arg.open_seqid);
5509 if (data->cancelled != 0) {
5510 struct rpc_task *task;
5511 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5512 data->arg.lock_seqid);
5514 rpc_put_task_async(task);
5515 dprintk("%s: cancelling lock!\n", __func__);
5517 nfs_free_seqid(data->arg.lock_seqid);
5518 nfs4_put_lock_state(data->lsp);
5519 put_nfs_open_context(data->ctx);
5521 dprintk("%s: done!\n", __func__);
5524 static const struct rpc_call_ops nfs4_lock_ops = {
5525 .rpc_call_prepare = nfs4_lock_prepare,
5526 .rpc_call_done = nfs4_lock_done,
5527 .rpc_release = nfs4_lock_release,
5530 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5533 case -NFS4ERR_ADMIN_REVOKED:
5534 case -NFS4ERR_BAD_STATEID:
5535 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5536 if (new_lock_owner != 0 ||
5537 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5538 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5540 case -NFS4ERR_STALE_STATEID:
5541 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5542 case -NFS4ERR_EXPIRED:
5543 nfs4_schedule_lease_recovery(server->nfs_client);
5547 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5549 struct nfs4_lockdata *data;
5550 struct rpc_task *task;
5551 struct rpc_message msg = {
5552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5553 .rpc_cred = state->owner->so_cred,
5555 struct rpc_task_setup task_setup_data = {
5556 .rpc_client = NFS_CLIENT(state->inode),
5557 .rpc_message = &msg,
5558 .callback_ops = &nfs4_lock_ops,
5559 .workqueue = nfsiod_workqueue,
5560 .flags = RPC_TASK_ASYNC,
5564 dprintk("%s: begin!\n", __func__);
5565 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5566 fl->fl_u.nfs4_fl.owner,
5567 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5571 data->arg.block = 1;
5572 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5573 msg.rpc_argp = &data->arg;
5574 msg.rpc_resp = &data->res;
5575 task_setup_data.callback_data = data;
5576 if (recovery_type > NFS_LOCK_NEW) {
5577 if (recovery_type == NFS_LOCK_RECLAIM)
5578 data->arg.reclaim = NFS_LOCK_RECLAIM;
5579 nfs4_set_sequence_privileged(&data->arg.seq_args);
5581 task = rpc_run_task(&task_setup_data);
5583 return PTR_ERR(task);
5584 ret = nfs4_wait_for_completion_rpc_task(task);
5586 ret = data->rpc_status;
5588 nfs4_handle_setlk_error(data->server, data->lsp,
5589 data->arg.new_lock_owner, ret);
5591 data->cancelled = 1;
5593 dprintk("%s: done, ret = %d!\n", __func__, ret);
5597 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5599 struct nfs_server *server = NFS_SERVER(state->inode);
5600 struct nfs4_exception exception = {
5601 .inode = state->inode,
5606 /* Cache the lock if possible... */
5607 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5609 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5610 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5611 if (err != -NFS4ERR_DELAY)
5613 nfs4_handle_exception(server, err, &exception);
5614 } while (exception.retry);
5618 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5620 struct nfs_server *server = NFS_SERVER(state->inode);
5621 struct nfs4_exception exception = {
5622 .inode = state->inode,
5626 err = nfs4_set_lock_state(state, request);
5629 if (!recover_lost_locks) {
5630 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5634 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5636 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5637 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5641 case -NFS4ERR_GRACE:
5642 case -NFS4ERR_DELAY:
5643 nfs4_handle_exception(server, err, &exception);
5646 } while (exception.retry);
5651 #if defined(CONFIG_NFS_V4_1)
5653 * nfs41_check_expired_locks - possibly free a lock stateid
5655 * @state: NFSv4 state for an inode
5657 * Returns NFS_OK if recovery for this stateid is now finished.
5658 * Otherwise a negative NFS4ERR value is returned.
5660 static int nfs41_check_expired_locks(struct nfs4_state *state)
5662 int status, ret = -NFS4ERR_BAD_STATEID;
5663 struct nfs4_lock_state *lsp;
5664 struct nfs_server *server = NFS_SERVER(state->inode);
5666 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5667 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5668 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5670 status = nfs41_test_stateid(server,
5673 trace_nfs4_test_lock_stateid(state, lsp, status);
5674 if (status != NFS_OK) {
5675 /* Free the stateid unless the server
5676 * informs us the stateid is unrecognized. */
5677 if (status != -NFS4ERR_BAD_STATEID)
5678 nfs41_free_stateid(server,
5681 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5690 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5692 int status = NFS_OK;
5694 if (test_bit(LK_STATE_IN_USE, &state->flags))
5695 status = nfs41_check_expired_locks(state);
5696 if (status != NFS_OK)
5697 status = nfs4_lock_expired(state, request);
5702 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5704 struct nfs4_state_owner *sp = state->owner;
5705 struct nfs_inode *nfsi = NFS_I(state->inode);
5706 unsigned char fl_flags = request->fl_flags;
5708 int status = -ENOLCK;
5710 if ((fl_flags & FL_POSIX) &&
5711 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5713 /* Is this a delegated open? */
5714 status = nfs4_set_lock_state(state, request);
5717 request->fl_flags |= FL_ACCESS;
5718 status = do_vfs_lock(request->fl_file, request);
5721 down_read(&nfsi->rwsem);
5722 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5723 /* Yes: cache locks! */
5724 /* ...but avoid races with delegation recall... */
5725 request->fl_flags = fl_flags & ~FL_SLEEP;
5726 status = do_vfs_lock(request->fl_file, request);
5729 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5730 up_read(&nfsi->rwsem);
5731 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5734 down_read(&nfsi->rwsem);
5735 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5736 status = -NFS4ERR_DELAY;
5739 /* Note: we always want to sleep here! */
5740 request->fl_flags = fl_flags | FL_SLEEP;
5741 if (do_vfs_lock(request->fl_file, request) < 0)
5742 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5743 "manager!\n", __func__);
5745 up_read(&nfsi->rwsem);
5747 request->fl_flags = fl_flags;
5751 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5753 struct nfs4_exception exception = {
5755 .inode = state->inode,
5760 err = _nfs4_proc_setlk(state, cmd, request);
5761 trace_nfs4_set_lock(request, state, cmd, err);
5762 if (err == -NFS4ERR_DENIED)
5764 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5766 } while (exception.retry);
5771 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5773 struct nfs_open_context *ctx;
5774 struct nfs4_state *state;
5775 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5778 /* verify open state */
5779 ctx = nfs_file_open_context(filp);
5782 if (request->fl_start < 0 || request->fl_end < 0)
5785 if (IS_GETLK(cmd)) {
5787 return nfs4_proc_getlk(state, F_GETLK, request);
5791 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5794 if (request->fl_type == F_UNLCK) {
5796 return nfs4_proc_unlck(state, cmd, request);
5803 * Don't rely on the VFS having checked the file open mode,
5804 * since it won't do this for flock() locks.
5806 switch (request->fl_type) {
5808 if (!(filp->f_mode & FMODE_READ))
5812 if (!(filp->f_mode & FMODE_WRITE))
5817 status = nfs4_proc_setlk(state, cmd, request);
5818 if ((status != -EAGAIN) || IS_SETLK(cmd))
5820 timeout = nfs4_set_lock_task_retry(timeout);
5821 status = -ERESTARTSYS;
5824 } while(status < 0);
5828 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5830 struct nfs_server *server = NFS_SERVER(state->inode);
5833 err = nfs4_set_lock_state(state, fl);
5836 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5837 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5840 struct nfs_release_lockowner_data {
5841 struct nfs4_lock_state *lsp;
5842 struct nfs_server *server;
5843 struct nfs_release_lockowner_args args;
5844 struct nfs4_sequence_args seq_args;
5845 struct nfs4_sequence_res seq_res;
5846 unsigned long timestamp;
5849 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5851 struct nfs_release_lockowner_data *data = calldata;
5852 nfs40_setup_sequence(data->server,
5853 &data->seq_args, &data->seq_res, task);
5854 data->timestamp = jiffies;
5857 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5859 struct nfs_release_lockowner_data *data = calldata;
5860 struct nfs_server *server = data->server;
5862 nfs40_sequence_done(task, &data->seq_res);
5864 switch (task->tk_status) {
5866 renew_lease(server, data->timestamp);
5868 case -NFS4ERR_STALE_CLIENTID:
5869 case -NFS4ERR_EXPIRED:
5870 case -NFS4ERR_LEASE_MOVED:
5871 case -NFS4ERR_DELAY:
5872 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5873 rpc_restart_call_prepare(task);
5877 static void nfs4_release_lockowner_release(void *calldata)
5879 struct nfs_release_lockowner_data *data = calldata;
5880 nfs4_free_lock_state(data->server, data->lsp);
5884 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5885 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5886 .rpc_call_done = nfs4_release_lockowner_done,
5887 .rpc_release = nfs4_release_lockowner_release,
5890 static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
5892 struct nfs_release_lockowner_data *data;
5893 struct rpc_message msg = {
5894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5897 if (server->nfs_client->cl_mvops->minor_version != 0)
5900 data = kmalloc(sizeof(*data), GFP_NOFS);
5903 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
5905 data->server = server;
5906 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5907 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5908 data->args.lock_owner.s_dev = server->s_dev;
5910 msg.rpc_argp = &data->args;
5911 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5915 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5917 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5918 const void *buf, size_t buflen,
5919 int flags, int type)
5921 if (strcmp(key, "") != 0)
5924 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5927 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5928 void *buf, size_t buflen, int type)
5930 if (strcmp(key, "") != 0)
5933 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5936 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5937 size_t list_len, const char *name,
5938 size_t name_len, int type)
5940 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5942 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5945 if (list && len <= list_len)
5946 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5950 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5951 static inline int nfs4_server_supports_labels(struct nfs_server *server)
5953 return server->caps & NFS_CAP_SECURITY_LABEL;
5956 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5957 const void *buf, size_t buflen,
5958 int flags, int type)
5960 if (security_ismaclabel(key))
5961 return nfs4_set_security_label(dentry, buf, buflen);
5966 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5967 void *buf, size_t buflen, int type)
5969 if (security_ismaclabel(key))
5970 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5974 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5975 size_t list_len, const char *name,
5976 size_t name_len, int type)
5980 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5981 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5982 if (list && len <= list_len)
5983 security_inode_listsecurity(dentry->d_inode, list, len);
5988 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5989 .prefix = XATTR_SECURITY_PREFIX,
5990 .list = nfs4_xattr_list_nfs4_label,
5991 .get = nfs4_xattr_get_nfs4_label,
5992 .set = nfs4_xattr_set_nfs4_label,
5998 * nfs_fhget will use either the mounted_on_fileid or the fileid
6000 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6002 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6003 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6004 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6005 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6008 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6009 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6010 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6014 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6015 const struct qstr *name,
6016 struct nfs4_fs_locations *fs_locations,
6019 struct nfs_server *server = NFS_SERVER(dir);
6021 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6023 struct nfs4_fs_locations_arg args = {
6024 .dir_fh = NFS_FH(dir),
6029 struct nfs4_fs_locations_res res = {
6030 .fs_locations = fs_locations,
6032 struct rpc_message msg = {
6033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6039 dprintk("%s: start\n", __func__);
6041 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6042 * is not supported */
6043 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6044 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6046 bitmask[0] |= FATTR4_WORD0_FILEID;
6048 nfs_fattr_init(&fs_locations->fattr);
6049 fs_locations->server = server;
6050 fs_locations->nlocations = 0;
6051 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6052 dprintk("%s: returned status = %d\n", __func__, status);
6056 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6057 const struct qstr *name,
6058 struct nfs4_fs_locations *fs_locations,
6061 struct nfs4_exception exception = { };
6064 err = _nfs4_proc_fs_locations(client, dir, name,
6065 fs_locations, page);
6066 trace_nfs4_get_fs_locations(dir, name, err);
6067 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6069 } while (exception.retry);
6074 * This operation also signals the server that this client is
6075 * performing migration recovery. The server can stop returning
6076 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6077 * appended to this compound to identify the client ID which is
6078 * performing recovery.
6080 static int _nfs40_proc_get_locations(struct inode *inode,
6081 struct nfs4_fs_locations *locations,
6082 struct page *page, struct rpc_cred *cred)
6084 struct nfs_server *server = NFS_SERVER(inode);
6085 struct rpc_clnt *clnt = server->client;
6087 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6089 struct nfs4_fs_locations_arg args = {
6090 .clientid = server->nfs_client->cl_clientid,
6091 .fh = NFS_FH(inode),
6094 .migration = 1, /* skip LOOKUP */
6095 .renew = 1, /* append RENEW */
6097 struct nfs4_fs_locations_res res = {
6098 .fs_locations = locations,
6102 struct rpc_message msg = {
6103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6108 unsigned long now = jiffies;
6111 nfs_fattr_init(&locations->fattr);
6112 locations->server = server;
6113 locations->nlocations = 0;
6115 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6116 nfs4_set_sequence_privileged(&args.seq_args);
6117 status = nfs4_call_sync_sequence(clnt, server, &msg,
6118 &args.seq_args, &res.seq_res);
6122 renew_lease(server, now);
6126 #ifdef CONFIG_NFS_V4_1
6129 * This operation also signals the server that this client is
6130 * performing migration recovery. The server can stop asserting
6131 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6132 * performing this operation is identified in the SEQUENCE
6133 * operation in this compound.
6135 * When the client supports GETATTR(fs_locations_info), it can
6136 * be plumbed in here.
6138 static int _nfs41_proc_get_locations(struct inode *inode,
6139 struct nfs4_fs_locations *locations,
6140 struct page *page, struct rpc_cred *cred)
6142 struct nfs_server *server = NFS_SERVER(inode);
6143 struct rpc_clnt *clnt = server->client;
6145 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6147 struct nfs4_fs_locations_arg args = {
6148 .fh = NFS_FH(inode),
6151 .migration = 1, /* skip LOOKUP */
6153 struct nfs4_fs_locations_res res = {
6154 .fs_locations = locations,
6157 struct rpc_message msg = {
6158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6165 nfs_fattr_init(&locations->fattr);
6166 locations->server = server;
6167 locations->nlocations = 0;
6169 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6170 nfs4_set_sequence_privileged(&args.seq_args);
6171 status = nfs4_call_sync_sequence(clnt, server, &msg,
6172 &args.seq_args, &res.seq_res);
6173 if (status == NFS4_OK &&
6174 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6175 status = -NFS4ERR_LEASE_MOVED;
6179 #endif /* CONFIG_NFS_V4_1 */
6182 * nfs4_proc_get_locations - discover locations for a migrated FSID
6183 * @inode: inode on FSID that is migrating
6184 * @locations: result of query
6186 * @cred: credential to use for this operation
6188 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6189 * operation failed, or a negative errno if a local error occurred.
6191 * On success, "locations" is filled in, but if the server has
6192 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6195 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6196 * from this client that require migration recovery.
6198 int nfs4_proc_get_locations(struct inode *inode,
6199 struct nfs4_fs_locations *locations,
6200 struct page *page, struct rpc_cred *cred)
6202 struct nfs_server *server = NFS_SERVER(inode);
6203 struct nfs_client *clp = server->nfs_client;
6204 const struct nfs4_mig_recovery_ops *ops =
6205 clp->cl_mvops->mig_recovery_ops;
6206 struct nfs4_exception exception = { };
6209 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6210 (unsigned long long)server->fsid.major,
6211 (unsigned long long)server->fsid.minor,
6213 nfs_display_fhandle(NFS_FH(inode), __func__);
6216 status = ops->get_locations(inode, locations, page, cred);
6217 if (status != -NFS4ERR_DELAY)
6219 nfs4_handle_exception(server, status, &exception);
6220 } while (exception.retry);
6225 * This operation also signals the server that this client is
6226 * performing "lease moved" recovery. The server can stop
6227 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6228 * is appended to this compound to identify the client ID which is
6229 * performing recovery.
6231 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6233 struct nfs_server *server = NFS_SERVER(inode);
6234 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6235 struct rpc_clnt *clnt = server->client;
6236 struct nfs4_fsid_present_arg args = {
6237 .fh = NFS_FH(inode),
6238 .clientid = clp->cl_clientid,
6239 .renew = 1, /* append RENEW */
6241 struct nfs4_fsid_present_res res = {
6244 struct rpc_message msg = {
6245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6250 unsigned long now = jiffies;
6253 res.fh = nfs_alloc_fhandle();
6257 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6258 nfs4_set_sequence_privileged(&args.seq_args);
6259 status = nfs4_call_sync_sequence(clnt, server, &msg,
6260 &args.seq_args, &res.seq_res);
6261 nfs_free_fhandle(res.fh);
6265 do_renew_lease(clp, now);
6269 #ifdef CONFIG_NFS_V4_1
6272 * This operation also signals the server that this client is
6273 * performing "lease moved" recovery. The server can stop asserting
6274 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6275 * this operation is identified in the SEQUENCE operation in this
6278 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6280 struct nfs_server *server = NFS_SERVER(inode);
6281 struct rpc_clnt *clnt = server->client;
6282 struct nfs4_fsid_present_arg args = {
6283 .fh = NFS_FH(inode),
6285 struct nfs4_fsid_present_res res = {
6287 struct rpc_message msg = {
6288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6295 res.fh = nfs_alloc_fhandle();
6299 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6300 nfs4_set_sequence_privileged(&args.seq_args);
6301 status = nfs4_call_sync_sequence(clnt, server, &msg,
6302 &args.seq_args, &res.seq_res);
6303 nfs_free_fhandle(res.fh);
6304 if (status == NFS4_OK &&
6305 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6306 status = -NFS4ERR_LEASE_MOVED;
6310 #endif /* CONFIG_NFS_V4_1 */
6313 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6314 * @inode: inode on FSID to check
6315 * @cred: credential to use for this operation
6317 * Server indicates whether the FSID is present, moved, or not
6318 * recognized. This operation is necessary to clear a LEASE_MOVED
6319 * condition for this client ID.
6321 * Returns NFS4_OK if the FSID is present on this server,
6322 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6323 * NFS4ERR code if some error occurred on the server, or a
6324 * negative errno if a local failure occurred.
6326 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6328 struct nfs_server *server = NFS_SERVER(inode);
6329 struct nfs_client *clp = server->nfs_client;
6330 const struct nfs4_mig_recovery_ops *ops =
6331 clp->cl_mvops->mig_recovery_ops;
6332 struct nfs4_exception exception = { };
6335 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6336 (unsigned long long)server->fsid.major,
6337 (unsigned long long)server->fsid.minor,
6339 nfs_display_fhandle(NFS_FH(inode), __func__);
6342 status = ops->fsid_present(inode, cred);
6343 if (status != -NFS4ERR_DELAY)
6345 nfs4_handle_exception(server, status, &exception);
6346 } while (exception.retry);
6351 * If 'use_integrity' is true and the state managment nfs_client
6352 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6353 * and the machine credential as per RFC3530bis and RFC5661 Security
6354 * Considerations sections. Otherwise, just use the user cred with the
6355 * filesystem's rpc_client.
6357 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6360 struct nfs4_secinfo_arg args = {
6361 .dir_fh = NFS_FH(dir),
6364 struct nfs4_secinfo_res res = {
6367 struct rpc_message msg = {
6368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6372 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6373 struct rpc_cred *cred = NULL;
6375 if (use_integrity) {
6376 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6377 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6378 msg.rpc_cred = cred;
6381 dprintk("NFS call secinfo %s\n", name->name);
6383 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6384 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6386 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6388 dprintk("NFS reply secinfo: %d\n", status);
6396 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6397 struct nfs4_secinfo_flavors *flavors)
6399 struct nfs4_exception exception = { };
6402 err = -NFS4ERR_WRONGSEC;
6404 /* try to use integrity protection with machine cred */
6405 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6406 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6409 * if unable to use integrity protection, or SECINFO with
6410 * integrity protection returns NFS4ERR_WRONGSEC (which is
6411 * disallowed by spec, but exists in deployed servers) use
6412 * the current filesystem's rpc_client and the user cred.
6414 if (err == -NFS4ERR_WRONGSEC)
6415 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6417 trace_nfs4_secinfo(dir, name, err);
6418 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6420 } while (exception.retry);
6424 #ifdef CONFIG_NFS_V4_1
6426 * Check the exchange flags returned by the server for invalid flags, having
6427 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6430 static int nfs4_check_cl_exchange_flags(u32 flags)
6432 if (flags & ~EXCHGID4_FLAG_MASK_R)
6434 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6435 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6437 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6441 return -NFS4ERR_INVAL;
6445 nfs41_same_server_scope(struct nfs41_server_scope *a,
6446 struct nfs41_server_scope *b)
6448 if (a->server_scope_sz == b->server_scope_sz &&
6449 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6456 * nfs4_proc_bind_conn_to_session()
6458 * The 4.1 client currently uses the same TCP connection for the
6459 * fore and backchannel.
6461 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6464 struct nfs41_bind_conn_to_session_res res;
6465 struct rpc_message msg = {
6467 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6473 dprintk("--> %s\n", __func__);
6475 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6476 if (unlikely(res.session == NULL)) {
6481 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6482 trace_nfs4_bind_conn_to_session(clp, status);
6484 if (memcmp(res.session->sess_id.data,
6485 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6486 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6490 if (res.dir != NFS4_CDFS4_BOTH) {
6491 dprintk("NFS: %s: Unexpected direction from server\n",
6496 if (res.use_conn_in_rdma_mode) {
6497 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6506 dprintk("<-- %s status= %d\n", __func__, status);
6511 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6512 * and operations we'd like to see to enable certain features in the allow map
6514 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6515 .how = SP4_MACH_CRED,
6516 .enforce.u.words = {
6517 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6518 1 << (OP_EXCHANGE_ID - 32) |
6519 1 << (OP_CREATE_SESSION - 32) |
6520 1 << (OP_DESTROY_SESSION - 32) |
6521 1 << (OP_DESTROY_CLIENTID - 32)
6524 [0] = 1 << (OP_CLOSE) |
6527 [1] = 1 << (OP_SECINFO - 32) |
6528 1 << (OP_SECINFO_NO_NAME - 32) |
6529 1 << (OP_TEST_STATEID - 32) |
6530 1 << (OP_FREE_STATEID - 32) |
6531 1 << (OP_WRITE - 32)
6536 * Select the state protection mode for client `clp' given the server results
6537 * from exchange_id in `sp'.
6539 * Returns 0 on success, negative errno otherwise.
6541 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6542 struct nfs41_state_protection *sp)
6544 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6545 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6546 1 << (OP_EXCHANGE_ID - 32) |
6547 1 << (OP_CREATE_SESSION - 32) |
6548 1 << (OP_DESTROY_SESSION - 32) |
6549 1 << (OP_DESTROY_CLIENTID - 32)
6553 if (sp->how == SP4_MACH_CRED) {
6554 /* Print state protect result */
6555 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6556 for (i = 0; i <= LAST_NFS4_OP; i++) {
6557 if (test_bit(i, sp->enforce.u.longs))
6558 dfprintk(MOUNT, " enforce op %d\n", i);
6559 if (test_bit(i, sp->allow.u.longs))
6560 dfprintk(MOUNT, " allow op %d\n", i);
6563 /* make sure nothing is on enforce list that isn't supported */
6564 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6565 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6566 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6572 * Minimal mode - state operations are allowed to use machine
6573 * credential. Note this already happens by default, so the
6574 * client doesn't have to do anything more than the negotiation.
6576 * NOTE: we don't care if EXCHANGE_ID is in the list -
6577 * we're already using the machine cred for exchange_id
6578 * and will never use a different cred.
6580 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6581 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6582 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6583 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6584 dfprintk(MOUNT, "sp4_mach_cred:\n");
6585 dfprintk(MOUNT, " minimal mode enabled\n");
6586 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6588 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6592 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6593 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6594 dfprintk(MOUNT, " cleanup mode enabled\n");
6595 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6598 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6599 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6600 dfprintk(MOUNT, " secinfo mode enabled\n");
6601 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6604 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6605 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6606 dfprintk(MOUNT, " stateid mode enabled\n");
6607 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6610 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6611 dfprintk(MOUNT, " write mode enabled\n");
6612 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6615 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6616 dfprintk(MOUNT, " commit mode enabled\n");
6617 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6625 * _nfs4_proc_exchange_id()
6627 * Wrapper for EXCHANGE_ID operation.
6629 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6632 nfs4_verifier verifier;
6633 struct nfs41_exchange_id_args args = {
6634 .verifier = &verifier,
6636 #ifdef CONFIG_NFS_V4_1_MIGRATION
6637 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6638 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6639 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6641 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6642 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6645 struct nfs41_exchange_id_res res = {
6649 struct rpc_message msg = {
6650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6656 nfs4_init_boot_verifier(clp, &verifier);
6657 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6659 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6660 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6661 args.id_len, args.id);
6663 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6665 if (unlikely(res.server_owner == NULL)) {
6670 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6672 if (unlikely(res.server_scope == NULL)) {
6674 goto out_server_owner;
6677 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6678 if (unlikely(res.impl_id == NULL)) {
6680 goto out_server_scope;
6685 args.state_protect.how = SP4_NONE;
6689 args.state_protect = nfs4_sp4_mach_cred_request;
6696 goto out_server_scope;
6699 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6700 trace_nfs4_exchange_id(clp, status);
6702 status = nfs4_check_cl_exchange_flags(res.flags);
6705 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6708 clp->cl_clientid = res.clientid;
6709 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6710 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6711 clp->cl_seqid = res.seqid;
6713 kfree(clp->cl_serverowner);
6714 clp->cl_serverowner = res.server_owner;
6715 res.server_owner = NULL;
6717 /* use the most recent implementation id */
6718 kfree(clp->cl_implid);
6719 clp->cl_implid = res.impl_id;
6721 if (clp->cl_serverscope != NULL &&
6722 !nfs41_same_server_scope(clp->cl_serverscope,
6723 res.server_scope)) {
6724 dprintk("%s: server_scope mismatch detected\n",
6726 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6727 kfree(clp->cl_serverscope);
6728 clp->cl_serverscope = NULL;
6731 if (clp->cl_serverscope == NULL) {
6732 clp->cl_serverscope = res.server_scope;
6739 kfree(res.server_owner);
6741 kfree(res.server_scope);
6743 if (clp->cl_implid != NULL)
6744 dprintk("NFS reply exchange_id: Server Implementation ID: "
6745 "domain: %s, name: %s, date: %llu,%u\n",
6746 clp->cl_implid->domain, clp->cl_implid->name,
6747 clp->cl_implid->date.seconds,
6748 clp->cl_implid->date.nseconds);
6749 dprintk("NFS reply exchange_id: %d\n", status);
6754 * nfs4_proc_exchange_id()
6756 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6758 * Since the clientid has expired, all compounds using sessions
6759 * associated with the stale clientid will be returning
6760 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6761 * be in some phase of session reset.
6763 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6765 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6767 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6770 /* try SP4_MACH_CRED if krb5i/p */
6771 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6772 authflavor == RPC_AUTH_GSS_KRB5P) {
6773 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6779 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6782 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6783 struct rpc_cred *cred)
6785 struct rpc_message msg = {
6786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6792 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6793 trace_nfs4_destroy_clientid(clp, status);
6795 dprintk("NFS: Got error %d from the server %s on "
6796 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6800 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6801 struct rpc_cred *cred)
6806 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6807 ret = _nfs4_proc_destroy_clientid(clp, cred);
6809 case -NFS4ERR_DELAY:
6810 case -NFS4ERR_CLIENTID_BUSY:
6820 int nfs4_destroy_clientid(struct nfs_client *clp)
6822 struct rpc_cred *cred;
6825 if (clp->cl_mvops->minor_version < 1)
6827 if (clp->cl_exchange_flags == 0)
6829 if (clp->cl_preserve_clid)
6831 cred = nfs4_get_clid_cred(clp);
6832 ret = nfs4_proc_destroy_clientid(clp, cred);
6837 case -NFS4ERR_STALE_CLIENTID:
6838 clp->cl_exchange_flags = 0;
6844 struct nfs4_get_lease_time_data {
6845 struct nfs4_get_lease_time_args *args;
6846 struct nfs4_get_lease_time_res *res;
6847 struct nfs_client *clp;
6850 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6853 struct nfs4_get_lease_time_data *data =
6854 (struct nfs4_get_lease_time_data *)calldata;
6856 dprintk("--> %s\n", __func__);
6857 /* just setup sequence, do not trigger session recovery
6858 since we're invoked within one */
6859 nfs41_setup_sequence(data->clp->cl_session,
6860 &data->args->la_seq_args,
6861 &data->res->lr_seq_res,
6863 dprintk("<-- %s\n", __func__);
6867 * Called from nfs4_state_manager thread for session setup, so don't recover
6868 * from sequence operation or clientid errors.
6870 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6872 struct nfs4_get_lease_time_data *data =
6873 (struct nfs4_get_lease_time_data *)calldata;
6875 dprintk("--> %s\n", __func__);
6876 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6878 switch (task->tk_status) {
6879 case -NFS4ERR_DELAY:
6880 case -NFS4ERR_GRACE:
6881 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6882 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6883 task->tk_status = 0;
6885 case -NFS4ERR_RETRY_UNCACHED_REP:
6886 rpc_restart_call_prepare(task);
6889 dprintk("<-- %s\n", __func__);
6892 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
6893 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6894 .rpc_call_done = nfs4_get_lease_time_done,
6897 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6899 struct rpc_task *task;
6900 struct nfs4_get_lease_time_args args;
6901 struct nfs4_get_lease_time_res res = {
6902 .lr_fsinfo = fsinfo,
6904 struct nfs4_get_lease_time_data data = {
6909 struct rpc_message msg = {
6910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6914 struct rpc_task_setup task_setup = {
6915 .rpc_client = clp->cl_rpcclient,
6916 .rpc_message = &msg,
6917 .callback_ops = &nfs4_get_lease_time_ops,
6918 .callback_data = &data,
6919 .flags = RPC_TASK_TIMEOUT,
6923 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
6924 nfs4_set_sequence_privileged(&args.la_seq_args);
6925 dprintk("--> %s\n", __func__);
6926 task = rpc_run_task(&task_setup);
6929 status = PTR_ERR(task);
6931 status = task->tk_status;
6934 dprintk("<-- %s return %d\n", __func__, status);
6940 * Initialize the values to be used by the client in CREATE_SESSION
6941 * If nfs4_init_session set the fore channel request and response sizes,
6944 * Set the back channel max_resp_sz_cached to zero to force the client to
6945 * always set csa_cachethis to FALSE because the current implementation
6946 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6948 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6950 unsigned int max_rqst_sz, max_resp_sz;
6952 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6953 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6955 /* Fore channel attributes */
6956 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6957 args->fc_attrs.max_resp_sz = max_resp_sz;
6958 args->fc_attrs.max_ops = NFS4_MAX_OPS;
6959 args->fc_attrs.max_reqs = max_session_slots;
6961 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6962 "max_ops=%u max_reqs=%u\n",
6964 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
6965 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
6967 /* Back channel attributes */
6968 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6969 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6970 args->bc_attrs.max_resp_sz_cached = 0;
6971 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6972 args->bc_attrs.max_reqs = 1;
6974 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6975 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6977 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6978 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6979 args->bc_attrs.max_reqs);
6982 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6984 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6985 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6987 if (rcvd->max_resp_sz > sent->max_resp_sz)
6990 * Our requested max_ops is the minimum we need; we're not
6991 * prepared to break up compounds into smaller pieces than that.
6992 * So, no point even trying to continue if the server won't
6995 if (rcvd->max_ops < sent->max_ops)
6997 if (rcvd->max_reqs == 0)
6999 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7000 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7004 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7006 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7007 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
7009 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7011 if (rcvd->max_resp_sz < sent->max_resp_sz)
7013 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7015 /* These would render the backchannel useless: */
7016 if (rcvd->max_ops != sent->max_ops)
7018 if (rcvd->max_reqs != sent->max_reqs)
7023 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7024 struct nfs4_session *session)
7028 ret = nfs4_verify_fore_channel_attrs(args, session);
7031 return nfs4_verify_back_channel_attrs(args, session);
7034 static int _nfs4_proc_create_session(struct nfs_client *clp,
7035 struct rpc_cred *cred)
7037 struct nfs4_session *session = clp->cl_session;
7038 struct nfs41_create_session_args args = {
7040 .cb_program = NFS4_CALLBACK,
7042 struct nfs41_create_session_res res = {
7045 struct rpc_message msg = {
7046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7053 nfs4_init_channel_attrs(&args);
7054 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7056 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7057 trace_nfs4_create_session(clp, status);
7060 /* Verify the session's negotiated channel_attrs values */
7061 status = nfs4_verify_channel_attrs(&args, session);
7062 /* Increment the clientid slot sequence id */
7070 * Issues a CREATE_SESSION operation to the server.
7071 * It is the responsibility of the caller to verify the session is
7072 * expired before calling this routine.
7074 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7078 struct nfs4_session *session = clp->cl_session;
7080 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7082 status = _nfs4_proc_create_session(clp, cred);
7086 /* Init or reset the session slot tables */
7087 status = nfs4_setup_session_slot_tables(session);
7088 dprintk("slot table setup returned %d\n", status);
7092 ptr = (unsigned *)&session->sess_id.data[0];
7093 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7094 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7096 dprintk("<-- %s\n", __func__);
7101 * Issue the over-the-wire RPC DESTROY_SESSION.
7102 * The caller must serialize access to this routine.
7104 int nfs4_proc_destroy_session(struct nfs4_session *session,
7105 struct rpc_cred *cred)
7107 struct rpc_message msg = {
7108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7109 .rpc_argp = session,
7114 dprintk("--> nfs4_proc_destroy_session\n");
7116 /* session is still being setup */
7117 if (session->clp->cl_cons_state != NFS_CS_READY)
7120 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7121 trace_nfs4_destroy_session(session->clp, status);
7124 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7125 "Session has been destroyed regardless...\n", status);
7127 dprintk("<-- nfs4_proc_destroy_session\n");
7132 * Renew the cl_session lease.
7134 struct nfs4_sequence_data {
7135 struct nfs_client *clp;
7136 struct nfs4_sequence_args args;
7137 struct nfs4_sequence_res res;
7140 static void nfs41_sequence_release(void *data)
7142 struct nfs4_sequence_data *calldata = data;
7143 struct nfs_client *clp = calldata->clp;
7145 if (atomic_read(&clp->cl_count) > 1)
7146 nfs4_schedule_state_renewal(clp);
7147 nfs_put_client(clp);
7151 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7153 switch(task->tk_status) {
7154 case -NFS4ERR_DELAY:
7155 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7158 nfs4_schedule_lease_recovery(clp);
7163 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7165 struct nfs4_sequence_data *calldata = data;
7166 struct nfs_client *clp = calldata->clp;
7168 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7171 trace_nfs4_sequence(clp, task->tk_status);
7172 if (task->tk_status < 0) {
7173 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7174 if (atomic_read(&clp->cl_count) == 1)
7177 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7178 rpc_restart_call_prepare(task);
7182 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7184 dprintk("<-- %s\n", __func__);
7187 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7189 struct nfs4_sequence_data *calldata = data;
7190 struct nfs_client *clp = calldata->clp;
7191 struct nfs4_sequence_args *args;
7192 struct nfs4_sequence_res *res;
7194 args = task->tk_msg.rpc_argp;
7195 res = task->tk_msg.rpc_resp;
7197 nfs41_setup_sequence(clp->cl_session, args, res, task);
7200 static const struct rpc_call_ops nfs41_sequence_ops = {
7201 .rpc_call_done = nfs41_sequence_call_done,
7202 .rpc_call_prepare = nfs41_sequence_prepare,
7203 .rpc_release = nfs41_sequence_release,
7206 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7207 struct rpc_cred *cred,
7210 struct nfs4_sequence_data *calldata;
7211 struct rpc_message msg = {
7212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7215 struct rpc_task_setup task_setup_data = {
7216 .rpc_client = clp->cl_rpcclient,
7217 .rpc_message = &msg,
7218 .callback_ops = &nfs41_sequence_ops,
7219 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7222 if (!atomic_inc_not_zero(&clp->cl_count))
7223 return ERR_PTR(-EIO);
7224 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7225 if (calldata == NULL) {
7226 nfs_put_client(clp);
7227 return ERR_PTR(-ENOMEM);
7229 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7231 nfs4_set_sequence_privileged(&calldata->args);
7232 msg.rpc_argp = &calldata->args;
7233 msg.rpc_resp = &calldata->res;
7234 calldata->clp = clp;
7235 task_setup_data.callback_data = calldata;
7237 return rpc_run_task(&task_setup_data);
7240 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7242 struct rpc_task *task;
7245 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7247 task = _nfs41_proc_sequence(clp, cred, false);
7249 ret = PTR_ERR(task);
7251 rpc_put_task_async(task);
7252 dprintk("<-- %s status=%d\n", __func__, ret);
7256 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7258 struct rpc_task *task;
7261 task = _nfs41_proc_sequence(clp, cred, true);
7263 ret = PTR_ERR(task);
7266 ret = rpc_wait_for_completion_task(task);
7268 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7270 if (task->tk_status == 0)
7271 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7272 ret = task->tk_status;
7276 dprintk("<-- %s status=%d\n", __func__, ret);
7280 struct nfs4_reclaim_complete_data {
7281 struct nfs_client *clp;
7282 struct nfs41_reclaim_complete_args arg;
7283 struct nfs41_reclaim_complete_res res;
7286 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7288 struct nfs4_reclaim_complete_data *calldata = data;
7290 nfs41_setup_sequence(calldata->clp->cl_session,
7291 &calldata->arg.seq_args,
7292 &calldata->res.seq_res,
7296 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7298 switch(task->tk_status) {
7300 case -NFS4ERR_COMPLETE_ALREADY:
7301 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7303 case -NFS4ERR_DELAY:
7304 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7306 case -NFS4ERR_RETRY_UNCACHED_REP:
7309 nfs4_schedule_lease_recovery(clp);
7314 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7316 struct nfs4_reclaim_complete_data *calldata = data;
7317 struct nfs_client *clp = calldata->clp;
7318 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7320 dprintk("--> %s\n", __func__);
7321 if (!nfs41_sequence_done(task, res))
7324 trace_nfs4_reclaim_complete(clp, task->tk_status);
7325 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7326 rpc_restart_call_prepare(task);
7329 dprintk("<-- %s\n", __func__);
7332 static void nfs4_free_reclaim_complete_data(void *data)
7334 struct nfs4_reclaim_complete_data *calldata = data;
7339 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7340 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7341 .rpc_call_done = nfs4_reclaim_complete_done,
7342 .rpc_release = nfs4_free_reclaim_complete_data,
7346 * Issue a global reclaim complete.
7348 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7349 struct rpc_cred *cred)
7351 struct nfs4_reclaim_complete_data *calldata;
7352 struct rpc_task *task;
7353 struct rpc_message msg = {
7354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7357 struct rpc_task_setup task_setup_data = {
7358 .rpc_client = clp->cl_rpcclient,
7359 .rpc_message = &msg,
7360 .callback_ops = &nfs4_reclaim_complete_call_ops,
7361 .flags = RPC_TASK_ASYNC,
7363 int status = -ENOMEM;
7365 dprintk("--> %s\n", __func__);
7366 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7367 if (calldata == NULL)
7369 calldata->clp = clp;
7370 calldata->arg.one_fs = 0;
7372 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7373 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7374 msg.rpc_argp = &calldata->arg;
7375 msg.rpc_resp = &calldata->res;
7376 task_setup_data.callback_data = calldata;
7377 task = rpc_run_task(&task_setup_data);
7379 status = PTR_ERR(task);
7382 status = nfs4_wait_for_completion_rpc_task(task);
7384 status = task->tk_status;
7388 dprintk("<-- %s status=%d\n", __func__, status);
7393 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7395 struct nfs4_layoutget *lgp = calldata;
7396 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7397 struct nfs4_session *session = nfs4_get_session(server);
7399 dprintk("--> %s\n", __func__);
7400 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7401 * right now covering the LAYOUTGET we are about to send.
7402 * However, that is not so catastrophic, and there seems
7403 * to be no way to prevent it completely.
7405 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7406 &lgp->res.seq_res, task))
7408 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7409 NFS_I(lgp->args.inode)->layout,
7410 lgp->args.ctx->state)) {
7411 rpc_exit(task, NFS4_OK);
7415 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7417 struct nfs4_layoutget *lgp = calldata;
7418 struct inode *inode = lgp->args.inode;
7419 struct nfs_server *server = NFS_SERVER(inode);
7420 struct pnfs_layout_hdr *lo;
7421 struct nfs4_state *state = NULL;
7422 unsigned long timeo, now, giveup;
7424 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7426 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7429 switch (task->tk_status) {
7433 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7434 * (or clients) writing to the same RAID stripe
7436 case -NFS4ERR_LAYOUTTRYLATER:
7438 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7439 * existing layout before getting a new one).
7441 case -NFS4ERR_RECALLCONFLICT:
7442 timeo = rpc_get_timeout(task->tk_client);
7443 giveup = lgp->args.timestamp + timeo;
7445 if (time_after(giveup, now)) {
7446 unsigned long delay;
7449 * - Not less then NFS4_POLL_RETRY_MIN.
7450 * - One last time a jiffie before we give up
7451 * - exponential backoff (time_now minus start_attempt)
7453 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7454 min((giveup - now - 1),
7455 now - lgp->args.timestamp));
7457 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7459 rpc_delay(task, delay);
7460 task->tk_status = 0;
7461 rpc_restart_call_prepare(task);
7462 goto out; /* Do not call nfs4_async_handle_error() */
7465 case -NFS4ERR_EXPIRED:
7466 case -NFS4ERR_BAD_STATEID:
7467 spin_lock(&inode->i_lock);
7468 lo = NFS_I(inode)->layout;
7469 if (!lo || list_empty(&lo->plh_segs)) {
7470 spin_unlock(&inode->i_lock);
7471 /* If the open stateid was bad, then recover it. */
7472 state = lgp->args.ctx->state;
7476 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7477 spin_unlock(&inode->i_lock);
7478 /* Mark the bad layout state as invalid, then
7479 * retry using the open stateid. */
7480 pnfs_free_lseg_list(&head);
7483 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7484 rpc_restart_call_prepare(task);
7486 dprintk("<-- %s\n", __func__);
7489 static size_t max_response_pages(struct nfs_server *server)
7491 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7492 return nfs_page_array_len(0, max_resp_sz);
7495 static void nfs4_free_pages(struct page **pages, size_t size)
7502 for (i = 0; i < size; i++) {
7505 __free_page(pages[i]);
7510 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7512 struct page **pages;
7515 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7517 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7521 for (i = 0; i < size; i++) {
7522 pages[i] = alloc_page(gfp_flags);
7524 dprintk("%s: failed to allocate page\n", __func__);
7525 nfs4_free_pages(pages, size);
7533 static void nfs4_layoutget_release(void *calldata)
7535 struct nfs4_layoutget *lgp = calldata;
7536 struct inode *inode = lgp->args.inode;
7537 struct nfs_server *server = NFS_SERVER(inode);
7538 size_t max_pages = max_response_pages(server);
7540 dprintk("--> %s\n", __func__);
7541 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7542 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7543 put_nfs_open_context(lgp->args.ctx);
7545 dprintk("<-- %s\n", __func__);
7548 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7549 .rpc_call_prepare = nfs4_layoutget_prepare,
7550 .rpc_call_done = nfs4_layoutget_done,
7551 .rpc_release = nfs4_layoutget_release,
7554 struct pnfs_layout_segment *
7555 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7557 struct inode *inode = lgp->args.inode;
7558 struct nfs_server *server = NFS_SERVER(inode);
7559 size_t max_pages = max_response_pages(server);
7560 struct rpc_task *task;
7561 struct rpc_message msg = {
7562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7563 .rpc_argp = &lgp->args,
7564 .rpc_resp = &lgp->res,
7565 .rpc_cred = lgp->cred,
7567 struct rpc_task_setup task_setup_data = {
7568 .rpc_client = server->client,
7569 .rpc_message = &msg,
7570 .callback_ops = &nfs4_layoutget_call_ops,
7571 .callback_data = lgp,
7572 .flags = RPC_TASK_ASYNC,
7574 struct pnfs_layout_segment *lseg = NULL;
7577 dprintk("--> %s\n", __func__);
7579 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7580 if (!lgp->args.layout.pages) {
7581 nfs4_layoutget_release(lgp);
7582 return ERR_PTR(-ENOMEM);
7584 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7585 lgp->args.timestamp = jiffies;
7587 lgp->res.layoutp = &lgp->args.layout;
7588 lgp->res.seq_res.sr_slot = NULL;
7589 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7591 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7592 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7594 task = rpc_run_task(&task_setup_data);
7596 return ERR_CAST(task);
7597 status = nfs4_wait_for_completion_rpc_task(task);
7599 status = task->tk_status;
7600 trace_nfs4_layoutget(lgp->args.ctx,
7604 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7605 if (status == 0 && lgp->res.layoutp->len)
7606 lseg = pnfs_layout_process(lgp);
7608 dprintk("<-- %s status=%d\n", __func__, status);
7610 return ERR_PTR(status);
7615 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7617 struct nfs4_layoutreturn *lrp = calldata;
7619 dprintk("--> %s\n", __func__);
7620 nfs41_setup_sequence(lrp->clp->cl_session,
7621 &lrp->args.seq_args,
7626 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7628 struct nfs4_layoutreturn *lrp = calldata;
7629 struct nfs_server *server;
7631 dprintk("--> %s\n", __func__);
7633 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7636 server = NFS_SERVER(lrp->args.inode);
7637 switch (task->tk_status) {
7639 task->tk_status = 0;
7642 case -NFS4ERR_DELAY:
7643 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7645 rpc_restart_call_prepare(task);
7648 dprintk("<-- %s\n", __func__);
7651 static void nfs4_layoutreturn_release(void *calldata)
7653 struct nfs4_layoutreturn *lrp = calldata;
7654 struct pnfs_layout_hdr *lo = lrp->args.layout;
7656 dprintk("--> %s\n", __func__);
7657 spin_lock(&lo->plh_inode->i_lock);
7658 if (lrp->res.lrs_present)
7659 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7660 lo->plh_block_lgets--;
7661 spin_unlock(&lo->plh_inode->i_lock);
7662 pnfs_put_layout_hdr(lrp->args.layout);
7664 dprintk("<-- %s\n", __func__);
7667 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7668 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7669 .rpc_call_done = nfs4_layoutreturn_done,
7670 .rpc_release = nfs4_layoutreturn_release,
7673 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7675 struct rpc_task *task;
7676 struct rpc_message msg = {
7677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7678 .rpc_argp = &lrp->args,
7679 .rpc_resp = &lrp->res,
7680 .rpc_cred = lrp->cred,
7682 struct rpc_task_setup task_setup_data = {
7683 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7684 .rpc_message = &msg,
7685 .callback_ops = &nfs4_layoutreturn_call_ops,
7686 .callback_data = lrp,
7690 dprintk("--> %s\n", __func__);
7691 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7692 task = rpc_run_task(&task_setup_data);
7694 return PTR_ERR(task);
7695 status = task->tk_status;
7696 trace_nfs4_layoutreturn(lrp->args.inode, status);
7697 dprintk("<-- %s status=%d\n", __func__, status);
7703 * Retrieve the list of Data Server devices from the MDS.
7705 static int _nfs4_getdevicelist(struct nfs_server *server,
7706 const struct nfs_fh *fh,
7707 struct pnfs_devicelist *devlist)
7709 struct nfs4_getdevicelist_args args = {
7711 .layoutclass = server->pnfs_curr_ld->id,
7713 struct nfs4_getdevicelist_res res = {
7716 struct rpc_message msg = {
7717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7723 dprintk("--> %s\n", __func__);
7724 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7726 dprintk("<-- %s status=%d\n", __func__, status);
7730 int nfs4_proc_getdevicelist(struct nfs_server *server,
7731 const struct nfs_fh *fh,
7732 struct pnfs_devicelist *devlist)
7734 struct nfs4_exception exception = { };
7738 err = nfs4_handle_exception(server,
7739 _nfs4_getdevicelist(server, fh, devlist),
7741 } while (exception.retry);
7743 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7744 err, devlist->num_devs);
7748 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7751 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7752 struct pnfs_device *pdev,
7753 struct rpc_cred *cred)
7755 struct nfs4_getdeviceinfo_args args = {
7758 struct nfs4_getdeviceinfo_res res = {
7761 struct rpc_message msg = {
7762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7769 dprintk("--> %s\n", __func__);
7770 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7771 dprintk("<-- %s status=%d\n", __func__, status);
7776 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7777 struct pnfs_device *pdev,
7778 struct rpc_cred *cred)
7780 struct nfs4_exception exception = { };
7784 err = nfs4_handle_exception(server,
7785 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7787 } while (exception.retry);
7790 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7792 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7794 struct nfs4_layoutcommit_data *data = calldata;
7795 struct nfs_server *server = NFS_SERVER(data->args.inode);
7796 struct nfs4_session *session = nfs4_get_session(server);
7798 nfs41_setup_sequence(session,
7799 &data->args.seq_args,
7805 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7807 struct nfs4_layoutcommit_data *data = calldata;
7808 struct nfs_server *server = NFS_SERVER(data->args.inode);
7810 if (!nfs41_sequence_done(task, &data->res.seq_res))
7813 switch (task->tk_status) { /* Just ignore these failures */
7814 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7815 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7816 case -NFS4ERR_BADLAYOUT: /* no layout */
7817 case -NFS4ERR_GRACE: /* loca_recalim always false */
7818 task->tk_status = 0;
7822 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7823 rpc_restart_call_prepare(task);
7829 static void nfs4_layoutcommit_release(void *calldata)
7831 struct nfs4_layoutcommit_data *data = calldata;
7833 pnfs_cleanup_layoutcommit(data);
7834 nfs_post_op_update_inode_force_wcc(data->args.inode,
7836 put_rpccred(data->cred);
7840 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7841 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7842 .rpc_call_done = nfs4_layoutcommit_done,
7843 .rpc_release = nfs4_layoutcommit_release,
7847 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7849 struct rpc_message msg = {
7850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7851 .rpc_argp = &data->args,
7852 .rpc_resp = &data->res,
7853 .rpc_cred = data->cred,
7855 struct rpc_task_setup task_setup_data = {
7856 .task = &data->task,
7857 .rpc_client = NFS_CLIENT(data->args.inode),
7858 .rpc_message = &msg,
7859 .callback_ops = &nfs4_layoutcommit_ops,
7860 .callback_data = data,
7861 .flags = RPC_TASK_ASYNC,
7863 struct rpc_task *task;
7866 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7867 "lbw: %llu inode %lu\n",
7868 data->task.tk_pid, sync,
7869 data->args.lastbytewritten,
7870 data->args.inode->i_ino);
7872 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7873 task = rpc_run_task(&task_setup_data);
7875 return PTR_ERR(task);
7878 status = nfs4_wait_for_completion_rpc_task(task);
7881 status = task->tk_status;
7882 trace_nfs4_layoutcommit(data->args.inode, status);
7884 dprintk("%s: status %d\n", __func__, status);
7890 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7891 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7894 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7895 struct nfs_fsinfo *info,
7896 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7898 struct nfs41_secinfo_no_name_args args = {
7899 .style = SECINFO_STYLE_CURRENT_FH,
7901 struct nfs4_secinfo_res res = {
7904 struct rpc_message msg = {
7905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7909 struct rpc_clnt *clnt = server->client;
7910 struct rpc_cred *cred = NULL;
7913 if (use_integrity) {
7914 clnt = server->nfs_client->cl_rpcclient;
7915 cred = nfs4_get_clid_cred(server->nfs_client);
7916 msg.rpc_cred = cred;
7919 dprintk("--> %s\n", __func__);
7920 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7922 dprintk("<-- %s status=%d\n", __func__, status);
7931 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7932 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7934 struct nfs4_exception exception = { };
7937 /* first try using integrity protection */
7938 err = -NFS4ERR_WRONGSEC;
7940 /* try to use integrity protection with machine cred */
7941 if (_nfs4_is_integrity_protected(server->nfs_client))
7942 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7946 * if unable to use integrity protection, or SECINFO with
7947 * integrity protection returns NFS4ERR_WRONGSEC (which is
7948 * disallowed by spec, but exists in deployed servers) use
7949 * the current filesystem's rpc_client and the user cred.
7951 if (err == -NFS4ERR_WRONGSEC)
7952 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7957 case -NFS4ERR_WRONGSEC:
7961 err = nfs4_handle_exception(server, err, &exception);
7963 } while (exception.retry);
7969 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7970 struct nfs_fsinfo *info)
7974 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
7975 struct nfs4_secinfo_flavors *flavors;
7976 struct nfs4_secinfo4 *secinfo;
7979 page = alloc_page(GFP_KERNEL);
7985 flavors = page_address(page);
7986 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7989 * Fall back on "guess and check" method if
7990 * the server doesn't support SECINFO_NO_NAME
7992 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
7993 err = nfs4_find_root_sec(server, fhandle, info);
7999 for (i = 0; i < flavors->num_flavors; i++) {
8000 secinfo = &flavors->flavors[i];
8002 switch (secinfo->flavor) {
8006 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8007 &secinfo->flavor_info);
8010 flavor = RPC_AUTH_MAXFLAVOR;
8014 if (!nfs_auth_info_match(&server->auth_info, flavor))
8015 flavor = RPC_AUTH_MAXFLAVOR;
8017 if (flavor != RPC_AUTH_MAXFLAVOR) {
8018 err = nfs4_lookup_root_sec(server, fhandle,
8025 if (flavor == RPC_AUTH_MAXFLAVOR)
8036 static int _nfs41_test_stateid(struct nfs_server *server,
8037 nfs4_stateid *stateid,
8038 struct rpc_cred *cred)
8041 struct nfs41_test_stateid_args args = {
8044 struct nfs41_test_stateid_res res;
8045 struct rpc_message msg = {
8046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8051 struct rpc_clnt *rpc_client = server->client;
8053 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8056 dprintk("NFS call test_stateid %p\n", stateid);
8057 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8058 nfs4_set_sequence_privileged(&args.seq_args);
8059 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8060 &args.seq_args, &res.seq_res);
8061 if (status != NFS_OK) {
8062 dprintk("NFS reply test_stateid: failed, %d\n", status);
8065 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8070 * nfs41_test_stateid - perform a TEST_STATEID operation
8072 * @server: server / transport on which to perform the operation
8073 * @stateid: state ID to test
8076 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8077 * Otherwise a negative NFS4ERR value is returned if the operation
8078 * failed or the state ID is not currently valid.
8080 static int nfs41_test_stateid(struct nfs_server *server,
8081 nfs4_stateid *stateid,
8082 struct rpc_cred *cred)
8084 struct nfs4_exception exception = { };
8087 err = _nfs41_test_stateid(server, stateid, cred);
8088 if (err != -NFS4ERR_DELAY)
8090 nfs4_handle_exception(server, err, &exception);
8091 } while (exception.retry);
8095 struct nfs_free_stateid_data {
8096 struct nfs_server *server;
8097 struct nfs41_free_stateid_args args;
8098 struct nfs41_free_stateid_res res;
8101 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8103 struct nfs_free_stateid_data *data = calldata;
8104 nfs41_setup_sequence(nfs4_get_session(data->server),
8105 &data->args.seq_args,
8110 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8112 struct nfs_free_stateid_data *data = calldata;
8114 nfs41_sequence_done(task, &data->res.seq_res);
8116 switch (task->tk_status) {
8117 case -NFS4ERR_DELAY:
8118 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8119 rpc_restart_call_prepare(task);
8123 static void nfs41_free_stateid_release(void *calldata)
8128 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8129 .rpc_call_prepare = nfs41_free_stateid_prepare,
8130 .rpc_call_done = nfs41_free_stateid_done,
8131 .rpc_release = nfs41_free_stateid_release,
8134 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8135 nfs4_stateid *stateid,
8136 struct rpc_cred *cred,
8139 struct rpc_message msg = {
8140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8143 struct rpc_task_setup task_setup = {
8144 .rpc_client = server->client,
8145 .rpc_message = &msg,
8146 .callback_ops = &nfs41_free_stateid_ops,
8147 .flags = RPC_TASK_ASYNC,
8149 struct nfs_free_stateid_data *data;
8151 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8152 &task_setup.rpc_client, &msg);
8154 dprintk("NFS call free_stateid %p\n", stateid);
8155 data = kmalloc(sizeof(*data), GFP_NOFS);
8157 return ERR_PTR(-ENOMEM);
8158 data->server = server;
8159 nfs4_stateid_copy(&data->args.stateid, stateid);
8161 task_setup.callback_data = data;
8163 msg.rpc_argp = &data->args;
8164 msg.rpc_resp = &data->res;
8165 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8167 nfs4_set_sequence_privileged(&data->args.seq_args);
8169 return rpc_run_task(&task_setup);
8173 * nfs41_free_stateid - perform a FREE_STATEID operation
8175 * @server: server / transport on which to perform the operation
8176 * @stateid: state ID to release
8179 * Returns NFS_OK if the server freed "stateid". Otherwise a
8180 * negative NFS4ERR value is returned.
8182 static int nfs41_free_stateid(struct nfs_server *server,
8183 nfs4_stateid *stateid,
8184 struct rpc_cred *cred)
8186 struct rpc_task *task;
8189 task = _nfs41_free_stateid(server, stateid, cred, true);
8191 return PTR_ERR(task);
8192 ret = rpc_wait_for_completion_task(task);
8194 ret = task->tk_status;
8199 static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8201 struct rpc_task *task;
8202 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8204 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8205 nfs4_free_lock_state(server, lsp);
8207 return PTR_ERR(task);
8212 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8213 const nfs4_stateid *s2)
8215 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8218 if (s1->seqid == s2->seqid)
8220 if (s1->seqid == 0 || s2->seqid == 0)
8226 #endif /* CONFIG_NFS_V4_1 */
8228 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8229 const nfs4_stateid *s2)
8231 return nfs4_stateid_match(s1, s2);
8235 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8236 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8237 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8238 .recover_open = nfs4_open_reclaim,
8239 .recover_lock = nfs4_lock_reclaim,
8240 .establish_clid = nfs4_init_clientid,
8241 .detect_trunking = nfs40_discover_server_trunking,
8244 #if defined(CONFIG_NFS_V4_1)
8245 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8246 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8247 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8248 .recover_open = nfs4_open_reclaim,
8249 .recover_lock = nfs4_lock_reclaim,
8250 .establish_clid = nfs41_init_clientid,
8251 .reclaim_complete = nfs41_proc_reclaim_complete,
8252 .detect_trunking = nfs41_discover_server_trunking,
8254 #endif /* CONFIG_NFS_V4_1 */
8256 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8257 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8258 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8259 .recover_open = nfs4_open_expired,
8260 .recover_lock = nfs4_lock_expired,
8261 .establish_clid = nfs4_init_clientid,
8264 #if defined(CONFIG_NFS_V4_1)
8265 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8266 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8267 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8268 .recover_open = nfs41_open_expired,
8269 .recover_lock = nfs41_lock_expired,
8270 .establish_clid = nfs41_init_clientid,
8272 #endif /* CONFIG_NFS_V4_1 */
8274 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8275 .sched_state_renewal = nfs4_proc_async_renew,
8276 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8277 .renew_lease = nfs4_proc_renew,
8280 #if defined(CONFIG_NFS_V4_1)
8281 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8282 .sched_state_renewal = nfs41_proc_async_sequence,
8283 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8284 .renew_lease = nfs4_proc_sequence,
8288 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8289 .get_locations = _nfs40_proc_get_locations,
8290 .fsid_present = _nfs40_proc_fsid_present,
8293 #if defined(CONFIG_NFS_V4_1)
8294 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8295 .get_locations = _nfs41_proc_get_locations,
8296 .fsid_present = _nfs41_proc_fsid_present,
8298 #endif /* CONFIG_NFS_V4_1 */
8300 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8302 .init_caps = NFS_CAP_READDIRPLUS
8303 | NFS_CAP_ATOMIC_OPEN
8304 | NFS_CAP_CHANGE_ATTR
8305 | NFS_CAP_POSIX_LOCK,
8306 .init_client = nfs40_init_client,
8307 .shutdown_client = nfs40_shutdown_client,
8308 .match_stateid = nfs4_match_stateid,
8309 .find_root_sec = nfs4_find_root_sec,
8310 .free_lock_state = nfs4_release_lockowner,
8311 .call_sync_ops = &nfs40_call_sync_ops,
8312 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8313 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8314 .state_renewal_ops = &nfs40_state_renewal_ops,
8315 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8318 #if defined(CONFIG_NFS_V4_1)
8319 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8321 .init_caps = NFS_CAP_READDIRPLUS
8322 | NFS_CAP_ATOMIC_OPEN
8323 | NFS_CAP_CHANGE_ATTR
8324 | NFS_CAP_POSIX_LOCK
8325 | NFS_CAP_STATEID_NFSV41
8326 | NFS_CAP_ATOMIC_OPEN_V1,
8327 .init_client = nfs41_init_client,
8328 .shutdown_client = nfs41_shutdown_client,
8329 .match_stateid = nfs41_match_stateid,
8330 .find_root_sec = nfs41_find_root_sec,
8331 .free_lock_state = nfs41_free_lock_state,
8332 .call_sync_ops = &nfs41_call_sync_ops,
8333 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8334 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8335 .state_renewal_ops = &nfs41_state_renewal_ops,
8336 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8340 #if defined(CONFIG_NFS_V4_2)
8341 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8343 .init_caps = NFS_CAP_READDIRPLUS
8344 | NFS_CAP_ATOMIC_OPEN
8345 | NFS_CAP_CHANGE_ATTR
8346 | NFS_CAP_POSIX_LOCK
8347 | NFS_CAP_STATEID_NFSV41
8348 | NFS_CAP_ATOMIC_OPEN_V1,
8349 .init_client = nfs41_init_client,
8350 .shutdown_client = nfs41_shutdown_client,
8351 .match_stateid = nfs41_match_stateid,
8352 .find_root_sec = nfs41_find_root_sec,
8353 .free_lock_state = nfs41_free_lock_state,
8354 .call_sync_ops = &nfs41_call_sync_ops,
8355 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8356 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8357 .state_renewal_ops = &nfs41_state_renewal_ops,
8361 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8362 [0] = &nfs_v4_0_minor_ops,
8363 #if defined(CONFIG_NFS_V4_1)
8364 [1] = &nfs_v4_1_minor_ops,
8366 #if defined(CONFIG_NFS_V4_2)
8367 [2] = &nfs_v4_2_minor_ops,
8371 static const struct inode_operations nfs4_dir_inode_operations = {
8372 .create = nfs_create,
8373 .lookup = nfs_lookup,
8374 .atomic_open = nfs_atomic_open,
8376 .unlink = nfs_unlink,
8377 .symlink = nfs_symlink,
8381 .rename = nfs_rename,
8382 .permission = nfs_permission,
8383 .getattr = nfs_getattr,
8384 .setattr = nfs_setattr,
8385 .getxattr = generic_getxattr,
8386 .setxattr = generic_setxattr,
8387 .listxattr = generic_listxattr,
8388 .removexattr = generic_removexattr,
8391 static const struct inode_operations nfs4_file_inode_operations = {
8392 .permission = nfs_permission,
8393 .getattr = nfs_getattr,
8394 .setattr = nfs_setattr,
8395 .getxattr = generic_getxattr,
8396 .setxattr = generic_setxattr,
8397 .listxattr = generic_listxattr,
8398 .removexattr = generic_removexattr,
8401 const struct nfs_rpc_ops nfs_v4_clientops = {
8402 .version = 4, /* protocol version */
8403 .dentry_ops = &nfs4_dentry_operations,
8404 .dir_inode_ops = &nfs4_dir_inode_operations,
8405 .file_inode_ops = &nfs4_file_inode_operations,
8406 .file_ops = &nfs4_file_operations,
8407 .getroot = nfs4_proc_get_root,
8408 .submount = nfs4_submount,
8409 .try_mount = nfs4_try_mount,
8410 .getattr = nfs4_proc_getattr,
8411 .setattr = nfs4_proc_setattr,
8412 .lookup = nfs4_proc_lookup,
8413 .access = nfs4_proc_access,
8414 .readlink = nfs4_proc_readlink,
8415 .create = nfs4_proc_create,
8416 .remove = nfs4_proc_remove,
8417 .unlink_setup = nfs4_proc_unlink_setup,
8418 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8419 .unlink_done = nfs4_proc_unlink_done,
8420 .rename = nfs4_proc_rename,
8421 .rename_setup = nfs4_proc_rename_setup,
8422 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8423 .rename_done = nfs4_proc_rename_done,
8424 .link = nfs4_proc_link,
8425 .symlink = nfs4_proc_symlink,
8426 .mkdir = nfs4_proc_mkdir,
8427 .rmdir = nfs4_proc_remove,
8428 .readdir = nfs4_proc_readdir,
8429 .mknod = nfs4_proc_mknod,
8430 .statfs = nfs4_proc_statfs,
8431 .fsinfo = nfs4_proc_fsinfo,
8432 .pathconf = nfs4_proc_pathconf,
8433 .set_capabilities = nfs4_server_capabilities,
8434 .decode_dirent = nfs4_decode_dirent,
8435 .read_setup = nfs4_proc_read_setup,
8436 .read_pageio_init = pnfs_pageio_init_read,
8437 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
8438 .read_done = nfs4_read_done,
8439 .write_setup = nfs4_proc_write_setup,
8440 .write_pageio_init = pnfs_pageio_init_write,
8441 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
8442 .write_done = nfs4_write_done,
8443 .commit_setup = nfs4_proc_commit_setup,
8444 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8445 .commit_done = nfs4_commit_done,
8446 .lock = nfs4_proc_lock,
8447 .clear_acl_cache = nfs4_zap_acl_attr,
8448 .close_context = nfs4_close_context,
8449 .open_context = nfs4_atomic_open,
8450 .have_delegation = nfs4_have_delegation,
8451 .return_delegation = nfs4_inode_return_delegation,
8452 .alloc_client = nfs4_alloc_client,
8453 .init_client = nfs4_init_client,
8454 .free_client = nfs4_free_client,
8455 .create_server = nfs4_create_server,
8456 .clone_server = nfs_clone_server,
8459 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8460 .prefix = XATTR_NAME_NFSV4_ACL,
8461 .list = nfs4_xattr_list_nfs4_acl,
8462 .get = nfs4_xattr_get_nfs4_acl,
8463 .set = nfs4_xattr_set_nfs4_acl,
8466 const struct xattr_handler *nfs4_xattr_handlers[] = {
8467 &nfs4_xattr_nfs4_acl_handler,
8468 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8469 &nfs4_xattr_nfs4_label_handler,