4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 static int _nfs4_proc_open(struct nfs4_opendata *data);
78 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
79 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
80 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
81 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
82 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
84 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
88 #ifdef CONFIG_NFS_V4_1
89 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
95 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
96 static inline struct nfs4_label *
97 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
119 nfs4_label_release_security(struct nfs4_label *label)
122 security_release_secctx(label->label, label->len);
124 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
127 return server->attr_bitmask;
129 return server->attr_bitmask_nl;
132 static inline struct nfs4_label *
133 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
137 nfs4_label_release_security(struct nfs4_label *label)
140 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141 { return server->attr_bitmask; }
144 /* Prevent leaks of NFSv4 errors into userland */
145 static int nfs4_map_errors(int err)
150 case -NFS4ERR_RESOURCE:
151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
154 case -NFS4ERR_WRONGSEC:
155 case -NFS4ERR_WRONG_CRED:
157 case -NFS4ERR_BADOWNER:
158 case -NFS4ERR_BADNAME:
160 case -NFS4ERR_SHARE_DENIED:
162 case -NFS4ERR_MINOR_VERS_MISMATCH:
163 return -EPROTONOSUPPORT;
164 case -NFS4ERR_ACCESS:
166 case -NFS4ERR_FILE_OPEN:
169 dprintk("%s could not handle NFSv4 error %d\n",
177 * This is our standard bitmap for GETATTR requests.
179 const u32 nfs4_fattr_bitmap[3] = {
181 | FATTR4_WORD0_CHANGE
184 | FATTR4_WORD0_FILEID,
186 | FATTR4_WORD1_NUMLINKS
188 | FATTR4_WORD1_OWNER_GROUP
189 | FATTR4_WORD1_RAWDEV
190 | FATTR4_WORD1_SPACE_USED
191 | FATTR4_WORD1_TIME_ACCESS
192 | FATTR4_WORD1_TIME_METADATA
193 | FATTR4_WORD1_TIME_MODIFY,
194 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
195 FATTR4_WORD2_SECURITY_LABEL
199 static const u32 nfs4_pnfs_open_bitmap[3] = {
201 | FATTR4_WORD0_CHANGE
204 | FATTR4_WORD0_FILEID,
206 | FATTR4_WORD1_NUMLINKS
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY,
214 FATTR4_WORD2_MDSTHRESHOLD
217 static const u32 nfs4_open_noattr_bitmap[3] = {
219 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID,
223 const u32 nfs4_statfs_bitmap[3] = {
224 FATTR4_WORD0_FILES_AVAIL
225 | FATTR4_WORD0_FILES_FREE
226 | FATTR4_WORD0_FILES_TOTAL,
227 FATTR4_WORD1_SPACE_AVAIL
228 | FATTR4_WORD1_SPACE_FREE
229 | FATTR4_WORD1_SPACE_TOTAL
232 const u32 nfs4_pathconf_bitmap[3] = {
234 | FATTR4_WORD0_MAXNAME,
238 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
239 | FATTR4_WORD0_MAXREAD
240 | FATTR4_WORD0_MAXWRITE
241 | FATTR4_WORD0_LEASE_TIME,
242 FATTR4_WORD1_TIME_DELTA
243 | FATTR4_WORD1_FS_LAYOUT_TYPES,
244 FATTR4_WORD2_LAYOUT_BLKSIZE
247 const u32 nfs4_fs_locations_bitmap[3] = {
249 | FATTR4_WORD0_CHANGE
252 | FATTR4_WORD0_FILEID
253 | FATTR4_WORD0_FS_LOCATIONS,
255 | FATTR4_WORD1_NUMLINKS
257 | FATTR4_WORD1_OWNER_GROUP
258 | FATTR4_WORD1_RAWDEV
259 | FATTR4_WORD1_SPACE_USED
260 | FATTR4_WORD1_TIME_ACCESS
261 | FATTR4_WORD1_TIME_METADATA
262 | FATTR4_WORD1_TIME_MODIFY
263 | FATTR4_WORD1_MOUNTED_ON_FILEID,
266 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
267 struct nfs4_readdir_arg *readdir)
272 readdir->cookie = cookie;
273 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
278 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
283 * NFSv4 servers do not return entries for '.' and '..'
284 * Therefore, we fake these entries here. We let '.'
285 * have cookie 0 and '..' have cookie 1. Note that
286 * when talking to the server, we always send cookie 0
289 start = p = kmap_atomic(*readdir->pages);
292 *p++ = xdr_one; /* next */
293 *p++ = xdr_zero; /* cookie, first word */
294 *p++ = xdr_one; /* cookie, second word */
295 *p++ = xdr_one; /* entry len */
296 memcpy(p, ".\0\0\0", 4); /* entry */
298 *p++ = xdr_one; /* bitmap length */
299 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
300 *p++ = htonl(8); /* attribute buffer length */
301 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_two; /* cookie, second word */
307 *p++ = xdr_two; /* entry len */
308 memcpy(p, "..\0\0", 4); /* entry */
310 *p++ = xdr_one; /* bitmap length */
311 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
312 *p++ = htonl(8); /* attribute buffer length */
313 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
315 readdir->pgbase = (char *)p - (char *)start;
316 readdir->count -= readdir->pgbase;
317 kunmap_atomic(start);
320 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
327 *timeout = NFS4_POLL_RETRY_MIN;
328 if (*timeout > NFS4_POLL_RETRY_MAX)
329 *timeout = NFS4_POLL_RETRY_MAX;
330 freezable_schedule_timeout_killable_unsafe(*timeout);
331 if (fatal_signal_pending(current))
337 /* This is the error handling routine for processes that are allowed
340 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
342 struct nfs_client *clp = server->nfs_client;
343 struct nfs4_state *state = exception->state;
344 struct inode *inode = exception->inode;
347 exception->retry = 0;
351 case -NFS4ERR_OPENMODE:
352 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
353 nfs4_inode_return_delegation(inode);
354 exception->retry = 1;
359 ret = nfs4_schedule_stateid_recovery(server, state);
362 goto wait_on_recovery;
363 case -NFS4ERR_DELEG_REVOKED:
364 case -NFS4ERR_ADMIN_REVOKED:
365 case -NFS4ERR_BAD_STATEID:
366 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
367 nfs_remove_bad_delegation(inode);
368 exception->retry = 1;
373 ret = nfs4_schedule_stateid_recovery(server, state);
376 goto wait_on_recovery;
377 case -NFS4ERR_EXPIRED:
379 ret = nfs4_schedule_stateid_recovery(server, state);
383 case -NFS4ERR_STALE_STATEID:
384 case -NFS4ERR_STALE_CLIENTID:
385 nfs4_schedule_lease_recovery(clp);
386 goto wait_on_recovery;
388 ret = nfs4_schedule_migration_recovery(server);
391 goto wait_on_recovery;
392 #if defined(CONFIG_NFS_V4_1)
393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
403 goto wait_on_recovery;
404 #endif /* defined(CONFIG_NFS_V4_1) */
405 case -NFS4ERR_FILE_OPEN:
406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
415 ret = nfs4_delay(server->client, &exception->timeout);
418 case -NFS4ERR_RETRY_UNCACHED_REP:
419 case -NFS4ERR_OLD_STATEID:
420 exception->retry = 1;
422 case -NFS4ERR_BADOWNER:
423 /* The following works around a Linux server bug! */
424 case -NFS4ERR_BADNAME:
425 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
426 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
427 exception->retry = 1;
428 printk(KERN_WARNING "NFS: v4 server %s "
429 "does not accept raw "
431 "Reenabling the idmapper.\n",
432 server->nfs_client->cl_hostname);
435 /* We failed to handle the error */
436 return nfs4_map_errors(ret);
438 ret = nfs4_wait_clnt_recover(clp);
439 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
442 exception->retry = 1;
447 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
448 * or 'false' otherwise.
450 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
452 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
454 if (flavor == RPC_AUTH_GSS_KRB5I ||
455 flavor == RPC_AUTH_GSS_KRB5P)
461 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
463 spin_lock(&clp->cl_lock);
464 if (time_before(clp->cl_last_renewal,timestamp))
465 clp->cl_last_renewal = timestamp;
466 spin_unlock(&clp->cl_lock);
469 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
471 do_renew_lease(server->nfs_client, timestamp);
474 struct nfs4_call_sync_data {
475 const struct nfs_server *seq_server;
476 struct nfs4_sequence_args *seq_args;
477 struct nfs4_sequence_res *seq_res;
480 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
481 struct nfs4_sequence_res *res, int cache_reply)
483 args->sa_slot = NULL;
484 args->sa_cache_this = cache_reply;
485 args->sa_privileged = 0;
490 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
492 args->sa_privileged = 1;
495 static int nfs40_setup_sequence(const struct nfs_server *server,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 struct rpc_task *task)
500 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
501 struct nfs4_slot *slot;
503 /* slot already allocated? */
504 if (res->sr_slot != NULL)
507 spin_lock(&tbl->slot_tbl_lock);
508 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
511 slot = nfs4_alloc_slot(tbl);
513 if (slot == ERR_PTR(-ENOMEM))
514 task->tk_timeout = HZ >> 2;
517 spin_unlock(&tbl->slot_tbl_lock);
519 args->sa_slot = slot;
523 rpc_call_start(task);
527 if (args->sa_privileged)
528 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
529 NULL, RPC_PRIORITY_PRIVILEGED);
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
536 static int nfs40_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
539 struct nfs4_slot *slot = res->sr_slot;
540 struct nfs4_slot_table *tbl;
542 if (!RPC_WAS_SENT(task))
546 spin_lock(&tbl->slot_tbl_lock);
547 if (!nfs41_wake_and_assign_slot(tbl, slot))
548 nfs4_free_slot(tbl, slot);
549 spin_unlock(&tbl->slot_tbl_lock);
556 #if defined(CONFIG_NFS_V4_1)
558 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
560 struct nfs4_session *session;
561 struct nfs4_slot_table *tbl;
562 bool send_new_highest_used_slotid = false;
565 /* just wake up the next guy waiting since
566 * we may have not consumed a slot after all */
567 dprintk("%s: No slot\n", __func__);
570 tbl = res->sr_slot->table;
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, res->sr_slot)) {
581 send_new_highest_used_slotid = false;
584 nfs4_free_slot(tbl, res->sr_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 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
597 struct nfs4_session *session;
598 struct nfs4_slot *slot;
599 struct nfs_client *clp;
600 bool interrupted = false;
603 /* don't increment the sequence number if the task wasn't sent */
604 if (!RPC_WAS_SENT(task))
608 session = slot->table->session;
610 if (slot->interrupted) {
611 slot->interrupted = 0;
615 trace_nfs4_sequence_done(session, res);
616 /* Check the SEQUENCE operation status */
617 switch (res->sr_status) {
619 /* Update the slot's sequence and clientid lease timer */
622 do_renew_lease(clp, res->sr_timestamp);
623 /* Check sequence flags */
624 if (res->sr_status_flags != 0)
625 nfs4_schedule_lease_recovery(clp);
626 nfs41_update_target_slotid(slot->table, slot, res);
630 * sr_status remains 1 if an RPC level error occurred.
631 * The server may or may not have processed the sequence
633 * Mark the slot as having hosted an interrupted RPC call.
635 slot->interrupted = 1;
638 /* The server detected a resend of the RPC call and
639 * returned NFS4ERR_DELAY as per Section 2.10.6.2
642 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
647 case -NFS4ERR_BADSLOT:
649 * The slot id we used was probably retired. Try again
650 * using a different slot id.
653 case -NFS4ERR_SEQ_MISORDERED:
655 * Was the last operation on this sequence interrupted?
656 * If so, retry after bumping the sequence number.
663 * Could this slot have been previously retired?
664 * If so, then the server may be expecting seq_nr = 1!
666 if (slot->seq_nr != 1) {
671 case -NFS4ERR_SEQ_FALSE_RETRY:
675 /* Just update the slot sequence no. */
679 /* The session may be reset by one of the error handlers. */
680 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
681 nfs41_sequence_free_slot(res);
684 if (rpc_restart_call_prepare(task)) {
690 if (!rpc_restart_call(task))
692 rpc_delay(task, NFS4_POLL_RETRY_MAX);
696 static int nfs4_sequence_done(struct rpc_task *task,
697 struct nfs4_sequence_res *res)
699 if (res->sr_slot == NULL)
701 if (!res->sr_slot->table->session)
702 return nfs40_sequence_done(task, res);
703 return nfs41_sequence_done(task, res);
706 int nfs41_setup_sequence(struct nfs4_session *session,
707 struct nfs4_sequence_args *args,
708 struct nfs4_sequence_res *res,
709 struct rpc_task *task)
711 struct nfs4_slot *slot;
712 struct nfs4_slot_table *tbl;
714 dprintk("--> %s\n", __func__);
715 /* slot already allocated? */
716 if (res->sr_slot != NULL)
719 tbl = &session->fc_slot_table;
721 task->tk_timeout = 0;
723 spin_lock(&tbl->slot_tbl_lock);
724 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
725 !args->sa_privileged) {
726 /* The state manager will wait until the slot table is empty */
727 dprintk("%s session is draining\n", __func__);
731 slot = nfs4_alloc_slot(tbl);
733 /* If out of memory, try again in 1/4 second */
734 if (slot == ERR_PTR(-ENOMEM))
735 task->tk_timeout = HZ >> 2;
736 dprintk("<-- %s: no free slots\n", __func__);
739 spin_unlock(&tbl->slot_tbl_lock);
741 args->sa_slot = slot;
743 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
744 slot->slot_nr, slot->seq_nr);
747 res->sr_timestamp = jiffies;
748 res->sr_status_flags = 0;
750 * sr_status is only set in decode_sequence, and so will remain
751 * set to 1 if an rpc level failure occurs.
754 trace_nfs4_setup_sequence(session, args);
756 rpc_call_start(task);
759 /* Privileged tasks are queued with top priority */
760 if (args->sa_privileged)
761 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
762 NULL, RPC_PRIORITY_PRIVILEGED);
764 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
765 spin_unlock(&tbl->slot_tbl_lock);
768 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
770 static int nfs4_setup_sequence(const struct nfs_server *server,
771 struct nfs4_sequence_args *args,
772 struct nfs4_sequence_res *res,
773 struct rpc_task *task)
775 struct nfs4_session *session = nfs4_get_session(server);
779 return nfs40_setup_sequence(server, args, res, task);
781 dprintk("--> %s clp %p session %p sr_slot %u\n",
782 __func__, session->clp, session, res->sr_slot ?
783 res->sr_slot->slot_nr : NFS4_NO_SLOT);
785 ret = nfs41_setup_sequence(session, args, res, task);
787 dprintk("<-- %s status=%d\n", __func__, ret);
791 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
793 struct nfs4_call_sync_data *data = calldata;
794 struct nfs4_session *session = nfs4_get_session(data->seq_server);
796 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
798 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
801 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
803 struct nfs4_call_sync_data *data = calldata;
805 nfs41_sequence_done(task, data->seq_res);
808 static const struct rpc_call_ops nfs41_call_sync_ops = {
809 .rpc_call_prepare = nfs41_call_sync_prepare,
810 .rpc_call_done = nfs41_call_sync_done,
813 #else /* !CONFIG_NFS_V4_1 */
815 static int nfs4_setup_sequence(const struct nfs_server *server,
816 struct nfs4_sequence_args *args,
817 struct nfs4_sequence_res *res,
818 struct rpc_task *task)
820 return nfs40_setup_sequence(server, args, res, task);
823 static int nfs4_sequence_done(struct rpc_task *task,
824 struct nfs4_sequence_res *res)
826 return nfs40_sequence_done(task, res);
829 #endif /* !CONFIG_NFS_V4_1 */
831 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
833 struct nfs4_call_sync_data *data = calldata;
834 nfs4_setup_sequence(data->seq_server,
835 data->seq_args, data->seq_res, task);
838 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
840 struct nfs4_call_sync_data *data = calldata;
841 nfs4_sequence_done(task, data->seq_res);
844 static const struct rpc_call_ops nfs40_call_sync_ops = {
845 .rpc_call_prepare = nfs40_call_sync_prepare,
846 .rpc_call_done = nfs40_call_sync_done,
849 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
850 struct nfs_server *server,
851 struct rpc_message *msg,
852 struct nfs4_sequence_args *args,
853 struct nfs4_sequence_res *res)
856 struct rpc_task *task;
857 struct nfs_client *clp = server->nfs_client;
858 struct nfs4_call_sync_data data = {
859 .seq_server = server,
863 struct rpc_task_setup task_setup = {
866 .callback_ops = clp->cl_mvops->call_sync_ops,
867 .callback_data = &data
870 task = rpc_run_task(&task_setup);
874 ret = task->tk_status;
881 int nfs4_call_sync(struct rpc_clnt *clnt,
882 struct nfs_server *server,
883 struct rpc_message *msg,
884 struct nfs4_sequence_args *args,
885 struct nfs4_sequence_res *res,
888 nfs4_init_sequence(args, res, cache_reply);
889 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
892 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
894 struct nfs_inode *nfsi = NFS_I(dir);
896 spin_lock(&dir->i_lock);
897 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
898 if (!cinfo->atomic || cinfo->before != dir->i_version)
899 nfs_force_lookup_revalidate(dir);
900 dir->i_version = cinfo->after;
901 nfs_fscache_invalidate(dir);
902 spin_unlock(&dir->i_lock);
905 struct nfs4_opendata {
907 struct nfs_openargs o_arg;
908 struct nfs_openres o_res;
909 struct nfs_open_confirmargs c_arg;
910 struct nfs_open_confirmres c_res;
911 struct nfs4_string owner_name;
912 struct nfs4_string group_name;
913 struct nfs_fattr f_attr;
914 struct nfs4_label *f_label;
916 struct dentry *dentry;
917 struct nfs4_state_owner *owner;
918 struct nfs4_state *state;
920 unsigned long timestamp;
921 unsigned int rpc_done : 1;
922 unsigned int file_created : 1;
923 unsigned int is_recover : 1;
928 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
929 int err, struct nfs4_exception *exception)
933 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
935 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
936 exception->retry = 1;
940 static enum open_claim_type4
941 nfs4_map_atomic_open_claim(struct nfs_server *server,
942 enum open_claim_type4 claim)
944 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
949 case NFS4_OPEN_CLAIM_FH:
950 return NFS4_OPEN_CLAIM_NULL;
951 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
952 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
953 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
954 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
958 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
960 p->o_res.f_attr = &p->f_attr;
961 p->o_res.f_label = p->f_label;
962 p->o_res.seqid = p->o_arg.seqid;
963 p->c_res.seqid = p->c_arg.seqid;
964 p->o_res.server = p->o_arg.server;
965 p->o_res.access_request = p->o_arg.access;
966 nfs_fattr_init(&p->f_attr);
967 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
970 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
971 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
972 const struct iattr *attrs,
973 struct nfs4_label *label,
974 enum open_claim_type4 claim,
977 struct dentry *parent = dget_parent(dentry);
978 struct inode *dir = parent->d_inode;
979 struct nfs_server *server = NFS_SERVER(dir);
980 struct nfs4_opendata *p;
982 p = kzalloc(sizeof(*p), gfp_mask);
986 p->f_label = nfs4_label_alloc(server, gfp_mask);
987 if (IS_ERR(p->f_label))
990 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
991 if (p->o_arg.seqid == NULL)
993 nfs_sb_active(dentry->d_sb);
994 p->dentry = dget(dentry);
997 atomic_inc(&sp->so_count);
998 p->o_arg.open_flags = flags;
999 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1000 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1001 * will return permission denied for all bits until close */
1002 if (!(flags & O_EXCL)) {
1003 /* ask server to check for all possible rights as results
1005 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1006 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1008 p->o_arg.clientid = server->nfs_client->cl_clientid;
1009 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1010 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1011 p->o_arg.name = &dentry->d_name;
1012 p->o_arg.server = server;
1013 p->o_arg.bitmask = nfs4_bitmask(server, label);
1014 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1015 p->o_arg.label = label;
1016 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1017 switch (p->o_arg.claim) {
1018 case NFS4_OPEN_CLAIM_NULL:
1019 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1020 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1021 p->o_arg.fh = NFS_FH(dir);
1023 case NFS4_OPEN_CLAIM_PREVIOUS:
1024 case NFS4_OPEN_CLAIM_FH:
1025 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1026 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1027 p->o_arg.fh = NFS_FH(dentry->d_inode);
1029 if (attrs != NULL && attrs->ia_valid != 0) {
1032 p->o_arg.u.attrs = &p->attrs;
1033 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1036 verf[1] = current->pid;
1037 memcpy(p->o_arg.u.verifier.data, verf,
1038 sizeof(p->o_arg.u.verifier.data));
1040 p->c_arg.fh = &p->o_res.fh;
1041 p->c_arg.stateid = &p->o_res.stateid;
1042 p->c_arg.seqid = p->o_arg.seqid;
1043 nfs4_init_opendata_res(p);
1044 kref_init(&p->kref);
1048 nfs4_label_free(p->f_label);
1056 static void nfs4_opendata_free(struct kref *kref)
1058 struct nfs4_opendata *p = container_of(kref,
1059 struct nfs4_opendata, kref);
1060 struct super_block *sb = p->dentry->d_sb;
1062 nfs_free_seqid(p->o_arg.seqid);
1063 if (p->state != NULL)
1064 nfs4_put_open_state(p->state);
1065 nfs4_put_state_owner(p->owner);
1067 nfs4_label_free(p->f_label);
1071 nfs_sb_deactive(sb);
1072 nfs_fattr_free_names(&p->f_attr);
1076 static void nfs4_opendata_put(struct nfs4_opendata *p)
1079 kref_put(&p->kref, nfs4_opendata_free);
1082 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1086 ret = rpc_wait_for_completion_task(task);
1090 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1094 if (open_mode & (O_EXCL|O_TRUNC))
1096 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1098 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1099 && state->n_rdonly != 0;
1102 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1103 && state->n_wronly != 0;
1105 case FMODE_READ|FMODE_WRITE:
1106 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1107 && state->n_rdwr != 0;
1113 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1115 if (delegation == NULL)
1117 if ((delegation->type & fmode) != fmode)
1119 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1121 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1123 nfs_mark_delegation_referenced(delegation);
1127 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1136 case FMODE_READ|FMODE_WRITE:
1139 nfs4_state_set_mode_locked(state, state->state | fmode);
1142 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1144 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1145 nfs4_stateid_copy(&state->stateid, stateid);
1146 nfs4_stateid_copy(&state->open_stateid, stateid);
1147 set_bit(NFS_OPEN_STATE, &state->flags);
1150 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1153 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1155 case FMODE_READ|FMODE_WRITE:
1156 set_bit(NFS_O_RDWR_STATE, &state->flags);
1160 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1162 write_seqlock(&state->seqlock);
1163 nfs_set_open_stateid_locked(state, stateid, fmode);
1164 write_sequnlock(&state->seqlock);
1167 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1170 * Protect the call to nfs4_state_set_mode_locked and
1171 * serialise the stateid update
1173 write_seqlock(&state->seqlock);
1174 if (deleg_stateid != NULL) {
1175 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1176 set_bit(NFS_DELEGATED_STATE, &state->flags);
1178 if (open_stateid != NULL)
1179 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1180 write_sequnlock(&state->seqlock);
1181 spin_lock(&state->owner->so_lock);
1182 update_open_stateflags(state, fmode);
1183 spin_unlock(&state->owner->so_lock);
1186 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1188 struct nfs_inode *nfsi = NFS_I(state->inode);
1189 struct nfs_delegation *deleg_cur;
1192 fmode &= (FMODE_READ|FMODE_WRITE);
1195 deleg_cur = rcu_dereference(nfsi->delegation);
1196 if (deleg_cur == NULL)
1199 spin_lock(&deleg_cur->lock);
1200 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1201 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1202 (deleg_cur->type & fmode) != fmode)
1203 goto no_delegation_unlock;
1205 if (delegation == NULL)
1206 delegation = &deleg_cur->stateid;
1207 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1208 goto no_delegation_unlock;
1210 nfs_mark_delegation_referenced(deleg_cur);
1211 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1213 no_delegation_unlock:
1214 spin_unlock(&deleg_cur->lock);
1218 if (!ret && open_stateid != NULL) {
1219 __update_open_stateid(state, open_stateid, NULL, fmode);
1227 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1229 struct nfs_delegation *delegation;
1232 delegation = rcu_dereference(NFS_I(inode)->delegation);
1233 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1238 nfs4_inode_return_delegation(inode);
1241 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1243 struct nfs4_state *state = opendata->state;
1244 struct nfs_inode *nfsi = NFS_I(state->inode);
1245 struct nfs_delegation *delegation;
1246 int open_mode = opendata->o_arg.open_flags;
1247 fmode_t fmode = opendata->o_arg.fmode;
1248 nfs4_stateid stateid;
1252 if (can_open_cached(state, fmode, open_mode)) {
1253 spin_lock(&state->owner->so_lock);
1254 if (can_open_cached(state, fmode, open_mode)) {
1255 update_open_stateflags(state, fmode);
1256 spin_unlock(&state->owner->so_lock);
1257 goto out_return_state;
1259 spin_unlock(&state->owner->so_lock);
1262 delegation = rcu_dereference(nfsi->delegation);
1263 if (!can_open_delegated(delegation, fmode)) {
1267 /* Save the delegation */
1268 nfs4_stateid_copy(&stateid, &delegation->stateid);
1270 nfs_release_seqid(opendata->o_arg.seqid);
1271 if (!opendata->is_recover) {
1272 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1278 /* Try to update the stateid using the delegation */
1279 if (update_open_stateid(state, NULL, &stateid, fmode))
1280 goto out_return_state;
1283 return ERR_PTR(ret);
1285 atomic_inc(&state->count);
1290 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1292 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1293 struct nfs_delegation *delegation;
1294 int delegation_flags = 0;
1297 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1299 delegation_flags = delegation->flags;
1301 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1302 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1303 "returning a delegation for "
1304 "OPEN(CLAIM_DELEGATE_CUR)\n",
1306 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1307 nfs_inode_set_delegation(state->inode,
1308 data->owner->so_cred,
1311 nfs_inode_reclaim_delegation(state->inode,
1312 data->owner->so_cred,
1317 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1318 * and update the nfs4_state.
1320 static struct nfs4_state *
1321 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1323 struct inode *inode = data->state->inode;
1324 struct nfs4_state *state = data->state;
1327 if (!data->rpc_done) {
1328 if (data->rpc_status) {
1329 ret = data->rpc_status;
1332 /* cached opens have already been processed */
1336 ret = nfs_refresh_inode(inode, &data->f_attr);
1340 if (data->o_res.delegation_type != 0)
1341 nfs4_opendata_check_deleg(data, state);
1343 update_open_stateid(state, &data->o_res.stateid, NULL,
1345 atomic_inc(&state->count);
1349 return ERR_PTR(ret);
1353 static struct nfs4_state *
1354 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1356 struct inode *inode;
1357 struct nfs4_state *state = NULL;
1360 if (!data->rpc_done) {
1361 state = nfs4_try_open_cached(data);
1366 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1368 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1369 ret = PTR_ERR(inode);
1373 state = nfs4_get_open_state(inode, data->owner);
1376 if (data->o_res.delegation_type != 0)
1377 nfs4_opendata_check_deleg(data, state);
1378 update_open_stateid(state, &data->o_res.stateid, NULL,
1382 nfs_release_seqid(data->o_arg.seqid);
1387 return ERR_PTR(ret);
1390 static struct nfs4_state *
1391 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1393 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1394 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1395 return _nfs4_opendata_to_nfs4_state(data);
1398 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1400 struct nfs_inode *nfsi = NFS_I(state->inode);
1401 struct nfs_open_context *ctx;
1403 spin_lock(&state->inode->i_lock);
1404 list_for_each_entry(ctx, &nfsi->open_files, list) {
1405 if (ctx->state != state)
1407 get_nfs_open_context(ctx);
1408 spin_unlock(&state->inode->i_lock);
1411 spin_unlock(&state->inode->i_lock);
1412 return ERR_PTR(-ENOENT);
1415 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1416 struct nfs4_state *state, enum open_claim_type4 claim)
1418 struct nfs4_opendata *opendata;
1420 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1421 NULL, NULL, claim, GFP_NOFS);
1422 if (opendata == NULL)
1423 return ERR_PTR(-ENOMEM);
1424 opendata->state = state;
1425 atomic_inc(&state->count);
1429 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1431 struct nfs4_state *newstate;
1434 opendata->o_arg.open_flags = 0;
1435 opendata->o_arg.fmode = fmode;
1436 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1437 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1438 nfs4_init_opendata_res(opendata);
1439 ret = _nfs4_recover_proc_open(opendata);
1442 newstate = nfs4_opendata_to_nfs4_state(opendata);
1443 if (IS_ERR(newstate))
1444 return PTR_ERR(newstate);
1445 nfs4_close_state(newstate, fmode);
1450 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1452 struct nfs4_state *newstate;
1455 /* memory barrier prior to reading state->n_* */
1456 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1457 clear_bit(NFS_OPEN_STATE, &state->flags);
1459 if (state->n_rdwr != 0) {
1460 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1461 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1464 if (newstate != state)
1467 if (state->n_wronly != 0) {
1468 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1469 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1472 if (newstate != state)
1475 if (state->n_rdonly != 0) {
1476 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1477 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1480 if (newstate != state)
1484 * We may have performed cached opens for all three recoveries.
1485 * Check if we need to update the current stateid.
1487 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1488 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1489 write_seqlock(&state->seqlock);
1490 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1491 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1492 write_sequnlock(&state->seqlock);
1499 * reclaim state on the server after a reboot.
1501 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1503 struct nfs_delegation *delegation;
1504 struct nfs4_opendata *opendata;
1505 fmode_t delegation_type = 0;
1508 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1509 NFS4_OPEN_CLAIM_PREVIOUS);
1510 if (IS_ERR(opendata))
1511 return PTR_ERR(opendata);
1513 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1514 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1515 delegation_type = delegation->type;
1517 opendata->o_arg.u.delegation_type = delegation_type;
1518 status = nfs4_open_recover(opendata, state);
1519 nfs4_opendata_put(opendata);
1523 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1525 struct nfs_server *server = NFS_SERVER(state->inode);
1526 struct nfs4_exception exception = { };
1529 err = _nfs4_do_open_reclaim(ctx, state);
1530 trace_nfs4_open_reclaim(ctx, 0, err);
1531 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1533 if (err != -NFS4ERR_DELAY)
1535 nfs4_handle_exception(server, err, &exception);
1536 } while (exception.retry);
1540 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1542 struct nfs_open_context *ctx;
1545 ctx = nfs4_state_find_open_context(state);
1548 ret = nfs4_do_open_reclaim(ctx, state);
1549 put_nfs_open_context(ctx);
1553 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1557 printk(KERN_ERR "NFS: %s: unhandled error "
1558 "%d.\n", __func__, err);
1563 case -NFS4ERR_BADSESSION:
1564 case -NFS4ERR_BADSLOT:
1565 case -NFS4ERR_BAD_HIGH_SLOT:
1566 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1567 case -NFS4ERR_DEADSESSION:
1568 set_bit(NFS_DELEGATED_STATE, &state->flags);
1569 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1571 case -NFS4ERR_STALE_CLIENTID:
1572 case -NFS4ERR_STALE_STATEID:
1573 set_bit(NFS_DELEGATED_STATE, &state->flags);
1574 case -NFS4ERR_EXPIRED:
1575 /* Don't recall a delegation if it was lost */
1576 nfs4_schedule_lease_recovery(server->nfs_client);
1578 case -NFS4ERR_DELEG_REVOKED:
1579 case -NFS4ERR_ADMIN_REVOKED:
1580 case -NFS4ERR_BAD_STATEID:
1581 case -NFS4ERR_OPENMODE:
1582 nfs_inode_find_state_and_recover(state->inode,
1584 nfs4_schedule_stateid_recovery(server, state);
1586 case -NFS4ERR_DELAY:
1587 case -NFS4ERR_GRACE:
1588 set_bit(NFS_DELEGATED_STATE, &state->flags);
1592 case -NFS4ERR_DENIED:
1593 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1599 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1601 struct nfs_server *server = NFS_SERVER(state->inode);
1602 struct nfs4_opendata *opendata;
1605 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1606 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1607 if (IS_ERR(opendata))
1608 return PTR_ERR(opendata);
1609 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1610 err = nfs4_open_recover(opendata, state);
1611 nfs4_opendata_put(opendata);
1612 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1615 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1617 struct nfs4_opendata *data = calldata;
1619 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1620 &data->o_res.seq_res, task);
1623 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1625 struct nfs4_opendata *data = calldata;
1627 nfs40_sequence_done(task, &data->o_res.seq_res);
1629 data->rpc_status = task->tk_status;
1630 if (data->rpc_status == 0) {
1631 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1632 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1633 renew_lease(data->o_res.server, data->timestamp);
1638 static void nfs4_open_confirm_release(void *calldata)
1640 struct nfs4_opendata *data = calldata;
1641 struct nfs4_state *state = NULL;
1643 /* If this request hasn't been cancelled, do nothing */
1644 if (data->cancelled == 0)
1646 /* In case of error, no cleanup! */
1647 if (!data->rpc_done)
1649 state = nfs4_opendata_to_nfs4_state(data);
1651 nfs4_close_state(state, data->o_arg.fmode);
1653 nfs4_opendata_put(data);
1656 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1657 .rpc_call_prepare = nfs4_open_confirm_prepare,
1658 .rpc_call_done = nfs4_open_confirm_done,
1659 .rpc_release = nfs4_open_confirm_release,
1663 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1665 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1667 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1668 struct rpc_task *task;
1669 struct rpc_message msg = {
1670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1671 .rpc_argp = &data->c_arg,
1672 .rpc_resp = &data->c_res,
1673 .rpc_cred = data->owner->so_cred,
1675 struct rpc_task_setup task_setup_data = {
1676 .rpc_client = server->client,
1677 .rpc_message = &msg,
1678 .callback_ops = &nfs4_open_confirm_ops,
1679 .callback_data = data,
1680 .workqueue = nfsiod_workqueue,
1681 .flags = RPC_TASK_ASYNC,
1685 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
1686 kref_get(&data->kref);
1688 data->rpc_status = 0;
1689 data->timestamp = jiffies;
1690 task = rpc_run_task(&task_setup_data);
1692 return PTR_ERR(task);
1693 status = nfs4_wait_for_completion_rpc_task(task);
1695 data->cancelled = 1;
1698 status = data->rpc_status;
1703 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1705 struct nfs4_opendata *data = calldata;
1706 struct nfs4_state_owner *sp = data->owner;
1707 struct nfs_client *clp = sp->so_server->nfs_client;
1709 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1712 * Check if we still need to send an OPEN call, or if we can use
1713 * a delegation instead.
1715 if (data->state != NULL) {
1716 struct nfs_delegation *delegation;
1718 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1721 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1722 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1723 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1724 can_open_delegated(delegation, data->o_arg.fmode))
1725 goto unlock_no_action;
1728 /* Update client id. */
1729 data->o_arg.clientid = clp->cl_clientid;
1730 switch (data->o_arg.claim) {
1731 case NFS4_OPEN_CLAIM_PREVIOUS:
1732 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1733 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1734 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1735 case NFS4_OPEN_CLAIM_FH:
1736 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1737 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1739 data->timestamp = jiffies;
1740 if (nfs4_setup_sequence(data->o_arg.server,
1741 &data->o_arg.seq_args,
1742 &data->o_res.seq_res,
1744 nfs_release_seqid(data->o_arg.seqid);
1746 /* Set the create mode (note dependency on the session type) */
1747 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1748 if (data->o_arg.open_flags & O_EXCL) {
1749 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1750 if (nfs4_has_persistent_session(clp))
1751 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1752 else if (clp->cl_mvops->minor_version > 0)
1753 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1759 task->tk_action = NULL;
1761 nfs4_sequence_done(task, &data->o_res.seq_res);
1764 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1766 struct nfs4_opendata *data = calldata;
1768 data->rpc_status = task->tk_status;
1770 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1773 if (task->tk_status == 0) {
1774 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1775 switch (data->o_res.f_attr->mode & S_IFMT) {
1779 data->rpc_status = -ELOOP;
1782 data->rpc_status = -EISDIR;
1785 data->rpc_status = -ENOTDIR;
1788 renew_lease(data->o_res.server, data->timestamp);
1789 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1790 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1795 static void nfs4_open_release(void *calldata)
1797 struct nfs4_opendata *data = calldata;
1798 struct nfs4_state *state = NULL;
1800 /* If this request hasn't been cancelled, do nothing */
1801 if (data->cancelled == 0)
1803 /* In case of error, no cleanup! */
1804 if (data->rpc_status != 0 || !data->rpc_done)
1806 /* In case we need an open_confirm, no cleanup! */
1807 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1809 state = nfs4_opendata_to_nfs4_state(data);
1811 nfs4_close_state(state, data->o_arg.fmode);
1813 nfs4_opendata_put(data);
1816 static const struct rpc_call_ops nfs4_open_ops = {
1817 .rpc_call_prepare = nfs4_open_prepare,
1818 .rpc_call_done = nfs4_open_done,
1819 .rpc_release = nfs4_open_release,
1822 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1824 struct inode *dir = data->dir->d_inode;
1825 struct nfs_server *server = NFS_SERVER(dir);
1826 struct nfs_openargs *o_arg = &data->o_arg;
1827 struct nfs_openres *o_res = &data->o_res;
1828 struct rpc_task *task;
1829 struct rpc_message msg = {
1830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1833 .rpc_cred = data->owner->so_cred,
1835 struct rpc_task_setup task_setup_data = {
1836 .rpc_client = server->client,
1837 .rpc_message = &msg,
1838 .callback_ops = &nfs4_open_ops,
1839 .callback_data = data,
1840 .workqueue = nfsiod_workqueue,
1841 .flags = RPC_TASK_ASYNC,
1845 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1846 kref_get(&data->kref);
1848 data->rpc_status = 0;
1849 data->cancelled = 0;
1850 data->is_recover = 0;
1852 nfs4_set_sequence_privileged(&o_arg->seq_args);
1853 data->is_recover = 1;
1855 task = rpc_run_task(&task_setup_data);
1857 return PTR_ERR(task);
1858 status = nfs4_wait_for_completion_rpc_task(task);
1860 data->cancelled = 1;
1863 status = data->rpc_status;
1869 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1871 struct inode *dir = data->dir->d_inode;
1872 struct nfs_openres *o_res = &data->o_res;
1875 status = nfs4_run_open_task(data, 1);
1876 if (status != 0 || !data->rpc_done)
1879 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1881 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1882 status = _nfs4_proc_open_confirm(data);
1890 static int nfs4_opendata_access(struct rpc_cred *cred,
1891 struct nfs4_opendata *opendata,
1892 struct nfs4_state *state, fmode_t fmode,
1895 struct nfs_access_entry cache;
1898 /* access call failed or for some reason the server doesn't
1899 * support any access modes -- defer access call until later */
1900 if (opendata->o_res.access_supported == 0)
1904 /* don't check MAY_WRITE - a newly created file may not have
1905 * write mode bits, but POSIX allows the creating process to write.
1906 * use openflags to check for exec, because fmode won't
1907 * always have FMODE_EXEC set when file open for exec. */
1908 if (openflags & __FMODE_EXEC) {
1909 /* ONLY check for exec rights */
1911 } else if (fmode & FMODE_READ)
1915 cache.jiffies = jiffies;
1916 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1917 nfs_access_add_cache(state->inode, &cache);
1919 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1922 /* even though OPEN succeeded, access is denied. Close the file */
1923 nfs4_close_state(state, fmode);
1928 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1930 static int _nfs4_proc_open(struct nfs4_opendata *data)
1932 struct inode *dir = data->dir->d_inode;
1933 struct nfs_server *server = NFS_SERVER(dir);
1934 struct nfs_openargs *o_arg = &data->o_arg;
1935 struct nfs_openres *o_res = &data->o_res;
1938 status = nfs4_run_open_task(data, 0);
1939 if (!data->rpc_done)
1942 if (status == -NFS4ERR_BADNAME &&
1943 !(o_arg->open_flags & O_CREAT))
1948 nfs_fattr_map_and_free_names(server, &data->f_attr);
1950 if (o_arg->open_flags & O_CREAT) {
1951 update_changeattr(dir, &o_res->cinfo);
1952 if (o_arg->open_flags & O_EXCL)
1953 data->file_created = 1;
1954 else if (o_res->cinfo.before != o_res->cinfo.after)
1955 data->file_created = 1;
1957 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1958 server->caps &= ~NFS_CAP_POSIX_LOCK;
1959 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1960 status = _nfs4_proc_open_confirm(data);
1964 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1965 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
1969 static int nfs4_recover_expired_lease(struct nfs_server *server)
1971 return nfs4_client_recover_expired_lease(server->nfs_client);
1976 * reclaim state on the server after a network partition.
1977 * Assumes caller holds the appropriate lock
1979 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1981 struct nfs4_opendata *opendata;
1984 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1985 NFS4_OPEN_CLAIM_FH);
1986 if (IS_ERR(opendata))
1987 return PTR_ERR(opendata);
1988 ret = nfs4_open_recover(opendata, state);
1990 d_drop(ctx->dentry);
1991 nfs4_opendata_put(opendata);
1995 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1997 struct nfs_server *server = NFS_SERVER(state->inode);
1998 struct nfs4_exception exception = { };
2002 err = _nfs4_open_expired(ctx, state);
2003 trace_nfs4_open_expired(ctx, 0, err);
2004 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2009 case -NFS4ERR_GRACE:
2010 case -NFS4ERR_DELAY:
2011 nfs4_handle_exception(server, err, &exception);
2014 } while (exception.retry);
2019 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2021 struct nfs_open_context *ctx;
2024 ctx = nfs4_state_find_open_context(state);
2027 ret = nfs4_do_open_expired(ctx, state);
2028 put_nfs_open_context(ctx);
2032 #if defined(CONFIG_NFS_V4_1)
2033 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
2035 struct nfs_server *server = NFS_SERVER(state->inode);
2036 nfs4_stateid *stateid = &state->stateid;
2037 struct nfs_delegation *delegation;
2038 struct rpc_cred *cred = NULL;
2039 int status = -NFS4ERR_BAD_STATEID;
2041 /* If a state reset has been done, test_stateid is unneeded */
2042 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2045 /* Get the delegation credential for use by test/free_stateid */
2047 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2048 if (delegation != NULL &&
2049 nfs4_stateid_match(&delegation->stateid, stateid)) {
2050 cred = get_rpccred(delegation->cred);
2052 status = nfs41_test_stateid(server, stateid, cred);
2053 trace_nfs4_test_delegation_stateid(state, NULL, status);
2057 if (status != NFS_OK) {
2058 /* Free the stateid unless the server explicitly
2059 * informs us the stateid is unrecognized. */
2060 if (status != -NFS4ERR_BAD_STATEID)
2061 nfs41_free_stateid(server, stateid, cred);
2062 nfs_remove_bad_delegation(state->inode);
2064 write_seqlock(&state->seqlock);
2065 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2066 write_sequnlock(&state->seqlock);
2067 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2075 * nfs41_check_open_stateid - possibly free an open stateid
2077 * @state: NFSv4 state for an inode
2079 * Returns NFS_OK if recovery for this stateid is now finished.
2080 * Otherwise a negative NFS4ERR value is returned.
2082 static int nfs41_check_open_stateid(struct nfs4_state *state)
2084 struct nfs_server *server = NFS_SERVER(state->inode);
2085 nfs4_stateid *stateid = &state->open_stateid;
2086 struct rpc_cred *cred = state->owner->so_cred;
2089 /* If a state reset has been done, test_stateid is unneeded */
2090 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2091 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2092 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2093 return -NFS4ERR_BAD_STATEID;
2095 status = nfs41_test_stateid(server, stateid, cred);
2096 trace_nfs4_test_open_stateid(state, NULL, status);
2097 if (status != NFS_OK) {
2098 /* Free the stateid unless the server explicitly
2099 * informs us the stateid is unrecognized. */
2100 if (status != -NFS4ERR_BAD_STATEID)
2101 nfs41_free_stateid(server, stateid, cred);
2103 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2104 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2105 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2106 clear_bit(NFS_OPEN_STATE, &state->flags);
2111 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2115 nfs41_clear_delegation_stateid(state);
2116 status = nfs41_check_open_stateid(state);
2117 if (status != NFS_OK)
2118 status = nfs4_open_expired(sp, state);
2124 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2125 * fields corresponding to attributes that were used to store the verifier.
2126 * Make sure we clobber those fields in the later setattr call
2128 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2130 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2131 !(sattr->ia_valid & ATTR_ATIME_SET))
2132 sattr->ia_valid |= ATTR_ATIME;
2134 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2135 !(sattr->ia_valid & ATTR_MTIME_SET))
2136 sattr->ia_valid |= ATTR_MTIME;
2139 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2142 struct nfs_open_context *ctx)
2144 struct nfs4_state_owner *sp = opendata->owner;
2145 struct nfs_server *server = sp->so_server;
2146 struct dentry *dentry;
2147 struct nfs4_state *state;
2151 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2153 ret = _nfs4_proc_open(opendata);
2157 state = nfs4_opendata_to_nfs4_state(opendata);
2158 ret = PTR_ERR(state);
2161 if (server->caps & NFS_CAP_POSIX_LOCK)
2162 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2164 dentry = opendata->dentry;
2165 if (dentry->d_inode == NULL) {
2166 /* FIXME: Is this d_drop() ever needed? */
2168 dentry = d_add_unique(dentry, igrab(state->inode));
2169 if (dentry == NULL) {
2170 dentry = opendata->dentry;
2171 } else if (dentry != ctx->dentry) {
2173 ctx->dentry = dget(dentry);
2175 nfs_set_verifier(dentry,
2176 nfs_save_change_attribute(opendata->dir->d_inode));
2179 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2184 if (dentry->d_inode == state->inode) {
2185 nfs_inode_attach_open_context(ctx);
2186 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2187 nfs4_schedule_stateid_recovery(server, state);
2194 * Returns a referenced nfs4_state
2196 static int _nfs4_do_open(struct inode *dir,
2197 struct nfs_open_context *ctx,
2199 struct iattr *sattr,
2200 struct nfs4_label *label,
2203 struct nfs4_state_owner *sp;
2204 struct nfs4_state *state = NULL;
2205 struct nfs_server *server = NFS_SERVER(dir);
2206 struct nfs4_opendata *opendata;
2207 struct dentry *dentry = ctx->dentry;
2208 struct rpc_cred *cred = ctx->cred;
2209 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2210 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2211 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2212 struct nfs4_label *olabel = NULL;
2215 /* Protect against reboot recovery conflicts */
2217 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2219 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2222 status = nfs4_recover_expired_lease(server);
2224 goto err_put_state_owner;
2225 if (dentry->d_inode != NULL)
2226 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2228 if (dentry->d_inode)
2229 claim = NFS4_OPEN_CLAIM_FH;
2230 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2231 label, claim, GFP_KERNEL);
2232 if (opendata == NULL)
2233 goto err_put_state_owner;
2236 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2237 if (IS_ERR(olabel)) {
2238 status = PTR_ERR(olabel);
2239 goto err_opendata_put;
2243 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2244 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2245 if (!opendata->f_attr.mdsthreshold)
2246 goto err_free_label;
2247 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2249 if (dentry->d_inode != NULL)
2250 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2252 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2254 goto err_free_label;
2257 if ((opendata->o_arg.open_flags & O_EXCL) &&
2258 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2259 nfs4_exclusive_attrset(opendata, sattr);
2261 nfs_fattr_init(opendata->o_res.f_attr);
2262 status = nfs4_do_setattr(state->inode, cred,
2263 opendata->o_res.f_attr, sattr,
2264 state, label, olabel);
2266 nfs_setattr_update_inode(state->inode, sattr);
2267 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2268 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2271 if (opendata->file_created)
2272 *opened |= FILE_CREATED;
2274 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2275 *ctx_th = opendata->f_attr.mdsthreshold;
2277 kfree(opendata->f_attr.mdsthreshold);
2278 opendata->f_attr.mdsthreshold = NULL;
2280 nfs4_label_free(olabel);
2282 nfs4_opendata_put(opendata);
2283 nfs4_put_state_owner(sp);
2286 nfs4_label_free(olabel);
2288 kfree(opendata->f_attr.mdsthreshold);
2289 nfs4_opendata_put(opendata);
2290 err_put_state_owner:
2291 nfs4_put_state_owner(sp);
2297 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2298 struct nfs_open_context *ctx,
2300 struct iattr *sattr,
2301 struct nfs4_label *label,
2304 struct nfs_server *server = NFS_SERVER(dir);
2305 struct nfs4_exception exception = { };
2306 struct nfs4_state *res;
2310 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2312 trace_nfs4_open_file(ctx, flags, status);
2315 /* NOTE: BAD_SEQID means the server and client disagree about the
2316 * book-keeping w.r.t. state-changing operations
2317 * (OPEN/CLOSE/LOCK/LOCKU...)
2318 * It is actually a sign of a bug on the client or on the server.
2320 * If we receive a BAD_SEQID error in the particular case of
2321 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2322 * have unhashed the old state_owner for us, and that we can
2323 * therefore safely retry using a new one. We should still warn
2324 * the user though...
2326 if (status == -NFS4ERR_BAD_SEQID) {
2327 pr_warn_ratelimited("NFS: v4 server %s "
2328 " returned a bad sequence-id error!\n",
2329 NFS_SERVER(dir)->nfs_client->cl_hostname);
2330 exception.retry = 1;
2334 * BAD_STATEID on OPEN means that the server cancelled our
2335 * state before it received the OPEN_CONFIRM.
2336 * Recover by retrying the request as per the discussion
2337 * on Page 181 of RFC3530.
2339 if (status == -NFS4ERR_BAD_STATEID) {
2340 exception.retry = 1;
2343 if (status == -EAGAIN) {
2344 /* We must have found a delegation */
2345 exception.retry = 1;
2348 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2350 res = ERR_PTR(nfs4_handle_exception(server,
2351 status, &exception));
2352 } while (exception.retry);
2356 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2357 struct nfs_fattr *fattr, struct iattr *sattr,
2358 struct nfs4_state *state, struct nfs4_label *ilabel,
2359 struct nfs4_label *olabel)
2361 struct nfs_server *server = NFS_SERVER(inode);
2362 struct nfs_setattrargs arg = {
2363 .fh = NFS_FH(inode),
2366 .bitmask = server->attr_bitmask,
2369 struct nfs_setattrres res = {
2374 struct rpc_message msg = {
2375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2380 unsigned long timestamp = jiffies;
2385 arg.bitmask = nfs4_bitmask(server, ilabel);
2387 arg.bitmask = nfs4_bitmask(server, olabel);
2389 nfs_fattr_init(fattr);
2391 /* Servers should only apply open mode checks for file size changes */
2392 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2393 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2395 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2396 /* Use that stateid */
2397 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
2398 struct nfs_lockowner lockowner = {
2399 .l_owner = current->files,
2400 .l_pid = current->tgid,
2402 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2405 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2407 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2408 if (status == 0 && state != NULL)
2409 renew_lease(server, timestamp);
2413 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2414 struct nfs_fattr *fattr, struct iattr *sattr,
2415 struct nfs4_state *state, struct nfs4_label *ilabel,
2416 struct nfs4_label *olabel)
2418 struct nfs_server *server = NFS_SERVER(inode);
2419 struct nfs4_exception exception = {
2425 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2426 trace_nfs4_setattr(inode, err);
2428 case -NFS4ERR_OPENMODE:
2429 if (!(sattr->ia_valid & ATTR_SIZE)) {
2430 pr_warn_once("NFSv4: server %s is incorrectly "
2431 "applying open mode checks to "
2432 "a SETATTR that is not "
2433 "changing file size.\n",
2434 server->nfs_client->cl_hostname);
2436 if (state && !(state->state & FMODE_WRITE)) {
2438 if (sattr->ia_valid & ATTR_OPEN)
2443 err = nfs4_handle_exception(server, err, &exception);
2444 } while (exception.retry);
2449 struct nfs4_closedata {
2450 struct inode *inode;
2451 struct nfs4_state *state;
2452 struct nfs_closeargs arg;
2453 struct nfs_closeres res;
2454 struct nfs_fattr fattr;
2455 unsigned long timestamp;
2460 static void nfs4_free_closedata(void *data)
2462 struct nfs4_closedata *calldata = data;
2463 struct nfs4_state_owner *sp = calldata->state->owner;
2464 struct super_block *sb = calldata->state->inode->i_sb;
2467 pnfs_roc_release(calldata->state->inode);
2468 nfs4_put_open_state(calldata->state);
2469 nfs_free_seqid(calldata->arg.seqid);
2470 nfs4_put_state_owner(sp);
2471 nfs_sb_deactive(sb);
2475 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2478 spin_lock(&state->owner->so_lock);
2479 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2480 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2482 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2485 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2488 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2489 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2490 clear_bit(NFS_OPEN_STATE, &state->flags);
2492 spin_unlock(&state->owner->so_lock);
2495 static void nfs4_close_done(struct rpc_task *task, void *data)
2497 struct nfs4_closedata *calldata = data;
2498 struct nfs4_state *state = calldata->state;
2499 struct nfs_server *server = NFS_SERVER(calldata->inode);
2501 dprintk("%s: begin!\n", __func__);
2502 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2504 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2505 /* hmm. we are done with the inode, and in the process of freeing
2506 * the state_owner. we keep this around to process errors
2508 switch (task->tk_status) {
2511 pnfs_roc_set_barrier(state->inode,
2512 calldata->roc_barrier);
2513 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2514 renew_lease(server, calldata->timestamp);
2515 nfs4_close_clear_stateid_flags(state,
2516 calldata->arg.fmode);
2518 case -NFS4ERR_STALE_STATEID:
2519 case -NFS4ERR_OLD_STATEID:
2520 case -NFS4ERR_BAD_STATEID:
2521 case -NFS4ERR_EXPIRED:
2522 if (calldata->arg.fmode == 0)
2525 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2526 rpc_restart_call_prepare(task);
2528 nfs_release_seqid(calldata->arg.seqid);
2529 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2530 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2533 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2535 struct nfs4_closedata *calldata = data;
2536 struct nfs4_state *state = calldata->state;
2537 struct inode *inode = calldata->inode;
2540 dprintk("%s: begin!\n", __func__);
2541 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2544 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2545 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2546 spin_lock(&state->owner->so_lock);
2547 /* Calculate the change in open mode */
2548 if (state->n_rdwr == 0) {
2549 if (state->n_rdonly == 0) {
2550 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2551 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2552 calldata->arg.fmode &= ~FMODE_READ;
2554 if (state->n_wronly == 0) {
2555 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2556 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2557 calldata->arg.fmode &= ~FMODE_WRITE;
2560 if (!nfs4_valid_open_stateid(state))
2562 spin_unlock(&state->owner->so_lock);
2565 /* Note: exit _without_ calling nfs4_close_done */
2569 if (calldata->arg.fmode == 0) {
2570 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2571 if (calldata->roc &&
2572 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2573 nfs_release_seqid(calldata->arg.seqid);
2578 nfs_fattr_init(calldata->res.fattr);
2579 calldata->timestamp = jiffies;
2580 if (nfs4_setup_sequence(NFS_SERVER(inode),
2581 &calldata->arg.seq_args,
2582 &calldata->res.seq_res,
2584 nfs_release_seqid(calldata->arg.seqid);
2585 dprintk("%s: done!\n", __func__);
2588 task->tk_action = NULL;
2590 nfs4_sequence_done(task, &calldata->res.seq_res);
2593 static const struct rpc_call_ops nfs4_close_ops = {
2594 .rpc_call_prepare = nfs4_close_prepare,
2595 .rpc_call_done = nfs4_close_done,
2596 .rpc_release = nfs4_free_closedata,
2600 * It is possible for data to be read/written from a mem-mapped file
2601 * after the sys_close call (which hits the vfs layer as a flush).
2602 * This means that we can't safely call nfsv4 close on a file until
2603 * the inode is cleared. This in turn means that we are not good
2604 * NFSv4 citizens - we do not indicate to the server to update the file's
2605 * share state even when we are done with one of the three share
2606 * stateid's in the inode.
2608 * NOTE: Caller must be holding the sp->so_owner semaphore!
2610 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2612 struct nfs_server *server = NFS_SERVER(state->inode);
2613 struct nfs4_closedata *calldata;
2614 struct nfs4_state_owner *sp = state->owner;
2615 struct rpc_task *task;
2616 struct rpc_message msg = {
2617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2618 .rpc_cred = state->owner->so_cred,
2620 struct rpc_task_setup task_setup_data = {
2621 .rpc_client = server->client,
2622 .rpc_message = &msg,
2623 .callback_ops = &nfs4_close_ops,
2624 .workqueue = nfsiod_workqueue,
2625 .flags = RPC_TASK_ASYNC,
2627 int status = -ENOMEM;
2629 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2630 &task_setup_data.rpc_client, &msg);
2632 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2633 if (calldata == NULL)
2635 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2636 calldata->inode = state->inode;
2637 calldata->state = state;
2638 calldata->arg.fh = NFS_FH(state->inode);
2639 calldata->arg.stateid = &state->open_stateid;
2640 /* Serialization for the sequence id */
2641 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2642 if (calldata->arg.seqid == NULL)
2643 goto out_free_calldata;
2644 calldata->arg.fmode = 0;
2645 calldata->arg.bitmask = server->cache_consistency_bitmask;
2646 calldata->res.fattr = &calldata->fattr;
2647 calldata->res.seqid = calldata->arg.seqid;
2648 calldata->res.server = server;
2649 calldata->roc = pnfs_roc(state->inode);
2650 nfs_sb_active(calldata->inode->i_sb);
2652 msg.rpc_argp = &calldata->arg;
2653 msg.rpc_resp = &calldata->res;
2654 task_setup_data.callback_data = calldata;
2655 task = rpc_run_task(&task_setup_data);
2657 return PTR_ERR(task);
2660 status = rpc_wait_for_completion_task(task);
2666 nfs4_put_open_state(state);
2667 nfs4_put_state_owner(sp);
2671 static struct inode *
2672 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2673 int open_flags, struct iattr *attr, int *opened)
2675 struct nfs4_state *state;
2676 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2678 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2680 /* Protect against concurrent sillydeletes */
2681 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2683 nfs4_label_release_security(label);
2686 return ERR_CAST(state);
2687 return state->inode;
2690 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2692 if (ctx->state == NULL)
2695 nfs4_close_sync(ctx->state, ctx->mode);
2697 nfs4_close_state(ctx->state, ctx->mode);
2700 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2702 struct nfs4_server_caps_arg args = {
2705 struct nfs4_server_caps_res res = {};
2706 struct rpc_message msg = {
2707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2713 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2715 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2716 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2717 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2718 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2719 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2720 NFS_CAP_CTIME|NFS_CAP_MTIME);
2721 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2722 server->caps |= NFS_CAP_ACLS;
2723 if (res.has_links != 0)
2724 server->caps |= NFS_CAP_HARDLINKS;
2725 if (res.has_symlinks != 0)
2726 server->caps |= NFS_CAP_SYMLINKS;
2727 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2728 server->caps |= NFS_CAP_FILEID;
2729 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2730 server->caps |= NFS_CAP_MODE;
2731 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2732 server->caps |= NFS_CAP_NLINK;
2733 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2734 server->caps |= NFS_CAP_OWNER;
2735 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2736 server->caps |= NFS_CAP_OWNER_GROUP;
2737 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2738 server->caps |= NFS_CAP_ATIME;
2739 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2740 server->caps |= NFS_CAP_CTIME;
2741 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2742 server->caps |= NFS_CAP_MTIME;
2743 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2744 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2745 server->caps |= NFS_CAP_SECURITY_LABEL;
2747 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2748 sizeof(server->attr_bitmask));
2750 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2751 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2752 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2754 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2755 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2756 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2757 server->acl_bitmask = res.acl_bitmask;
2758 server->fh_expire_type = res.fh_expire_type;
2764 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2766 struct nfs4_exception exception = { };
2769 err = nfs4_handle_exception(server,
2770 _nfs4_server_capabilities(server, fhandle),
2772 } while (exception.retry);
2776 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2777 struct nfs_fsinfo *info)
2780 struct nfs4_lookup_root_arg args = {
2783 struct nfs4_lookup_res res = {
2785 .fattr = info->fattr,
2788 struct rpc_message msg = {
2789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2794 bitmask[0] = nfs4_fattr_bitmap[0];
2795 bitmask[1] = nfs4_fattr_bitmap[1];
2797 * Process the label in the upcoming getfattr
2799 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2801 nfs_fattr_init(info->fattr);
2802 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2805 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2806 struct nfs_fsinfo *info)
2808 struct nfs4_exception exception = { };
2811 err = _nfs4_lookup_root(server, fhandle, info);
2812 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2815 case -NFS4ERR_WRONGSEC:
2818 err = nfs4_handle_exception(server, err, &exception);
2820 } while (exception.retry);
2825 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2826 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2828 struct rpc_auth_create_args auth_args = {
2829 .pseudoflavor = flavor,
2831 struct rpc_auth *auth;
2834 auth = rpcauth_create(&auth_args, server->client);
2839 ret = nfs4_lookup_root(server, fhandle, info);
2845 * Retry pseudoroot lookup with various security flavors. We do this when:
2847 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2848 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2850 * Returns zero on success, or a negative NFS4ERR value, or a
2851 * negative errno value.
2853 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2854 struct nfs_fsinfo *info)
2856 /* Per 3530bis 15.33.5 */
2857 static const rpc_authflavor_t flav_array[] = {
2861 RPC_AUTH_UNIX, /* courtesy */
2864 int status = -EPERM;
2867 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2868 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2869 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2875 * -EACCESS could mean that the user doesn't have correct permissions
2876 * to access the mount. It could also mean that we tried to mount
2877 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2878 * existing mount programs don't handle -EACCES very well so it should
2879 * be mapped to -EPERM instead.
2881 if (status == -EACCES)
2886 static int nfs4_do_find_root_sec(struct nfs_server *server,
2887 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2889 int mv = server->nfs_client->cl_minorversion;
2890 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2894 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2895 * @server: initialized nfs_server handle
2896 * @fhandle: we fill in the pseudo-fs root file handle
2897 * @info: we fill in an FSINFO struct
2898 * @auth_probe: probe the auth flavours
2900 * Returns zero on success, or a negative errno.
2902 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2903 struct nfs_fsinfo *info,
2908 switch (auth_probe) {
2910 status = nfs4_lookup_root(server, fhandle, info);
2911 if (status != -NFS4ERR_WRONGSEC)
2913 /* Did user force a 'sec=' mount option? */
2914 if (server->flags & NFS_MOUNT_SECFLAVOUR)
2917 status = nfs4_do_find_root_sec(server, fhandle, info);
2921 status = nfs4_server_capabilities(server, fhandle);
2923 status = nfs4_do_fsinfo(server, fhandle, info);
2925 return nfs4_map_errors(status);
2928 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2929 struct nfs_fsinfo *info)
2932 struct nfs_fattr *fattr = info->fattr;
2933 struct nfs4_label *label = NULL;
2935 error = nfs4_server_capabilities(server, mntfh);
2937 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2941 label = nfs4_label_alloc(server, GFP_KERNEL);
2943 return PTR_ERR(label);
2945 error = nfs4_proc_getattr(server, mntfh, fattr, label);
2947 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2948 goto err_free_label;
2951 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2952 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2953 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2956 nfs4_label_free(label);
2962 * Get locations and (maybe) other attributes of a referral.
2963 * Note that we'll actually follow the referral later when
2964 * we detect fsid mismatch in inode revalidation
2966 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2967 const struct qstr *name, struct nfs_fattr *fattr,
2968 struct nfs_fh *fhandle)
2970 int status = -ENOMEM;
2971 struct page *page = NULL;
2972 struct nfs4_fs_locations *locations = NULL;
2974 page = alloc_page(GFP_KERNEL);
2977 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2978 if (locations == NULL)
2981 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2986 * If the fsid didn't change, this is a migration event, not a
2987 * referral. Cause us to drop into the exception handler, which
2988 * will kick off migration recovery.
2990 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2991 dprintk("%s: server did not return a different fsid for"
2992 " a referral at %s\n", __func__, name->name);
2993 status = -NFS4ERR_MOVED;
2996 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2997 nfs_fixup_referral_attributes(&locations->fattr);
2999 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3000 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3001 memset(fhandle, 0, sizeof(struct nfs_fh));
3009 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3010 struct nfs_fattr *fattr, struct nfs4_label *label)
3012 struct nfs4_getattr_arg args = {
3014 .bitmask = server->attr_bitmask,
3016 struct nfs4_getattr_res res = {
3021 struct rpc_message msg = {
3022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3027 args.bitmask = nfs4_bitmask(server, label);
3029 nfs_fattr_init(fattr);
3030 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3033 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3034 struct nfs_fattr *fattr, struct nfs4_label *label)
3036 struct nfs4_exception exception = { };
3039 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3040 trace_nfs4_getattr(server, fhandle, fattr, err);
3041 err = nfs4_handle_exception(server, err,
3043 } while (exception.retry);
3048 * The file is not closed if it is opened due to the a request to change
3049 * the size of the file. The open call will not be needed once the
3050 * VFS layer lookup-intents are implemented.
3052 * Close is called when the inode is destroyed.
3053 * If we haven't opened the file for O_WRONLY, we
3054 * need to in the size_change case to obtain a stateid.
3057 * Because OPEN is always done by name in nfsv4, it is
3058 * possible that we opened a different file by the same
3059 * name. We can recognize this race condition, but we
3060 * can't do anything about it besides returning an error.
3062 * This will be fixed with VFS changes (lookup-intent).
3065 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3066 struct iattr *sattr)
3068 struct inode *inode = dentry->d_inode;
3069 struct rpc_cred *cred = NULL;
3070 struct nfs4_state *state = NULL;
3071 struct nfs4_label *label = NULL;
3074 if (pnfs_ld_layoutret_on_setattr(inode))
3075 pnfs_commit_and_return_layout(inode);
3077 nfs_fattr_init(fattr);
3079 /* Deal with open(O_TRUNC) */
3080 if (sattr->ia_valid & ATTR_OPEN)
3081 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3083 /* Optimization: if the end result is no change, don't RPC */
3084 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3087 /* Search for an existing open(O_WRITE) file */
3088 if (sattr->ia_valid & ATTR_FILE) {
3089 struct nfs_open_context *ctx;
3091 ctx = nfs_file_open_context(sattr->ia_file);
3098 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3100 return PTR_ERR(label);
3102 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3104 nfs_setattr_update_inode(inode, sattr);
3105 nfs_setsecurity(inode, fattr, label);
3107 nfs4_label_free(label);
3111 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3112 const struct qstr *name, struct nfs_fh *fhandle,
3113 struct nfs_fattr *fattr, struct nfs4_label *label)
3115 struct nfs_server *server = NFS_SERVER(dir);
3117 struct nfs4_lookup_arg args = {
3118 .bitmask = server->attr_bitmask,
3119 .dir_fh = NFS_FH(dir),
3122 struct nfs4_lookup_res res = {
3128 struct rpc_message msg = {
3129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3134 args.bitmask = nfs4_bitmask(server, label);
3136 nfs_fattr_init(fattr);
3138 dprintk("NFS call lookup %s\n", name->name);
3139 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3140 dprintk("NFS reply lookup: %d\n", status);
3144 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3146 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3147 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3148 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3152 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3153 struct qstr *name, struct nfs_fh *fhandle,
3154 struct nfs_fattr *fattr, struct nfs4_label *label)
3156 struct nfs4_exception exception = { };
3157 struct rpc_clnt *client = *clnt;
3160 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3161 trace_nfs4_lookup(dir, name, err);
3163 case -NFS4ERR_BADNAME:
3166 case -NFS4ERR_MOVED:
3167 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3169 case -NFS4ERR_WRONGSEC:
3171 if (client != *clnt)
3173 /* No security negotiation if the user specified 'sec=' */
3174 if (NFS_SERVER(dir)->flags & NFS_MOUNT_SECFLAVOUR)
3176 client = nfs4_create_sec_client(client, dir, name);
3178 return PTR_ERR(client);
3180 exception.retry = 1;
3183 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3185 } while (exception.retry);
3190 else if (client != *clnt)
3191 rpc_shutdown_client(client);
3196 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3197 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3198 struct nfs4_label *label)
3201 struct rpc_clnt *client = NFS_CLIENT(dir);
3203 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3204 if (client != NFS_CLIENT(dir)) {
3205 rpc_shutdown_client(client);
3206 nfs_fixup_secinfo_attributes(fattr);
3212 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3213 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3215 struct rpc_clnt *client = NFS_CLIENT(dir);
3218 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3220 return ERR_PTR(status);
3221 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3224 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3226 struct nfs_server *server = NFS_SERVER(inode);
3227 struct nfs4_accessargs args = {
3228 .fh = NFS_FH(inode),
3229 .bitmask = server->cache_consistency_bitmask,
3231 struct nfs4_accessres res = {
3234 struct rpc_message msg = {
3235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3238 .rpc_cred = entry->cred,
3240 int mode = entry->mask;
3244 * Determine which access bits we want to ask for...
3246 if (mode & MAY_READ)
3247 args.access |= NFS4_ACCESS_READ;
3248 if (S_ISDIR(inode->i_mode)) {
3249 if (mode & MAY_WRITE)
3250 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3251 if (mode & MAY_EXEC)
3252 args.access |= NFS4_ACCESS_LOOKUP;
3254 if (mode & MAY_WRITE)
3255 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3256 if (mode & MAY_EXEC)
3257 args.access |= NFS4_ACCESS_EXECUTE;
3260 res.fattr = nfs_alloc_fattr();
3261 if (res.fattr == NULL)
3264 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3266 nfs_access_set_mask(entry, res.access);
3267 nfs_refresh_inode(inode, res.fattr);
3269 nfs_free_fattr(res.fattr);
3273 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3275 struct nfs4_exception exception = { };
3278 err = _nfs4_proc_access(inode, entry);
3279 trace_nfs4_access(inode, err);
3280 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3282 } while (exception.retry);
3287 * TODO: For the time being, we don't try to get any attributes
3288 * along with any of the zero-copy operations READ, READDIR,
3291 * In the case of the first three, we want to put the GETATTR
3292 * after the read-type operation -- this is because it is hard
3293 * to predict the length of a GETATTR response in v4, and thus
3294 * align the READ data correctly. This means that the GETATTR
3295 * may end up partially falling into the page cache, and we should
3296 * shift it into the 'tail' of the xdr_buf before processing.
3297 * To do this efficiently, we need to know the total length
3298 * of data received, which doesn't seem to be available outside
3301 * In the case of WRITE, we also want to put the GETATTR after
3302 * the operation -- in this case because we want to make sure
3303 * we get the post-operation mtime and size.
3305 * Both of these changes to the XDR layer would in fact be quite
3306 * minor, but I decided to leave them for a subsequent patch.
3308 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3309 unsigned int pgbase, unsigned int pglen)
3311 struct nfs4_readlink args = {
3312 .fh = NFS_FH(inode),
3317 struct nfs4_readlink_res res;
3318 struct rpc_message msg = {
3319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3324 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3327 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3328 unsigned int pgbase, unsigned int pglen)
3330 struct nfs4_exception exception = { };
3333 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3334 trace_nfs4_readlink(inode, err);
3335 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3337 } while (exception.retry);
3342 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3345 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3348 struct nfs4_label l, *ilabel = NULL;
3349 struct nfs_open_context *ctx;
3350 struct nfs4_state *state;
3354 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3356 return PTR_ERR(ctx);
3358 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3360 sattr->ia_mode &= ~current_umask();
3361 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3362 if (IS_ERR(state)) {
3363 status = PTR_ERR(state);
3367 nfs4_label_release_security(ilabel);
3368 put_nfs_open_context(ctx);
3372 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3374 struct nfs_server *server = NFS_SERVER(dir);
3375 struct nfs_removeargs args = {
3379 struct nfs_removeres res = {
3382 struct rpc_message msg = {
3383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3389 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3391 update_changeattr(dir, &res.cinfo);
3395 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3397 struct nfs4_exception exception = { };
3400 err = _nfs4_proc_remove(dir, name);
3401 trace_nfs4_remove(dir, name, err);
3402 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3404 } while (exception.retry);
3408 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3410 struct nfs_server *server = NFS_SERVER(dir);
3411 struct nfs_removeargs *args = msg->rpc_argp;
3412 struct nfs_removeres *res = msg->rpc_resp;
3414 res->server = server;
3415 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3416 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3418 nfs_fattr_init(res->dir_attr);
3421 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3423 nfs4_setup_sequence(NFS_SERVER(data->dir),
3424 &data->args.seq_args,
3429 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3431 struct nfs_unlinkdata *data = task->tk_calldata;
3432 struct nfs_removeres *res = &data->res;
3434 if (!nfs4_sequence_done(task, &res->seq_res))
3436 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3438 update_changeattr(dir, &res->cinfo);
3442 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3444 struct nfs_server *server = NFS_SERVER(dir);
3445 struct nfs_renameargs *arg = msg->rpc_argp;
3446 struct nfs_renameres *res = msg->rpc_resp;
3448 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3449 res->server = server;
3450 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3453 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3455 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3456 &data->args.seq_args,
3461 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3462 struct inode *new_dir)
3464 struct nfs_renamedata *data = task->tk_calldata;
3465 struct nfs_renameres *res = &data->res;
3467 if (!nfs4_sequence_done(task, &res->seq_res))
3469 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3472 update_changeattr(old_dir, &res->old_cinfo);
3473 update_changeattr(new_dir, &res->new_cinfo);
3477 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3478 struct inode *new_dir, struct qstr *new_name)
3480 struct nfs_server *server = NFS_SERVER(old_dir);
3481 struct nfs_renameargs arg = {
3482 .old_dir = NFS_FH(old_dir),
3483 .new_dir = NFS_FH(new_dir),
3484 .old_name = old_name,
3485 .new_name = new_name,
3487 struct nfs_renameres res = {
3490 struct rpc_message msg = {
3491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3495 int status = -ENOMEM;
3497 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3499 update_changeattr(old_dir, &res.old_cinfo);
3500 update_changeattr(new_dir, &res.new_cinfo);
3505 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3506 struct inode *new_dir, struct qstr *new_name)
3508 struct nfs4_exception exception = { };
3511 err = _nfs4_proc_rename(old_dir, old_name,
3513 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3514 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
3516 } while (exception.retry);
3520 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3522 struct nfs_server *server = NFS_SERVER(inode);
3523 struct nfs4_link_arg arg = {
3524 .fh = NFS_FH(inode),
3525 .dir_fh = NFS_FH(dir),
3527 .bitmask = server->attr_bitmask,
3529 struct nfs4_link_res res = {
3533 struct rpc_message msg = {
3534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3538 int status = -ENOMEM;
3540 res.fattr = nfs_alloc_fattr();
3541 if (res.fattr == NULL)
3544 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3545 if (IS_ERR(res.label)) {
3546 status = PTR_ERR(res.label);
3549 arg.bitmask = nfs4_bitmask(server, res.label);
3551 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3553 update_changeattr(dir, &res.cinfo);
3554 status = nfs_post_op_update_inode(inode, res.fattr);
3556 nfs_setsecurity(inode, res.fattr, res.label);
3560 nfs4_label_free(res.label);
3563 nfs_free_fattr(res.fattr);
3567 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3569 struct nfs4_exception exception = { };
3572 err = nfs4_handle_exception(NFS_SERVER(inode),
3573 _nfs4_proc_link(inode, dir, name),
3575 } while (exception.retry);
3579 struct nfs4_createdata {
3580 struct rpc_message msg;
3581 struct nfs4_create_arg arg;
3582 struct nfs4_create_res res;
3584 struct nfs_fattr fattr;
3585 struct nfs4_label *label;
3588 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3589 struct qstr *name, struct iattr *sattr, u32 ftype)
3591 struct nfs4_createdata *data;
3593 data = kzalloc(sizeof(*data), GFP_KERNEL);
3595 struct nfs_server *server = NFS_SERVER(dir);
3597 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3598 if (IS_ERR(data->label))
3601 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3602 data->msg.rpc_argp = &data->arg;
3603 data->msg.rpc_resp = &data->res;
3604 data->arg.dir_fh = NFS_FH(dir);
3605 data->arg.server = server;
3606 data->arg.name = name;
3607 data->arg.attrs = sattr;
3608 data->arg.ftype = ftype;
3609 data->arg.bitmask = nfs4_bitmask(server, data->label);
3610 data->res.server = server;
3611 data->res.fh = &data->fh;
3612 data->res.fattr = &data->fattr;
3613 data->res.label = data->label;
3614 nfs_fattr_init(data->res.fattr);
3622 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3624 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3625 &data->arg.seq_args, &data->res.seq_res, 1);
3627 update_changeattr(dir, &data->res.dir_cinfo);
3628 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3633 static void nfs4_free_createdata(struct nfs4_createdata *data)
3635 nfs4_label_free(data->label);
3639 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3640 struct page *page, unsigned int len, struct iattr *sattr,
3641 struct nfs4_label *label)
3643 struct nfs4_createdata *data;
3644 int status = -ENAMETOOLONG;
3646 if (len > NFS4_MAXPATHLEN)
3650 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3654 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3655 data->arg.u.symlink.pages = &page;
3656 data->arg.u.symlink.len = len;
3657 data->arg.label = label;
3659 status = nfs4_do_create(dir, dentry, data);
3661 nfs4_free_createdata(data);
3666 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3667 struct page *page, unsigned int len, struct iattr *sattr)
3669 struct nfs4_exception exception = { };
3670 struct nfs4_label l, *label = NULL;
3673 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3676 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3677 trace_nfs4_symlink(dir, &dentry->d_name, err);
3678 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3680 } while (exception.retry);
3682 nfs4_label_release_security(label);
3686 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3687 struct iattr *sattr, struct nfs4_label *label)
3689 struct nfs4_createdata *data;
3690 int status = -ENOMEM;
3692 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3696 data->arg.label = label;
3697 status = nfs4_do_create(dir, dentry, data);
3699 nfs4_free_createdata(data);
3704 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3705 struct iattr *sattr)
3707 struct nfs4_exception exception = { };
3708 struct nfs4_label l, *label = NULL;
3711 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3713 sattr->ia_mode &= ~current_umask();
3715 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3716 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3717 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3719 } while (exception.retry);
3720 nfs4_label_release_security(label);
3725 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3726 u64 cookie, struct page **pages, unsigned int count, int plus)
3728 struct inode *dir = dentry->d_inode;
3729 struct nfs4_readdir_arg args = {
3734 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3737 struct nfs4_readdir_res res;
3738 struct rpc_message msg = {
3739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3746 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3747 dentry->d_parent->d_name.name,
3748 dentry->d_name.name,
3749 (unsigned long long)cookie);
3750 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3751 res.pgbase = args.pgbase;
3752 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3754 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3755 status += args.pgbase;
3758 nfs_invalidate_atime(dir);
3760 dprintk("%s: returns %d\n", __func__, status);
3764 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3765 u64 cookie, struct page **pages, unsigned int count, int plus)
3767 struct nfs4_exception exception = { };
3770 err = _nfs4_proc_readdir(dentry, cred, cookie,
3771 pages, count, plus);
3772 trace_nfs4_readdir(dentry->d_inode, err);
3773 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3775 } while (exception.retry);
3779 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3780 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3782 struct nfs4_createdata *data;
3783 int mode = sattr->ia_mode;
3784 int status = -ENOMEM;
3786 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3791 data->arg.ftype = NF4FIFO;
3792 else if (S_ISBLK(mode)) {
3793 data->arg.ftype = NF4BLK;
3794 data->arg.u.device.specdata1 = MAJOR(rdev);
3795 data->arg.u.device.specdata2 = MINOR(rdev);
3797 else if (S_ISCHR(mode)) {
3798 data->arg.ftype = NF4CHR;
3799 data->arg.u.device.specdata1 = MAJOR(rdev);
3800 data->arg.u.device.specdata2 = MINOR(rdev);
3801 } else if (!S_ISSOCK(mode)) {
3806 data->arg.label = label;
3807 status = nfs4_do_create(dir, dentry, data);
3809 nfs4_free_createdata(data);
3814 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3815 struct iattr *sattr, dev_t rdev)
3817 struct nfs4_exception exception = { };
3818 struct nfs4_label l, *label = NULL;
3821 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3823 sattr->ia_mode &= ~current_umask();
3825 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3826 trace_nfs4_mknod(dir, &dentry->d_name, err);
3827 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3829 } while (exception.retry);
3831 nfs4_label_release_security(label);
3836 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3837 struct nfs_fsstat *fsstat)
3839 struct nfs4_statfs_arg args = {
3841 .bitmask = server->attr_bitmask,
3843 struct nfs4_statfs_res res = {
3846 struct rpc_message msg = {
3847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3852 nfs_fattr_init(fsstat->fattr);
3853 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3856 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3858 struct nfs4_exception exception = { };
3861 err = nfs4_handle_exception(server,
3862 _nfs4_proc_statfs(server, fhandle, fsstat),
3864 } while (exception.retry);
3868 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3869 struct nfs_fsinfo *fsinfo)
3871 struct nfs4_fsinfo_arg args = {
3873 .bitmask = server->attr_bitmask,
3875 struct nfs4_fsinfo_res res = {
3878 struct rpc_message msg = {
3879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3884 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3887 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3889 struct nfs4_exception exception = { };
3890 unsigned long now = jiffies;
3894 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3895 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
3897 struct nfs_client *clp = server->nfs_client;
3899 spin_lock(&clp->cl_lock);
3900 clp->cl_lease_time = fsinfo->lease_time * HZ;
3901 clp->cl_last_renewal = now;
3902 spin_unlock(&clp->cl_lock);
3905 err = nfs4_handle_exception(server, err, &exception);
3906 } while (exception.retry);
3910 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3914 nfs_fattr_init(fsinfo->fattr);
3915 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3917 /* block layout checks this! */
3918 server->pnfs_blksize = fsinfo->blksize;
3919 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3925 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3926 struct nfs_pathconf *pathconf)
3928 struct nfs4_pathconf_arg args = {
3930 .bitmask = server->attr_bitmask,
3932 struct nfs4_pathconf_res res = {
3933 .pathconf = pathconf,
3935 struct rpc_message msg = {
3936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3941 /* None of the pathconf attributes are mandatory to implement */
3942 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3943 memset(pathconf, 0, sizeof(*pathconf));
3947 nfs_fattr_init(pathconf->fattr);
3948 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3951 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3952 struct nfs_pathconf *pathconf)
3954 struct nfs4_exception exception = { };
3958 err = nfs4_handle_exception(server,
3959 _nfs4_proc_pathconf(server, fhandle, pathconf),
3961 } while (exception.retry);
3965 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
3966 const struct nfs_open_context *ctx,
3967 const struct nfs_lock_context *l_ctx,
3970 const struct nfs_lockowner *lockowner = NULL;
3973 lockowner = &l_ctx->lockowner;
3974 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
3976 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3978 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3979 const struct nfs_open_context *ctx,
3980 const struct nfs_lock_context *l_ctx,
3983 nfs4_stateid current_stateid;
3985 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode))
3987 return nfs4_stateid_match(stateid, ¤t_stateid);
3990 static bool nfs4_error_stateid_expired(int err)
3993 case -NFS4ERR_DELEG_REVOKED:
3994 case -NFS4ERR_ADMIN_REVOKED:
3995 case -NFS4ERR_BAD_STATEID:
3996 case -NFS4ERR_STALE_STATEID:
3997 case -NFS4ERR_OLD_STATEID:
3998 case -NFS4ERR_OPENMODE:
3999 case -NFS4ERR_EXPIRED:
4005 void __nfs4_read_done_cb(struct nfs_read_data *data)
4007 nfs_invalidate_atime(data->header->inode);
4010 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
4012 struct nfs_server *server = NFS_SERVER(data->header->inode);
4014 trace_nfs4_read(data, task->tk_status);
4015 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
4016 rpc_restart_call_prepare(task);
4020 __nfs4_read_done_cb(data);
4021 if (task->tk_status > 0)
4022 renew_lease(server, data->timestamp);
4026 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4027 struct nfs_readargs *args)
4030 if (!nfs4_error_stateid_expired(task->tk_status) ||
4031 nfs4_stateid_is_current(&args->stateid,
4036 rpc_restart_call_prepare(task);
4040 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4043 dprintk("--> %s\n", __func__);
4045 if (!nfs4_sequence_done(task, &data->res.seq_res))
4047 if (nfs4_read_stateid_changed(task, &data->args))
4049 return data->read_done_cb ? data->read_done_cb(task, data) :
4050 nfs4_read_done_cb(task, data);
4053 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
4055 data->timestamp = jiffies;
4056 data->read_done_cb = nfs4_read_done_cb;
4057 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4058 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
4061 static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
4063 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4064 &data->args.seq_args,
4068 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4069 data->args.lock_context, FMODE_READ) == -EIO)
4071 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4076 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
4078 struct inode *inode = data->header->inode;
4080 trace_nfs4_write(data, task->tk_status);
4081 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
4082 rpc_restart_call_prepare(task);
4085 if (task->tk_status >= 0) {
4086 renew_lease(NFS_SERVER(inode), data->timestamp);
4087 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4092 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4093 struct nfs_writeargs *args)
4096 if (!nfs4_error_stateid_expired(task->tk_status) ||
4097 nfs4_stateid_is_current(&args->stateid,
4102 rpc_restart_call_prepare(task);
4106 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4108 if (!nfs4_sequence_done(task, &data->res.seq_res))
4110 if (nfs4_write_stateid_changed(task, &data->args))
4112 return data->write_done_cb ? data->write_done_cb(task, data) :
4113 nfs4_write_done_cb(task, data);
4117 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
4119 const struct nfs_pgio_header *hdr = data->header;
4121 /* Don't request attributes for pNFS or O_DIRECT writes */
4122 if (data->ds_clp != NULL || hdr->dreq != NULL)
4124 /* Otherwise, request attributes if and only if we don't hold
4127 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4130 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
4132 struct nfs_server *server = NFS_SERVER(data->header->inode);
4134 if (!nfs4_write_need_cache_consistency_data(data)) {
4135 data->args.bitmask = NULL;
4136 data->res.fattr = NULL;
4138 data->args.bitmask = server->cache_consistency_bitmask;
4140 if (!data->write_done_cb)
4141 data->write_done_cb = nfs4_write_done_cb;
4142 data->res.server = server;
4143 data->timestamp = jiffies;
4145 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4146 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4149 static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4151 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4152 &data->args.seq_args,
4156 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4157 data->args.lock_context, FMODE_WRITE) == -EIO)
4159 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4164 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4166 nfs4_setup_sequence(NFS_SERVER(data->inode),
4167 &data->args.seq_args,
4172 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4174 struct inode *inode = data->inode;
4176 trace_nfs4_commit(data, task->tk_status);
4177 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
4178 rpc_restart_call_prepare(task);
4184 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4186 if (!nfs4_sequence_done(task, &data->res.seq_res))
4188 return data->commit_done_cb(task, data);
4191 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4193 struct nfs_server *server = NFS_SERVER(data->inode);
4195 if (data->commit_done_cb == NULL)
4196 data->commit_done_cb = nfs4_commit_done_cb;
4197 data->res.server = server;
4198 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4199 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4202 struct nfs4_renewdata {
4203 struct nfs_client *client;
4204 unsigned long timestamp;
4208 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4209 * standalone procedure for queueing an asynchronous RENEW.
4211 static void nfs4_renew_release(void *calldata)
4213 struct nfs4_renewdata *data = calldata;
4214 struct nfs_client *clp = data->client;
4216 if (atomic_read(&clp->cl_count) > 1)
4217 nfs4_schedule_state_renewal(clp);
4218 nfs_put_client(clp);
4222 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4224 struct nfs4_renewdata *data = calldata;
4225 struct nfs_client *clp = data->client;
4226 unsigned long timestamp = data->timestamp;
4228 trace_nfs4_renew_async(clp, task->tk_status);
4229 if (task->tk_status < 0) {
4230 /* Unless we're shutting down, schedule state recovery! */
4231 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4233 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4234 nfs4_schedule_lease_recovery(clp);
4237 nfs4_schedule_path_down_recovery(clp);
4239 do_renew_lease(clp, timestamp);
4242 static const struct rpc_call_ops nfs4_renew_ops = {
4243 .rpc_call_done = nfs4_renew_done,
4244 .rpc_release = nfs4_renew_release,
4247 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4249 struct rpc_message msg = {
4250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4254 struct nfs4_renewdata *data;
4256 if (renew_flags == 0)
4258 if (!atomic_inc_not_zero(&clp->cl_count))
4260 data = kmalloc(sizeof(*data), GFP_NOFS);
4264 data->timestamp = jiffies;
4265 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4266 &nfs4_renew_ops, data);
4269 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4276 unsigned long now = jiffies;
4279 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4282 do_renew_lease(clp, now);
4286 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4288 return (server->caps & NFS_CAP_ACLS)
4289 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4290 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4293 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4294 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4297 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4299 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4300 struct page **pages, unsigned int *pgbase)
4302 struct page *newpage, **spages;
4308 len = min_t(size_t, PAGE_SIZE, buflen);
4309 newpage = alloc_page(GFP_KERNEL);
4311 if (newpage == NULL)
4313 memcpy(page_address(newpage), buf, len);
4318 } while (buflen != 0);
4324 __free_page(spages[rc-1]);
4328 struct nfs4_cached_acl {
4334 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4336 struct nfs_inode *nfsi = NFS_I(inode);
4338 spin_lock(&inode->i_lock);
4339 kfree(nfsi->nfs4_acl);
4340 nfsi->nfs4_acl = acl;
4341 spin_unlock(&inode->i_lock);
4344 static void nfs4_zap_acl_attr(struct inode *inode)
4346 nfs4_set_cached_acl(inode, NULL);
4349 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4351 struct nfs_inode *nfsi = NFS_I(inode);
4352 struct nfs4_cached_acl *acl;
4355 spin_lock(&inode->i_lock);
4356 acl = nfsi->nfs4_acl;
4359 if (buf == NULL) /* user is just asking for length */
4361 if (acl->cached == 0)
4363 ret = -ERANGE; /* see getxattr(2) man page */
4364 if (acl->len > buflen)
4366 memcpy(buf, acl->data, acl->len);
4370 spin_unlock(&inode->i_lock);
4374 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4376 struct nfs4_cached_acl *acl;
4377 size_t buflen = sizeof(*acl) + acl_len;
4379 if (buflen <= PAGE_SIZE) {
4380 acl = kmalloc(buflen, GFP_KERNEL);
4384 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4386 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4393 nfs4_set_cached_acl(inode, acl);
4397 * The getxattr API returns the required buffer length when called with a
4398 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4399 * the required buf. On a NULL buf, we send a page of data to the server
4400 * guessing that the ACL request can be serviced by a page. If so, we cache
4401 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4402 * the cache. If not so, we throw away the page, and cache the required
4403 * length. The next getxattr call will then produce another round trip to
4404 * the server, this time with the input buf of the required size.
4406 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4408 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4409 struct nfs_getaclargs args = {
4410 .fh = NFS_FH(inode),
4414 struct nfs_getaclres res = {
4417 struct rpc_message msg = {
4418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4422 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4423 int ret = -ENOMEM, i;
4425 /* As long as we're doing a round trip to the server anyway,
4426 * let's be prepared for a page of acl data. */
4429 if (npages > ARRAY_SIZE(pages))
4432 for (i = 0; i < npages; i++) {
4433 pages[i] = alloc_page(GFP_KERNEL);
4438 /* for decoding across pages */
4439 res.acl_scratch = alloc_page(GFP_KERNEL);
4440 if (!res.acl_scratch)
4443 args.acl_len = npages * PAGE_SIZE;
4444 args.acl_pgbase = 0;
4446 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4447 __func__, buf, buflen, npages, args.acl_len);
4448 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4449 &msg, &args.seq_args, &res.seq_res, 0);
4453 /* Handle the case where the passed-in buffer is too short */
4454 if (res.acl_flags & NFS4_ACL_TRUNC) {
4455 /* Did the user only issue a request for the acl length? */
4461 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4463 if (res.acl_len > buflen) {
4467 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4472 for (i = 0; i < npages; i++)
4474 __free_page(pages[i]);
4475 if (res.acl_scratch)
4476 __free_page(res.acl_scratch);
4480 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4482 struct nfs4_exception exception = { };
4485 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4486 trace_nfs4_get_acl(inode, ret);
4489 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4490 } while (exception.retry);
4494 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4496 struct nfs_server *server = NFS_SERVER(inode);
4499 if (!nfs4_server_supports_acls(server))
4501 ret = nfs_revalidate_inode(server, inode);
4504 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4505 nfs_zap_acl_cache(inode);
4506 ret = nfs4_read_cached_acl(inode, buf, buflen);
4508 /* -ENOENT is returned if there is no ACL or if there is an ACL
4509 * but no cached acl data, just the acl length */
4511 return nfs4_get_acl_uncached(inode, buf, buflen);
4514 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4516 struct nfs_server *server = NFS_SERVER(inode);
4517 struct page *pages[NFS4ACL_MAXPAGES];
4518 struct nfs_setaclargs arg = {
4519 .fh = NFS_FH(inode),
4523 struct nfs_setaclres res;
4524 struct rpc_message msg = {
4525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4529 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4532 if (!nfs4_server_supports_acls(server))
4534 if (npages > ARRAY_SIZE(pages))
4536 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4539 nfs4_inode_return_delegation(inode);
4540 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4543 * Free each page after tx, so the only ref left is
4544 * held by the network stack
4547 put_page(pages[i-1]);
4550 * Acl update can result in inode attribute update.
4551 * so mark the attribute cache invalid.
4553 spin_lock(&inode->i_lock);
4554 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4555 spin_unlock(&inode->i_lock);
4556 nfs_access_zap_cache(inode);
4557 nfs_zap_acl_cache(inode);
4561 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4563 struct nfs4_exception exception = { };
4566 err = __nfs4_proc_set_acl(inode, buf, buflen);
4567 trace_nfs4_set_acl(inode, err);
4568 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4570 } while (exception.retry);
4574 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4575 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4578 struct nfs_server *server = NFS_SERVER(inode);
4579 struct nfs_fattr fattr;
4580 struct nfs4_label label = {0, 0, buflen, buf};
4582 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4583 struct nfs4_getattr_arg args = {
4584 .fh = NFS_FH(inode),
4587 struct nfs4_getattr_res res = {
4592 struct rpc_message msg = {
4593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4599 nfs_fattr_init(&fattr);
4601 ret = rpc_call_sync(server->client, &msg, 0);
4604 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4606 if (buflen < label.len)
4611 static int nfs4_get_security_label(struct inode *inode, void *buf,
4614 struct nfs4_exception exception = { };
4617 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4621 err = _nfs4_get_security_label(inode, buf, buflen);
4622 trace_nfs4_get_security_label(inode, err);
4623 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4625 } while (exception.retry);
4629 static int _nfs4_do_set_security_label(struct inode *inode,
4630 struct nfs4_label *ilabel,
4631 struct nfs_fattr *fattr,
4632 struct nfs4_label *olabel)
4635 struct iattr sattr = {0};
4636 struct nfs_server *server = NFS_SERVER(inode);
4637 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4638 struct nfs_setattrargs args = {
4639 .fh = NFS_FH(inode),
4645 struct nfs_setattrres res = {
4650 struct rpc_message msg = {
4651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4657 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4659 status = rpc_call_sync(server->client, &msg, 0);
4661 dprintk("%s failed: %d\n", __func__, status);
4666 static int nfs4_do_set_security_label(struct inode *inode,
4667 struct nfs4_label *ilabel,
4668 struct nfs_fattr *fattr,
4669 struct nfs4_label *olabel)
4671 struct nfs4_exception exception = { };
4675 err = _nfs4_do_set_security_label(inode, ilabel,
4677 trace_nfs4_set_security_label(inode, err);
4678 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4680 } while (exception.retry);
4685 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4687 struct nfs4_label ilabel, *olabel = NULL;
4688 struct nfs_fattr fattr;
4689 struct rpc_cred *cred;
4690 struct inode *inode = dentry->d_inode;
4693 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4696 nfs_fattr_init(&fattr);
4700 ilabel.label = (char *)buf;
4701 ilabel.len = buflen;
4703 cred = rpc_lookup_cred();
4705 return PTR_ERR(cred);
4707 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4708 if (IS_ERR(olabel)) {
4709 status = -PTR_ERR(olabel);
4713 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4715 nfs_setsecurity(inode, &fattr, olabel);
4717 nfs4_label_free(olabel);
4722 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4726 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4728 struct nfs_client *clp = server->nfs_client;
4730 if (task->tk_status >= 0)
4732 switch(task->tk_status) {
4733 case -NFS4ERR_DELEG_REVOKED:
4734 case -NFS4ERR_ADMIN_REVOKED:
4735 case -NFS4ERR_BAD_STATEID:
4738 nfs_remove_bad_delegation(state->inode);
4739 case -NFS4ERR_OPENMODE:
4742 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4743 goto recovery_failed;
4744 goto wait_on_recovery;
4745 case -NFS4ERR_EXPIRED:
4746 if (state != NULL) {
4747 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4748 goto recovery_failed;
4750 case -NFS4ERR_STALE_STATEID:
4751 case -NFS4ERR_STALE_CLIENTID:
4752 nfs4_schedule_lease_recovery(clp);
4753 goto wait_on_recovery;
4754 case -NFS4ERR_MOVED:
4755 if (nfs4_schedule_migration_recovery(server) < 0)
4756 goto recovery_failed;
4757 goto wait_on_recovery;
4758 #if defined(CONFIG_NFS_V4_1)
4759 case -NFS4ERR_BADSESSION:
4760 case -NFS4ERR_BADSLOT:
4761 case -NFS4ERR_BAD_HIGH_SLOT:
4762 case -NFS4ERR_DEADSESSION:
4763 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4764 case -NFS4ERR_SEQ_FALSE_RETRY:
4765 case -NFS4ERR_SEQ_MISORDERED:
4766 dprintk("%s ERROR %d, Reset session\n", __func__,
4768 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4770 #endif /* CONFIG_NFS_V4_1 */
4771 case -NFS4ERR_DELAY:
4772 nfs_inc_server_stats(server, NFSIOS_DELAY);
4773 case -NFS4ERR_GRACE:
4774 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4775 case -NFS4ERR_RETRY_UNCACHED_REP:
4776 case -NFS4ERR_OLD_STATEID:
4779 task->tk_status = nfs4_map_errors(task->tk_status);
4782 task->tk_status = -EIO;
4785 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4786 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4787 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4788 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4789 goto recovery_failed;
4791 task->tk_status = 0;
4795 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4796 nfs4_verifier *bootverf)
4800 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4801 /* An impossible timestamp guarantees this value
4802 * will never match a generated boot time. */
4804 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4806 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4807 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4808 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4810 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4814 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4815 char *buf, size_t len)
4817 unsigned int result;
4820 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4822 rpc_peeraddr2str(clp->cl_rpcclient,
4824 rpc_peeraddr2str(clp->cl_rpcclient,
4825 RPC_DISPLAY_PROTO));
4831 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4832 char *buf, size_t len)
4834 const char *nodename = clp->cl_rpcclient->cl_nodename;
4836 if (nfs4_client_id_uniquifier[0] != '\0')
4837 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4838 clp->rpc_ops->version,
4839 clp->cl_minorversion,
4840 nfs4_client_id_uniquifier,
4842 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4843 clp->rpc_ops->version, clp->cl_minorversion,
4848 * nfs4_proc_setclientid - Negotiate client ID
4849 * @clp: state data structure
4850 * @program: RPC program for NFSv4 callback service
4851 * @port: IP port number for NFS4 callback service
4852 * @cred: RPC credential to use for this call
4853 * @res: where to place the result
4855 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4857 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4858 unsigned short port, struct rpc_cred *cred,
4859 struct nfs4_setclientid_res *res)
4861 nfs4_verifier sc_verifier;
4862 struct nfs4_setclientid setclientid = {
4863 .sc_verifier = &sc_verifier,
4865 .sc_cb_ident = clp->cl_cb_ident,
4867 struct rpc_message msg = {
4868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4869 .rpc_argp = &setclientid,
4875 /* nfs_client_id4 */
4876 nfs4_init_boot_verifier(clp, &sc_verifier);
4877 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4878 setclientid.sc_name_len =
4879 nfs4_init_uniform_client_string(clp,
4880 setclientid.sc_name,
4881 sizeof(setclientid.sc_name));
4883 setclientid.sc_name_len =
4884 nfs4_init_nonuniform_client_string(clp,
4885 setclientid.sc_name,
4886 sizeof(setclientid.sc_name));
4889 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4890 sizeof(setclientid.sc_netid), "%s",
4891 rpc_peeraddr2str(clp->cl_rpcclient,
4892 RPC_DISPLAY_NETID));
4894 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4895 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4896 clp->cl_ipaddr, port >> 8, port & 255);
4898 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4899 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4900 setclientid.sc_name_len, setclientid.sc_name);
4901 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4902 trace_nfs4_setclientid(clp, status);
4903 dprintk("NFS reply setclientid: %d\n", status);
4908 * nfs4_proc_setclientid_confirm - Confirm client ID
4909 * @clp: state data structure
4910 * @res: result of a previous SETCLIENTID
4911 * @cred: RPC credential to use for this call
4913 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4915 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4916 struct nfs4_setclientid_res *arg,
4917 struct rpc_cred *cred)
4919 struct rpc_message msg = {
4920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4926 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4927 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4929 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4930 trace_nfs4_setclientid_confirm(clp, status);
4931 dprintk("NFS reply setclientid_confirm: %d\n", status);
4935 struct nfs4_delegreturndata {
4936 struct nfs4_delegreturnargs args;
4937 struct nfs4_delegreturnres res;
4939 nfs4_stateid stateid;
4940 unsigned long timestamp;
4941 struct nfs_fattr fattr;
4945 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4947 struct nfs4_delegreturndata *data = calldata;
4949 if (!nfs4_sequence_done(task, &data->res.seq_res))
4952 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
4953 switch (task->tk_status) {
4954 case -NFS4ERR_STALE_STATEID:
4955 case -NFS4ERR_EXPIRED:
4957 renew_lease(data->res.server, data->timestamp);
4960 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4962 rpc_restart_call_prepare(task);
4966 data->rpc_status = task->tk_status;
4969 static void nfs4_delegreturn_release(void *calldata)
4974 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4976 struct nfs4_delegreturndata *d_data;
4978 d_data = (struct nfs4_delegreturndata *)data;
4980 nfs4_setup_sequence(d_data->res.server,
4981 &d_data->args.seq_args,
4982 &d_data->res.seq_res,
4986 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4987 .rpc_call_prepare = nfs4_delegreturn_prepare,
4988 .rpc_call_done = nfs4_delegreturn_done,
4989 .rpc_release = nfs4_delegreturn_release,
4992 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4994 struct nfs4_delegreturndata *data;
4995 struct nfs_server *server = NFS_SERVER(inode);
4996 struct rpc_task *task;
4997 struct rpc_message msg = {
4998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5001 struct rpc_task_setup task_setup_data = {
5002 .rpc_client = server->client,
5003 .rpc_message = &msg,
5004 .callback_ops = &nfs4_delegreturn_ops,
5005 .flags = RPC_TASK_ASYNC,
5009 data = kzalloc(sizeof(*data), GFP_NOFS);
5012 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5013 data->args.fhandle = &data->fh;
5014 data->args.stateid = &data->stateid;
5015 data->args.bitmask = server->cache_consistency_bitmask;
5016 nfs_copy_fh(&data->fh, NFS_FH(inode));
5017 nfs4_stateid_copy(&data->stateid, stateid);
5018 data->res.fattr = &data->fattr;
5019 data->res.server = server;
5020 nfs_fattr_init(data->res.fattr);
5021 data->timestamp = jiffies;
5022 data->rpc_status = 0;
5024 task_setup_data.callback_data = data;
5025 msg.rpc_argp = &data->args;
5026 msg.rpc_resp = &data->res;
5027 task = rpc_run_task(&task_setup_data);
5029 return PTR_ERR(task);
5032 status = nfs4_wait_for_completion_rpc_task(task);
5035 status = data->rpc_status;
5037 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5039 nfs_refresh_inode(inode, &data->fattr);
5045 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5047 struct nfs_server *server = NFS_SERVER(inode);
5048 struct nfs4_exception exception = { };
5051 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5052 trace_nfs4_delegreturn(inode, err);
5054 case -NFS4ERR_STALE_STATEID:
5055 case -NFS4ERR_EXPIRED:
5059 err = nfs4_handle_exception(server, err, &exception);
5060 } while (exception.retry);
5064 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5065 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5068 * sleep, with exponential backoff, and retry the LOCK operation.
5070 static unsigned long
5071 nfs4_set_lock_task_retry(unsigned long timeout)
5073 freezable_schedule_timeout_killable_unsafe(timeout);
5075 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5076 return NFS4_LOCK_MAXTIMEOUT;
5080 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5082 struct inode *inode = state->inode;
5083 struct nfs_server *server = NFS_SERVER(inode);
5084 struct nfs_client *clp = server->nfs_client;
5085 struct nfs_lockt_args arg = {
5086 .fh = NFS_FH(inode),
5089 struct nfs_lockt_res res = {
5092 struct rpc_message msg = {
5093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5096 .rpc_cred = state->owner->so_cred,
5098 struct nfs4_lock_state *lsp;
5101 arg.lock_owner.clientid = clp->cl_clientid;
5102 status = nfs4_set_lock_state(state, request);
5105 lsp = request->fl_u.nfs4_fl.owner;
5106 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5107 arg.lock_owner.s_dev = server->s_dev;
5108 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5111 request->fl_type = F_UNLCK;
5113 case -NFS4ERR_DENIED:
5116 request->fl_ops->fl_release_private(request);
5117 request->fl_ops = NULL;
5122 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5124 struct nfs4_exception exception = { };
5128 err = _nfs4_proc_getlk(state, cmd, request);
5129 trace_nfs4_get_lock(request, state, cmd, err);
5130 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5132 } while (exception.retry);
5136 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5139 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5141 res = posix_lock_file_wait(file, fl);
5144 res = flock_lock_file_wait(file, fl);
5152 struct nfs4_unlockdata {
5153 struct nfs_locku_args arg;
5154 struct nfs_locku_res res;
5155 struct nfs4_lock_state *lsp;
5156 struct nfs_open_context *ctx;
5157 struct file_lock fl;
5158 const struct nfs_server *server;
5159 unsigned long timestamp;
5162 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5163 struct nfs_open_context *ctx,
5164 struct nfs4_lock_state *lsp,
5165 struct nfs_seqid *seqid)
5167 struct nfs4_unlockdata *p;
5168 struct inode *inode = lsp->ls_state->inode;
5170 p = kzalloc(sizeof(*p), GFP_NOFS);
5173 p->arg.fh = NFS_FH(inode);
5175 p->arg.seqid = seqid;
5176 p->res.seqid = seqid;
5177 p->arg.stateid = &lsp->ls_stateid;
5179 atomic_inc(&lsp->ls_count);
5180 /* Ensure we don't close file until we're done freeing locks! */
5181 p->ctx = get_nfs_open_context(ctx);
5182 memcpy(&p->fl, fl, sizeof(p->fl));
5183 p->server = NFS_SERVER(inode);
5187 static void nfs4_locku_release_calldata(void *data)
5189 struct nfs4_unlockdata *calldata = data;
5190 nfs_free_seqid(calldata->arg.seqid);
5191 nfs4_put_lock_state(calldata->lsp);
5192 put_nfs_open_context(calldata->ctx);
5196 static void nfs4_locku_done(struct rpc_task *task, void *data)
5198 struct nfs4_unlockdata *calldata = data;
5200 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5202 switch (task->tk_status) {
5204 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5205 &calldata->res.stateid);
5206 renew_lease(calldata->server, calldata->timestamp);
5208 case -NFS4ERR_BAD_STATEID:
5209 case -NFS4ERR_OLD_STATEID:
5210 case -NFS4ERR_STALE_STATEID:
5211 case -NFS4ERR_EXPIRED:
5214 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
5215 rpc_restart_call_prepare(task);
5217 nfs_release_seqid(calldata->arg.seqid);
5220 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5222 struct nfs4_unlockdata *calldata = data;
5224 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5226 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5227 /* Note: exit _without_ running nfs4_locku_done */
5230 calldata->timestamp = jiffies;
5231 if (nfs4_setup_sequence(calldata->server,
5232 &calldata->arg.seq_args,
5233 &calldata->res.seq_res,
5235 nfs_release_seqid(calldata->arg.seqid);
5238 task->tk_action = NULL;
5240 nfs4_sequence_done(task, &calldata->res.seq_res);
5243 static const struct rpc_call_ops nfs4_locku_ops = {
5244 .rpc_call_prepare = nfs4_locku_prepare,
5245 .rpc_call_done = nfs4_locku_done,
5246 .rpc_release = nfs4_locku_release_calldata,
5249 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5250 struct nfs_open_context *ctx,
5251 struct nfs4_lock_state *lsp,
5252 struct nfs_seqid *seqid)
5254 struct nfs4_unlockdata *data;
5255 struct rpc_message msg = {
5256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5257 .rpc_cred = ctx->cred,
5259 struct rpc_task_setup task_setup_data = {
5260 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5261 .rpc_message = &msg,
5262 .callback_ops = &nfs4_locku_ops,
5263 .workqueue = nfsiod_workqueue,
5264 .flags = RPC_TASK_ASYNC,
5267 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5268 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5270 /* Ensure this is an unlock - when canceling a lock, the
5271 * canceled lock is passed in, and it won't be an unlock.
5273 fl->fl_type = F_UNLCK;
5275 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5277 nfs_free_seqid(seqid);
5278 return ERR_PTR(-ENOMEM);
5281 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5282 msg.rpc_argp = &data->arg;
5283 msg.rpc_resp = &data->res;
5284 task_setup_data.callback_data = data;
5285 return rpc_run_task(&task_setup_data);
5288 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5290 struct inode *inode = state->inode;
5291 struct nfs4_state_owner *sp = state->owner;
5292 struct nfs_inode *nfsi = NFS_I(inode);
5293 struct nfs_seqid *seqid;
5294 struct nfs4_lock_state *lsp;
5295 struct rpc_task *task;
5297 unsigned char fl_flags = request->fl_flags;
5299 status = nfs4_set_lock_state(state, request);
5300 /* Unlock _before_ we do the RPC call */
5301 request->fl_flags |= FL_EXISTS;
5302 /* Exclude nfs_delegation_claim_locks() */
5303 mutex_lock(&sp->so_delegreturn_mutex);
5304 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5305 down_read(&nfsi->rwsem);
5306 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5307 up_read(&nfsi->rwsem);
5308 mutex_unlock(&sp->so_delegreturn_mutex);
5311 up_read(&nfsi->rwsem);
5312 mutex_unlock(&sp->so_delegreturn_mutex);
5315 /* Is this a delegated lock? */
5316 lsp = request->fl_u.nfs4_fl.owner;
5317 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5319 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5323 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5324 status = PTR_ERR(task);
5327 status = nfs4_wait_for_completion_rpc_task(task);
5330 request->fl_flags = fl_flags;
5331 trace_nfs4_unlock(request, state, F_SETLK, status);
5335 struct nfs4_lockdata {
5336 struct nfs_lock_args arg;
5337 struct nfs_lock_res res;
5338 struct nfs4_lock_state *lsp;
5339 struct nfs_open_context *ctx;
5340 struct file_lock fl;
5341 unsigned long timestamp;
5344 struct nfs_server *server;
5347 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5348 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5351 struct nfs4_lockdata *p;
5352 struct inode *inode = lsp->ls_state->inode;
5353 struct nfs_server *server = NFS_SERVER(inode);
5355 p = kzalloc(sizeof(*p), gfp_mask);
5359 p->arg.fh = NFS_FH(inode);
5361 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5362 if (p->arg.open_seqid == NULL)
5364 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5365 if (p->arg.lock_seqid == NULL)
5366 goto out_free_seqid;
5367 p->arg.lock_stateid = &lsp->ls_stateid;
5368 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5369 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5370 p->arg.lock_owner.s_dev = server->s_dev;
5371 p->res.lock_seqid = p->arg.lock_seqid;
5374 atomic_inc(&lsp->ls_count);
5375 p->ctx = get_nfs_open_context(ctx);
5376 memcpy(&p->fl, fl, sizeof(p->fl));
5379 nfs_free_seqid(p->arg.open_seqid);
5385 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5387 struct nfs4_lockdata *data = calldata;
5388 struct nfs4_state *state = data->lsp->ls_state;
5390 dprintk("%s: begin!\n", __func__);
5391 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5393 /* Do we need to do an open_to_lock_owner? */
5394 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5395 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5396 goto out_release_lock_seqid;
5398 data->arg.open_stateid = &state->open_stateid;
5399 data->arg.new_lock_owner = 1;
5400 data->res.open_seqid = data->arg.open_seqid;
5402 data->arg.new_lock_owner = 0;
5403 if (!nfs4_valid_open_stateid(state)) {
5404 data->rpc_status = -EBADF;
5405 task->tk_action = NULL;
5406 goto out_release_open_seqid;
5408 data->timestamp = jiffies;
5409 if (nfs4_setup_sequence(data->server,
5410 &data->arg.seq_args,
5414 out_release_open_seqid:
5415 nfs_release_seqid(data->arg.open_seqid);
5416 out_release_lock_seqid:
5417 nfs_release_seqid(data->arg.lock_seqid);
5419 nfs4_sequence_done(task, &data->res.seq_res);
5420 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5423 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5425 struct nfs4_lockdata *data = calldata;
5427 dprintk("%s: begin!\n", __func__);
5429 if (!nfs4_sequence_done(task, &data->res.seq_res))
5432 data->rpc_status = task->tk_status;
5433 if (data->arg.new_lock_owner != 0) {
5434 if (data->rpc_status == 0)
5435 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5439 if (data->rpc_status == 0) {
5440 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5441 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5442 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5445 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5448 static void nfs4_lock_release(void *calldata)
5450 struct nfs4_lockdata *data = calldata;
5452 dprintk("%s: begin!\n", __func__);
5453 nfs_free_seqid(data->arg.open_seqid);
5454 if (data->cancelled != 0) {
5455 struct rpc_task *task;
5456 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5457 data->arg.lock_seqid);
5459 rpc_put_task_async(task);
5460 dprintk("%s: cancelling lock!\n", __func__);
5462 nfs_free_seqid(data->arg.lock_seqid);
5463 nfs4_put_lock_state(data->lsp);
5464 put_nfs_open_context(data->ctx);
5466 dprintk("%s: done!\n", __func__);
5469 static const struct rpc_call_ops nfs4_lock_ops = {
5470 .rpc_call_prepare = nfs4_lock_prepare,
5471 .rpc_call_done = nfs4_lock_done,
5472 .rpc_release = nfs4_lock_release,
5475 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5478 case -NFS4ERR_ADMIN_REVOKED:
5479 case -NFS4ERR_BAD_STATEID:
5480 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5481 if (new_lock_owner != 0 ||
5482 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5483 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5485 case -NFS4ERR_STALE_STATEID:
5486 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5487 case -NFS4ERR_EXPIRED:
5488 nfs4_schedule_lease_recovery(server->nfs_client);
5492 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5494 struct nfs4_lockdata *data;
5495 struct rpc_task *task;
5496 struct rpc_message msg = {
5497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5498 .rpc_cred = state->owner->so_cred,
5500 struct rpc_task_setup task_setup_data = {
5501 .rpc_client = NFS_CLIENT(state->inode),
5502 .rpc_message = &msg,
5503 .callback_ops = &nfs4_lock_ops,
5504 .workqueue = nfsiod_workqueue,
5505 .flags = RPC_TASK_ASYNC,
5509 dprintk("%s: begin!\n", __func__);
5510 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5511 fl->fl_u.nfs4_fl.owner,
5512 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5516 data->arg.block = 1;
5517 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5518 msg.rpc_argp = &data->arg;
5519 msg.rpc_resp = &data->res;
5520 task_setup_data.callback_data = data;
5521 if (recovery_type > NFS_LOCK_NEW) {
5522 if (recovery_type == NFS_LOCK_RECLAIM)
5523 data->arg.reclaim = NFS_LOCK_RECLAIM;
5524 nfs4_set_sequence_privileged(&data->arg.seq_args);
5526 task = rpc_run_task(&task_setup_data);
5528 return PTR_ERR(task);
5529 ret = nfs4_wait_for_completion_rpc_task(task);
5531 ret = data->rpc_status;
5533 nfs4_handle_setlk_error(data->server, data->lsp,
5534 data->arg.new_lock_owner, ret);
5536 data->cancelled = 1;
5538 dprintk("%s: done, ret = %d!\n", __func__, ret);
5542 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5544 struct nfs_server *server = NFS_SERVER(state->inode);
5545 struct nfs4_exception exception = {
5546 .inode = state->inode,
5551 /* Cache the lock if possible... */
5552 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5554 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5555 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5556 if (err != -NFS4ERR_DELAY)
5558 nfs4_handle_exception(server, err, &exception);
5559 } while (exception.retry);
5563 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5565 struct nfs_server *server = NFS_SERVER(state->inode);
5566 struct nfs4_exception exception = {
5567 .inode = state->inode,
5571 err = nfs4_set_lock_state(state, request);
5574 if (!recover_lost_locks) {
5575 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5579 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5581 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5582 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5586 case -NFS4ERR_GRACE:
5587 case -NFS4ERR_DELAY:
5588 nfs4_handle_exception(server, err, &exception);
5591 } while (exception.retry);
5596 #if defined(CONFIG_NFS_V4_1)
5598 * nfs41_check_expired_locks - possibly free a lock stateid
5600 * @state: NFSv4 state for an inode
5602 * Returns NFS_OK if recovery for this stateid is now finished.
5603 * Otherwise a negative NFS4ERR value is returned.
5605 static int nfs41_check_expired_locks(struct nfs4_state *state)
5607 int status, ret = -NFS4ERR_BAD_STATEID;
5608 struct nfs4_lock_state *lsp;
5609 struct nfs_server *server = NFS_SERVER(state->inode);
5611 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5612 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5613 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5615 status = nfs41_test_stateid(server,
5618 trace_nfs4_test_lock_stateid(state, lsp, status);
5619 if (status != NFS_OK) {
5620 /* Free the stateid unless the server
5621 * informs us the stateid is unrecognized. */
5622 if (status != -NFS4ERR_BAD_STATEID)
5623 nfs41_free_stateid(server,
5626 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5635 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5637 int status = NFS_OK;
5639 if (test_bit(LK_STATE_IN_USE, &state->flags))
5640 status = nfs41_check_expired_locks(state);
5641 if (status != NFS_OK)
5642 status = nfs4_lock_expired(state, request);
5647 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5649 struct nfs4_state_owner *sp = state->owner;
5650 struct nfs_inode *nfsi = NFS_I(state->inode);
5651 unsigned char fl_flags = request->fl_flags;
5653 int status = -ENOLCK;
5655 if ((fl_flags & FL_POSIX) &&
5656 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5658 /* Is this a delegated open? */
5659 status = nfs4_set_lock_state(state, request);
5662 request->fl_flags |= FL_ACCESS;
5663 status = do_vfs_lock(request->fl_file, request);
5666 down_read(&nfsi->rwsem);
5667 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5668 /* Yes: cache locks! */
5669 /* ...but avoid races with delegation recall... */
5670 request->fl_flags = fl_flags & ~FL_SLEEP;
5671 status = do_vfs_lock(request->fl_file, request);
5674 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5675 up_read(&nfsi->rwsem);
5676 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5679 down_read(&nfsi->rwsem);
5680 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5681 status = -NFS4ERR_DELAY;
5684 /* Note: we always want to sleep here! */
5685 request->fl_flags = fl_flags | FL_SLEEP;
5686 if (do_vfs_lock(request->fl_file, request) < 0)
5687 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5688 "manager!\n", __func__);
5690 up_read(&nfsi->rwsem);
5692 request->fl_flags = fl_flags;
5696 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5698 struct nfs4_exception exception = {
5700 .inode = state->inode,
5705 err = _nfs4_proc_setlk(state, cmd, request);
5706 trace_nfs4_set_lock(request, state, cmd, err);
5707 if (err == -NFS4ERR_DENIED)
5709 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5711 } while (exception.retry);
5716 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5718 struct nfs_open_context *ctx;
5719 struct nfs4_state *state;
5720 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5723 /* verify open state */
5724 ctx = nfs_file_open_context(filp);
5727 if (request->fl_start < 0 || request->fl_end < 0)
5730 if (IS_GETLK(cmd)) {
5732 return nfs4_proc_getlk(state, F_GETLK, request);
5736 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5739 if (request->fl_type == F_UNLCK) {
5741 return nfs4_proc_unlck(state, cmd, request);
5748 * Don't rely on the VFS having checked the file open mode,
5749 * since it won't do this for flock() locks.
5751 switch (request->fl_type) {
5753 if (!(filp->f_mode & FMODE_READ))
5757 if (!(filp->f_mode & FMODE_WRITE))
5762 status = nfs4_proc_setlk(state, cmd, request);
5763 if ((status != -EAGAIN) || IS_SETLK(cmd))
5765 timeout = nfs4_set_lock_task_retry(timeout);
5766 status = -ERESTARTSYS;
5769 } while(status < 0);
5773 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5775 struct nfs_server *server = NFS_SERVER(state->inode);
5778 err = nfs4_set_lock_state(state, fl);
5781 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5782 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5785 struct nfs_release_lockowner_data {
5786 struct nfs4_lock_state *lsp;
5787 struct nfs_server *server;
5788 struct nfs_release_lockowner_args args;
5789 struct nfs4_sequence_args seq_args;
5790 struct nfs4_sequence_res seq_res;
5793 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5795 struct nfs_release_lockowner_data *data = calldata;
5796 nfs40_setup_sequence(data->server,
5797 &data->seq_args, &data->seq_res, task);
5800 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5802 struct nfs_release_lockowner_data *data = calldata;
5803 nfs40_sequence_done(task, &data->seq_res);
5806 static void nfs4_release_lockowner_release(void *calldata)
5808 struct nfs_release_lockowner_data *data = calldata;
5809 nfs4_free_lock_state(data->server, data->lsp);
5813 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5814 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5815 .rpc_call_done = nfs4_release_lockowner_done,
5816 .rpc_release = nfs4_release_lockowner_release,
5819 static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
5821 struct nfs_release_lockowner_data *data;
5822 struct rpc_message msg = {
5823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5826 if (server->nfs_client->cl_mvops->minor_version != 0)
5829 data = kmalloc(sizeof(*data), GFP_NOFS);
5832 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
5834 data->server = server;
5835 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5836 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5837 data->args.lock_owner.s_dev = server->s_dev;
5839 msg.rpc_argp = &data->args;
5840 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5844 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5846 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5847 const void *buf, size_t buflen,
5848 int flags, int type)
5850 if (strcmp(key, "") != 0)
5853 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5856 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5857 void *buf, size_t buflen, int type)
5859 if (strcmp(key, "") != 0)
5862 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5865 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5866 size_t list_len, const char *name,
5867 size_t name_len, int type)
5869 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5871 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5874 if (list && len <= list_len)
5875 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5879 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5880 static inline int nfs4_server_supports_labels(struct nfs_server *server)
5882 return server->caps & NFS_CAP_SECURITY_LABEL;
5885 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5886 const void *buf, size_t buflen,
5887 int flags, int type)
5889 if (security_ismaclabel(key))
5890 return nfs4_set_security_label(dentry, buf, buflen);
5895 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5896 void *buf, size_t buflen, int type)
5898 if (security_ismaclabel(key))
5899 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5903 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5904 size_t list_len, const char *name,
5905 size_t name_len, int type)
5909 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5910 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5911 if (list && len <= list_len)
5912 security_inode_listsecurity(dentry->d_inode, list, len);
5917 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5918 .prefix = XATTR_SECURITY_PREFIX,
5919 .list = nfs4_xattr_list_nfs4_label,
5920 .get = nfs4_xattr_get_nfs4_label,
5921 .set = nfs4_xattr_set_nfs4_label,
5927 * nfs_fhget will use either the mounted_on_fileid or the fileid
5929 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5931 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5932 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5933 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5934 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5937 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5938 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5939 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5943 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5944 const struct qstr *name,
5945 struct nfs4_fs_locations *fs_locations,
5948 struct nfs_server *server = NFS_SERVER(dir);
5950 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5952 struct nfs4_fs_locations_arg args = {
5953 .dir_fh = NFS_FH(dir),
5958 struct nfs4_fs_locations_res res = {
5959 .fs_locations = fs_locations,
5961 struct rpc_message msg = {
5962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5968 dprintk("%s: start\n", __func__);
5970 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5971 * is not supported */
5972 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5973 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5975 bitmask[0] |= FATTR4_WORD0_FILEID;
5977 nfs_fattr_init(&fs_locations->fattr);
5978 fs_locations->server = server;
5979 fs_locations->nlocations = 0;
5980 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5981 dprintk("%s: returned status = %d\n", __func__, status);
5985 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5986 const struct qstr *name,
5987 struct nfs4_fs_locations *fs_locations,
5990 struct nfs4_exception exception = { };
5993 err = _nfs4_proc_fs_locations(client, dir, name,
5994 fs_locations, page);
5995 trace_nfs4_get_fs_locations(dir, name, err);
5996 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5998 } while (exception.retry);
6003 * This operation also signals the server that this client is
6004 * performing migration recovery. The server can stop returning
6005 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6006 * appended to this compound to identify the client ID which is
6007 * performing recovery.
6009 static int _nfs40_proc_get_locations(struct inode *inode,
6010 struct nfs4_fs_locations *locations,
6011 struct page *page, struct rpc_cred *cred)
6013 struct nfs_server *server = NFS_SERVER(inode);
6014 struct rpc_clnt *clnt = server->client;
6016 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6018 struct nfs4_fs_locations_arg args = {
6019 .clientid = server->nfs_client->cl_clientid,
6020 .fh = NFS_FH(inode),
6023 .migration = 1, /* skip LOOKUP */
6024 .renew = 1, /* append RENEW */
6026 struct nfs4_fs_locations_res res = {
6027 .fs_locations = locations,
6031 struct rpc_message msg = {
6032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6037 unsigned long now = jiffies;
6040 nfs_fattr_init(&locations->fattr);
6041 locations->server = server;
6042 locations->nlocations = 0;
6044 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6045 nfs4_set_sequence_privileged(&args.seq_args);
6046 status = nfs4_call_sync_sequence(clnt, server, &msg,
6047 &args.seq_args, &res.seq_res);
6051 renew_lease(server, now);
6055 #ifdef CONFIG_NFS_V4_1
6058 * This operation also signals the server that this client is
6059 * performing migration recovery. The server can stop asserting
6060 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6061 * performing this operation is identified in the SEQUENCE
6062 * operation in this compound.
6064 * When the client supports GETATTR(fs_locations_info), it can
6065 * be plumbed in here.
6067 static int _nfs41_proc_get_locations(struct inode *inode,
6068 struct nfs4_fs_locations *locations,
6069 struct page *page, struct rpc_cred *cred)
6071 struct nfs_server *server = NFS_SERVER(inode);
6072 struct rpc_clnt *clnt = server->client;
6074 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6076 struct nfs4_fs_locations_arg args = {
6077 .fh = NFS_FH(inode),
6080 .migration = 1, /* skip LOOKUP */
6082 struct nfs4_fs_locations_res res = {
6083 .fs_locations = locations,
6086 struct rpc_message msg = {
6087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6094 nfs_fattr_init(&locations->fattr);
6095 locations->server = server;
6096 locations->nlocations = 0;
6098 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6099 nfs4_set_sequence_privileged(&args.seq_args);
6100 status = nfs4_call_sync_sequence(clnt, server, &msg,
6101 &args.seq_args, &res.seq_res);
6102 if (status == NFS4_OK &&
6103 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6104 status = -NFS4ERR_LEASE_MOVED;
6108 #endif /* CONFIG_NFS_V4_1 */
6111 * nfs4_proc_get_locations - discover locations for a migrated FSID
6112 * @inode: inode on FSID that is migrating
6113 * @locations: result of query
6115 * @cred: credential to use for this operation
6117 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6118 * operation failed, or a negative errno if a local error occurred.
6120 * On success, "locations" is filled in, but if the server has
6121 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6124 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6125 * from this client that require migration recovery.
6127 int nfs4_proc_get_locations(struct inode *inode,
6128 struct nfs4_fs_locations *locations,
6129 struct page *page, struct rpc_cred *cred)
6131 struct nfs_server *server = NFS_SERVER(inode);
6132 struct nfs_client *clp = server->nfs_client;
6133 const struct nfs4_mig_recovery_ops *ops =
6134 clp->cl_mvops->mig_recovery_ops;
6135 struct nfs4_exception exception = { };
6138 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6139 (unsigned long long)server->fsid.major,
6140 (unsigned long long)server->fsid.minor,
6142 nfs_display_fhandle(NFS_FH(inode), __func__);
6145 status = ops->get_locations(inode, locations, page, cred);
6146 if (status != -NFS4ERR_DELAY)
6148 nfs4_handle_exception(server, status, &exception);
6149 } while (exception.retry);
6154 * If 'use_integrity' is true and the state managment nfs_client
6155 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6156 * and the machine credential as per RFC3530bis and RFC5661 Security
6157 * Considerations sections. Otherwise, just use the user cred with the
6158 * filesystem's rpc_client.
6160 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6163 struct nfs4_secinfo_arg args = {
6164 .dir_fh = NFS_FH(dir),
6167 struct nfs4_secinfo_res res = {
6170 struct rpc_message msg = {
6171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6175 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6176 struct rpc_cred *cred = NULL;
6178 if (use_integrity) {
6179 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6180 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6181 msg.rpc_cred = cred;
6184 dprintk("NFS call secinfo %s\n", name->name);
6186 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6187 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6189 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6191 dprintk("NFS reply secinfo: %d\n", status);
6199 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6200 struct nfs4_secinfo_flavors *flavors)
6202 struct nfs4_exception exception = { };
6205 err = -NFS4ERR_WRONGSEC;
6207 /* try to use integrity protection with machine cred */
6208 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6209 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6212 * if unable to use integrity protection, or SECINFO with
6213 * integrity protection returns NFS4ERR_WRONGSEC (which is
6214 * disallowed by spec, but exists in deployed servers) use
6215 * the current filesystem's rpc_client and the user cred.
6217 if (err == -NFS4ERR_WRONGSEC)
6218 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6220 trace_nfs4_secinfo(dir, name, err);
6221 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6223 } while (exception.retry);
6227 #ifdef CONFIG_NFS_V4_1
6229 * Check the exchange flags returned by the server for invalid flags, having
6230 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6233 static int nfs4_check_cl_exchange_flags(u32 flags)
6235 if (flags & ~EXCHGID4_FLAG_MASK_R)
6237 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6238 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6240 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6244 return -NFS4ERR_INVAL;
6248 nfs41_same_server_scope(struct nfs41_server_scope *a,
6249 struct nfs41_server_scope *b)
6251 if (a->server_scope_sz == b->server_scope_sz &&
6252 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6259 * nfs4_proc_bind_conn_to_session()
6261 * The 4.1 client currently uses the same TCP connection for the
6262 * fore and backchannel.
6264 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6267 struct nfs41_bind_conn_to_session_res res;
6268 struct rpc_message msg = {
6270 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6276 dprintk("--> %s\n", __func__);
6278 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6279 if (unlikely(res.session == NULL)) {
6284 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6285 trace_nfs4_bind_conn_to_session(clp, status);
6287 if (memcmp(res.session->sess_id.data,
6288 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6289 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6293 if (res.dir != NFS4_CDFS4_BOTH) {
6294 dprintk("NFS: %s: Unexpected direction from server\n",
6299 if (res.use_conn_in_rdma_mode) {
6300 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6309 dprintk("<-- %s status= %d\n", __func__, status);
6314 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6315 * and operations we'd like to see to enable certain features in the allow map
6317 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6318 .how = SP4_MACH_CRED,
6319 .enforce.u.words = {
6320 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6321 1 << (OP_EXCHANGE_ID - 32) |
6322 1 << (OP_CREATE_SESSION - 32) |
6323 1 << (OP_DESTROY_SESSION - 32) |
6324 1 << (OP_DESTROY_CLIENTID - 32)
6327 [0] = 1 << (OP_CLOSE) |
6330 [1] = 1 << (OP_SECINFO - 32) |
6331 1 << (OP_SECINFO_NO_NAME - 32) |
6332 1 << (OP_TEST_STATEID - 32) |
6333 1 << (OP_FREE_STATEID - 32) |
6334 1 << (OP_WRITE - 32)
6339 * Select the state protection mode for client `clp' given the server results
6340 * from exchange_id in `sp'.
6342 * Returns 0 on success, negative errno otherwise.
6344 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6345 struct nfs41_state_protection *sp)
6347 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6348 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6349 1 << (OP_EXCHANGE_ID - 32) |
6350 1 << (OP_CREATE_SESSION - 32) |
6351 1 << (OP_DESTROY_SESSION - 32) |
6352 1 << (OP_DESTROY_CLIENTID - 32)
6356 if (sp->how == SP4_MACH_CRED) {
6357 /* Print state protect result */
6358 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6359 for (i = 0; i <= LAST_NFS4_OP; i++) {
6360 if (test_bit(i, sp->enforce.u.longs))
6361 dfprintk(MOUNT, " enforce op %d\n", i);
6362 if (test_bit(i, sp->allow.u.longs))
6363 dfprintk(MOUNT, " allow op %d\n", i);
6366 /* make sure nothing is on enforce list that isn't supported */
6367 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6368 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6369 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6375 * Minimal mode - state operations are allowed to use machine
6376 * credential. Note this already happens by default, so the
6377 * client doesn't have to do anything more than the negotiation.
6379 * NOTE: we don't care if EXCHANGE_ID is in the list -
6380 * we're already using the machine cred for exchange_id
6381 * and will never use a different cred.
6383 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6384 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6385 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6386 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6387 dfprintk(MOUNT, "sp4_mach_cred:\n");
6388 dfprintk(MOUNT, " minimal mode enabled\n");
6389 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6391 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6395 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6396 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6397 dfprintk(MOUNT, " cleanup mode enabled\n");
6398 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6401 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6402 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6403 dfprintk(MOUNT, " secinfo mode enabled\n");
6404 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6407 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6408 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6409 dfprintk(MOUNT, " stateid mode enabled\n");
6410 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6413 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6414 dfprintk(MOUNT, " write mode enabled\n");
6415 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6418 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6419 dfprintk(MOUNT, " commit mode enabled\n");
6420 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6428 * _nfs4_proc_exchange_id()
6430 * Wrapper for EXCHANGE_ID operation.
6432 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6435 nfs4_verifier verifier;
6436 struct nfs41_exchange_id_args args = {
6437 .verifier = &verifier,
6439 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6440 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6442 struct nfs41_exchange_id_res res = {
6446 struct rpc_message msg = {
6447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6453 nfs4_init_boot_verifier(clp, &verifier);
6454 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6456 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6457 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6458 args.id_len, args.id);
6460 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6462 if (unlikely(res.server_owner == NULL)) {
6467 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6469 if (unlikely(res.server_scope == NULL)) {
6471 goto out_server_owner;
6474 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6475 if (unlikely(res.impl_id == NULL)) {
6477 goto out_server_scope;
6482 args.state_protect.how = SP4_NONE;
6486 args.state_protect = nfs4_sp4_mach_cred_request;
6493 goto out_server_scope;
6496 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6497 trace_nfs4_exchange_id(clp, status);
6499 status = nfs4_check_cl_exchange_flags(res.flags);
6502 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6505 clp->cl_clientid = res.clientid;
6506 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6507 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6508 clp->cl_seqid = res.seqid;
6510 kfree(clp->cl_serverowner);
6511 clp->cl_serverowner = res.server_owner;
6512 res.server_owner = NULL;
6514 /* use the most recent implementation id */
6515 kfree(clp->cl_implid);
6516 clp->cl_implid = res.impl_id;
6518 if (clp->cl_serverscope != NULL &&
6519 !nfs41_same_server_scope(clp->cl_serverscope,
6520 res.server_scope)) {
6521 dprintk("%s: server_scope mismatch detected\n",
6523 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6524 kfree(clp->cl_serverscope);
6525 clp->cl_serverscope = NULL;
6528 if (clp->cl_serverscope == NULL) {
6529 clp->cl_serverscope = res.server_scope;
6536 kfree(res.server_owner);
6538 kfree(res.server_scope);
6540 if (clp->cl_implid != NULL)
6541 dprintk("NFS reply exchange_id: Server Implementation ID: "
6542 "domain: %s, name: %s, date: %llu,%u\n",
6543 clp->cl_implid->domain, clp->cl_implid->name,
6544 clp->cl_implid->date.seconds,
6545 clp->cl_implid->date.nseconds);
6546 dprintk("NFS reply exchange_id: %d\n", status);
6551 * nfs4_proc_exchange_id()
6553 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6555 * Since the clientid has expired, all compounds using sessions
6556 * associated with the stale clientid will be returning
6557 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6558 * be in some phase of session reset.
6560 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6562 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6564 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6567 /* try SP4_MACH_CRED if krb5i/p */
6568 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6569 authflavor == RPC_AUTH_GSS_KRB5P) {
6570 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6576 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6579 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6580 struct rpc_cred *cred)
6582 struct rpc_message msg = {
6583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6589 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6590 trace_nfs4_destroy_clientid(clp, status);
6592 dprintk("NFS: Got error %d from the server %s on "
6593 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6597 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6598 struct rpc_cred *cred)
6603 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6604 ret = _nfs4_proc_destroy_clientid(clp, cred);
6606 case -NFS4ERR_DELAY:
6607 case -NFS4ERR_CLIENTID_BUSY:
6617 int nfs4_destroy_clientid(struct nfs_client *clp)
6619 struct rpc_cred *cred;
6622 if (clp->cl_mvops->minor_version < 1)
6624 if (clp->cl_exchange_flags == 0)
6626 if (clp->cl_preserve_clid)
6628 cred = nfs4_get_clid_cred(clp);
6629 ret = nfs4_proc_destroy_clientid(clp, cred);
6634 case -NFS4ERR_STALE_CLIENTID:
6635 clp->cl_exchange_flags = 0;
6641 struct nfs4_get_lease_time_data {
6642 struct nfs4_get_lease_time_args *args;
6643 struct nfs4_get_lease_time_res *res;
6644 struct nfs_client *clp;
6647 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6650 struct nfs4_get_lease_time_data *data =
6651 (struct nfs4_get_lease_time_data *)calldata;
6653 dprintk("--> %s\n", __func__);
6654 /* just setup sequence, do not trigger session recovery
6655 since we're invoked within one */
6656 nfs41_setup_sequence(data->clp->cl_session,
6657 &data->args->la_seq_args,
6658 &data->res->lr_seq_res,
6660 dprintk("<-- %s\n", __func__);
6664 * Called from nfs4_state_manager thread for session setup, so don't recover
6665 * from sequence operation or clientid errors.
6667 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6669 struct nfs4_get_lease_time_data *data =
6670 (struct nfs4_get_lease_time_data *)calldata;
6672 dprintk("--> %s\n", __func__);
6673 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6675 switch (task->tk_status) {
6676 case -NFS4ERR_DELAY:
6677 case -NFS4ERR_GRACE:
6678 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6679 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6680 task->tk_status = 0;
6682 case -NFS4ERR_RETRY_UNCACHED_REP:
6683 rpc_restart_call_prepare(task);
6686 dprintk("<-- %s\n", __func__);
6689 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
6690 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6691 .rpc_call_done = nfs4_get_lease_time_done,
6694 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6696 struct rpc_task *task;
6697 struct nfs4_get_lease_time_args args;
6698 struct nfs4_get_lease_time_res res = {
6699 .lr_fsinfo = fsinfo,
6701 struct nfs4_get_lease_time_data data = {
6706 struct rpc_message msg = {
6707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6711 struct rpc_task_setup task_setup = {
6712 .rpc_client = clp->cl_rpcclient,
6713 .rpc_message = &msg,
6714 .callback_ops = &nfs4_get_lease_time_ops,
6715 .callback_data = &data,
6716 .flags = RPC_TASK_TIMEOUT,
6720 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
6721 nfs4_set_sequence_privileged(&args.la_seq_args);
6722 dprintk("--> %s\n", __func__);
6723 task = rpc_run_task(&task_setup);
6726 status = PTR_ERR(task);
6728 status = task->tk_status;
6731 dprintk("<-- %s return %d\n", __func__, status);
6737 * Initialize the values to be used by the client in CREATE_SESSION
6738 * If nfs4_init_session set the fore channel request and response sizes,
6741 * Set the back channel max_resp_sz_cached to zero to force the client to
6742 * always set csa_cachethis to FALSE because the current implementation
6743 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6745 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6747 unsigned int max_rqst_sz, max_resp_sz;
6749 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6750 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6752 /* Fore channel attributes */
6753 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6754 args->fc_attrs.max_resp_sz = max_resp_sz;
6755 args->fc_attrs.max_ops = NFS4_MAX_OPS;
6756 args->fc_attrs.max_reqs = max_session_slots;
6758 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6759 "max_ops=%u max_reqs=%u\n",
6761 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
6762 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
6764 /* Back channel attributes */
6765 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6766 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6767 args->bc_attrs.max_resp_sz_cached = 0;
6768 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6769 args->bc_attrs.max_reqs = 1;
6771 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6772 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6774 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6775 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6776 args->bc_attrs.max_reqs);
6779 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6781 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6782 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6784 if (rcvd->max_resp_sz > sent->max_resp_sz)
6787 * Our requested max_ops is the minimum we need; we're not
6788 * prepared to break up compounds into smaller pieces than that.
6789 * So, no point even trying to continue if the server won't
6792 if (rcvd->max_ops < sent->max_ops)
6794 if (rcvd->max_reqs == 0)
6796 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6797 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
6801 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6803 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6804 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
6806 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6808 if (rcvd->max_resp_sz < sent->max_resp_sz)
6810 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6812 /* These would render the backchannel useless: */
6813 if (rcvd->max_ops != sent->max_ops)
6815 if (rcvd->max_reqs != sent->max_reqs)
6820 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6821 struct nfs4_session *session)
6825 ret = nfs4_verify_fore_channel_attrs(args, session);
6828 return nfs4_verify_back_channel_attrs(args, session);
6831 static int _nfs4_proc_create_session(struct nfs_client *clp,
6832 struct rpc_cred *cred)
6834 struct nfs4_session *session = clp->cl_session;
6835 struct nfs41_create_session_args args = {
6837 .cb_program = NFS4_CALLBACK,
6839 struct nfs41_create_session_res res = {
6842 struct rpc_message msg = {
6843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6850 nfs4_init_channel_attrs(&args);
6851 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
6853 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6854 trace_nfs4_create_session(clp, status);
6857 /* Verify the session's negotiated channel_attrs values */
6858 status = nfs4_verify_channel_attrs(&args, session);
6859 /* Increment the clientid slot sequence id */
6867 * Issues a CREATE_SESSION operation to the server.
6868 * It is the responsibility of the caller to verify the session is
6869 * expired before calling this routine.
6871 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
6875 struct nfs4_session *session = clp->cl_session;
6877 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6879 status = _nfs4_proc_create_session(clp, cred);
6883 /* Init or reset the session slot tables */
6884 status = nfs4_setup_session_slot_tables(session);
6885 dprintk("slot table setup returned %d\n", status);
6889 ptr = (unsigned *)&session->sess_id.data[0];
6890 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6891 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
6893 dprintk("<-- %s\n", __func__);
6898 * Issue the over-the-wire RPC DESTROY_SESSION.
6899 * The caller must serialize access to this routine.
6901 int nfs4_proc_destroy_session(struct nfs4_session *session,
6902 struct rpc_cred *cred)
6904 struct rpc_message msg = {
6905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6906 .rpc_argp = session,
6911 dprintk("--> nfs4_proc_destroy_session\n");
6913 /* session is still being setup */
6914 if (session->clp->cl_cons_state != NFS_CS_READY)
6917 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6918 trace_nfs4_destroy_session(session->clp, status);
6921 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6922 "Session has been destroyed regardless...\n", status);
6924 dprintk("<-- nfs4_proc_destroy_session\n");
6929 * Renew the cl_session lease.
6931 struct nfs4_sequence_data {
6932 struct nfs_client *clp;
6933 struct nfs4_sequence_args args;
6934 struct nfs4_sequence_res res;
6937 static void nfs41_sequence_release(void *data)
6939 struct nfs4_sequence_data *calldata = data;
6940 struct nfs_client *clp = calldata->clp;
6942 if (atomic_read(&clp->cl_count) > 1)
6943 nfs4_schedule_state_renewal(clp);
6944 nfs_put_client(clp);
6948 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6950 switch(task->tk_status) {
6951 case -NFS4ERR_DELAY:
6952 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6955 nfs4_schedule_lease_recovery(clp);
6960 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6962 struct nfs4_sequence_data *calldata = data;
6963 struct nfs_client *clp = calldata->clp;
6965 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6968 trace_nfs4_sequence(clp, task->tk_status);
6969 if (task->tk_status < 0) {
6970 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6971 if (atomic_read(&clp->cl_count) == 1)
6974 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6975 rpc_restart_call_prepare(task);
6979 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6981 dprintk("<-- %s\n", __func__);
6984 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6986 struct nfs4_sequence_data *calldata = data;
6987 struct nfs_client *clp = calldata->clp;
6988 struct nfs4_sequence_args *args;
6989 struct nfs4_sequence_res *res;
6991 args = task->tk_msg.rpc_argp;
6992 res = task->tk_msg.rpc_resp;
6994 nfs41_setup_sequence(clp->cl_session, args, res, task);
6997 static const struct rpc_call_ops nfs41_sequence_ops = {
6998 .rpc_call_done = nfs41_sequence_call_done,
6999 .rpc_call_prepare = nfs41_sequence_prepare,
7000 .rpc_release = nfs41_sequence_release,
7003 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7004 struct rpc_cred *cred,
7007 struct nfs4_sequence_data *calldata;
7008 struct rpc_message msg = {
7009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7012 struct rpc_task_setup task_setup_data = {
7013 .rpc_client = clp->cl_rpcclient,
7014 .rpc_message = &msg,
7015 .callback_ops = &nfs41_sequence_ops,
7016 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7019 if (!atomic_inc_not_zero(&clp->cl_count))
7020 return ERR_PTR(-EIO);
7021 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7022 if (calldata == NULL) {
7023 nfs_put_client(clp);
7024 return ERR_PTR(-ENOMEM);
7026 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7028 nfs4_set_sequence_privileged(&calldata->args);
7029 msg.rpc_argp = &calldata->args;
7030 msg.rpc_resp = &calldata->res;
7031 calldata->clp = clp;
7032 task_setup_data.callback_data = calldata;
7034 return rpc_run_task(&task_setup_data);
7037 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7039 struct rpc_task *task;
7042 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7044 task = _nfs41_proc_sequence(clp, cred, false);
7046 ret = PTR_ERR(task);
7048 rpc_put_task_async(task);
7049 dprintk("<-- %s status=%d\n", __func__, ret);
7053 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7055 struct rpc_task *task;
7058 task = _nfs41_proc_sequence(clp, cred, true);
7060 ret = PTR_ERR(task);
7063 ret = rpc_wait_for_completion_task(task);
7065 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7067 if (task->tk_status == 0)
7068 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7069 ret = task->tk_status;
7073 dprintk("<-- %s status=%d\n", __func__, ret);
7077 struct nfs4_reclaim_complete_data {
7078 struct nfs_client *clp;
7079 struct nfs41_reclaim_complete_args arg;
7080 struct nfs41_reclaim_complete_res res;
7083 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7085 struct nfs4_reclaim_complete_data *calldata = data;
7087 nfs41_setup_sequence(calldata->clp->cl_session,
7088 &calldata->arg.seq_args,
7089 &calldata->res.seq_res,
7093 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7095 switch(task->tk_status) {
7097 case -NFS4ERR_COMPLETE_ALREADY:
7098 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7100 case -NFS4ERR_DELAY:
7101 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7103 case -NFS4ERR_RETRY_UNCACHED_REP:
7106 nfs4_schedule_lease_recovery(clp);
7111 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7113 struct nfs4_reclaim_complete_data *calldata = data;
7114 struct nfs_client *clp = calldata->clp;
7115 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7117 dprintk("--> %s\n", __func__);
7118 if (!nfs41_sequence_done(task, res))
7121 trace_nfs4_reclaim_complete(clp, task->tk_status);
7122 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7123 rpc_restart_call_prepare(task);
7126 dprintk("<-- %s\n", __func__);
7129 static void nfs4_free_reclaim_complete_data(void *data)
7131 struct nfs4_reclaim_complete_data *calldata = data;
7136 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7137 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7138 .rpc_call_done = nfs4_reclaim_complete_done,
7139 .rpc_release = nfs4_free_reclaim_complete_data,
7143 * Issue a global reclaim complete.
7145 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7146 struct rpc_cred *cred)
7148 struct nfs4_reclaim_complete_data *calldata;
7149 struct rpc_task *task;
7150 struct rpc_message msg = {
7151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7154 struct rpc_task_setup task_setup_data = {
7155 .rpc_client = clp->cl_rpcclient,
7156 .rpc_message = &msg,
7157 .callback_ops = &nfs4_reclaim_complete_call_ops,
7158 .flags = RPC_TASK_ASYNC,
7160 int status = -ENOMEM;
7162 dprintk("--> %s\n", __func__);
7163 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7164 if (calldata == NULL)
7166 calldata->clp = clp;
7167 calldata->arg.one_fs = 0;
7169 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7170 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7171 msg.rpc_argp = &calldata->arg;
7172 msg.rpc_resp = &calldata->res;
7173 task_setup_data.callback_data = calldata;
7174 task = rpc_run_task(&task_setup_data);
7176 status = PTR_ERR(task);
7179 status = nfs4_wait_for_completion_rpc_task(task);
7181 status = task->tk_status;
7185 dprintk("<-- %s status=%d\n", __func__, status);
7190 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7192 struct nfs4_layoutget *lgp = calldata;
7193 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7194 struct nfs4_session *session = nfs4_get_session(server);
7196 dprintk("--> %s\n", __func__);
7197 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7198 * right now covering the LAYOUTGET we are about to send.
7199 * However, that is not so catastrophic, and there seems
7200 * to be no way to prevent it completely.
7202 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7203 &lgp->res.seq_res, task))
7205 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7206 NFS_I(lgp->args.inode)->layout,
7207 lgp->args.ctx->state)) {
7208 rpc_exit(task, NFS4_OK);
7212 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7214 struct nfs4_layoutget *lgp = calldata;
7215 struct inode *inode = lgp->args.inode;
7216 struct nfs_server *server = NFS_SERVER(inode);
7217 struct pnfs_layout_hdr *lo;
7218 struct nfs4_state *state = NULL;
7219 unsigned long timeo, giveup;
7221 dprintk("--> %s\n", __func__);
7223 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7226 switch (task->tk_status) {
7229 case -NFS4ERR_LAYOUTTRYLATER:
7230 case -NFS4ERR_RECALLCONFLICT:
7231 timeo = rpc_get_timeout(task->tk_client);
7232 giveup = lgp->args.timestamp + timeo;
7233 if (time_after(giveup, jiffies))
7234 task->tk_status = -NFS4ERR_DELAY;
7236 case -NFS4ERR_EXPIRED:
7237 case -NFS4ERR_BAD_STATEID:
7238 spin_lock(&inode->i_lock);
7239 lo = NFS_I(inode)->layout;
7240 if (!lo || list_empty(&lo->plh_segs)) {
7241 spin_unlock(&inode->i_lock);
7242 /* If the open stateid was bad, then recover it. */
7243 state = lgp->args.ctx->state;
7247 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7248 spin_unlock(&inode->i_lock);
7249 /* Mark the bad layout state as invalid, then
7250 * retry using the open stateid. */
7251 pnfs_free_lseg_list(&head);
7254 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7255 rpc_restart_call_prepare(task);
7257 dprintk("<-- %s\n", __func__);
7260 static size_t max_response_pages(struct nfs_server *server)
7262 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7263 return nfs_page_array_len(0, max_resp_sz);
7266 static void nfs4_free_pages(struct page **pages, size_t size)
7273 for (i = 0; i < size; i++) {
7276 __free_page(pages[i]);
7281 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7283 struct page **pages;
7286 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7288 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7292 for (i = 0; i < size; i++) {
7293 pages[i] = alloc_page(gfp_flags);
7295 dprintk("%s: failed to allocate page\n", __func__);
7296 nfs4_free_pages(pages, size);
7304 static void nfs4_layoutget_release(void *calldata)
7306 struct nfs4_layoutget *lgp = calldata;
7307 struct inode *inode = lgp->args.inode;
7308 struct nfs_server *server = NFS_SERVER(inode);
7309 size_t max_pages = max_response_pages(server);
7311 dprintk("--> %s\n", __func__);
7312 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7313 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7314 put_nfs_open_context(lgp->args.ctx);
7316 dprintk("<-- %s\n", __func__);
7319 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7320 .rpc_call_prepare = nfs4_layoutget_prepare,
7321 .rpc_call_done = nfs4_layoutget_done,
7322 .rpc_release = nfs4_layoutget_release,
7325 struct pnfs_layout_segment *
7326 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7328 struct inode *inode = lgp->args.inode;
7329 struct nfs_server *server = NFS_SERVER(inode);
7330 size_t max_pages = max_response_pages(server);
7331 struct rpc_task *task;
7332 struct rpc_message msg = {
7333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7334 .rpc_argp = &lgp->args,
7335 .rpc_resp = &lgp->res,
7336 .rpc_cred = lgp->cred,
7338 struct rpc_task_setup task_setup_data = {
7339 .rpc_client = server->client,
7340 .rpc_message = &msg,
7341 .callback_ops = &nfs4_layoutget_call_ops,
7342 .callback_data = lgp,
7343 .flags = RPC_TASK_ASYNC,
7345 struct pnfs_layout_segment *lseg = NULL;
7348 dprintk("--> %s\n", __func__);
7350 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7351 if (!lgp->args.layout.pages) {
7352 nfs4_layoutget_release(lgp);
7353 return ERR_PTR(-ENOMEM);
7355 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7356 lgp->args.timestamp = jiffies;
7358 lgp->res.layoutp = &lgp->args.layout;
7359 lgp->res.seq_res.sr_slot = NULL;
7360 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7362 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7363 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7365 task = rpc_run_task(&task_setup_data);
7367 return ERR_CAST(task);
7368 status = nfs4_wait_for_completion_rpc_task(task);
7370 status = task->tk_status;
7371 trace_nfs4_layoutget(lgp->args.ctx,
7375 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7376 if (status == 0 && lgp->res.layoutp->len)
7377 lseg = pnfs_layout_process(lgp);
7379 dprintk("<-- %s status=%d\n", __func__, status);
7381 return ERR_PTR(status);
7386 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7388 struct nfs4_layoutreturn *lrp = calldata;
7390 dprintk("--> %s\n", __func__);
7391 nfs41_setup_sequence(lrp->clp->cl_session,
7392 &lrp->args.seq_args,
7397 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7399 struct nfs4_layoutreturn *lrp = calldata;
7400 struct nfs_server *server;
7402 dprintk("--> %s\n", __func__);
7404 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7407 server = NFS_SERVER(lrp->args.inode);
7408 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7409 rpc_restart_call_prepare(task);
7412 dprintk("<-- %s\n", __func__);
7415 static void nfs4_layoutreturn_release(void *calldata)
7417 struct nfs4_layoutreturn *lrp = calldata;
7418 struct pnfs_layout_hdr *lo = lrp->args.layout;
7420 dprintk("--> %s\n", __func__);
7421 spin_lock(&lo->plh_inode->i_lock);
7422 if (lrp->res.lrs_present)
7423 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7424 lo->plh_block_lgets--;
7425 spin_unlock(&lo->plh_inode->i_lock);
7426 pnfs_put_layout_hdr(lrp->args.layout);
7428 dprintk("<-- %s\n", __func__);
7431 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7432 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7433 .rpc_call_done = nfs4_layoutreturn_done,
7434 .rpc_release = nfs4_layoutreturn_release,
7437 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7439 struct rpc_task *task;
7440 struct rpc_message msg = {
7441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7442 .rpc_argp = &lrp->args,
7443 .rpc_resp = &lrp->res,
7444 .rpc_cred = lrp->cred,
7446 struct rpc_task_setup task_setup_data = {
7447 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7448 .rpc_message = &msg,
7449 .callback_ops = &nfs4_layoutreturn_call_ops,
7450 .callback_data = lrp,
7454 dprintk("--> %s\n", __func__);
7455 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7456 task = rpc_run_task(&task_setup_data);
7458 return PTR_ERR(task);
7459 status = task->tk_status;
7460 trace_nfs4_layoutreturn(lrp->args.inode, status);
7461 dprintk("<-- %s status=%d\n", __func__, status);
7467 * Retrieve the list of Data Server devices from the MDS.
7469 static int _nfs4_getdevicelist(struct nfs_server *server,
7470 const struct nfs_fh *fh,
7471 struct pnfs_devicelist *devlist)
7473 struct nfs4_getdevicelist_args args = {
7475 .layoutclass = server->pnfs_curr_ld->id,
7477 struct nfs4_getdevicelist_res res = {
7480 struct rpc_message msg = {
7481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7487 dprintk("--> %s\n", __func__);
7488 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7490 dprintk("<-- %s status=%d\n", __func__, status);
7494 int nfs4_proc_getdevicelist(struct nfs_server *server,
7495 const struct nfs_fh *fh,
7496 struct pnfs_devicelist *devlist)
7498 struct nfs4_exception exception = { };
7502 err = nfs4_handle_exception(server,
7503 _nfs4_getdevicelist(server, fh, devlist),
7505 } while (exception.retry);
7507 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7508 err, devlist->num_devs);
7512 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7515 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7516 struct pnfs_device *pdev,
7517 struct rpc_cred *cred)
7519 struct nfs4_getdeviceinfo_args args = {
7522 struct nfs4_getdeviceinfo_res res = {
7525 struct rpc_message msg = {
7526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7533 dprintk("--> %s\n", __func__);
7534 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7535 dprintk("<-- %s status=%d\n", __func__, status);
7540 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7541 struct pnfs_device *pdev,
7542 struct rpc_cred *cred)
7544 struct nfs4_exception exception = { };
7548 err = nfs4_handle_exception(server,
7549 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7551 } while (exception.retry);
7554 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7556 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7558 struct nfs4_layoutcommit_data *data = calldata;
7559 struct nfs_server *server = NFS_SERVER(data->args.inode);
7560 struct nfs4_session *session = nfs4_get_session(server);
7562 nfs41_setup_sequence(session,
7563 &data->args.seq_args,
7569 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7571 struct nfs4_layoutcommit_data *data = calldata;
7572 struct nfs_server *server = NFS_SERVER(data->args.inode);
7574 if (!nfs41_sequence_done(task, &data->res.seq_res))
7577 switch (task->tk_status) { /* Just ignore these failures */
7578 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7579 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7580 case -NFS4ERR_BADLAYOUT: /* no layout */
7581 case -NFS4ERR_GRACE: /* loca_recalim always false */
7582 task->tk_status = 0;
7585 nfs_post_op_update_inode_force_wcc(data->args.inode,
7589 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7590 rpc_restart_call_prepare(task);
7596 static void nfs4_layoutcommit_release(void *calldata)
7598 struct nfs4_layoutcommit_data *data = calldata;
7600 pnfs_cleanup_layoutcommit(data);
7601 put_rpccred(data->cred);
7605 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7606 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7607 .rpc_call_done = nfs4_layoutcommit_done,
7608 .rpc_release = nfs4_layoutcommit_release,
7612 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7614 struct rpc_message msg = {
7615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7616 .rpc_argp = &data->args,
7617 .rpc_resp = &data->res,
7618 .rpc_cred = data->cred,
7620 struct rpc_task_setup task_setup_data = {
7621 .task = &data->task,
7622 .rpc_client = NFS_CLIENT(data->args.inode),
7623 .rpc_message = &msg,
7624 .callback_ops = &nfs4_layoutcommit_ops,
7625 .callback_data = data,
7626 .flags = RPC_TASK_ASYNC,
7628 struct rpc_task *task;
7631 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7632 "lbw: %llu inode %lu\n",
7633 data->task.tk_pid, sync,
7634 data->args.lastbytewritten,
7635 data->args.inode->i_ino);
7637 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7638 task = rpc_run_task(&task_setup_data);
7640 return PTR_ERR(task);
7643 status = nfs4_wait_for_completion_rpc_task(task);
7646 status = task->tk_status;
7647 trace_nfs4_layoutcommit(data->args.inode, status);
7649 dprintk("%s: status %d\n", __func__, status);
7655 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7656 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7659 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7660 struct nfs_fsinfo *info,
7661 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7663 struct nfs41_secinfo_no_name_args args = {
7664 .style = SECINFO_STYLE_CURRENT_FH,
7666 struct nfs4_secinfo_res res = {
7669 struct rpc_message msg = {
7670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7674 struct rpc_clnt *clnt = server->client;
7675 struct rpc_cred *cred = NULL;
7678 if (use_integrity) {
7679 clnt = server->nfs_client->cl_rpcclient;
7680 cred = nfs4_get_clid_cred(server->nfs_client);
7681 msg.rpc_cred = cred;
7684 dprintk("--> %s\n", __func__);
7685 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7687 dprintk("<-- %s status=%d\n", __func__, status);
7696 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7697 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7699 struct nfs4_exception exception = { };
7702 /* first try using integrity protection */
7703 err = -NFS4ERR_WRONGSEC;
7705 /* try to use integrity protection with machine cred */
7706 if (_nfs4_is_integrity_protected(server->nfs_client))
7707 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7711 * if unable to use integrity protection, or SECINFO with
7712 * integrity protection returns NFS4ERR_WRONGSEC (which is
7713 * disallowed by spec, but exists in deployed servers) use
7714 * the current filesystem's rpc_client and the user cred.
7716 if (err == -NFS4ERR_WRONGSEC)
7717 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7722 case -NFS4ERR_WRONGSEC:
7723 case -NFS4ERR_NOTSUPP:
7726 err = nfs4_handle_exception(server, err, &exception);
7728 } while (exception.retry);
7734 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7735 struct nfs_fsinfo *info)
7739 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
7740 struct nfs4_secinfo_flavors *flavors;
7741 struct nfs4_secinfo4 *secinfo;
7744 page = alloc_page(GFP_KERNEL);
7750 flavors = page_address(page);
7751 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7754 * Fall back on "guess and check" method if
7755 * the server doesn't support SECINFO_NO_NAME
7757 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7758 err = nfs4_find_root_sec(server, fhandle, info);
7764 for (i = 0; i < flavors->num_flavors; i++) {
7765 secinfo = &flavors->flavors[i];
7767 switch (secinfo->flavor) {
7771 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7772 &secinfo->flavor_info);
7775 flavor = RPC_AUTH_MAXFLAVOR;
7779 if (flavor != RPC_AUTH_MAXFLAVOR) {
7780 err = nfs4_lookup_root_sec(server, fhandle,
7787 if (flavor == RPC_AUTH_MAXFLAVOR)
7798 static int _nfs41_test_stateid(struct nfs_server *server,
7799 nfs4_stateid *stateid,
7800 struct rpc_cred *cred)
7803 struct nfs41_test_stateid_args args = {
7806 struct nfs41_test_stateid_res res;
7807 struct rpc_message msg = {
7808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7813 struct rpc_clnt *rpc_client = server->client;
7815 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7818 dprintk("NFS call test_stateid %p\n", stateid);
7819 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7820 nfs4_set_sequence_privileged(&args.seq_args);
7821 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
7822 &args.seq_args, &res.seq_res);
7823 if (status != NFS_OK) {
7824 dprintk("NFS reply test_stateid: failed, %d\n", status);
7827 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
7832 * nfs41_test_stateid - perform a TEST_STATEID operation
7834 * @server: server / transport on which to perform the operation
7835 * @stateid: state ID to test
7838 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7839 * Otherwise a negative NFS4ERR value is returned if the operation
7840 * failed or the state ID is not currently valid.
7842 static int nfs41_test_stateid(struct nfs_server *server,
7843 nfs4_stateid *stateid,
7844 struct rpc_cred *cred)
7846 struct nfs4_exception exception = { };
7849 err = _nfs41_test_stateid(server, stateid, cred);
7850 if (err != -NFS4ERR_DELAY)
7852 nfs4_handle_exception(server, err, &exception);
7853 } while (exception.retry);
7857 struct nfs_free_stateid_data {
7858 struct nfs_server *server;
7859 struct nfs41_free_stateid_args args;
7860 struct nfs41_free_stateid_res res;
7863 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7865 struct nfs_free_stateid_data *data = calldata;
7866 nfs41_setup_sequence(nfs4_get_session(data->server),
7867 &data->args.seq_args,
7872 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7874 struct nfs_free_stateid_data *data = calldata;
7876 nfs41_sequence_done(task, &data->res.seq_res);
7878 switch (task->tk_status) {
7879 case -NFS4ERR_DELAY:
7880 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7881 rpc_restart_call_prepare(task);
7885 static void nfs41_free_stateid_release(void *calldata)
7890 static const struct rpc_call_ops nfs41_free_stateid_ops = {
7891 .rpc_call_prepare = nfs41_free_stateid_prepare,
7892 .rpc_call_done = nfs41_free_stateid_done,
7893 .rpc_release = nfs41_free_stateid_release,
7896 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7897 nfs4_stateid *stateid,
7898 struct rpc_cred *cred,
7901 struct rpc_message msg = {
7902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7905 struct rpc_task_setup task_setup = {
7906 .rpc_client = server->client,
7907 .rpc_message = &msg,
7908 .callback_ops = &nfs41_free_stateid_ops,
7909 .flags = RPC_TASK_ASYNC,
7911 struct nfs_free_stateid_data *data;
7913 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7914 &task_setup.rpc_client, &msg);
7916 dprintk("NFS call free_stateid %p\n", stateid);
7917 data = kmalloc(sizeof(*data), GFP_NOFS);
7919 return ERR_PTR(-ENOMEM);
7920 data->server = server;
7921 nfs4_stateid_copy(&data->args.stateid, stateid);
7923 task_setup.callback_data = data;
7925 msg.rpc_argp = &data->args;
7926 msg.rpc_resp = &data->res;
7927 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7929 nfs4_set_sequence_privileged(&data->args.seq_args);
7931 return rpc_run_task(&task_setup);
7935 * nfs41_free_stateid - perform a FREE_STATEID operation
7937 * @server: server / transport on which to perform the operation
7938 * @stateid: state ID to release
7941 * Returns NFS_OK if the server freed "stateid". Otherwise a
7942 * negative NFS4ERR value is returned.
7944 static int nfs41_free_stateid(struct nfs_server *server,
7945 nfs4_stateid *stateid,
7946 struct rpc_cred *cred)
7948 struct rpc_task *task;
7951 task = _nfs41_free_stateid(server, stateid, cred, true);
7953 return PTR_ERR(task);
7954 ret = rpc_wait_for_completion_task(task);
7956 ret = task->tk_status;
7961 static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7963 struct rpc_task *task;
7964 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
7966 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
7967 nfs4_free_lock_state(server, lsp);
7969 return PTR_ERR(task);
7974 static bool nfs41_match_stateid(const nfs4_stateid *s1,
7975 const nfs4_stateid *s2)
7977 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
7980 if (s1->seqid == s2->seqid)
7982 if (s1->seqid == 0 || s2->seqid == 0)
7988 #endif /* CONFIG_NFS_V4_1 */
7990 static bool nfs4_match_stateid(const nfs4_stateid *s1,
7991 const nfs4_stateid *s2)
7993 return nfs4_stateid_match(s1, s2);
7997 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7998 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7999 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8000 .recover_open = nfs4_open_reclaim,
8001 .recover_lock = nfs4_lock_reclaim,
8002 .establish_clid = nfs4_init_clientid,
8003 .detect_trunking = nfs40_discover_server_trunking,
8006 #if defined(CONFIG_NFS_V4_1)
8007 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8008 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8009 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8010 .recover_open = nfs4_open_reclaim,
8011 .recover_lock = nfs4_lock_reclaim,
8012 .establish_clid = nfs41_init_clientid,
8013 .reclaim_complete = nfs41_proc_reclaim_complete,
8014 .detect_trunking = nfs41_discover_server_trunking,
8016 #endif /* CONFIG_NFS_V4_1 */
8018 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8019 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8020 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8021 .recover_open = nfs4_open_expired,
8022 .recover_lock = nfs4_lock_expired,
8023 .establish_clid = nfs4_init_clientid,
8026 #if defined(CONFIG_NFS_V4_1)
8027 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8028 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8029 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8030 .recover_open = nfs41_open_expired,
8031 .recover_lock = nfs41_lock_expired,
8032 .establish_clid = nfs41_init_clientid,
8034 #endif /* CONFIG_NFS_V4_1 */
8036 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8037 .sched_state_renewal = nfs4_proc_async_renew,
8038 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8039 .renew_lease = nfs4_proc_renew,
8042 #if defined(CONFIG_NFS_V4_1)
8043 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8044 .sched_state_renewal = nfs41_proc_async_sequence,
8045 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8046 .renew_lease = nfs4_proc_sequence,
8050 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8051 .get_locations = _nfs40_proc_get_locations,
8054 #if defined(CONFIG_NFS_V4_1)
8055 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8056 .get_locations = _nfs41_proc_get_locations,
8058 #endif /* CONFIG_NFS_V4_1 */
8060 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8062 .init_caps = NFS_CAP_READDIRPLUS
8063 | NFS_CAP_ATOMIC_OPEN
8064 | NFS_CAP_CHANGE_ATTR
8065 | NFS_CAP_POSIX_LOCK,
8066 .init_client = nfs40_init_client,
8067 .shutdown_client = nfs40_shutdown_client,
8068 .match_stateid = nfs4_match_stateid,
8069 .find_root_sec = nfs4_find_root_sec,
8070 .free_lock_state = nfs4_release_lockowner,
8071 .call_sync_ops = &nfs40_call_sync_ops,
8072 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8073 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8074 .state_renewal_ops = &nfs40_state_renewal_ops,
8075 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8078 #if defined(CONFIG_NFS_V4_1)
8079 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8081 .init_caps = NFS_CAP_READDIRPLUS
8082 | NFS_CAP_ATOMIC_OPEN
8083 | NFS_CAP_CHANGE_ATTR
8084 | NFS_CAP_POSIX_LOCK
8085 | NFS_CAP_STATEID_NFSV41
8086 | NFS_CAP_ATOMIC_OPEN_V1,
8087 .init_client = nfs41_init_client,
8088 .shutdown_client = nfs41_shutdown_client,
8089 .match_stateid = nfs41_match_stateid,
8090 .find_root_sec = nfs41_find_root_sec,
8091 .free_lock_state = nfs41_free_lock_state,
8092 .call_sync_ops = &nfs41_call_sync_ops,
8093 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8094 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8095 .state_renewal_ops = &nfs41_state_renewal_ops,
8096 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8100 #if defined(CONFIG_NFS_V4_2)
8101 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8103 .init_caps = NFS_CAP_READDIRPLUS
8104 | NFS_CAP_ATOMIC_OPEN
8105 | NFS_CAP_CHANGE_ATTR
8106 | NFS_CAP_POSIX_LOCK
8107 | NFS_CAP_STATEID_NFSV41
8108 | NFS_CAP_ATOMIC_OPEN_V1,
8109 .init_client = nfs41_init_client,
8110 .shutdown_client = nfs41_shutdown_client,
8111 .match_stateid = nfs41_match_stateid,
8112 .find_root_sec = nfs41_find_root_sec,
8113 .free_lock_state = nfs41_free_lock_state,
8114 .call_sync_ops = &nfs41_call_sync_ops,
8115 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8116 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8117 .state_renewal_ops = &nfs41_state_renewal_ops,
8121 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8122 [0] = &nfs_v4_0_minor_ops,
8123 #if defined(CONFIG_NFS_V4_1)
8124 [1] = &nfs_v4_1_minor_ops,
8126 #if defined(CONFIG_NFS_V4_2)
8127 [2] = &nfs_v4_2_minor_ops,
8131 static const struct inode_operations nfs4_dir_inode_operations = {
8132 .create = nfs_create,
8133 .lookup = nfs_lookup,
8134 .atomic_open = nfs_atomic_open,
8136 .unlink = nfs_unlink,
8137 .symlink = nfs_symlink,
8141 .rename = nfs_rename,
8142 .permission = nfs_permission,
8143 .getattr = nfs_getattr,
8144 .setattr = nfs_setattr,
8145 .getxattr = generic_getxattr,
8146 .setxattr = generic_setxattr,
8147 .listxattr = generic_listxattr,
8148 .removexattr = generic_removexattr,
8151 static const struct inode_operations nfs4_file_inode_operations = {
8152 .permission = nfs_permission,
8153 .getattr = nfs_getattr,
8154 .setattr = nfs_setattr,
8155 .getxattr = generic_getxattr,
8156 .setxattr = generic_setxattr,
8157 .listxattr = generic_listxattr,
8158 .removexattr = generic_removexattr,
8161 const struct nfs_rpc_ops nfs_v4_clientops = {
8162 .version = 4, /* protocol version */
8163 .dentry_ops = &nfs4_dentry_operations,
8164 .dir_inode_ops = &nfs4_dir_inode_operations,
8165 .file_inode_ops = &nfs4_file_inode_operations,
8166 .file_ops = &nfs4_file_operations,
8167 .getroot = nfs4_proc_get_root,
8168 .submount = nfs4_submount,
8169 .try_mount = nfs4_try_mount,
8170 .getattr = nfs4_proc_getattr,
8171 .setattr = nfs4_proc_setattr,
8172 .lookup = nfs4_proc_lookup,
8173 .access = nfs4_proc_access,
8174 .readlink = nfs4_proc_readlink,
8175 .create = nfs4_proc_create,
8176 .remove = nfs4_proc_remove,
8177 .unlink_setup = nfs4_proc_unlink_setup,
8178 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8179 .unlink_done = nfs4_proc_unlink_done,
8180 .rename = nfs4_proc_rename,
8181 .rename_setup = nfs4_proc_rename_setup,
8182 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8183 .rename_done = nfs4_proc_rename_done,
8184 .link = nfs4_proc_link,
8185 .symlink = nfs4_proc_symlink,
8186 .mkdir = nfs4_proc_mkdir,
8187 .rmdir = nfs4_proc_remove,
8188 .readdir = nfs4_proc_readdir,
8189 .mknod = nfs4_proc_mknod,
8190 .statfs = nfs4_proc_statfs,
8191 .fsinfo = nfs4_proc_fsinfo,
8192 .pathconf = nfs4_proc_pathconf,
8193 .set_capabilities = nfs4_server_capabilities,
8194 .decode_dirent = nfs4_decode_dirent,
8195 .read_setup = nfs4_proc_read_setup,
8196 .read_pageio_init = pnfs_pageio_init_read,
8197 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
8198 .read_done = nfs4_read_done,
8199 .write_setup = nfs4_proc_write_setup,
8200 .write_pageio_init = pnfs_pageio_init_write,
8201 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
8202 .write_done = nfs4_write_done,
8203 .commit_setup = nfs4_proc_commit_setup,
8204 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8205 .commit_done = nfs4_commit_done,
8206 .lock = nfs4_proc_lock,
8207 .clear_acl_cache = nfs4_zap_acl_attr,
8208 .close_context = nfs4_close_context,
8209 .open_context = nfs4_atomic_open,
8210 .have_delegation = nfs4_have_delegation,
8211 .return_delegation = nfs4_inode_return_delegation,
8212 .alloc_client = nfs4_alloc_client,
8213 .init_client = nfs4_init_client,
8214 .free_client = nfs4_free_client,
8215 .create_server = nfs4_create_server,
8216 .clone_server = nfs_clone_server,
8219 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8220 .prefix = XATTR_NAME_NFSV4_ACL,
8221 .list = nfs4_xattr_list_nfs4_acl,
8222 .get = nfs4_xattr_get_nfs4_acl,
8223 .set = nfs4_xattr_set_nfs4_acl,
8226 const struct xattr_handler *nfs4_xattr_handlers[] = {
8227 &nfs4_xattr_nfs4_acl_handler,
8228 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8229 &nfs4_xattr_nfs4_label_handler,