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_ACCESS:
163 case -NFS4ERR_FILE_OPEN:
166 dprintk("%s could not handle NFSv4 error %d\n",
174 * This is our standard bitmap for GETATTR requests.
176 const u32 nfs4_fattr_bitmap[3] = {
178 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_FILEID,
183 | FATTR4_WORD1_NUMLINKS
185 | FATTR4_WORD1_OWNER_GROUP
186 | FATTR4_WORD1_RAWDEV
187 | FATTR4_WORD1_SPACE_USED
188 | FATTR4_WORD1_TIME_ACCESS
189 | FATTR4_WORD1_TIME_METADATA
190 | FATTR4_WORD1_TIME_MODIFY,
191 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
196 static const u32 nfs4_pnfs_open_bitmap[3] = {
198 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_FILEID,
203 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
214 static const u32 nfs4_open_noattr_bitmap[3] = {
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
220 const u32 nfs4_statfs_bitmap[3] = {
221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
229 const u32 nfs4_pathconf_bitmap[3] = {
231 | FATTR4_WORD0_MAXNAME,
235 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
239 FATTR4_WORD1_TIME_DELTA
240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
244 const u32 nfs4_fs_locations_bitmap[3] = {
246 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
252 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
263 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
264 struct nfs4_readdir_arg *readdir)
269 readdir->cookie = cookie;
270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
286 start = p = kmap_atomic(*readdir->pages);
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
298 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
310 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
314 kunmap_atomic(start);
317 static long nfs4_update_delay(long *timeout)
321 return NFS4_POLL_RETRY_MAX;
323 *timeout = NFS4_POLL_RETRY_MIN;
324 if (*timeout > NFS4_POLL_RETRY_MAX)
325 *timeout = NFS4_POLL_RETRY_MAX;
331 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
337 freezable_schedule_timeout_killable_unsafe(
338 nfs4_update_delay(timeout));
339 if (fatal_signal_pending(current))
344 /* This is the error handling routine for processes that are allowed
347 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
349 struct nfs_client *clp = server->nfs_client;
350 struct nfs4_state *state = exception->state;
351 struct inode *inode = exception->inode;
354 exception->retry = 0;
358 case -NFS4ERR_OPENMODE:
359 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
360 nfs4_inode_return_delegation(inode);
361 exception->retry = 1;
366 ret = nfs4_schedule_stateid_recovery(server, state);
369 goto wait_on_recovery;
370 case -NFS4ERR_DELEG_REVOKED:
371 case -NFS4ERR_ADMIN_REVOKED:
372 case -NFS4ERR_BAD_STATEID:
375 ret = nfs4_schedule_stateid_recovery(server, state);
378 goto wait_on_recovery;
379 case -NFS4ERR_EXPIRED:
381 ret = nfs4_schedule_stateid_recovery(server, state);
385 case -NFS4ERR_STALE_STATEID:
386 case -NFS4ERR_STALE_CLIENTID:
387 nfs4_schedule_lease_recovery(clp);
388 goto wait_on_recovery;
390 ret = nfs4_schedule_migration_recovery(server);
393 goto wait_on_recovery;
394 case -NFS4ERR_LEASE_MOVED:
395 nfs4_schedule_lease_moved_recovery(clp);
396 goto wait_on_recovery;
397 #if defined(CONFIG_NFS_V4_1)
398 case -NFS4ERR_BADSESSION:
399 case -NFS4ERR_BADSLOT:
400 case -NFS4ERR_BAD_HIGH_SLOT:
401 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
402 case -NFS4ERR_DEADSESSION:
403 case -NFS4ERR_SEQ_FALSE_RETRY:
404 case -NFS4ERR_SEQ_MISORDERED:
405 dprintk("%s ERROR: %d Reset session\n", __func__,
407 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
408 goto wait_on_recovery;
409 #endif /* defined(CONFIG_NFS_V4_1) */
410 case -NFS4ERR_FILE_OPEN:
411 if (exception->timeout > HZ) {
412 /* We have retried a decent amount, time to
420 ret = nfs4_delay(server->client, &exception->timeout);
423 case -NFS4ERR_RETRY_UNCACHED_REP:
424 case -NFS4ERR_OLD_STATEID:
425 exception->retry = 1;
427 case -NFS4ERR_BADOWNER:
428 /* The following works around a Linux server bug! */
429 case -NFS4ERR_BADNAME:
430 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
431 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
432 exception->retry = 1;
433 printk(KERN_WARNING "NFS: v4 server %s "
434 "does not accept raw "
436 "Reenabling the idmapper.\n",
437 server->nfs_client->cl_hostname);
440 /* We failed to handle the error */
441 return nfs4_map_errors(ret);
443 ret = nfs4_wait_clnt_recover(clp);
444 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
447 exception->retry = 1;
452 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
453 * or 'false' otherwise.
455 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
457 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
459 if (flavor == RPC_AUTH_GSS_KRB5I ||
460 flavor == RPC_AUTH_GSS_KRB5P)
466 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
468 spin_lock(&clp->cl_lock);
469 if (time_before(clp->cl_last_renewal,timestamp))
470 clp->cl_last_renewal = timestamp;
471 spin_unlock(&clp->cl_lock);
474 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
476 do_renew_lease(server->nfs_client, timestamp);
479 struct nfs4_call_sync_data {
480 const struct nfs_server *seq_server;
481 struct nfs4_sequence_args *seq_args;
482 struct nfs4_sequence_res *seq_res;
485 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
486 struct nfs4_sequence_res *res, int cache_reply)
488 args->sa_slot = NULL;
489 args->sa_cache_this = cache_reply;
490 args->sa_privileged = 0;
495 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
497 args->sa_privileged = 1;
500 static int nfs40_setup_sequence(const struct nfs_server *server,
501 struct nfs4_sequence_args *args,
502 struct nfs4_sequence_res *res,
503 struct rpc_task *task)
505 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
506 struct nfs4_slot *slot;
508 /* slot already allocated? */
509 if (res->sr_slot != NULL)
512 spin_lock(&tbl->slot_tbl_lock);
513 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
516 slot = nfs4_alloc_slot(tbl);
518 if (slot == ERR_PTR(-ENOMEM))
519 task->tk_timeout = HZ >> 2;
522 spin_unlock(&tbl->slot_tbl_lock);
524 args->sa_slot = slot;
528 rpc_call_start(task);
532 if (args->sa_privileged)
533 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
534 NULL, RPC_PRIORITY_PRIVILEGED);
536 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
537 spin_unlock(&tbl->slot_tbl_lock);
541 static int nfs40_sequence_done(struct rpc_task *task,
542 struct nfs4_sequence_res *res)
544 struct nfs4_slot *slot = res->sr_slot;
545 struct nfs4_slot_table *tbl;
551 spin_lock(&tbl->slot_tbl_lock);
552 if (!nfs41_wake_and_assign_slot(tbl, slot))
553 nfs4_free_slot(tbl, slot);
554 spin_unlock(&tbl->slot_tbl_lock);
561 #if defined(CONFIG_NFS_V4_1)
563 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
565 struct nfs4_session *session;
566 struct nfs4_slot_table *tbl;
567 struct nfs4_slot *slot = res->sr_slot;
568 bool send_new_highest_used_slotid = false;
571 session = tbl->session;
573 spin_lock(&tbl->slot_tbl_lock);
574 /* Be nice to the server: try to ensure that the last transmitted
575 * value for highest_user_slotid <= target_highest_slotid
577 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
578 send_new_highest_used_slotid = true;
580 if (nfs41_wake_and_assign_slot(tbl, slot)) {
581 send_new_highest_used_slotid = false;
584 nfs4_free_slot(tbl, slot);
586 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
587 send_new_highest_used_slotid = false;
589 spin_unlock(&tbl->slot_tbl_lock);
591 if (send_new_highest_used_slotid)
592 nfs41_server_notify_highest_slotid_update(session->clp);
595 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
597 struct nfs4_session *session;
598 struct nfs4_slot *slot = res->sr_slot;
599 struct nfs_client *clp;
600 bool interrupted = false;
605 /* don't increment the sequence number if the task wasn't sent */
606 if (!RPC_WAS_SENT(task))
609 session = slot->table->session;
611 if (slot->interrupted) {
612 slot->interrupted = 0;
616 trace_nfs4_sequence_done(session, res);
617 /* Check the SEQUENCE operation status */
618 switch (res->sr_status) {
620 /* Update the slot's sequence and clientid lease timer */
623 do_renew_lease(clp, res->sr_timestamp);
624 /* Check sequence flags */
625 if (res->sr_status_flags != 0)
626 nfs4_schedule_lease_recovery(clp);
627 nfs41_update_target_slotid(slot->table, slot, res);
631 * sr_status remains 1 if an RPC level error occurred.
632 * The server may or may not have processed the sequence
634 * Mark the slot as having hosted an interrupted RPC call.
636 slot->interrupted = 1;
639 /* The server detected a resend of the RPC call and
640 * returned NFS4ERR_DELAY as per Section 2.10.6.2
643 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
648 case -NFS4ERR_BADSLOT:
650 * The slot id we used was probably retired. Try again
651 * using a different slot id.
654 case -NFS4ERR_SEQ_MISORDERED:
656 * Was the last operation on this sequence interrupted?
657 * If so, retry after bumping the sequence number.
664 * Could this slot have been previously retired?
665 * If so, then the server may be expecting seq_nr = 1!
667 if (slot->seq_nr != 1) {
672 case -NFS4ERR_SEQ_FALSE_RETRY:
676 /* Just update the slot sequence no. */
680 /* The session may be reset by one of the error handlers. */
681 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
682 nfs41_sequence_free_slot(res);
686 if (rpc_restart_call_prepare(task)) {
692 if (!rpc_restart_call(task))
694 rpc_delay(task, NFS4_POLL_RETRY_MAX);
697 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
699 static int nfs4_sequence_done(struct rpc_task *task,
700 struct nfs4_sequence_res *res)
702 if (res->sr_slot == NULL)
704 if (!res->sr_slot->table->session)
705 return nfs40_sequence_done(task, res);
706 return nfs41_sequence_done(task, res);
709 int nfs41_setup_sequence(struct nfs4_session *session,
710 struct nfs4_sequence_args *args,
711 struct nfs4_sequence_res *res,
712 struct rpc_task *task)
714 struct nfs4_slot *slot;
715 struct nfs4_slot_table *tbl;
717 dprintk("--> %s\n", __func__);
718 /* slot already allocated? */
719 if (res->sr_slot != NULL)
722 tbl = &session->fc_slot_table;
724 task->tk_timeout = 0;
726 spin_lock(&tbl->slot_tbl_lock);
727 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
728 !args->sa_privileged) {
729 /* The state manager will wait until the slot table is empty */
730 dprintk("%s session is draining\n", __func__);
734 slot = nfs4_alloc_slot(tbl);
736 /* If out of memory, try again in 1/4 second */
737 if (slot == ERR_PTR(-ENOMEM))
738 task->tk_timeout = HZ >> 2;
739 dprintk("<-- %s: no free slots\n", __func__);
742 spin_unlock(&tbl->slot_tbl_lock);
744 args->sa_slot = slot;
746 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
747 slot->slot_nr, slot->seq_nr);
750 res->sr_timestamp = jiffies;
751 res->sr_status_flags = 0;
753 * sr_status is only set in decode_sequence, and so will remain
754 * set to 1 if an rpc level failure occurs.
757 trace_nfs4_setup_sequence(session, args);
759 rpc_call_start(task);
762 /* Privileged tasks are queued with top priority */
763 if (args->sa_privileged)
764 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
765 NULL, RPC_PRIORITY_PRIVILEGED);
767 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
768 spin_unlock(&tbl->slot_tbl_lock);
771 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
773 static int nfs4_setup_sequence(const struct nfs_server *server,
774 struct nfs4_sequence_args *args,
775 struct nfs4_sequence_res *res,
776 struct rpc_task *task)
778 struct nfs4_session *session = nfs4_get_session(server);
782 return nfs40_setup_sequence(server, args, res, task);
784 dprintk("--> %s clp %p session %p sr_slot %u\n",
785 __func__, session->clp, session, res->sr_slot ?
786 res->sr_slot->slot_nr : NFS4_NO_SLOT);
788 ret = nfs41_setup_sequence(session, args, res, task);
790 dprintk("<-- %s status=%d\n", __func__, ret);
794 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
796 struct nfs4_call_sync_data *data = calldata;
797 struct nfs4_session *session = nfs4_get_session(data->seq_server);
799 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
801 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
804 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
806 struct nfs4_call_sync_data *data = calldata;
808 nfs41_sequence_done(task, data->seq_res);
811 static const struct rpc_call_ops nfs41_call_sync_ops = {
812 .rpc_call_prepare = nfs41_call_sync_prepare,
813 .rpc_call_done = nfs41_call_sync_done,
816 #else /* !CONFIG_NFS_V4_1 */
818 static int nfs4_setup_sequence(const struct nfs_server *server,
819 struct nfs4_sequence_args *args,
820 struct nfs4_sequence_res *res,
821 struct rpc_task *task)
823 return nfs40_setup_sequence(server, args, res, task);
826 static int nfs4_sequence_done(struct rpc_task *task,
827 struct nfs4_sequence_res *res)
829 return nfs40_sequence_done(task, res);
832 #endif /* !CONFIG_NFS_V4_1 */
834 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
836 struct nfs4_call_sync_data *data = calldata;
837 nfs4_setup_sequence(data->seq_server,
838 data->seq_args, data->seq_res, task);
841 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
843 struct nfs4_call_sync_data *data = calldata;
844 nfs4_sequence_done(task, data->seq_res);
847 static const struct rpc_call_ops nfs40_call_sync_ops = {
848 .rpc_call_prepare = nfs40_call_sync_prepare,
849 .rpc_call_done = nfs40_call_sync_done,
852 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
853 struct nfs_server *server,
854 struct rpc_message *msg,
855 struct nfs4_sequence_args *args,
856 struct nfs4_sequence_res *res)
859 struct rpc_task *task;
860 struct nfs_client *clp = server->nfs_client;
861 struct nfs4_call_sync_data data = {
862 .seq_server = server,
866 struct rpc_task_setup task_setup = {
869 .callback_ops = clp->cl_mvops->call_sync_ops,
870 .callback_data = &data
873 task = rpc_run_task(&task_setup);
877 ret = task->tk_status;
883 int nfs4_call_sync(struct rpc_clnt *clnt,
884 struct nfs_server *server,
885 struct rpc_message *msg,
886 struct nfs4_sequence_args *args,
887 struct nfs4_sequence_res *res,
890 nfs4_init_sequence(args, res, cache_reply);
891 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
894 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
896 struct nfs_inode *nfsi = NFS_I(dir);
898 spin_lock(&dir->i_lock);
899 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
900 if (!cinfo->atomic || cinfo->before != dir->i_version)
901 nfs_force_lookup_revalidate(dir);
902 dir->i_version = cinfo->after;
903 nfs_fscache_invalidate(dir);
904 spin_unlock(&dir->i_lock);
907 struct nfs4_opendata {
909 struct nfs_openargs o_arg;
910 struct nfs_openres o_res;
911 struct nfs_open_confirmargs c_arg;
912 struct nfs_open_confirmres c_res;
913 struct nfs4_string owner_name;
914 struct nfs4_string group_name;
915 struct nfs_fattr f_attr;
916 struct nfs4_label *f_label;
918 struct dentry *dentry;
919 struct nfs4_state_owner *owner;
920 struct nfs4_state *state;
922 unsigned long timestamp;
923 unsigned int rpc_done : 1;
924 unsigned int file_created : 1;
925 unsigned int is_recover : 1;
930 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
931 int err, struct nfs4_exception *exception)
935 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
937 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
938 exception->retry = 1;
942 static enum open_claim_type4
943 nfs4_map_atomic_open_claim(struct nfs_server *server,
944 enum open_claim_type4 claim)
946 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
951 case NFS4_OPEN_CLAIM_FH:
952 return NFS4_OPEN_CLAIM_NULL;
953 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
954 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
955 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
956 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
960 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
962 p->o_res.f_attr = &p->f_attr;
963 p->o_res.f_label = p->f_label;
964 p->o_res.seqid = p->o_arg.seqid;
965 p->c_res.seqid = p->c_arg.seqid;
966 p->o_res.server = p->o_arg.server;
967 p->o_res.access_request = p->o_arg.access;
968 nfs_fattr_init(&p->f_attr);
969 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
972 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
973 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
974 const struct iattr *attrs,
975 struct nfs4_label *label,
976 enum open_claim_type4 claim,
979 struct dentry *parent = dget_parent(dentry);
980 struct inode *dir = parent->d_inode;
981 struct nfs_server *server = NFS_SERVER(dir);
982 struct nfs4_opendata *p;
984 p = kzalloc(sizeof(*p), gfp_mask);
988 p->f_label = nfs4_label_alloc(server, gfp_mask);
989 if (IS_ERR(p->f_label))
992 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
993 if (p->o_arg.seqid == NULL)
995 nfs_sb_active(dentry->d_sb);
996 p->dentry = dget(dentry);
999 atomic_inc(&sp->so_count);
1000 p->o_arg.open_flags = flags;
1001 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1002 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1003 * will return permission denied for all bits until close */
1004 if (!(flags & O_EXCL)) {
1005 /* ask server to check for all possible rights as results
1007 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1008 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1010 p->o_arg.clientid = server->nfs_client->cl_clientid;
1011 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1012 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1013 p->o_arg.name = &dentry->d_name;
1014 p->o_arg.server = server;
1015 p->o_arg.bitmask = nfs4_bitmask(server, label);
1016 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1017 p->o_arg.label = label;
1018 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1019 switch (p->o_arg.claim) {
1020 case NFS4_OPEN_CLAIM_NULL:
1021 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1022 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1023 p->o_arg.fh = NFS_FH(dir);
1025 case NFS4_OPEN_CLAIM_PREVIOUS:
1026 case NFS4_OPEN_CLAIM_FH:
1027 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1028 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1029 p->o_arg.fh = NFS_FH(dentry->d_inode);
1031 if (attrs != NULL && attrs->ia_valid != 0) {
1034 p->o_arg.u.attrs = &p->attrs;
1035 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1038 verf[1] = current->pid;
1039 memcpy(p->o_arg.u.verifier.data, verf,
1040 sizeof(p->o_arg.u.verifier.data));
1042 p->c_arg.fh = &p->o_res.fh;
1043 p->c_arg.stateid = &p->o_res.stateid;
1044 p->c_arg.seqid = p->o_arg.seqid;
1045 nfs4_init_opendata_res(p);
1046 kref_init(&p->kref);
1050 nfs4_label_free(p->f_label);
1058 static void nfs4_opendata_free(struct kref *kref)
1060 struct nfs4_opendata *p = container_of(kref,
1061 struct nfs4_opendata, kref);
1062 struct super_block *sb = p->dentry->d_sb;
1064 nfs_free_seqid(p->o_arg.seqid);
1065 if (p->state != NULL)
1066 nfs4_put_open_state(p->state);
1067 nfs4_put_state_owner(p->owner);
1069 nfs4_label_free(p->f_label);
1073 nfs_sb_deactive(sb);
1074 nfs_fattr_free_names(&p->f_attr);
1075 kfree(p->f_attr.mdsthreshold);
1079 static void nfs4_opendata_put(struct nfs4_opendata *p)
1082 kref_put(&p->kref, nfs4_opendata_free);
1085 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1089 ret = rpc_wait_for_completion_task(task);
1093 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1097 if (open_mode & (O_EXCL|O_TRUNC))
1099 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1101 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1102 && state->n_rdonly != 0;
1105 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1106 && state->n_wronly != 0;
1108 case FMODE_READ|FMODE_WRITE:
1109 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1110 && state->n_rdwr != 0;
1116 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1118 if (delegation == NULL)
1120 if ((delegation->type & fmode) != fmode)
1122 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1124 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1126 nfs_mark_delegation_referenced(delegation);
1130 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1139 case FMODE_READ|FMODE_WRITE:
1142 nfs4_state_set_mode_locked(state, state->state | fmode);
1145 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1147 struct nfs_client *clp = state->owner->so_server->nfs_client;
1148 bool need_recover = false;
1150 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1151 need_recover = true;
1152 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1153 need_recover = true;
1154 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1155 need_recover = true;
1157 nfs4_state_mark_reclaim_nograce(clp, state);
1160 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1161 nfs4_stateid *stateid)
1163 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1165 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1166 nfs_test_and_clear_all_open_stateid(state);
1169 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1174 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1175 nfs4_stateid *stateid, fmode_t fmode)
1177 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1178 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1180 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1183 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1186 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1187 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1188 clear_bit(NFS_OPEN_STATE, &state->flags);
1190 if (stateid == NULL)
1192 if (!nfs_need_update_open_stateid(state, stateid))
1194 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1195 nfs4_stateid_copy(&state->stateid, stateid);
1196 nfs4_stateid_copy(&state->open_stateid, stateid);
1199 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1201 write_seqlock(&state->seqlock);
1202 nfs_clear_open_stateid_locked(state, stateid, fmode);
1203 write_sequnlock(&state->seqlock);
1204 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1205 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1208 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1212 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1215 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1217 case FMODE_READ|FMODE_WRITE:
1218 set_bit(NFS_O_RDWR_STATE, &state->flags);
1220 if (!nfs_need_update_open_stateid(state, stateid))
1222 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1223 nfs4_stateid_copy(&state->stateid, stateid);
1224 nfs4_stateid_copy(&state->open_stateid, stateid);
1227 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1230 * Protect the call to nfs4_state_set_mode_locked and
1231 * serialise the stateid update
1233 write_seqlock(&state->seqlock);
1234 if (deleg_stateid != NULL) {
1235 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1236 set_bit(NFS_DELEGATED_STATE, &state->flags);
1238 if (open_stateid != NULL)
1239 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1240 write_sequnlock(&state->seqlock);
1241 spin_lock(&state->owner->so_lock);
1242 update_open_stateflags(state, fmode);
1243 spin_unlock(&state->owner->so_lock);
1246 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1248 struct nfs_inode *nfsi = NFS_I(state->inode);
1249 struct nfs_delegation *deleg_cur;
1252 fmode &= (FMODE_READ|FMODE_WRITE);
1255 deleg_cur = rcu_dereference(nfsi->delegation);
1256 if (deleg_cur == NULL)
1259 spin_lock(&deleg_cur->lock);
1260 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1261 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1262 (deleg_cur->type & fmode) != fmode)
1263 goto no_delegation_unlock;
1265 if (delegation == NULL)
1266 delegation = &deleg_cur->stateid;
1267 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1268 goto no_delegation_unlock;
1270 nfs_mark_delegation_referenced(deleg_cur);
1271 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1273 no_delegation_unlock:
1274 spin_unlock(&deleg_cur->lock);
1278 if (!ret && open_stateid != NULL) {
1279 __update_open_stateid(state, open_stateid, NULL, fmode);
1282 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1283 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1289 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1291 struct nfs_delegation *delegation;
1294 delegation = rcu_dereference(NFS_I(inode)->delegation);
1295 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1300 nfs4_inode_return_delegation(inode);
1303 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1305 struct nfs4_state *state = opendata->state;
1306 struct nfs_inode *nfsi = NFS_I(state->inode);
1307 struct nfs_delegation *delegation;
1308 int open_mode = opendata->o_arg.open_flags;
1309 fmode_t fmode = opendata->o_arg.fmode;
1310 nfs4_stateid stateid;
1314 spin_lock(&state->owner->so_lock);
1315 if (can_open_cached(state, fmode, open_mode)) {
1316 update_open_stateflags(state, fmode);
1317 spin_unlock(&state->owner->so_lock);
1318 goto out_return_state;
1320 spin_unlock(&state->owner->so_lock);
1322 delegation = rcu_dereference(nfsi->delegation);
1323 if (!can_open_delegated(delegation, fmode)) {
1327 /* Save the delegation */
1328 nfs4_stateid_copy(&stateid, &delegation->stateid);
1330 nfs_release_seqid(opendata->o_arg.seqid);
1331 if (!opendata->is_recover) {
1332 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1338 /* Try to update the stateid using the delegation */
1339 if (update_open_stateid(state, NULL, &stateid, fmode))
1340 goto out_return_state;
1343 return ERR_PTR(ret);
1345 atomic_inc(&state->count);
1350 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1352 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1353 struct nfs_delegation *delegation;
1354 int delegation_flags = 0;
1357 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1359 delegation_flags = delegation->flags;
1361 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1362 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1363 "returning a delegation for "
1364 "OPEN(CLAIM_DELEGATE_CUR)\n",
1366 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1367 nfs_inode_set_delegation(state->inode,
1368 data->owner->so_cred,
1371 nfs_inode_reclaim_delegation(state->inode,
1372 data->owner->so_cred,
1377 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1378 * and update the nfs4_state.
1380 static struct nfs4_state *
1381 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1383 struct inode *inode = data->state->inode;
1384 struct nfs4_state *state = data->state;
1387 if (!data->rpc_done) {
1388 if (data->rpc_status) {
1389 ret = data->rpc_status;
1392 /* cached opens have already been processed */
1396 ret = nfs_refresh_inode(inode, &data->f_attr);
1400 if (data->o_res.delegation_type != 0)
1401 nfs4_opendata_check_deleg(data, state);
1403 update_open_stateid(state, &data->o_res.stateid, NULL,
1405 atomic_inc(&state->count);
1409 return ERR_PTR(ret);
1413 static struct nfs4_state *
1414 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1416 struct inode *inode;
1417 struct nfs4_state *state = NULL;
1420 if (!data->rpc_done) {
1421 state = nfs4_try_open_cached(data);
1426 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1428 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1429 ret = PTR_ERR(inode);
1433 state = nfs4_get_open_state(inode, data->owner);
1436 if (data->o_res.delegation_type != 0)
1437 nfs4_opendata_check_deleg(data, state);
1438 update_open_stateid(state, &data->o_res.stateid, NULL,
1442 nfs_release_seqid(data->o_arg.seqid);
1447 return ERR_PTR(ret);
1450 static struct nfs4_state *
1451 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1453 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1454 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1455 return _nfs4_opendata_to_nfs4_state(data);
1458 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1460 struct nfs_inode *nfsi = NFS_I(state->inode);
1461 struct nfs_open_context *ctx;
1463 spin_lock(&state->inode->i_lock);
1464 list_for_each_entry(ctx, &nfsi->open_files, list) {
1465 if (ctx->state != state)
1467 get_nfs_open_context(ctx);
1468 spin_unlock(&state->inode->i_lock);
1471 spin_unlock(&state->inode->i_lock);
1472 return ERR_PTR(-ENOENT);
1475 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1476 struct nfs4_state *state, enum open_claim_type4 claim)
1478 struct nfs4_opendata *opendata;
1480 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1481 NULL, NULL, claim, GFP_NOFS);
1482 if (opendata == NULL)
1483 return ERR_PTR(-ENOMEM);
1484 opendata->state = state;
1485 atomic_inc(&state->count);
1489 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1491 struct nfs4_state *newstate;
1494 opendata->o_arg.open_flags = 0;
1495 opendata->o_arg.fmode = fmode;
1496 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1497 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1498 nfs4_init_opendata_res(opendata);
1499 ret = _nfs4_recover_proc_open(opendata);
1502 newstate = nfs4_opendata_to_nfs4_state(opendata);
1503 if (IS_ERR(newstate))
1504 return PTR_ERR(newstate);
1505 nfs4_close_state(newstate, fmode);
1510 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1512 struct nfs4_state *newstate;
1515 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1516 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1517 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1518 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1519 /* memory barrier prior to reading state->n_* */
1520 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1521 clear_bit(NFS_OPEN_STATE, &state->flags);
1523 if (state->n_rdwr != 0) {
1524 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1527 if (newstate != state)
1530 if (state->n_wronly != 0) {
1531 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1534 if (newstate != state)
1537 if (state->n_rdonly != 0) {
1538 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1541 if (newstate != state)
1545 * We may have performed cached opens for all three recoveries.
1546 * Check if we need to update the current stateid.
1548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1549 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1550 write_seqlock(&state->seqlock);
1551 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1552 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1553 write_sequnlock(&state->seqlock);
1560 * reclaim state on the server after a reboot.
1562 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1564 struct nfs_delegation *delegation;
1565 struct nfs4_opendata *opendata;
1566 fmode_t delegation_type = 0;
1569 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1570 NFS4_OPEN_CLAIM_PREVIOUS);
1571 if (IS_ERR(opendata))
1572 return PTR_ERR(opendata);
1574 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1575 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1576 delegation_type = delegation->type;
1578 opendata->o_arg.u.delegation_type = delegation_type;
1579 status = nfs4_open_recover(opendata, state);
1580 nfs4_opendata_put(opendata);
1584 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1586 struct nfs_server *server = NFS_SERVER(state->inode);
1587 struct nfs4_exception exception = { };
1590 err = _nfs4_do_open_reclaim(ctx, state);
1591 trace_nfs4_open_reclaim(ctx, 0, err);
1592 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1594 if (err != -NFS4ERR_DELAY)
1596 nfs4_handle_exception(server, err, &exception);
1597 } while (exception.retry);
1601 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1603 struct nfs_open_context *ctx;
1606 ctx = nfs4_state_find_open_context(state);
1609 ret = nfs4_do_open_reclaim(ctx, state);
1610 put_nfs_open_context(ctx);
1614 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1618 printk(KERN_ERR "NFS: %s: unhandled error "
1619 "%d.\n", __func__, err);
1624 case -NFS4ERR_BADSESSION:
1625 case -NFS4ERR_BADSLOT:
1626 case -NFS4ERR_BAD_HIGH_SLOT:
1627 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1628 case -NFS4ERR_DEADSESSION:
1629 set_bit(NFS_DELEGATED_STATE, &state->flags);
1630 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1632 case -NFS4ERR_STALE_CLIENTID:
1633 case -NFS4ERR_STALE_STATEID:
1634 set_bit(NFS_DELEGATED_STATE, &state->flags);
1635 case -NFS4ERR_EXPIRED:
1636 /* Don't recall a delegation if it was lost */
1637 nfs4_schedule_lease_recovery(server->nfs_client);
1639 case -NFS4ERR_MOVED:
1640 nfs4_schedule_migration_recovery(server);
1642 case -NFS4ERR_LEASE_MOVED:
1643 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1645 case -NFS4ERR_DELEG_REVOKED:
1646 case -NFS4ERR_ADMIN_REVOKED:
1647 case -NFS4ERR_BAD_STATEID:
1648 case -NFS4ERR_OPENMODE:
1649 nfs_inode_find_state_and_recover(state->inode,
1651 nfs4_schedule_stateid_recovery(server, state);
1653 case -NFS4ERR_DELAY:
1654 case -NFS4ERR_GRACE:
1655 set_bit(NFS_DELEGATED_STATE, &state->flags);
1659 case -NFS4ERR_DENIED:
1660 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1666 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1668 struct nfs_server *server = NFS_SERVER(state->inode);
1669 struct nfs4_opendata *opendata;
1672 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1673 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1674 if (IS_ERR(opendata))
1675 return PTR_ERR(opendata);
1676 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1677 err = nfs4_open_recover(opendata, state);
1678 nfs4_opendata_put(opendata);
1679 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1682 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1684 struct nfs4_opendata *data = calldata;
1686 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1687 &data->c_res.seq_res, task);
1690 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1692 struct nfs4_opendata *data = calldata;
1694 nfs40_sequence_done(task, &data->c_res.seq_res);
1696 data->rpc_status = task->tk_status;
1697 if (data->rpc_status == 0) {
1698 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1699 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1700 renew_lease(data->o_res.server, data->timestamp);
1705 static void nfs4_open_confirm_release(void *calldata)
1707 struct nfs4_opendata *data = calldata;
1708 struct nfs4_state *state = NULL;
1710 /* If this request hasn't been cancelled, do nothing */
1711 if (data->cancelled == 0)
1713 /* In case of error, no cleanup! */
1714 if (!data->rpc_done)
1716 state = nfs4_opendata_to_nfs4_state(data);
1718 nfs4_close_state(state, data->o_arg.fmode);
1720 nfs4_opendata_put(data);
1723 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1724 .rpc_call_prepare = nfs4_open_confirm_prepare,
1725 .rpc_call_done = nfs4_open_confirm_done,
1726 .rpc_release = nfs4_open_confirm_release,
1730 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1732 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1734 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1735 struct rpc_task *task;
1736 struct rpc_message msg = {
1737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1738 .rpc_argp = &data->c_arg,
1739 .rpc_resp = &data->c_res,
1740 .rpc_cred = data->owner->so_cred,
1742 struct rpc_task_setup task_setup_data = {
1743 .rpc_client = server->client,
1744 .rpc_message = &msg,
1745 .callback_ops = &nfs4_open_confirm_ops,
1746 .callback_data = data,
1747 .workqueue = nfsiod_workqueue,
1748 .flags = RPC_TASK_ASYNC,
1752 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1753 kref_get(&data->kref);
1755 data->rpc_status = 0;
1756 data->timestamp = jiffies;
1757 task = rpc_run_task(&task_setup_data);
1759 return PTR_ERR(task);
1760 status = nfs4_wait_for_completion_rpc_task(task);
1762 data->cancelled = 1;
1765 status = data->rpc_status;
1770 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1772 struct nfs4_opendata *data = calldata;
1773 struct nfs4_state_owner *sp = data->owner;
1774 struct nfs_client *clp = sp->so_server->nfs_client;
1776 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1779 * Check if we still need to send an OPEN call, or if we can use
1780 * a delegation instead.
1782 if (data->state != NULL) {
1783 struct nfs_delegation *delegation;
1785 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1788 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1789 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1790 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1791 can_open_delegated(delegation, data->o_arg.fmode))
1792 goto unlock_no_action;
1795 /* Update client id. */
1796 data->o_arg.clientid = clp->cl_clientid;
1797 switch (data->o_arg.claim) {
1798 case NFS4_OPEN_CLAIM_PREVIOUS:
1799 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1800 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1801 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1802 case NFS4_OPEN_CLAIM_FH:
1803 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1804 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1806 data->timestamp = jiffies;
1807 if (nfs4_setup_sequence(data->o_arg.server,
1808 &data->o_arg.seq_args,
1809 &data->o_res.seq_res,
1811 nfs_release_seqid(data->o_arg.seqid);
1813 /* Set the create mode (note dependency on the session type) */
1814 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1815 if (data->o_arg.open_flags & O_EXCL) {
1816 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1817 if (nfs4_has_persistent_session(clp))
1818 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1819 else if (clp->cl_mvops->minor_version > 0)
1820 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1826 task->tk_action = NULL;
1828 nfs4_sequence_done(task, &data->o_res.seq_res);
1831 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1833 struct nfs4_opendata *data = calldata;
1835 data->rpc_status = task->tk_status;
1837 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1840 if (task->tk_status == 0) {
1841 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1842 switch (data->o_res.f_attr->mode & S_IFMT) {
1846 data->rpc_status = -ELOOP;
1849 data->rpc_status = -EISDIR;
1852 data->rpc_status = -ENOTDIR;
1855 renew_lease(data->o_res.server, data->timestamp);
1856 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1857 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1862 static void nfs4_open_release(void *calldata)
1864 struct nfs4_opendata *data = calldata;
1865 struct nfs4_state *state = NULL;
1867 /* If this request hasn't been cancelled, do nothing */
1868 if (data->cancelled == 0)
1870 /* In case of error, no cleanup! */
1871 if (data->rpc_status != 0 || !data->rpc_done)
1873 /* In case we need an open_confirm, no cleanup! */
1874 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1876 state = nfs4_opendata_to_nfs4_state(data);
1878 nfs4_close_state(state, data->o_arg.fmode);
1880 nfs4_opendata_put(data);
1883 static const struct rpc_call_ops nfs4_open_ops = {
1884 .rpc_call_prepare = nfs4_open_prepare,
1885 .rpc_call_done = nfs4_open_done,
1886 .rpc_release = nfs4_open_release,
1889 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1891 struct inode *dir = data->dir->d_inode;
1892 struct nfs_server *server = NFS_SERVER(dir);
1893 struct nfs_openargs *o_arg = &data->o_arg;
1894 struct nfs_openres *o_res = &data->o_res;
1895 struct rpc_task *task;
1896 struct rpc_message msg = {
1897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1900 .rpc_cred = data->owner->so_cred,
1902 struct rpc_task_setup task_setup_data = {
1903 .rpc_client = server->client,
1904 .rpc_message = &msg,
1905 .callback_ops = &nfs4_open_ops,
1906 .callback_data = data,
1907 .workqueue = nfsiod_workqueue,
1908 .flags = RPC_TASK_ASYNC,
1912 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1913 kref_get(&data->kref);
1915 data->rpc_status = 0;
1916 data->cancelled = 0;
1917 data->is_recover = 0;
1919 nfs4_set_sequence_privileged(&o_arg->seq_args);
1920 data->is_recover = 1;
1922 task = rpc_run_task(&task_setup_data);
1924 return PTR_ERR(task);
1925 status = nfs4_wait_for_completion_rpc_task(task);
1927 data->cancelled = 1;
1930 status = data->rpc_status;
1936 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1938 struct inode *dir = data->dir->d_inode;
1939 struct nfs_openres *o_res = &data->o_res;
1942 status = nfs4_run_open_task(data, 1);
1943 if (status != 0 || !data->rpc_done)
1946 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1948 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1949 status = _nfs4_proc_open_confirm(data);
1958 * Additional permission checks in order to distinguish between an
1959 * open for read, and an open for execute. This works around the
1960 * fact that NFSv4 OPEN treats read and execute permissions as being
1962 * Note that in the non-execute case, we want to turn off permission
1963 * checking if we just created a new file (POSIX open() semantics).
1965 static int nfs4_opendata_access(struct rpc_cred *cred,
1966 struct nfs4_opendata *opendata,
1967 struct nfs4_state *state, fmode_t fmode,
1970 struct nfs_access_entry cache;
1973 /* access call failed or for some reason the server doesn't
1974 * support any access modes -- defer access call until later */
1975 if (opendata->o_res.access_supported == 0)
1980 * Use openflags to check for exec, because fmode won't
1981 * always have FMODE_EXEC set when file open for exec.
1983 if (openflags & __FMODE_EXEC) {
1984 /* ONLY check for exec rights */
1986 } else if ((fmode & FMODE_READ) && !opendata->file_created)
1990 cache.jiffies = jiffies;
1991 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1992 nfs_access_add_cache(state->inode, &cache);
1994 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1997 /* even though OPEN succeeded, access is denied. Close the file */
1998 nfs4_close_state(state, fmode);
2003 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2005 static int _nfs4_proc_open(struct nfs4_opendata *data)
2007 struct inode *dir = data->dir->d_inode;
2008 struct nfs_server *server = NFS_SERVER(dir);
2009 struct nfs_openargs *o_arg = &data->o_arg;
2010 struct nfs_openres *o_res = &data->o_res;
2013 status = nfs4_run_open_task(data, 0);
2014 if (!data->rpc_done)
2017 if (status == -NFS4ERR_BADNAME &&
2018 !(o_arg->open_flags & O_CREAT))
2023 nfs_fattr_map_and_free_names(server, &data->f_attr);
2025 if (o_arg->open_flags & O_CREAT) {
2026 update_changeattr(dir, &o_res->cinfo);
2027 if (o_arg->open_flags & O_EXCL)
2028 data->file_created = 1;
2029 else if (o_res->cinfo.before != o_res->cinfo.after)
2030 data->file_created = 1;
2032 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2033 server->caps &= ~NFS_CAP_POSIX_LOCK;
2034 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2035 status = _nfs4_proc_open_confirm(data);
2039 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2040 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2044 static int nfs4_recover_expired_lease(struct nfs_server *server)
2046 return nfs4_client_recover_expired_lease(server->nfs_client);
2051 * reclaim state on the server after a network partition.
2052 * Assumes caller holds the appropriate lock
2054 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2056 struct nfs4_opendata *opendata;
2059 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2060 NFS4_OPEN_CLAIM_FH);
2061 if (IS_ERR(opendata))
2062 return PTR_ERR(opendata);
2063 ret = nfs4_open_recover(opendata, state);
2065 d_drop(ctx->dentry);
2066 nfs4_opendata_put(opendata);
2070 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2072 struct nfs_server *server = NFS_SERVER(state->inode);
2073 struct nfs4_exception exception = { };
2077 err = _nfs4_open_expired(ctx, state);
2078 trace_nfs4_open_expired(ctx, 0, err);
2079 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2084 case -NFS4ERR_GRACE:
2085 case -NFS4ERR_DELAY:
2086 nfs4_handle_exception(server, err, &exception);
2089 } while (exception.retry);
2094 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2096 struct nfs_open_context *ctx;
2099 ctx = nfs4_state_find_open_context(state);
2102 ret = nfs4_do_open_expired(ctx, state);
2103 put_nfs_open_context(ctx);
2107 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2109 nfs_remove_bad_delegation(state->inode);
2110 write_seqlock(&state->seqlock);
2111 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2112 write_sequnlock(&state->seqlock);
2113 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2116 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2118 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2119 nfs_finish_clear_delegation_stateid(state);
2122 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2124 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2125 nfs40_clear_delegation_stateid(state);
2126 return nfs4_open_expired(sp, state);
2129 #if defined(CONFIG_NFS_V4_1)
2130 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2132 struct nfs_server *server = NFS_SERVER(state->inode);
2133 nfs4_stateid stateid;
2134 struct nfs_delegation *delegation;
2135 struct rpc_cred *cred;
2138 /* Get the delegation credential for use by test/free_stateid */
2140 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2141 if (delegation == NULL) {
2146 nfs4_stateid_copy(&stateid, &delegation->stateid);
2147 cred = get_rpccred(delegation->cred);
2149 status = nfs41_test_stateid(server, &stateid, cred);
2150 trace_nfs4_test_delegation_stateid(state, NULL, status);
2152 if (status != NFS_OK) {
2153 /* Free the stateid unless the server explicitly
2154 * informs us the stateid is unrecognized. */
2155 if (status != -NFS4ERR_BAD_STATEID)
2156 nfs41_free_stateid(server, &stateid, cred);
2157 nfs_finish_clear_delegation_stateid(state);
2164 * nfs41_check_open_stateid - possibly free an open stateid
2166 * @state: NFSv4 state for an inode
2168 * Returns NFS_OK if recovery for this stateid is now finished.
2169 * Otherwise a negative NFS4ERR value is returned.
2171 static int nfs41_check_open_stateid(struct nfs4_state *state)
2173 struct nfs_server *server = NFS_SERVER(state->inode);
2174 nfs4_stateid *stateid = &state->open_stateid;
2175 struct rpc_cred *cred = state->owner->so_cred;
2178 /* If a state reset has been done, test_stateid is unneeded */
2179 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2180 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2181 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2182 return -NFS4ERR_BAD_STATEID;
2184 status = nfs41_test_stateid(server, stateid, cred);
2185 trace_nfs4_test_open_stateid(state, NULL, status);
2186 if (status != NFS_OK) {
2187 /* Free the stateid unless the server explicitly
2188 * informs us the stateid is unrecognized. */
2189 if (status != -NFS4ERR_BAD_STATEID)
2190 nfs41_free_stateid(server, stateid, cred);
2192 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2193 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2194 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2195 clear_bit(NFS_OPEN_STATE, &state->flags);
2200 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2204 nfs41_check_delegation_stateid(state);
2205 status = nfs41_check_open_stateid(state);
2206 if (status != NFS_OK)
2207 status = nfs4_open_expired(sp, state);
2213 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2214 * fields corresponding to attributes that were used to store the verifier.
2215 * Make sure we clobber those fields in the later setattr call
2217 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2219 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2220 !(sattr->ia_valid & ATTR_ATIME_SET))
2221 sattr->ia_valid |= ATTR_ATIME;
2223 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2224 !(sattr->ia_valid & ATTR_MTIME_SET))
2225 sattr->ia_valid |= ATTR_MTIME;
2228 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2231 struct nfs_open_context *ctx)
2233 struct nfs4_state_owner *sp = opendata->owner;
2234 struct nfs_server *server = sp->so_server;
2235 struct dentry *dentry;
2236 struct nfs4_state *state;
2240 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2242 ret = _nfs4_proc_open(opendata);
2246 state = nfs4_opendata_to_nfs4_state(opendata);
2247 ret = PTR_ERR(state);
2250 if (server->caps & NFS_CAP_POSIX_LOCK)
2251 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2253 dentry = opendata->dentry;
2254 if (dentry->d_inode == NULL) {
2255 /* FIXME: Is this d_drop() ever needed? */
2257 dentry = d_add_unique(dentry, igrab(state->inode));
2258 if (dentry == NULL) {
2259 dentry = opendata->dentry;
2260 } else if (dentry != ctx->dentry) {
2262 ctx->dentry = dget(dentry);
2264 nfs_set_verifier(dentry,
2265 nfs_save_change_attribute(opendata->dir->d_inode));
2268 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2273 if (dentry->d_inode == state->inode) {
2274 nfs_inode_attach_open_context(ctx);
2275 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2276 nfs4_schedule_stateid_recovery(server, state);
2283 * Returns a referenced nfs4_state
2285 static int _nfs4_do_open(struct inode *dir,
2286 struct nfs_open_context *ctx,
2288 struct iattr *sattr,
2289 struct nfs4_label *label,
2292 struct nfs4_state_owner *sp;
2293 struct nfs4_state *state = NULL;
2294 struct nfs_server *server = NFS_SERVER(dir);
2295 struct nfs4_opendata *opendata;
2296 struct dentry *dentry = ctx->dentry;
2297 struct rpc_cred *cred = ctx->cred;
2298 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2299 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2300 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2301 struct nfs4_label *olabel = NULL;
2304 /* Protect against reboot recovery conflicts */
2306 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2308 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2311 status = nfs4_recover_expired_lease(server);
2313 goto err_put_state_owner;
2314 if (dentry->d_inode != NULL)
2315 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2317 if (dentry->d_inode)
2318 claim = NFS4_OPEN_CLAIM_FH;
2319 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2320 label, claim, GFP_KERNEL);
2321 if (opendata == NULL)
2322 goto err_put_state_owner;
2325 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2326 if (IS_ERR(olabel)) {
2327 status = PTR_ERR(olabel);
2328 goto err_opendata_put;
2332 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2333 if (!opendata->f_attr.mdsthreshold) {
2334 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2335 if (!opendata->f_attr.mdsthreshold)
2336 goto err_free_label;
2338 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2340 if (dentry->d_inode != NULL)
2341 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2343 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2345 goto err_free_label;
2348 if ((opendata->o_arg.open_flags & O_EXCL) &&
2349 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2350 nfs4_exclusive_attrset(opendata, sattr);
2352 nfs_fattr_init(opendata->o_res.f_attr);
2353 status = nfs4_do_setattr(state->inode, cred,
2354 opendata->o_res.f_attr, sattr,
2355 state, label, olabel);
2357 nfs_setattr_update_inode(state->inode, sattr);
2358 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2359 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2362 if (opendata->file_created)
2363 *opened |= FILE_CREATED;
2365 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2366 *ctx_th = opendata->f_attr.mdsthreshold;
2367 opendata->f_attr.mdsthreshold = NULL;
2370 nfs4_label_free(olabel);
2372 nfs4_opendata_put(opendata);
2373 nfs4_put_state_owner(sp);
2376 nfs4_label_free(olabel);
2378 nfs4_opendata_put(opendata);
2379 err_put_state_owner:
2380 nfs4_put_state_owner(sp);
2386 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2387 struct nfs_open_context *ctx,
2389 struct iattr *sattr,
2390 struct nfs4_label *label,
2393 struct nfs_server *server = NFS_SERVER(dir);
2394 struct nfs4_exception exception = { };
2395 struct nfs4_state *res;
2399 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2401 trace_nfs4_open_file(ctx, flags, status);
2404 /* NOTE: BAD_SEQID means the server and client disagree about the
2405 * book-keeping w.r.t. state-changing operations
2406 * (OPEN/CLOSE/LOCK/LOCKU...)
2407 * It is actually a sign of a bug on the client or on the server.
2409 * If we receive a BAD_SEQID error in the particular case of
2410 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2411 * have unhashed the old state_owner for us, and that we can
2412 * therefore safely retry using a new one. We should still warn
2413 * the user though...
2415 if (status == -NFS4ERR_BAD_SEQID) {
2416 pr_warn_ratelimited("NFS: v4 server %s "
2417 " returned a bad sequence-id error!\n",
2418 NFS_SERVER(dir)->nfs_client->cl_hostname);
2419 exception.retry = 1;
2423 * BAD_STATEID on OPEN means that the server cancelled our
2424 * state before it received the OPEN_CONFIRM.
2425 * Recover by retrying the request as per the discussion
2426 * on Page 181 of RFC3530.
2428 if (status == -NFS4ERR_BAD_STATEID) {
2429 exception.retry = 1;
2432 if (status == -EAGAIN) {
2433 /* We must have found a delegation */
2434 exception.retry = 1;
2437 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2439 res = ERR_PTR(nfs4_handle_exception(server,
2440 status, &exception));
2441 } while (exception.retry);
2445 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2446 struct nfs_fattr *fattr, struct iattr *sattr,
2447 struct nfs4_state *state, struct nfs4_label *ilabel,
2448 struct nfs4_label *olabel)
2450 struct nfs_server *server = NFS_SERVER(inode);
2451 struct nfs_setattrargs arg = {
2452 .fh = NFS_FH(inode),
2455 .bitmask = server->attr_bitmask,
2458 struct nfs_setattrres res = {
2463 struct rpc_message msg = {
2464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2469 unsigned long timestamp = jiffies;
2474 arg.bitmask = nfs4_bitmask(server, ilabel);
2476 arg.bitmask = nfs4_bitmask(server, olabel);
2478 nfs_fattr_init(fattr);
2480 /* Servers should only apply open mode checks for file size changes */
2481 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2482 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2484 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2485 /* Use that stateid */
2486 } else if (truncate && state != NULL) {
2487 struct nfs_lockowner lockowner = {
2488 .l_owner = current->files,
2489 .l_pid = current->tgid,
2491 if (!nfs4_valid_open_stateid(state))
2493 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2494 &lockowner) == -EIO)
2497 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2499 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2500 if (status == 0 && state != NULL)
2501 renew_lease(server, timestamp);
2505 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2506 struct nfs_fattr *fattr, struct iattr *sattr,
2507 struct nfs4_state *state, struct nfs4_label *ilabel,
2508 struct nfs4_label *olabel)
2510 struct nfs_server *server = NFS_SERVER(inode);
2511 struct nfs4_exception exception = {
2517 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2518 trace_nfs4_setattr(inode, err);
2520 case -NFS4ERR_OPENMODE:
2521 if (!(sattr->ia_valid & ATTR_SIZE)) {
2522 pr_warn_once("NFSv4: server %s is incorrectly "
2523 "applying open mode checks to "
2524 "a SETATTR that is not "
2525 "changing file size.\n",
2526 server->nfs_client->cl_hostname);
2528 if (state && !(state->state & FMODE_WRITE)) {
2530 if (sattr->ia_valid & ATTR_OPEN)
2535 err = nfs4_handle_exception(server, err, &exception);
2536 } while (exception.retry);
2541 struct nfs4_closedata {
2542 struct inode *inode;
2543 struct nfs4_state *state;
2544 struct nfs_closeargs arg;
2545 struct nfs_closeres res;
2546 struct nfs_fattr fattr;
2547 unsigned long timestamp;
2552 static void nfs4_free_closedata(void *data)
2554 struct nfs4_closedata *calldata = data;
2555 struct nfs4_state_owner *sp = calldata->state->owner;
2556 struct super_block *sb = calldata->state->inode->i_sb;
2559 pnfs_roc_release(calldata->state->inode);
2560 nfs4_put_open_state(calldata->state);
2561 nfs_free_seqid(calldata->arg.seqid);
2562 nfs4_put_state_owner(sp);
2563 nfs_sb_deactive(sb);
2567 static void nfs4_close_done(struct rpc_task *task, void *data)
2569 struct nfs4_closedata *calldata = data;
2570 struct nfs4_state *state = calldata->state;
2571 struct nfs_server *server = NFS_SERVER(calldata->inode);
2572 nfs4_stateid *res_stateid = NULL;
2574 dprintk("%s: begin!\n", __func__);
2575 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2577 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2578 /* hmm. we are done with the inode, and in the process of freeing
2579 * the state_owner. we keep this around to process errors
2581 switch (task->tk_status) {
2583 res_stateid = &calldata->res.stateid;
2584 if (calldata->arg.fmode == 0 && calldata->roc)
2585 pnfs_roc_set_barrier(state->inode,
2586 calldata->roc_barrier);
2587 renew_lease(server, calldata->timestamp);
2589 case -NFS4ERR_ADMIN_REVOKED:
2590 case -NFS4ERR_STALE_STATEID:
2591 case -NFS4ERR_OLD_STATEID:
2592 case -NFS4ERR_BAD_STATEID:
2593 case -NFS4ERR_EXPIRED:
2594 if (calldata->arg.fmode == 0)
2597 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2598 rpc_restart_call_prepare(task);
2602 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
2604 nfs_release_seqid(calldata->arg.seqid);
2605 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2606 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2609 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2611 struct nfs4_closedata *calldata = data;
2612 struct nfs4_state *state = calldata->state;
2613 struct inode *inode = calldata->inode;
2614 bool is_rdonly, is_wronly, is_rdwr;
2617 dprintk("%s: begin!\n", __func__);
2618 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2621 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2622 spin_lock(&state->owner->so_lock);
2623 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2624 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2625 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2626 /* Calculate the change in open mode */
2627 calldata->arg.fmode = 0;
2628 if (state->n_rdwr == 0) {
2629 if (state->n_rdonly == 0)
2630 call_close |= is_rdonly;
2632 calldata->arg.fmode |= FMODE_READ;
2633 if (state->n_wronly == 0)
2634 call_close |= is_wronly;
2636 calldata->arg.fmode |= FMODE_WRITE;
2638 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2640 if (calldata->arg.fmode == 0)
2641 call_close |= is_rdwr;
2643 if (!nfs4_valid_open_stateid(state))
2645 spin_unlock(&state->owner->so_lock);
2648 /* Note: exit _without_ calling nfs4_close_done */
2652 if (calldata->arg.fmode == 0) {
2653 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2654 if (calldata->roc &&
2655 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2656 nfs_release_seqid(calldata->arg.seqid);
2661 nfs_fattr_init(calldata->res.fattr);
2662 calldata->timestamp = jiffies;
2663 if (nfs4_setup_sequence(NFS_SERVER(inode),
2664 &calldata->arg.seq_args,
2665 &calldata->res.seq_res,
2667 nfs_release_seqid(calldata->arg.seqid);
2668 dprintk("%s: done!\n", __func__);
2671 task->tk_action = NULL;
2673 nfs4_sequence_done(task, &calldata->res.seq_res);
2676 static const struct rpc_call_ops nfs4_close_ops = {
2677 .rpc_call_prepare = nfs4_close_prepare,
2678 .rpc_call_done = nfs4_close_done,
2679 .rpc_release = nfs4_free_closedata,
2682 static bool nfs4_state_has_opener(struct nfs4_state *state)
2684 /* first check existing openers */
2685 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 &&
2686 state->n_rdonly != 0)
2689 if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 &&
2690 state->n_wronly != 0)
2693 if (test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 &&
2700 static bool nfs4_roc(struct inode *inode)
2702 struct nfs_inode *nfsi = NFS_I(inode);
2703 struct nfs_open_context *ctx;
2704 struct nfs4_state *state;
2706 spin_lock(&inode->i_lock);
2707 list_for_each_entry(ctx, &nfsi->open_files, list) {
2711 if (nfs4_state_has_opener(state)) {
2712 spin_unlock(&inode->i_lock);
2716 spin_unlock(&inode->i_lock);
2718 if (nfs4_check_delegation(inode, FMODE_READ))
2721 return pnfs_roc(inode);
2725 * It is possible for data to be read/written from a mem-mapped file
2726 * after the sys_close call (which hits the vfs layer as a flush).
2727 * This means that we can't safely call nfsv4 close on a file until
2728 * the inode is cleared. This in turn means that we are not good
2729 * NFSv4 citizens - we do not indicate to the server to update the file's
2730 * share state even when we are done with one of the three share
2731 * stateid's in the inode.
2733 * NOTE: Caller must be holding the sp->so_owner semaphore!
2735 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2737 struct nfs_server *server = NFS_SERVER(state->inode);
2738 struct nfs4_closedata *calldata;
2739 struct nfs4_state_owner *sp = state->owner;
2740 struct rpc_task *task;
2741 struct rpc_message msg = {
2742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2743 .rpc_cred = state->owner->so_cred,
2745 struct rpc_task_setup task_setup_data = {
2746 .rpc_client = server->client,
2747 .rpc_message = &msg,
2748 .callback_ops = &nfs4_close_ops,
2749 .workqueue = nfsiod_workqueue,
2750 .flags = RPC_TASK_ASYNC,
2752 int status = -ENOMEM;
2754 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2755 &task_setup_data.rpc_client, &msg);
2757 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2758 if (calldata == NULL)
2760 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2761 calldata->inode = state->inode;
2762 calldata->state = state;
2763 calldata->arg.fh = NFS_FH(state->inode);
2764 calldata->arg.stateid = &state->open_stateid;
2765 /* Serialization for the sequence id */
2766 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2767 if (calldata->arg.seqid == NULL)
2768 goto out_free_calldata;
2769 calldata->arg.fmode = 0;
2770 calldata->arg.bitmask = server->cache_consistency_bitmask;
2771 calldata->res.fattr = &calldata->fattr;
2772 calldata->res.seqid = calldata->arg.seqid;
2773 calldata->res.server = server;
2774 calldata->roc = nfs4_roc(state->inode);
2775 nfs_sb_active(calldata->inode->i_sb);
2777 msg.rpc_argp = &calldata->arg;
2778 msg.rpc_resp = &calldata->res;
2779 task_setup_data.callback_data = calldata;
2780 task = rpc_run_task(&task_setup_data);
2782 return PTR_ERR(task);
2785 status = rpc_wait_for_completion_task(task);
2791 nfs4_put_open_state(state);
2792 nfs4_put_state_owner(sp);
2796 static struct inode *
2797 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2798 int open_flags, struct iattr *attr, int *opened)
2800 struct nfs4_state *state;
2801 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2803 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2805 /* Protect against concurrent sillydeletes */
2806 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2808 nfs4_label_release_security(label);
2811 return ERR_CAST(state);
2812 return state->inode;
2815 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2817 if (ctx->state == NULL)
2820 nfs4_close_sync(ctx->state, ctx->mode);
2822 nfs4_close_state(ctx->state, ctx->mode);
2825 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2826 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2827 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2829 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2831 struct nfs4_server_caps_arg args = {
2834 struct nfs4_server_caps_res res = {};
2835 struct rpc_message msg = {
2836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2842 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2844 /* Sanity check the server answers */
2845 switch (server->nfs_client->cl_minorversion) {
2847 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2848 res.attr_bitmask[2] = 0;
2851 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2854 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2856 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2857 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2858 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2859 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2860 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2861 NFS_CAP_CTIME|NFS_CAP_MTIME|
2862 NFS_CAP_SECURITY_LABEL);
2863 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2864 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2865 server->caps |= NFS_CAP_ACLS;
2866 if (res.has_links != 0)
2867 server->caps |= NFS_CAP_HARDLINKS;
2868 if (res.has_symlinks != 0)
2869 server->caps |= NFS_CAP_SYMLINKS;
2870 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2871 server->caps |= NFS_CAP_FILEID;
2872 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2873 server->caps |= NFS_CAP_MODE;
2874 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2875 server->caps |= NFS_CAP_NLINK;
2876 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2877 server->caps |= NFS_CAP_OWNER;
2878 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2879 server->caps |= NFS_CAP_OWNER_GROUP;
2880 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2881 server->caps |= NFS_CAP_ATIME;
2882 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2883 server->caps |= NFS_CAP_CTIME;
2884 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2885 server->caps |= NFS_CAP_MTIME;
2886 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2887 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2888 server->caps |= NFS_CAP_SECURITY_LABEL;
2890 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2891 sizeof(server->attr_bitmask));
2892 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2894 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2895 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2896 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2897 server->cache_consistency_bitmask[2] = 0;
2898 server->acl_bitmask = res.acl_bitmask;
2899 server->fh_expire_type = res.fh_expire_type;
2905 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2907 struct nfs4_exception exception = { };
2910 err = nfs4_handle_exception(server,
2911 _nfs4_server_capabilities(server, fhandle),
2913 } while (exception.retry);
2917 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2918 struct nfs_fsinfo *info)
2921 struct nfs4_lookup_root_arg args = {
2924 struct nfs4_lookup_res res = {
2926 .fattr = info->fattr,
2929 struct rpc_message msg = {
2930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2935 bitmask[0] = nfs4_fattr_bitmap[0];
2936 bitmask[1] = nfs4_fattr_bitmap[1];
2938 * Process the label in the upcoming getfattr
2940 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2942 nfs_fattr_init(info->fattr);
2943 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2946 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2947 struct nfs_fsinfo *info)
2949 struct nfs4_exception exception = { };
2952 err = _nfs4_lookup_root(server, fhandle, info);
2953 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2956 case -NFS4ERR_WRONGSEC:
2959 err = nfs4_handle_exception(server, err, &exception);
2961 } while (exception.retry);
2966 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2967 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2969 struct rpc_auth_create_args auth_args = {
2970 .pseudoflavor = flavor,
2972 struct rpc_auth *auth;
2975 auth = rpcauth_create(&auth_args, server->client);
2980 ret = nfs4_lookup_root(server, fhandle, info);
2986 * Retry pseudoroot lookup with various security flavors. We do this when:
2988 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2989 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2991 * Returns zero on success, or a negative NFS4ERR value, or a
2992 * negative errno value.
2994 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2995 struct nfs_fsinfo *info)
2997 /* Per 3530bis 15.33.5 */
2998 static const rpc_authflavor_t flav_array[] = {
3002 RPC_AUTH_UNIX, /* courtesy */
3005 int status = -EPERM;
3008 if (server->auth_info.flavor_len > 0) {
3009 /* try each flavor specified by user */
3010 for (i = 0; i < server->auth_info.flavor_len; i++) {
3011 status = nfs4_lookup_root_sec(server, fhandle, info,
3012 server->auth_info.flavors[i]);
3013 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3018 /* no flavors specified by user, try default list */
3019 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3020 status = nfs4_lookup_root_sec(server, fhandle, info,
3022 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3029 * -EACCESS could mean that the user doesn't have correct permissions
3030 * to access the mount. It could also mean that we tried to mount
3031 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3032 * existing mount programs don't handle -EACCES very well so it should
3033 * be mapped to -EPERM instead.
3035 if (status == -EACCES)
3040 static int nfs4_do_find_root_sec(struct nfs_server *server,
3041 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3043 int mv = server->nfs_client->cl_minorversion;
3044 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3048 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3049 * @server: initialized nfs_server handle
3050 * @fhandle: we fill in the pseudo-fs root file handle
3051 * @info: we fill in an FSINFO struct
3052 * @auth_probe: probe the auth flavours
3054 * Returns zero on success, or a negative errno.
3056 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3057 struct nfs_fsinfo *info,
3062 switch (auth_probe) {
3064 status = nfs4_lookup_root(server, fhandle, info);
3065 if (status != -NFS4ERR_WRONGSEC)
3068 status = nfs4_do_find_root_sec(server, fhandle, info);
3072 status = nfs4_server_capabilities(server, fhandle);
3074 status = nfs4_do_fsinfo(server, fhandle, info);
3076 return nfs4_map_errors(status);
3079 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3080 struct nfs_fsinfo *info)
3083 struct nfs_fattr *fattr = info->fattr;
3084 struct nfs4_label *label = NULL;
3086 error = nfs4_server_capabilities(server, mntfh);
3088 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3092 label = nfs4_label_alloc(server, GFP_KERNEL);
3094 return PTR_ERR(label);
3096 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3098 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3099 goto err_free_label;
3102 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3103 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3104 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3107 nfs4_label_free(label);
3113 * Get locations and (maybe) other attributes of a referral.
3114 * Note that we'll actually follow the referral later when
3115 * we detect fsid mismatch in inode revalidation
3117 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3118 const struct qstr *name, struct nfs_fattr *fattr,
3119 struct nfs_fh *fhandle)
3121 int status = -ENOMEM;
3122 struct page *page = NULL;
3123 struct nfs4_fs_locations *locations = NULL;
3125 page = alloc_page(GFP_KERNEL);
3128 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3129 if (locations == NULL)
3132 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3137 * If the fsid didn't change, this is a migration event, not a
3138 * referral. Cause us to drop into the exception handler, which
3139 * will kick off migration recovery.
3141 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3142 dprintk("%s: server did not return a different fsid for"
3143 " a referral at %s\n", __func__, name->name);
3144 status = -NFS4ERR_MOVED;
3147 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3148 nfs_fixup_referral_attributes(&locations->fattr);
3150 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3151 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3152 memset(fhandle, 0, sizeof(struct nfs_fh));
3160 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3161 struct nfs_fattr *fattr, struct nfs4_label *label)
3163 struct nfs4_getattr_arg args = {
3165 .bitmask = server->attr_bitmask,
3167 struct nfs4_getattr_res res = {
3172 struct rpc_message msg = {
3173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3178 args.bitmask = nfs4_bitmask(server, label);
3180 nfs_fattr_init(fattr);
3181 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3184 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3185 struct nfs_fattr *fattr, struct nfs4_label *label)
3187 struct nfs4_exception exception = { };
3190 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3191 trace_nfs4_getattr(server, fhandle, fattr, err);
3192 err = nfs4_handle_exception(server, err,
3194 } while (exception.retry);
3199 * The file is not closed if it is opened due to the a request to change
3200 * the size of the file. The open call will not be needed once the
3201 * VFS layer lookup-intents are implemented.
3203 * Close is called when the inode is destroyed.
3204 * If we haven't opened the file for O_WRONLY, we
3205 * need to in the size_change case to obtain a stateid.
3208 * Because OPEN is always done by name in nfsv4, it is
3209 * possible that we opened a different file by the same
3210 * name. We can recognize this race condition, but we
3211 * can't do anything about it besides returning an error.
3213 * This will be fixed with VFS changes (lookup-intent).
3216 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3217 struct iattr *sattr)
3219 struct inode *inode = dentry->d_inode;
3220 struct rpc_cred *cred = NULL;
3221 struct nfs4_state *state = NULL;
3222 struct nfs4_label *label = NULL;
3225 if (pnfs_ld_layoutret_on_setattr(inode) &&
3226 sattr->ia_valid & ATTR_SIZE &&
3227 sattr->ia_size < i_size_read(inode))
3228 pnfs_commit_and_return_layout(inode);
3230 nfs_fattr_init(fattr);
3232 /* Deal with open(O_TRUNC) */
3233 if (sattr->ia_valid & ATTR_OPEN)
3234 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3236 /* Optimization: if the end result is no change, don't RPC */
3237 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3240 /* Search for an existing open(O_WRITE) file */
3241 if (sattr->ia_valid & ATTR_FILE) {
3242 struct nfs_open_context *ctx;
3244 ctx = nfs_file_open_context(sattr->ia_file);
3251 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3253 return PTR_ERR(label);
3255 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3257 nfs_setattr_update_inode(inode, sattr);
3258 nfs_setsecurity(inode, fattr, label);
3260 nfs4_label_free(label);
3264 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3265 const struct qstr *name, struct nfs_fh *fhandle,
3266 struct nfs_fattr *fattr, struct nfs4_label *label)
3268 struct nfs_server *server = NFS_SERVER(dir);
3270 struct nfs4_lookup_arg args = {
3271 .bitmask = server->attr_bitmask,
3272 .dir_fh = NFS_FH(dir),
3275 struct nfs4_lookup_res res = {
3281 struct rpc_message msg = {
3282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3287 args.bitmask = nfs4_bitmask(server, label);
3289 nfs_fattr_init(fattr);
3291 dprintk("NFS call lookup %s\n", name->name);
3292 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3293 dprintk("NFS reply lookup: %d\n", status);
3297 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3299 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3300 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3301 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3305 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3306 struct qstr *name, struct nfs_fh *fhandle,
3307 struct nfs_fattr *fattr, struct nfs4_label *label)
3309 struct nfs4_exception exception = { };
3310 struct rpc_clnt *client = *clnt;
3313 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3314 trace_nfs4_lookup(dir, name, err);
3316 case -NFS4ERR_BADNAME:
3319 case -NFS4ERR_MOVED:
3320 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3322 case -NFS4ERR_WRONGSEC:
3324 if (client != *clnt)
3326 client = nfs4_negotiate_security(client, dir, name);
3328 return PTR_ERR(client);
3330 exception.retry = 1;
3333 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3335 } while (exception.retry);
3340 else if (client != *clnt)
3341 rpc_shutdown_client(client);
3346 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3347 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3348 struct nfs4_label *label)
3351 struct rpc_clnt *client = NFS_CLIENT(dir);
3353 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3354 if (client != NFS_CLIENT(dir)) {
3355 rpc_shutdown_client(client);
3356 nfs_fixup_secinfo_attributes(fattr);
3362 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3363 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3365 struct rpc_clnt *client = NFS_CLIENT(dir);
3368 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3370 return ERR_PTR(status);
3371 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3374 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3376 struct nfs_server *server = NFS_SERVER(inode);
3377 struct nfs4_accessargs args = {
3378 .fh = NFS_FH(inode),
3379 .bitmask = server->cache_consistency_bitmask,
3381 struct nfs4_accessres res = {
3384 struct rpc_message msg = {
3385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3388 .rpc_cred = entry->cred,
3390 int mode = entry->mask;
3394 * Determine which access bits we want to ask for...
3396 if (mode & MAY_READ)
3397 args.access |= NFS4_ACCESS_READ;
3398 if (S_ISDIR(inode->i_mode)) {
3399 if (mode & MAY_WRITE)
3400 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3401 if (mode & MAY_EXEC)
3402 args.access |= NFS4_ACCESS_LOOKUP;
3404 if (mode & MAY_WRITE)
3405 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3406 if (mode & MAY_EXEC)
3407 args.access |= NFS4_ACCESS_EXECUTE;
3410 res.fattr = nfs_alloc_fattr();
3411 if (res.fattr == NULL)
3414 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3416 nfs_access_set_mask(entry, res.access);
3417 nfs_refresh_inode(inode, res.fattr);
3419 nfs_free_fattr(res.fattr);
3423 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3425 struct nfs4_exception exception = { };
3428 err = _nfs4_proc_access(inode, entry);
3429 trace_nfs4_access(inode, err);
3430 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3432 } while (exception.retry);
3437 * TODO: For the time being, we don't try to get any attributes
3438 * along with any of the zero-copy operations READ, READDIR,
3441 * In the case of the first three, we want to put the GETATTR
3442 * after the read-type operation -- this is because it is hard
3443 * to predict the length of a GETATTR response in v4, and thus
3444 * align the READ data correctly. This means that the GETATTR
3445 * may end up partially falling into the page cache, and we should
3446 * shift it into the 'tail' of the xdr_buf before processing.
3447 * To do this efficiently, we need to know the total length
3448 * of data received, which doesn't seem to be available outside
3451 * In the case of WRITE, we also want to put the GETATTR after
3452 * the operation -- in this case because we want to make sure
3453 * we get the post-operation mtime and size.
3455 * Both of these changes to the XDR layer would in fact be quite
3456 * minor, but I decided to leave them for a subsequent patch.
3458 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3459 unsigned int pgbase, unsigned int pglen)
3461 struct nfs4_readlink args = {
3462 .fh = NFS_FH(inode),
3467 struct nfs4_readlink_res res;
3468 struct rpc_message msg = {
3469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3474 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3477 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3478 unsigned int pgbase, unsigned int pglen)
3480 struct nfs4_exception exception = { };
3483 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3484 trace_nfs4_readlink(inode, err);
3485 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3487 } while (exception.retry);
3492 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3495 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3498 struct nfs4_label l, *ilabel = NULL;
3499 struct nfs_open_context *ctx;
3500 struct nfs4_state *state;
3504 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3506 return PTR_ERR(ctx);
3508 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3510 sattr->ia_mode &= ~current_umask();
3511 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3512 if (IS_ERR(state)) {
3513 status = PTR_ERR(state);
3517 nfs4_label_release_security(ilabel);
3518 put_nfs_open_context(ctx);
3522 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3524 struct nfs_server *server = NFS_SERVER(dir);
3525 struct nfs_removeargs args = {
3529 struct nfs_removeres res = {
3532 struct rpc_message msg = {
3533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3539 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3541 update_changeattr(dir, &res.cinfo);
3545 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3547 struct nfs4_exception exception = { };
3550 err = _nfs4_proc_remove(dir, name);
3551 trace_nfs4_remove(dir, name, err);
3552 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3554 } while (exception.retry);
3558 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3560 struct nfs_server *server = NFS_SERVER(dir);
3561 struct nfs_removeargs *args = msg->rpc_argp;
3562 struct nfs_removeres *res = msg->rpc_resp;
3564 res->server = server;
3565 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3566 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3568 nfs_fattr_init(res->dir_attr);
3571 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3573 nfs4_setup_sequence(NFS_SERVER(data->dir),
3574 &data->args.seq_args,
3579 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3581 struct nfs_unlinkdata *data = task->tk_calldata;
3582 struct nfs_removeres *res = &data->res;
3584 if (!nfs4_sequence_done(task, &res->seq_res))
3586 if (nfs4_async_handle_error(task, res->server, NULL,
3587 &data->timeout) == -EAGAIN)
3589 update_changeattr(dir, &res->cinfo);
3593 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3595 struct nfs_server *server = NFS_SERVER(dir);
3596 struct nfs_renameargs *arg = msg->rpc_argp;
3597 struct nfs_renameres *res = msg->rpc_resp;
3599 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3600 res->server = server;
3601 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3604 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3606 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3607 &data->args.seq_args,
3612 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3613 struct inode *new_dir)
3615 struct nfs_renamedata *data = task->tk_calldata;
3616 struct nfs_renameres *res = &data->res;
3618 if (!nfs4_sequence_done(task, &res->seq_res))
3620 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3623 update_changeattr(old_dir, &res->old_cinfo);
3624 update_changeattr(new_dir, &res->new_cinfo);
3628 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3630 struct nfs_server *server = NFS_SERVER(inode);
3631 struct nfs4_link_arg arg = {
3632 .fh = NFS_FH(inode),
3633 .dir_fh = NFS_FH(dir),
3635 .bitmask = server->attr_bitmask,
3637 struct nfs4_link_res res = {
3641 struct rpc_message msg = {
3642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3646 int status = -ENOMEM;
3648 res.fattr = nfs_alloc_fattr();
3649 if (res.fattr == NULL)
3652 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3653 if (IS_ERR(res.label)) {
3654 status = PTR_ERR(res.label);
3657 arg.bitmask = nfs4_bitmask(server, res.label);
3659 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3661 update_changeattr(dir, &res.cinfo);
3662 status = nfs_post_op_update_inode(inode, res.fattr);
3664 nfs_setsecurity(inode, res.fattr, res.label);
3668 nfs4_label_free(res.label);
3671 nfs_free_fattr(res.fattr);
3675 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3677 struct nfs4_exception exception = { };
3680 err = nfs4_handle_exception(NFS_SERVER(inode),
3681 _nfs4_proc_link(inode, dir, name),
3683 } while (exception.retry);
3687 struct nfs4_createdata {
3688 struct rpc_message msg;
3689 struct nfs4_create_arg arg;
3690 struct nfs4_create_res res;
3692 struct nfs_fattr fattr;
3693 struct nfs4_label *label;
3696 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3697 struct qstr *name, struct iattr *sattr, u32 ftype)
3699 struct nfs4_createdata *data;
3701 data = kzalloc(sizeof(*data), GFP_KERNEL);
3703 struct nfs_server *server = NFS_SERVER(dir);
3705 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3706 if (IS_ERR(data->label))
3709 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3710 data->msg.rpc_argp = &data->arg;
3711 data->msg.rpc_resp = &data->res;
3712 data->arg.dir_fh = NFS_FH(dir);
3713 data->arg.server = server;
3714 data->arg.name = name;
3715 data->arg.attrs = sattr;
3716 data->arg.ftype = ftype;
3717 data->arg.bitmask = nfs4_bitmask(server, data->label);
3718 data->res.server = server;
3719 data->res.fh = &data->fh;
3720 data->res.fattr = &data->fattr;
3721 data->res.label = data->label;
3722 nfs_fattr_init(data->res.fattr);
3730 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3732 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3733 &data->arg.seq_args, &data->res.seq_res, 1);
3735 update_changeattr(dir, &data->res.dir_cinfo);
3736 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3741 static void nfs4_free_createdata(struct nfs4_createdata *data)
3743 nfs4_label_free(data->label);
3747 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3748 struct page *page, unsigned int len, struct iattr *sattr,
3749 struct nfs4_label *label)
3751 struct nfs4_createdata *data;
3752 int status = -ENAMETOOLONG;
3754 if (len > NFS4_MAXPATHLEN)
3758 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3762 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3763 data->arg.u.symlink.pages = &page;
3764 data->arg.u.symlink.len = len;
3765 data->arg.label = label;
3767 status = nfs4_do_create(dir, dentry, data);
3769 nfs4_free_createdata(data);
3774 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3775 struct page *page, unsigned int len, struct iattr *sattr)
3777 struct nfs4_exception exception = { };
3778 struct nfs4_label l, *label = NULL;
3781 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3784 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3785 trace_nfs4_symlink(dir, &dentry->d_name, err);
3786 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3788 } while (exception.retry);
3790 nfs4_label_release_security(label);
3794 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3795 struct iattr *sattr, struct nfs4_label *label)
3797 struct nfs4_createdata *data;
3798 int status = -ENOMEM;
3800 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3804 data->arg.label = label;
3805 status = nfs4_do_create(dir, dentry, data);
3807 nfs4_free_createdata(data);
3812 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3813 struct iattr *sattr)
3815 struct nfs4_exception exception = { };
3816 struct nfs4_label l, *label = NULL;
3819 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3821 sattr->ia_mode &= ~current_umask();
3823 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3824 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3825 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3827 } while (exception.retry);
3828 nfs4_label_release_security(label);
3833 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3834 u64 cookie, struct page **pages, unsigned int count, int plus)
3836 struct inode *dir = dentry->d_inode;
3837 struct nfs4_readdir_arg args = {
3842 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3845 struct nfs4_readdir_res res;
3846 struct rpc_message msg = {
3847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3854 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3856 (unsigned long long)cookie);
3857 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3858 res.pgbase = args.pgbase;
3859 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3861 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3862 status += args.pgbase;
3865 nfs_invalidate_atime(dir);
3867 dprintk("%s: returns %d\n", __func__, status);
3871 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3872 u64 cookie, struct page **pages, unsigned int count, int plus)
3874 struct nfs4_exception exception = { };
3877 err = _nfs4_proc_readdir(dentry, cred, cookie,
3878 pages, count, plus);
3879 trace_nfs4_readdir(dentry->d_inode, err);
3880 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3882 } while (exception.retry);
3886 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3887 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3889 struct nfs4_createdata *data;
3890 int mode = sattr->ia_mode;
3891 int status = -ENOMEM;
3893 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3898 data->arg.ftype = NF4FIFO;
3899 else if (S_ISBLK(mode)) {
3900 data->arg.ftype = NF4BLK;
3901 data->arg.u.device.specdata1 = MAJOR(rdev);
3902 data->arg.u.device.specdata2 = MINOR(rdev);
3904 else if (S_ISCHR(mode)) {
3905 data->arg.ftype = NF4CHR;
3906 data->arg.u.device.specdata1 = MAJOR(rdev);
3907 data->arg.u.device.specdata2 = MINOR(rdev);
3908 } else if (!S_ISSOCK(mode)) {
3913 data->arg.label = label;
3914 status = nfs4_do_create(dir, dentry, data);
3916 nfs4_free_createdata(data);
3921 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3922 struct iattr *sattr, dev_t rdev)
3924 struct nfs4_exception exception = { };
3925 struct nfs4_label l, *label = NULL;
3928 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3930 sattr->ia_mode &= ~current_umask();
3932 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3933 trace_nfs4_mknod(dir, &dentry->d_name, err);
3934 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3936 } while (exception.retry);
3938 nfs4_label_release_security(label);
3943 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3944 struct nfs_fsstat *fsstat)
3946 struct nfs4_statfs_arg args = {
3948 .bitmask = server->attr_bitmask,
3950 struct nfs4_statfs_res res = {
3953 struct rpc_message msg = {
3954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3959 nfs_fattr_init(fsstat->fattr);
3960 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3963 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3965 struct nfs4_exception exception = { };
3968 err = nfs4_handle_exception(server,
3969 _nfs4_proc_statfs(server, fhandle, fsstat),
3971 } while (exception.retry);
3975 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3976 struct nfs_fsinfo *fsinfo)
3978 struct nfs4_fsinfo_arg args = {
3980 .bitmask = server->attr_bitmask,
3982 struct nfs4_fsinfo_res res = {
3985 struct rpc_message msg = {
3986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3991 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3994 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3996 struct nfs4_exception exception = { };
3997 unsigned long now = jiffies;
4001 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4002 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4004 struct nfs_client *clp = server->nfs_client;
4006 spin_lock(&clp->cl_lock);
4007 clp->cl_lease_time = fsinfo->lease_time * HZ;
4008 clp->cl_last_renewal = now;
4009 spin_unlock(&clp->cl_lock);
4012 err = nfs4_handle_exception(server, err, &exception);
4013 } while (exception.retry);
4017 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4021 nfs_fattr_init(fsinfo->fattr);
4022 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4024 /* block layout checks this! */
4025 server->pnfs_blksize = fsinfo->blksize;
4026 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4032 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4033 struct nfs_pathconf *pathconf)
4035 struct nfs4_pathconf_arg args = {
4037 .bitmask = server->attr_bitmask,
4039 struct nfs4_pathconf_res res = {
4040 .pathconf = pathconf,
4042 struct rpc_message msg = {
4043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4048 /* None of the pathconf attributes are mandatory to implement */
4049 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4050 memset(pathconf, 0, sizeof(*pathconf));
4054 nfs_fattr_init(pathconf->fattr);
4055 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4058 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4059 struct nfs_pathconf *pathconf)
4061 struct nfs4_exception exception = { };
4065 err = nfs4_handle_exception(server,
4066 _nfs4_proc_pathconf(server, fhandle, pathconf),
4068 } while (exception.retry);
4072 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4073 const struct nfs_open_context *ctx,
4074 const struct nfs_lock_context *l_ctx,
4077 const struct nfs_lockowner *lockowner = NULL;
4080 lockowner = &l_ctx->lockowner;
4081 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4083 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4085 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4086 const struct nfs_open_context *ctx,
4087 const struct nfs_lock_context *l_ctx,
4090 nfs4_stateid current_stateid;
4092 /* If the current stateid represents a lost lock, then exit */
4093 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4095 return nfs4_stateid_match(stateid, ¤t_stateid);
4098 static bool nfs4_error_stateid_expired(int err)
4101 case -NFS4ERR_DELEG_REVOKED:
4102 case -NFS4ERR_ADMIN_REVOKED:
4103 case -NFS4ERR_BAD_STATEID:
4104 case -NFS4ERR_STALE_STATEID:
4105 case -NFS4ERR_OLD_STATEID:
4106 case -NFS4ERR_OPENMODE:
4107 case -NFS4ERR_EXPIRED:
4113 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4115 nfs_invalidate_atime(hdr->inode);
4118 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4120 struct nfs_server *server = NFS_SERVER(hdr->inode);
4122 trace_nfs4_read(hdr, task->tk_status);
4123 if (nfs4_async_handle_error(task, server,
4124 hdr->args.context->state,
4126 rpc_restart_call_prepare(task);
4130 __nfs4_read_done_cb(hdr);
4131 if (task->tk_status > 0)
4132 renew_lease(server, hdr->timestamp);
4136 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4137 struct nfs_pgio_args *args)
4140 if (!nfs4_error_stateid_expired(task->tk_status) ||
4141 nfs4_stateid_is_current(&args->stateid,
4146 rpc_restart_call_prepare(task);
4150 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4153 dprintk("--> %s\n", __func__);
4155 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4157 if (nfs4_read_stateid_changed(task, &hdr->args))
4159 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4160 nfs4_read_done_cb(task, hdr);
4163 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4164 struct rpc_message *msg)
4166 hdr->timestamp = jiffies;
4167 hdr->pgio_done_cb = nfs4_read_done_cb;
4168 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4169 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4172 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4173 struct nfs_pgio_header *hdr)
4175 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4176 &hdr->args.seq_args,
4180 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4181 hdr->args.lock_context,
4182 hdr->rw_ops->rw_mode) == -EIO)
4184 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4189 static int nfs4_write_done_cb(struct rpc_task *task,
4190 struct nfs_pgio_header *hdr)
4192 struct inode *inode = hdr->inode;
4194 trace_nfs4_write(hdr, task->tk_status);
4195 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4196 hdr->args.context->state,
4198 rpc_restart_call_prepare(task);
4201 if (task->tk_status >= 0) {
4202 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4203 nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr);
4208 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4209 struct nfs_pgio_args *args)
4212 if (!nfs4_error_stateid_expired(task->tk_status) ||
4213 nfs4_stateid_is_current(&args->stateid,
4218 rpc_restart_call_prepare(task);
4222 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4224 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4226 if (nfs4_write_stateid_changed(task, &hdr->args))
4228 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4229 nfs4_write_done_cb(task, hdr);
4233 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4235 /* Don't request attributes for pNFS or O_DIRECT writes */
4236 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4238 /* Otherwise, request attributes if and only if we don't hold
4241 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4244 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4245 struct rpc_message *msg)
4247 struct nfs_server *server = NFS_SERVER(hdr->inode);
4249 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4250 hdr->args.bitmask = NULL;
4251 hdr->res.fattr = NULL;
4253 hdr->args.bitmask = server->cache_consistency_bitmask;
4255 if (!hdr->pgio_done_cb)
4256 hdr->pgio_done_cb = nfs4_write_done_cb;
4257 hdr->res.server = server;
4258 hdr->timestamp = jiffies;
4260 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4261 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4264 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4266 nfs4_setup_sequence(NFS_SERVER(data->inode),
4267 &data->args.seq_args,
4272 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4274 struct inode *inode = data->inode;
4276 trace_nfs4_commit(data, task->tk_status);
4277 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4278 NULL, NULL) == -EAGAIN) {
4279 rpc_restart_call_prepare(task);
4285 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4287 if (!nfs4_sequence_done(task, &data->res.seq_res))
4289 return data->commit_done_cb(task, data);
4292 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4294 struct nfs_server *server = NFS_SERVER(data->inode);
4296 if (data->commit_done_cb == NULL)
4297 data->commit_done_cb = nfs4_commit_done_cb;
4298 data->res.server = server;
4299 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4300 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4303 struct nfs4_renewdata {
4304 struct nfs_client *client;
4305 unsigned long timestamp;
4309 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4310 * standalone procedure for queueing an asynchronous RENEW.
4312 static void nfs4_renew_release(void *calldata)
4314 struct nfs4_renewdata *data = calldata;
4315 struct nfs_client *clp = data->client;
4317 if (atomic_read(&clp->cl_count) > 1)
4318 nfs4_schedule_state_renewal(clp);
4319 nfs_put_client(clp);
4323 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4325 struct nfs4_renewdata *data = calldata;
4326 struct nfs_client *clp = data->client;
4327 unsigned long timestamp = data->timestamp;
4329 trace_nfs4_renew_async(clp, task->tk_status);
4330 switch (task->tk_status) {
4333 case -NFS4ERR_LEASE_MOVED:
4334 nfs4_schedule_lease_moved_recovery(clp);
4337 /* Unless we're shutting down, schedule state recovery! */
4338 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4340 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4341 nfs4_schedule_lease_recovery(clp);
4344 nfs4_schedule_path_down_recovery(clp);
4346 do_renew_lease(clp, timestamp);
4349 static const struct rpc_call_ops nfs4_renew_ops = {
4350 .rpc_call_done = nfs4_renew_done,
4351 .rpc_release = nfs4_renew_release,
4354 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4356 struct rpc_message msg = {
4357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4361 struct nfs4_renewdata *data;
4363 if (renew_flags == 0)
4365 if (!atomic_inc_not_zero(&clp->cl_count))
4367 data = kmalloc(sizeof(*data), GFP_NOFS);
4371 data->timestamp = jiffies;
4372 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4373 &nfs4_renew_ops, data);
4376 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4378 struct rpc_message msg = {
4379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4383 unsigned long now = jiffies;
4386 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4389 do_renew_lease(clp, now);
4393 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4395 return server->caps & NFS_CAP_ACLS;
4398 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4399 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4402 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4404 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4405 struct page **pages, unsigned int *pgbase)
4407 struct page *newpage, **spages;
4413 len = min_t(size_t, PAGE_SIZE, buflen);
4414 newpage = alloc_page(GFP_KERNEL);
4416 if (newpage == NULL)
4418 memcpy(page_address(newpage), buf, len);
4423 } while (buflen != 0);
4429 __free_page(spages[rc-1]);
4433 struct nfs4_cached_acl {
4439 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4441 struct nfs_inode *nfsi = NFS_I(inode);
4443 spin_lock(&inode->i_lock);
4444 kfree(nfsi->nfs4_acl);
4445 nfsi->nfs4_acl = acl;
4446 spin_unlock(&inode->i_lock);
4449 static void nfs4_zap_acl_attr(struct inode *inode)
4451 nfs4_set_cached_acl(inode, NULL);
4454 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4456 struct nfs_inode *nfsi = NFS_I(inode);
4457 struct nfs4_cached_acl *acl;
4460 spin_lock(&inode->i_lock);
4461 acl = nfsi->nfs4_acl;
4464 if (buf == NULL) /* user is just asking for length */
4466 if (acl->cached == 0)
4468 ret = -ERANGE; /* see getxattr(2) man page */
4469 if (acl->len > buflen)
4471 memcpy(buf, acl->data, acl->len);
4475 spin_unlock(&inode->i_lock);
4479 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4481 struct nfs4_cached_acl *acl;
4482 size_t buflen = sizeof(*acl) + acl_len;
4484 if (buflen <= PAGE_SIZE) {
4485 acl = kmalloc(buflen, GFP_KERNEL);
4489 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4491 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4498 nfs4_set_cached_acl(inode, acl);
4502 * The getxattr API returns the required buffer length when called with a
4503 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4504 * the required buf. On a NULL buf, we send a page of data to the server
4505 * guessing that the ACL request can be serviced by a page. If so, we cache
4506 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4507 * the cache. If not so, we throw away the page, and cache the required
4508 * length. The next getxattr call will then produce another round trip to
4509 * the server, this time with the input buf of the required size.
4511 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4513 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4514 struct nfs_getaclargs args = {
4515 .fh = NFS_FH(inode),
4519 struct nfs_getaclres res = {
4522 struct rpc_message msg = {
4523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4527 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4528 int ret = -ENOMEM, i;
4530 /* As long as we're doing a round trip to the server anyway,
4531 * let's be prepared for a page of acl data. */
4534 if (npages > ARRAY_SIZE(pages))
4537 for (i = 0; i < npages; i++) {
4538 pages[i] = alloc_page(GFP_KERNEL);
4543 /* for decoding across pages */
4544 res.acl_scratch = alloc_page(GFP_KERNEL);
4545 if (!res.acl_scratch)
4548 args.acl_len = npages * PAGE_SIZE;
4549 args.acl_pgbase = 0;
4551 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4552 __func__, buf, buflen, npages, args.acl_len);
4553 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4554 &msg, &args.seq_args, &res.seq_res, 0);
4558 /* Handle the case where the passed-in buffer is too short */
4559 if (res.acl_flags & NFS4_ACL_TRUNC) {
4560 /* Did the user only issue a request for the acl length? */
4566 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4568 if (res.acl_len > buflen) {
4572 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4577 for (i = 0; i < npages; i++)
4579 __free_page(pages[i]);
4580 if (res.acl_scratch)
4581 __free_page(res.acl_scratch);
4585 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4587 struct nfs4_exception exception = { };
4590 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4591 trace_nfs4_get_acl(inode, ret);
4594 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4595 } while (exception.retry);
4599 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4601 struct nfs_server *server = NFS_SERVER(inode);
4604 if (!nfs4_server_supports_acls(server))
4606 ret = nfs_revalidate_inode(server, inode);
4609 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4610 nfs_zap_acl_cache(inode);
4611 ret = nfs4_read_cached_acl(inode, buf, buflen);
4613 /* -ENOENT is returned if there is no ACL or if there is an ACL
4614 * but no cached acl data, just the acl length */
4616 return nfs4_get_acl_uncached(inode, buf, buflen);
4619 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4621 struct nfs_server *server = NFS_SERVER(inode);
4622 struct page *pages[NFS4ACL_MAXPAGES];
4623 struct nfs_setaclargs arg = {
4624 .fh = NFS_FH(inode),
4628 struct nfs_setaclres res;
4629 struct rpc_message msg = {
4630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4634 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4637 if (!nfs4_server_supports_acls(server))
4639 if (npages > ARRAY_SIZE(pages))
4641 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4644 nfs4_inode_return_delegation(inode);
4645 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4648 * Free each page after tx, so the only ref left is
4649 * held by the network stack
4652 put_page(pages[i-1]);
4655 * Acl update can result in inode attribute update.
4656 * so mark the attribute cache invalid.
4658 spin_lock(&inode->i_lock);
4659 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4660 spin_unlock(&inode->i_lock);
4661 nfs_access_zap_cache(inode);
4662 nfs_zap_acl_cache(inode);
4666 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4668 struct nfs4_exception exception = { };
4671 err = __nfs4_proc_set_acl(inode, buf, buflen);
4672 trace_nfs4_set_acl(inode, err);
4673 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4675 } while (exception.retry);
4679 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4680 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4683 struct nfs_server *server = NFS_SERVER(inode);
4684 struct nfs_fattr fattr;
4685 struct nfs4_label label = {0, 0, buflen, buf};
4687 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4688 struct nfs4_getattr_arg arg = {
4689 .fh = NFS_FH(inode),
4692 struct nfs4_getattr_res res = {
4697 struct rpc_message msg = {
4698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4704 nfs_fattr_init(&fattr);
4706 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4709 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4711 if (buflen < label.len)
4716 static int nfs4_get_security_label(struct inode *inode, void *buf,
4719 struct nfs4_exception exception = { };
4722 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4726 err = _nfs4_get_security_label(inode, buf, buflen);
4727 trace_nfs4_get_security_label(inode, err);
4728 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4730 } while (exception.retry);
4734 static int _nfs4_do_set_security_label(struct inode *inode,
4735 struct nfs4_label *ilabel,
4736 struct nfs_fattr *fattr,
4737 struct nfs4_label *olabel)
4740 struct iattr sattr = {0};
4741 struct nfs_server *server = NFS_SERVER(inode);
4742 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4743 struct nfs_setattrargs arg = {
4744 .fh = NFS_FH(inode),
4750 struct nfs_setattrres res = {
4755 struct rpc_message msg = {
4756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4762 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4764 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4766 dprintk("%s failed: %d\n", __func__, status);
4771 static int nfs4_do_set_security_label(struct inode *inode,
4772 struct nfs4_label *ilabel,
4773 struct nfs_fattr *fattr,
4774 struct nfs4_label *olabel)
4776 struct nfs4_exception exception = { };
4780 err = _nfs4_do_set_security_label(inode, ilabel,
4782 trace_nfs4_set_security_label(inode, err);
4783 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4785 } while (exception.retry);
4790 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4792 struct nfs4_label ilabel, *olabel = NULL;
4793 struct nfs_fattr fattr;
4794 struct rpc_cred *cred;
4795 struct inode *inode = dentry->d_inode;
4798 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4801 nfs_fattr_init(&fattr);
4805 ilabel.label = (char *)buf;
4806 ilabel.len = buflen;
4808 cred = rpc_lookup_cred();
4810 return PTR_ERR(cred);
4812 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4813 if (IS_ERR(olabel)) {
4814 status = -PTR_ERR(olabel);
4818 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4820 nfs_setsecurity(inode, &fattr, olabel);
4822 nfs4_label_free(olabel);
4827 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4831 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4832 struct nfs4_state *state, long *timeout)
4834 struct nfs_client *clp = server->nfs_client;
4836 if (task->tk_status >= 0)
4838 switch(task->tk_status) {
4839 case -NFS4ERR_DELEG_REVOKED:
4840 case -NFS4ERR_ADMIN_REVOKED:
4841 case -NFS4ERR_BAD_STATEID:
4842 case -NFS4ERR_OPENMODE:
4845 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4846 goto recovery_failed;
4847 goto wait_on_recovery;
4848 case -NFS4ERR_EXPIRED:
4849 if (state != NULL) {
4850 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4851 goto recovery_failed;
4853 case -NFS4ERR_STALE_STATEID:
4854 case -NFS4ERR_STALE_CLIENTID:
4855 nfs4_schedule_lease_recovery(clp);
4856 goto wait_on_recovery;
4857 case -NFS4ERR_MOVED:
4858 if (nfs4_schedule_migration_recovery(server) < 0)
4859 goto recovery_failed;
4860 goto wait_on_recovery;
4861 case -NFS4ERR_LEASE_MOVED:
4862 nfs4_schedule_lease_moved_recovery(clp);
4863 goto wait_on_recovery;
4864 #if defined(CONFIG_NFS_V4_1)
4865 case -NFS4ERR_BADSESSION:
4866 case -NFS4ERR_BADSLOT:
4867 case -NFS4ERR_BAD_HIGH_SLOT:
4868 case -NFS4ERR_DEADSESSION:
4869 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4870 case -NFS4ERR_SEQ_FALSE_RETRY:
4871 case -NFS4ERR_SEQ_MISORDERED:
4872 dprintk("%s ERROR %d, Reset session\n", __func__,
4874 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4875 goto wait_on_recovery;
4876 #endif /* CONFIG_NFS_V4_1 */
4877 case -NFS4ERR_DELAY:
4878 nfs_inc_server_stats(server, NFSIOS_DELAY);
4879 rpc_delay(task, nfs4_update_delay(timeout));
4881 case -NFS4ERR_GRACE:
4882 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4883 case -NFS4ERR_RETRY_UNCACHED_REP:
4884 case -NFS4ERR_OLD_STATEID:
4887 task->tk_status = nfs4_map_errors(task->tk_status);
4890 task->tk_status = -EIO;
4893 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4894 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4895 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4896 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4897 goto recovery_failed;
4899 task->tk_status = 0;
4903 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4904 nfs4_verifier *bootverf)
4908 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4909 /* An impossible timestamp guarantees this value
4910 * will never match a generated boot time. */
4912 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4914 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4915 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4916 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4918 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4922 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4923 char *buf, size_t len)
4925 unsigned int result;
4928 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4930 rpc_peeraddr2str(clp->cl_rpcclient,
4932 rpc_peeraddr2str(clp->cl_rpcclient,
4933 RPC_DISPLAY_PROTO));
4939 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4940 char *buf, size_t len)
4942 const char *nodename = clp->cl_rpcclient->cl_nodename;
4944 if (nfs4_client_id_uniquifier[0] != '\0')
4945 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4946 clp->rpc_ops->version,
4947 clp->cl_minorversion,
4948 nfs4_client_id_uniquifier,
4950 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4951 clp->rpc_ops->version, clp->cl_minorversion,
4956 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4957 * services. Advertise one based on the address family of the
4961 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4963 if (strchr(clp->cl_ipaddr, ':') != NULL)
4964 return scnprintf(buf, len, "tcp6");
4966 return scnprintf(buf, len, "tcp");
4969 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
4971 struct nfs4_setclientid *sc = calldata;
4973 if (task->tk_status == 0)
4974 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
4977 static const struct rpc_call_ops nfs4_setclientid_ops = {
4978 .rpc_call_done = nfs4_setclientid_done,
4982 * nfs4_proc_setclientid - Negotiate client ID
4983 * @clp: state data structure
4984 * @program: RPC program for NFSv4 callback service
4985 * @port: IP port number for NFS4 callback service
4986 * @cred: RPC credential to use for this call
4987 * @res: where to place the result
4989 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4991 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4992 unsigned short port, struct rpc_cred *cred,
4993 struct nfs4_setclientid_res *res)
4995 nfs4_verifier sc_verifier;
4996 struct nfs4_setclientid setclientid = {
4997 .sc_verifier = &sc_verifier,
4999 .sc_cb_ident = clp->cl_cb_ident,
5001 struct rpc_message msg = {
5002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5003 .rpc_argp = &setclientid,
5007 struct rpc_task *task;
5008 struct rpc_task_setup task_setup_data = {
5009 .rpc_client = clp->cl_rpcclient,
5010 .rpc_message = &msg,
5011 .callback_ops = &nfs4_setclientid_ops,
5012 .callback_data = &setclientid,
5013 .flags = RPC_TASK_TIMEOUT,
5017 /* nfs_client_id4 */
5018 nfs4_init_boot_verifier(clp, &sc_verifier);
5019 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5020 setclientid.sc_name_len =
5021 nfs4_init_uniform_client_string(clp,
5022 setclientid.sc_name,
5023 sizeof(setclientid.sc_name));
5025 setclientid.sc_name_len =
5026 nfs4_init_nonuniform_client_string(clp,
5027 setclientid.sc_name,
5028 sizeof(setclientid.sc_name));
5030 setclientid.sc_netid_len =
5031 nfs4_init_callback_netid(clp,
5032 setclientid.sc_netid,
5033 sizeof(setclientid.sc_netid));
5034 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5035 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5036 clp->cl_ipaddr, port >> 8, port & 255);
5038 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
5039 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5040 setclientid.sc_name_len, setclientid.sc_name);
5041 task = rpc_run_task(&task_setup_data);
5043 status = PTR_ERR(task);
5046 status = task->tk_status;
5047 if (setclientid.sc_cred) {
5048 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5049 put_rpccred(setclientid.sc_cred);
5053 trace_nfs4_setclientid(clp, status);
5054 dprintk("NFS reply setclientid: %d\n", status);
5059 * nfs4_proc_setclientid_confirm - Confirm client ID
5060 * @clp: state data structure
5061 * @res: result of a previous SETCLIENTID
5062 * @cred: RPC credential to use for this call
5064 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5066 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5067 struct nfs4_setclientid_res *arg,
5068 struct rpc_cred *cred)
5070 struct rpc_message msg = {
5071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5077 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5078 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5080 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5081 trace_nfs4_setclientid_confirm(clp, status);
5082 dprintk("NFS reply setclientid_confirm: %d\n", status);
5086 struct nfs4_delegreturndata {
5087 struct nfs4_delegreturnargs args;
5088 struct nfs4_delegreturnres res;
5090 nfs4_stateid stateid;
5091 unsigned long timestamp;
5092 struct nfs_fattr fattr;
5094 struct inode *inode;
5099 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5101 struct nfs4_delegreturndata *data = calldata;
5103 if (!nfs4_sequence_done(task, &data->res.seq_res))
5106 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5107 switch (task->tk_status) {
5109 renew_lease(data->res.server, data->timestamp);
5110 case -NFS4ERR_ADMIN_REVOKED:
5111 case -NFS4ERR_DELEG_REVOKED:
5112 case -NFS4ERR_BAD_STATEID:
5113 case -NFS4ERR_OLD_STATEID:
5114 case -NFS4ERR_STALE_STATEID:
5115 case -NFS4ERR_EXPIRED:
5116 task->tk_status = 0;
5118 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5121 if (nfs4_async_handle_error(task, data->res.server,
5122 NULL, NULL) == -EAGAIN) {
5123 rpc_restart_call_prepare(task);
5127 data->rpc_status = task->tk_status;
5130 static void nfs4_delegreturn_release(void *calldata)
5132 struct nfs4_delegreturndata *data = calldata;
5135 pnfs_roc_release(data->inode);
5139 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5141 struct nfs4_delegreturndata *d_data;
5143 d_data = (struct nfs4_delegreturndata *)data;
5146 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5149 nfs4_setup_sequence(d_data->res.server,
5150 &d_data->args.seq_args,
5151 &d_data->res.seq_res,
5155 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5156 .rpc_call_prepare = nfs4_delegreturn_prepare,
5157 .rpc_call_done = nfs4_delegreturn_done,
5158 .rpc_release = nfs4_delegreturn_release,
5161 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5163 struct nfs4_delegreturndata *data;
5164 struct nfs_server *server = NFS_SERVER(inode);
5165 struct rpc_task *task;
5166 struct rpc_message msg = {
5167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5170 struct rpc_task_setup task_setup_data = {
5171 .rpc_client = server->client,
5172 .rpc_message = &msg,
5173 .callback_ops = &nfs4_delegreturn_ops,
5174 .flags = RPC_TASK_ASYNC,
5178 data = kzalloc(sizeof(*data), GFP_NOFS);
5181 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5182 data->args.fhandle = &data->fh;
5183 data->args.stateid = &data->stateid;
5184 data->args.bitmask = server->cache_consistency_bitmask;
5185 nfs_copy_fh(&data->fh, NFS_FH(inode));
5186 nfs4_stateid_copy(&data->stateid, stateid);
5187 data->res.fattr = &data->fattr;
5188 data->res.server = server;
5189 nfs_fattr_init(data->res.fattr);
5190 data->timestamp = jiffies;
5191 data->rpc_status = 0;
5192 data->inode = inode;
5193 data->roc = list_empty(&NFS_I(inode)->open_files) ?
5194 pnfs_roc(inode) : false;
5196 task_setup_data.callback_data = data;
5197 msg.rpc_argp = &data->args;
5198 msg.rpc_resp = &data->res;
5199 task = rpc_run_task(&task_setup_data);
5201 return PTR_ERR(task);
5204 status = nfs4_wait_for_completion_rpc_task(task);
5207 status = data->rpc_status;
5209 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5211 nfs_refresh_inode(inode, &data->fattr);
5217 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5219 struct nfs_server *server = NFS_SERVER(inode);
5220 struct nfs4_exception exception = { };
5223 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5224 trace_nfs4_delegreturn(inode, err);
5226 case -NFS4ERR_STALE_STATEID:
5227 case -NFS4ERR_EXPIRED:
5231 err = nfs4_handle_exception(server, err, &exception);
5232 } while (exception.retry);
5236 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5237 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5240 * sleep, with exponential backoff, and retry the LOCK operation.
5242 static unsigned long
5243 nfs4_set_lock_task_retry(unsigned long timeout)
5245 freezable_schedule_timeout_killable_unsafe(timeout);
5247 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5248 return NFS4_LOCK_MAXTIMEOUT;
5252 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5254 struct inode *inode = state->inode;
5255 struct nfs_server *server = NFS_SERVER(inode);
5256 struct nfs_client *clp = server->nfs_client;
5257 struct nfs_lockt_args arg = {
5258 .fh = NFS_FH(inode),
5261 struct nfs_lockt_res res = {
5264 struct rpc_message msg = {
5265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5268 .rpc_cred = state->owner->so_cred,
5270 struct nfs4_lock_state *lsp;
5273 arg.lock_owner.clientid = clp->cl_clientid;
5274 status = nfs4_set_lock_state(state, request);
5277 lsp = request->fl_u.nfs4_fl.owner;
5278 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5279 arg.lock_owner.s_dev = server->s_dev;
5280 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5283 request->fl_type = F_UNLCK;
5285 case -NFS4ERR_DENIED:
5288 request->fl_ops->fl_release_private(request);
5289 request->fl_ops = NULL;
5294 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5296 struct nfs4_exception exception = { };
5300 err = _nfs4_proc_getlk(state, cmd, request);
5301 trace_nfs4_get_lock(request, state, cmd, err);
5302 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5304 } while (exception.retry);
5308 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5311 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5313 res = posix_lock_file_wait(file, fl);
5316 res = flock_lock_file_wait(file, fl);
5324 struct nfs4_unlockdata {
5325 struct nfs_locku_args arg;
5326 struct nfs_locku_res res;
5327 struct nfs4_lock_state *lsp;
5328 struct nfs_open_context *ctx;
5329 struct file_lock fl;
5330 const struct nfs_server *server;
5331 unsigned long timestamp;
5334 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5335 struct nfs_open_context *ctx,
5336 struct nfs4_lock_state *lsp,
5337 struct nfs_seqid *seqid)
5339 struct nfs4_unlockdata *p;
5340 struct inode *inode = lsp->ls_state->inode;
5342 p = kzalloc(sizeof(*p), GFP_NOFS);
5345 p->arg.fh = NFS_FH(inode);
5347 p->arg.seqid = seqid;
5348 p->res.seqid = seqid;
5349 p->arg.stateid = &lsp->ls_stateid;
5351 atomic_inc(&lsp->ls_count);
5352 /* Ensure we don't close file until we're done freeing locks! */
5353 p->ctx = get_nfs_open_context(ctx);
5354 memcpy(&p->fl, fl, sizeof(p->fl));
5355 p->server = NFS_SERVER(inode);
5359 static void nfs4_locku_release_calldata(void *data)
5361 struct nfs4_unlockdata *calldata = data;
5362 nfs_free_seqid(calldata->arg.seqid);
5363 nfs4_put_lock_state(calldata->lsp);
5364 put_nfs_open_context(calldata->ctx);
5368 static void nfs4_locku_done(struct rpc_task *task, void *data)
5370 struct nfs4_unlockdata *calldata = data;
5372 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5374 switch (task->tk_status) {
5376 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5377 &calldata->res.stateid);
5378 renew_lease(calldata->server, calldata->timestamp);
5380 case -NFS4ERR_BAD_STATEID:
5381 case -NFS4ERR_OLD_STATEID:
5382 case -NFS4ERR_STALE_STATEID:
5383 case -NFS4ERR_EXPIRED:
5386 if (nfs4_async_handle_error(task, calldata->server,
5387 NULL, NULL) == -EAGAIN)
5388 rpc_restart_call_prepare(task);
5390 nfs_release_seqid(calldata->arg.seqid);
5393 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5395 struct nfs4_unlockdata *calldata = data;
5397 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5399 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5400 /* Note: exit _without_ running nfs4_locku_done */
5403 calldata->timestamp = jiffies;
5404 if (nfs4_setup_sequence(calldata->server,
5405 &calldata->arg.seq_args,
5406 &calldata->res.seq_res,
5408 nfs_release_seqid(calldata->arg.seqid);
5411 task->tk_action = NULL;
5413 nfs4_sequence_done(task, &calldata->res.seq_res);
5416 static const struct rpc_call_ops nfs4_locku_ops = {
5417 .rpc_call_prepare = nfs4_locku_prepare,
5418 .rpc_call_done = nfs4_locku_done,
5419 .rpc_release = nfs4_locku_release_calldata,
5422 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5423 struct nfs_open_context *ctx,
5424 struct nfs4_lock_state *lsp,
5425 struct nfs_seqid *seqid)
5427 struct nfs4_unlockdata *data;
5428 struct rpc_message msg = {
5429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5430 .rpc_cred = ctx->cred,
5432 struct rpc_task_setup task_setup_data = {
5433 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5434 .rpc_message = &msg,
5435 .callback_ops = &nfs4_locku_ops,
5436 .workqueue = nfsiod_workqueue,
5437 .flags = RPC_TASK_ASYNC,
5440 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5441 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5443 /* Ensure this is an unlock - when canceling a lock, the
5444 * canceled lock is passed in, and it won't be an unlock.
5446 fl->fl_type = F_UNLCK;
5448 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5450 nfs_free_seqid(seqid);
5451 return ERR_PTR(-ENOMEM);
5454 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5455 msg.rpc_argp = &data->arg;
5456 msg.rpc_resp = &data->res;
5457 task_setup_data.callback_data = data;
5458 return rpc_run_task(&task_setup_data);
5461 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5463 struct inode *inode = state->inode;
5464 struct nfs4_state_owner *sp = state->owner;
5465 struct nfs_inode *nfsi = NFS_I(inode);
5466 struct nfs_seqid *seqid;
5467 struct nfs4_lock_state *lsp;
5468 struct rpc_task *task;
5470 unsigned char fl_flags = request->fl_flags;
5472 status = nfs4_set_lock_state(state, request);
5473 /* Unlock _before_ we do the RPC call */
5474 request->fl_flags |= FL_EXISTS;
5475 /* Exclude nfs_delegation_claim_locks() */
5476 mutex_lock(&sp->so_delegreturn_mutex);
5477 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5478 down_read(&nfsi->rwsem);
5479 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5480 up_read(&nfsi->rwsem);
5481 mutex_unlock(&sp->so_delegreturn_mutex);
5484 up_read(&nfsi->rwsem);
5485 mutex_unlock(&sp->so_delegreturn_mutex);
5488 /* Is this a delegated lock? */
5489 lsp = request->fl_u.nfs4_fl.owner;
5490 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5492 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5496 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5497 status = PTR_ERR(task);
5500 status = nfs4_wait_for_completion_rpc_task(task);
5503 request->fl_flags = fl_flags;
5504 trace_nfs4_unlock(request, state, F_SETLK, status);
5508 struct nfs4_lockdata {
5509 struct nfs_lock_args arg;
5510 struct nfs_lock_res res;
5511 struct nfs4_lock_state *lsp;
5512 struct nfs_open_context *ctx;
5513 struct file_lock fl;
5514 unsigned long timestamp;
5517 struct nfs_server *server;
5520 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5521 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5524 struct nfs4_lockdata *p;
5525 struct inode *inode = lsp->ls_state->inode;
5526 struct nfs_server *server = NFS_SERVER(inode);
5528 p = kzalloc(sizeof(*p), gfp_mask);
5532 p->arg.fh = NFS_FH(inode);
5534 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5535 if (p->arg.open_seqid == NULL)
5537 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5538 if (p->arg.lock_seqid == NULL)
5539 goto out_free_seqid;
5540 p->arg.lock_stateid = &lsp->ls_stateid;
5541 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5542 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5543 p->arg.lock_owner.s_dev = server->s_dev;
5544 p->res.lock_seqid = p->arg.lock_seqid;
5547 atomic_inc(&lsp->ls_count);
5548 p->ctx = get_nfs_open_context(ctx);
5549 memcpy(&p->fl, fl, sizeof(p->fl));
5552 nfs_free_seqid(p->arg.open_seqid);
5558 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5560 struct nfs4_lockdata *data = calldata;
5561 struct nfs4_state *state = data->lsp->ls_state;
5563 dprintk("%s: begin!\n", __func__);
5564 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5566 /* Do we need to do an open_to_lock_owner? */
5567 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5568 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5569 goto out_release_lock_seqid;
5571 data->arg.open_stateid = &state->open_stateid;
5572 data->arg.new_lock_owner = 1;
5573 data->res.open_seqid = data->arg.open_seqid;
5575 data->arg.new_lock_owner = 0;
5576 if (!nfs4_valid_open_stateid(state)) {
5577 data->rpc_status = -EBADF;
5578 task->tk_action = NULL;
5579 goto out_release_open_seqid;
5581 data->timestamp = jiffies;
5582 if (nfs4_setup_sequence(data->server,
5583 &data->arg.seq_args,
5587 out_release_open_seqid:
5588 nfs_release_seqid(data->arg.open_seqid);
5589 out_release_lock_seqid:
5590 nfs_release_seqid(data->arg.lock_seqid);
5592 nfs4_sequence_done(task, &data->res.seq_res);
5593 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5596 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5598 struct nfs4_lockdata *data = calldata;
5600 dprintk("%s: begin!\n", __func__);
5602 if (!nfs4_sequence_done(task, &data->res.seq_res))
5605 data->rpc_status = task->tk_status;
5606 if (data->arg.new_lock_owner != 0) {
5607 if (data->rpc_status == 0)
5608 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5612 if (data->rpc_status == 0) {
5613 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5614 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5615 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5618 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5621 static void nfs4_lock_release(void *calldata)
5623 struct nfs4_lockdata *data = calldata;
5625 dprintk("%s: begin!\n", __func__);
5626 nfs_free_seqid(data->arg.open_seqid);
5627 if (data->cancelled != 0) {
5628 struct rpc_task *task;
5629 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5630 data->arg.lock_seqid);
5632 rpc_put_task_async(task);
5633 dprintk("%s: cancelling lock!\n", __func__);
5635 nfs_free_seqid(data->arg.lock_seqid);
5636 nfs4_put_lock_state(data->lsp);
5637 put_nfs_open_context(data->ctx);
5639 dprintk("%s: done!\n", __func__);
5642 static const struct rpc_call_ops nfs4_lock_ops = {
5643 .rpc_call_prepare = nfs4_lock_prepare,
5644 .rpc_call_done = nfs4_lock_done,
5645 .rpc_release = nfs4_lock_release,
5648 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5651 case -NFS4ERR_ADMIN_REVOKED:
5652 case -NFS4ERR_BAD_STATEID:
5653 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5654 if (new_lock_owner != 0 ||
5655 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5656 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5658 case -NFS4ERR_STALE_STATEID:
5659 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5660 case -NFS4ERR_EXPIRED:
5661 nfs4_schedule_lease_recovery(server->nfs_client);
5665 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5667 struct nfs4_lockdata *data;
5668 struct rpc_task *task;
5669 struct rpc_message msg = {
5670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5671 .rpc_cred = state->owner->so_cred,
5673 struct rpc_task_setup task_setup_data = {
5674 .rpc_client = NFS_CLIENT(state->inode),
5675 .rpc_message = &msg,
5676 .callback_ops = &nfs4_lock_ops,
5677 .workqueue = nfsiod_workqueue,
5678 .flags = RPC_TASK_ASYNC,
5682 dprintk("%s: begin!\n", __func__);
5683 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5684 fl->fl_u.nfs4_fl.owner,
5685 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5689 data->arg.block = 1;
5690 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5691 msg.rpc_argp = &data->arg;
5692 msg.rpc_resp = &data->res;
5693 task_setup_data.callback_data = data;
5694 if (recovery_type > NFS_LOCK_NEW) {
5695 if (recovery_type == NFS_LOCK_RECLAIM)
5696 data->arg.reclaim = NFS_LOCK_RECLAIM;
5697 nfs4_set_sequence_privileged(&data->arg.seq_args);
5699 task = rpc_run_task(&task_setup_data);
5701 return PTR_ERR(task);
5702 ret = nfs4_wait_for_completion_rpc_task(task);
5704 ret = data->rpc_status;
5706 nfs4_handle_setlk_error(data->server, data->lsp,
5707 data->arg.new_lock_owner, ret);
5709 data->cancelled = 1;
5711 dprintk("%s: done, ret = %d!\n", __func__, ret);
5715 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5717 struct nfs_server *server = NFS_SERVER(state->inode);
5718 struct nfs4_exception exception = {
5719 .inode = state->inode,
5724 /* Cache the lock if possible... */
5725 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5727 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5728 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5729 if (err != -NFS4ERR_DELAY)
5731 nfs4_handle_exception(server, err, &exception);
5732 } while (exception.retry);
5736 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5738 struct nfs_server *server = NFS_SERVER(state->inode);
5739 struct nfs4_exception exception = {
5740 .inode = state->inode,
5744 err = nfs4_set_lock_state(state, request);
5747 if (!recover_lost_locks) {
5748 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5752 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5754 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5755 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5759 case -NFS4ERR_GRACE:
5760 case -NFS4ERR_DELAY:
5761 nfs4_handle_exception(server, err, &exception);
5764 } while (exception.retry);
5769 #if defined(CONFIG_NFS_V4_1)
5771 * nfs41_check_expired_locks - possibly free a lock stateid
5773 * @state: NFSv4 state for an inode
5775 * Returns NFS_OK if recovery for this stateid is now finished.
5776 * Otherwise a negative NFS4ERR value is returned.
5778 static int nfs41_check_expired_locks(struct nfs4_state *state)
5780 int status, ret = -NFS4ERR_BAD_STATEID;
5781 struct nfs4_lock_state *lsp;
5782 struct nfs_server *server = NFS_SERVER(state->inode);
5784 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5785 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5786 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5788 status = nfs41_test_stateid(server,
5791 trace_nfs4_test_lock_stateid(state, lsp, status);
5792 if (status != NFS_OK) {
5793 /* Free the stateid unless the server
5794 * informs us the stateid is unrecognized. */
5795 if (status != -NFS4ERR_BAD_STATEID)
5796 nfs41_free_stateid(server,
5799 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5808 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5810 int status = NFS_OK;
5812 if (test_bit(LK_STATE_IN_USE, &state->flags))
5813 status = nfs41_check_expired_locks(state);
5814 if (status != NFS_OK)
5815 status = nfs4_lock_expired(state, request);
5820 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5822 struct nfs4_state_owner *sp = state->owner;
5823 struct nfs_inode *nfsi = NFS_I(state->inode);
5824 unsigned char fl_flags = request->fl_flags;
5826 int status = -ENOLCK;
5828 if ((fl_flags & FL_POSIX) &&
5829 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5831 /* Is this a delegated open? */
5832 status = nfs4_set_lock_state(state, request);
5835 request->fl_flags |= FL_ACCESS;
5836 status = do_vfs_lock(request->fl_file, request);
5839 down_read(&nfsi->rwsem);
5840 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5841 /* Yes: cache locks! */
5842 /* ...but avoid races with delegation recall... */
5843 request->fl_flags = fl_flags & ~FL_SLEEP;
5844 status = do_vfs_lock(request->fl_file, request);
5847 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5848 up_read(&nfsi->rwsem);
5849 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5852 down_read(&nfsi->rwsem);
5853 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5854 status = -NFS4ERR_DELAY;
5857 /* Note: we always want to sleep here! */
5858 request->fl_flags = fl_flags | FL_SLEEP;
5859 if (do_vfs_lock(request->fl_file, request) < 0)
5860 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5861 "manager!\n", __func__);
5863 up_read(&nfsi->rwsem);
5865 request->fl_flags = fl_flags;
5869 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5871 struct nfs4_exception exception = {
5873 .inode = state->inode,
5878 err = _nfs4_proc_setlk(state, cmd, request);
5879 trace_nfs4_set_lock(request, state, cmd, err);
5880 if (err == -NFS4ERR_DENIED)
5882 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5884 } while (exception.retry);
5889 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5891 struct nfs_open_context *ctx;
5892 struct nfs4_state *state;
5893 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5896 /* verify open state */
5897 ctx = nfs_file_open_context(filp);
5900 if (request->fl_start < 0 || request->fl_end < 0)
5903 if (IS_GETLK(cmd)) {
5905 return nfs4_proc_getlk(state, F_GETLK, request);
5909 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5912 if (request->fl_type == F_UNLCK) {
5914 return nfs4_proc_unlck(state, cmd, request);
5921 * Don't rely on the VFS having checked the file open mode,
5922 * since it won't do this for flock() locks.
5924 switch (request->fl_type) {
5926 if (!(filp->f_mode & FMODE_READ))
5930 if (!(filp->f_mode & FMODE_WRITE))
5935 status = nfs4_proc_setlk(state, cmd, request);
5936 if ((status != -EAGAIN) || IS_SETLK(cmd))
5938 timeout = nfs4_set_lock_task_retry(timeout);
5939 status = -ERESTARTSYS;
5942 } while(status < 0);
5946 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5948 struct nfs_server *server = NFS_SERVER(state->inode);
5951 err = nfs4_set_lock_state(state, fl);
5954 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5955 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5958 struct nfs_release_lockowner_data {
5959 struct nfs4_lock_state *lsp;
5960 struct nfs_server *server;
5961 struct nfs_release_lockowner_args args;
5962 struct nfs_release_lockowner_res res;
5963 unsigned long timestamp;
5966 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5968 struct nfs_release_lockowner_data *data = calldata;
5969 struct nfs_server *server = data->server;
5970 nfs40_setup_sequence(server, &data->args.seq_args,
5971 &data->res.seq_res, task);
5972 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5973 data->timestamp = jiffies;
5976 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5978 struct nfs_release_lockowner_data *data = calldata;
5979 struct nfs_server *server = data->server;
5981 nfs40_sequence_done(task, &data->res.seq_res);
5983 switch (task->tk_status) {
5985 renew_lease(server, data->timestamp);
5987 case -NFS4ERR_STALE_CLIENTID:
5988 case -NFS4ERR_EXPIRED:
5989 nfs4_schedule_lease_recovery(server->nfs_client);
5991 case -NFS4ERR_LEASE_MOVED:
5992 case -NFS4ERR_DELAY:
5993 if (nfs4_async_handle_error(task, server,
5994 NULL, NULL) == -EAGAIN)
5995 rpc_restart_call_prepare(task);
5999 static void nfs4_release_lockowner_release(void *calldata)
6001 struct nfs_release_lockowner_data *data = calldata;
6002 nfs4_free_lock_state(data->server, data->lsp);
6006 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6007 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6008 .rpc_call_done = nfs4_release_lockowner_done,
6009 .rpc_release = nfs4_release_lockowner_release,
6013 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6015 struct nfs_release_lockowner_data *data;
6016 struct rpc_message msg = {
6017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6020 if (server->nfs_client->cl_mvops->minor_version != 0)
6023 data = kmalloc(sizeof(*data), GFP_NOFS);
6027 data->server = server;
6028 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6029 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6030 data->args.lock_owner.s_dev = server->s_dev;
6032 msg.rpc_argp = &data->args;
6033 msg.rpc_resp = &data->res;
6034 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6035 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6038 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6040 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6041 const void *buf, size_t buflen,
6042 int flags, int type)
6044 if (strcmp(key, "") != 0)
6047 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
6050 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6051 void *buf, size_t buflen, int type)
6053 if (strcmp(key, "") != 0)
6056 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
6059 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6060 size_t list_len, const char *name,
6061 size_t name_len, int type)
6063 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
6065 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
6068 if (list && len <= list_len)
6069 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
6073 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6074 static inline int nfs4_server_supports_labels(struct nfs_server *server)
6076 return server->caps & NFS_CAP_SECURITY_LABEL;
6079 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6080 const void *buf, size_t buflen,
6081 int flags, int type)
6083 if (security_ismaclabel(key))
6084 return nfs4_set_security_label(dentry, buf, buflen);
6089 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6090 void *buf, size_t buflen, int type)
6092 if (security_ismaclabel(key))
6093 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
6097 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6098 size_t list_len, const char *name,
6099 size_t name_len, int type)
6103 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
6104 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
6105 if (list && len <= list_len)
6106 security_inode_listsecurity(dentry->d_inode, list, len);
6111 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6112 .prefix = XATTR_SECURITY_PREFIX,
6113 .list = nfs4_xattr_list_nfs4_label,
6114 .get = nfs4_xattr_get_nfs4_label,
6115 .set = nfs4_xattr_set_nfs4_label,
6121 * nfs_fhget will use either the mounted_on_fileid or the fileid
6123 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6125 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6126 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6127 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6128 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6131 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6132 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6133 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6137 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6138 const struct qstr *name,
6139 struct nfs4_fs_locations *fs_locations,
6142 struct nfs_server *server = NFS_SERVER(dir);
6144 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6146 struct nfs4_fs_locations_arg args = {
6147 .dir_fh = NFS_FH(dir),
6152 struct nfs4_fs_locations_res res = {
6153 .fs_locations = fs_locations,
6155 struct rpc_message msg = {
6156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6162 dprintk("%s: start\n", __func__);
6164 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6165 * is not supported */
6166 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6167 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6169 bitmask[0] |= FATTR4_WORD0_FILEID;
6171 nfs_fattr_init(&fs_locations->fattr);
6172 fs_locations->server = server;
6173 fs_locations->nlocations = 0;
6174 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6175 dprintk("%s: returned status = %d\n", __func__, status);
6179 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6180 const struct qstr *name,
6181 struct nfs4_fs_locations *fs_locations,
6184 struct nfs4_exception exception = { };
6187 err = _nfs4_proc_fs_locations(client, dir, name,
6188 fs_locations, page);
6189 trace_nfs4_get_fs_locations(dir, name, err);
6190 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6192 } while (exception.retry);
6197 * This operation also signals the server that this client is
6198 * performing migration recovery. The server can stop returning
6199 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6200 * appended to this compound to identify the client ID which is
6201 * performing recovery.
6203 static int _nfs40_proc_get_locations(struct inode *inode,
6204 struct nfs4_fs_locations *locations,
6205 struct page *page, struct rpc_cred *cred)
6207 struct nfs_server *server = NFS_SERVER(inode);
6208 struct rpc_clnt *clnt = server->client;
6210 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6212 struct nfs4_fs_locations_arg args = {
6213 .clientid = server->nfs_client->cl_clientid,
6214 .fh = NFS_FH(inode),
6217 .migration = 1, /* skip LOOKUP */
6218 .renew = 1, /* append RENEW */
6220 struct nfs4_fs_locations_res res = {
6221 .fs_locations = locations,
6225 struct rpc_message msg = {
6226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6231 unsigned long now = jiffies;
6234 nfs_fattr_init(&locations->fattr);
6235 locations->server = server;
6236 locations->nlocations = 0;
6238 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6239 nfs4_set_sequence_privileged(&args.seq_args);
6240 status = nfs4_call_sync_sequence(clnt, server, &msg,
6241 &args.seq_args, &res.seq_res);
6245 renew_lease(server, now);
6249 #ifdef CONFIG_NFS_V4_1
6252 * This operation also signals the server that this client is
6253 * performing migration recovery. The server can stop asserting
6254 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6255 * performing this operation is identified in the SEQUENCE
6256 * operation in this compound.
6258 * When the client supports GETATTR(fs_locations_info), it can
6259 * be plumbed in here.
6261 static int _nfs41_proc_get_locations(struct inode *inode,
6262 struct nfs4_fs_locations *locations,
6263 struct page *page, struct rpc_cred *cred)
6265 struct nfs_server *server = NFS_SERVER(inode);
6266 struct rpc_clnt *clnt = server->client;
6268 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6270 struct nfs4_fs_locations_arg args = {
6271 .fh = NFS_FH(inode),
6274 .migration = 1, /* skip LOOKUP */
6276 struct nfs4_fs_locations_res res = {
6277 .fs_locations = locations,
6280 struct rpc_message msg = {
6281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6288 nfs_fattr_init(&locations->fattr);
6289 locations->server = server;
6290 locations->nlocations = 0;
6292 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6293 nfs4_set_sequence_privileged(&args.seq_args);
6294 status = nfs4_call_sync_sequence(clnt, server, &msg,
6295 &args.seq_args, &res.seq_res);
6296 if (status == NFS4_OK &&
6297 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6298 status = -NFS4ERR_LEASE_MOVED;
6302 #endif /* CONFIG_NFS_V4_1 */
6305 * nfs4_proc_get_locations - discover locations for a migrated FSID
6306 * @inode: inode on FSID that is migrating
6307 * @locations: result of query
6309 * @cred: credential to use for this operation
6311 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6312 * operation failed, or a negative errno if a local error occurred.
6314 * On success, "locations" is filled in, but if the server has
6315 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6318 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6319 * from this client that require migration recovery.
6321 int nfs4_proc_get_locations(struct inode *inode,
6322 struct nfs4_fs_locations *locations,
6323 struct page *page, struct rpc_cred *cred)
6325 struct nfs_server *server = NFS_SERVER(inode);
6326 struct nfs_client *clp = server->nfs_client;
6327 const struct nfs4_mig_recovery_ops *ops =
6328 clp->cl_mvops->mig_recovery_ops;
6329 struct nfs4_exception exception = { };
6332 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6333 (unsigned long long)server->fsid.major,
6334 (unsigned long long)server->fsid.minor,
6336 nfs_display_fhandle(NFS_FH(inode), __func__);
6339 status = ops->get_locations(inode, locations, page, cred);
6340 if (status != -NFS4ERR_DELAY)
6342 nfs4_handle_exception(server, status, &exception);
6343 } while (exception.retry);
6348 * This operation also signals the server that this client is
6349 * performing "lease moved" recovery. The server can stop
6350 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6351 * is appended to this compound to identify the client ID which is
6352 * performing recovery.
6354 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6356 struct nfs_server *server = NFS_SERVER(inode);
6357 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6358 struct rpc_clnt *clnt = server->client;
6359 struct nfs4_fsid_present_arg args = {
6360 .fh = NFS_FH(inode),
6361 .clientid = clp->cl_clientid,
6362 .renew = 1, /* append RENEW */
6364 struct nfs4_fsid_present_res res = {
6367 struct rpc_message msg = {
6368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6373 unsigned long now = jiffies;
6376 res.fh = nfs_alloc_fhandle();
6380 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6381 nfs4_set_sequence_privileged(&args.seq_args);
6382 status = nfs4_call_sync_sequence(clnt, server, &msg,
6383 &args.seq_args, &res.seq_res);
6384 nfs_free_fhandle(res.fh);
6388 do_renew_lease(clp, now);
6392 #ifdef CONFIG_NFS_V4_1
6395 * This operation also signals the server that this client is
6396 * performing "lease moved" recovery. The server can stop asserting
6397 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6398 * this operation is identified in the SEQUENCE operation in this
6401 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6403 struct nfs_server *server = NFS_SERVER(inode);
6404 struct rpc_clnt *clnt = server->client;
6405 struct nfs4_fsid_present_arg args = {
6406 .fh = NFS_FH(inode),
6408 struct nfs4_fsid_present_res res = {
6410 struct rpc_message msg = {
6411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6418 res.fh = nfs_alloc_fhandle();
6422 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6423 nfs4_set_sequence_privileged(&args.seq_args);
6424 status = nfs4_call_sync_sequence(clnt, server, &msg,
6425 &args.seq_args, &res.seq_res);
6426 nfs_free_fhandle(res.fh);
6427 if (status == NFS4_OK &&
6428 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6429 status = -NFS4ERR_LEASE_MOVED;
6433 #endif /* CONFIG_NFS_V4_1 */
6436 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6437 * @inode: inode on FSID to check
6438 * @cred: credential to use for this operation
6440 * Server indicates whether the FSID is present, moved, or not
6441 * recognized. This operation is necessary to clear a LEASE_MOVED
6442 * condition for this client ID.
6444 * Returns NFS4_OK if the FSID is present on this server,
6445 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6446 * NFS4ERR code if some error occurred on the server, or a
6447 * negative errno if a local failure occurred.
6449 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6451 struct nfs_server *server = NFS_SERVER(inode);
6452 struct nfs_client *clp = server->nfs_client;
6453 const struct nfs4_mig_recovery_ops *ops =
6454 clp->cl_mvops->mig_recovery_ops;
6455 struct nfs4_exception exception = { };
6458 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6459 (unsigned long long)server->fsid.major,
6460 (unsigned long long)server->fsid.minor,
6462 nfs_display_fhandle(NFS_FH(inode), __func__);
6465 status = ops->fsid_present(inode, cred);
6466 if (status != -NFS4ERR_DELAY)
6468 nfs4_handle_exception(server, status, &exception);
6469 } while (exception.retry);
6474 * If 'use_integrity' is true and the state managment nfs_client
6475 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6476 * and the machine credential as per RFC3530bis and RFC5661 Security
6477 * Considerations sections. Otherwise, just use the user cred with the
6478 * filesystem's rpc_client.
6480 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6483 struct nfs4_secinfo_arg args = {
6484 .dir_fh = NFS_FH(dir),
6487 struct nfs4_secinfo_res res = {
6490 struct rpc_message msg = {
6491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6495 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6496 struct rpc_cred *cred = NULL;
6498 if (use_integrity) {
6499 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6500 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6501 msg.rpc_cred = cred;
6504 dprintk("NFS call secinfo %s\n", name->name);
6506 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6507 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6509 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6511 dprintk("NFS reply secinfo: %d\n", status);
6519 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6520 struct nfs4_secinfo_flavors *flavors)
6522 struct nfs4_exception exception = { };
6525 err = -NFS4ERR_WRONGSEC;
6527 /* try to use integrity protection with machine cred */
6528 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6529 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6532 * if unable to use integrity protection, or SECINFO with
6533 * integrity protection returns NFS4ERR_WRONGSEC (which is
6534 * disallowed by spec, but exists in deployed servers) use
6535 * the current filesystem's rpc_client and the user cred.
6537 if (err == -NFS4ERR_WRONGSEC)
6538 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6540 trace_nfs4_secinfo(dir, name, err);
6541 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6543 } while (exception.retry);
6547 #ifdef CONFIG_NFS_V4_1
6549 * Check the exchange flags returned by the server for invalid flags, having
6550 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6553 static int nfs4_check_cl_exchange_flags(u32 flags)
6555 if (flags & ~EXCHGID4_FLAG_MASK_R)
6557 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6558 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6560 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6564 return -NFS4ERR_INVAL;
6568 nfs41_same_server_scope(struct nfs41_server_scope *a,
6569 struct nfs41_server_scope *b)
6571 if (a->server_scope_sz == b->server_scope_sz &&
6572 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6579 * nfs4_proc_bind_conn_to_session()
6581 * The 4.1 client currently uses the same TCP connection for the
6582 * fore and backchannel.
6584 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6587 struct nfs41_bind_conn_to_session_res res;
6588 struct rpc_message msg = {
6590 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6596 dprintk("--> %s\n", __func__);
6598 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6599 if (unlikely(res.session == NULL)) {
6604 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6605 trace_nfs4_bind_conn_to_session(clp, status);
6607 if (memcmp(res.session->sess_id.data,
6608 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6609 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6613 if (res.dir != NFS4_CDFS4_BOTH) {
6614 dprintk("NFS: %s: Unexpected direction from server\n",
6619 if (res.use_conn_in_rdma_mode) {
6620 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6629 dprintk("<-- %s status= %d\n", __func__, status);
6634 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6635 * and operations we'd like to see to enable certain features in the allow map
6637 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6638 .how = SP4_MACH_CRED,
6639 .enforce.u.words = {
6640 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6641 1 << (OP_EXCHANGE_ID - 32) |
6642 1 << (OP_CREATE_SESSION - 32) |
6643 1 << (OP_DESTROY_SESSION - 32) |
6644 1 << (OP_DESTROY_CLIENTID - 32)
6647 [0] = 1 << (OP_CLOSE) |
6650 [1] = 1 << (OP_SECINFO - 32) |
6651 1 << (OP_SECINFO_NO_NAME - 32) |
6652 1 << (OP_TEST_STATEID - 32) |
6653 1 << (OP_FREE_STATEID - 32) |
6654 1 << (OP_WRITE - 32)
6659 * Select the state protection mode for client `clp' given the server results
6660 * from exchange_id in `sp'.
6662 * Returns 0 on success, negative errno otherwise.
6664 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6665 struct nfs41_state_protection *sp)
6667 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6668 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6669 1 << (OP_EXCHANGE_ID - 32) |
6670 1 << (OP_CREATE_SESSION - 32) |
6671 1 << (OP_DESTROY_SESSION - 32) |
6672 1 << (OP_DESTROY_CLIENTID - 32)
6676 if (sp->how == SP4_MACH_CRED) {
6677 /* Print state protect result */
6678 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6679 for (i = 0; i <= LAST_NFS4_OP; i++) {
6680 if (test_bit(i, sp->enforce.u.longs))
6681 dfprintk(MOUNT, " enforce op %d\n", i);
6682 if (test_bit(i, sp->allow.u.longs))
6683 dfprintk(MOUNT, " allow op %d\n", i);
6686 /* make sure nothing is on enforce list that isn't supported */
6687 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6688 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6689 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6695 * Minimal mode - state operations are allowed to use machine
6696 * credential. Note this already happens by default, so the
6697 * client doesn't have to do anything more than the negotiation.
6699 * NOTE: we don't care if EXCHANGE_ID is in the list -
6700 * we're already using the machine cred for exchange_id
6701 * and will never use a different cred.
6703 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6704 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6705 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6706 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6707 dfprintk(MOUNT, "sp4_mach_cred:\n");
6708 dfprintk(MOUNT, " minimal mode enabled\n");
6709 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6711 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6715 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6716 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6717 dfprintk(MOUNT, " cleanup mode enabled\n");
6718 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6721 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6722 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6723 dfprintk(MOUNT, " secinfo mode enabled\n");
6724 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6727 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6728 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6729 dfprintk(MOUNT, " stateid mode enabled\n");
6730 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6733 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6734 dfprintk(MOUNT, " write mode enabled\n");
6735 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6738 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6739 dfprintk(MOUNT, " commit mode enabled\n");
6740 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6748 * _nfs4_proc_exchange_id()
6750 * Wrapper for EXCHANGE_ID operation.
6752 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6755 nfs4_verifier verifier;
6756 struct nfs41_exchange_id_args args = {
6757 .verifier = &verifier,
6759 #ifdef CONFIG_NFS_V4_1_MIGRATION
6760 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6761 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6762 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6764 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6765 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6768 struct nfs41_exchange_id_res res = {
6772 struct rpc_message msg = {
6773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6779 nfs4_init_boot_verifier(clp, &verifier);
6780 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6782 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6783 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6784 args.id_len, args.id);
6786 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6788 if (unlikely(res.server_owner == NULL)) {
6793 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6795 if (unlikely(res.server_scope == NULL)) {
6797 goto out_server_owner;
6800 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6801 if (unlikely(res.impl_id == NULL)) {
6803 goto out_server_scope;
6808 args.state_protect.how = SP4_NONE;
6812 args.state_protect = nfs4_sp4_mach_cred_request;
6819 goto out_server_scope;
6822 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6823 trace_nfs4_exchange_id(clp, status);
6825 status = nfs4_check_cl_exchange_flags(res.flags);
6828 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6831 clp->cl_clientid = res.clientid;
6832 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6833 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6834 clp->cl_seqid = res.seqid;
6836 kfree(clp->cl_serverowner);
6837 clp->cl_serverowner = res.server_owner;
6838 res.server_owner = NULL;
6840 /* use the most recent implementation id */
6841 kfree(clp->cl_implid);
6842 clp->cl_implid = res.impl_id;
6844 if (clp->cl_serverscope != NULL &&
6845 !nfs41_same_server_scope(clp->cl_serverscope,
6846 res.server_scope)) {
6847 dprintk("%s: server_scope mismatch detected\n",
6849 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6850 kfree(clp->cl_serverscope);
6851 clp->cl_serverscope = NULL;
6854 if (clp->cl_serverscope == NULL) {
6855 clp->cl_serverscope = res.server_scope;
6862 kfree(res.server_owner);
6864 kfree(res.server_scope);
6866 if (clp->cl_implid != NULL)
6867 dprintk("NFS reply exchange_id: Server Implementation ID: "
6868 "domain: %s, name: %s, date: %llu,%u\n",
6869 clp->cl_implid->domain, clp->cl_implid->name,
6870 clp->cl_implid->date.seconds,
6871 clp->cl_implid->date.nseconds);
6872 dprintk("NFS reply exchange_id: %d\n", status);
6877 * nfs4_proc_exchange_id()
6879 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6881 * Since the clientid has expired, all compounds using sessions
6882 * associated with the stale clientid will be returning
6883 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6884 * be in some phase of session reset.
6886 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6888 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6890 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6893 /* try SP4_MACH_CRED if krb5i/p */
6894 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6895 authflavor == RPC_AUTH_GSS_KRB5P) {
6896 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6902 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6905 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6906 struct rpc_cred *cred)
6908 struct rpc_message msg = {
6909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6915 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6916 trace_nfs4_destroy_clientid(clp, status);
6918 dprintk("NFS: Got error %d from the server %s on "
6919 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6923 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6924 struct rpc_cred *cred)
6929 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6930 ret = _nfs4_proc_destroy_clientid(clp, cred);
6932 case -NFS4ERR_DELAY:
6933 case -NFS4ERR_CLIENTID_BUSY:
6943 int nfs4_destroy_clientid(struct nfs_client *clp)
6945 struct rpc_cred *cred;
6948 if (clp->cl_mvops->minor_version < 1)
6950 if (clp->cl_exchange_flags == 0)
6952 if (clp->cl_preserve_clid)
6954 cred = nfs4_get_clid_cred(clp);
6955 ret = nfs4_proc_destroy_clientid(clp, cred);
6960 case -NFS4ERR_STALE_CLIENTID:
6961 clp->cl_exchange_flags = 0;
6967 struct nfs4_get_lease_time_data {
6968 struct nfs4_get_lease_time_args *args;
6969 struct nfs4_get_lease_time_res *res;
6970 struct nfs_client *clp;
6973 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6976 struct nfs4_get_lease_time_data *data =
6977 (struct nfs4_get_lease_time_data *)calldata;
6979 dprintk("--> %s\n", __func__);
6980 /* just setup sequence, do not trigger session recovery
6981 since we're invoked within one */
6982 nfs41_setup_sequence(data->clp->cl_session,
6983 &data->args->la_seq_args,
6984 &data->res->lr_seq_res,
6986 dprintk("<-- %s\n", __func__);
6990 * Called from nfs4_state_manager thread for session setup, so don't recover
6991 * from sequence operation or clientid errors.
6993 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6995 struct nfs4_get_lease_time_data *data =
6996 (struct nfs4_get_lease_time_data *)calldata;
6998 dprintk("--> %s\n", __func__);
6999 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7001 switch (task->tk_status) {
7002 case -NFS4ERR_DELAY:
7003 case -NFS4ERR_GRACE:
7004 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7005 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7006 task->tk_status = 0;
7008 case -NFS4ERR_RETRY_UNCACHED_REP:
7009 rpc_restart_call_prepare(task);
7012 dprintk("<-- %s\n", __func__);
7015 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7016 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7017 .rpc_call_done = nfs4_get_lease_time_done,
7020 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7022 struct rpc_task *task;
7023 struct nfs4_get_lease_time_args args;
7024 struct nfs4_get_lease_time_res res = {
7025 .lr_fsinfo = fsinfo,
7027 struct nfs4_get_lease_time_data data = {
7032 struct rpc_message msg = {
7033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7037 struct rpc_task_setup task_setup = {
7038 .rpc_client = clp->cl_rpcclient,
7039 .rpc_message = &msg,
7040 .callback_ops = &nfs4_get_lease_time_ops,
7041 .callback_data = &data,
7042 .flags = RPC_TASK_TIMEOUT,
7046 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7047 nfs4_set_sequence_privileged(&args.la_seq_args);
7048 dprintk("--> %s\n", __func__);
7049 task = rpc_run_task(&task_setup);
7052 status = PTR_ERR(task);
7054 status = task->tk_status;
7057 dprintk("<-- %s return %d\n", __func__, status);
7063 * Initialize the values to be used by the client in CREATE_SESSION
7064 * If nfs4_init_session set the fore channel request and response sizes,
7067 * Set the back channel max_resp_sz_cached to zero to force the client to
7068 * always set csa_cachethis to FALSE because the current implementation
7069 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7071 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7073 unsigned int max_rqst_sz, max_resp_sz;
7075 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7076 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7078 /* Fore channel attributes */
7079 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7080 args->fc_attrs.max_resp_sz = max_resp_sz;
7081 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7082 args->fc_attrs.max_reqs = max_session_slots;
7084 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7085 "max_ops=%u max_reqs=%u\n",
7087 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7088 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7090 /* Back channel attributes */
7091 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7092 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7093 args->bc_attrs.max_resp_sz_cached = 0;
7094 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7095 args->bc_attrs.max_reqs = 1;
7097 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7098 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7100 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7101 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7102 args->bc_attrs.max_reqs);
7105 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7107 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7108 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
7110 if (rcvd->max_resp_sz > sent->max_resp_sz)
7113 * Our requested max_ops is the minimum we need; we're not
7114 * prepared to break up compounds into smaller pieces than that.
7115 * So, no point even trying to continue if the server won't
7118 if (rcvd->max_ops < sent->max_ops)
7120 if (rcvd->max_reqs == 0)
7122 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7123 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7127 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7129 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7130 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
7132 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7134 if (rcvd->max_resp_sz < sent->max_resp_sz)
7136 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7138 /* These would render the backchannel useless: */
7139 if (rcvd->max_ops != sent->max_ops)
7141 if (rcvd->max_reqs != sent->max_reqs)
7146 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7147 struct nfs4_session *session)
7151 ret = nfs4_verify_fore_channel_attrs(args, session);
7154 return nfs4_verify_back_channel_attrs(args, session);
7157 static int _nfs4_proc_create_session(struct nfs_client *clp,
7158 struct rpc_cred *cred)
7160 struct nfs4_session *session = clp->cl_session;
7161 struct nfs41_create_session_args args = {
7163 .cb_program = NFS4_CALLBACK,
7165 struct nfs41_create_session_res res = {
7168 struct rpc_message msg = {
7169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7176 nfs4_init_channel_attrs(&args);
7177 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7179 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7180 trace_nfs4_create_session(clp, status);
7183 /* Verify the session's negotiated channel_attrs values */
7184 status = nfs4_verify_channel_attrs(&args, session);
7185 /* Increment the clientid slot sequence id */
7193 * Issues a CREATE_SESSION operation to the server.
7194 * It is the responsibility of the caller to verify the session is
7195 * expired before calling this routine.
7197 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7201 struct nfs4_session *session = clp->cl_session;
7203 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7205 status = _nfs4_proc_create_session(clp, cred);
7209 /* Init or reset the session slot tables */
7210 status = nfs4_setup_session_slot_tables(session);
7211 dprintk("slot table setup returned %d\n", status);
7215 ptr = (unsigned *)&session->sess_id.data[0];
7216 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7217 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7219 dprintk("<-- %s\n", __func__);
7224 * Issue the over-the-wire RPC DESTROY_SESSION.
7225 * The caller must serialize access to this routine.
7227 int nfs4_proc_destroy_session(struct nfs4_session *session,
7228 struct rpc_cred *cred)
7230 struct rpc_message msg = {
7231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7232 .rpc_argp = session,
7237 dprintk("--> nfs4_proc_destroy_session\n");
7239 /* session is still being setup */
7240 if (session->clp->cl_cons_state != NFS_CS_READY)
7243 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7244 trace_nfs4_destroy_session(session->clp, status);
7247 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7248 "Session has been destroyed regardless...\n", status);
7250 dprintk("<-- nfs4_proc_destroy_session\n");
7255 * Renew the cl_session lease.
7257 struct nfs4_sequence_data {
7258 struct nfs_client *clp;
7259 struct nfs4_sequence_args args;
7260 struct nfs4_sequence_res res;
7263 static void nfs41_sequence_release(void *data)
7265 struct nfs4_sequence_data *calldata = data;
7266 struct nfs_client *clp = calldata->clp;
7268 if (atomic_read(&clp->cl_count) > 1)
7269 nfs4_schedule_state_renewal(clp);
7270 nfs_put_client(clp);
7274 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7276 switch(task->tk_status) {
7277 case -NFS4ERR_DELAY:
7278 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7281 nfs4_schedule_lease_recovery(clp);
7286 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7288 struct nfs4_sequence_data *calldata = data;
7289 struct nfs_client *clp = calldata->clp;
7291 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7294 trace_nfs4_sequence(clp, task->tk_status);
7295 if (task->tk_status < 0) {
7296 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7297 if (atomic_read(&clp->cl_count) == 1)
7300 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7301 rpc_restart_call_prepare(task);
7305 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7307 dprintk("<-- %s\n", __func__);
7310 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7312 struct nfs4_sequence_data *calldata = data;
7313 struct nfs_client *clp = calldata->clp;
7314 struct nfs4_sequence_args *args;
7315 struct nfs4_sequence_res *res;
7317 args = task->tk_msg.rpc_argp;
7318 res = task->tk_msg.rpc_resp;
7320 nfs41_setup_sequence(clp->cl_session, args, res, task);
7323 static const struct rpc_call_ops nfs41_sequence_ops = {
7324 .rpc_call_done = nfs41_sequence_call_done,
7325 .rpc_call_prepare = nfs41_sequence_prepare,
7326 .rpc_release = nfs41_sequence_release,
7329 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7330 struct rpc_cred *cred,
7333 struct nfs4_sequence_data *calldata;
7334 struct rpc_message msg = {
7335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7338 struct rpc_task_setup task_setup_data = {
7339 .rpc_client = clp->cl_rpcclient,
7340 .rpc_message = &msg,
7341 .callback_ops = &nfs41_sequence_ops,
7342 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7345 if (!atomic_inc_not_zero(&clp->cl_count))
7346 return ERR_PTR(-EIO);
7347 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7348 if (calldata == NULL) {
7349 nfs_put_client(clp);
7350 return ERR_PTR(-ENOMEM);
7352 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7354 nfs4_set_sequence_privileged(&calldata->args);
7355 msg.rpc_argp = &calldata->args;
7356 msg.rpc_resp = &calldata->res;
7357 calldata->clp = clp;
7358 task_setup_data.callback_data = calldata;
7360 return rpc_run_task(&task_setup_data);
7363 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7365 struct rpc_task *task;
7368 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7370 task = _nfs41_proc_sequence(clp, cred, false);
7372 ret = PTR_ERR(task);
7374 rpc_put_task_async(task);
7375 dprintk("<-- %s status=%d\n", __func__, ret);
7379 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7381 struct rpc_task *task;
7384 task = _nfs41_proc_sequence(clp, cred, true);
7386 ret = PTR_ERR(task);
7389 ret = rpc_wait_for_completion_task(task);
7391 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7393 if (task->tk_status == 0)
7394 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7395 ret = task->tk_status;
7399 dprintk("<-- %s status=%d\n", __func__, ret);
7403 struct nfs4_reclaim_complete_data {
7404 struct nfs_client *clp;
7405 struct nfs41_reclaim_complete_args arg;
7406 struct nfs41_reclaim_complete_res res;
7409 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7411 struct nfs4_reclaim_complete_data *calldata = data;
7413 nfs41_setup_sequence(calldata->clp->cl_session,
7414 &calldata->arg.seq_args,
7415 &calldata->res.seq_res,
7419 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7421 switch(task->tk_status) {
7423 case -NFS4ERR_COMPLETE_ALREADY:
7424 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7426 case -NFS4ERR_DELAY:
7427 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7429 case -NFS4ERR_RETRY_UNCACHED_REP:
7432 nfs4_schedule_lease_recovery(clp);
7437 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7439 struct nfs4_reclaim_complete_data *calldata = data;
7440 struct nfs_client *clp = calldata->clp;
7441 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7443 dprintk("--> %s\n", __func__);
7444 if (!nfs41_sequence_done(task, res))
7447 trace_nfs4_reclaim_complete(clp, task->tk_status);
7448 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7449 rpc_restart_call_prepare(task);
7452 dprintk("<-- %s\n", __func__);
7455 static void nfs4_free_reclaim_complete_data(void *data)
7457 struct nfs4_reclaim_complete_data *calldata = data;
7462 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7463 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7464 .rpc_call_done = nfs4_reclaim_complete_done,
7465 .rpc_release = nfs4_free_reclaim_complete_data,
7469 * Issue a global reclaim complete.
7471 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7472 struct rpc_cred *cred)
7474 struct nfs4_reclaim_complete_data *calldata;
7475 struct rpc_task *task;
7476 struct rpc_message msg = {
7477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7480 struct rpc_task_setup task_setup_data = {
7481 .rpc_client = clp->cl_rpcclient,
7482 .rpc_message = &msg,
7483 .callback_ops = &nfs4_reclaim_complete_call_ops,
7484 .flags = RPC_TASK_ASYNC,
7486 int status = -ENOMEM;
7488 dprintk("--> %s\n", __func__);
7489 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7490 if (calldata == NULL)
7492 calldata->clp = clp;
7493 calldata->arg.one_fs = 0;
7495 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7496 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7497 msg.rpc_argp = &calldata->arg;
7498 msg.rpc_resp = &calldata->res;
7499 task_setup_data.callback_data = calldata;
7500 task = rpc_run_task(&task_setup_data);
7502 status = PTR_ERR(task);
7505 status = nfs4_wait_for_completion_rpc_task(task);
7507 status = task->tk_status;
7511 dprintk("<-- %s status=%d\n", __func__, status);
7516 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7518 struct nfs4_layoutget *lgp = calldata;
7519 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7520 struct nfs4_session *session = nfs4_get_session(server);
7522 dprintk("--> %s\n", __func__);
7523 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7524 * right now covering the LAYOUTGET we are about to send.
7525 * However, that is not so catastrophic, and there seems
7526 * to be no way to prevent it completely.
7528 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7529 &lgp->res.seq_res, task))
7531 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7532 NFS_I(lgp->args.inode)->layout,
7533 lgp->args.ctx->state)) {
7534 rpc_exit(task, NFS4_OK);
7538 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7540 struct nfs4_layoutget *lgp = calldata;
7541 struct inode *inode = lgp->args.inode;
7542 struct nfs_server *server = NFS_SERVER(inode);
7543 struct pnfs_layout_hdr *lo;
7544 struct nfs4_state *state = NULL;
7545 unsigned long timeo, now, giveup;
7547 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7549 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7552 switch (task->tk_status) {
7556 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7557 * (or clients) writing to the same RAID stripe
7559 case -NFS4ERR_LAYOUTTRYLATER:
7561 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7562 * existing layout before getting a new one).
7564 case -NFS4ERR_RECALLCONFLICT:
7565 timeo = rpc_get_timeout(task->tk_client);
7566 giveup = lgp->args.timestamp + timeo;
7568 if (time_after(giveup, now)) {
7569 unsigned long delay;
7572 * - Not less then NFS4_POLL_RETRY_MIN.
7573 * - One last time a jiffie before we give up
7574 * - exponential backoff (time_now minus start_attempt)
7576 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7577 min((giveup - now - 1),
7578 now - lgp->args.timestamp));
7580 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7582 rpc_delay(task, delay);
7583 task->tk_status = 0;
7584 rpc_restart_call_prepare(task);
7585 goto out; /* Do not call nfs4_async_handle_error() */
7588 case -NFS4ERR_EXPIRED:
7589 case -NFS4ERR_BAD_STATEID:
7590 spin_lock(&inode->i_lock);
7591 lo = NFS_I(inode)->layout;
7592 if (!lo || list_empty(&lo->plh_segs)) {
7593 spin_unlock(&inode->i_lock);
7594 /* If the open stateid was bad, then recover it. */
7595 state = lgp->args.ctx->state;
7600 * Mark the bad layout state as invalid, then retry
7601 * with the current stateid.
7603 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7604 spin_unlock(&inode->i_lock);
7605 pnfs_free_lseg_list(&head);
7607 task->tk_status = 0;
7608 rpc_restart_call_prepare(task);
7611 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
7612 rpc_restart_call_prepare(task);
7614 dprintk("<-- %s\n", __func__);
7617 static size_t max_response_pages(struct nfs_server *server)
7619 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7620 return nfs_page_array_len(0, max_resp_sz);
7623 static void nfs4_free_pages(struct page **pages, size_t size)
7630 for (i = 0; i < size; i++) {
7633 __free_page(pages[i]);
7638 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7640 struct page **pages;
7643 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7645 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7649 for (i = 0; i < size; i++) {
7650 pages[i] = alloc_page(gfp_flags);
7652 dprintk("%s: failed to allocate page\n", __func__);
7653 nfs4_free_pages(pages, size);
7661 static void nfs4_layoutget_release(void *calldata)
7663 struct nfs4_layoutget *lgp = calldata;
7664 struct inode *inode = lgp->args.inode;
7665 struct nfs_server *server = NFS_SERVER(inode);
7666 size_t max_pages = max_response_pages(server);
7668 dprintk("--> %s\n", __func__);
7669 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7670 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7671 put_nfs_open_context(lgp->args.ctx);
7673 dprintk("<-- %s\n", __func__);
7676 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7677 .rpc_call_prepare = nfs4_layoutget_prepare,
7678 .rpc_call_done = nfs4_layoutget_done,
7679 .rpc_release = nfs4_layoutget_release,
7682 struct pnfs_layout_segment *
7683 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7685 struct inode *inode = lgp->args.inode;
7686 struct nfs_server *server = NFS_SERVER(inode);
7687 size_t max_pages = max_response_pages(server);
7688 struct rpc_task *task;
7689 struct rpc_message msg = {
7690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7691 .rpc_argp = &lgp->args,
7692 .rpc_resp = &lgp->res,
7693 .rpc_cred = lgp->cred,
7695 struct rpc_task_setup task_setup_data = {
7696 .rpc_client = server->client,
7697 .rpc_message = &msg,
7698 .callback_ops = &nfs4_layoutget_call_ops,
7699 .callback_data = lgp,
7700 .flags = RPC_TASK_ASYNC,
7702 struct pnfs_layout_segment *lseg = NULL;
7705 dprintk("--> %s\n", __func__);
7707 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7708 if (!lgp->args.layout.pages) {
7709 nfs4_layoutget_release(lgp);
7710 return ERR_PTR(-ENOMEM);
7712 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7713 lgp->args.timestamp = jiffies;
7715 lgp->res.layoutp = &lgp->args.layout;
7716 lgp->res.seq_res.sr_slot = NULL;
7717 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7719 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7720 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7722 task = rpc_run_task(&task_setup_data);
7724 return ERR_CAST(task);
7725 status = nfs4_wait_for_completion_rpc_task(task);
7727 status = task->tk_status;
7728 trace_nfs4_layoutget(lgp->args.ctx,
7732 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7733 if (status == 0 && lgp->res.layoutp->len)
7734 lseg = pnfs_layout_process(lgp);
7736 dprintk("<-- %s status=%d\n", __func__, status);
7738 return ERR_PTR(status);
7743 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7745 struct nfs4_layoutreturn *lrp = calldata;
7747 dprintk("--> %s\n", __func__);
7748 nfs41_setup_sequence(lrp->clp->cl_session,
7749 &lrp->args.seq_args,
7754 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7756 struct nfs4_layoutreturn *lrp = calldata;
7757 struct nfs_server *server;
7759 dprintk("--> %s\n", __func__);
7761 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7764 server = NFS_SERVER(lrp->args.inode);
7765 switch (task->tk_status) {
7767 task->tk_status = 0;
7770 case -NFS4ERR_DELAY:
7771 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
7773 rpc_restart_call_prepare(task);
7776 dprintk("<-- %s\n", __func__);
7779 static void nfs4_layoutreturn_release(void *calldata)
7781 struct nfs4_layoutreturn *lrp = calldata;
7782 struct pnfs_layout_hdr *lo = lrp->args.layout;
7784 dprintk("--> %s\n", __func__);
7785 spin_lock(&lo->plh_inode->i_lock);
7786 if (lrp->res.lrs_present)
7787 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7788 lo->plh_block_lgets--;
7789 spin_unlock(&lo->plh_inode->i_lock);
7790 pnfs_put_layout_hdr(lrp->args.layout);
7792 dprintk("<-- %s\n", __func__);
7795 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7796 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7797 .rpc_call_done = nfs4_layoutreturn_done,
7798 .rpc_release = nfs4_layoutreturn_release,
7801 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7803 struct rpc_task *task;
7804 struct rpc_message msg = {
7805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7806 .rpc_argp = &lrp->args,
7807 .rpc_resp = &lrp->res,
7808 .rpc_cred = lrp->cred,
7810 struct rpc_task_setup task_setup_data = {
7811 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7812 .rpc_message = &msg,
7813 .callback_ops = &nfs4_layoutreturn_call_ops,
7814 .callback_data = lrp,
7818 dprintk("--> %s\n", __func__);
7819 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7820 task = rpc_run_task(&task_setup_data);
7822 return PTR_ERR(task);
7823 status = task->tk_status;
7824 trace_nfs4_layoutreturn(lrp->args.inode, status);
7825 dprintk("<-- %s status=%d\n", __func__, status);
7831 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7832 struct pnfs_device *pdev,
7833 struct rpc_cred *cred)
7835 struct nfs4_getdeviceinfo_args args = {
7838 struct nfs4_getdeviceinfo_res res = {
7841 struct rpc_message msg = {
7842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7849 dprintk("--> %s\n", __func__);
7850 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7851 dprintk("<-- %s status=%d\n", __func__, status);
7856 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7857 struct pnfs_device *pdev,
7858 struct rpc_cred *cred)
7860 struct nfs4_exception exception = { };
7864 err = nfs4_handle_exception(server,
7865 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7867 } while (exception.retry);
7870 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7872 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7874 struct nfs4_layoutcommit_data *data = calldata;
7875 struct nfs_server *server = NFS_SERVER(data->args.inode);
7876 struct nfs4_session *session = nfs4_get_session(server);
7878 nfs41_setup_sequence(session,
7879 &data->args.seq_args,
7885 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7887 struct nfs4_layoutcommit_data *data = calldata;
7888 struct nfs_server *server = NFS_SERVER(data->args.inode);
7890 if (!nfs41_sequence_done(task, &data->res.seq_res))
7893 switch (task->tk_status) { /* Just ignore these failures */
7894 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7895 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7896 case -NFS4ERR_BADLAYOUT: /* no layout */
7897 case -NFS4ERR_GRACE: /* loca_recalim always false */
7898 task->tk_status = 0;
7902 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
7903 rpc_restart_call_prepare(task);
7909 static void nfs4_layoutcommit_release(void *calldata)
7911 struct nfs4_layoutcommit_data *data = calldata;
7913 pnfs_cleanup_layoutcommit(data);
7914 nfs_post_op_update_inode_force_wcc(data->args.inode,
7916 put_rpccred(data->cred);
7920 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7921 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7922 .rpc_call_done = nfs4_layoutcommit_done,
7923 .rpc_release = nfs4_layoutcommit_release,
7927 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7929 struct rpc_message msg = {
7930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7931 .rpc_argp = &data->args,
7932 .rpc_resp = &data->res,
7933 .rpc_cred = data->cred,
7935 struct rpc_task_setup task_setup_data = {
7936 .task = &data->task,
7937 .rpc_client = NFS_CLIENT(data->args.inode),
7938 .rpc_message = &msg,
7939 .callback_ops = &nfs4_layoutcommit_ops,
7940 .callback_data = data,
7941 .flags = RPC_TASK_ASYNC,
7943 struct rpc_task *task;
7946 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7947 "lbw: %llu inode %lu\n",
7948 data->task.tk_pid, sync,
7949 data->args.lastbytewritten,
7950 data->args.inode->i_ino);
7952 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7953 task = rpc_run_task(&task_setup_data);
7955 return PTR_ERR(task);
7958 status = nfs4_wait_for_completion_rpc_task(task);
7961 status = task->tk_status;
7962 trace_nfs4_layoutcommit(data->args.inode, status);
7964 dprintk("%s: status %d\n", __func__, status);
7970 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7971 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7974 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7975 struct nfs_fsinfo *info,
7976 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7978 struct nfs41_secinfo_no_name_args args = {
7979 .style = SECINFO_STYLE_CURRENT_FH,
7981 struct nfs4_secinfo_res res = {
7984 struct rpc_message msg = {
7985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7989 struct rpc_clnt *clnt = server->client;
7990 struct rpc_cred *cred = NULL;
7993 if (use_integrity) {
7994 clnt = server->nfs_client->cl_rpcclient;
7995 cred = nfs4_get_clid_cred(server->nfs_client);
7996 msg.rpc_cred = cred;
7999 dprintk("--> %s\n", __func__);
8000 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8002 dprintk("<-- %s status=%d\n", __func__, status);
8011 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8012 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8014 struct nfs4_exception exception = { };
8017 /* first try using integrity protection */
8018 err = -NFS4ERR_WRONGSEC;
8020 /* try to use integrity protection with machine cred */
8021 if (_nfs4_is_integrity_protected(server->nfs_client))
8022 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8026 * if unable to use integrity protection, or SECINFO with
8027 * integrity protection returns NFS4ERR_WRONGSEC (which is
8028 * disallowed by spec, but exists in deployed servers) use
8029 * the current filesystem's rpc_client and the user cred.
8031 if (err == -NFS4ERR_WRONGSEC)
8032 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8037 case -NFS4ERR_WRONGSEC:
8041 err = nfs4_handle_exception(server, err, &exception);
8043 } while (exception.retry);
8049 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8050 struct nfs_fsinfo *info)
8054 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8055 struct nfs4_secinfo_flavors *flavors;
8056 struct nfs4_secinfo4 *secinfo;
8059 page = alloc_page(GFP_KERNEL);
8065 flavors = page_address(page);
8066 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8069 * Fall back on "guess and check" method if
8070 * the server doesn't support SECINFO_NO_NAME
8072 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8073 err = nfs4_find_root_sec(server, fhandle, info);
8079 for (i = 0; i < flavors->num_flavors; i++) {
8080 secinfo = &flavors->flavors[i];
8082 switch (secinfo->flavor) {
8086 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8087 &secinfo->flavor_info);
8090 flavor = RPC_AUTH_MAXFLAVOR;
8094 if (!nfs_auth_info_match(&server->auth_info, flavor))
8095 flavor = RPC_AUTH_MAXFLAVOR;
8097 if (flavor != RPC_AUTH_MAXFLAVOR) {
8098 err = nfs4_lookup_root_sec(server, fhandle,
8105 if (flavor == RPC_AUTH_MAXFLAVOR)
8116 static int _nfs41_test_stateid(struct nfs_server *server,
8117 nfs4_stateid *stateid,
8118 struct rpc_cred *cred)
8121 struct nfs41_test_stateid_args args = {
8124 struct nfs41_test_stateid_res res;
8125 struct rpc_message msg = {
8126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8131 struct rpc_clnt *rpc_client = server->client;
8133 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8136 dprintk("NFS call test_stateid %p\n", stateid);
8137 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8138 nfs4_set_sequence_privileged(&args.seq_args);
8139 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8140 &args.seq_args, &res.seq_res);
8141 if (status != NFS_OK) {
8142 dprintk("NFS reply test_stateid: failed, %d\n", status);
8145 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8150 * nfs41_test_stateid - perform a TEST_STATEID operation
8152 * @server: server / transport on which to perform the operation
8153 * @stateid: state ID to test
8156 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8157 * Otherwise a negative NFS4ERR value is returned if the operation
8158 * failed or the state ID is not currently valid.
8160 static int nfs41_test_stateid(struct nfs_server *server,
8161 nfs4_stateid *stateid,
8162 struct rpc_cred *cred)
8164 struct nfs4_exception exception = { };
8167 err = _nfs41_test_stateid(server, stateid, cred);
8168 if (err != -NFS4ERR_DELAY)
8170 nfs4_handle_exception(server, err, &exception);
8171 } while (exception.retry);
8175 struct nfs_free_stateid_data {
8176 struct nfs_server *server;
8177 struct nfs41_free_stateid_args args;
8178 struct nfs41_free_stateid_res res;
8181 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8183 struct nfs_free_stateid_data *data = calldata;
8184 nfs41_setup_sequence(nfs4_get_session(data->server),
8185 &data->args.seq_args,
8190 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8192 struct nfs_free_stateid_data *data = calldata;
8194 nfs41_sequence_done(task, &data->res.seq_res);
8196 switch (task->tk_status) {
8197 case -NFS4ERR_DELAY:
8198 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
8199 rpc_restart_call_prepare(task);
8203 static void nfs41_free_stateid_release(void *calldata)
8208 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8209 .rpc_call_prepare = nfs41_free_stateid_prepare,
8210 .rpc_call_done = nfs41_free_stateid_done,
8211 .rpc_release = nfs41_free_stateid_release,
8214 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8215 nfs4_stateid *stateid,
8216 struct rpc_cred *cred,
8219 struct rpc_message msg = {
8220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8223 struct rpc_task_setup task_setup = {
8224 .rpc_client = server->client,
8225 .rpc_message = &msg,
8226 .callback_ops = &nfs41_free_stateid_ops,
8227 .flags = RPC_TASK_ASYNC,
8229 struct nfs_free_stateid_data *data;
8231 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8232 &task_setup.rpc_client, &msg);
8234 dprintk("NFS call free_stateid %p\n", stateid);
8235 data = kmalloc(sizeof(*data), GFP_NOFS);
8237 return ERR_PTR(-ENOMEM);
8238 data->server = server;
8239 nfs4_stateid_copy(&data->args.stateid, stateid);
8241 task_setup.callback_data = data;
8243 msg.rpc_argp = &data->args;
8244 msg.rpc_resp = &data->res;
8245 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8247 nfs4_set_sequence_privileged(&data->args.seq_args);
8249 return rpc_run_task(&task_setup);
8253 * nfs41_free_stateid - perform a FREE_STATEID operation
8255 * @server: server / transport on which to perform the operation
8256 * @stateid: state ID to release
8259 * Returns NFS_OK if the server freed "stateid". Otherwise a
8260 * negative NFS4ERR value is returned.
8262 static int nfs41_free_stateid(struct nfs_server *server,
8263 nfs4_stateid *stateid,
8264 struct rpc_cred *cred)
8266 struct rpc_task *task;
8269 task = _nfs41_free_stateid(server, stateid, cred, true);
8271 return PTR_ERR(task);
8272 ret = rpc_wait_for_completion_task(task);
8274 ret = task->tk_status;
8280 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8282 struct rpc_task *task;
8283 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8285 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8286 nfs4_free_lock_state(server, lsp);
8292 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8293 const nfs4_stateid *s2)
8295 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8298 if (s1->seqid == s2->seqid)
8300 if (s1->seqid == 0 || s2->seqid == 0)
8306 #endif /* CONFIG_NFS_V4_1 */
8308 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8309 const nfs4_stateid *s2)
8311 return nfs4_stateid_match(s1, s2);
8315 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8316 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8317 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8318 .recover_open = nfs4_open_reclaim,
8319 .recover_lock = nfs4_lock_reclaim,
8320 .establish_clid = nfs4_init_clientid,
8321 .detect_trunking = nfs40_discover_server_trunking,
8324 #if defined(CONFIG_NFS_V4_1)
8325 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8326 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8327 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8328 .recover_open = nfs4_open_reclaim,
8329 .recover_lock = nfs4_lock_reclaim,
8330 .establish_clid = nfs41_init_clientid,
8331 .reclaim_complete = nfs41_proc_reclaim_complete,
8332 .detect_trunking = nfs41_discover_server_trunking,
8334 #endif /* CONFIG_NFS_V4_1 */
8336 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8337 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8338 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8339 .recover_open = nfs40_open_expired,
8340 .recover_lock = nfs4_lock_expired,
8341 .establish_clid = nfs4_init_clientid,
8344 #if defined(CONFIG_NFS_V4_1)
8345 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8346 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8347 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8348 .recover_open = nfs41_open_expired,
8349 .recover_lock = nfs41_lock_expired,
8350 .establish_clid = nfs41_init_clientid,
8352 #endif /* CONFIG_NFS_V4_1 */
8354 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8355 .sched_state_renewal = nfs4_proc_async_renew,
8356 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8357 .renew_lease = nfs4_proc_renew,
8360 #if defined(CONFIG_NFS_V4_1)
8361 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8362 .sched_state_renewal = nfs41_proc_async_sequence,
8363 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8364 .renew_lease = nfs4_proc_sequence,
8368 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8369 .get_locations = _nfs40_proc_get_locations,
8370 .fsid_present = _nfs40_proc_fsid_present,
8373 #if defined(CONFIG_NFS_V4_1)
8374 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8375 .get_locations = _nfs41_proc_get_locations,
8376 .fsid_present = _nfs41_proc_fsid_present,
8378 #endif /* CONFIG_NFS_V4_1 */
8380 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8382 .init_caps = NFS_CAP_READDIRPLUS
8383 | NFS_CAP_ATOMIC_OPEN
8384 | NFS_CAP_CHANGE_ATTR
8385 | NFS_CAP_POSIX_LOCK,
8386 .init_client = nfs40_init_client,
8387 .shutdown_client = nfs40_shutdown_client,
8388 .match_stateid = nfs4_match_stateid,
8389 .find_root_sec = nfs4_find_root_sec,
8390 .free_lock_state = nfs4_release_lockowner,
8391 .call_sync_ops = &nfs40_call_sync_ops,
8392 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8393 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8394 .state_renewal_ops = &nfs40_state_renewal_ops,
8395 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8398 #if defined(CONFIG_NFS_V4_1)
8399 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8401 .init_caps = NFS_CAP_READDIRPLUS
8402 | NFS_CAP_ATOMIC_OPEN
8403 | NFS_CAP_CHANGE_ATTR
8404 | NFS_CAP_POSIX_LOCK
8405 | NFS_CAP_STATEID_NFSV41
8406 | NFS_CAP_ATOMIC_OPEN_V1,
8407 .init_client = nfs41_init_client,
8408 .shutdown_client = nfs41_shutdown_client,
8409 .match_stateid = nfs41_match_stateid,
8410 .find_root_sec = nfs41_find_root_sec,
8411 .free_lock_state = nfs41_free_lock_state,
8412 .call_sync_ops = &nfs41_call_sync_ops,
8413 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8414 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8415 .state_renewal_ops = &nfs41_state_renewal_ops,
8416 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8420 #if defined(CONFIG_NFS_V4_2)
8421 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8423 .init_caps = NFS_CAP_READDIRPLUS
8424 | NFS_CAP_ATOMIC_OPEN
8425 | NFS_CAP_CHANGE_ATTR
8426 | NFS_CAP_POSIX_LOCK
8427 | NFS_CAP_STATEID_NFSV41
8428 | NFS_CAP_ATOMIC_OPEN_V1
8430 .init_client = nfs41_init_client,
8431 .shutdown_client = nfs41_shutdown_client,
8432 .match_stateid = nfs41_match_stateid,
8433 .find_root_sec = nfs41_find_root_sec,
8434 .free_lock_state = nfs41_free_lock_state,
8435 .call_sync_ops = &nfs41_call_sync_ops,
8436 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8437 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8438 .state_renewal_ops = &nfs41_state_renewal_ops,
8442 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8443 [0] = &nfs_v4_0_minor_ops,
8444 #if defined(CONFIG_NFS_V4_1)
8445 [1] = &nfs_v4_1_minor_ops,
8447 #if defined(CONFIG_NFS_V4_2)
8448 [2] = &nfs_v4_2_minor_ops,
8452 static const struct inode_operations nfs4_dir_inode_operations = {
8453 .create = nfs_create,
8454 .lookup = nfs_lookup,
8455 .atomic_open = nfs_atomic_open,
8457 .unlink = nfs_unlink,
8458 .symlink = nfs_symlink,
8462 .rename = nfs_rename,
8463 .permission = nfs_permission,
8464 .getattr = nfs_getattr,
8465 .setattr = nfs_setattr,
8466 .getxattr = generic_getxattr,
8467 .setxattr = generic_setxattr,
8468 .listxattr = generic_listxattr,
8469 .removexattr = generic_removexattr,
8472 static const struct inode_operations nfs4_file_inode_operations = {
8473 .permission = nfs_permission,
8474 .getattr = nfs_getattr,
8475 .setattr = nfs_setattr,
8476 .getxattr = generic_getxattr,
8477 .setxattr = generic_setxattr,
8478 .listxattr = generic_listxattr,
8479 .removexattr = generic_removexattr,
8482 const struct nfs_rpc_ops nfs_v4_clientops = {
8483 .version = 4, /* protocol version */
8484 .dentry_ops = &nfs4_dentry_operations,
8485 .dir_inode_ops = &nfs4_dir_inode_operations,
8486 .file_inode_ops = &nfs4_file_inode_operations,
8487 .file_ops = &nfs4_file_operations,
8488 .getroot = nfs4_proc_get_root,
8489 .submount = nfs4_submount,
8490 .try_mount = nfs4_try_mount,
8491 .getattr = nfs4_proc_getattr,
8492 .setattr = nfs4_proc_setattr,
8493 .lookup = nfs4_proc_lookup,
8494 .access = nfs4_proc_access,
8495 .readlink = nfs4_proc_readlink,
8496 .create = nfs4_proc_create,
8497 .remove = nfs4_proc_remove,
8498 .unlink_setup = nfs4_proc_unlink_setup,
8499 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8500 .unlink_done = nfs4_proc_unlink_done,
8501 .rename_setup = nfs4_proc_rename_setup,
8502 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8503 .rename_done = nfs4_proc_rename_done,
8504 .link = nfs4_proc_link,
8505 .symlink = nfs4_proc_symlink,
8506 .mkdir = nfs4_proc_mkdir,
8507 .rmdir = nfs4_proc_remove,
8508 .readdir = nfs4_proc_readdir,
8509 .mknod = nfs4_proc_mknod,
8510 .statfs = nfs4_proc_statfs,
8511 .fsinfo = nfs4_proc_fsinfo,
8512 .pathconf = nfs4_proc_pathconf,
8513 .set_capabilities = nfs4_server_capabilities,
8514 .decode_dirent = nfs4_decode_dirent,
8515 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8516 .read_setup = nfs4_proc_read_setup,
8517 .read_done = nfs4_read_done,
8518 .write_setup = nfs4_proc_write_setup,
8519 .write_done = nfs4_write_done,
8520 .commit_setup = nfs4_proc_commit_setup,
8521 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8522 .commit_done = nfs4_commit_done,
8523 .lock = nfs4_proc_lock,
8524 .clear_acl_cache = nfs4_zap_acl_attr,
8525 .close_context = nfs4_close_context,
8526 .open_context = nfs4_atomic_open,
8527 .have_delegation = nfs4_have_delegation,
8528 .return_delegation = nfs4_inode_return_delegation,
8529 .alloc_client = nfs4_alloc_client,
8530 .init_client = nfs4_init_client,
8531 .free_client = nfs4_free_client,
8532 .create_server = nfs4_create_server,
8533 .clone_server = nfs_clone_server,
8536 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8537 .prefix = XATTR_NAME_NFSV4_ACL,
8538 .list = nfs4_xattr_list_nfs4_acl,
8539 .get = nfs4_xattr_get_nfs4_acl,
8540 .set = nfs4_xattr_set_nfs4_acl,
8543 const struct xattr_handler *nfs4_xattr_handlers[] = {
8544 &nfs4_xattr_nfs4_acl_handler,
8545 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8546 &nfs4_xattr_nfs4_label_handler,