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 *, long *);
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_FILE_OPEN:
164 dprintk("%s could not handle NFSv4 error %d\n",
172 * This is our standard bitmap for GETATTR requests.
174 const u32 nfs4_fattr_bitmap[3] = {
176 | FATTR4_WORD0_CHANGE
179 | FATTR4_WORD0_FILEID,
181 | FATTR4_WORD1_NUMLINKS
183 | FATTR4_WORD1_OWNER_GROUP
184 | FATTR4_WORD1_RAWDEV
185 | FATTR4_WORD1_SPACE_USED
186 | FATTR4_WORD1_TIME_ACCESS
187 | FATTR4_WORD1_TIME_METADATA
188 | FATTR4_WORD1_TIME_MODIFY,
189 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
190 FATTR4_WORD2_SECURITY_LABEL
194 static const u32 nfs4_pnfs_open_bitmap[3] = {
196 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_FILEID,
201 | FATTR4_WORD1_NUMLINKS
203 | FATTR4_WORD1_OWNER_GROUP
204 | FATTR4_WORD1_RAWDEV
205 | FATTR4_WORD1_SPACE_USED
206 | FATTR4_WORD1_TIME_ACCESS
207 | FATTR4_WORD1_TIME_METADATA
208 | FATTR4_WORD1_TIME_MODIFY,
209 FATTR4_WORD2_MDSTHRESHOLD
212 static const u32 nfs4_open_noattr_bitmap[3] = {
214 | FATTR4_WORD0_CHANGE
215 | FATTR4_WORD0_FILEID,
218 const u32 nfs4_statfs_bitmap[3] = {
219 FATTR4_WORD0_FILES_AVAIL
220 | FATTR4_WORD0_FILES_FREE
221 | FATTR4_WORD0_FILES_TOTAL,
222 FATTR4_WORD1_SPACE_AVAIL
223 | FATTR4_WORD1_SPACE_FREE
224 | FATTR4_WORD1_SPACE_TOTAL
227 const u32 nfs4_pathconf_bitmap[3] = {
229 | FATTR4_WORD0_MAXNAME,
233 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
234 | FATTR4_WORD0_MAXREAD
235 | FATTR4_WORD0_MAXWRITE
236 | FATTR4_WORD0_LEASE_TIME,
237 FATTR4_WORD1_TIME_DELTA
238 | FATTR4_WORD1_FS_LAYOUT_TYPES,
239 FATTR4_WORD2_LAYOUT_BLKSIZE
242 const u32 nfs4_fs_locations_bitmap[3] = {
244 | FATTR4_WORD0_CHANGE
247 | FATTR4_WORD0_FILEID
248 | FATTR4_WORD0_FS_LOCATIONS,
250 | FATTR4_WORD1_NUMLINKS
252 | FATTR4_WORD1_OWNER_GROUP
253 | FATTR4_WORD1_RAWDEV
254 | FATTR4_WORD1_SPACE_USED
255 | FATTR4_WORD1_TIME_ACCESS
256 | FATTR4_WORD1_TIME_METADATA
257 | FATTR4_WORD1_TIME_MODIFY
258 | FATTR4_WORD1_MOUNTED_ON_FILEID,
261 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
262 struct nfs4_readdir_arg *readdir)
267 readdir->cookie = cookie;
268 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
273 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
278 * NFSv4 servers do not return entries for '.' and '..'
279 * Therefore, we fake these entries here. We let '.'
280 * have cookie 0 and '..' have cookie 1. Note that
281 * when talking to the server, we always send cookie 0
284 start = p = kmap_atomic(*readdir->pages);
287 *p++ = xdr_one; /* next */
288 *p++ = xdr_zero; /* cookie, first word */
289 *p++ = xdr_one; /* cookie, second word */
290 *p++ = xdr_one; /* entry len */
291 memcpy(p, ".\0\0\0", 4); /* entry */
293 *p++ = xdr_one; /* bitmap length */
294 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
295 *p++ = htonl(8); /* attribute buffer length */
296 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
299 *p++ = xdr_one; /* next */
300 *p++ = xdr_zero; /* cookie, first word */
301 *p++ = xdr_two; /* cookie, second word */
302 *p++ = xdr_two; /* entry len */
303 memcpy(p, "..\0\0", 4); /* entry */
305 *p++ = xdr_one; /* bitmap length */
306 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
307 *p++ = htonl(8); /* attribute buffer length */
308 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
310 readdir->pgbase = (char *)p - (char *)start;
311 readdir->count -= readdir->pgbase;
312 kunmap_atomic(start);
315 static long nfs4_update_delay(long *timeout)
319 return NFS4_POLL_RETRY_MAX;
321 *timeout = NFS4_POLL_RETRY_MIN;
322 if (*timeout > NFS4_POLL_RETRY_MAX)
323 *timeout = NFS4_POLL_RETRY_MAX;
329 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
335 freezable_schedule_timeout_killable_unsafe(
336 nfs4_update_delay(timeout));
337 if (fatal_signal_pending(current))
342 /* This is the error handling routine for processes that are allowed
345 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
347 struct nfs_client *clp = server->nfs_client;
348 struct nfs4_state *state = exception->state;
349 struct inode *inode = exception->inode;
352 exception->retry = 0;
356 case -NFS4ERR_OPENMODE:
357 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
358 nfs4_inode_return_delegation(inode);
359 exception->retry = 1;
364 ret = nfs4_schedule_stateid_recovery(server, state);
367 goto wait_on_recovery;
368 case -NFS4ERR_DELEG_REVOKED:
369 case -NFS4ERR_ADMIN_REVOKED:
370 case -NFS4ERR_BAD_STATEID:
373 ret = nfs4_schedule_stateid_recovery(server, state);
376 goto wait_on_recovery;
377 case -NFS4ERR_EXPIRED:
379 ret = nfs4_schedule_stateid_recovery(server, state);
383 case -NFS4ERR_STALE_STATEID:
384 case -NFS4ERR_STALE_CLIENTID:
385 nfs4_schedule_lease_recovery(clp);
386 goto wait_on_recovery;
388 ret = nfs4_schedule_migration_recovery(server);
391 goto wait_on_recovery;
392 case -NFS4ERR_LEASE_MOVED:
393 nfs4_schedule_lease_moved_recovery(clp);
394 goto wait_on_recovery;
395 #if defined(CONFIG_NFS_V4_1)
396 case -NFS4ERR_BADSESSION:
397 case -NFS4ERR_BADSLOT:
398 case -NFS4ERR_BAD_HIGH_SLOT:
399 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
400 case -NFS4ERR_DEADSESSION:
401 case -NFS4ERR_SEQ_FALSE_RETRY:
402 case -NFS4ERR_SEQ_MISORDERED:
403 dprintk("%s ERROR: %d Reset session\n", __func__,
405 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
406 goto wait_on_recovery;
407 #endif /* defined(CONFIG_NFS_V4_1) */
408 case -NFS4ERR_FILE_OPEN:
409 if (exception->timeout > HZ) {
410 /* We have retried a decent amount, time to
418 ret = nfs4_delay(server->client, &exception->timeout);
421 case -NFS4ERR_RETRY_UNCACHED_REP:
422 case -NFS4ERR_OLD_STATEID:
423 exception->retry = 1;
425 case -NFS4ERR_BADOWNER:
426 /* The following works around a Linux server bug! */
427 case -NFS4ERR_BADNAME:
428 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
429 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
430 exception->retry = 1;
431 printk(KERN_WARNING "NFS: v4 server %s "
432 "does not accept raw "
434 "Reenabling the idmapper.\n",
435 server->nfs_client->cl_hostname);
438 /* We failed to handle the error */
439 return nfs4_map_errors(ret);
441 ret = nfs4_wait_clnt_recover(clp);
442 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
445 exception->retry = 1;
450 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
451 * or 'false' otherwise.
453 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
455 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
457 if (flavor == RPC_AUTH_GSS_KRB5I ||
458 flavor == RPC_AUTH_GSS_KRB5P)
464 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
466 spin_lock(&clp->cl_lock);
467 if (time_before(clp->cl_last_renewal,timestamp))
468 clp->cl_last_renewal = timestamp;
469 spin_unlock(&clp->cl_lock);
472 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
474 do_renew_lease(server->nfs_client, timestamp);
477 struct nfs4_call_sync_data {
478 const struct nfs_server *seq_server;
479 struct nfs4_sequence_args *seq_args;
480 struct nfs4_sequence_res *seq_res;
483 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
484 struct nfs4_sequence_res *res, int cache_reply)
486 args->sa_slot = NULL;
487 args->sa_cache_this = cache_reply;
488 args->sa_privileged = 0;
493 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
495 args->sa_privileged = 1;
498 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
499 struct nfs4_sequence_args *args,
500 struct nfs4_sequence_res *res,
501 struct rpc_task *task)
503 struct nfs4_slot *slot;
505 /* slot already allocated? */
506 if (res->sr_slot != NULL)
509 spin_lock(&tbl->slot_tbl_lock);
510 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
513 slot = nfs4_alloc_slot(tbl);
515 if (slot == ERR_PTR(-ENOMEM))
516 task->tk_timeout = HZ >> 2;
519 spin_unlock(&tbl->slot_tbl_lock);
521 args->sa_slot = slot;
525 rpc_call_start(task);
529 if (args->sa_privileged)
530 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
531 NULL, RPC_PRIORITY_PRIVILEGED);
533 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
534 spin_unlock(&tbl->slot_tbl_lock);
537 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
539 static int nfs40_sequence_done(struct rpc_task *task,
540 struct nfs4_sequence_res *res)
542 struct nfs4_slot *slot = res->sr_slot;
543 struct nfs4_slot_table *tbl;
549 spin_lock(&tbl->slot_tbl_lock);
550 if (!nfs41_wake_and_assign_slot(tbl, slot))
551 nfs4_free_slot(tbl, slot);
552 spin_unlock(&tbl->slot_tbl_lock);
559 #if defined(CONFIG_NFS_V4_1)
561 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
563 struct nfs4_session *session;
564 struct nfs4_slot_table *tbl;
565 struct nfs4_slot *slot = res->sr_slot;
566 bool send_new_highest_used_slotid = false;
569 session = tbl->session;
571 spin_lock(&tbl->slot_tbl_lock);
572 /* Be nice to the server: try to ensure that the last transmitted
573 * value for highest_user_slotid <= target_highest_slotid
575 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
576 send_new_highest_used_slotid = true;
578 if (nfs41_wake_and_assign_slot(tbl, slot)) {
579 send_new_highest_used_slotid = false;
582 nfs4_free_slot(tbl, slot);
584 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
585 send_new_highest_used_slotid = false;
587 spin_unlock(&tbl->slot_tbl_lock);
589 if (send_new_highest_used_slotid)
590 nfs41_server_notify_highest_slotid_update(session->clp);
593 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
595 struct nfs4_session *session;
596 struct nfs4_slot *slot = res->sr_slot;
597 struct nfs_client *clp;
598 bool interrupted = false;
603 /* don't increment the sequence number if the task wasn't sent */
604 if (!RPC_WAS_SENT(task))
607 session = slot->table->session;
609 if (slot->interrupted) {
610 slot->interrupted = 0;
614 trace_nfs4_sequence_done(session, res);
615 /* Check the SEQUENCE operation status */
616 switch (res->sr_status) {
618 /* Update the slot's sequence and clientid lease timer */
621 do_renew_lease(clp, res->sr_timestamp);
622 /* Check sequence flags */
623 if (res->sr_status_flags != 0)
624 nfs4_schedule_lease_recovery(clp);
625 nfs41_update_target_slotid(slot->table, slot, res);
629 * sr_status remains 1 if an RPC level error occurred.
630 * The server may or may not have processed the sequence
632 * Mark the slot as having hosted an interrupted RPC call.
634 slot->interrupted = 1;
637 /* The server detected a resend of the RPC call and
638 * returned NFS4ERR_DELAY as per Section 2.10.6.2
641 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
646 case -NFS4ERR_BADSLOT:
648 * The slot id we used was probably retired. Try again
649 * using a different slot id.
652 case -NFS4ERR_SEQ_MISORDERED:
654 * Was the last operation on this sequence interrupted?
655 * If so, retry after bumping the sequence number.
662 * Could this slot have been previously retired?
663 * If so, then the server may be expecting seq_nr = 1!
665 if (slot->seq_nr != 1) {
670 case -NFS4ERR_SEQ_FALSE_RETRY:
674 /* Just update the slot sequence no. */
678 /* The session may be reset by one of the error handlers. */
679 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
680 nfs41_sequence_free_slot(res);
684 if (rpc_restart_call_prepare(task)) {
690 if (!rpc_restart_call(task))
692 rpc_delay(task, NFS4_POLL_RETRY_MAX);
695 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
697 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
699 if (res->sr_slot == NULL)
701 if (!res->sr_slot->table->session)
702 return nfs40_sequence_done(task, res);
703 return nfs41_sequence_done(task, res);
705 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
707 int nfs41_setup_sequence(struct nfs4_session *session,
708 struct nfs4_sequence_args *args,
709 struct nfs4_sequence_res *res,
710 struct rpc_task *task)
712 struct nfs4_slot *slot;
713 struct nfs4_slot_table *tbl;
715 dprintk("--> %s\n", __func__);
716 /* slot already allocated? */
717 if (res->sr_slot != NULL)
720 tbl = &session->fc_slot_table;
722 task->tk_timeout = 0;
724 spin_lock(&tbl->slot_tbl_lock);
725 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
726 !args->sa_privileged) {
727 /* The state manager will wait until the slot table is empty */
728 dprintk("%s session is draining\n", __func__);
732 slot = nfs4_alloc_slot(tbl);
734 /* If out of memory, try again in 1/4 second */
735 if (slot == ERR_PTR(-ENOMEM))
736 task->tk_timeout = HZ >> 2;
737 dprintk("<-- %s: no free slots\n", __func__);
740 spin_unlock(&tbl->slot_tbl_lock);
742 args->sa_slot = slot;
744 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
745 slot->slot_nr, slot->seq_nr);
748 res->sr_timestamp = jiffies;
749 res->sr_status_flags = 0;
751 * sr_status is only set in decode_sequence, and so will remain
752 * set to 1 if an rpc level failure occurs.
755 trace_nfs4_setup_sequence(session, args);
757 rpc_call_start(task);
760 /* Privileged tasks are queued with top priority */
761 if (args->sa_privileged)
762 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
763 NULL, RPC_PRIORITY_PRIVILEGED);
765 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
766 spin_unlock(&tbl->slot_tbl_lock);
769 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
771 static int nfs4_setup_sequence(const struct nfs_server *server,
772 struct nfs4_sequence_args *args,
773 struct nfs4_sequence_res *res,
774 struct rpc_task *task)
776 struct nfs4_session *session = nfs4_get_session(server);
780 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
783 dprintk("--> %s clp %p session %p sr_slot %u\n",
784 __func__, session->clp, session, res->sr_slot ?
785 res->sr_slot->slot_nr : NFS4_NO_SLOT);
787 ret = nfs41_setup_sequence(session, args, res, task);
789 dprintk("<-- %s status=%d\n", __func__, ret);
793 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
795 struct nfs4_call_sync_data *data = calldata;
796 struct nfs4_session *session = nfs4_get_session(data->seq_server);
798 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
800 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
803 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
805 struct nfs4_call_sync_data *data = calldata;
807 nfs41_sequence_done(task, data->seq_res);
810 static const struct rpc_call_ops nfs41_call_sync_ops = {
811 .rpc_call_prepare = nfs41_call_sync_prepare,
812 .rpc_call_done = nfs41_call_sync_done,
815 #else /* !CONFIG_NFS_V4_1 */
817 static int nfs4_setup_sequence(const struct nfs_server *server,
818 struct nfs4_sequence_args *args,
819 struct nfs4_sequence_res *res,
820 struct rpc_task *task)
822 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
826 int nfs4_sequence_done(struct rpc_task *task,
827 struct nfs4_sequence_res *res)
829 return nfs40_sequence_done(task, res);
831 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
833 #endif /* !CONFIG_NFS_V4_1 */
835 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
837 struct nfs4_call_sync_data *data = calldata;
838 nfs4_setup_sequence(data->seq_server,
839 data->seq_args, data->seq_res, task);
842 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
844 struct nfs4_call_sync_data *data = calldata;
845 nfs4_sequence_done(task, data->seq_res);
848 static const struct rpc_call_ops nfs40_call_sync_ops = {
849 .rpc_call_prepare = nfs40_call_sync_prepare,
850 .rpc_call_done = nfs40_call_sync_done,
853 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
854 struct nfs_server *server,
855 struct rpc_message *msg,
856 struct nfs4_sequence_args *args,
857 struct nfs4_sequence_res *res)
860 struct rpc_task *task;
861 struct nfs_client *clp = server->nfs_client;
862 struct nfs4_call_sync_data data = {
863 .seq_server = server,
867 struct rpc_task_setup task_setup = {
870 .callback_ops = clp->cl_mvops->call_sync_ops,
871 .callback_data = &data
874 task = rpc_run_task(&task_setup);
878 ret = task->tk_status;
884 int nfs4_call_sync(struct rpc_clnt *clnt,
885 struct nfs_server *server,
886 struct rpc_message *msg,
887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
891 nfs4_init_sequence(args, res, cache_reply);
892 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
895 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
897 struct nfs_inode *nfsi = NFS_I(dir);
899 spin_lock(&dir->i_lock);
900 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
901 if (!cinfo->atomic || cinfo->before != dir->i_version)
902 nfs_force_lookup_revalidate(dir);
903 dir->i_version = cinfo->after;
904 nfs_fscache_invalidate(dir);
905 spin_unlock(&dir->i_lock);
908 struct nfs4_opendata {
910 struct nfs_openargs o_arg;
911 struct nfs_openres o_res;
912 struct nfs_open_confirmargs c_arg;
913 struct nfs_open_confirmres c_res;
914 struct nfs4_string owner_name;
915 struct nfs4_string group_name;
916 struct nfs_fattr f_attr;
917 struct nfs4_label *f_label;
919 struct dentry *dentry;
920 struct nfs4_state_owner *owner;
921 struct nfs4_state *state;
923 unsigned long timestamp;
924 unsigned int rpc_done : 1;
925 unsigned int file_created : 1;
926 unsigned int is_recover : 1;
931 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
932 int err, struct nfs4_exception *exception)
936 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
938 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
939 exception->retry = 1;
943 static enum open_claim_type4
944 nfs4_map_atomic_open_claim(struct nfs_server *server,
945 enum open_claim_type4 claim)
947 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
952 case NFS4_OPEN_CLAIM_FH:
953 return NFS4_OPEN_CLAIM_NULL;
954 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
955 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
956 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
957 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
961 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
963 p->o_res.f_attr = &p->f_attr;
964 p->o_res.f_label = p->f_label;
965 p->o_res.seqid = p->o_arg.seqid;
966 p->c_res.seqid = p->c_arg.seqid;
967 p->o_res.server = p->o_arg.server;
968 p->o_res.access_request = p->o_arg.access;
969 nfs_fattr_init(&p->f_attr);
970 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
973 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
974 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
975 const struct iattr *attrs,
976 struct nfs4_label *label,
977 enum open_claim_type4 claim,
980 struct dentry *parent = dget_parent(dentry);
981 struct inode *dir = parent->d_inode;
982 struct nfs_server *server = NFS_SERVER(dir);
983 struct nfs4_opendata *p;
985 p = kzalloc(sizeof(*p), gfp_mask);
989 p->f_label = nfs4_label_alloc(server, gfp_mask);
990 if (IS_ERR(p->f_label))
993 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
994 if (p->o_arg.seqid == NULL)
996 nfs_sb_active(dentry->d_sb);
997 p->dentry = dget(dentry);
1000 atomic_inc(&sp->so_count);
1001 p->o_arg.open_flags = flags;
1002 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1003 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1004 * will return permission denied for all bits until close */
1005 if (!(flags & O_EXCL)) {
1006 /* ask server to check for all possible rights as results
1008 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1009 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1011 p->o_arg.clientid = server->nfs_client->cl_clientid;
1012 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1013 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1014 p->o_arg.name = &dentry->d_name;
1015 p->o_arg.server = server;
1016 p->o_arg.bitmask = nfs4_bitmask(server, label);
1017 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1018 p->o_arg.label = label;
1019 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1020 switch (p->o_arg.claim) {
1021 case NFS4_OPEN_CLAIM_NULL:
1022 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1023 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1024 p->o_arg.fh = NFS_FH(dir);
1026 case NFS4_OPEN_CLAIM_PREVIOUS:
1027 case NFS4_OPEN_CLAIM_FH:
1028 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1029 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1030 p->o_arg.fh = NFS_FH(dentry->d_inode);
1032 if (attrs != NULL && attrs->ia_valid != 0) {
1035 p->o_arg.u.attrs = &p->attrs;
1036 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1039 verf[1] = current->pid;
1040 memcpy(p->o_arg.u.verifier.data, verf,
1041 sizeof(p->o_arg.u.verifier.data));
1043 p->c_arg.fh = &p->o_res.fh;
1044 p->c_arg.stateid = &p->o_res.stateid;
1045 p->c_arg.seqid = p->o_arg.seqid;
1046 nfs4_init_opendata_res(p);
1047 kref_init(&p->kref);
1051 nfs4_label_free(p->f_label);
1059 static void nfs4_opendata_free(struct kref *kref)
1061 struct nfs4_opendata *p = container_of(kref,
1062 struct nfs4_opendata, kref);
1063 struct super_block *sb = p->dentry->d_sb;
1065 nfs_free_seqid(p->o_arg.seqid);
1066 if (p->state != NULL)
1067 nfs4_put_open_state(p->state);
1068 nfs4_put_state_owner(p->owner);
1070 nfs4_label_free(p->f_label);
1074 nfs_sb_deactive(sb);
1075 nfs_fattr_free_names(&p->f_attr);
1076 kfree(p->f_attr.mdsthreshold);
1080 static void nfs4_opendata_put(struct nfs4_opendata *p)
1083 kref_put(&p->kref, nfs4_opendata_free);
1086 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1090 ret = rpc_wait_for_completion_task(task);
1094 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1098 if (open_mode & (O_EXCL|O_TRUNC))
1100 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1102 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1103 && state->n_rdonly != 0;
1106 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1107 && state->n_wronly != 0;
1109 case FMODE_READ|FMODE_WRITE:
1110 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1111 && state->n_rdwr != 0;
1117 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1119 if (delegation == NULL)
1121 if ((delegation->type & fmode) != fmode)
1123 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1125 nfs_mark_delegation_referenced(delegation);
1129 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1138 case FMODE_READ|FMODE_WRITE:
1141 nfs4_state_set_mode_locked(state, state->state | fmode);
1144 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1146 struct nfs_client *clp = state->owner->so_server->nfs_client;
1147 bool need_recover = false;
1149 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1150 need_recover = true;
1151 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1152 need_recover = true;
1153 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1154 need_recover = true;
1156 nfs4_state_mark_reclaim_nograce(clp, state);
1159 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1160 nfs4_stateid *stateid)
1162 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1164 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1165 nfs_test_and_clear_all_open_stateid(state);
1168 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1173 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1174 nfs4_stateid *stateid, fmode_t fmode)
1176 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1177 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1179 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1182 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1185 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1186 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1187 clear_bit(NFS_OPEN_STATE, &state->flags);
1189 if (stateid == NULL)
1191 if (!nfs_need_update_open_stateid(state, stateid))
1193 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1194 nfs4_stateid_copy(&state->stateid, stateid);
1195 nfs4_stateid_copy(&state->open_stateid, stateid);
1198 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1200 write_seqlock(&state->seqlock);
1201 nfs_clear_open_stateid_locked(state, stateid, fmode);
1202 write_sequnlock(&state->seqlock);
1203 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1204 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1207 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1211 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1214 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1216 case FMODE_READ|FMODE_WRITE:
1217 set_bit(NFS_O_RDWR_STATE, &state->flags);
1219 if (!nfs_need_update_open_stateid(state, stateid))
1221 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1222 nfs4_stateid_copy(&state->stateid, stateid);
1223 nfs4_stateid_copy(&state->open_stateid, stateid);
1226 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1229 * Protect the call to nfs4_state_set_mode_locked and
1230 * serialise the stateid update
1232 write_seqlock(&state->seqlock);
1233 if (deleg_stateid != NULL) {
1234 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1235 set_bit(NFS_DELEGATED_STATE, &state->flags);
1237 if (open_stateid != NULL)
1238 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1239 write_sequnlock(&state->seqlock);
1240 spin_lock(&state->owner->so_lock);
1241 update_open_stateflags(state, fmode);
1242 spin_unlock(&state->owner->so_lock);
1245 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1247 struct nfs_inode *nfsi = NFS_I(state->inode);
1248 struct nfs_delegation *deleg_cur;
1251 fmode &= (FMODE_READ|FMODE_WRITE);
1254 deleg_cur = rcu_dereference(nfsi->delegation);
1255 if (deleg_cur == NULL)
1258 spin_lock(&deleg_cur->lock);
1259 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1260 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1261 (deleg_cur->type & fmode) != fmode)
1262 goto no_delegation_unlock;
1264 if (delegation == NULL)
1265 delegation = &deleg_cur->stateid;
1266 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1267 goto no_delegation_unlock;
1269 nfs_mark_delegation_referenced(deleg_cur);
1270 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1272 no_delegation_unlock:
1273 spin_unlock(&deleg_cur->lock);
1277 if (!ret && open_stateid != NULL) {
1278 __update_open_stateid(state, open_stateid, NULL, fmode);
1281 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1282 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1288 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1290 struct nfs_delegation *delegation;
1293 delegation = rcu_dereference(NFS_I(inode)->delegation);
1294 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1299 nfs4_inode_return_delegation(inode);
1302 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1304 struct nfs4_state *state = opendata->state;
1305 struct nfs_inode *nfsi = NFS_I(state->inode);
1306 struct nfs_delegation *delegation;
1307 int open_mode = opendata->o_arg.open_flags;
1308 fmode_t fmode = opendata->o_arg.fmode;
1309 nfs4_stateid stateid;
1313 spin_lock(&state->owner->so_lock);
1314 if (can_open_cached(state, fmode, open_mode)) {
1315 update_open_stateflags(state, fmode);
1316 spin_unlock(&state->owner->so_lock);
1317 goto out_return_state;
1319 spin_unlock(&state->owner->so_lock);
1321 delegation = rcu_dereference(nfsi->delegation);
1322 if (!can_open_delegated(delegation, fmode)) {
1326 /* Save the delegation */
1327 nfs4_stateid_copy(&stateid, &delegation->stateid);
1329 nfs_release_seqid(opendata->o_arg.seqid);
1330 if (!opendata->is_recover) {
1331 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1337 /* Try to update the stateid using the delegation */
1338 if (update_open_stateid(state, NULL, &stateid, fmode))
1339 goto out_return_state;
1342 return ERR_PTR(ret);
1344 atomic_inc(&state->count);
1349 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1351 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1352 struct nfs_delegation *delegation;
1353 int delegation_flags = 0;
1356 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1358 delegation_flags = delegation->flags;
1360 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1361 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1362 "returning a delegation for "
1363 "OPEN(CLAIM_DELEGATE_CUR)\n",
1365 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1366 nfs_inode_set_delegation(state->inode,
1367 data->owner->so_cred,
1370 nfs_inode_reclaim_delegation(state->inode,
1371 data->owner->so_cred,
1376 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1377 * and update the nfs4_state.
1379 static struct nfs4_state *
1380 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1382 struct inode *inode = data->state->inode;
1383 struct nfs4_state *state = data->state;
1386 if (!data->rpc_done) {
1387 if (data->rpc_status) {
1388 ret = data->rpc_status;
1391 /* cached opens have already been processed */
1395 ret = nfs_refresh_inode(inode, &data->f_attr);
1399 if (data->o_res.delegation_type != 0)
1400 nfs4_opendata_check_deleg(data, state);
1402 update_open_stateid(state, &data->o_res.stateid, NULL,
1404 atomic_inc(&state->count);
1408 return ERR_PTR(ret);
1412 static struct nfs4_state *
1413 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1415 struct inode *inode;
1416 struct nfs4_state *state = NULL;
1419 if (!data->rpc_done) {
1420 state = nfs4_try_open_cached(data);
1425 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1427 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1428 ret = PTR_ERR(inode);
1432 state = nfs4_get_open_state(inode, data->owner);
1435 if (data->o_res.delegation_type != 0)
1436 nfs4_opendata_check_deleg(data, state);
1437 update_open_stateid(state, &data->o_res.stateid, NULL,
1441 nfs_release_seqid(data->o_arg.seqid);
1446 return ERR_PTR(ret);
1449 static struct nfs4_state *
1450 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1452 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1453 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1454 return _nfs4_opendata_to_nfs4_state(data);
1457 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1459 struct nfs_inode *nfsi = NFS_I(state->inode);
1460 struct nfs_open_context *ctx;
1462 spin_lock(&state->inode->i_lock);
1463 list_for_each_entry(ctx, &nfsi->open_files, list) {
1464 if (ctx->state != state)
1466 get_nfs_open_context(ctx);
1467 spin_unlock(&state->inode->i_lock);
1470 spin_unlock(&state->inode->i_lock);
1471 return ERR_PTR(-ENOENT);
1474 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1475 struct nfs4_state *state, enum open_claim_type4 claim)
1477 struct nfs4_opendata *opendata;
1479 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1480 NULL, NULL, claim, GFP_NOFS);
1481 if (opendata == NULL)
1482 return ERR_PTR(-ENOMEM);
1483 opendata->state = state;
1484 atomic_inc(&state->count);
1488 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1490 struct nfs4_state *newstate;
1493 opendata->o_arg.open_flags = 0;
1494 opendata->o_arg.fmode = fmode;
1495 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1496 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1497 nfs4_init_opendata_res(opendata);
1498 ret = _nfs4_recover_proc_open(opendata);
1501 newstate = nfs4_opendata_to_nfs4_state(opendata);
1502 if (IS_ERR(newstate))
1503 return PTR_ERR(newstate);
1504 nfs4_close_state(newstate, fmode);
1509 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1511 struct nfs4_state *newstate;
1514 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1515 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1516 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1517 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1518 /* memory barrier prior to reading state->n_* */
1519 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1520 clear_bit(NFS_OPEN_STATE, &state->flags);
1522 if (state->n_rdwr != 0) {
1523 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1526 if (newstate != state)
1529 if (state->n_wronly != 0) {
1530 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1533 if (newstate != state)
1536 if (state->n_rdonly != 0) {
1537 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1540 if (newstate != state)
1544 * We may have performed cached opens for all three recoveries.
1545 * Check if we need to update the current stateid.
1547 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1548 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1549 write_seqlock(&state->seqlock);
1550 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1551 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1552 write_sequnlock(&state->seqlock);
1559 * reclaim state on the server after a reboot.
1561 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1563 struct nfs_delegation *delegation;
1564 struct nfs4_opendata *opendata;
1565 fmode_t delegation_type = 0;
1568 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1569 NFS4_OPEN_CLAIM_PREVIOUS);
1570 if (IS_ERR(opendata))
1571 return PTR_ERR(opendata);
1573 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1574 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1575 delegation_type = delegation->type;
1577 opendata->o_arg.u.delegation_type = delegation_type;
1578 status = nfs4_open_recover(opendata, state);
1579 nfs4_opendata_put(opendata);
1583 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1585 struct nfs_server *server = NFS_SERVER(state->inode);
1586 struct nfs4_exception exception = { };
1589 err = _nfs4_do_open_reclaim(ctx, state);
1590 trace_nfs4_open_reclaim(ctx, 0, err);
1591 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1593 if (err != -NFS4ERR_DELAY)
1595 nfs4_handle_exception(server, err, &exception);
1596 } while (exception.retry);
1600 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1602 struct nfs_open_context *ctx;
1605 ctx = nfs4_state_find_open_context(state);
1608 ret = nfs4_do_open_reclaim(ctx, state);
1609 put_nfs_open_context(ctx);
1613 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1617 printk(KERN_ERR "NFS: %s: unhandled error "
1618 "%d.\n", __func__, err);
1623 case -NFS4ERR_BADSESSION:
1624 case -NFS4ERR_BADSLOT:
1625 case -NFS4ERR_BAD_HIGH_SLOT:
1626 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1627 case -NFS4ERR_DEADSESSION:
1628 set_bit(NFS_DELEGATED_STATE, &state->flags);
1629 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1631 case -NFS4ERR_STALE_CLIENTID:
1632 case -NFS4ERR_STALE_STATEID:
1633 set_bit(NFS_DELEGATED_STATE, &state->flags);
1634 case -NFS4ERR_EXPIRED:
1635 /* Don't recall a delegation if it was lost */
1636 nfs4_schedule_lease_recovery(server->nfs_client);
1638 case -NFS4ERR_MOVED:
1639 nfs4_schedule_migration_recovery(server);
1641 case -NFS4ERR_LEASE_MOVED:
1642 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1644 case -NFS4ERR_DELEG_REVOKED:
1645 case -NFS4ERR_ADMIN_REVOKED:
1646 case -NFS4ERR_BAD_STATEID:
1647 case -NFS4ERR_OPENMODE:
1648 nfs_inode_find_state_and_recover(state->inode,
1650 nfs4_schedule_stateid_recovery(server, state);
1652 case -NFS4ERR_DELAY:
1653 case -NFS4ERR_GRACE:
1654 set_bit(NFS_DELEGATED_STATE, &state->flags);
1658 case -NFS4ERR_DENIED:
1659 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1665 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1667 struct nfs_server *server = NFS_SERVER(state->inode);
1668 struct nfs4_opendata *opendata;
1671 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1672 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1673 if (IS_ERR(opendata))
1674 return PTR_ERR(opendata);
1675 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1676 err = nfs4_open_recover(opendata, state);
1677 nfs4_opendata_put(opendata);
1678 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1681 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1683 struct nfs4_opendata *data = calldata;
1685 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1686 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1689 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1691 struct nfs4_opendata *data = calldata;
1693 nfs40_sequence_done(task, &data->c_res.seq_res);
1695 data->rpc_status = task->tk_status;
1696 if (data->rpc_status == 0) {
1697 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1698 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1699 renew_lease(data->o_res.server, data->timestamp);
1704 static void nfs4_open_confirm_release(void *calldata)
1706 struct nfs4_opendata *data = calldata;
1707 struct nfs4_state *state = NULL;
1709 /* If this request hasn't been cancelled, do nothing */
1710 if (data->cancelled == 0)
1712 /* In case of error, no cleanup! */
1713 if (!data->rpc_done)
1715 state = nfs4_opendata_to_nfs4_state(data);
1717 nfs4_close_state(state, data->o_arg.fmode);
1719 nfs4_opendata_put(data);
1722 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1723 .rpc_call_prepare = nfs4_open_confirm_prepare,
1724 .rpc_call_done = nfs4_open_confirm_done,
1725 .rpc_release = nfs4_open_confirm_release,
1729 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1731 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1733 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1734 struct rpc_task *task;
1735 struct rpc_message msg = {
1736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1737 .rpc_argp = &data->c_arg,
1738 .rpc_resp = &data->c_res,
1739 .rpc_cred = data->owner->so_cred,
1741 struct rpc_task_setup task_setup_data = {
1742 .rpc_client = server->client,
1743 .rpc_message = &msg,
1744 .callback_ops = &nfs4_open_confirm_ops,
1745 .callback_data = data,
1746 .workqueue = nfsiod_workqueue,
1747 .flags = RPC_TASK_ASYNC,
1751 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1752 kref_get(&data->kref);
1754 data->rpc_status = 0;
1755 data->timestamp = jiffies;
1756 task = rpc_run_task(&task_setup_data);
1758 return PTR_ERR(task);
1759 status = nfs4_wait_for_completion_rpc_task(task);
1761 data->cancelled = 1;
1764 status = data->rpc_status;
1769 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1771 struct nfs4_opendata *data = calldata;
1772 struct nfs4_state_owner *sp = data->owner;
1773 struct nfs_client *clp = sp->so_server->nfs_client;
1775 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1778 * Check if we still need to send an OPEN call, or if we can use
1779 * a delegation instead.
1781 if (data->state != NULL) {
1782 struct nfs_delegation *delegation;
1784 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1787 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1788 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1789 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1790 can_open_delegated(delegation, data->o_arg.fmode))
1791 goto unlock_no_action;
1794 /* Update client id. */
1795 data->o_arg.clientid = clp->cl_clientid;
1796 switch (data->o_arg.claim) {
1797 case NFS4_OPEN_CLAIM_PREVIOUS:
1798 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1799 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1800 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1801 case NFS4_OPEN_CLAIM_FH:
1802 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1803 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1805 data->timestamp = jiffies;
1806 if (nfs4_setup_sequence(data->o_arg.server,
1807 &data->o_arg.seq_args,
1808 &data->o_res.seq_res,
1810 nfs_release_seqid(data->o_arg.seqid);
1812 /* Set the create mode (note dependency on the session type) */
1813 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1814 if (data->o_arg.open_flags & O_EXCL) {
1815 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1816 if (nfs4_has_persistent_session(clp))
1817 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1818 else if (clp->cl_mvops->minor_version > 0)
1819 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1825 task->tk_action = NULL;
1827 nfs4_sequence_done(task, &data->o_res.seq_res);
1830 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1832 struct nfs4_opendata *data = calldata;
1834 data->rpc_status = task->tk_status;
1836 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1839 if (task->tk_status == 0) {
1840 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1841 switch (data->o_res.f_attr->mode & S_IFMT) {
1845 data->rpc_status = -ELOOP;
1848 data->rpc_status = -EISDIR;
1851 data->rpc_status = -ENOTDIR;
1854 renew_lease(data->o_res.server, data->timestamp);
1855 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1856 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1861 static void nfs4_open_release(void *calldata)
1863 struct nfs4_opendata *data = calldata;
1864 struct nfs4_state *state = NULL;
1866 /* If this request hasn't been cancelled, do nothing */
1867 if (data->cancelled == 0)
1869 /* In case of error, no cleanup! */
1870 if (data->rpc_status != 0 || !data->rpc_done)
1872 /* In case we need an open_confirm, no cleanup! */
1873 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1875 state = nfs4_opendata_to_nfs4_state(data);
1877 nfs4_close_state(state, data->o_arg.fmode);
1879 nfs4_opendata_put(data);
1882 static const struct rpc_call_ops nfs4_open_ops = {
1883 .rpc_call_prepare = nfs4_open_prepare,
1884 .rpc_call_done = nfs4_open_done,
1885 .rpc_release = nfs4_open_release,
1888 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1890 struct inode *dir = data->dir->d_inode;
1891 struct nfs_server *server = NFS_SERVER(dir);
1892 struct nfs_openargs *o_arg = &data->o_arg;
1893 struct nfs_openres *o_res = &data->o_res;
1894 struct rpc_task *task;
1895 struct rpc_message msg = {
1896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1899 .rpc_cred = data->owner->so_cred,
1901 struct rpc_task_setup task_setup_data = {
1902 .rpc_client = server->client,
1903 .rpc_message = &msg,
1904 .callback_ops = &nfs4_open_ops,
1905 .callback_data = data,
1906 .workqueue = nfsiod_workqueue,
1907 .flags = RPC_TASK_ASYNC,
1911 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1912 kref_get(&data->kref);
1914 data->rpc_status = 0;
1915 data->cancelled = 0;
1916 data->is_recover = 0;
1918 nfs4_set_sequence_privileged(&o_arg->seq_args);
1919 data->is_recover = 1;
1921 task = rpc_run_task(&task_setup_data);
1923 return PTR_ERR(task);
1924 status = nfs4_wait_for_completion_rpc_task(task);
1926 data->cancelled = 1;
1929 status = data->rpc_status;
1935 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1937 struct inode *dir = data->dir->d_inode;
1938 struct nfs_openres *o_res = &data->o_res;
1941 status = nfs4_run_open_task(data, 1);
1942 if (status != 0 || !data->rpc_done)
1945 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1947 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1948 status = _nfs4_proc_open_confirm(data);
1957 * Additional permission checks in order to distinguish between an
1958 * open for read, and an open for execute. This works around the
1959 * fact that NFSv4 OPEN treats read and execute permissions as being
1961 * Note that in the non-execute case, we want to turn off permission
1962 * checking if we just created a new file (POSIX open() semantics).
1964 static int nfs4_opendata_access(struct rpc_cred *cred,
1965 struct nfs4_opendata *opendata,
1966 struct nfs4_state *state, fmode_t fmode,
1969 struct nfs_access_entry cache;
1972 /* access call failed or for some reason the server doesn't
1973 * support any access modes -- defer access call until later */
1974 if (opendata->o_res.access_supported == 0)
1979 * Use openflags to check for exec, because fmode won't
1980 * always have FMODE_EXEC set when file open for exec.
1982 if (openflags & __FMODE_EXEC) {
1983 /* ONLY check for exec rights */
1985 } else if ((fmode & FMODE_READ) && !opendata->file_created)
1989 cache.jiffies = jiffies;
1990 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1991 nfs_access_add_cache(state->inode, &cache);
1993 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1996 /* even though OPEN succeeded, access is denied. Close the file */
1997 nfs4_close_state(state, fmode);
2002 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2004 static int _nfs4_proc_open(struct nfs4_opendata *data)
2006 struct inode *dir = data->dir->d_inode;
2007 struct nfs_server *server = NFS_SERVER(dir);
2008 struct nfs_openargs *o_arg = &data->o_arg;
2009 struct nfs_openres *o_res = &data->o_res;
2012 status = nfs4_run_open_task(data, 0);
2013 if (!data->rpc_done)
2016 if (status == -NFS4ERR_BADNAME &&
2017 !(o_arg->open_flags & O_CREAT))
2022 nfs_fattr_map_and_free_names(server, &data->f_attr);
2024 if (o_arg->open_flags & O_CREAT) {
2025 update_changeattr(dir, &o_res->cinfo);
2026 if (o_arg->open_flags & O_EXCL)
2027 data->file_created = 1;
2028 else if (o_res->cinfo.before != o_res->cinfo.after)
2029 data->file_created = 1;
2031 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2032 server->caps &= ~NFS_CAP_POSIX_LOCK;
2033 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2034 status = _nfs4_proc_open_confirm(data);
2038 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2039 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2043 static int nfs4_recover_expired_lease(struct nfs_server *server)
2045 return nfs4_client_recover_expired_lease(server->nfs_client);
2050 * reclaim state on the server after a network partition.
2051 * Assumes caller holds the appropriate lock
2053 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2055 struct nfs4_opendata *opendata;
2058 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2059 NFS4_OPEN_CLAIM_FH);
2060 if (IS_ERR(opendata))
2061 return PTR_ERR(opendata);
2062 ret = nfs4_open_recover(opendata, state);
2064 d_drop(ctx->dentry);
2065 nfs4_opendata_put(opendata);
2069 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2071 struct nfs_server *server = NFS_SERVER(state->inode);
2072 struct nfs4_exception exception = { };
2076 err = _nfs4_open_expired(ctx, state);
2077 trace_nfs4_open_expired(ctx, 0, err);
2078 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2083 case -NFS4ERR_GRACE:
2084 case -NFS4ERR_DELAY:
2085 nfs4_handle_exception(server, err, &exception);
2088 } while (exception.retry);
2093 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2095 struct nfs_open_context *ctx;
2098 ctx = nfs4_state_find_open_context(state);
2101 ret = nfs4_do_open_expired(ctx, state);
2102 put_nfs_open_context(ctx);
2106 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2108 nfs_remove_bad_delegation(state->inode);
2109 write_seqlock(&state->seqlock);
2110 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2111 write_sequnlock(&state->seqlock);
2112 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2115 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2117 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2118 nfs_finish_clear_delegation_stateid(state);
2121 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2123 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2124 nfs40_clear_delegation_stateid(state);
2125 return nfs4_open_expired(sp, state);
2128 #if defined(CONFIG_NFS_V4_1)
2129 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2131 struct nfs_server *server = NFS_SERVER(state->inode);
2132 nfs4_stateid stateid;
2133 struct nfs_delegation *delegation;
2134 struct rpc_cred *cred;
2137 /* Get the delegation credential for use by test/free_stateid */
2139 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2140 if (delegation == NULL) {
2145 nfs4_stateid_copy(&stateid, &delegation->stateid);
2146 cred = get_rpccred(delegation->cred);
2148 status = nfs41_test_stateid(server, &stateid, cred);
2149 trace_nfs4_test_delegation_stateid(state, NULL, status);
2151 if (status != NFS_OK) {
2152 /* Free the stateid unless the server explicitly
2153 * informs us the stateid is unrecognized. */
2154 if (status != -NFS4ERR_BAD_STATEID)
2155 nfs41_free_stateid(server, &stateid, cred);
2156 nfs_finish_clear_delegation_stateid(state);
2163 * nfs41_check_open_stateid - possibly free an open stateid
2165 * @state: NFSv4 state for an inode
2167 * Returns NFS_OK if recovery for this stateid is now finished.
2168 * Otherwise a negative NFS4ERR value is returned.
2170 static int nfs41_check_open_stateid(struct nfs4_state *state)
2172 struct nfs_server *server = NFS_SERVER(state->inode);
2173 nfs4_stateid *stateid = &state->open_stateid;
2174 struct rpc_cred *cred = state->owner->so_cred;
2177 /* If a state reset has been done, test_stateid is unneeded */
2178 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2179 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2180 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2181 return -NFS4ERR_BAD_STATEID;
2183 status = nfs41_test_stateid(server, stateid, cred);
2184 trace_nfs4_test_open_stateid(state, NULL, status);
2185 if (status != NFS_OK) {
2186 /* Free the stateid unless the server explicitly
2187 * informs us the stateid is unrecognized. */
2188 if (status != -NFS4ERR_BAD_STATEID)
2189 nfs41_free_stateid(server, stateid, cred);
2191 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2192 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2193 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2194 clear_bit(NFS_OPEN_STATE, &state->flags);
2199 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2203 nfs41_check_delegation_stateid(state);
2204 status = nfs41_check_open_stateid(state);
2205 if (status != NFS_OK)
2206 status = nfs4_open_expired(sp, state);
2212 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2213 * fields corresponding to attributes that were used to store the verifier.
2214 * Make sure we clobber those fields in the later setattr call
2216 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2218 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2219 !(sattr->ia_valid & ATTR_ATIME_SET))
2220 sattr->ia_valid |= ATTR_ATIME;
2222 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2223 !(sattr->ia_valid & ATTR_MTIME_SET))
2224 sattr->ia_valid |= ATTR_MTIME;
2227 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2230 struct nfs_open_context *ctx)
2232 struct nfs4_state_owner *sp = opendata->owner;
2233 struct nfs_server *server = sp->so_server;
2234 struct dentry *dentry;
2235 struct nfs4_state *state;
2239 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2241 ret = _nfs4_proc_open(opendata);
2245 state = nfs4_opendata_to_nfs4_state(opendata);
2246 ret = PTR_ERR(state);
2249 if (server->caps & NFS_CAP_POSIX_LOCK)
2250 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2252 dentry = opendata->dentry;
2253 if (dentry->d_inode == NULL) {
2254 /* FIXME: Is this d_drop() ever needed? */
2256 dentry = d_add_unique(dentry, igrab(state->inode));
2257 if (dentry == NULL) {
2258 dentry = opendata->dentry;
2259 } else if (dentry != ctx->dentry) {
2261 ctx->dentry = dget(dentry);
2263 nfs_set_verifier(dentry,
2264 nfs_save_change_attribute(opendata->dir->d_inode));
2267 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2272 if (dentry->d_inode == state->inode) {
2273 nfs_inode_attach_open_context(ctx);
2274 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2275 nfs4_schedule_stateid_recovery(server, state);
2282 * Returns a referenced nfs4_state
2284 static int _nfs4_do_open(struct inode *dir,
2285 struct nfs_open_context *ctx,
2287 struct iattr *sattr,
2288 struct nfs4_label *label,
2291 struct nfs4_state_owner *sp;
2292 struct nfs4_state *state = NULL;
2293 struct nfs_server *server = NFS_SERVER(dir);
2294 struct nfs4_opendata *opendata;
2295 struct dentry *dentry = ctx->dentry;
2296 struct rpc_cred *cred = ctx->cred;
2297 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2298 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2299 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2300 struct nfs4_label *olabel = NULL;
2303 /* Protect against reboot recovery conflicts */
2305 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2307 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2310 status = nfs4_recover_expired_lease(server);
2312 goto err_put_state_owner;
2313 if (dentry->d_inode != NULL)
2314 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2316 if (dentry->d_inode)
2317 claim = NFS4_OPEN_CLAIM_FH;
2318 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2319 label, claim, GFP_KERNEL);
2320 if (opendata == NULL)
2321 goto err_put_state_owner;
2324 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2325 if (IS_ERR(olabel)) {
2326 status = PTR_ERR(olabel);
2327 goto err_opendata_put;
2331 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2332 if (!opendata->f_attr.mdsthreshold) {
2333 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2334 if (!opendata->f_attr.mdsthreshold)
2335 goto err_free_label;
2337 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2339 if (dentry->d_inode != NULL)
2340 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2342 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2344 goto err_free_label;
2347 if ((opendata->o_arg.open_flags & O_EXCL) &&
2348 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2349 nfs4_exclusive_attrset(opendata, sattr);
2351 nfs_fattr_init(opendata->o_res.f_attr);
2352 status = nfs4_do_setattr(state->inode, cred,
2353 opendata->o_res.f_attr, sattr,
2354 state, label, olabel);
2356 nfs_setattr_update_inode(state->inode, sattr);
2357 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2358 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2361 if (opendata->file_created)
2362 *opened |= FILE_CREATED;
2364 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2365 *ctx_th = opendata->f_attr.mdsthreshold;
2366 opendata->f_attr.mdsthreshold = NULL;
2369 nfs4_label_free(olabel);
2371 nfs4_opendata_put(opendata);
2372 nfs4_put_state_owner(sp);
2375 nfs4_label_free(olabel);
2377 nfs4_opendata_put(opendata);
2378 err_put_state_owner:
2379 nfs4_put_state_owner(sp);
2385 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2386 struct nfs_open_context *ctx,
2388 struct iattr *sattr,
2389 struct nfs4_label *label,
2392 struct nfs_server *server = NFS_SERVER(dir);
2393 struct nfs4_exception exception = { };
2394 struct nfs4_state *res;
2398 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2400 trace_nfs4_open_file(ctx, flags, status);
2403 /* NOTE: BAD_SEQID means the server and client disagree about the
2404 * book-keeping w.r.t. state-changing operations
2405 * (OPEN/CLOSE/LOCK/LOCKU...)
2406 * It is actually a sign of a bug on the client or on the server.
2408 * If we receive a BAD_SEQID error in the particular case of
2409 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2410 * have unhashed the old state_owner for us, and that we can
2411 * therefore safely retry using a new one. We should still warn
2412 * the user though...
2414 if (status == -NFS4ERR_BAD_SEQID) {
2415 pr_warn_ratelimited("NFS: v4 server %s "
2416 " returned a bad sequence-id error!\n",
2417 NFS_SERVER(dir)->nfs_client->cl_hostname);
2418 exception.retry = 1;
2422 * BAD_STATEID on OPEN means that the server cancelled our
2423 * state before it received the OPEN_CONFIRM.
2424 * Recover by retrying the request as per the discussion
2425 * on Page 181 of RFC3530.
2427 if (status == -NFS4ERR_BAD_STATEID) {
2428 exception.retry = 1;
2431 if (status == -EAGAIN) {
2432 /* We must have found a delegation */
2433 exception.retry = 1;
2436 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2438 res = ERR_PTR(nfs4_handle_exception(server,
2439 status, &exception));
2440 } while (exception.retry);
2444 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2445 struct nfs_fattr *fattr, struct iattr *sattr,
2446 struct nfs4_state *state, struct nfs4_label *ilabel,
2447 struct nfs4_label *olabel)
2449 struct nfs_server *server = NFS_SERVER(inode);
2450 struct nfs_setattrargs arg = {
2451 .fh = NFS_FH(inode),
2454 .bitmask = server->attr_bitmask,
2457 struct nfs_setattrres res = {
2462 struct rpc_message msg = {
2463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2468 unsigned long timestamp = jiffies;
2473 arg.bitmask = nfs4_bitmask(server, ilabel);
2475 arg.bitmask = nfs4_bitmask(server, olabel);
2477 nfs_fattr_init(fattr);
2479 /* Servers should only apply open mode checks for file size changes */
2480 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2481 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2483 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2484 /* Use that stateid */
2485 } else if (truncate && state != NULL) {
2486 struct nfs_lockowner lockowner = {
2487 .l_owner = current->files,
2488 .l_pid = current->tgid,
2490 if (!nfs4_valid_open_stateid(state))
2492 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2493 &lockowner) == -EIO)
2496 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2498 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2499 if (status == 0 && state != NULL)
2500 renew_lease(server, timestamp);
2504 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2505 struct nfs_fattr *fattr, struct iattr *sattr,
2506 struct nfs4_state *state, struct nfs4_label *ilabel,
2507 struct nfs4_label *olabel)
2509 struct nfs_server *server = NFS_SERVER(inode);
2510 struct nfs4_exception exception = {
2516 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2517 trace_nfs4_setattr(inode, err);
2519 case -NFS4ERR_OPENMODE:
2520 if (!(sattr->ia_valid & ATTR_SIZE)) {
2521 pr_warn_once("NFSv4: server %s is incorrectly "
2522 "applying open mode checks to "
2523 "a SETATTR that is not "
2524 "changing file size.\n",
2525 server->nfs_client->cl_hostname);
2527 if (state && !(state->state & FMODE_WRITE)) {
2529 if (sattr->ia_valid & ATTR_OPEN)
2534 err = nfs4_handle_exception(server, err, &exception);
2535 } while (exception.retry);
2540 struct nfs4_closedata {
2541 struct inode *inode;
2542 struct nfs4_state *state;
2543 struct nfs_closeargs arg;
2544 struct nfs_closeres res;
2545 struct nfs_fattr fattr;
2546 unsigned long timestamp;
2551 static void nfs4_free_closedata(void *data)
2553 struct nfs4_closedata *calldata = data;
2554 struct nfs4_state_owner *sp = calldata->state->owner;
2555 struct super_block *sb = calldata->state->inode->i_sb;
2558 pnfs_roc_release(calldata->state->inode);
2559 nfs4_put_open_state(calldata->state);
2560 nfs_free_seqid(calldata->arg.seqid);
2561 nfs4_put_state_owner(sp);
2562 nfs_sb_deactive(sb);
2566 static void nfs4_close_done(struct rpc_task *task, void *data)
2568 struct nfs4_closedata *calldata = data;
2569 struct nfs4_state *state = calldata->state;
2570 struct nfs_server *server = NFS_SERVER(calldata->inode);
2571 nfs4_stateid *res_stateid = NULL;
2573 dprintk("%s: begin!\n", __func__);
2574 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2576 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2577 /* hmm. we are done with the inode, and in the process of freeing
2578 * the state_owner. we keep this around to process errors
2580 switch (task->tk_status) {
2582 res_stateid = &calldata->res.stateid;
2583 if (calldata->arg.fmode == 0 && calldata->roc)
2584 pnfs_roc_set_barrier(state->inode,
2585 calldata->roc_barrier);
2586 renew_lease(server, calldata->timestamp);
2588 case -NFS4ERR_ADMIN_REVOKED:
2589 case -NFS4ERR_STALE_STATEID:
2590 case -NFS4ERR_OLD_STATEID:
2591 case -NFS4ERR_BAD_STATEID:
2592 case -NFS4ERR_EXPIRED:
2593 if (calldata->arg.fmode == 0)
2596 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2597 rpc_restart_call_prepare(task);
2601 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
2603 nfs_release_seqid(calldata->arg.seqid);
2604 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2605 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2608 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2610 struct nfs4_closedata *calldata = data;
2611 struct nfs4_state *state = calldata->state;
2612 struct inode *inode = calldata->inode;
2613 bool is_rdonly, is_wronly, is_rdwr;
2616 dprintk("%s: begin!\n", __func__);
2617 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2620 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2621 spin_lock(&state->owner->so_lock);
2622 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2623 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2624 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2625 /* Calculate the change in open mode */
2626 calldata->arg.fmode = 0;
2627 if (state->n_rdwr == 0) {
2628 if (state->n_rdonly == 0)
2629 call_close |= is_rdonly;
2631 calldata->arg.fmode |= FMODE_READ;
2632 if (state->n_wronly == 0)
2633 call_close |= is_wronly;
2635 calldata->arg.fmode |= FMODE_WRITE;
2637 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2639 if (calldata->arg.fmode == 0)
2640 call_close |= is_rdwr;
2642 if (!nfs4_valid_open_stateid(state))
2644 spin_unlock(&state->owner->so_lock);
2647 /* Note: exit _without_ calling nfs4_close_done */
2651 if (calldata->arg.fmode == 0) {
2652 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2653 if (calldata->roc &&
2654 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2655 nfs_release_seqid(calldata->arg.seqid);
2660 nfs_fattr_init(calldata->res.fattr);
2661 calldata->timestamp = jiffies;
2662 if (nfs4_setup_sequence(NFS_SERVER(inode),
2663 &calldata->arg.seq_args,
2664 &calldata->res.seq_res,
2666 nfs_release_seqid(calldata->arg.seqid);
2667 dprintk("%s: done!\n", __func__);
2670 task->tk_action = NULL;
2672 nfs4_sequence_done(task, &calldata->res.seq_res);
2675 static const struct rpc_call_ops nfs4_close_ops = {
2676 .rpc_call_prepare = nfs4_close_prepare,
2677 .rpc_call_done = nfs4_close_done,
2678 .rpc_release = nfs4_free_closedata,
2681 static bool nfs4_state_has_opener(struct nfs4_state *state)
2683 /* first check existing openers */
2684 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 &&
2685 state->n_rdonly != 0)
2688 if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 &&
2689 state->n_wronly != 0)
2692 if (test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 &&
2699 static bool nfs4_roc(struct inode *inode)
2701 struct nfs_inode *nfsi = NFS_I(inode);
2702 struct nfs_open_context *ctx;
2703 struct nfs4_state *state;
2705 spin_lock(&inode->i_lock);
2706 list_for_each_entry(ctx, &nfsi->open_files, list) {
2710 if (nfs4_state_has_opener(state)) {
2711 spin_unlock(&inode->i_lock);
2715 spin_unlock(&inode->i_lock);
2717 if (nfs4_check_delegation(inode, FMODE_READ))
2720 return pnfs_roc(inode);
2724 * It is possible for data to be read/written from a mem-mapped file
2725 * after the sys_close call (which hits the vfs layer as a flush).
2726 * This means that we can't safely call nfsv4 close on a file until
2727 * the inode is cleared. This in turn means that we are not good
2728 * NFSv4 citizens - we do not indicate to the server to update the file's
2729 * share state even when we are done with one of the three share
2730 * stateid's in the inode.
2732 * NOTE: Caller must be holding the sp->so_owner semaphore!
2734 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2736 struct nfs_server *server = NFS_SERVER(state->inode);
2737 struct nfs4_closedata *calldata;
2738 struct nfs4_state_owner *sp = state->owner;
2739 struct rpc_task *task;
2740 struct rpc_message msg = {
2741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2742 .rpc_cred = state->owner->so_cred,
2744 struct rpc_task_setup task_setup_data = {
2745 .rpc_client = server->client,
2746 .rpc_message = &msg,
2747 .callback_ops = &nfs4_close_ops,
2748 .workqueue = nfsiod_workqueue,
2749 .flags = RPC_TASK_ASYNC,
2751 int status = -ENOMEM;
2753 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2754 &task_setup_data.rpc_client, &msg);
2756 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2757 if (calldata == NULL)
2759 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2760 calldata->inode = state->inode;
2761 calldata->state = state;
2762 calldata->arg.fh = NFS_FH(state->inode);
2763 calldata->arg.stateid = &state->open_stateid;
2764 /* Serialization for the sequence id */
2765 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2766 if (calldata->arg.seqid == NULL)
2767 goto out_free_calldata;
2768 calldata->arg.fmode = 0;
2769 calldata->arg.bitmask = server->cache_consistency_bitmask;
2770 calldata->res.fattr = &calldata->fattr;
2771 calldata->res.seqid = calldata->arg.seqid;
2772 calldata->res.server = server;
2773 calldata->roc = nfs4_roc(state->inode);
2774 nfs_sb_active(calldata->inode->i_sb);
2776 msg.rpc_argp = &calldata->arg;
2777 msg.rpc_resp = &calldata->res;
2778 task_setup_data.callback_data = calldata;
2779 task = rpc_run_task(&task_setup_data);
2781 return PTR_ERR(task);
2784 status = rpc_wait_for_completion_task(task);
2790 nfs4_put_open_state(state);
2791 nfs4_put_state_owner(sp);
2795 static struct inode *
2796 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2797 int open_flags, struct iattr *attr, int *opened)
2799 struct nfs4_state *state;
2800 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2802 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2804 /* Protect against concurrent sillydeletes */
2805 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2807 nfs4_label_release_security(label);
2810 return ERR_CAST(state);
2811 return state->inode;
2814 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2816 if (ctx->state == NULL)
2819 nfs4_close_sync(ctx->state, ctx->mode);
2821 nfs4_close_state(ctx->state, ctx->mode);
2824 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2825 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2826 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2828 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2830 struct nfs4_server_caps_arg args = {
2833 struct nfs4_server_caps_res res = {};
2834 struct rpc_message msg = {
2835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2841 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2843 /* Sanity check the server answers */
2844 switch (server->nfs_client->cl_minorversion) {
2846 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2847 res.attr_bitmask[2] = 0;
2850 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2853 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2855 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2856 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2857 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2858 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2859 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2860 NFS_CAP_CTIME|NFS_CAP_MTIME|
2861 NFS_CAP_SECURITY_LABEL);
2862 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2863 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2864 server->caps |= NFS_CAP_ACLS;
2865 if (res.has_links != 0)
2866 server->caps |= NFS_CAP_HARDLINKS;
2867 if (res.has_symlinks != 0)
2868 server->caps |= NFS_CAP_SYMLINKS;
2869 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2870 server->caps |= NFS_CAP_FILEID;
2871 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2872 server->caps |= NFS_CAP_MODE;
2873 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2874 server->caps |= NFS_CAP_NLINK;
2875 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2876 server->caps |= NFS_CAP_OWNER;
2877 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2878 server->caps |= NFS_CAP_OWNER_GROUP;
2879 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2880 server->caps |= NFS_CAP_ATIME;
2881 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2882 server->caps |= NFS_CAP_CTIME;
2883 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2884 server->caps |= NFS_CAP_MTIME;
2885 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2886 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2887 server->caps |= NFS_CAP_SECURITY_LABEL;
2889 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2890 sizeof(server->attr_bitmask));
2891 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2893 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2894 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2895 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2896 server->cache_consistency_bitmask[2] = 0;
2897 server->acl_bitmask = res.acl_bitmask;
2898 server->fh_expire_type = res.fh_expire_type;
2904 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2906 struct nfs4_exception exception = { };
2909 err = nfs4_handle_exception(server,
2910 _nfs4_server_capabilities(server, fhandle),
2912 } while (exception.retry);
2916 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2917 struct nfs_fsinfo *info)
2920 struct nfs4_lookup_root_arg args = {
2923 struct nfs4_lookup_res res = {
2925 .fattr = info->fattr,
2928 struct rpc_message msg = {
2929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2934 bitmask[0] = nfs4_fattr_bitmap[0];
2935 bitmask[1] = nfs4_fattr_bitmap[1];
2937 * Process the label in the upcoming getfattr
2939 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2941 nfs_fattr_init(info->fattr);
2942 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2945 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2946 struct nfs_fsinfo *info)
2948 struct nfs4_exception exception = { };
2951 err = _nfs4_lookup_root(server, fhandle, info);
2952 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2955 case -NFS4ERR_WRONGSEC:
2958 err = nfs4_handle_exception(server, err, &exception);
2960 } while (exception.retry);
2965 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2966 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2968 struct rpc_auth_create_args auth_args = {
2969 .pseudoflavor = flavor,
2971 struct rpc_auth *auth;
2974 auth = rpcauth_create(&auth_args, server->client);
2979 ret = nfs4_lookup_root(server, fhandle, info);
2985 * Retry pseudoroot lookup with various security flavors. We do this when:
2987 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2988 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2990 * Returns zero on success, or a negative NFS4ERR value, or a
2991 * negative errno value.
2993 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2994 struct nfs_fsinfo *info)
2996 /* Per 3530bis 15.33.5 */
2997 static const rpc_authflavor_t flav_array[] = {
3001 RPC_AUTH_UNIX, /* courtesy */
3004 int status = -EPERM;
3007 if (server->auth_info.flavor_len > 0) {
3008 /* try each flavor specified by user */
3009 for (i = 0; i < server->auth_info.flavor_len; i++) {
3010 status = nfs4_lookup_root_sec(server, fhandle, info,
3011 server->auth_info.flavors[i]);
3012 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3017 /* no flavors specified by user, try default list */
3018 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3019 status = nfs4_lookup_root_sec(server, fhandle, info,
3021 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3028 * -EACCESS could mean that the user doesn't have correct permissions
3029 * to access the mount. It could also mean that we tried to mount
3030 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3031 * existing mount programs don't handle -EACCES very well so it should
3032 * be mapped to -EPERM instead.
3034 if (status == -EACCES)
3039 static int nfs4_do_find_root_sec(struct nfs_server *server,
3040 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3042 int mv = server->nfs_client->cl_minorversion;
3043 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3047 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3048 * @server: initialized nfs_server handle
3049 * @fhandle: we fill in the pseudo-fs root file handle
3050 * @info: we fill in an FSINFO struct
3051 * @auth_probe: probe the auth flavours
3053 * Returns zero on success, or a negative errno.
3055 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3056 struct nfs_fsinfo *info,
3061 switch (auth_probe) {
3063 status = nfs4_lookup_root(server, fhandle, info);
3064 if (status != -NFS4ERR_WRONGSEC)
3067 status = nfs4_do_find_root_sec(server, fhandle, info);
3071 status = nfs4_server_capabilities(server, fhandle);
3073 status = nfs4_do_fsinfo(server, fhandle, info);
3075 return nfs4_map_errors(status);
3078 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3079 struct nfs_fsinfo *info)
3082 struct nfs_fattr *fattr = info->fattr;
3083 struct nfs4_label *label = NULL;
3085 error = nfs4_server_capabilities(server, mntfh);
3087 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3091 label = nfs4_label_alloc(server, GFP_KERNEL);
3093 return PTR_ERR(label);
3095 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3097 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3098 goto err_free_label;
3101 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3102 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3103 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3106 nfs4_label_free(label);
3112 * Get locations and (maybe) other attributes of a referral.
3113 * Note that we'll actually follow the referral later when
3114 * we detect fsid mismatch in inode revalidation
3116 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3117 const struct qstr *name, struct nfs_fattr *fattr,
3118 struct nfs_fh *fhandle)
3120 int status = -ENOMEM;
3121 struct page *page = NULL;
3122 struct nfs4_fs_locations *locations = NULL;
3124 page = alloc_page(GFP_KERNEL);
3127 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3128 if (locations == NULL)
3131 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3136 * If the fsid didn't change, this is a migration event, not a
3137 * referral. Cause us to drop into the exception handler, which
3138 * will kick off migration recovery.
3140 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3141 dprintk("%s: server did not return a different fsid for"
3142 " a referral at %s\n", __func__, name->name);
3143 status = -NFS4ERR_MOVED;
3146 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3147 nfs_fixup_referral_attributes(&locations->fattr);
3149 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3150 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3151 memset(fhandle, 0, sizeof(struct nfs_fh));
3159 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3160 struct nfs_fattr *fattr, struct nfs4_label *label)
3162 struct nfs4_getattr_arg args = {
3164 .bitmask = server->attr_bitmask,
3166 struct nfs4_getattr_res res = {
3171 struct rpc_message msg = {
3172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3177 args.bitmask = nfs4_bitmask(server, label);
3179 nfs_fattr_init(fattr);
3180 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3183 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3184 struct nfs_fattr *fattr, struct nfs4_label *label)
3186 struct nfs4_exception exception = { };
3189 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3190 trace_nfs4_getattr(server, fhandle, fattr, err);
3191 err = nfs4_handle_exception(server, err,
3193 } while (exception.retry);
3198 * The file is not closed if it is opened due to the a request to change
3199 * the size of the file. The open call will not be needed once the
3200 * VFS layer lookup-intents are implemented.
3202 * Close is called when the inode is destroyed.
3203 * If we haven't opened the file for O_WRONLY, we
3204 * need to in the size_change case to obtain a stateid.
3207 * Because OPEN is always done by name in nfsv4, it is
3208 * possible that we opened a different file by the same
3209 * name. We can recognize this race condition, but we
3210 * can't do anything about it besides returning an error.
3212 * This will be fixed with VFS changes (lookup-intent).
3215 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3216 struct iattr *sattr)
3218 struct inode *inode = dentry->d_inode;
3219 struct rpc_cred *cred = NULL;
3220 struct nfs4_state *state = NULL;
3221 struct nfs4_label *label = NULL;
3224 if (pnfs_ld_layoutret_on_setattr(inode) &&
3225 sattr->ia_valid & ATTR_SIZE &&
3226 sattr->ia_size < i_size_read(inode))
3227 pnfs_commit_and_return_layout(inode);
3229 nfs_fattr_init(fattr);
3231 /* Deal with open(O_TRUNC) */
3232 if (sattr->ia_valid & ATTR_OPEN)
3233 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3235 /* Optimization: if the end result is no change, don't RPC */
3236 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3239 /* Search for an existing open(O_WRITE) file */
3240 if (sattr->ia_valid & ATTR_FILE) {
3241 struct nfs_open_context *ctx;
3243 ctx = nfs_file_open_context(sattr->ia_file);
3250 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3252 return PTR_ERR(label);
3254 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3256 nfs_setattr_update_inode(inode, sattr);
3257 nfs_setsecurity(inode, fattr, label);
3259 nfs4_label_free(label);
3263 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3264 const struct qstr *name, struct nfs_fh *fhandle,
3265 struct nfs_fattr *fattr, struct nfs4_label *label)
3267 struct nfs_server *server = NFS_SERVER(dir);
3269 struct nfs4_lookup_arg args = {
3270 .bitmask = server->attr_bitmask,
3271 .dir_fh = NFS_FH(dir),
3274 struct nfs4_lookup_res res = {
3280 struct rpc_message msg = {
3281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3286 args.bitmask = nfs4_bitmask(server, label);
3288 nfs_fattr_init(fattr);
3290 dprintk("NFS call lookup %s\n", name->name);
3291 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3292 dprintk("NFS reply lookup: %d\n", status);
3296 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3298 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3299 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3300 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3304 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3305 struct qstr *name, struct nfs_fh *fhandle,
3306 struct nfs_fattr *fattr, struct nfs4_label *label)
3308 struct nfs4_exception exception = { };
3309 struct rpc_clnt *client = *clnt;
3312 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3313 trace_nfs4_lookup(dir, name, err);
3315 case -NFS4ERR_BADNAME:
3318 case -NFS4ERR_MOVED:
3319 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3321 case -NFS4ERR_WRONGSEC:
3323 if (client != *clnt)
3325 client = nfs4_negotiate_security(client, dir, name);
3327 return PTR_ERR(client);
3329 exception.retry = 1;
3332 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3334 } while (exception.retry);
3339 else if (client != *clnt)
3340 rpc_shutdown_client(client);
3345 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3346 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3347 struct nfs4_label *label)
3350 struct rpc_clnt *client = NFS_CLIENT(dir);
3352 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3353 if (client != NFS_CLIENT(dir)) {
3354 rpc_shutdown_client(client);
3355 nfs_fixup_secinfo_attributes(fattr);
3361 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3362 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3364 struct rpc_clnt *client = NFS_CLIENT(dir);
3367 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3369 return ERR_PTR(status);
3370 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3373 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3375 struct nfs_server *server = NFS_SERVER(inode);
3376 struct nfs4_accessargs args = {
3377 .fh = NFS_FH(inode),
3378 .bitmask = server->cache_consistency_bitmask,
3380 struct nfs4_accessres res = {
3383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3387 .rpc_cred = entry->cred,
3389 int mode = entry->mask;
3393 * Determine which access bits we want to ask for...
3395 if (mode & MAY_READ)
3396 args.access |= NFS4_ACCESS_READ;
3397 if (S_ISDIR(inode->i_mode)) {
3398 if (mode & MAY_WRITE)
3399 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3400 if (mode & MAY_EXEC)
3401 args.access |= NFS4_ACCESS_LOOKUP;
3403 if (mode & MAY_WRITE)
3404 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3405 if (mode & MAY_EXEC)
3406 args.access |= NFS4_ACCESS_EXECUTE;
3409 res.fattr = nfs_alloc_fattr();
3410 if (res.fattr == NULL)
3413 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3415 nfs_access_set_mask(entry, res.access);
3416 nfs_refresh_inode(inode, res.fattr);
3418 nfs_free_fattr(res.fattr);
3422 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3424 struct nfs4_exception exception = { };
3427 err = _nfs4_proc_access(inode, entry);
3428 trace_nfs4_access(inode, err);
3429 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3431 } while (exception.retry);
3436 * TODO: For the time being, we don't try to get any attributes
3437 * along with any of the zero-copy operations READ, READDIR,
3440 * In the case of the first three, we want to put the GETATTR
3441 * after the read-type operation -- this is because it is hard
3442 * to predict the length of a GETATTR response in v4, and thus
3443 * align the READ data correctly. This means that the GETATTR
3444 * may end up partially falling into the page cache, and we should
3445 * shift it into the 'tail' of the xdr_buf before processing.
3446 * To do this efficiently, we need to know the total length
3447 * of data received, which doesn't seem to be available outside
3450 * In the case of WRITE, we also want to put the GETATTR after
3451 * the operation -- in this case because we want to make sure
3452 * we get the post-operation mtime and size.
3454 * Both of these changes to the XDR layer would in fact be quite
3455 * minor, but I decided to leave them for a subsequent patch.
3457 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3458 unsigned int pgbase, unsigned int pglen)
3460 struct nfs4_readlink args = {
3461 .fh = NFS_FH(inode),
3466 struct nfs4_readlink_res res;
3467 struct rpc_message msg = {
3468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3473 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3476 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3477 unsigned int pgbase, unsigned int pglen)
3479 struct nfs4_exception exception = { };
3482 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3483 trace_nfs4_readlink(inode, err);
3484 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3486 } while (exception.retry);
3491 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3494 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3497 struct nfs4_label l, *ilabel = NULL;
3498 struct nfs_open_context *ctx;
3499 struct nfs4_state *state;
3503 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3505 return PTR_ERR(ctx);
3507 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3509 sattr->ia_mode &= ~current_umask();
3510 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3511 if (IS_ERR(state)) {
3512 status = PTR_ERR(state);
3516 nfs4_label_release_security(ilabel);
3517 put_nfs_open_context(ctx);
3521 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3523 struct nfs_server *server = NFS_SERVER(dir);
3524 struct nfs_removeargs args = {
3528 struct nfs_removeres res = {
3531 struct rpc_message msg = {
3532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3538 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3540 update_changeattr(dir, &res.cinfo);
3544 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3546 struct nfs4_exception exception = { };
3549 err = _nfs4_proc_remove(dir, name);
3550 trace_nfs4_remove(dir, name, err);
3551 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3553 } while (exception.retry);
3557 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3559 struct nfs_server *server = NFS_SERVER(dir);
3560 struct nfs_removeargs *args = msg->rpc_argp;
3561 struct nfs_removeres *res = msg->rpc_resp;
3563 res->server = server;
3564 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3565 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3567 nfs_fattr_init(res->dir_attr);
3570 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3572 nfs4_setup_sequence(NFS_SERVER(data->dir),
3573 &data->args.seq_args,
3578 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3580 struct nfs_unlinkdata *data = task->tk_calldata;
3581 struct nfs_removeres *res = &data->res;
3583 if (!nfs4_sequence_done(task, &res->seq_res))
3585 if (nfs4_async_handle_error(task, res->server, NULL,
3586 &data->timeout) == -EAGAIN)
3588 update_changeattr(dir, &res->cinfo);
3592 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3594 struct nfs_server *server = NFS_SERVER(dir);
3595 struct nfs_renameargs *arg = msg->rpc_argp;
3596 struct nfs_renameres *res = msg->rpc_resp;
3598 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3599 res->server = server;
3600 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3603 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3605 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3606 &data->args.seq_args,
3611 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3612 struct inode *new_dir)
3614 struct nfs_renamedata *data = task->tk_calldata;
3615 struct nfs_renameres *res = &data->res;
3617 if (!nfs4_sequence_done(task, &res->seq_res))
3619 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3622 update_changeattr(old_dir, &res->old_cinfo);
3623 update_changeattr(new_dir, &res->new_cinfo);
3627 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3629 struct nfs_server *server = NFS_SERVER(inode);
3630 struct nfs4_link_arg arg = {
3631 .fh = NFS_FH(inode),
3632 .dir_fh = NFS_FH(dir),
3634 .bitmask = server->attr_bitmask,
3636 struct nfs4_link_res res = {
3640 struct rpc_message msg = {
3641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3645 int status = -ENOMEM;
3647 res.fattr = nfs_alloc_fattr();
3648 if (res.fattr == NULL)
3651 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3652 if (IS_ERR(res.label)) {
3653 status = PTR_ERR(res.label);
3656 arg.bitmask = nfs4_bitmask(server, res.label);
3658 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3660 update_changeattr(dir, &res.cinfo);
3661 status = nfs_post_op_update_inode(inode, res.fattr);
3663 nfs_setsecurity(inode, res.fattr, res.label);
3667 nfs4_label_free(res.label);
3670 nfs_free_fattr(res.fattr);
3674 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3676 struct nfs4_exception exception = { };
3679 err = nfs4_handle_exception(NFS_SERVER(inode),
3680 _nfs4_proc_link(inode, dir, name),
3682 } while (exception.retry);
3686 struct nfs4_createdata {
3687 struct rpc_message msg;
3688 struct nfs4_create_arg arg;
3689 struct nfs4_create_res res;
3691 struct nfs_fattr fattr;
3692 struct nfs4_label *label;
3695 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3696 struct qstr *name, struct iattr *sattr, u32 ftype)
3698 struct nfs4_createdata *data;
3700 data = kzalloc(sizeof(*data), GFP_KERNEL);
3702 struct nfs_server *server = NFS_SERVER(dir);
3704 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3705 if (IS_ERR(data->label))
3708 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3709 data->msg.rpc_argp = &data->arg;
3710 data->msg.rpc_resp = &data->res;
3711 data->arg.dir_fh = NFS_FH(dir);
3712 data->arg.server = server;
3713 data->arg.name = name;
3714 data->arg.attrs = sattr;
3715 data->arg.ftype = ftype;
3716 data->arg.bitmask = nfs4_bitmask(server, data->label);
3717 data->res.server = server;
3718 data->res.fh = &data->fh;
3719 data->res.fattr = &data->fattr;
3720 data->res.label = data->label;
3721 nfs_fattr_init(data->res.fattr);
3729 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3731 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3732 &data->arg.seq_args, &data->res.seq_res, 1);
3734 update_changeattr(dir, &data->res.dir_cinfo);
3735 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3740 static void nfs4_free_createdata(struct nfs4_createdata *data)
3742 nfs4_label_free(data->label);
3746 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3747 struct page *page, unsigned int len, struct iattr *sattr,
3748 struct nfs4_label *label)
3750 struct nfs4_createdata *data;
3751 int status = -ENAMETOOLONG;
3753 if (len > NFS4_MAXPATHLEN)
3757 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3761 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3762 data->arg.u.symlink.pages = &page;
3763 data->arg.u.symlink.len = len;
3764 data->arg.label = label;
3766 status = nfs4_do_create(dir, dentry, data);
3768 nfs4_free_createdata(data);
3773 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3774 struct page *page, unsigned int len, struct iattr *sattr)
3776 struct nfs4_exception exception = { };
3777 struct nfs4_label l, *label = NULL;
3780 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3783 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3784 trace_nfs4_symlink(dir, &dentry->d_name, err);
3785 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3787 } while (exception.retry);
3789 nfs4_label_release_security(label);
3793 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3794 struct iattr *sattr, struct nfs4_label *label)
3796 struct nfs4_createdata *data;
3797 int status = -ENOMEM;
3799 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3803 data->arg.label = label;
3804 status = nfs4_do_create(dir, dentry, data);
3806 nfs4_free_createdata(data);
3811 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3812 struct iattr *sattr)
3814 struct nfs4_exception exception = { };
3815 struct nfs4_label l, *label = NULL;
3818 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3820 sattr->ia_mode &= ~current_umask();
3822 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3823 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3824 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3826 } while (exception.retry);
3827 nfs4_label_release_security(label);
3832 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3833 u64 cookie, struct page **pages, unsigned int count, int plus)
3835 struct inode *dir = dentry->d_inode;
3836 struct nfs4_readdir_arg args = {
3841 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3844 struct nfs4_readdir_res res;
3845 struct rpc_message msg = {
3846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3853 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3855 (unsigned long long)cookie);
3856 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3857 res.pgbase = args.pgbase;
3858 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3860 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3861 status += args.pgbase;
3864 nfs_invalidate_atime(dir);
3866 dprintk("%s: returns %d\n", __func__, status);
3870 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3871 u64 cookie, struct page **pages, unsigned int count, int plus)
3873 struct nfs4_exception exception = { };
3876 err = _nfs4_proc_readdir(dentry, cred, cookie,
3877 pages, count, plus);
3878 trace_nfs4_readdir(dentry->d_inode, err);
3879 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3881 } while (exception.retry);
3885 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3886 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3888 struct nfs4_createdata *data;
3889 int mode = sattr->ia_mode;
3890 int status = -ENOMEM;
3892 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3897 data->arg.ftype = NF4FIFO;
3898 else if (S_ISBLK(mode)) {
3899 data->arg.ftype = NF4BLK;
3900 data->arg.u.device.specdata1 = MAJOR(rdev);
3901 data->arg.u.device.specdata2 = MINOR(rdev);
3903 else if (S_ISCHR(mode)) {
3904 data->arg.ftype = NF4CHR;
3905 data->arg.u.device.specdata1 = MAJOR(rdev);
3906 data->arg.u.device.specdata2 = MINOR(rdev);
3907 } else if (!S_ISSOCK(mode)) {
3912 data->arg.label = label;
3913 status = nfs4_do_create(dir, dentry, data);
3915 nfs4_free_createdata(data);
3920 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3921 struct iattr *sattr, dev_t rdev)
3923 struct nfs4_exception exception = { };
3924 struct nfs4_label l, *label = NULL;
3927 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3929 sattr->ia_mode &= ~current_umask();
3931 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3932 trace_nfs4_mknod(dir, &dentry->d_name, err);
3933 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3935 } while (exception.retry);
3937 nfs4_label_release_security(label);
3942 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3943 struct nfs_fsstat *fsstat)
3945 struct nfs4_statfs_arg args = {
3947 .bitmask = server->attr_bitmask,
3949 struct nfs4_statfs_res res = {
3952 struct rpc_message msg = {
3953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3958 nfs_fattr_init(fsstat->fattr);
3959 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3962 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3964 struct nfs4_exception exception = { };
3967 err = nfs4_handle_exception(server,
3968 _nfs4_proc_statfs(server, fhandle, fsstat),
3970 } while (exception.retry);
3974 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3975 struct nfs_fsinfo *fsinfo)
3977 struct nfs4_fsinfo_arg args = {
3979 .bitmask = server->attr_bitmask,
3981 struct nfs4_fsinfo_res res = {
3984 struct rpc_message msg = {
3985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3990 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3993 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3995 struct nfs4_exception exception = { };
3996 unsigned long now = jiffies;
4000 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4001 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4003 struct nfs_client *clp = server->nfs_client;
4005 spin_lock(&clp->cl_lock);
4006 clp->cl_lease_time = fsinfo->lease_time * HZ;
4007 clp->cl_last_renewal = now;
4008 spin_unlock(&clp->cl_lock);
4011 err = nfs4_handle_exception(server, err, &exception);
4012 } while (exception.retry);
4016 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4020 nfs_fattr_init(fsinfo->fattr);
4021 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4023 /* block layout checks this! */
4024 server->pnfs_blksize = fsinfo->blksize;
4025 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4031 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4032 struct nfs_pathconf *pathconf)
4034 struct nfs4_pathconf_arg args = {
4036 .bitmask = server->attr_bitmask,
4038 struct nfs4_pathconf_res res = {
4039 .pathconf = pathconf,
4041 struct rpc_message msg = {
4042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4047 /* None of the pathconf attributes are mandatory to implement */
4048 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4049 memset(pathconf, 0, sizeof(*pathconf));
4053 nfs_fattr_init(pathconf->fattr);
4054 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4057 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4058 struct nfs_pathconf *pathconf)
4060 struct nfs4_exception exception = { };
4064 err = nfs4_handle_exception(server,
4065 _nfs4_proc_pathconf(server, fhandle, pathconf),
4067 } while (exception.retry);
4071 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4072 const struct nfs_open_context *ctx,
4073 const struct nfs_lock_context *l_ctx,
4076 const struct nfs_lockowner *lockowner = NULL;
4079 lockowner = &l_ctx->lockowner;
4080 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4082 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4084 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4085 const struct nfs_open_context *ctx,
4086 const struct nfs_lock_context *l_ctx,
4089 nfs4_stateid current_stateid;
4091 /* If the current stateid represents a lost lock, then exit */
4092 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4094 return nfs4_stateid_match(stateid, ¤t_stateid);
4097 static bool nfs4_error_stateid_expired(int err)
4100 case -NFS4ERR_DELEG_REVOKED:
4101 case -NFS4ERR_ADMIN_REVOKED:
4102 case -NFS4ERR_BAD_STATEID:
4103 case -NFS4ERR_STALE_STATEID:
4104 case -NFS4ERR_OLD_STATEID:
4105 case -NFS4ERR_OPENMODE:
4106 case -NFS4ERR_EXPIRED:
4112 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4114 nfs_invalidate_atime(hdr->inode);
4117 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4119 struct nfs_server *server = NFS_SERVER(hdr->inode);
4121 trace_nfs4_read(hdr, task->tk_status);
4122 if (nfs4_async_handle_error(task, server,
4123 hdr->args.context->state,
4125 rpc_restart_call_prepare(task);
4129 __nfs4_read_done_cb(hdr);
4130 if (task->tk_status > 0)
4131 renew_lease(server, hdr->timestamp);
4135 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4136 struct nfs_pgio_args *args)
4139 if (!nfs4_error_stateid_expired(task->tk_status) ||
4140 nfs4_stateid_is_current(&args->stateid,
4145 rpc_restart_call_prepare(task);
4149 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4152 dprintk("--> %s\n", __func__);
4154 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4156 if (nfs4_read_stateid_changed(task, &hdr->args))
4158 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4159 nfs4_read_done_cb(task, hdr);
4162 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4163 struct rpc_message *msg)
4165 hdr->timestamp = jiffies;
4166 hdr->pgio_done_cb = nfs4_read_done_cb;
4167 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4168 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4171 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4172 struct nfs_pgio_header *hdr)
4174 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4175 &hdr->args.seq_args,
4179 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4180 hdr->args.lock_context,
4181 hdr->rw_ops->rw_mode) == -EIO)
4183 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4188 static int nfs4_write_done_cb(struct rpc_task *task,
4189 struct nfs_pgio_header *hdr)
4191 struct inode *inode = hdr->inode;
4193 trace_nfs4_write(hdr, task->tk_status);
4194 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4195 hdr->args.context->state,
4197 rpc_restart_call_prepare(task);
4200 if (task->tk_status >= 0) {
4201 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4202 nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr);
4207 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4208 struct nfs_pgio_args *args)
4211 if (!nfs4_error_stateid_expired(task->tk_status) ||
4212 nfs4_stateid_is_current(&args->stateid,
4217 rpc_restart_call_prepare(task);
4221 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4223 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4225 if (nfs4_write_stateid_changed(task, &hdr->args))
4227 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4228 nfs4_write_done_cb(task, hdr);
4232 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4234 /* Don't request attributes for pNFS or O_DIRECT writes */
4235 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4237 /* Otherwise, request attributes if and only if we don't hold
4240 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4243 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4244 struct rpc_message *msg)
4246 struct nfs_server *server = NFS_SERVER(hdr->inode);
4248 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4249 hdr->args.bitmask = NULL;
4250 hdr->res.fattr = NULL;
4252 hdr->args.bitmask = server->cache_consistency_bitmask;
4254 if (!hdr->pgio_done_cb)
4255 hdr->pgio_done_cb = nfs4_write_done_cb;
4256 hdr->res.server = server;
4257 hdr->timestamp = jiffies;
4259 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4260 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4263 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4265 nfs4_setup_sequence(NFS_SERVER(data->inode),
4266 &data->args.seq_args,
4271 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4273 struct inode *inode = data->inode;
4275 trace_nfs4_commit(data, task->tk_status);
4276 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4277 NULL, NULL) == -EAGAIN) {
4278 rpc_restart_call_prepare(task);
4284 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4286 if (!nfs4_sequence_done(task, &data->res.seq_res))
4288 return data->commit_done_cb(task, data);
4291 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4293 struct nfs_server *server = NFS_SERVER(data->inode);
4295 if (data->commit_done_cb == NULL)
4296 data->commit_done_cb = nfs4_commit_done_cb;
4297 data->res.server = server;
4298 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4299 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4302 struct nfs4_renewdata {
4303 struct nfs_client *client;
4304 unsigned long timestamp;
4308 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4309 * standalone procedure for queueing an asynchronous RENEW.
4311 static void nfs4_renew_release(void *calldata)
4313 struct nfs4_renewdata *data = calldata;
4314 struct nfs_client *clp = data->client;
4316 if (atomic_read(&clp->cl_count) > 1)
4317 nfs4_schedule_state_renewal(clp);
4318 nfs_put_client(clp);
4322 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4324 struct nfs4_renewdata *data = calldata;
4325 struct nfs_client *clp = data->client;
4326 unsigned long timestamp = data->timestamp;
4328 trace_nfs4_renew_async(clp, task->tk_status);
4329 switch (task->tk_status) {
4332 case -NFS4ERR_LEASE_MOVED:
4333 nfs4_schedule_lease_moved_recovery(clp);
4336 /* Unless we're shutting down, schedule state recovery! */
4337 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4339 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4340 nfs4_schedule_lease_recovery(clp);
4343 nfs4_schedule_path_down_recovery(clp);
4345 do_renew_lease(clp, timestamp);
4348 static const struct rpc_call_ops nfs4_renew_ops = {
4349 .rpc_call_done = nfs4_renew_done,
4350 .rpc_release = nfs4_renew_release,
4353 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4355 struct rpc_message msg = {
4356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4360 struct nfs4_renewdata *data;
4362 if (renew_flags == 0)
4364 if (!atomic_inc_not_zero(&clp->cl_count))
4366 data = kmalloc(sizeof(*data), GFP_NOFS);
4370 data->timestamp = jiffies;
4371 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4372 &nfs4_renew_ops, data);
4375 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4377 struct rpc_message msg = {
4378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4382 unsigned long now = jiffies;
4385 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4388 do_renew_lease(clp, now);
4392 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4394 return server->caps & NFS_CAP_ACLS;
4397 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4398 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4401 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4403 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4404 struct page **pages, unsigned int *pgbase)
4406 struct page *newpage, **spages;
4412 len = min_t(size_t, PAGE_SIZE, buflen);
4413 newpage = alloc_page(GFP_KERNEL);
4415 if (newpage == NULL)
4417 memcpy(page_address(newpage), buf, len);
4422 } while (buflen != 0);
4428 __free_page(spages[rc-1]);
4432 struct nfs4_cached_acl {
4438 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4440 struct nfs_inode *nfsi = NFS_I(inode);
4442 spin_lock(&inode->i_lock);
4443 kfree(nfsi->nfs4_acl);
4444 nfsi->nfs4_acl = acl;
4445 spin_unlock(&inode->i_lock);
4448 static void nfs4_zap_acl_attr(struct inode *inode)
4450 nfs4_set_cached_acl(inode, NULL);
4453 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4455 struct nfs_inode *nfsi = NFS_I(inode);
4456 struct nfs4_cached_acl *acl;
4459 spin_lock(&inode->i_lock);
4460 acl = nfsi->nfs4_acl;
4463 if (buf == NULL) /* user is just asking for length */
4465 if (acl->cached == 0)
4467 ret = -ERANGE; /* see getxattr(2) man page */
4468 if (acl->len > buflen)
4470 memcpy(buf, acl->data, acl->len);
4474 spin_unlock(&inode->i_lock);
4478 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4480 struct nfs4_cached_acl *acl;
4481 size_t buflen = sizeof(*acl) + acl_len;
4483 if (buflen <= PAGE_SIZE) {
4484 acl = kmalloc(buflen, GFP_KERNEL);
4488 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4490 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4497 nfs4_set_cached_acl(inode, acl);
4501 * The getxattr API returns the required buffer length when called with a
4502 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4503 * the required buf. On a NULL buf, we send a page of data to the server
4504 * guessing that the ACL request can be serviced by a page. If so, we cache
4505 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4506 * the cache. If not so, we throw away the page, and cache the required
4507 * length. The next getxattr call will then produce another round trip to
4508 * the server, this time with the input buf of the required size.
4510 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4512 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4513 struct nfs_getaclargs args = {
4514 .fh = NFS_FH(inode),
4518 struct nfs_getaclres res = {
4521 struct rpc_message msg = {
4522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4526 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4527 int ret = -ENOMEM, i;
4529 /* As long as we're doing a round trip to the server anyway,
4530 * let's be prepared for a page of acl data. */
4533 if (npages > ARRAY_SIZE(pages))
4536 for (i = 0; i < npages; i++) {
4537 pages[i] = alloc_page(GFP_KERNEL);
4542 /* for decoding across pages */
4543 res.acl_scratch = alloc_page(GFP_KERNEL);
4544 if (!res.acl_scratch)
4547 args.acl_len = npages * PAGE_SIZE;
4548 args.acl_pgbase = 0;
4550 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4551 __func__, buf, buflen, npages, args.acl_len);
4552 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4553 &msg, &args.seq_args, &res.seq_res, 0);
4557 /* Handle the case where the passed-in buffer is too short */
4558 if (res.acl_flags & NFS4_ACL_TRUNC) {
4559 /* Did the user only issue a request for the acl length? */
4565 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4567 if (res.acl_len > buflen) {
4571 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4576 for (i = 0; i < npages; i++)
4578 __free_page(pages[i]);
4579 if (res.acl_scratch)
4580 __free_page(res.acl_scratch);
4584 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4586 struct nfs4_exception exception = { };
4589 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4590 trace_nfs4_get_acl(inode, ret);
4593 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4594 } while (exception.retry);
4598 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4600 struct nfs_server *server = NFS_SERVER(inode);
4603 if (!nfs4_server_supports_acls(server))
4605 ret = nfs_revalidate_inode(server, inode);
4608 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4609 nfs_zap_acl_cache(inode);
4610 ret = nfs4_read_cached_acl(inode, buf, buflen);
4612 /* -ENOENT is returned if there is no ACL or if there is an ACL
4613 * but no cached acl data, just the acl length */
4615 return nfs4_get_acl_uncached(inode, buf, buflen);
4618 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4620 struct nfs_server *server = NFS_SERVER(inode);
4621 struct page *pages[NFS4ACL_MAXPAGES];
4622 struct nfs_setaclargs arg = {
4623 .fh = NFS_FH(inode),
4627 struct nfs_setaclres res;
4628 struct rpc_message msg = {
4629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4633 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4636 if (!nfs4_server_supports_acls(server))
4638 if (npages > ARRAY_SIZE(pages))
4640 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4643 nfs4_inode_return_delegation(inode);
4644 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4647 * Free each page after tx, so the only ref left is
4648 * held by the network stack
4651 put_page(pages[i-1]);
4654 * Acl update can result in inode attribute update.
4655 * so mark the attribute cache invalid.
4657 spin_lock(&inode->i_lock);
4658 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4659 spin_unlock(&inode->i_lock);
4660 nfs_access_zap_cache(inode);
4661 nfs_zap_acl_cache(inode);
4665 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4667 struct nfs4_exception exception = { };
4670 err = __nfs4_proc_set_acl(inode, buf, buflen);
4671 trace_nfs4_set_acl(inode, err);
4672 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4674 } while (exception.retry);
4678 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4679 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4682 struct nfs_server *server = NFS_SERVER(inode);
4683 struct nfs_fattr fattr;
4684 struct nfs4_label label = {0, 0, buflen, buf};
4686 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4687 struct nfs4_getattr_arg arg = {
4688 .fh = NFS_FH(inode),
4691 struct nfs4_getattr_res res = {
4696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4703 nfs_fattr_init(&fattr);
4705 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4708 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4710 if (buflen < label.len)
4715 static int nfs4_get_security_label(struct inode *inode, void *buf,
4718 struct nfs4_exception exception = { };
4721 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4725 err = _nfs4_get_security_label(inode, buf, buflen);
4726 trace_nfs4_get_security_label(inode, err);
4727 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4729 } while (exception.retry);
4733 static int _nfs4_do_set_security_label(struct inode *inode,
4734 struct nfs4_label *ilabel,
4735 struct nfs_fattr *fattr,
4736 struct nfs4_label *olabel)
4739 struct iattr sattr = {0};
4740 struct nfs_server *server = NFS_SERVER(inode);
4741 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4742 struct nfs_setattrargs arg = {
4743 .fh = NFS_FH(inode),
4749 struct nfs_setattrres res = {
4754 struct rpc_message msg = {
4755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4761 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4763 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4765 dprintk("%s failed: %d\n", __func__, status);
4770 static int nfs4_do_set_security_label(struct inode *inode,
4771 struct nfs4_label *ilabel,
4772 struct nfs_fattr *fattr,
4773 struct nfs4_label *olabel)
4775 struct nfs4_exception exception = { };
4779 err = _nfs4_do_set_security_label(inode, ilabel,
4781 trace_nfs4_set_security_label(inode, err);
4782 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4784 } while (exception.retry);
4789 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4791 struct nfs4_label ilabel, *olabel = NULL;
4792 struct nfs_fattr fattr;
4793 struct rpc_cred *cred;
4794 struct inode *inode = dentry->d_inode;
4797 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4800 nfs_fattr_init(&fattr);
4804 ilabel.label = (char *)buf;
4805 ilabel.len = buflen;
4807 cred = rpc_lookup_cred();
4809 return PTR_ERR(cred);
4811 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4812 if (IS_ERR(olabel)) {
4813 status = -PTR_ERR(olabel);
4817 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4819 nfs_setsecurity(inode, &fattr, olabel);
4821 nfs4_label_free(olabel);
4826 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4830 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4831 struct nfs4_state *state, long *timeout)
4833 struct nfs_client *clp = server->nfs_client;
4835 if (task->tk_status >= 0)
4837 switch(task->tk_status) {
4838 case -NFS4ERR_DELEG_REVOKED:
4839 case -NFS4ERR_ADMIN_REVOKED:
4840 case -NFS4ERR_BAD_STATEID:
4841 case -NFS4ERR_OPENMODE:
4844 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4845 goto recovery_failed;
4846 goto wait_on_recovery;
4847 case -NFS4ERR_EXPIRED:
4848 if (state != NULL) {
4849 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4850 goto recovery_failed;
4852 case -NFS4ERR_STALE_STATEID:
4853 case -NFS4ERR_STALE_CLIENTID:
4854 nfs4_schedule_lease_recovery(clp);
4855 goto wait_on_recovery;
4856 case -NFS4ERR_MOVED:
4857 if (nfs4_schedule_migration_recovery(server) < 0)
4858 goto recovery_failed;
4859 goto wait_on_recovery;
4860 case -NFS4ERR_LEASE_MOVED:
4861 nfs4_schedule_lease_moved_recovery(clp);
4862 goto wait_on_recovery;
4863 #if defined(CONFIG_NFS_V4_1)
4864 case -NFS4ERR_BADSESSION:
4865 case -NFS4ERR_BADSLOT:
4866 case -NFS4ERR_BAD_HIGH_SLOT:
4867 case -NFS4ERR_DEADSESSION:
4868 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4869 case -NFS4ERR_SEQ_FALSE_RETRY:
4870 case -NFS4ERR_SEQ_MISORDERED:
4871 dprintk("%s ERROR %d, Reset session\n", __func__,
4873 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4874 goto wait_on_recovery;
4875 #endif /* CONFIG_NFS_V4_1 */
4876 case -NFS4ERR_DELAY:
4877 nfs_inc_server_stats(server, NFSIOS_DELAY);
4878 rpc_delay(task, nfs4_update_delay(timeout));
4880 case -NFS4ERR_GRACE:
4881 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4882 case -NFS4ERR_RETRY_UNCACHED_REP:
4883 case -NFS4ERR_OLD_STATEID:
4886 task->tk_status = nfs4_map_errors(task->tk_status);
4889 task->tk_status = -EIO;
4892 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4893 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4894 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4895 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4896 goto recovery_failed;
4898 task->tk_status = 0;
4902 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4903 nfs4_verifier *bootverf)
4907 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4908 /* An impossible timestamp guarantees this value
4909 * will never match a generated boot time. */
4911 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4913 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4914 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4915 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4917 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4921 nfs4_init_nonuniform_client_string(struct nfs_client *clp,
4922 char *buf, size_t len)
4924 unsigned int result;
4926 if (clp->cl_owner_id != NULL)
4927 return strlcpy(buf, clp->cl_owner_id, len);
4930 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4932 rpc_peeraddr2str(clp->cl_rpcclient,
4934 rpc_peeraddr2str(clp->cl_rpcclient,
4935 RPC_DISPLAY_PROTO));
4937 clp->cl_owner_id = kstrdup(buf, GFP_KERNEL);
4942 nfs4_init_uniform_client_string(struct nfs_client *clp,
4943 char *buf, size_t len)
4945 const char *nodename = clp->cl_rpcclient->cl_nodename;
4946 unsigned int result;
4948 if (clp->cl_owner_id != NULL)
4949 return strlcpy(buf, clp->cl_owner_id, len);
4951 if (nfs4_client_id_uniquifier[0] != '\0')
4952 result = scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4953 clp->rpc_ops->version,
4954 clp->cl_minorversion,
4955 nfs4_client_id_uniquifier,
4958 result = scnprintf(buf, len, "Linux NFSv%u.%u %s",
4959 clp->rpc_ops->version, clp->cl_minorversion,
4961 clp->cl_owner_id = kstrdup(buf, GFP_KERNEL);
4966 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4967 * services. Advertise one based on the address family of the
4971 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4973 if (strchr(clp->cl_ipaddr, ':') != NULL)
4974 return scnprintf(buf, len, "tcp6");
4976 return scnprintf(buf, len, "tcp");
4979 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
4981 struct nfs4_setclientid *sc = calldata;
4983 if (task->tk_status == 0)
4984 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
4987 static const struct rpc_call_ops nfs4_setclientid_ops = {
4988 .rpc_call_done = nfs4_setclientid_done,
4992 * nfs4_proc_setclientid - Negotiate client ID
4993 * @clp: state data structure
4994 * @program: RPC program for NFSv4 callback service
4995 * @port: IP port number for NFS4 callback service
4996 * @cred: RPC credential to use for this call
4997 * @res: where to place the result
4999 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5001 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5002 unsigned short port, struct rpc_cred *cred,
5003 struct nfs4_setclientid_res *res)
5005 nfs4_verifier sc_verifier;
5006 struct nfs4_setclientid setclientid = {
5007 .sc_verifier = &sc_verifier,
5009 .sc_cb_ident = clp->cl_cb_ident,
5011 struct rpc_message msg = {
5012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5013 .rpc_argp = &setclientid,
5017 struct rpc_task *task;
5018 struct rpc_task_setup task_setup_data = {
5019 .rpc_client = clp->cl_rpcclient,
5020 .rpc_message = &msg,
5021 .callback_ops = &nfs4_setclientid_ops,
5022 .callback_data = &setclientid,
5023 .flags = RPC_TASK_TIMEOUT,
5027 /* nfs_client_id4 */
5028 nfs4_init_boot_verifier(clp, &sc_verifier);
5029 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5030 setclientid.sc_name_len =
5031 nfs4_init_uniform_client_string(clp,
5032 setclientid.sc_name,
5033 sizeof(setclientid.sc_name));
5035 setclientid.sc_name_len =
5036 nfs4_init_nonuniform_client_string(clp,
5037 setclientid.sc_name,
5038 sizeof(setclientid.sc_name));
5040 setclientid.sc_netid_len =
5041 nfs4_init_callback_netid(clp,
5042 setclientid.sc_netid,
5043 sizeof(setclientid.sc_netid));
5044 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5045 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5046 clp->cl_ipaddr, port >> 8, port & 255);
5048 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
5049 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5050 setclientid.sc_name_len, setclientid.sc_name);
5051 task = rpc_run_task(&task_setup_data);
5053 status = PTR_ERR(task);
5056 status = task->tk_status;
5057 if (setclientid.sc_cred) {
5058 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5059 put_rpccred(setclientid.sc_cred);
5063 trace_nfs4_setclientid(clp, status);
5064 dprintk("NFS reply setclientid: %d\n", status);
5069 * nfs4_proc_setclientid_confirm - Confirm client ID
5070 * @clp: state data structure
5071 * @res: result of a previous SETCLIENTID
5072 * @cred: RPC credential to use for this call
5074 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5076 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5077 struct nfs4_setclientid_res *arg,
5078 struct rpc_cred *cred)
5080 struct rpc_message msg = {
5081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5087 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5088 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5090 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5091 trace_nfs4_setclientid_confirm(clp, status);
5092 dprintk("NFS reply setclientid_confirm: %d\n", status);
5096 struct nfs4_delegreturndata {
5097 struct nfs4_delegreturnargs args;
5098 struct nfs4_delegreturnres res;
5100 nfs4_stateid stateid;
5101 unsigned long timestamp;
5102 struct nfs_fattr fattr;
5104 struct inode *inode;
5109 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5111 struct nfs4_delegreturndata *data = calldata;
5113 if (!nfs4_sequence_done(task, &data->res.seq_res))
5116 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5117 switch (task->tk_status) {
5119 renew_lease(data->res.server, data->timestamp);
5120 case -NFS4ERR_ADMIN_REVOKED:
5121 case -NFS4ERR_DELEG_REVOKED:
5122 case -NFS4ERR_BAD_STATEID:
5123 case -NFS4ERR_OLD_STATEID:
5124 case -NFS4ERR_STALE_STATEID:
5125 case -NFS4ERR_EXPIRED:
5126 task->tk_status = 0;
5128 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5131 if (nfs4_async_handle_error(task, data->res.server,
5132 NULL, NULL) == -EAGAIN) {
5133 rpc_restart_call_prepare(task);
5137 data->rpc_status = task->tk_status;
5140 static void nfs4_delegreturn_release(void *calldata)
5142 struct nfs4_delegreturndata *data = calldata;
5145 pnfs_roc_release(data->inode);
5149 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5151 struct nfs4_delegreturndata *d_data;
5153 d_data = (struct nfs4_delegreturndata *)data;
5156 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5159 nfs4_setup_sequence(d_data->res.server,
5160 &d_data->args.seq_args,
5161 &d_data->res.seq_res,
5165 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5166 .rpc_call_prepare = nfs4_delegreturn_prepare,
5167 .rpc_call_done = nfs4_delegreturn_done,
5168 .rpc_release = nfs4_delegreturn_release,
5171 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5173 struct nfs4_delegreturndata *data;
5174 struct nfs_server *server = NFS_SERVER(inode);
5175 struct rpc_task *task;
5176 struct rpc_message msg = {
5177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5180 struct rpc_task_setup task_setup_data = {
5181 .rpc_client = server->client,
5182 .rpc_message = &msg,
5183 .callback_ops = &nfs4_delegreturn_ops,
5184 .flags = RPC_TASK_ASYNC,
5188 data = kzalloc(sizeof(*data), GFP_NOFS);
5191 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5192 data->args.fhandle = &data->fh;
5193 data->args.stateid = &data->stateid;
5194 data->args.bitmask = server->cache_consistency_bitmask;
5195 nfs_copy_fh(&data->fh, NFS_FH(inode));
5196 nfs4_stateid_copy(&data->stateid, stateid);
5197 data->res.fattr = &data->fattr;
5198 data->res.server = server;
5199 nfs_fattr_init(data->res.fattr);
5200 data->timestamp = jiffies;
5201 data->rpc_status = 0;
5202 data->inode = inode;
5203 data->roc = list_empty(&NFS_I(inode)->open_files) ?
5204 pnfs_roc(inode) : false;
5206 task_setup_data.callback_data = data;
5207 msg.rpc_argp = &data->args;
5208 msg.rpc_resp = &data->res;
5209 task = rpc_run_task(&task_setup_data);
5211 return PTR_ERR(task);
5214 status = nfs4_wait_for_completion_rpc_task(task);
5217 status = data->rpc_status;
5219 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5221 nfs_refresh_inode(inode, &data->fattr);
5227 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5229 struct nfs_server *server = NFS_SERVER(inode);
5230 struct nfs4_exception exception = { };
5233 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5234 trace_nfs4_delegreturn(inode, err);
5236 case -NFS4ERR_STALE_STATEID:
5237 case -NFS4ERR_EXPIRED:
5241 err = nfs4_handle_exception(server, err, &exception);
5242 } while (exception.retry);
5246 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5247 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5250 * sleep, with exponential backoff, and retry the LOCK operation.
5252 static unsigned long
5253 nfs4_set_lock_task_retry(unsigned long timeout)
5255 freezable_schedule_timeout_killable_unsafe(timeout);
5257 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5258 return NFS4_LOCK_MAXTIMEOUT;
5262 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5264 struct inode *inode = state->inode;
5265 struct nfs_server *server = NFS_SERVER(inode);
5266 struct nfs_client *clp = server->nfs_client;
5267 struct nfs_lockt_args arg = {
5268 .fh = NFS_FH(inode),
5271 struct nfs_lockt_res res = {
5274 struct rpc_message msg = {
5275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5278 .rpc_cred = state->owner->so_cred,
5280 struct nfs4_lock_state *lsp;
5283 arg.lock_owner.clientid = clp->cl_clientid;
5284 status = nfs4_set_lock_state(state, request);
5287 lsp = request->fl_u.nfs4_fl.owner;
5288 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5289 arg.lock_owner.s_dev = server->s_dev;
5290 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5293 request->fl_type = F_UNLCK;
5295 case -NFS4ERR_DENIED:
5298 request->fl_ops->fl_release_private(request);
5299 request->fl_ops = NULL;
5304 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5306 struct nfs4_exception exception = { };
5310 err = _nfs4_proc_getlk(state, cmd, request);
5311 trace_nfs4_get_lock(request, state, cmd, err);
5312 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5314 } while (exception.retry);
5318 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5321 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5323 res = posix_lock_file_wait(file, fl);
5326 res = flock_lock_file_wait(file, fl);
5334 struct nfs4_unlockdata {
5335 struct nfs_locku_args arg;
5336 struct nfs_locku_res res;
5337 struct nfs4_lock_state *lsp;
5338 struct nfs_open_context *ctx;
5339 struct file_lock fl;
5340 const struct nfs_server *server;
5341 unsigned long timestamp;
5344 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5345 struct nfs_open_context *ctx,
5346 struct nfs4_lock_state *lsp,
5347 struct nfs_seqid *seqid)
5349 struct nfs4_unlockdata *p;
5350 struct inode *inode = lsp->ls_state->inode;
5352 p = kzalloc(sizeof(*p), GFP_NOFS);
5355 p->arg.fh = NFS_FH(inode);
5357 p->arg.seqid = seqid;
5358 p->res.seqid = seqid;
5359 p->arg.stateid = &lsp->ls_stateid;
5361 atomic_inc(&lsp->ls_count);
5362 /* Ensure we don't close file until we're done freeing locks! */
5363 p->ctx = get_nfs_open_context(ctx);
5364 memcpy(&p->fl, fl, sizeof(p->fl));
5365 p->server = NFS_SERVER(inode);
5369 static void nfs4_locku_release_calldata(void *data)
5371 struct nfs4_unlockdata *calldata = data;
5372 nfs_free_seqid(calldata->arg.seqid);
5373 nfs4_put_lock_state(calldata->lsp);
5374 put_nfs_open_context(calldata->ctx);
5378 static void nfs4_locku_done(struct rpc_task *task, void *data)
5380 struct nfs4_unlockdata *calldata = data;
5382 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5384 switch (task->tk_status) {
5386 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5387 &calldata->res.stateid);
5388 renew_lease(calldata->server, calldata->timestamp);
5390 case -NFS4ERR_BAD_STATEID:
5391 case -NFS4ERR_OLD_STATEID:
5392 case -NFS4ERR_STALE_STATEID:
5393 case -NFS4ERR_EXPIRED:
5396 if (nfs4_async_handle_error(task, calldata->server,
5397 NULL, NULL) == -EAGAIN)
5398 rpc_restart_call_prepare(task);
5400 nfs_release_seqid(calldata->arg.seqid);
5403 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5405 struct nfs4_unlockdata *calldata = data;
5407 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5409 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5410 /* Note: exit _without_ running nfs4_locku_done */
5413 calldata->timestamp = jiffies;
5414 if (nfs4_setup_sequence(calldata->server,
5415 &calldata->arg.seq_args,
5416 &calldata->res.seq_res,
5418 nfs_release_seqid(calldata->arg.seqid);
5421 task->tk_action = NULL;
5423 nfs4_sequence_done(task, &calldata->res.seq_res);
5426 static const struct rpc_call_ops nfs4_locku_ops = {
5427 .rpc_call_prepare = nfs4_locku_prepare,
5428 .rpc_call_done = nfs4_locku_done,
5429 .rpc_release = nfs4_locku_release_calldata,
5432 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5433 struct nfs_open_context *ctx,
5434 struct nfs4_lock_state *lsp,
5435 struct nfs_seqid *seqid)
5437 struct nfs4_unlockdata *data;
5438 struct rpc_message msg = {
5439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5440 .rpc_cred = ctx->cred,
5442 struct rpc_task_setup task_setup_data = {
5443 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5444 .rpc_message = &msg,
5445 .callback_ops = &nfs4_locku_ops,
5446 .workqueue = nfsiod_workqueue,
5447 .flags = RPC_TASK_ASYNC,
5450 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5451 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5453 /* Ensure this is an unlock - when canceling a lock, the
5454 * canceled lock is passed in, and it won't be an unlock.
5456 fl->fl_type = F_UNLCK;
5458 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5460 nfs_free_seqid(seqid);
5461 return ERR_PTR(-ENOMEM);
5464 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5465 msg.rpc_argp = &data->arg;
5466 msg.rpc_resp = &data->res;
5467 task_setup_data.callback_data = data;
5468 return rpc_run_task(&task_setup_data);
5471 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5473 struct inode *inode = state->inode;
5474 struct nfs4_state_owner *sp = state->owner;
5475 struct nfs_inode *nfsi = NFS_I(inode);
5476 struct nfs_seqid *seqid;
5477 struct nfs4_lock_state *lsp;
5478 struct rpc_task *task;
5480 unsigned char fl_flags = request->fl_flags;
5482 status = nfs4_set_lock_state(state, request);
5483 /* Unlock _before_ we do the RPC call */
5484 request->fl_flags |= FL_EXISTS;
5485 /* Exclude nfs_delegation_claim_locks() */
5486 mutex_lock(&sp->so_delegreturn_mutex);
5487 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5488 down_read(&nfsi->rwsem);
5489 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5490 up_read(&nfsi->rwsem);
5491 mutex_unlock(&sp->so_delegreturn_mutex);
5494 up_read(&nfsi->rwsem);
5495 mutex_unlock(&sp->so_delegreturn_mutex);
5498 /* Is this a delegated lock? */
5499 lsp = request->fl_u.nfs4_fl.owner;
5500 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5502 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5506 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5507 status = PTR_ERR(task);
5510 status = nfs4_wait_for_completion_rpc_task(task);
5513 request->fl_flags = fl_flags;
5514 trace_nfs4_unlock(request, state, F_SETLK, status);
5518 struct nfs4_lockdata {
5519 struct nfs_lock_args arg;
5520 struct nfs_lock_res res;
5521 struct nfs4_lock_state *lsp;
5522 struct nfs_open_context *ctx;
5523 struct file_lock fl;
5524 unsigned long timestamp;
5527 struct nfs_server *server;
5530 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5531 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5534 struct nfs4_lockdata *p;
5535 struct inode *inode = lsp->ls_state->inode;
5536 struct nfs_server *server = NFS_SERVER(inode);
5538 p = kzalloc(sizeof(*p), gfp_mask);
5542 p->arg.fh = NFS_FH(inode);
5544 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5545 if (p->arg.open_seqid == NULL)
5547 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5548 if (p->arg.lock_seqid == NULL)
5549 goto out_free_seqid;
5550 p->arg.lock_stateid = &lsp->ls_stateid;
5551 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5552 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5553 p->arg.lock_owner.s_dev = server->s_dev;
5554 p->res.lock_seqid = p->arg.lock_seqid;
5557 atomic_inc(&lsp->ls_count);
5558 p->ctx = get_nfs_open_context(ctx);
5559 memcpy(&p->fl, fl, sizeof(p->fl));
5562 nfs_free_seqid(p->arg.open_seqid);
5568 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5570 struct nfs4_lockdata *data = calldata;
5571 struct nfs4_state *state = data->lsp->ls_state;
5573 dprintk("%s: begin!\n", __func__);
5574 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5576 /* Do we need to do an open_to_lock_owner? */
5577 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5578 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5579 goto out_release_lock_seqid;
5581 data->arg.open_stateid = &state->open_stateid;
5582 data->arg.new_lock_owner = 1;
5583 data->res.open_seqid = data->arg.open_seqid;
5585 data->arg.new_lock_owner = 0;
5586 if (!nfs4_valid_open_stateid(state)) {
5587 data->rpc_status = -EBADF;
5588 task->tk_action = NULL;
5589 goto out_release_open_seqid;
5591 data->timestamp = jiffies;
5592 if (nfs4_setup_sequence(data->server,
5593 &data->arg.seq_args,
5597 out_release_open_seqid:
5598 nfs_release_seqid(data->arg.open_seqid);
5599 out_release_lock_seqid:
5600 nfs_release_seqid(data->arg.lock_seqid);
5602 nfs4_sequence_done(task, &data->res.seq_res);
5603 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5606 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5608 struct nfs4_lockdata *data = calldata;
5610 dprintk("%s: begin!\n", __func__);
5612 if (!nfs4_sequence_done(task, &data->res.seq_res))
5615 data->rpc_status = task->tk_status;
5616 if (data->arg.new_lock_owner != 0) {
5617 if (data->rpc_status == 0)
5618 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5622 if (data->rpc_status == 0) {
5623 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5624 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5625 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5628 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5631 static void nfs4_lock_release(void *calldata)
5633 struct nfs4_lockdata *data = calldata;
5635 dprintk("%s: begin!\n", __func__);
5636 nfs_free_seqid(data->arg.open_seqid);
5637 if (data->cancelled != 0) {
5638 struct rpc_task *task;
5639 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5640 data->arg.lock_seqid);
5642 rpc_put_task_async(task);
5643 dprintk("%s: cancelling lock!\n", __func__);
5645 nfs_free_seqid(data->arg.lock_seqid);
5646 nfs4_put_lock_state(data->lsp);
5647 put_nfs_open_context(data->ctx);
5649 dprintk("%s: done!\n", __func__);
5652 static const struct rpc_call_ops nfs4_lock_ops = {
5653 .rpc_call_prepare = nfs4_lock_prepare,
5654 .rpc_call_done = nfs4_lock_done,
5655 .rpc_release = nfs4_lock_release,
5658 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5661 case -NFS4ERR_ADMIN_REVOKED:
5662 case -NFS4ERR_BAD_STATEID:
5663 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5664 if (new_lock_owner != 0 ||
5665 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5666 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5668 case -NFS4ERR_STALE_STATEID:
5669 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5670 case -NFS4ERR_EXPIRED:
5671 nfs4_schedule_lease_recovery(server->nfs_client);
5675 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5677 struct nfs4_lockdata *data;
5678 struct rpc_task *task;
5679 struct rpc_message msg = {
5680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5681 .rpc_cred = state->owner->so_cred,
5683 struct rpc_task_setup task_setup_data = {
5684 .rpc_client = NFS_CLIENT(state->inode),
5685 .rpc_message = &msg,
5686 .callback_ops = &nfs4_lock_ops,
5687 .workqueue = nfsiod_workqueue,
5688 .flags = RPC_TASK_ASYNC,
5692 dprintk("%s: begin!\n", __func__);
5693 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5694 fl->fl_u.nfs4_fl.owner,
5695 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5699 data->arg.block = 1;
5700 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5701 msg.rpc_argp = &data->arg;
5702 msg.rpc_resp = &data->res;
5703 task_setup_data.callback_data = data;
5704 if (recovery_type > NFS_LOCK_NEW) {
5705 if (recovery_type == NFS_LOCK_RECLAIM)
5706 data->arg.reclaim = NFS_LOCK_RECLAIM;
5707 nfs4_set_sequence_privileged(&data->arg.seq_args);
5709 task = rpc_run_task(&task_setup_data);
5711 return PTR_ERR(task);
5712 ret = nfs4_wait_for_completion_rpc_task(task);
5714 ret = data->rpc_status;
5716 nfs4_handle_setlk_error(data->server, data->lsp,
5717 data->arg.new_lock_owner, ret);
5719 data->cancelled = 1;
5721 dprintk("%s: done, ret = %d!\n", __func__, ret);
5725 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5727 struct nfs_server *server = NFS_SERVER(state->inode);
5728 struct nfs4_exception exception = {
5729 .inode = state->inode,
5734 /* Cache the lock if possible... */
5735 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5737 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5738 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5739 if (err != -NFS4ERR_DELAY)
5741 nfs4_handle_exception(server, err, &exception);
5742 } while (exception.retry);
5746 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5748 struct nfs_server *server = NFS_SERVER(state->inode);
5749 struct nfs4_exception exception = {
5750 .inode = state->inode,
5754 err = nfs4_set_lock_state(state, request);
5757 if (!recover_lost_locks) {
5758 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5762 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5764 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5765 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5769 case -NFS4ERR_GRACE:
5770 case -NFS4ERR_DELAY:
5771 nfs4_handle_exception(server, err, &exception);
5774 } while (exception.retry);
5779 #if defined(CONFIG_NFS_V4_1)
5781 * nfs41_check_expired_locks - possibly free a lock stateid
5783 * @state: NFSv4 state for an inode
5785 * Returns NFS_OK if recovery for this stateid is now finished.
5786 * Otherwise a negative NFS4ERR value is returned.
5788 static int nfs41_check_expired_locks(struct nfs4_state *state)
5790 int status, ret = -NFS4ERR_BAD_STATEID;
5791 struct nfs4_lock_state *lsp;
5792 struct nfs_server *server = NFS_SERVER(state->inode);
5794 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5795 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5796 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5798 status = nfs41_test_stateid(server,
5801 trace_nfs4_test_lock_stateid(state, lsp, status);
5802 if (status != NFS_OK) {
5803 /* Free the stateid unless the server
5804 * informs us the stateid is unrecognized. */
5805 if (status != -NFS4ERR_BAD_STATEID)
5806 nfs41_free_stateid(server,
5809 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5818 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5820 int status = NFS_OK;
5822 if (test_bit(LK_STATE_IN_USE, &state->flags))
5823 status = nfs41_check_expired_locks(state);
5824 if (status != NFS_OK)
5825 status = nfs4_lock_expired(state, request);
5830 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5832 struct nfs4_state_owner *sp = state->owner;
5833 struct nfs_inode *nfsi = NFS_I(state->inode);
5834 unsigned char fl_flags = request->fl_flags;
5836 int status = -ENOLCK;
5838 if ((fl_flags & FL_POSIX) &&
5839 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5841 /* Is this a delegated open? */
5842 status = nfs4_set_lock_state(state, request);
5845 request->fl_flags |= FL_ACCESS;
5846 status = do_vfs_lock(request->fl_file, request);
5849 down_read(&nfsi->rwsem);
5850 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5851 /* Yes: cache locks! */
5852 /* ...but avoid races with delegation recall... */
5853 request->fl_flags = fl_flags & ~FL_SLEEP;
5854 status = do_vfs_lock(request->fl_file, request);
5857 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5858 up_read(&nfsi->rwsem);
5859 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5862 down_read(&nfsi->rwsem);
5863 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5864 status = -NFS4ERR_DELAY;
5867 /* Note: we always want to sleep here! */
5868 request->fl_flags = fl_flags | FL_SLEEP;
5869 if (do_vfs_lock(request->fl_file, request) < 0)
5870 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5871 "manager!\n", __func__);
5873 up_read(&nfsi->rwsem);
5875 request->fl_flags = fl_flags;
5879 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5881 struct nfs4_exception exception = {
5883 .inode = state->inode,
5888 err = _nfs4_proc_setlk(state, cmd, request);
5889 trace_nfs4_set_lock(request, state, cmd, err);
5890 if (err == -NFS4ERR_DENIED)
5892 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5894 } while (exception.retry);
5899 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5901 struct nfs_open_context *ctx;
5902 struct nfs4_state *state;
5903 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5906 /* verify open state */
5907 ctx = nfs_file_open_context(filp);
5910 if (request->fl_start < 0 || request->fl_end < 0)
5913 if (IS_GETLK(cmd)) {
5915 return nfs4_proc_getlk(state, F_GETLK, request);
5919 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5922 if (request->fl_type == F_UNLCK) {
5924 return nfs4_proc_unlck(state, cmd, request);
5931 * Don't rely on the VFS having checked the file open mode,
5932 * since it won't do this for flock() locks.
5934 switch (request->fl_type) {
5936 if (!(filp->f_mode & FMODE_READ))
5940 if (!(filp->f_mode & FMODE_WRITE))
5945 status = nfs4_proc_setlk(state, cmd, request);
5946 if ((status != -EAGAIN) || IS_SETLK(cmd))
5948 timeout = nfs4_set_lock_task_retry(timeout);
5949 status = -ERESTARTSYS;
5952 } while(status < 0);
5956 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5958 struct nfs_server *server = NFS_SERVER(state->inode);
5961 err = nfs4_set_lock_state(state, fl);
5964 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5965 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5968 struct nfs_release_lockowner_data {
5969 struct nfs4_lock_state *lsp;
5970 struct nfs_server *server;
5971 struct nfs_release_lockowner_args args;
5972 struct nfs_release_lockowner_res res;
5973 unsigned long timestamp;
5976 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5978 struct nfs_release_lockowner_data *data = calldata;
5979 struct nfs_server *server = data->server;
5980 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
5981 &data->args.seq_args, &data->res.seq_res, task);
5982 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5983 data->timestamp = jiffies;
5986 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5988 struct nfs_release_lockowner_data *data = calldata;
5989 struct nfs_server *server = data->server;
5991 nfs40_sequence_done(task, &data->res.seq_res);
5993 switch (task->tk_status) {
5995 renew_lease(server, data->timestamp);
5997 case -NFS4ERR_STALE_CLIENTID:
5998 case -NFS4ERR_EXPIRED:
5999 nfs4_schedule_lease_recovery(server->nfs_client);
6001 case -NFS4ERR_LEASE_MOVED:
6002 case -NFS4ERR_DELAY:
6003 if (nfs4_async_handle_error(task, server,
6004 NULL, NULL) == -EAGAIN)
6005 rpc_restart_call_prepare(task);
6009 static void nfs4_release_lockowner_release(void *calldata)
6011 struct nfs_release_lockowner_data *data = calldata;
6012 nfs4_free_lock_state(data->server, data->lsp);
6016 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6017 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6018 .rpc_call_done = nfs4_release_lockowner_done,
6019 .rpc_release = nfs4_release_lockowner_release,
6023 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6025 struct nfs_release_lockowner_data *data;
6026 struct rpc_message msg = {
6027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6030 if (server->nfs_client->cl_mvops->minor_version != 0)
6033 data = kmalloc(sizeof(*data), GFP_NOFS);
6037 data->server = server;
6038 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6039 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6040 data->args.lock_owner.s_dev = server->s_dev;
6042 msg.rpc_argp = &data->args;
6043 msg.rpc_resp = &data->res;
6044 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6045 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6048 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6050 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6051 const void *buf, size_t buflen,
6052 int flags, int type)
6054 if (strcmp(key, "") != 0)
6057 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
6060 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6061 void *buf, size_t buflen, int type)
6063 if (strcmp(key, "") != 0)
6066 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
6069 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6070 size_t list_len, const char *name,
6071 size_t name_len, int type)
6073 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
6075 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
6078 if (list && len <= list_len)
6079 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
6083 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6084 static inline int nfs4_server_supports_labels(struct nfs_server *server)
6086 return server->caps & NFS_CAP_SECURITY_LABEL;
6089 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6090 const void *buf, size_t buflen,
6091 int flags, int type)
6093 if (security_ismaclabel(key))
6094 return nfs4_set_security_label(dentry, buf, buflen);
6099 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6100 void *buf, size_t buflen, int type)
6102 if (security_ismaclabel(key))
6103 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
6107 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6108 size_t list_len, const char *name,
6109 size_t name_len, int type)
6113 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
6114 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
6115 if (list && len <= list_len)
6116 security_inode_listsecurity(dentry->d_inode, list, len);
6121 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6122 .prefix = XATTR_SECURITY_PREFIX,
6123 .list = nfs4_xattr_list_nfs4_label,
6124 .get = nfs4_xattr_get_nfs4_label,
6125 .set = nfs4_xattr_set_nfs4_label,
6131 * nfs_fhget will use either the mounted_on_fileid or the fileid
6133 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6135 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6136 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6137 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6138 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6141 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6142 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6143 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6147 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6148 const struct qstr *name,
6149 struct nfs4_fs_locations *fs_locations,
6152 struct nfs_server *server = NFS_SERVER(dir);
6154 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6156 struct nfs4_fs_locations_arg args = {
6157 .dir_fh = NFS_FH(dir),
6162 struct nfs4_fs_locations_res res = {
6163 .fs_locations = fs_locations,
6165 struct rpc_message msg = {
6166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6172 dprintk("%s: start\n", __func__);
6174 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6175 * is not supported */
6176 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6177 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6179 bitmask[0] |= FATTR4_WORD0_FILEID;
6181 nfs_fattr_init(&fs_locations->fattr);
6182 fs_locations->server = server;
6183 fs_locations->nlocations = 0;
6184 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6185 dprintk("%s: returned status = %d\n", __func__, status);
6189 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6190 const struct qstr *name,
6191 struct nfs4_fs_locations *fs_locations,
6194 struct nfs4_exception exception = { };
6197 err = _nfs4_proc_fs_locations(client, dir, name,
6198 fs_locations, page);
6199 trace_nfs4_get_fs_locations(dir, name, err);
6200 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6202 } while (exception.retry);
6207 * This operation also signals the server that this client is
6208 * performing migration recovery. The server can stop returning
6209 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6210 * appended to this compound to identify the client ID which is
6211 * performing recovery.
6213 static int _nfs40_proc_get_locations(struct inode *inode,
6214 struct nfs4_fs_locations *locations,
6215 struct page *page, struct rpc_cred *cred)
6217 struct nfs_server *server = NFS_SERVER(inode);
6218 struct rpc_clnt *clnt = server->client;
6220 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6222 struct nfs4_fs_locations_arg args = {
6223 .clientid = server->nfs_client->cl_clientid,
6224 .fh = NFS_FH(inode),
6227 .migration = 1, /* skip LOOKUP */
6228 .renew = 1, /* append RENEW */
6230 struct nfs4_fs_locations_res res = {
6231 .fs_locations = locations,
6235 struct rpc_message msg = {
6236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6241 unsigned long now = jiffies;
6244 nfs_fattr_init(&locations->fattr);
6245 locations->server = server;
6246 locations->nlocations = 0;
6248 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6249 nfs4_set_sequence_privileged(&args.seq_args);
6250 status = nfs4_call_sync_sequence(clnt, server, &msg,
6251 &args.seq_args, &res.seq_res);
6255 renew_lease(server, now);
6259 #ifdef CONFIG_NFS_V4_1
6262 * This operation also signals the server that this client is
6263 * performing migration recovery. The server can stop asserting
6264 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6265 * performing this operation is identified in the SEQUENCE
6266 * operation in this compound.
6268 * When the client supports GETATTR(fs_locations_info), it can
6269 * be plumbed in here.
6271 static int _nfs41_proc_get_locations(struct inode *inode,
6272 struct nfs4_fs_locations *locations,
6273 struct page *page, struct rpc_cred *cred)
6275 struct nfs_server *server = NFS_SERVER(inode);
6276 struct rpc_clnt *clnt = server->client;
6278 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6280 struct nfs4_fs_locations_arg args = {
6281 .fh = NFS_FH(inode),
6284 .migration = 1, /* skip LOOKUP */
6286 struct nfs4_fs_locations_res res = {
6287 .fs_locations = locations,
6290 struct rpc_message msg = {
6291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6298 nfs_fattr_init(&locations->fattr);
6299 locations->server = server;
6300 locations->nlocations = 0;
6302 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6303 nfs4_set_sequence_privileged(&args.seq_args);
6304 status = nfs4_call_sync_sequence(clnt, server, &msg,
6305 &args.seq_args, &res.seq_res);
6306 if (status == NFS4_OK &&
6307 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6308 status = -NFS4ERR_LEASE_MOVED;
6312 #endif /* CONFIG_NFS_V4_1 */
6315 * nfs4_proc_get_locations - discover locations for a migrated FSID
6316 * @inode: inode on FSID that is migrating
6317 * @locations: result of query
6319 * @cred: credential to use for this operation
6321 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6322 * operation failed, or a negative errno if a local error occurred.
6324 * On success, "locations" is filled in, but if the server has
6325 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6328 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6329 * from this client that require migration recovery.
6331 int nfs4_proc_get_locations(struct inode *inode,
6332 struct nfs4_fs_locations *locations,
6333 struct page *page, struct rpc_cred *cred)
6335 struct nfs_server *server = NFS_SERVER(inode);
6336 struct nfs_client *clp = server->nfs_client;
6337 const struct nfs4_mig_recovery_ops *ops =
6338 clp->cl_mvops->mig_recovery_ops;
6339 struct nfs4_exception exception = { };
6342 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6343 (unsigned long long)server->fsid.major,
6344 (unsigned long long)server->fsid.minor,
6346 nfs_display_fhandle(NFS_FH(inode), __func__);
6349 status = ops->get_locations(inode, locations, page, cred);
6350 if (status != -NFS4ERR_DELAY)
6352 nfs4_handle_exception(server, status, &exception);
6353 } while (exception.retry);
6358 * This operation also signals the server that this client is
6359 * performing "lease moved" recovery. The server can stop
6360 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6361 * is appended to this compound to identify the client ID which is
6362 * performing recovery.
6364 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6366 struct nfs_server *server = NFS_SERVER(inode);
6367 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6368 struct rpc_clnt *clnt = server->client;
6369 struct nfs4_fsid_present_arg args = {
6370 .fh = NFS_FH(inode),
6371 .clientid = clp->cl_clientid,
6372 .renew = 1, /* append RENEW */
6374 struct nfs4_fsid_present_res res = {
6377 struct rpc_message msg = {
6378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6383 unsigned long now = jiffies;
6386 res.fh = nfs_alloc_fhandle();
6390 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6391 nfs4_set_sequence_privileged(&args.seq_args);
6392 status = nfs4_call_sync_sequence(clnt, server, &msg,
6393 &args.seq_args, &res.seq_res);
6394 nfs_free_fhandle(res.fh);
6398 do_renew_lease(clp, now);
6402 #ifdef CONFIG_NFS_V4_1
6405 * This operation also signals the server that this client is
6406 * performing "lease moved" recovery. The server can stop asserting
6407 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6408 * this operation is identified in the SEQUENCE operation in this
6411 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6413 struct nfs_server *server = NFS_SERVER(inode);
6414 struct rpc_clnt *clnt = server->client;
6415 struct nfs4_fsid_present_arg args = {
6416 .fh = NFS_FH(inode),
6418 struct nfs4_fsid_present_res res = {
6420 struct rpc_message msg = {
6421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6428 res.fh = nfs_alloc_fhandle();
6432 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6433 nfs4_set_sequence_privileged(&args.seq_args);
6434 status = nfs4_call_sync_sequence(clnt, server, &msg,
6435 &args.seq_args, &res.seq_res);
6436 nfs_free_fhandle(res.fh);
6437 if (status == NFS4_OK &&
6438 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6439 status = -NFS4ERR_LEASE_MOVED;
6443 #endif /* CONFIG_NFS_V4_1 */
6446 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6447 * @inode: inode on FSID to check
6448 * @cred: credential to use for this operation
6450 * Server indicates whether the FSID is present, moved, or not
6451 * recognized. This operation is necessary to clear a LEASE_MOVED
6452 * condition for this client ID.
6454 * Returns NFS4_OK if the FSID is present on this server,
6455 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6456 * NFS4ERR code if some error occurred on the server, or a
6457 * negative errno if a local failure occurred.
6459 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6461 struct nfs_server *server = NFS_SERVER(inode);
6462 struct nfs_client *clp = server->nfs_client;
6463 const struct nfs4_mig_recovery_ops *ops =
6464 clp->cl_mvops->mig_recovery_ops;
6465 struct nfs4_exception exception = { };
6468 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6469 (unsigned long long)server->fsid.major,
6470 (unsigned long long)server->fsid.minor,
6472 nfs_display_fhandle(NFS_FH(inode), __func__);
6475 status = ops->fsid_present(inode, cred);
6476 if (status != -NFS4ERR_DELAY)
6478 nfs4_handle_exception(server, status, &exception);
6479 } while (exception.retry);
6484 * If 'use_integrity' is true and the state managment nfs_client
6485 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6486 * and the machine credential as per RFC3530bis and RFC5661 Security
6487 * Considerations sections. Otherwise, just use the user cred with the
6488 * filesystem's rpc_client.
6490 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6493 struct nfs4_secinfo_arg args = {
6494 .dir_fh = NFS_FH(dir),
6497 struct nfs4_secinfo_res res = {
6500 struct rpc_message msg = {
6501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6505 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6506 struct rpc_cred *cred = NULL;
6508 if (use_integrity) {
6509 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6510 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6511 msg.rpc_cred = cred;
6514 dprintk("NFS call secinfo %s\n", name->name);
6516 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6517 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6519 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6521 dprintk("NFS reply secinfo: %d\n", status);
6529 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6530 struct nfs4_secinfo_flavors *flavors)
6532 struct nfs4_exception exception = { };
6535 err = -NFS4ERR_WRONGSEC;
6537 /* try to use integrity protection with machine cred */
6538 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6539 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6542 * if unable to use integrity protection, or SECINFO with
6543 * integrity protection returns NFS4ERR_WRONGSEC (which is
6544 * disallowed by spec, but exists in deployed servers) use
6545 * the current filesystem's rpc_client and the user cred.
6547 if (err == -NFS4ERR_WRONGSEC)
6548 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6550 trace_nfs4_secinfo(dir, name, err);
6551 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6553 } while (exception.retry);
6557 #ifdef CONFIG_NFS_V4_1
6559 * Check the exchange flags returned by the server for invalid flags, having
6560 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6563 static int nfs4_check_cl_exchange_flags(u32 flags)
6565 if (flags & ~EXCHGID4_FLAG_MASK_R)
6567 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6568 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6570 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6574 return -NFS4ERR_INVAL;
6578 nfs41_same_server_scope(struct nfs41_server_scope *a,
6579 struct nfs41_server_scope *b)
6581 if (a->server_scope_sz == b->server_scope_sz &&
6582 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6589 * nfs4_proc_bind_conn_to_session()
6591 * The 4.1 client currently uses the same TCP connection for the
6592 * fore and backchannel.
6594 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6597 struct nfs41_bind_conn_to_session_res res;
6598 struct rpc_message msg = {
6600 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6606 dprintk("--> %s\n", __func__);
6608 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6609 if (unlikely(res.session == NULL)) {
6614 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6615 trace_nfs4_bind_conn_to_session(clp, status);
6617 if (memcmp(res.session->sess_id.data,
6618 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6619 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6623 if (res.dir != NFS4_CDFS4_BOTH) {
6624 dprintk("NFS: %s: Unexpected direction from server\n",
6629 if (res.use_conn_in_rdma_mode) {
6630 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6639 dprintk("<-- %s status= %d\n", __func__, status);
6644 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6645 * and operations we'd like to see to enable certain features in the allow map
6647 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6648 .how = SP4_MACH_CRED,
6649 .enforce.u.words = {
6650 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6651 1 << (OP_EXCHANGE_ID - 32) |
6652 1 << (OP_CREATE_SESSION - 32) |
6653 1 << (OP_DESTROY_SESSION - 32) |
6654 1 << (OP_DESTROY_CLIENTID - 32)
6657 [0] = 1 << (OP_CLOSE) |
6660 [1] = 1 << (OP_SECINFO - 32) |
6661 1 << (OP_SECINFO_NO_NAME - 32) |
6662 1 << (OP_TEST_STATEID - 32) |
6663 1 << (OP_FREE_STATEID - 32) |
6664 1 << (OP_WRITE - 32)
6669 * Select the state protection mode for client `clp' given the server results
6670 * from exchange_id in `sp'.
6672 * Returns 0 on success, negative errno otherwise.
6674 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6675 struct nfs41_state_protection *sp)
6677 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6678 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6679 1 << (OP_EXCHANGE_ID - 32) |
6680 1 << (OP_CREATE_SESSION - 32) |
6681 1 << (OP_DESTROY_SESSION - 32) |
6682 1 << (OP_DESTROY_CLIENTID - 32)
6686 if (sp->how == SP4_MACH_CRED) {
6687 /* Print state protect result */
6688 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6689 for (i = 0; i <= LAST_NFS4_OP; i++) {
6690 if (test_bit(i, sp->enforce.u.longs))
6691 dfprintk(MOUNT, " enforce op %d\n", i);
6692 if (test_bit(i, sp->allow.u.longs))
6693 dfprintk(MOUNT, " allow op %d\n", i);
6696 /* make sure nothing is on enforce list that isn't supported */
6697 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6698 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6699 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6705 * Minimal mode - state operations are allowed to use machine
6706 * credential. Note this already happens by default, so the
6707 * client doesn't have to do anything more than the negotiation.
6709 * NOTE: we don't care if EXCHANGE_ID is in the list -
6710 * we're already using the machine cred for exchange_id
6711 * and will never use a different cred.
6713 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6714 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6715 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6716 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6717 dfprintk(MOUNT, "sp4_mach_cred:\n");
6718 dfprintk(MOUNT, " minimal mode enabled\n");
6719 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6721 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6725 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6726 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6727 dfprintk(MOUNT, " cleanup mode enabled\n");
6728 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6731 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6732 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6733 dfprintk(MOUNT, " secinfo mode enabled\n");
6734 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6737 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6738 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6739 dfprintk(MOUNT, " stateid mode enabled\n");
6740 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6743 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6744 dfprintk(MOUNT, " write mode enabled\n");
6745 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6748 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6749 dfprintk(MOUNT, " commit mode enabled\n");
6750 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6758 * _nfs4_proc_exchange_id()
6760 * Wrapper for EXCHANGE_ID operation.
6762 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6765 nfs4_verifier verifier;
6766 struct nfs41_exchange_id_args args = {
6767 .verifier = &verifier,
6769 #ifdef CONFIG_NFS_V4_1_MIGRATION
6770 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6771 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6772 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6774 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6775 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6778 struct nfs41_exchange_id_res res = {
6782 struct rpc_message msg = {
6783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6789 nfs4_init_boot_verifier(clp, &verifier);
6790 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6792 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6793 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6794 args.id_len, args.id);
6796 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6798 if (unlikely(res.server_owner == NULL)) {
6803 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6805 if (unlikely(res.server_scope == NULL)) {
6807 goto out_server_owner;
6810 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6811 if (unlikely(res.impl_id == NULL)) {
6813 goto out_server_scope;
6818 args.state_protect.how = SP4_NONE;
6822 args.state_protect = nfs4_sp4_mach_cred_request;
6829 goto out_server_scope;
6832 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6833 trace_nfs4_exchange_id(clp, status);
6835 status = nfs4_check_cl_exchange_flags(res.flags);
6838 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6841 clp->cl_clientid = res.clientid;
6842 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6843 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6844 clp->cl_seqid = res.seqid;
6846 kfree(clp->cl_serverowner);
6847 clp->cl_serverowner = res.server_owner;
6848 res.server_owner = NULL;
6850 /* use the most recent implementation id */
6851 kfree(clp->cl_implid);
6852 clp->cl_implid = res.impl_id;
6854 if (clp->cl_serverscope != NULL &&
6855 !nfs41_same_server_scope(clp->cl_serverscope,
6856 res.server_scope)) {
6857 dprintk("%s: server_scope mismatch detected\n",
6859 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6860 kfree(clp->cl_serverscope);
6861 clp->cl_serverscope = NULL;
6864 if (clp->cl_serverscope == NULL) {
6865 clp->cl_serverscope = res.server_scope;
6872 kfree(res.server_owner);
6874 kfree(res.server_scope);
6876 if (clp->cl_implid != NULL)
6877 dprintk("NFS reply exchange_id: Server Implementation ID: "
6878 "domain: %s, name: %s, date: %llu,%u\n",
6879 clp->cl_implid->domain, clp->cl_implid->name,
6880 clp->cl_implid->date.seconds,
6881 clp->cl_implid->date.nseconds);
6882 dprintk("NFS reply exchange_id: %d\n", status);
6887 * nfs4_proc_exchange_id()
6889 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6891 * Since the clientid has expired, all compounds using sessions
6892 * associated with the stale clientid will be returning
6893 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6894 * be in some phase of session reset.
6896 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6898 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6900 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6903 /* try SP4_MACH_CRED if krb5i/p */
6904 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6905 authflavor == RPC_AUTH_GSS_KRB5P) {
6906 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6912 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6915 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6916 struct rpc_cred *cred)
6918 struct rpc_message msg = {
6919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6925 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6926 trace_nfs4_destroy_clientid(clp, status);
6928 dprintk("NFS: Got error %d from the server %s on "
6929 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6933 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6934 struct rpc_cred *cred)
6939 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6940 ret = _nfs4_proc_destroy_clientid(clp, cred);
6942 case -NFS4ERR_DELAY:
6943 case -NFS4ERR_CLIENTID_BUSY:
6953 int nfs4_destroy_clientid(struct nfs_client *clp)
6955 struct rpc_cred *cred;
6958 if (clp->cl_mvops->minor_version < 1)
6960 if (clp->cl_exchange_flags == 0)
6962 if (clp->cl_preserve_clid)
6964 cred = nfs4_get_clid_cred(clp);
6965 ret = nfs4_proc_destroy_clientid(clp, cred);
6970 case -NFS4ERR_STALE_CLIENTID:
6971 clp->cl_exchange_flags = 0;
6977 struct nfs4_get_lease_time_data {
6978 struct nfs4_get_lease_time_args *args;
6979 struct nfs4_get_lease_time_res *res;
6980 struct nfs_client *clp;
6983 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6986 struct nfs4_get_lease_time_data *data =
6987 (struct nfs4_get_lease_time_data *)calldata;
6989 dprintk("--> %s\n", __func__);
6990 /* just setup sequence, do not trigger session recovery
6991 since we're invoked within one */
6992 nfs41_setup_sequence(data->clp->cl_session,
6993 &data->args->la_seq_args,
6994 &data->res->lr_seq_res,
6996 dprintk("<-- %s\n", __func__);
7000 * Called from nfs4_state_manager thread for session setup, so don't recover
7001 * from sequence operation or clientid errors.
7003 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7005 struct nfs4_get_lease_time_data *data =
7006 (struct nfs4_get_lease_time_data *)calldata;
7008 dprintk("--> %s\n", __func__);
7009 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7011 switch (task->tk_status) {
7012 case -NFS4ERR_DELAY:
7013 case -NFS4ERR_GRACE:
7014 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7015 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7016 task->tk_status = 0;
7018 case -NFS4ERR_RETRY_UNCACHED_REP:
7019 rpc_restart_call_prepare(task);
7022 dprintk("<-- %s\n", __func__);
7025 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7026 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7027 .rpc_call_done = nfs4_get_lease_time_done,
7030 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7032 struct rpc_task *task;
7033 struct nfs4_get_lease_time_args args;
7034 struct nfs4_get_lease_time_res res = {
7035 .lr_fsinfo = fsinfo,
7037 struct nfs4_get_lease_time_data data = {
7042 struct rpc_message msg = {
7043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7047 struct rpc_task_setup task_setup = {
7048 .rpc_client = clp->cl_rpcclient,
7049 .rpc_message = &msg,
7050 .callback_ops = &nfs4_get_lease_time_ops,
7051 .callback_data = &data,
7052 .flags = RPC_TASK_TIMEOUT,
7056 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7057 nfs4_set_sequence_privileged(&args.la_seq_args);
7058 dprintk("--> %s\n", __func__);
7059 task = rpc_run_task(&task_setup);
7062 status = PTR_ERR(task);
7064 status = task->tk_status;
7067 dprintk("<-- %s return %d\n", __func__, status);
7073 * Initialize the values to be used by the client in CREATE_SESSION
7074 * If nfs4_init_session set the fore channel request and response sizes,
7077 * Set the back channel max_resp_sz_cached to zero to force the client to
7078 * always set csa_cachethis to FALSE because the current implementation
7079 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7081 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7083 unsigned int max_rqst_sz, max_resp_sz;
7085 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7086 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7088 /* Fore channel attributes */
7089 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7090 args->fc_attrs.max_resp_sz = max_resp_sz;
7091 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7092 args->fc_attrs.max_reqs = max_session_slots;
7094 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7095 "max_ops=%u max_reqs=%u\n",
7097 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7098 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7100 /* Back channel attributes */
7101 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7102 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7103 args->bc_attrs.max_resp_sz_cached = 0;
7104 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7105 args->bc_attrs.max_reqs = 1;
7107 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7108 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7110 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7111 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7112 args->bc_attrs.max_reqs);
7115 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7117 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7118 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
7120 if (rcvd->max_resp_sz > sent->max_resp_sz)
7123 * Our requested max_ops is the minimum we need; we're not
7124 * prepared to break up compounds into smaller pieces than that.
7125 * So, no point even trying to continue if the server won't
7128 if (rcvd->max_ops < sent->max_ops)
7130 if (rcvd->max_reqs == 0)
7132 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7133 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7137 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7139 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7140 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
7142 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7144 if (rcvd->max_resp_sz < sent->max_resp_sz)
7146 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7148 /* These would render the backchannel useless: */
7149 if (rcvd->max_ops != sent->max_ops)
7151 if (rcvd->max_reqs != sent->max_reqs)
7156 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7157 struct nfs4_session *session)
7161 ret = nfs4_verify_fore_channel_attrs(args, session);
7164 return nfs4_verify_back_channel_attrs(args, session);
7167 static int _nfs4_proc_create_session(struct nfs_client *clp,
7168 struct rpc_cred *cred)
7170 struct nfs4_session *session = clp->cl_session;
7171 struct nfs41_create_session_args args = {
7173 .cb_program = NFS4_CALLBACK,
7175 struct nfs41_create_session_res res = {
7178 struct rpc_message msg = {
7179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7186 nfs4_init_channel_attrs(&args);
7187 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7189 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7190 trace_nfs4_create_session(clp, status);
7193 /* Verify the session's negotiated channel_attrs values */
7194 status = nfs4_verify_channel_attrs(&args, session);
7195 /* Increment the clientid slot sequence id */
7203 * Issues a CREATE_SESSION operation to the server.
7204 * It is the responsibility of the caller to verify the session is
7205 * expired before calling this routine.
7207 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7211 struct nfs4_session *session = clp->cl_session;
7213 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7215 status = _nfs4_proc_create_session(clp, cred);
7219 /* Init or reset the session slot tables */
7220 status = nfs4_setup_session_slot_tables(session);
7221 dprintk("slot table setup returned %d\n", status);
7225 ptr = (unsigned *)&session->sess_id.data[0];
7226 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7227 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7229 dprintk("<-- %s\n", __func__);
7234 * Issue the over-the-wire RPC DESTROY_SESSION.
7235 * The caller must serialize access to this routine.
7237 int nfs4_proc_destroy_session(struct nfs4_session *session,
7238 struct rpc_cred *cred)
7240 struct rpc_message msg = {
7241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7242 .rpc_argp = session,
7247 dprintk("--> nfs4_proc_destroy_session\n");
7249 /* session is still being setup */
7250 if (session->clp->cl_cons_state != NFS_CS_READY)
7253 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7254 trace_nfs4_destroy_session(session->clp, status);
7257 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7258 "Session has been destroyed regardless...\n", status);
7260 dprintk("<-- nfs4_proc_destroy_session\n");
7265 * Renew the cl_session lease.
7267 struct nfs4_sequence_data {
7268 struct nfs_client *clp;
7269 struct nfs4_sequence_args args;
7270 struct nfs4_sequence_res res;
7273 static void nfs41_sequence_release(void *data)
7275 struct nfs4_sequence_data *calldata = data;
7276 struct nfs_client *clp = calldata->clp;
7278 if (atomic_read(&clp->cl_count) > 1)
7279 nfs4_schedule_state_renewal(clp);
7280 nfs_put_client(clp);
7284 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7286 switch(task->tk_status) {
7287 case -NFS4ERR_DELAY:
7288 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7291 nfs4_schedule_lease_recovery(clp);
7296 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7298 struct nfs4_sequence_data *calldata = data;
7299 struct nfs_client *clp = calldata->clp;
7301 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7304 trace_nfs4_sequence(clp, task->tk_status);
7305 if (task->tk_status < 0) {
7306 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7307 if (atomic_read(&clp->cl_count) == 1)
7310 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7311 rpc_restart_call_prepare(task);
7315 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7317 dprintk("<-- %s\n", __func__);
7320 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7322 struct nfs4_sequence_data *calldata = data;
7323 struct nfs_client *clp = calldata->clp;
7324 struct nfs4_sequence_args *args;
7325 struct nfs4_sequence_res *res;
7327 args = task->tk_msg.rpc_argp;
7328 res = task->tk_msg.rpc_resp;
7330 nfs41_setup_sequence(clp->cl_session, args, res, task);
7333 static const struct rpc_call_ops nfs41_sequence_ops = {
7334 .rpc_call_done = nfs41_sequence_call_done,
7335 .rpc_call_prepare = nfs41_sequence_prepare,
7336 .rpc_release = nfs41_sequence_release,
7339 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7340 struct rpc_cred *cred,
7343 struct nfs4_sequence_data *calldata;
7344 struct rpc_message msg = {
7345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7348 struct rpc_task_setup task_setup_data = {
7349 .rpc_client = clp->cl_rpcclient,
7350 .rpc_message = &msg,
7351 .callback_ops = &nfs41_sequence_ops,
7352 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7355 if (!atomic_inc_not_zero(&clp->cl_count))
7356 return ERR_PTR(-EIO);
7357 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7358 if (calldata == NULL) {
7359 nfs_put_client(clp);
7360 return ERR_PTR(-ENOMEM);
7362 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7364 nfs4_set_sequence_privileged(&calldata->args);
7365 msg.rpc_argp = &calldata->args;
7366 msg.rpc_resp = &calldata->res;
7367 calldata->clp = clp;
7368 task_setup_data.callback_data = calldata;
7370 return rpc_run_task(&task_setup_data);
7373 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7375 struct rpc_task *task;
7378 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7380 task = _nfs41_proc_sequence(clp, cred, false);
7382 ret = PTR_ERR(task);
7384 rpc_put_task_async(task);
7385 dprintk("<-- %s status=%d\n", __func__, ret);
7389 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7391 struct rpc_task *task;
7394 task = _nfs41_proc_sequence(clp, cred, true);
7396 ret = PTR_ERR(task);
7399 ret = rpc_wait_for_completion_task(task);
7401 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7403 if (task->tk_status == 0)
7404 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7405 ret = task->tk_status;
7409 dprintk("<-- %s status=%d\n", __func__, ret);
7413 struct nfs4_reclaim_complete_data {
7414 struct nfs_client *clp;
7415 struct nfs41_reclaim_complete_args arg;
7416 struct nfs41_reclaim_complete_res res;
7419 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7421 struct nfs4_reclaim_complete_data *calldata = data;
7423 nfs41_setup_sequence(calldata->clp->cl_session,
7424 &calldata->arg.seq_args,
7425 &calldata->res.seq_res,
7429 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7431 switch(task->tk_status) {
7433 case -NFS4ERR_COMPLETE_ALREADY:
7434 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7436 case -NFS4ERR_DELAY:
7437 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7439 case -NFS4ERR_RETRY_UNCACHED_REP:
7442 nfs4_schedule_lease_recovery(clp);
7447 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7449 struct nfs4_reclaim_complete_data *calldata = data;
7450 struct nfs_client *clp = calldata->clp;
7451 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7453 dprintk("--> %s\n", __func__);
7454 if (!nfs41_sequence_done(task, res))
7457 trace_nfs4_reclaim_complete(clp, task->tk_status);
7458 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7459 rpc_restart_call_prepare(task);
7462 dprintk("<-- %s\n", __func__);
7465 static void nfs4_free_reclaim_complete_data(void *data)
7467 struct nfs4_reclaim_complete_data *calldata = data;
7472 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7473 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7474 .rpc_call_done = nfs4_reclaim_complete_done,
7475 .rpc_release = nfs4_free_reclaim_complete_data,
7479 * Issue a global reclaim complete.
7481 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7482 struct rpc_cred *cred)
7484 struct nfs4_reclaim_complete_data *calldata;
7485 struct rpc_task *task;
7486 struct rpc_message msg = {
7487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7490 struct rpc_task_setup task_setup_data = {
7491 .rpc_client = clp->cl_rpcclient,
7492 .rpc_message = &msg,
7493 .callback_ops = &nfs4_reclaim_complete_call_ops,
7494 .flags = RPC_TASK_ASYNC,
7496 int status = -ENOMEM;
7498 dprintk("--> %s\n", __func__);
7499 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7500 if (calldata == NULL)
7502 calldata->clp = clp;
7503 calldata->arg.one_fs = 0;
7505 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7506 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7507 msg.rpc_argp = &calldata->arg;
7508 msg.rpc_resp = &calldata->res;
7509 task_setup_data.callback_data = calldata;
7510 task = rpc_run_task(&task_setup_data);
7512 status = PTR_ERR(task);
7515 status = nfs4_wait_for_completion_rpc_task(task);
7517 status = task->tk_status;
7521 dprintk("<-- %s status=%d\n", __func__, status);
7526 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7528 struct nfs4_layoutget *lgp = calldata;
7529 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7530 struct nfs4_session *session = nfs4_get_session(server);
7532 dprintk("--> %s\n", __func__);
7533 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7534 * right now covering the LAYOUTGET we are about to send.
7535 * However, that is not so catastrophic, and there seems
7536 * to be no way to prevent it completely.
7538 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7539 &lgp->res.seq_res, task))
7541 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7542 NFS_I(lgp->args.inode)->layout,
7544 lgp->args.ctx->state)) {
7545 rpc_exit(task, NFS4_OK);
7549 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7551 struct nfs4_layoutget *lgp = calldata;
7552 struct inode *inode = lgp->args.inode;
7553 struct nfs_server *server = NFS_SERVER(inode);
7554 struct pnfs_layout_hdr *lo;
7555 struct nfs4_state *state = NULL;
7556 unsigned long timeo, now, giveup;
7558 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7560 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7563 switch (task->tk_status) {
7567 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7568 * (or clients) writing to the same RAID stripe
7570 case -NFS4ERR_LAYOUTTRYLATER:
7572 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7573 * existing layout before getting a new one).
7575 case -NFS4ERR_RECALLCONFLICT:
7576 timeo = rpc_get_timeout(task->tk_client);
7577 giveup = lgp->args.timestamp + timeo;
7579 if (time_after(giveup, now)) {
7580 unsigned long delay;
7583 * - Not less then NFS4_POLL_RETRY_MIN.
7584 * - One last time a jiffie before we give up
7585 * - exponential backoff (time_now minus start_attempt)
7587 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7588 min((giveup - now - 1),
7589 now - lgp->args.timestamp));
7591 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7593 rpc_delay(task, delay);
7594 task->tk_status = 0;
7595 rpc_restart_call_prepare(task);
7596 goto out; /* Do not call nfs4_async_handle_error() */
7599 case -NFS4ERR_EXPIRED:
7600 case -NFS4ERR_BAD_STATEID:
7601 spin_lock(&inode->i_lock);
7602 lo = NFS_I(inode)->layout;
7603 if (!lo || list_empty(&lo->plh_segs)) {
7604 spin_unlock(&inode->i_lock);
7605 /* If the open stateid was bad, then recover it. */
7606 state = lgp->args.ctx->state;
7611 * Mark the bad layout state as invalid, then retry
7612 * with the current stateid.
7614 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7615 spin_unlock(&inode->i_lock);
7616 pnfs_free_lseg_list(&head);
7618 task->tk_status = 0;
7619 rpc_restart_call_prepare(task);
7622 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
7623 rpc_restart_call_prepare(task);
7625 dprintk("<-- %s\n", __func__);
7628 static size_t max_response_pages(struct nfs_server *server)
7630 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7631 return nfs_page_array_len(0, max_resp_sz);
7634 static void nfs4_free_pages(struct page **pages, size_t size)
7641 for (i = 0; i < size; i++) {
7644 __free_page(pages[i]);
7649 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7651 struct page **pages;
7654 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7656 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7660 for (i = 0; i < size; i++) {
7661 pages[i] = alloc_page(gfp_flags);
7663 dprintk("%s: failed to allocate page\n", __func__);
7664 nfs4_free_pages(pages, size);
7672 static void nfs4_layoutget_release(void *calldata)
7674 struct nfs4_layoutget *lgp = calldata;
7675 struct inode *inode = lgp->args.inode;
7676 struct nfs_server *server = NFS_SERVER(inode);
7677 size_t max_pages = max_response_pages(server);
7679 dprintk("--> %s\n", __func__);
7680 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7681 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7682 put_nfs_open_context(lgp->args.ctx);
7684 dprintk("<-- %s\n", __func__);
7687 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7688 .rpc_call_prepare = nfs4_layoutget_prepare,
7689 .rpc_call_done = nfs4_layoutget_done,
7690 .rpc_release = nfs4_layoutget_release,
7693 struct pnfs_layout_segment *
7694 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7696 struct inode *inode = lgp->args.inode;
7697 struct nfs_server *server = NFS_SERVER(inode);
7698 size_t max_pages = max_response_pages(server);
7699 struct rpc_task *task;
7700 struct rpc_message msg = {
7701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7702 .rpc_argp = &lgp->args,
7703 .rpc_resp = &lgp->res,
7704 .rpc_cred = lgp->cred,
7706 struct rpc_task_setup task_setup_data = {
7707 .rpc_client = server->client,
7708 .rpc_message = &msg,
7709 .callback_ops = &nfs4_layoutget_call_ops,
7710 .callback_data = lgp,
7711 .flags = RPC_TASK_ASYNC,
7713 struct pnfs_layout_segment *lseg = NULL;
7716 dprintk("--> %s\n", __func__);
7718 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7719 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7721 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7722 if (!lgp->args.layout.pages) {
7723 nfs4_layoutget_release(lgp);
7724 return ERR_PTR(-ENOMEM);
7726 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7727 lgp->args.timestamp = jiffies;
7729 lgp->res.layoutp = &lgp->args.layout;
7730 lgp->res.seq_res.sr_slot = NULL;
7731 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7733 task = rpc_run_task(&task_setup_data);
7735 return ERR_CAST(task);
7736 status = nfs4_wait_for_completion_rpc_task(task);
7738 status = task->tk_status;
7739 trace_nfs4_layoutget(lgp->args.ctx,
7743 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7744 if (status == 0 && lgp->res.layoutp->len)
7745 lseg = pnfs_layout_process(lgp);
7747 dprintk("<-- %s status=%d\n", __func__, status);
7749 return ERR_PTR(status);
7754 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7756 struct nfs4_layoutreturn *lrp = calldata;
7758 dprintk("--> %s\n", __func__);
7759 nfs41_setup_sequence(lrp->clp->cl_session,
7760 &lrp->args.seq_args,
7765 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7767 struct nfs4_layoutreturn *lrp = calldata;
7768 struct nfs_server *server;
7770 dprintk("--> %s\n", __func__);
7772 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7775 server = NFS_SERVER(lrp->args.inode);
7776 switch (task->tk_status) {
7778 task->tk_status = 0;
7781 case -NFS4ERR_DELAY:
7782 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
7784 rpc_restart_call_prepare(task);
7787 dprintk("<-- %s\n", __func__);
7790 static void nfs4_layoutreturn_release(void *calldata)
7792 struct nfs4_layoutreturn *lrp = calldata;
7793 struct pnfs_layout_hdr *lo = lrp->args.layout;
7795 dprintk("--> %s\n", __func__);
7796 spin_lock(&lo->plh_inode->i_lock);
7797 if (lrp->res.lrs_present)
7798 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7799 clear_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
7800 lo->plh_block_lgets--;
7801 spin_unlock(&lo->plh_inode->i_lock);
7802 pnfs_put_layout_hdr(lrp->args.layout);
7804 dprintk("<-- %s\n", __func__);
7807 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7808 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7809 .rpc_call_done = nfs4_layoutreturn_done,
7810 .rpc_release = nfs4_layoutreturn_release,
7813 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7815 struct rpc_task *task;
7816 struct rpc_message msg = {
7817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7818 .rpc_argp = &lrp->args,
7819 .rpc_resp = &lrp->res,
7820 .rpc_cred = lrp->cred,
7822 struct rpc_task_setup task_setup_data = {
7823 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7824 .rpc_message = &msg,
7825 .callback_ops = &nfs4_layoutreturn_call_ops,
7826 .callback_data = lrp,
7830 dprintk("--> %s\n", __func__);
7831 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7832 task = rpc_run_task(&task_setup_data);
7834 return PTR_ERR(task);
7835 status = task->tk_status;
7836 trace_nfs4_layoutreturn(lrp->args.inode, status);
7837 dprintk("<-- %s status=%d\n", __func__, status);
7843 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7844 struct pnfs_device *pdev,
7845 struct rpc_cred *cred)
7847 struct nfs4_getdeviceinfo_args args = {
7850 struct nfs4_getdeviceinfo_res res = {
7853 struct rpc_message msg = {
7854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7861 dprintk("--> %s\n", __func__);
7862 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7863 dprintk("<-- %s status=%d\n", __func__, status);
7868 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7869 struct pnfs_device *pdev,
7870 struct rpc_cred *cred)
7872 struct nfs4_exception exception = { };
7876 err = nfs4_handle_exception(server,
7877 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7879 } while (exception.retry);
7882 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7884 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7886 struct nfs4_layoutcommit_data *data = calldata;
7887 struct nfs_server *server = NFS_SERVER(data->args.inode);
7888 struct nfs4_session *session = nfs4_get_session(server);
7890 nfs41_setup_sequence(session,
7891 &data->args.seq_args,
7897 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7899 struct nfs4_layoutcommit_data *data = calldata;
7900 struct nfs_server *server = NFS_SERVER(data->args.inode);
7902 if (!nfs41_sequence_done(task, &data->res.seq_res))
7905 switch (task->tk_status) { /* Just ignore these failures */
7906 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7907 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7908 case -NFS4ERR_BADLAYOUT: /* no layout */
7909 case -NFS4ERR_GRACE: /* loca_recalim always false */
7910 task->tk_status = 0;
7914 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
7915 rpc_restart_call_prepare(task);
7921 static void nfs4_layoutcommit_release(void *calldata)
7923 struct nfs4_layoutcommit_data *data = calldata;
7925 pnfs_cleanup_layoutcommit(data);
7926 nfs_post_op_update_inode_force_wcc(data->args.inode,
7928 put_rpccred(data->cred);
7932 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7933 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7934 .rpc_call_done = nfs4_layoutcommit_done,
7935 .rpc_release = nfs4_layoutcommit_release,
7939 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7941 struct rpc_message msg = {
7942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7943 .rpc_argp = &data->args,
7944 .rpc_resp = &data->res,
7945 .rpc_cred = data->cred,
7947 struct rpc_task_setup task_setup_data = {
7948 .task = &data->task,
7949 .rpc_client = NFS_CLIENT(data->args.inode),
7950 .rpc_message = &msg,
7951 .callback_ops = &nfs4_layoutcommit_ops,
7952 .callback_data = data,
7953 .flags = RPC_TASK_ASYNC,
7955 struct rpc_task *task;
7958 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7959 "lbw: %llu inode %lu\n",
7960 data->task.tk_pid, sync,
7961 data->args.lastbytewritten,
7962 data->args.inode->i_ino);
7964 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7965 task = rpc_run_task(&task_setup_data);
7967 return PTR_ERR(task);
7970 status = nfs4_wait_for_completion_rpc_task(task);
7973 status = task->tk_status;
7974 trace_nfs4_layoutcommit(data->args.inode, status);
7976 dprintk("%s: status %d\n", __func__, status);
7982 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7983 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7986 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7987 struct nfs_fsinfo *info,
7988 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7990 struct nfs41_secinfo_no_name_args args = {
7991 .style = SECINFO_STYLE_CURRENT_FH,
7993 struct nfs4_secinfo_res res = {
7996 struct rpc_message msg = {
7997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8001 struct rpc_clnt *clnt = server->client;
8002 struct rpc_cred *cred = NULL;
8005 if (use_integrity) {
8006 clnt = server->nfs_client->cl_rpcclient;
8007 cred = nfs4_get_clid_cred(server->nfs_client);
8008 msg.rpc_cred = cred;
8011 dprintk("--> %s\n", __func__);
8012 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8014 dprintk("<-- %s status=%d\n", __func__, status);
8023 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8024 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8026 struct nfs4_exception exception = { };
8029 /* first try using integrity protection */
8030 err = -NFS4ERR_WRONGSEC;
8032 /* try to use integrity protection with machine cred */
8033 if (_nfs4_is_integrity_protected(server->nfs_client))
8034 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8038 * if unable to use integrity protection, or SECINFO with
8039 * integrity protection returns NFS4ERR_WRONGSEC (which is
8040 * disallowed by spec, but exists in deployed servers) use
8041 * the current filesystem's rpc_client and the user cred.
8043 if (err == -NFS4ERR_WRONGSEC)
8044 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8049 case -NFS4ERR_WRONGSEC:
8053 err = nfs4_handle_exception(server, err, &exception);
8055 } while (exception.retry);
8061 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8062 struct nfs_fsinfo *info)
8066 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8067 struct nfs4_secinfo_flavors *flavors;
8068 struct nfs4_secinfo4 *secinfo;
8071 page = alloc_page(GFP_KERNEL);
8077 flavors = page_address(page);
8078 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8081 * Fall back on "guess and check" method if
8082 * the server doesn't support SECINFO_NO_NAME
8084 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8085 err = nfs4_find_root_sec(server, fhandle, info);
8091 for (i = 0; i < flavors->num_flavors; i++) {
8092 secinfo = &flavors->flavors[i];
8094 switch (secinfo->flavor) {
8098 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8099 &secinfo->flavor_info);
8102 flavor = RPC_AUTH_MAXFLAVOR;
8106 if (!nfs_auth_info_match(&server->auth_info, flavor))
8107 flavor = RPC_AUTH_MAXFLAVOR;
8109 if (flavor != RPC_AUTH_MAXFLAVOR) {
8110 err = nfs4_lookup_root_sec(server, fhandle,
8117 if (flavor == RPC_AUTH_MAXFLAVOR)
8128 static int _nfs41_test_stateid(struct nfs_server *server,
8129 nfs4_stateid *stateid,
8130 struct rpc_cred *cred)
8133 struct nfs41_test_stateid_args args = {
8136 struct nfs41_test_stateid_res res;
8137 struct rpc_message msg = {
8138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8143 struct rpc_clnt *rpc_client = server->client;
8145 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8148 dprintk("NFS call test_stateid %p\n", stateid);
8149 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8150 nfs4_set_sequence_privileged(&args.seq_args);
8151 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8152 &args.seq_args, &res.seq_res);
8153 if (status != NFS_OK) {
8154 dprintk("NFS reply test_stateid: failed, %d\n", status);
8157 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8162 * nfs41_test_stateid - perform a TEST_STATEID operation
8164 * @server: server / transport on which to perform the operation
8165 * @stateid: state ID to test
8168 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8169 * Otherwise a negative NFS4ERR value is returned if the operation
8170 * failed or the state ID is not currently valid.
8172 static int nfs41_test_stateid(struct nfs_server *server,
8173 nfs4_stateid *stateid,
8174 struct rpc_cred *cred)
8176 struct nfs4_exception exception = { };
8179 err = _nfs41_test_stateid(server, stateid, cred);
8180 if (err != -NFS4ERR_DELAY)
8182 nfs4_handle_exception(server, err, &exception);
8183 } while (exception.retry);
8187 struct nfs_free_stateid_data {
8188 struct nfs_server *server;
8189 struct nfs41_free_stateid_args args;
8190 struct nfs41_free_stateid_res res;
8193 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8195 struct nfs_free_stateid_data *data = calldata;
8196 nfs41_setup_sequence(nfs4_get_session(data->server),
8197 &data->args.seq_args,
8202 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8204 struct nfs_free_stateid_data *data = calldata;
8206 nfs41_sequence_done(task, &data->res.seq_res);
8208 switch (task->tk_status) {
8209 case -NFS4ERR_DELAY:
8210 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8211 rpc_restart_call_prepare(task);
8215 static void nfs41_free_stateid_release(void *calldata)
8220 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8221 .rpc_call_prepare = nfs41_free_stateid_prepare,
8222 .rpc_call_done = nfs41_free_stateid_done,
8223 .rpc_release = nfs41_free_stateid_release,
8226 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8227 nfs4_stateid *stateid,
8228 struct rpc_cred *cred,
8231 struct rpc_message msg = {
8232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8235 struct rpc_task_setup task_setup = {
8236 .rpc_client = server->client,
8237 .rpc_message = &msg,
8238 .callback_ops = &nfs41_free_stateid_ops,
8239 .flags = RPC_TASK_ASYNC,
8241 struct nfs_free_stateid_data *data;
8243 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8244 &task_setup.rpc_client, &msg);
8246 dprintk("NFS call free_stateid %p\n", stateid);
8247 data = kmalloc(sizeof(*data), GFP_NOFS);
8249 return ERR_PTR(-ENOMEM);
8250 data->server = server;
8251 nfs4_stateid_copy(&data->args.stateid, stateid);
8253 task_setup.callback_data = data;
8255 msg.rpc_argp = &data->args;
8256 msg.rpc_resp = &data->res;
8257 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8259 nfs4_set_sequence_privileged(&data->args.seq_args);
8261 return rpc_run_task(&task_setup);
8265 * nfs41_free_stateid - perform a FREE_STATEID operation
8267 * @server: server / transport on which to perform the operation
8268 * @stateid: state ID to release
8271 * Returns NFS_OK if the server freed "stateid". Otherwise a
8272 * negative NFS4ERR value is returned.
8274 static int nfs41_free_stateid(struct nfs_server *server,
8275 nfs4_stateid *stateid,
8276 struct rpc_cred *cred)
8278 struct rpc_task *task;
8281 task = _nfs41_free_stateid(server, stateid, cred, true);
8283 return PTR_ERR(task);
8284 ret = rpc_wait_for_completion_task(task);
8286 ret = task->tk_status;
8292 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8294 struct rpc_task *task;
8295 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8297 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8298 nfs4_free_lock_state(server, lsp);
8304 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8305 const nfs4_stateid *s2)
8307 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8310 if (s1->seqid == s2->seqid)
8312 if (s1->seqid == 0 || s2->seqid == 0)
8318 #endif /* CONFIG_NFS_V4_1 */
8320 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8321 const nfs4_stateid *s2)
8323 return nfs4_stateid_match(s1, s2);
8327 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8328 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8329 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8330 .recover_open = nfs4_open_reclaim,
8331 .recover_lock = nfs4_lock_reclaim,
8332 .establish_clid = nfs4_init_clientid,
8333 .detect_trunking = nfs40_discover_server_trunking,
8336 #if defined(CONFIG_NFS_V4_1)
8337 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8338 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8339 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8340 .recover_open = nfs4_open_reclaim,
8341 .recover_lock = nfs4_lock_reclaim,
8342 .establish_clid = nfs41_init_clientid,
8343 .reclaim_complete = nfs41_proc_reclaim_complete,
8344 .detect_trunking = nfs41_discover_server_trunking,
8346 #endif /* CONFIG_NFS_V4_1 */
8348 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8349 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8350 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8351 .recover_open = nfs40_open_expired,
8352 .recover_lock = nfs4_lock_expired,
8353 .establish_clid = nfs4_init_clientid,
8356 #if defined(CONFIG_NFS_V4_1)
8357 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8358 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8359 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8360 .recover_open = nfs41_open_expired,
8361 .recover_lock = nfs41_lock_expired,
8362 .establish_clid = nfs41_init_clientid,
8364 #endif /* CONFIG_NFS_V4_1 */
8366 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8367 .sched_state_renewal = nfs4_proc_async_renew,
8368 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8369 .renew_lease = nfs4_proc_renew,
8372 #if defined(CONFIG_NFS_V4_1)
8373 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8374 .sched_state_renewal = nfs41_proc_async_sequence,
8375 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8376 .renew_lease = nfs4_proc_sequence,
8380 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8381 .get_locations = _nfs40_proc_get_locations,
8382 .fsid_present = _nfs40_proc_fsid_present,
8385 #if defined(CONFIG_NFS_V4_1)
8386 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8387 .get_locations = _nfs41_proc_get_locations,
8388 .fsid_present = _nfs41_proc_fsid_present,
8390 #endif /* CONFIG_NFS_V4_1 */
8392 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8394 .init_caps = NFS_CAP_READDIRPLUS
8395 | NFS_CAP_ATOMIC_OPEN
8396 | NFS_CAP_CHANGE_ATTR
8397 | NFS_CAP_POSIX_LOCK,
8398 .init_client = nfs40_init_client,
8399 .shutdown_client = nfs40_shutdown_client,
8400 .match_stateid = nfs4_match_stateid,
8401 .find_root_sec = nfs4_find_root_sec,
8402 .free_lock_state = nfs4_release_lockowner,
8403 .call_sync_ops = &nfs40_call_sync_ops,
8404 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8405 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8406 .state_renewal_ops = &nfs40_state_renewal_ops,
8407 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8410 #if defined(CONFIG_NFS_V4_1)
8411 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8413 .init_caps = NFS_CAP_READDIRPLUS
8414 | NFS_CAP_ATOMIC_OPEN
8415 | NFS_CAP_CHANGE_ATTR
8416 | NFS_CAP_POSIX_LOCK
8417 | NFS_CAP_STATEID_NFSV41
8418 | NFS_CAP_ATOMIC_OPEN_V1,
8419 .init_client = nfs41_init_client,
8420 .shutdown_client = nfs41_shutdown_client,
8421 .match_stateid = nfs41_match_stateid,
8422 .find_root_sec = nfs41_find_root_sec,
8423 .free_lock_state = nfs41_free_lock_state,
8424 .call_sync_ops = &nfs41_call_sync_ops,
8425 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8426 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8427 .state_renewal_ops = &nfs41_state_renewal_ops,
8428 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8432 #if defined(CONFIG_NFS_V4_2)
8433 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8435 .init_caps = NFS_CAP_READDIRPLUS
8436 | NFS_CAP_ATOMIC_OPEN
8437 | NFS_CAP_CHANGE_ATTR
8438 | NFS_CAP_POSIX_LOCK
8439 | NFS_CAP_STATEID_NFSV41
8440 | NFS_CAP_ATOMIC_OPEN_V1
8442 | NFS_CAP_DEALLOCATE
8444 .init_client = nfs41_init_client,
8445 .shutdown_client = nfs41_shutdown_client,
8446 .match_stateid = nfs41_match_stateid,
8447 .find_root_sec = nfs41_find_root_sec,
8448 .free_lock_state = nfs41_free_lock_state,
8449 .call_sync_ops = &nfs41_call_sync_ops,
8450 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8451 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8452 .state_renewal_ops = &nfs41_state_renewal_ops,
8456 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8457 [0] = &nfs_v4_0_minor_ops,
8458 #if defined(CONFIG_NFS_V4_1)
8459 [1] = &nfs_v4_1_minor_ops,
8461 #if defined(CONFIG_NFS_V4_2)
8462 [2] = &nfs_v4_2_minor_ops,
8466 static const struct inode_operations nfs4_dir_inode_operations = {
8467 .create = nfs_create,
8468 .lookup = nfs_lookup,
8469 .atomic_open = nfs_atomic_open,
8471 .unlink = nfs_unlink,
8472 .symlink = nfs_symlink,
8476 .rename = nfs_rename,
8477 .permission = nfs_permission,
8478 .getattr = nfs_getattr,
8479 .setattr = nfs_setattr,
8480 .getxattr = generic_getxattr,
8481 .setxattr = generic_setxattr,
8482 .listxattr = generic_listxattr,
8483 .removexattr = generic_removexattr,
8486 static const struct inode_operations nfs4_file_inode_operations = {
8487 .permission = nfs_permission,
8488 .getattr = nfs_getattr,
8489 .setattr = nfs_setattr,
8490 .getxattr = generic_getxattr,
8491 .setxattr = generic_setxattr,
8492 .listxattr = generic_listxattr,
8493 .removexattr = generic_removexattr,
8496 const struct nfs_rpc_ops nfs_v4_clientops = {
8497 .version = 4, /* protocol version */
8498 .dentry_ops = &nfs4_dentry_operations,
8499 .dir_inode_ops = &nfs4_dir_inode_operations,
8500 .file_inode_ops = &nfs4_file_inode_operations,
8501 .file_ops = &nfs4_file_operations,
8502 .getroot = nfs4_proc_get_root,
8503 .submount = nfs4_submount,
8504 .try_mount = nfs4_try_mount,
8505 .getattr = nfs4_proc_getattr,
8506 .setattr = nfs4_proc_setattr,
8507 .lookup = nfs4_proc_lookup,
8508 .access = nfs4_proc_access,
8509 .readlink = nfs4_proc_readlink,
8510 .create = nfs4_proc_create,
8511 .remove = nfs4_proc_remove,
8512 .unlink_setup = nfs4_proc_unlink_setup,
8513 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8514 .unlink_done = nfs4_proc_unlink_done,
8515 .rename_setup = nfs4_proc_rename_setup,
8516 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8517 .rename_done = nfs4_proc_rename_done,
8518 .link = nfs4_proc_link,
8519 .symlink = nfs4_proc_symlink,
8520 .mkdir = nfs4_proc_mkdir,
8521 .rmdir = nfs4_proc_remove,
8522 .readdir = nfs4_proc_readdir,
8523 .mknod = nfs4_proc_mknod,
8524 .statfs = nfs4_proc_statfs,
8525 .fsinfo = nfs4_proc_fsinfo,
8526 .pathconf = nfs4_proc_pathconf,
8527 .set_capabilities = nfs4_server_capabilities,
8528 .decode_dirent = nfs4_decode_dirent,
8529 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8530 .read_setup = nfs4_proc_read_setup,
8531 .read_done = nfs4_read_done,
8532 .write_setup = nfs4_proc_write_setup,
8533 .write_done = nfs4_write_done,
8534 .commit_setup = nfs4_proc_commit_setup,
8535 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8536 .commit_done = nfs4_commit_done,
8537 .lock = nfs4_proc_lock,
8538 .clear_acl_cache = nfs4_zap_acl_attr,
8539 .close_context = nfs4_close_context,
8540 .open_context = nfs4_atomic_open,
8541 .have_delegation = nfs4_have_delegation,
8542 .return_delegation = nfs4_inode_return_delegation,
8543 .alloc_client = nfs4_alloc_client,
8544 .init_client = nfs4_init_client,
8545 .free_client = nfs4_free_client,
8546 .create_server = nfs4_create_server,
8547 .clone_server = nfs_clone_server,
8550 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8551 .prefix = XATTR_NAME_NFSV4_ACL,
8552 .list = nfs4_xattr_list_nfs4_acl,
8553 .get = nfs4_xattr_get_nfs4_acl,
8554 .set = nfs4_xattr_set_nfs4_acl,
8557 const struct xattr_handler *nfs4_xattr_handlers[] = {
8558 &nfs4_xattr_nfs4_acl_handler,
8559 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8560 &nfs4_xattr_nfs4_label_handler,