4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 static int _nfs4_proc_open(struct nfs4_opendata *data);
78 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
79 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
80 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
81 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
82 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
84 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
88 #ifdef CONFIG_NFS_V4_1
89 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
95 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
96 static inline struct nfs4_label *
97 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
108 err = security_dentry_init_security(dentry, sattr->ia_mode,
109 &dentry->d_name, (void **)&label->label, &label->len);
116 nfs4_label_release_security(struct nfs4_label *label)
119 security_release_secctx(label->label, label->len);
121 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
124 return server->attr_bitmask;
126 return server->attr_bitmask_nl;
129 static inline struct nfs4_label *
130 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
131 struct iattr *sattr, struct nfs4_label *l)
134 nfs4_label_release_security(struct nfs4_label *label)
137 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138 { return server->attr_bitmask; }
141 /* Prevent leaks of NFSv4 errors into userland */
142 static int nfs4_map_errors(int err)
147 case -NFS4ERR_RESOURCE:
148 case -NFS4ERR_LAYOUTTRYLATER:
149 case -NFS4ERR_RECALLCONFLICT:
151 case -NFS4ERR_WRONGSEC:
152 case -NFS4ERR_WRONG_CRED:
154 case -NFS4ERR_BADOWNER:
155 case -NFS4ERR_BADNAME:
157 case -NFS4ERR_SHARE_DENIED:
159 case -NFS4ERR_MINOR_VERS_MISMATCH:
160 return -EPROTONOSUPPORT;
161 case -NFS4ERR_ACCESS:
163 case -NFS4ERR_FILE_OPEN:
166 dprintk("%s could not handle NFSv4 error %d\n",
174 * This is our standard bitmap for GETATTR requests.
176 const u32 nfs4_fattr_bitmap[3] = {
178 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_FILEID,
183 | FATTR4_WORD1_NUMLINKS
185 | FATTR4_WORD1_OWNER_GROUP
186 | FATTR4_WORD1_RAWDEV
187 | FATTR4_WORD1_SPACE_USED
188 | FATTR4_WORD1_TIME_ACCESS
189 | FATTR4_WORD1_TIME_METADATA
190 | FATTR4_WORD1_TIME_MODIFY,
191 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
196 static const u32 nfs4_pnfs_open_bitmap[3] = {
198 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_FILEID,
203 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
214 static const u32 nfs4_open_noattr_bitmap[3] = {
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
220 const u32 nfs4_statfs_bitmap[3] = {
221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
229 const u32 nfs4_pathconf_bitmap[3] = {
231 | FATTR4_WORD0_MAXNAME,
235 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
239 FATTR4_WORD1_TIME_DELTA
240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
244 const u32 nfs4_fs_locations_bitmap[3] = {
246 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
252 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
263 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
264 struct nfs4_readdir_arg *readdir)
269 readdir->cookie = cookie;
270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
286 start = p = kmap_atomic(*readdir->pages);
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
298 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
310 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
314 kunmap_atomic(start);
317 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
324 *timeout = NFS4_POLL_RETRY_MIN;
325 if (*timeout > NFS4_POLL_RETRY_MAX)
326 *timeout = NFS4_POLL_RETRY_MAX;
327 freezable_schedule_timeout_killable_unsafe(*timeout);
328 if (fatal_signal_pending(current))
334 /* This is the error handling routine for processes that are allowed
337 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
339 struct nfs_client *clp = server->nfs_client;
340 struct nfs4_state *state = exception->state;
341 struct inode *inode = exception->inode;
344 exception->retry = 0;
348 case -NFS4ERR_OPENMODE:
349 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
350 nfs4_inode_return_delegation(inode);
351 exception->retry = 1;
356 ret = nfs4_schedule_stateid_recovery(server, state);
359 goto wait_on_recovery;
360 case -NFS4ERR_DELEG_REVOKED:
361 case -NFS4ERR_ADMIN_REVOKED:
362 case -NFS4ERR_BAD_STATEID:
363 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
364 nfs_remove_bad_delegation(inode);
365 exception->retry = 1;
370 ret = nfs4_schedule_stateid_recovery(server, state);
373 goto wait_on_recovery;
374 case -NFS4ERR_EXPIRED:
376 ret = nfs4_schedule_stateid_recovery(server, state);
380 case -NFS4ERR_STALE_STATEID:
381 case -NFS4ERR_STALE_CLIENTID:
382 nfs4_schedule_lease_recovery(clp);
383 goto wait_on_recovery;
385 ret = nfs4_schedule_migration_recovery(server);
388 goto wait_on_recovery;
389 case -NFS4ERR_LEASE_MOVED:
390 nfs4_schedule_lease_moved_recovery(clp);
391 goto wait_on_recovery;
392 #if defined(CONFIG_NFS_V4_1)
393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
403 goto wait_on_recovery;
404 #endif /* defined(CONFIG_NFS_V4_1) */
405 case -NFS4ERR_FILE_OPEN:
406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
415 ret = nfs4_delay(server->client, &exception->timeout);
418 case -NFS4ERR_RETRY_UNCACHED_REP:
419 case -NFS4ERR_OLD_STATEID:
420 exception->retry = 1;
422 case -NFS4ERR_BADOWNER:
423 /* The following works around a Linux server bug! */
424 case -NFS4ERR_BADNAME:
425 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
426 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
427 exception->retry = 1;
428 printk(KERN_WARNING "NFS: v4 server %s "
429 "does not accept raw "
431 "Reenabling the idmapper.\n",
432 server->nfs_client->cl_hostname);
435 /* We failed to handle the error */
436 return nfs4_map_errors(ret);
438 ret = nfs4_wait_clnt_recover(clp);
439 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
442 exception->retry = 1;
447 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
448 * or 'false' otherwise.
450 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
452 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
454 if (flavor == RPC_AUTH_GSS_KRB5I ||
455 flavor == RPC_AUTH_GSS_KRB5P)
461 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
463 spin_lock(&clp->cl_lock);
464 if (time_before(clp->cl_last_renewal,timestamp))
465 clp->cl_last_renewal = timestamp;
466 spin_unlock(&clp->cl_lock);
469 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
471 do_renew_lease(server->nfs_client, timestamp);
474 struct nfs4_call_sync_data {
475 const struct nfs_server *seq_server;
476 struct nfs4_sequence_args *seq_args;
477 struct nfs4_sequence_res *seq_res;
480 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
481 struct nfs4_sequence_res *res, int cache_reply)
483 args->sa_slot = NULL;
484 args->sa_cache_this = cache_reply;
485 args->sa_privileged = 0;
490 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
492 args->sa_privileged = 1;
495 static int nfs40_setup_sequence(const struct nfs_server *server,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 struct rpc_task *task)
500 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
501 struct nfs4_slot *slot;
503 /* slot already allocated? */
504 if (res->sr_slot != NULL)
507 spin_lock(&tbl->slot_tbl_lock);
508 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
511 slot = nfs4_alloc_slot(tbl);
513 if (slot == ERR_PTR(-ENOMEM))
514 task->tk_timeout = HZ >> 2;
517 spin_unlock(&tbl->slot_tbl_lock);
519 args->sa_slot = slot;
523 rpc_call_start(task);
527 if (args->sa_privileged)
528 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
529 NULL, RPC_PRIORITY_PRIVILEGED);
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
536 static int nfs40_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
539 struct nfs4_slot *slot = res->sr_slot;
540 struct nfs4_slot_table *tbl;
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_MOVED:
1579 nfs4_schedule_migration_recovery(server);
1581 case -NFS4ERR_LEASE_MOVED:
1582 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1584 case -NFS4ERR_DELEG_REVOKED:
1585 case -NFS4ERR_ADMIN_REVOKED:
1586 case -NFS4ERR_BAD_STATEID:
1587 case -NFS4ERR_OPENMODE:
1588 nfs_inode_find_state_and_recover(state->inode,
1590 nfs4_schedule_stateid_recovery(server, state);
1592 case -NFS4ERR_DELAY:
1593 case -NFS4ERR_GRACE:
1594 set_bit(NFS_DELEGATED_STATE, &state->flags);
1598 case -NFS4ERR_DENIED:
1599 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1605 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1607 struct nfs_server *server = NFS_SERVER(state->inode);
1608 struct nfs4_opendata *opendata;
1611 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1612 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1613 if (IS_ERR(opendata))
1614 return PTR_ERR(opendata);
1615 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1616 err = nfs4_open_recover(opendata, state);
1617 nfs4_opendata_put(opendata);
1618 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1621 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1623 struct nfs4_opendata *data = calldata;
1625 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1626 &data->o_res.seq_res, task);
1629 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1631 struct nfs4_opendata *data = calldata;
1633 nfs40_sequence_done(task, &data->o_res.seq_res);
1635 data->rpc_status = task->tk_status;
1636 if (data->rpc_status == 0) {
1637 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1638 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1639 renew_lease(data->o_res.server, data->timestamp);
1644 static void nfs4_open_confirm_release(void *calldata)
1646 struct nfs4_opendata *data = calldata;
1647 struct nfs4_state *state = NULL;
1649 /* If this request hasn't been cancelled, do nothing */
1650 if (data->cancelled == 0)
1652 /* In case of error, no cleanup! */
1653 if (!data->rpc_done)
1655 state = nfs4_opendata_to_nfs4_state(data);
1657 nfs4_close_state(state, data->o_arg.fmode);
1659 nfs4_opendata_put(data);
1662 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1663 .rpc_call_prepare = nfs4_open_confirm_prepare,
1664 .rpc_call_done = nfs4_open_confirm_done,
1665 .rpc_release = nfs4_open_confirm_release,
1669 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1671 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1673 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1674 struct rpc_task *task;
1675 struct rpc_message msg = {
1676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1677 .rpc_argp = &data->c_arg,
1678 .rpc_resp = &data->c_res,
1679 .rpc_cred = data->owner->so_cred,
1681 struct rpc_task_setup task_setup_data = {
1682 .rpc_client = server->client,
1683 .rpc_message = &msg,
1684 .callback_ops = &nfs4_open_confirm_ops,
1685 .callback_data = data,
1686 .workqueue = nfsiod_workqueue,
1687 .flags = RPC_TASK_ASYNC,
1691 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
1692 kref_get(&data->kref);
1694 data->rpc_status = 0;
1695 data->timestamp = jiffies;
1696 task = rpc_run_task(&task_setup_data);
1698 return PTR_ERR(task);
1699 status = nfs4_wait_for_completion_rpc_task(task);
1701 data->cancelled = 1;
1704 status = data->rpc_status;
1709 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1711 struct nfs4_opendata *data = calldata;
1712 struct nfs4_state_owner *sp = data->owner;
1713 struct nfs_client *clp = sp->so_server->nfs_client;
1715 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1718 * Check if we still need to send an OPEN call, or if we can use
1719 * a delegation instead.
1721 if (data->state != NULL) {
1722 struct nfs_delegation *delegation;
1724 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1727 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1728 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1729 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1730 can_open_delegated(delegation, data->o_arg.fmode))
1731 goto unlock_no_action;
1734 /* Update client id. */
1735 data->o_arg.clientid = clp->cl_clientid;
1736 switch (data->o_arg.claim) {
1737 case NFS4_OPEN_CLAIM_PREVIOUS:
1738 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1739 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1740 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1741 case NFS4_OPEN_CLAIM_FH:
1742 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1743 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1745 data->timestamp = jiffies;
1746 if (nfs4_setup_sequence(data->o_arg.server,
1747 &data->o_arg.seq_args,
1748 &data->o_res.seq_res,
1750 nfs_release_seqid(data->o_arg.seqid);
1752 /* Set the create mode (note dependency on the session type) */
1753 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1754 if (data->o_arg.open_flags & O_EXCL) {
1755 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1756 if (nfs4_has_persistent_session(clp))
1757 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1758 else if (clp->cl_mvops->minor_version > 0)
1759 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1765 task->tk_action = NULL;
1767 nfs4_sequence_done(task, &data->o_res.seq_res);
1770 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1772 struct nfs4_opendata *data = calldata;
1774 data->rpc_status = task->tk_status;
1776 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1779 if (task->tk_status == 0) {
1780 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1781 switch (data->o_res.f_attr->mode & S_IFMT) {
1785 data->rpc_status = -ELOOP;
1788 data->rpc_status = -EISDIR;
1791 data->rpc_status = -ENOTDIR;
1794 renew_lease(data->o_res.server, data->timestamp);
1795 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1796 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1801 static void nfs4_open_release(void *calldata)
1803 struct nfs4_opendata *data = calldata;
1804 struct nfs4_state *state = NULL;
1806 /* If this request hasn't been cancelled, do nothing */
1807 if (data->cancelled == 0)
1809 /* In case of error, no cleanup! */
1810 if (data->rpc_status != 0 || !data->rpc_done)
1812 /* In case we need an open_confirm, no cleanup! */
1813 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1815 state = nfs4_opendata_to_nfs4_state(data);
1817 nfs4_close_state(state, data->o_arg.fmode);
1819 nfs4_opendata_put(data);
1822 static const struct rpc_call_ops nfs4_open_ops = {
1823 .rpc_call_prepare = nfs4_open_prepare,
1824 .rpc_call_done = nfs4_open_done,
1825 .rpc_release = nfs4_open_release,
1828 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1830 struct inode *dir = data->dir->d_inode;
1831 struct nfs_server *server = NFS_SERVER(dir);
1832 struct nfs_openargs *o_arg = &data->o_arg;
1833 struct nfs_openres *o_res = &data->o_res;
1834 struct rpc_task *task;
1835 struct rpc_message msg = {
1836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1839 .rpc_cred = data->owner->so_cred,
1841 struct rpc_task_setup task_setup_data = {
1842 .rpc_client = server->client,
1843 .rpc_message = &msg,
1844 .callback_ops = &nfs4_open_ops,
1845 .callback_data = data,
1846 .workqueue = nfsiod_workqueue,
1847 .flags = RPC_TASK_ASYNC,
1851 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1852 kref_get(&data->kref);
1854 data->rpc_status = 0;
1855 data->cancelled = 0;
1856 data->is_recover = 0;
1858 nfs4_set_sequence_privileged(&o_arg->seq_args);
1859 data->is_recover = 1;
1861 task = rpc_run_task(&task_setup_data);
1863 return PTR_ERR(task);
1864 status = nfs4_wait_for_completion_rpc_task(task);
1866 data->cancelled = 1;
1869 status = data->rpc_status;
1875 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1877 struct inode *dir = data->dir->d_inode;
1878 struct nfs_openres *o_res = &data->o_res;
1881 status = nfs4_run_open_task(data, 1);
1882 if (status != 0 || !data->rpc_done)
1885 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1887 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1888 status = _nfs4_proc_open_confirm(data);
1896 static int nfs4_opendata_access(struct rpc_cred *cred,
1897 struct nfs4_opendata *opendata,
1898 struct nfs4_state *state, fmode_t fmode,
1901 struct nfs_access_entry cache;
1904 /* access call failed or for some reason the server doesn't
1905 * support any access modes -- defer access call until later */
1906 if (opendata->o_res.access_supported == 0)
1910 /* don't check MAY_WRITE - a newly created file may not have
1911 * write mode bits, but POSIX allows the creating process to write.
1912 * use openflags to check for exec, because fmode won't
1913 * always have FMODE_EXEC set when file open for exec. */
1914 if (openflags & __FMODE_EXEC) {
1915 /* ONLY check for exec rights */
1917 } else if (fmode & FMODE_READ)
1921 cache.jiffies = jiffies;
1922 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1923 nfs_access_add_cache(state->inode, &cache);
1925 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1928 /* even though OPEN succeeded, access is denied. Close the file */
1929 nfs4_close_state(state, fmode);
1934 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1936 static int _nfs4_proc_open(struct nfs4_opendata *data)
1938 struct inode *dir = data->dir->d_inode;
1939 struct nfs_server *server = NFS_SERVER(dir);
1940 struct nfs_openargs *o_arg = &data->o_arg;
1941 struct nfs_openres *o_res = &data->o_res;
1944 status = nfs4_run_open_task(data, 0);
1945 if (!data->rpc_done)
1948 if (status == -NFS4ERR_BADNAME &&
1949 !(o_arg->open_flags & O_CREAT))
1954 nfs_fattr_map_and_free_names(server, &data->f_attr);
1956 if (o_arg->open_flags & O_CREAT) {
1957 update_changeattr(dir, &o_res->cinfo);
1958 if (o_arg->open_flags & O_EXCL)
1959 data->file_created = 1;
1960 else if (o_res->cinfo.before != o_res->cinfo.after)
1961 data->file_created = 1;
1963 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1964 server->caps &= ~NFS_CAP_POSIX_LOCK;
1965 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1966 status = _nfs4_proc_open_confirm(data);
1970 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1971 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
1975 static int nfs4_recover_expired_lease(struct nfs_server *server)
1977 return nfs4_client_recover_expired_lease(server->nfs_client);
1982 * reclaim state on the server after a network partition.
1983 * Assumes caller holds the appropriate lock
1985 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1987 struct nfs4_opendata *opendata;
1990 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1991 NFS4_OPEN_CLAIM_FH);
1992 if (IS_ERR(opendata))
1993 return PTR_ERR(opendata);
1994 ret = nfs4_open_recover(opendata, state);
1996 d_drop(ctx->dentry);
1997 nfs4_opendata_put(opendata);
2001 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2003 struct nfs_server *server = NFS_SERVER(state->inode);
2004 struct nfs4_exception exception = { };
2008 err = _nfs4_open_expired(ctx, state);
2009 trace_nfs4_open_expired(ctx, 0, err);
2010 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2015 case -NFS4ERR_GRACE:
2016 case -NFS4ERR_DELAY:
2017 nfs4_handle_exception(server, err, &exception);
2020 } while (exception.retry);
2025 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2027 struct nfs_open_context *ctx;
2030 ctx = nfs4_state_find_open_context(state);
2033 ret = nfs4_do_open_expired(ctx, state);
2034 put_nfs_open_context(ctx);
2038 #if defined(CONFIG_NFS_V4_1)
2039 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
2041 struct nfs_server *server = NFS_SERVER(state->inode);
2042 nfs4_stateid *stateid = &state->stateid;
2043 struct nfs_delegation *delegation;
2044 struct rpc_cred *cred = NULL;
2045 int status = -NFS4ERR_BAD_STATEID;
2047 /* If a state reset has been done, test_stateid is unneeded */
2048 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2051 /* Get the delegation credential for use by test/free_stateid */
2053 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2054 if (delegation != NULL &&
2055 nfs4_stateid_match(&delegation->stateid, stateid)) {
2056 cred = get_rpccred(delegation->cred);
2058 status = nfs41_test_stateid(server, stateid, cred);
2059 trace_nfs4_test_delegation_stateid(state, NULL, status);
2063 if (status != NFS_OK) {
2064 /* Free the stateid unless the server explicitly
2065 * informs us the stateid is unrecognized. */
2066 if (status != -NFS4ERR_BAD_STATEID)
2067 nfs41_free_stateid(server, stateid, cred);
2068 nfs_remove_bad_delegation(state->inode);
2070 write_seqlock(&state->seqlock);
2071 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2072 write_sequnlock(&state->seqlock);
2073 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2081 * nfs41_check_open_stateid - possibly free an open stateid
2083 * @state: NFSv4 state for an inode
2085 * Returns NFS_OK if recovery for this stateid is now finished.
2086 * Otherwise a negative NFS4ERR value is returned.
2088 static int nfs41_check_open_stateid(struct nfs4_state *state)
2090 struct nfs_server *server = NFS_SERVER(state->inode);
2091 nfs4_stateid *stateid = &state->open_stateid;
2092 struct rpc_cred *cred = state->owner->so_cred;
2095 /* If a state reset has been done, test_stateid is unneeded */
2096 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2097 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2098 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2099 return -NFS4ERR_BAD_STATEID;
2101 status = nfs41_test_stateid(server, stateid, cred);
2102 trace_nfs4_test_open_stateid(state, NULL, status);
2103 if (status != NFS_OK) {
2104 /* Free the stateid unless the server explicitly
2105 * informs us the stateid is unrecognized. */
2106 if (status != -NFS4ERR_BAD_STATEID)
2107 nfs41_free_stateid(server, stateid, cred);
2109 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2110 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2111 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2112 clear_bit(NFS_OPEN_STATE, &state->flags);
2117 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2121 nfs41_clear_delegation_stateid(state);
2122 status = nfs41_check_open_stateid(state);
2123 if (status != NFS_OK)
2124 status = nfs4_open_expired(sp, state);
2130 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2131 * fields corresponding to attributes that were used to store the verifier.
2132 * Make sure we clobber those fields in the later setattr call
2134 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2136 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2137 !(sattr->ia_valid & ATTR_ATIME_SET))
2138 sattr->ia_valid |= ATTR_ATIME;
2140 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2141 !(sattr->ia_valid & ATTR_MTIME_SET))
2142 sattr->ia_valid |= ATTR_MTIME;
2145 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2148 struct nfs_open_context *ctx)
2150 struct nfs4_state_owner *sp = opendata->owner;
2151 struct nfs_server *server = sp->so_server;
2152 struct dentry *dentry;
2153 struct nfs4_state *state;
2157 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2159 ret = _nfs4_proc_open(opendata);
2163 state = nfs4_opendata_to_nfs4_state(opendata);
2164 ret = PTR_ERR(state);
2167 if (server->caps & NFS_CAP_POSIX_LOCK)
2168 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2170 dentry = opendata->dentry;
2171 if (dentry->d_inode == NULL) {
2172 /* FIXME: Is this d_drop() ever needed? */
2174 dentry = d_add_unique(dentry, igrab(state->inode));
2175 if (dentry == NULL) {
2176 dentry = opendata->dentry;
2177 } else if (dentry != ctx->dentry) {
2179 ctx->dentry = dget(dentry);
2181 nfs_set_verifier(dentry,
2182 nfs_save_change_attribute(opendata->dir->d_inode));
2185 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2190 if (dentry->d_inode == state->inode) {
2191 nfs_inode_attach_open_context(ctx);
2192 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2193 nfs4_schedule_stateid_recovery(server, state);
2200 * Returns a referenced nfs4_state
2202 static int _nfs4_do_open(struct inode *dir,
2203 struct nfs_open_context *ctx,
2205 struct iattr *sattr,
2206 struct nfs4_label *label,
2209 struct nfs4_state_owner *sp;
2210 struct nfs4_state *state = NULL;
2211 struct nfs_server *server = NFS_SERVER(dir);
2212 struct nfs4_opendata *opendata;
2213 struct dentry *dentry = ctx->dentry;
2214 struct rpc_cred *cred = ctx->cred;
2215 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2216 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2217 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2218 struct nfs4_label *olabel = NULL;
2221 /* Protect against reboot recovery conflicts */
2223 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2225 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2228 status = nfs4_recover_expired_lease(server);
2230 goto err_put_state_owner;
2231 if (dentry->d_inode != NULL)
2232 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2234 if (dentry->d_inode)
2235 claim = NFS4_OPEN_CLAIM_FH;
2236 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2237 label, claim, GFP_KERNEL);
2238 if (opendata == NULL)
2239 goto err_put_state_owner;
2242 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2243 if (IS_ERR(olabel)) {
2244 status = PTR_ERR(olabel);
2245 goto err_opendata_put;
2249 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2250 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2251 if (!opendata->f_attr.mdsthreshold)
2252 goto err_free_label;
2253 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2255 if (dentry->d_inode != NULL)
2256 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2258 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2260 goto err_free_label;
2263 if ((opendata->o_arg.open_flags & O_EXCL) &&
2264 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2265 nfs4_exclusive_attrset(opendata, sattr);
2267 nfs_fattr_init(opendata->o_res.f_attr);
2268 status = nfs4_do_setattr(state->inode, cred,
2269 opendata->o_res.f_attr, sattr,
2270 state, label, olabel);
2272 nfs_setattr_update_inode(state->inode, sattr);
2273 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2274 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2277 if (opendata->file_created)
2278 *opened |= FILE_CREATED;
2280 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2281 *ctx_th = opendata->f_attr.mdsthreshold;
2283 kfree(opendata->f_attr.mdsthreshold);
2284 opendata->f_attr.mdsthreshold = NULL;
2286 nfs4_label_free(olabel);
2288 nfs4_opendata_put(opendata);
2289 nfs4_put_state_owner(sp);
2292 nfs4_label_free(olabel);
2294 kfree(opendata->f_attr.mdsthreshold);
2295 nfs4_opendata_put(opendata);
2296 err_put_state_owner:
2297 nfs4_put_state_owner(sp);
2303 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2304 struct nfs_open_context *ctx,
2306 struct iattr *sattr,
2307 struct nfs4_label *label,
2310 struct nfs_server *server = NFS_SERVER(dir);
2311 struct nfs4_exception exception = { };
2312 struct nfs4_state *res;
2316 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2318 trace_nfs4_open_file(ctx, flags, status);
2321 /* NOTE: BAD_SEQID means the server and client disagree about the
2322 * book-keeping w.r.t. state-changing operations
2323 * (OPEN/CLOSE/LOCK/LOCKU...)
2324 * It is actually a sign of a bug on the client or on the server.
2326 * If we receive a BAD_SEQID error in the particular case of
2327 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2328 * have unhashed the old state_owner for us, and that we can
2329 * therefore safely retry using a new one. We should still warn
2330 * the user though...
2332 if (status == -NFS4ERR_BAD_SEQID) {
2333 pr_warn_ratelimited("NFS: v4 server %s "
2334 " returned a bad sequence-id error!\n",
2335 NFS_SERVER(dir)->nfs_client->cl_hostname);
2336 exception.retry = 1;
2340 * BAD_STATEID on OPEN means that the server cancelled our
2341 * state before it received the OPEN_CONFIRM.
2342 * Recover by retrying the request as per the discussion
2343 * on Page 181 of RFC3530.
2345 if (status == -NFS4ERR_BAD_STATEID) {
2346 exception.retry = 1;
2349 if (status == -EAGAIN) {
2350 /* We must have found a delegation */
2351 exception.retry = 1;
2354 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2356 res = ERR_PTR(nfs4_handle_exception(server,
2357 status, &exception));
2358 } while (exception.retry);
2362 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2363 struct nfs_fattr *fattr, struct iattr *sattr,
2364 struct nfs4_state *state, struct nfs4_label *ilabel,
2365 struct nfs4_label *olabel)
2367 struct nfs_server *server = NFS_SERVER(inode);
2368 struct nfs_setattrargs arg = {
2369 .fh = NFS_FH(inode),
2372 .bitmask = server->attr_bitmask,
2375 struct nfs_setattrres res = {
2380 struct rpc_message msg = {
2381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2386 unsigned long timestamp = jiffies;
2391 arg.bitmask = nfs4_bitmask(server, ilabel);
2393 arg.bitmask = nfs4_bitmask(server, olabel);
2395 nfs_fattr_init(fattr);
2397 /* Servers should only apply open mode checks for file size changes */
2398 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2399 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2401 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2402 /* Use that stateid */
2403 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
2404 struct nfs_lockowner lockowner = {
2405 .l_owner = current->files,
2406 .l_pid = current->tgid,
2408 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2411 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2413 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2414 if (status == 0 && state != NULL)
2415 renew_lease(server, timestamp);
2419 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2420 struct nfs_fattr *fattr, struct iattr *sattr,
2421 struct nfs4_state *state, struct nfs4_label *ilabel,
2422 struct nfs4_label *olabel)
2424 struct nfs_server *server = NFS_SERVER(inode);
2425 struct nfs4_exception exception = {
2431 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2432 trace_nfs4_setattr(inode, err);
2434 case -NFS4ERR_OPENMODE:
2435 if (!(sattr->ia_valid & ATTR_SIZE)) {
2436 pr_warn_once("NFSv4: server %s is incorrectly "
2437 "applying open mode checks to "
2438 "a SETATTR that is not "
2439 "changing file size.\n",
2440 server->nfs_client->cl_hostname);
2442 if (state && !(state->state & FMODE_WRITE)) {
2444 if (sattr->ia_valid & ATTR_OPEN)
2449 err = nfs4_handle_exception(server, err, &exception);
2450 } while (exception.retry);
2455 struct nfs4_closedata {
2456 struct inode *inode;
2457 struct nfs4_state *state;
2458 struct nfs_closeargs arg;
2459 struct nfs_closeres res;
2460 struct nfs_fattr fattr;
2461 unsigned long timestamp;
2466 static void nfs4_free_closedata(void *data)
2468 struct nfs4_closedata *calldata = data;
2469 struct nfs4_state_owner *sp = calldata->state->owner;
2470 struct super_block *sb = calldata->state->inode->i_sb;
2473 pnfs_roc_release(calldata->state->inode);
2474 nfs4_put_open_state(calldata->state);
2475 nfs_free_seqid(calldata->arg.seqid);
2476 nfs4_put_state_owner(sp);
2477 nfs_sb_deactive(sb);
2481 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2484 spin_lock(&state->owner->so_lock);
2485 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2486 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2488 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2491 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2494 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2495 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2496 clear_bit(NFS_OPEN_STATE, &state->flags);
2498 spin_unlock(&state->owner->so_lock);
2501 static void nfs4_close_done(struct rpc_task *task, void *data)
2503 struct nfs4_closedata *calldata = data;
2504 struct nfs4_state *state = calldata->state;
2505 struct nfs_server *server = NFS_SERVER(calldata->inode);
2507 dprintk("%s: begin!\n", __func__);
2508 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2510 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2511 /* hmm. we are done with the inode, and in the process of freeing
2512 * the state_owner. we keep this around to process errors
2514 switch (task->tk_status) {
2517 pnfs_roc_set_barrier(state->inode,
2518 calldata->roc_barrier);
2519 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2520 renew_lease(server, calldata->timestamp);
2521 nfs4_close_clear_stateid_flags(state,
2522 calldata->arg.fmode);
2524 case -NFS4ERR_STALE_STATEID:
2525 case -NFS4ERR_OLD_STATEID:
2526 case -NFS4ERR_BAD_STATEID:
2527 case -NFS4ERR_EXPIRED:
2528 if (calldata->arg.fmode == 0)
2531 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2532 rpc_restart_call_prepare(task);
2534 nfs_release_seqid(calldata->arg.seqid);
2535 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2536 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2539 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2541 struct nfs4_closedata *calldata = data;
2542 struct nfs4_state *state = calldata->state;
2543 struct inode *inode = calldata->inode;
2546 dprintk("%s: begin!\n", __func__);
2547 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2550 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2551 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2552 spin_lock(&state->owner->so_lock);
2553 /* Calculate the change in open mode */
2554 if (state->n_rdwr == 0) {
2555 if (state->n_rdonly == 0) {
2556 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2557 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2558 calldata->arg.fmode &= ~FMODE_READ;
2560 if (state->n_wronly == 0) {
2561 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2562 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2563 calldata->arg.fmode &= ~FMODE_WRITE;
2566 if (!nfs4_valid_open_stateid(state))
2568 spin_unlock(&state->owner->so_lock);
2571 /* Note: exit _without_ calling nfs4_close_done */
2575 if (calldata->arg.fmode == 0) {
2576 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2577 if (calldata->roc &&
2578 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2579 nfs_release_seqid(calldata->arg.seqid);
2584 nfs_fattr_init(calldata->res.fattr);
2585 calldata->timestamp = jiffies;
2586 if (nfs4_setup_sequence(NFS_SERVER(inode),
2587 &calldata->arg.seq_args,
2588 &calldata->res.seq_res,
2590 nfs_release_seqid(calldata->arg.seqid);
2591 dprintk("%s: done!\n", __func__);
2594 task->tk_action = NULL;
2596 nfs4_sequence_done(task, &calldata->res.seq_res);
2599 static const struct rpc_call_ops nfs4_close_ops = {
2600 .rpc_call_prepare = nfs4_close_prepare,
2601 .rpc_call_done = nfs4_close_done,
2602 .rpc_release = nfs4_free_closedata,
2606 * It is possible for data to be read/written from a mem-mapped file
2607 * after the sys_close call (which hits the vfs layer as a flush).
2608 * This means that we can't safely call nfsv4 close on a file until
2609 * the inode is cleared. This in turn means that we are not good
2610 * NFSv4 citizens - we do not indicate to the server to update the file's
2611 * share state even when we are done with one of the three share
2612 * stateid's in the inode.
2614 * NOTE: Caller must be holding the sp->so_owner semaphore!
2616 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2618 struct nfs_server *server = NFS_SERVER(state->inode);
2619 struct nfs4_closedata *calldata;
2620 struct nfs4_state_owner *sp = state->owner;
2621 struct rpc_task *task;
2622 struct rpc_message msg = {
2623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2624 .rpc_cred = state->owner->so_cred,
2626 struct rpc_task_setup task_setup_data = {
2627 .rpc_client = server->client,
2628 .rpc_message = &msg,
2629 .callback_ops = &nfs4_close_ops,
2630 .workqueue = nfsiod_workqueue,
2631 .flags = RPC_TASK_ASYNC,
2633 int status = -ENOMEM;
2635 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2636 &task_setup_data.rpc_client, &msg);
2638 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2639 if (calldata == NULL)
2641 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2642 calldata->inode = state->inode;
2643 calldata->state = state;
2644 calldata->arg.fh = NFS_FH(state->inode);
2645 calldata->arg.stateid = &state->open_stateid;
2646 /* Serialization for the sequence id */
2647 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2648 if (calldata->arg.seqid == NULL)
2649 goto out_free_calldata;
2650 calldata->arg.fmode = 0;
2651 calldata->arg.bitmask = server->cache_consistency_bitmask;
2652 calldata->res.fattr = &calldata->fattr;
2653 calldata->res.seqid = calldata->arg.seqid;
2654 calldata->res.server = server;
2655 calldata->roc = pnfs_roc(state->inode);
2656 nfs_sb_active(calldata->inode->i_sb);
2658 msg.rpc_argp = &calldata->arg;
2659 msg.rpc_resp = &calldata->res;
2660 task_setup_data.callback_data = calldata;
2661 task = rpc_run_task(&task_setup_data);
2663 return PTR_ERR(task);
2666 status = rpc_wait_for_completion_task(task);
2672 nfs4_put_open_state(state);
2673 nfs4_put_state_owner(sp);
2677 static struct inode *
2678 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2679 int open_flags, struct iattr *attr, int *opened)
2681 struct nfs4_state *state;
2682 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2684 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2686 /* Protect against concurrent sillydeletes */
2687 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2689 nfs4_label_release_security(label);
2692 return ERR_CAST(state);
2693 return state->inode;
2696 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2698 if (ctx->state == NULL)
2701 nfs4_close_sync(ctx->state, ctx->mode);
2703 nfs4_close_state(ctx->state, ctx->mode);
2706 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2707 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2708 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_CHANGE_SECURITY_LABEL - 1UL)
2710 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2712 struct nfs4_server_caps_arg args = {
2715 struct nfs4_server_caps_res res = {};
2716 struct rpc_message msg = {
2717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2723 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2725 /* Sanity check the server answers */
2726 switch (server->nfs_client->cl_minorversion) {
2728 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2729 res.attr_bitmask[2] = 0;
2732 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2735 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2737 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2738 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2739 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2740 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2741 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2742 NFS_CAP_CTIME|NFS_CAP_MTIME|
2743 NFS_CAP_SECURITY_LABEL);
2744 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2745 server->caps |= NFS_CAP_ACLS;
2746 if (res.has_links != 0)
2747 server->caps |= NFS_CAP_HARDLINKS;
2748 if (res.has_symlinks != 0)
2749 server->caps |= NFS_CAP_SYMLINKS;
2750 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2751 server->caps |= NFS_CAP_FILEID;
2752 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2753 server->caps |= NFS_CAP_MODE;
2754 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2755 server->caps |= NFS_CAP_NLINK;
2756 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2757 server->caps |= NFS_CAP_OWNER;
2758 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2759 server->caps |= NFS_CAP_OWNER_GROUP;
2760 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2761 server->caps |= NFS_CAP_ATIME;
2762 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2763 server->caps |= NFS_CAP_CTIME;
2764 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2765 server->caps |= NFS_CAP_MTIME;
2766 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2767 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2768 server->caps |= NFS_CAP_SECURITY_LABEL;
2770 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2771 sizeof(server->attr_bitmask));
2772 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2774 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2775 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2776 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2777 server->cache_consistency_bitmask[2] = 0;
2778 server->acl_bitmask = res.acl_bitmask;
2779 server->fh_expire_type = res.fh_expire_type;
2785 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2787 struct nfs4_exception exception = { };
2790 err = nfs4_handle_exception(server,
2791 _nfs4_server_capabilities(server, fhandle),
2793 } while (exception.retry);
2797 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2798 struct nfs_fsinfo *info)
2801 struct nfs4_lookup_root_arg args = {
2804 struct nfs4_lookup_res res = {
2806 .fattr = info->fattr,
2809 struct rpc_message msg = {
2810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2815 bitmask[0] = nfs4_fattr_bitmap[0];
2816 bitmask[1] = nfs4_fattr_bitmap[1];
2818 * Process the label in the upcoming getfattr
2820 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2822 nfs_fattr_init(info->fattr);
2823 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2826 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2827 struct nfs_fsinfo *info)
2829 struct nfs4_exception exception = { };
2832 err = _nfs4_lookup_root(server, fhandle, info);
2833 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2836 case -NFS4ERR_WRONGSEC:
2839 err = nfs4_handle_exception(server, err, &exception);
2841 } while (exception.retry);
2846 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2847 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2849 struct rpc_auth_create_args auth_args = {
2850 .pseudoflavor = flavor,
2852 struct rpc_auth *auth;
2855 auth = rpcauth_create(&auth_args, server->client);
2860 ret = nfs4_lookup_root(server, fhandle, info);
2866 * Retry pseudoroot lookup with various security flavors. We do this when:
2868 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2869 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2871 * Returns zero on success, or a negative NFS4ERR value, or a
2872 * negative errno value.
2874 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2875 struct nfs_fsinfo *info)
2877 /* Per 3530bis 15.33.5 */
2878 static const rpc_authflavor_t flav_array[] = {
2882 RPC_AUTH_UNIX, /* courtesy */
2885 int status = -EPERM;
2888 if (server->auth_info.flavor_len > 0) {
2889 /* try each flavor specified by user */
2890 for (i = 0; i < server->auth_info.flavor_len; i++) {
2891 status = nfs4_lookup_root_sec(server, fhandle, info,
2892 server->auth_info.flavors[i]);
2893 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2898 /* no flavors specified by user, try default list */
2899 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2900 status = nfs4_lookup_root_sec(server, fhandle, info,
2902 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2909 * -EACCESS could mean that the user doesn't have correct permissions
2910 * to access the mount. It could also mean that we tried to mount
2911 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2912 * existing mount programs don't handle -EACCES very well so it should
2913 * be mapped to -EPERM instead.
2915 if (status == -EACCES)
2920 static int nfs4_do_find_root_sec(struct nfs_server *server,
2921 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2923 int mv = server->nfs_client->cl_minorversion;
2924 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2928 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2929 * @server: initialized nfs_server handle
2930 * @fhandle: we fill in the pseudo-fs root file handle
2931 * @info: we fill in an FSINFO struct
2932 * @auth_probe: probe the auth flavours
2934 * Returns zero on success, or a negative errno.
2936 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2937 struct nfs_fsinfo *info,
2942 switch (auth_probe) {
2944 status = nfs4_lookup_root(server, fhandle, info);
2945 if (status != -NFS4ERR_WRONGSEC)
2948 status = nfs4_do_find_root_sec(server, fhandle, info);
2952 status = nfs4_server_capabilities(server, fhandle);
2954 status = nfs4_do_fsinfo(server, fhandle, info);
2956 return nfs4_map_errors(status);
2959 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2960 struct nfs_fsinfo *info)
2963 struct nfs_fattr *fattr = info->fattr;
2964 struct nfs4_label *label = NULL;
2966 error = nfs4_server_capabilities(server, mntfh);
2968 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2972 label = nfs4_label_alloc(server, GFP_KERNEL);
2974 return PTR_ERR(label);
2976 error = nfs4_proc_getattr(server, mntfh, fattr, label);
2978 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2979 goto err_free_label;
2982 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2983 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2984 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2987 nfs4_label_free(label);
2993 * Get locations and (maybe) other attributes of a referral.
2994 * Note that we'll actually follow the referral later when
2995 * we detect fsid mismatch in inode revalidation
2997 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2998 const struct qstr *name, struct nfs_fattr *fattr,
2999 struct nfs_fh *fhandle)
3001 int status = -ENOMEM;
3002 struct page *page = NULL;
3003 struct nfs4_fs_locations *locations = NULL;
3005 page = alloc_page(GFP_KERNEL);
3008 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3009 if (locations == NULL)
3012 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3017 * If the fsid didn't change, this is a migration event, not a
3018 * referral. Cause us to drop into the exception handler, which
3019 * will kick off migration recovery.
3021 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3022 dprintk("%s: server did not return a different fsid for"
3023 " a referral at %s\n", __func__, name->name);
3024 status = -NFS4ERR_MOVED;
3027 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3028 nfs_fixup_referral_attributes(&locations->fattr);
3030 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3031 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3032 memset(fhandle, 0, sizeof(struct nfs_fh));
3040 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3041 struct nfs_fattr *fattr, struct nfs4_label *label)
3043 struct nfs4_getattr_arg args = {
3045 .bitmask = server->attr_bitmask,
3047 struct nfs4_getattr_res res = {
3052 struct rpc_message msg = {
3053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3058 args.bitmask = nfs4_bitmask(server, label);
3060 nfs_fattr_init(fattr);
3061 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3064 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3065 struct nfs_fattr *fattr, struct nfs4_label *label)
3067 struct nfs4_exception exception = { };
3070 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3071 trace_nfs4_getattr(server, fhandle, fattr, err);
3072 err = nfs4_handle_exception(server, err,
3074 } while (exception.retry);
3079 * The file is not closed if it is opened due to the a request to change
3080 * the size of the file. The open call will not be needed once the
3081 * VFS layer lookup-intents are implemented.
3083 * Close is called when the inode is destroyed.
3084 * If we haven't opened the file for O_WRONLY, we
3085 * need to in the size_change case to obtain a stateid.
3088 * Because OPEN is always done by name in nfsv4, it is
3089 * possible that we opened a different file by the same
3090 * name. We can recognize this race condition, but we
3091 * can't do anything about it besides returning an error.
3093 * This will be fixed with VFS changes (lookup-intent).
3096 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3097 struct iattr *sattr)
3099 struct inode *inode = dentry->d_inode;
3100 struct rpc_cred *cred = NULL;
3101 struct nfs4_state *state = NULL;
3102 struct nfs4_label *label = NULL;
3105 if (pnfs_ld_layoutret_on_setattr(inode))
3106 pnfs_commit_and_return_layout(inode);
3108 nfs_fattr_init(fattr);
3110 /* Deal with open(O_TRUNC) */
3111 if (sattr->ia_valid & ATTR_OPEN)
3112 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3114 /* Optimization: if the end result is no change, don't RPC */
3115 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3118 /* Search for an existing open(O_WRITE) file */
3119 if (sattr->ia_valid & ATTR_FILE) {
3120 struct nfs_open_context *ctx;
3122 ctx = nfs_file_open_context(sattr->ia_file);
3129 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3131 return PTR_ERR(label);
3133 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3135 nfs_setattr_update_inode(inode, sattr);
3136 nfs_setsecurity(inode, fattr, label);
3138 nfs4_label_free(label);
3142 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3143 const struct qstr *name, struct nfs_fh *fhandle,
3144 struct nfs_fattr *fattr, struct nfs4_label *label)
3146 struct nfs_server *server = NFS_SERVER(dir);
3148 struct nfs4_lookup_arg args = {
3149 .bitmask = server->attr_bitmask,
3150 .dir_fh = NFS_FH(dir),
3153 struct nfs4_lookup_res res = {
3159 struct rpc_message msg = {
3160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3165 args.bitmask = nfs4_bitmask(server, label);
3167 nfs_fattr_init(fattr);
3169 dprintk("NFS call lookup %s\n", name->name);
3170 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3171 dprintk("NFS reply lookup: %d\n", status);
3175 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3177 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3178 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3179 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3183 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3184 struct qstr *name, struct nfs_fh *fhandle,
3185 struct nfs_fattr *fattr, struct nfs4_label *label)
3187 struct nfs4_exception exception = { };
3188 struct rpc_clnt *client = *clnt;
3191 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3192 trace_nfs4_lookup(dir, name, err);
3194 case -NFS4ERR_BADNAME:
3197 case -NFS4ERR_MOVED:
3198 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3200 case -NFS4ERR_WRONGSEC:
3202 if (client != *clnt)
3204 client = nfs4_create_sec_client(client, dir, name);
3206 return PTR_ERR(client);
3208 exception.retry = 1;
3211 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3213 } while (exception.retry);
3218 else if (client != *clnt)
3219 rpc_shutdown_client(client);
3224 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3225 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3226 struct nfs4_label *label)
3229 struct rpc_clnt *client = NFS_CLIENT(dir);
3231 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3232 if (client != NFS_CLIENT(dir)) {
3233 rpc_shutdown_client(client);
3234 nfs_fixup_secinfo_attributes(fattr);
3240 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3241 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3243 struct rpc_clnt *client = NFS_CLIENT(dir);
3246 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3248 return ERR_PTR(status);
3249 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3252 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3254 struct nfs_server *server = NFS_SERVER(inode);
3255 struct nfs4_accessargs args = {
3256 .fh = NFS_FH(inode),
3257 .bitmask = server->cache_consistency_bitmask,
3259 struct nfs4_accessres res = {
3262 struct rpc_message msg = {
3263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3266 .rpc_cred = entry->cred,
3268 int mode = entry->mask;
3272 * Determine which access bits we want to ask for...
3274 if (mode & MAY_READ)
3275 args.access |= NFS4_ACCESS_READ;
3276 if (S_ISDIR(inode->i_mode)) {
3277 if (mode & MAY_WRITE)
3278 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3279 if (mode & MAY_EXEC)
3280 args.access |= NFS4_ACCESS_LOOKUP;
3282 if (mode & MAY_WRITE)
3283 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3284 if (mode & MAY_EXEC)
3285 args.access |= NFS4_ACCESS_EXECUTE;
3288 res.fattr = nfs_alloc_fattr();
3289 if (res.fattr == NULL)
3292 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3294 nfs_access_set_mask(entry, res.access);
3295 nfs_refresh_inode(inode, res.fattr);
3297 nfs_free_fattr(res.fattr);
3301 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3303 struct nfs4_exception exception = { };
3306 err = _nfs4_proc_access(inode, entry);
3307 trace_nfs4_access(inode, err);
3308 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3310 } while (exception.retry);
3315 * TODO: For the time being, we don't try to get any attributes
3316 * along with any of the zero-copy operations READ, READDIR,
3319 * In the case of the first three, we want to put the GETATTR
3320 * after the read-type operation -- this is because it is hard
3321 * to predict the length of a GETATTR response in v4, and thus
3322 * align the READ data correctly. This means that the GETATTR
3323 * may end up partially falling into the page cache, and we should
3324 * shift it into the 'tail' of the xdr_buf before processing.
3325 * To do this efficiently, we need to know the total length
3326 * of data received, which doesn't seem to be available outside
3329 * In the case of WRITE, we also want to put the GETATTR after
3330 * the operation -- in this case because we want to make sure
3331 * we get the post-operation mtime and size.
3333 * Both of these changes to the XDR layer would in fact be quite
3334 * minor, but I decided to leave them for a subsequent patch.
3336 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3337 unsigned int pgbase, unsigned int pglen)
3339 struct nfs4_readlink args = {
3340 .fh = NFS_FH(inode),
3345 struct nfs4_readlink_res res;
3346 struct rpc_message msg = {
3347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3352 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3355 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3356 unsigned int pgbase, unsigned int pglen)
3358 struct nfs4_exception exception = { };
3361 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3362 trace_nfs4_readlink(inode, err);
3363 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3365 } while (exception.retry);
3370 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3373 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3376 struct nfs4_label l, *ilabel = NULL;
3377 struct nfs_open_context *ctx;
3378 struct nfs4_state *state;
3382 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3384 return PTR_ERR(ctx);
3386 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3388 sattr->ia_mode &= ~current_umask();
3389 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3390 if (IS_ERR(state)) {
3391 status = PTR_ERR(state);
3395 nfs4_label_release_security(ilabel);
3396 put_nfs_open_context(ctx);
3400 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3402 struct nfs_server *server = NFS_SERVER(dir);
3403 struct nfs_removeargs args = {
3407 struct nfs_removeres res = {
3410 struct rpc_message msg = {
3411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3417 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3419 update_changeattr(dir, &res.cinfo);
3423 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3425 struct nfs4_exception exception = { };
3428 err = _nfs4_proc_remove(dir, name);
3429 trace_nfs4_remove(dir, name, err);
3430 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3432 } while (exception.retry);
3436 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3438 struct nfs_server *server = NFS_SERVER(dir);
3439 struct nfs_removeargs *args = msg->rpc_argp;
3440 struct nfs_removeres *res = msg->rpc_resp;
3442 res->server = server;
3443 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3444 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3446 nfs_fattr_init(res->dir_attr);
3449 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3451 nfs4_setup_sequence(NFS_SERVER(data->dir),
3452 &data->args.seq_args,
3457 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3459 struct nfs_unlinkdata *data = task->tk_calldata;
3460 struct nfs_removeres *res = &data->res;
3462 if (!nfs4_sequence_done(task, &res->seq_res))
3464 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3466 update_changeattr(dir, &res->cinfo);
3470 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3472 struct nfs_server *server = NFS_SERVER(dir);
3473 struct nfs_renameargs *arg = msg->rpc_argp;
3474 struct nfs_renameres *res = msg->rpc_resp;
3476 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3477 res->server = server;
3478 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3481 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3483 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3484 &data->args.seq_args,
3489 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3490 struct inode *new_dir)
3492 struct nfs_renamedata *data = task->tk_calldata;
3493 struct nfs_renameres *res = &data->res;
3495 if (!nfs4_sequence_done(task, &res->seq_res))
3497 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3500 update_changeattr(old_dir, &res->old_cinfo);
3501 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 nfs_server *server = NFS_SERVER(old_dir);
3509 struct nfs_renameargs arg = {
3510 .old_dir = NFS_FH(old_dir),
3511 .new_dir = NFS_FH(new_dir),
3512 .old_name = old_name,
3513 .new_name = new_name,
3515 struct nfs_renameres res = {
3518 struct rpc_message msg = {
3519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3523 int status = -ENOMEM;
3525 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3527 update_changeattr(old_dir, &res.old_cinfo);
3528 update_changeattr(new_dir, &res.new_cinfo);
3533 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3534 struct inode *new_dir, struct qstr *new_name)
3536 struct nfs4_exception exception = { };
3539 err = _nfs4_proc_rename(old_dir, old_name,
3541 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3542 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
3544 } while (exception.retry);
3548 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3550 struct nfs_server *server = NFS_SERVER(inode);
3551 struct nfs4_link_arg arg = {
3552 .fh = NFS_FH(inode),
3553 .dir_fh = NFS_FH(dir),
3555 .bitmask = server->attr_bitmask,
3557 struct nfs4_link_res res = {
3561 struct rpc_message msg = {
3562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3566 int status = -ENOMEM;
3568 res.fattr = nfs_alloc_fattr();
3569 if (res.fattr == NULL)
3572 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3573 if (IS_ERR(res.label)) {
3574 status = PTR_ERR(res.label);
3577 arg.bitmask = nfs4_bitmask(server, res.label);
3579 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3581 update_changeattr(dir, &res.cinfo);
3582 status = nfs_post_op_update_inode(inode, res.fattr);
3584 nfs_setsecurity(inode, res.fattr, res.label);
3588 nfs4_label_free(res.label);
3591 nfs_free_fattr(res.fattr);
3595 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3597 struct nfs4_exception exception = { };
3600 err = nfs4_handle_exception(NFS_SERVER(inode),
3601 _nfs4_proc_link(inode, dir, name),
3603 } while (exception.retry);
3607 struct nfs4_createdata {
3608 struct rpc_message msg;
3609 struct nfs4_create_arg arg;
3610 struct nfs4_create_res res;
3612 struct nfs_fattr fattr;
3613 struct nfs4_label *label;
3616 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3617 struct qstr *name, struct iattr *sattr, u32 ftype)
3619 struct nfs4_createdata *data;
3621 data = kzalloc(sizeof(*data), GFP_KERNEL);
3623 struct nfs_server *server = NFS_SERVER(dir);
3625 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3626 if (IS_ERR(data->label))
3629 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3630 data->msg.rpc_argp = &data->arg;
3631 data->msg.rpc_resp = &data->res;
3632 data->arg.dir_fh = NFS_FH(dir);
3633 data->arg.server = server;
3634 data->arg.name = name;
3635 data->arg.attrs = sattr;
3636 data->arg.ftype = ftype;
3637 data->arg.bitmask = nfs4_bitmask(server, data->label);
3638 data->res.server = server;
3639 data->res.fh = &data->fh;
3640 data->res.fattr = &data->fattr;
3641 data->res.label = data->label;
3642 nfs_fattr_init(data->res.fattr);
3650 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3652 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3653 &data->arg.seq_args, &data->res.seq_res, 1);
3655 update_changeattr(dir, &data->res.dir_cinfo);
3656 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3661 static void nfs4_free_createdata(struct nfs4_createdata *data)
3663 nfs4_label_free(data->label);
3667 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3668 struct page *page, unsigned int len, struct iattr *sattr,
3669 struct nfs4_label *label)
3671 struct nfs4_createdata *data;
3672 int status = -ENAMETOOLONG;
3674 if (len > NFS4_MAXPATHLEN)
3678 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3682 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3683 data->arg.u.symlink.pages = &page;
3684 data->arg.u.symlink.len = len;
3685 data->arg.label = label;
3687 status = nfs4_do_create(dir, dentry, data);
3689 nfs4_free_createdata(data);
3694 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3695 struct page *page, unsigned int len, struct iattr *sattr)
3697 struct nfs4_exception exception = { };
3698 struct nfs4_label l, *label = NULL;
3701 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3704 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3705 trace_nfs4_symlink(dir, &dentry->d_name, err);
3706 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3708 } while (exception.retry);
3710 nfs4_label_release_security(label);
3714 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3715 struct iattr *sattr, struct nfs4_label *label)
3717 struct nfs4_createdata *data;
3718 int status = -ENOMEM;
3720 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3724 data->arg.label = label;
3725 status = nfs4_do_create(dir, dentry, data);
3727 nfs4_free_createdata(data);
3732 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3733 struct iattr *sattr)
3735 struct nfs4_exception exception = { };
3736 struct nfs4_label l, *label = NULL;
3739 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3741 sattr->ia_mode &= ~current_umask();
3743 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3744 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3745 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3747 } while (exception.retry);
3748 nfs4_label_release_security(label);
3753 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3754 u64 cookie, struct page **pages, unsigned int count, int plus)
3756 struct inode *dir = dentry->d_inode;
3757 struct nfs4_readdir_arg args = {
3762 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3765 struct nfs4_readdir_res res;
3766 struct rpc_message msg = {
3767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3774 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3775 dentry->d_parent->d_name.name,
3776 dentry->d_name.name,
3777 (unsigned long long)cookie);
3778 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3779 res.pgbase = args.pgbase;
3780 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3782 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3783 status += args.pgbase;
3786 nfs_invalidate_atime(dir);
3788 dprintk("%s: returns %d\n", __func__, status);
3792 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3793 u64 cookie, struct page **pages, unsigned int count, int plus)
3795 struct nfs4_exception exception = { };
3798 err = _nfs4_proc_readdir(dentry, cred, cookie,
3799 pages, count, plus);
3800 trace_nfs4_readdir(dentry->d_inode, err);
3801 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3803 } while (exception.retry);
3807 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3808 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3810 struct nfs4_createdata *data;
3811 int mode = sattr->ia_mode;
3812 int status = -ENOMEM;
3814 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3819 data->arg.ftype = NF4FIFO;
3820 else if (S_ISBLK(mode)) {
3821 data->arg.ftype = NF4BLK;
3822 data->arg.u.device.specdata1 = MAJOR(rdev);
3823 data->arg.u.device.specdata2 = MINOR(rdev);
3825 else if (S_ISCHR(mode)) {
3826 data->arg.ftype = NF4CHR;
3827 data->arg.u.device.specdata1 = MAJOR(rdev);
3828 data->arg.u.device.specdata2 = MINOR(rdev);
3829 } else if (!S_ISSOCK(mode)) {
3834 data->arg.label = label;
3835 status = nfs4_do_create(dir, dentry, data);
3837 nfs4_free_createdata(data);
3842 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3843 struct iattr *sattr, dev_t rdev)
3845 struct nfs4_exception exception = { };
3846 struct nfs4_label l, *label = NULL;
3849 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3851 sattr->ia_mode &= ~current_umask();
3853 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3854 trace_nfs4_mknod(dir, &dentry->d_name, err);
3855 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3857 } while (exception.retry);
3859 nfs4_label_release_security(label);
3864 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3865 struct nfs_fsstat *fsstat)
3867 struct nfs4_statfs_arg args = {
3869 .bitmask = server->attr_bitmask,
3871 struct nfs4_statfs_res res = {
3874 struct rpc_message msg = {
3875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3880 nfs_fattr_init(fsstat->fattr);
3881 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3884 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3886 struct nfs4_exception exception = { };
3889 err = nfs4_handle_exception(server,
3890 _nfs4_proc_statfs(server, fhandle, fsstat),
3892 } while (exception.retry);
3896 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3897 struct nfs_fsinfo *fsinfo)
3899 struct nfs4_fsinfo_arg args = {
3901 .bitmask = server->attr_bitmask,
3903 struct nfs4_fsinfo_res res = {
3906 struct rpc_message msg = {
3907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3912 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3915 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3917 struct nfs4_exception exception = { };
3918 unsigned long now = jiffies;
3922 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3923 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
3925 struct nfs_client *clp = server->nfs_client;
3927 spin_lock(&clp->cl_lock);
3928 clp->cl_lease_time = fsinfo->lease_time * HZ;
3929 clp->cl_last_renewal = now;
3930 spin_unlock(&clp->cl_lock);
3933 err = nfs4_handle_exception(server, err, &exception);
3934 } while (exception.retry);
3938 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3942 nfs_fattr_init(fsinfo->fattr);
3943 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3945 /* block layout checks this! */
3946 server->pnfs_blksize = fsinfo->blksize;
3947 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3953 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3954 struct nfs_pathconf *pathconf)
3956 struct nfs4_pathconf_arg args = {
3958 .bitmask = server->attr_bitmask,
3960 struct nfs4_pathconf_res res = {
3961 .pathconf = pathconf,
3963 struct rpc_message msg = {
3964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3969 /* None of the pathconf attributes are mandatory to implement */
3970 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3971 memset(pathconf, 0, sizeof(*pathconf));
3975 nfs_fattr_init(pathconf->fattr);
3976 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3979 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3980 struct nfs_pathconf *pathconf)
3982 struct nfs4_exception exception = { };
3986 err = nfs4_handle_exception(server,
3987 _nfs4_proc_pathconf(server, fhandle, pathconf),
3989 } while (exception.retry);
3993 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
3994 const struct nfs_open_context *ctx,
3995 const struct nfs_lock_context *l_ctx,
3998 const struct nfs_lockowner *lockowner = NULL;
4001 lockowner = &l_ctx->lockowner;
4002 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4004 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4006 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4007 const struct nfs_open_context *ctx,
4008 const struct nfs_lock_context *l_ctx,
4011 nfs4_stateid current_stateid;
4013 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode))
4015 return nfs4_stateid_match(stateid, ¤t_stateid);
4018 static bool nfs4_error_stateid_expired(int err)
4021 case -NFS4ERR_DELEG_REVOKED:
4022 case -NFS4ERR_ADMIN_REVOKED:
4023 case -NFS4ERR_BAD_STATEID:
4024 case -NFS4ERR_STALE_STATEID:
4025 case -NFS4ERR_OLD_STATEID:
4026 case -NFS4ERR_OPENMODE:
4027 case -NFS4ERR_EXPIRED:
4033 void __nfs4_read_done_cb(struct nfs_read_data *data)
4035 nfs_invalidate_atime(data->header->inode);
4038 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
4040 struct nfs_server *server = NFS_SERVER(data->header->inode);
4042 trace_nfs4_read(data, task->tk_status);
4043 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
4044 rpc_restart_call_prepare(task);
4048 __nfs4_read_done_cb(data);
4049 if (task->tk_status > 0)
4050 renew_lease(server, data->timestamp);
4054 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4055 struct nfs_readargs *args)
4058 if (!nfs4_error_stateid_expired(task->tk_status) ||
4059 nfs4_stateid_is_current(&args->stateid,
4064 rpc_restart_call_prepare(task);
4068 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4071 dprintk("--> %s\n", __func__);
4073 if (!nfs4_sequence_done(task, &data->res.seq_res))
4075 if (nfs4_read_stateid_changed(task, &data->args))
4077 return data->read_done_cb ? data->read_done_cb(task, data) :
4078 nfs4_read_done_cb(task, data);
4081 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
4083 data->timestamp = jiffies;
4084 data->read_done_cb = nfs4_read_done_cb;
4085 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4086 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
4089 static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
4091 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4092 &data->args.seq_args,
4096 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4097 data->args.lock_context, FMODE_READ) == -EIO)
4099 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4104 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
4106 struct inode *inode = data->header->inode;
4108 trace_nfs4_write(data, task->tk_status);
4109 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
4110 rpc_restart_call_prepare(task);
4113 if (task->tk_status >= 0) {
4114 renew_lease(NFS_SERVER(inode), data->timestamp);
4115 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4120 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4121 struct nfs_writeargs *args)
4124 if (!nfs4_error_stateid_expired(task->tk_status) ||
4125 nfs4_stateid_is_current(&args->stateid,
4130 rpc_restart_call_prepare(task);
4134 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4136 if (!nfs4_sequence_done(task, &data->res.seq_res))
4138 if (nfs4_write_stateid_changed(task, &data->args))
4140 return data->write_done_cb ? data->write_done_cb(task, data) :
4141 nfs4_write_done_cb(task, data);
4145 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
4147 const struct nfs_pgio_header *hdr = data->header;
4149 /* Don't request attributes for pNFS or O_DIRECT writes */
4150 if (data->ds_clp != NULL || hdr->dreq != NULL)
4152 /* Otherwise, request attributes if and only if we don't hold
4155 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4158 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
4160 struct nfs_server *server = NFS_SERVER(data->header->inode);
4162 if (!nfs4_write_need_cache_consistency_data(data)) {
4163 data->args.bitmask = NULL;
4164 data->res.fattr = NULL;
4166 data->args.bitmask = server->cache_consistency_bitmask;
4168 if (!data->write_done_cb)
4169 data->write_done_cb = nfs4_write_done_cb;
4170 data->res.server = server;
4171 data->timestamp = jiffies;
4173 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4174 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4177 static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4179 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4180 &data->args.seq_args,
4184 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4185 data->args.lock_context, FMODE_WRITE) == -EIO)
4187 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4192 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4194 nfs4_setup_sequence(NFS_SERVER(data->inode),
4195 &data->args.seq_args,
4200 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4202 struct inode *inode = data->inode;
4204 trace_nfs4_commit(data, task->tk_status);
4205 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
4206 rpc_restart_call_prepare(task);
4212 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4214 if (!nfs4_sequence_done(task, &data->res.seq_res))
4216 return data->commit_done_cb(task, data);
4219 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4221 struct nfs_server *server = NFS_SERVER(data->inode);
4223 if (data->commit_done_cb == NULL)
4224 data->commit_done_cb = nfs4_commit_done_cb;
4225 data->res.server = server;
4226 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4227 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4230 struct nfs4_renewdata {
4231 struct nfs_client *client;
4232 unsigned long timestamp;
4236 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4237 * standalone procedure for queueing an asynchronous RENEW.
4239 static void nfs4_renew_release(void *calldata)
4241 struct nfs4_renewdata *data = calldata;
4242 struct nfs_client *clp = data->client;
4244 if (atomic_read(&clp->cl_count) > 1)
4245 nfs4_schedule_state_renewal(clp);
4246 nfs_put_client(clp);
4250 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4252 struct nfs4_renewdata *data = calldata;
4253 struct nfs_client *clp = data->client;
4254 unsigned long timestamp = data->timestamp;
4256 trace_nfs4_renew_async(clp, task->tk_status);
4257 switch (task->tk_status) {
4260 case -NFS4ERR_LEASE_MOVED:
4261 nfs4_schedule_lease_moved_recovery(clp);
4264 /* Unless we're shutting down, schedule state recovery! */
4265 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4267 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4268 nfs4_schedule_lease_recovery(clp);
4271 nfs4_schedule_path_down_recovery(clp);
4273 do_renew_lease(clp, timestamp);
4276 static const struct rpc_call_ops nfs4_renew_ops = {
4277 .rpc_call_done = nfs4_renew_done,
4278 .rpc_release = nfs4_renew_release,
4281 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4283 struct rpc_message msg = {
4284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4288 struct nfs4_renewdata *data;
4290 if (renew_flags == 0)
4292 if (!atomic_inc_not_zero(&clp->cl_count))
4294 data = kmalloc(sizeof(*data), GFP_NOFS);
4298 data->timestamp = jiffies;
4299 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4300 &nfs4_renew_ops, data);
4303 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4305 struct rpc_message msg = {
4306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4310 unsigned long now = jiffies;
4313 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4316 do_renew_lease(clp, now);
4320 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4322 return (server->caps & NFS_CAP_ACLS)
4323 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4324 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4327 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4328 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4331 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4333 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4334 struct page **pages, unsigned int *pgbase)
4336 struct page *newpage, **spages;
4342 len = min_t(size_t, PAGE_SIZE, buflen);
4343 newpage = alloc_page(GFP_KERNEL);
4345 if (newpage == NULL)
4347 memcpy(page_address(newpage), buf, len);
4352 } while (buflen != 0);
4358 __free_page(spages[rc-1]);
4362 struct nfs4_cached_acl {
4368 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4370 struct nfs_inode *nfsi = NFS_I(inode);
4372 spin_lock(&inode->i_lock);
4373 kfree(nfsi->nfs4_acl);
4374 nfsi->nfs4_acl = acl;
4375 spin_unlock(&inode->i_lock);
4378 static void nfs4_zap_acl_attr(struct inode *inode)
4380 nfs4_set_cached_acl(inode, NULL);
4383 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4385 struct nfs_inode *nfsi = NFS_I(inode);
4386 struct nfs4_cached_acl *acl;
4389 spin_lock(&inode->i_lock);
4390 acl = nfsi->nfs4_acl;
4393 if (buf == NULL) /* user is just asking for length */
4395 if (acl->cached == 0)
4397 ret = -ERANGE; /* see getxattr(2) man page */
4398 if (acl->len > buflen)
4400 memcpy(buf, acl->data, acl->len);
4404 spin_unlock(&inode->i_lock);
4408 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4410 struct nfs4_cached_acl *acl;
4411 size_t buflen = sizeof(*acl) + acl_len;
4413 if (buflen <= PAGE_SIZE) {
4414 acl = kmalloc(buflen, GFP_KERNEL);
4418 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4420 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4427 nfs4_set_cached_acl(inode, acl);
4431 * The getxattr API returns the required buffer length when called with a
4432 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4433 * the required buf. On a NULL buf, we send a page of data to the server
4434 * guessing that the ACL request can be serviced by a page. If so, we cache
4435 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4436 * the cache. If not so, we throw away the page, and cache the required
4437 * length. The next getxattr call will then produce another round trip to
4438 * the server, this time with the input buf of the required size.
4440 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4442 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4443 struct nfs_getaclargs args = {
4444 .fh = NFS_FH(inode),
4448 struct nfs_getaclres res = {
4451 struct rpc_message msg = {
4452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4456 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4457 int ret = -ENOMEM, i;
4459 /* As long as we're doing a round trip to the server anyway,
4460 * let's be prepared for a page of acl data. */
4463 if (npages > ARRAY_SIZE(pages))
4466 for (i = 0; i < npages; i++) {
4467 pages[i] = alloc_page(GFP_KERNEL);
4472 /* for decoding across pages */
4473 res.acl_scratch = alloc_page(GFP_KERNEL);
4474 if (!res.acl_scratch)
4477 args.acl_len = npages * PAGE_SIZE;
4478 args.acl_pgbase = 0;
4480 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4481 __func__, buf, buflen, npages, args.acl_len);
4482 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4483 &msg, &args.seq_args, &res.seq_res, 0);
4487 /* Handle the case where the passed-in buffer is too short */
4488 if (res.acl_flags & NFS4_ACL_TRUNC) {
4489 /* Did the user only issue a request for the acl length? */
4495 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4497 if (res.acl_len > buflen) {
4501 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4506 for (i = 0; i < npages; i++)
4508 __free_page(pages[i]);
4509 if (res.acl_scratch)
4510 __free_page(res.acl_scratch);
4514 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4516 struct nfs4_exception exception = { };
4519 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4520 trace_nfs4_get_acl(inode, ret);
4523 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4524 } while (exception.retry);
4528 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4530 struct nfs_server *server = NFS_SERVER(inode);
4533 if (!nfs4_server_supports_acls(server))
4535 ret = nfs_revalidate_inode(server, inode);
4538 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4539 nfs_zap_acl_cache(inode);
4540 ret = nfs4_read_cached_acl(inode, buf, buflen);
4542 /* -ENOENT is returned if there is no ACL or if there is an ACL
4543 * but no cached acl data, just the acl length */
4545 return nfs4_get_acl_uncached(inode, buf, buflen);
4548 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4550 struct nfs_server *server = NFS_SERVER(inode);
4551 struct page *pages[NFS4ACL_MAXPAGES];
4552 struct nfs_setaclargs arg = {
4553 .fh = NFS_FH(inode),
4557 struct nfs_setaclres res;
4558 struct rpc_message msg = {
4559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4563 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4566 if (!nfs4_server_supports_acls(server))
4568 if (npages > ARRAY_SIZE(pages))
4570 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4573 nfs4_inode_return_delegation(inode);
4574 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4577 * Free each page after tx, so the only ref left is
4578 * held by the network stack
4581 put_page(pages[i-1]);
4584 * Acl update can result in inode attribute update.
4585 * so mark the attribute cache invalid.
4587 spin_lock(&inode->i_lock);
4588 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4589 spin_unlock(&inode->i_lock);
4590 nfs_access_zap_cache(inode);
4591 nfs_zap_acl_cache(inode);
4595 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4597 struct nfs4_exception exception = { };
4600 err = __nfs4_proc_set_acl(inode, buf, buflen);
4601 trace_nfs4_set_acl(inode, err);
4602 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4604 } while (exception.retry);
4608 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4609 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4612 struct nfs_server *server = NFS_SERVER(inode);
4613 struct nfs_fattr fattr;
4614 struct nfs4_label label = {0, 0, buflen, buf};
4616 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4617 struct nfs4_getattr_arg arg = {
4618 .fh = NFS_FH(inode),
4621 struct nfs4_getattr_res res = {
4626 struct rpc_message msg = {
4627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4633 nfs_fattr_init(&fattr);
4635 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4638 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4640 if (buflen < label.len)
4645 static int nfs4_get_security_label(struct inode *inode, void *buf,
4648 struct nfs4_exception exception = { };
4651 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4655 err = _nfs4_get_security_label(inode, buf, buflen);
4656 trace_nfs4_get_security_label(inode, err);
4657 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4659 } while (exception.retry);
4663 static int _nfs4_do_set_security_label(struct inode *inode,
4664 struct nfs4_label *ilabel,
4665 struct nfs_fattr *fattr,
4666 struct nfs4_label *olabel)
4669 struct iattr sattr = {0};
4670 struct nfs_server *server = NFS_SERVER(inode);
4671 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4672 struct nfs_setattrargs arg = {
4673 .fh = NFS_FH(inode),
4679 struct nfs_setattrres res = {
4684 struct rpc_message msg = {
4685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4691 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4693 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4695 dprintk("%s failed: %d\n", __func__, status);
4700 static int nfs4_do_set_security_label(struct inode *inode,
4701 struct nfs4_label *ilabel,
4702 struct nfs_fattr *fattr,
4703 struct nfs4_label *olabel)
4705 struct nfs4_exception exception = { };
4709 err = _nfs4_do_set_security_label(inode, ilabel,
4711 trace_nfs4_set_security_label(inode, err);
4712 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4714 } while (exception.retry);
4719 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4721 struct nfs4_label ilabel, *olabel = NULL;
4722 struct nfs_fattr fattr;
4723 struct rpc_cred *cred;
4724 struct inode *inode = dentry->d_inode;
4727 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4730 nfs_fattr_init(&fattr);
4734 ilabel.label = (char *)buf;
4735 ilabel.len = buflen;
4737 cred = rpc_lookup_cred();
4739 return PTR_ERR(cred);
4741 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4742 if (IS_ERR(olabel)) {
4743 status = -PTR_ERR(olabel);
4747 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4749 nfs_setsecurity(inode, &fattr, olabel);
4751 nfs4_label_free(olabel);
4756 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4760 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4762 struct nfs_client *clp = server->nfs_client;
4764 if (task->tk_status >= 0)
4766 switch(task->tk_status) {
4767 case -NFS4ERR_DELEG_REVOKED:
4768 case -NFS4ERR_ADMIN_REVOKED:
4769 case -NFS4ERR_BAD_STATEID:
4772 nfs_remove_bad_delegation(state->inode);
4773 case -NFS4ERR_OPENMODE:
4776 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4777 goto recovery_failed;
4778 goto wait_on_recovery;
4779 case -NFS4ERR_EXPIRED:
4780 if (state != NULL) {
4781 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4782 goto recovery_failed;
4784 case -NFS4ERR_STALE_STATEID:
4785 case -NFS4ERR_STALE_CLIENTID:
4786 nfs4_schedule_lease_recovery(clp);
4787 goto wait_on_recovery;
4788 case -NFS4ERR_MOVED:
4789 if (nfs4_schedule_migration_recovery(server) < 0)
4790 goto recovery_failed;
4791 goto wait_on_recovery;
4792 case -NFS4ERR_LEASE_MOVED:
4793 nfs4_schedule_lease_moved_recovery(clp);
4794 goto wait_on_recovery;
4795 #if defined(CONFIG_NFS_V4_1)
4796 case -NFS4ERR_BADSESSION:
4797 case -NFS4ERR_BADSLOT:
4798 case -NFS4ERR_BAD_HIGH_SLOT:
4799 case -NFS4ERR_DEADSESSION:
4800 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4801 case -NFS4ERR_SEQ_FALSE_RETRY:
4802 case -NFS4ERR_SEQ_MISORDERED:
4803 dprintk("%s ERROR %d, Reset session\n", __func__,
4805 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4807 #endif /* CONFIG_NFS_V4_1 */
4808 case -NFS4ERR_DELAY:
4809 nfs_inc_server_stats(server, NFSIOS_DELAY);
4810 case -NFS4ERR_GRACE:
4811 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4812 case -NFS4ERR_RETRY_UNCACHED_REP:
4813 case -NFS4ERR_OLD_STATEID:
4816 task->tk_status = nfs4_map_errors(task->tk_status);
4819 task->tk_status = -EIO;
4822 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4823 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4824 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4825 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4826 goto recovery_failed;
4828 task->tk_status = 0;
4832 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4833 nfs4_verifier *bootverf)
4837 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4838 /* An impossible timestamp guarantees this value
4839 * will never match a generated boot time. */
4841 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4843 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4844 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4845 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4847 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4851 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4852 char *buf, size_t len)
4854 unsigned int result;
4857 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4859 rpc_peeraddr2str(clp->cl_rpcclient,
4861 rpc_peeraddr2str(clp->cl_rpcclient,
4862 RPC_DISPLAY_PROTO));
4868 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4869 char *buf, size_t len)
4871 const char *nodename = clp->cl_rpcclient->cl_nodename;
4873 if (nfs4_client_id_uniquifier[0] != '\0')
4874 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4875 clp->rpc_ops->version,
4876 clp->cl_minorversion,
4877 nfs4_client_id_uniquifier,
4879 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4880 clp->rpc_ops->version, clp->cl_minorversion,
4885 * nfs4_proc_setclientid - Negotiate client ID
4886 * @clp: state data structure
4887 * @program: RPC program for NFSv4 callback service
4888 * @port: IP port number for NFS4 callback service
4889 * @cred: RPC credential to use for this call
4890 * @res: where to place the result
4892 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4894 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4895 unsigned short port, struct rpc_cred *cred,
4896 struct nfs4_setclientid_res *res)
4898 nfs4_verifier sc_verifier;
4899 struct nfs4_setclientid setclientid = {
4900 .sc_verifier = &sc_verifier,
4902 .sc_cb_ident = clp->cl_cb_ident,
4904 struct rpc_message msg = {
4905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4906 .rpc_argp = &setclientid,
4912 /* nfs_client_id4 */
4913 nfs4_init_boot_verifier(clp, &sc_verifier);
4914 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4915 setclientid.sc_name_len =
4916 nfs4_init_uniform_client_string(clp,
4917 setclientid.sc_name,
4918 sizeof(setclientid.sc_name));
4920 setclientid.sc_name_len =
4921 nfs4_init_nonuniform_client_string(clp,
4922 setclientid.sc_name,
4923 sizeof(setclientid.sc_name));
4926 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4927 sizeof(setclientid.sc_netid), "%s",
4928 rpc_peeraddr2str(clp->cl_rpcclient,
4929 RPC_DISPLAY_NETID));
4931 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4932 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4933 clp->cl_ipaddr, port >> 8, port & 255);
4935 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4936 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4937 setclientid.sc_name_len, setclientid.sc_name);
4938 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4939 trace_nfs4_setclientid(clp, status);
4940 dprintk("NFS reply setclientid: %d\n", status);
4945 * nfs4_proc_setclientid_confirm - Confirm client ID
4946 * @clp: state data structure
4947 * @res: result of a previous SETCLIENTID
4948 * @cred: RPC credential to use for this call
4950 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4952 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4953 struct nfs4_setclientid_res *arg,
4954 struct rpc_cred *cred)
4956 struct rpc_message msg = {
4957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4963 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4964 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4966 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4967 trace_nfs4_setclientid_confirm(clp, status);
4968 dprintk("NFS reply setclientid_confirm: %d\n", status);
4972 struct nfs4_delegreturndata {
4973 struct nfs4_delegreturnargs args;
4974 struct nfs4_delegreturnres res;
4976 nfs4_stateid stateid;
4977 unsigned long timestamp;
4978 struct nfs_fattr fattr;
4982 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4984 struct nfs4_delegreturndata *data = calldata;
4986 if (!nfs4_sequence_done(task, &data->res.seq_res))
4989 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
4990 switch (task->tk_status) {
4991 case -NFS4ERR_STALE_STATEID:
4992 case -NFS4ERR_EXPIRED:
4994 renew_lease(data->res.server, data->timestamp);
4997 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4999 rpc_restart_call_prepare(task);
5003 data->rpc_status = task->tk_status;
5006 static void nfs4_delegreturn_release(void *calldata)
5011 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5013 struct nfs4_delegreturndata *d_data;
5015 d_data = (struct nfs4_delegreturndata *)data;
5017 nfs4_setup_sequence(d_data->res.server,
5018 &d_data->args.seq_args,
5019 &d_data->res.seq_res,
5023 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5024 .rpc_call_prepare = nfs4_delegreturn_prepare,
5025 .rpc_call_done = nfs4_delegreturn_done,
5026 .rpc_release = nfs4_delegreturn_release,
5029 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5031 struct nfs4_delegreturndata *data;
5032 struct nfs_server *server = NFS_SERVER(inode);
5033 struct rpc_task *task;
5034 struct rpc_message msg = {
5035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5038 struct rpc_task_setup task_setup_data = {
5039 .rpc_client = server->client,
5040 .rpc_message = &msg,
5041 .callback_ops = &nfs4_delegreturn_ops,
5042 .flags = RPC_TASK_ASYNC,
5046 data = kzalloc(sizeof(*data), GFP_NOFS);
5049 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5050 data->args.fhandle = &data->fh;
5051 data->args.stateid = &data->stateid;
5052 data->args.bitmask = server->cache_consistency_bitmask;
5053 nfs_copy_fh(&data->fh, NFS_FH(inode));
5054 nfs4_stateid_copy(&data->stateid, stateid);
5055 data->res.fattr = &data->fattr;
5056 data->res.server = server;
5057 nfs_fattr_init(data->res.fattr);
5058 data->timestamp = jiffies;
5059 data->rpc_status = 0;
5061 task_setup_data.callback_data = data;
5062 msg.rpc_argp = &data->args;
5063 msg.rpc_resp = &data->res;
5064 task = rpc_run_task(&task_setup_data);
5066 return PTR_ERR(task);
5069 status = nfs4_wait_for_completion_rpc_task(task);
5072 status = data->rpc_status;
5074 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5076 nfs_refresh_inode(inode, &data->fattr);
5082 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5084 struct nfs_server *server = NFS_SERVER(inode);
5085 struct nfs4_exception exception = { };
5088 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5089 trace_nfs4_delegreturn(inode, err);
5091 case -NFS4ERR_STALE_STATEID:
5092 case -NFS4ERR_EXPIRED:
5096 err = nfs4_handle_exception(server, err, &exception);
5097 } while (exception.retry);
5101 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5102 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5105 * sleep, with exponential backoff, and retry the LOCK operation.
5107 static unsigned long
5108 nfs4_set_lock_task_retry(unsigned long timeout)
5110 freezable_schedule_timeout_killable_unsafe(timeout);
5112 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5113 return NFS4_LOCK_MAXTIMEOUT;
5117 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5119 struct inode *inode = state->inode;
5120 struct nfs_server *server = NFS_SERVER(inode);
5121 struct nfs_client *clp = server->nfs_client;
5122 struct nfs_lockt_args arg = {
5123 .fh = NFS_FH(inode),
5126 struct nfs_lockt_res res = {
5129 struct rpc_message msg = {
5130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5133 .rpc_cred = state->owner->so_cred,
5135 struct nfs4_lock_state *lsp;
5138 arg.lock_owner.clientid = clp->cl_clientid;
5139 status = nfs4_set_lock_state(state, request);
5142 lsp = request->fl_u.nfs4_fl.owner;
5143 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5144 arg.lock_owner.s_dev = server->s_dev;
5145 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5148 request->fl_type = F_UNLCK;
5150 case -NFS4ERR_DENIED:
5153 request->fl_ops->fl_release_private(request);
5154 request->fl_ops = NULL;
5159 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5161 struct nfs4_exception exception = { };
5165 err = _nfs4_proc_getlk(state, cmd, request);
5166 trace_nfs4_get_lock(request, state, cmd, err);
5167 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5169 } while (exception.retry);
5173 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5176 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5178 res = posix_lock_file_wait(file, fl);
5181 res = flock_lock_file_wait(file, fl);
5189 struct nfs4_unlockdata {
5190 struct nfs_locku_args arg;
5191 struct nfs_locku_res res;
5192 struct nfs4_lock_state *lsp;
5193 struct nfs_open_context *ctx;
5194 struct file_lock fl;
5195 const struct nfs_server *server;
5196 unsigned long timestamp;
5199 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5200 struct nfs_open_context *ctx,
5201 struct nfs4_lock_state *lsp,
5202 struct nfs_seqid *seqid)
5204 struct nfs4_unlockdata *p;
5205 struct inode *inode = lsp->ls_state->inode;
5207 p = kzalloc(sizeof(*p), GFP_NOFS);
5210 p->arg.fh = NFS_FH(inode);
5212 p->arg.seqid = seqid;
5213 p->res.seqid = seqid;
5214 p->arg.stateid = &lsp->ls_stateid;
5216 atomic_inc(&lsp->ls_count);
5217 /* Ensure we don't close file until we're done freeing locks! */
5218 p->ctx = get_nfs_open_context(ctx);
5219 memcpy(&p->fl, fl, sizeof(p->fl));
5220 p->server = NFS_SERVER(inode);
5224 static void nfs4_locku_release_calldata(void *data)
5226 struct nfs4_unlockdata *calldata = data;
5227 nfs_free_seqid(calldata->arg.seqid);
5228 nfs4_put_lock_state(calldata->lsp);
5229 put_nfs_open_context(calldata->ctx);
5233 static void nfs4_locku_done(struct rpc_task *task, void *data)
5235 struct nfs4_unlockdata *calldata = data;
5237 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5239 switch (task->tk_status) {
5241 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5242 &calldata->res.stateid);
5243 renew_lease(calldata->server, calldata->timestamp);
5245 case -NFS4ERR_BAD_STATEID:
5246 case -NFS4ERR_OLD_STATEID:
5247 case -NFS4ERR_STALE_STATEID:
5248 case -NFS4ERR_EXPIRED:
5251 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
5252 rpc_restart_call_prepare(task);
5254 nfs_release_seqid(calldata->arg.seqid);
5257 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5259 struct nfs4_unlockdata *calldata = data;
5261 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5263 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5264 /* Note: exit _without_ running nfs4_locku_done */
5267 calldata->timestamp = jiffies;
5268 if (nfs4_setup_sequence(calldata->server,
5269 &calldata->arg.seq_args,
5270 &calldata->res.seq_res,
5272 nfs_release_seqid(calldata->arg.seqid);
5275 task->tk_action = NULL;
5277 nfs4_sequence_done(task, &calldata->res.seq_res);
5280 static const struct rpc_call_ops nfs4_locku_ops = {
5281 .rpc_call_prepare = nfs4_locku_prepare,
5282 .rpc_call_done = nfs4_locku_done,
5283 .rpc_release = nfs4_locku_release_calldata,
5286 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5287 struct nfs_open_context *ctx,
5288 struct nfs4_lock_state *lsp,
5289 struct nfs_seqid *seqid)
5291 struct nfs4_unlockdata *data;
5292 struct rpc_message msg = {
5293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5294 .rpc_cred = ctx->cred,
5296 struct rpc_task_setup task_setup_data = {
5297 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5298 .rpc_message = &msg,
5299 .callback_ops = &nfs4_locku_ops,
5300 .workqueue = nfsiod_workqueue,
5301 .flags = RPC_TASK_ASYNC,
5304 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5305 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5307 /* Ensure this is an unlock - when canceling a lock, the
5308 * canceled lock is passed in, and it won't be an unlock.
5310 fl->fl_type = F_UNLCK;
5312 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5314 nfs_free_seqid(seqid);
5315 return ERR_PTR(-ENOMEM);
5318 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5319 msg.rpc_argp = &data->arg;
5320 msg.rpc_resp = &data->res;
5321 task_setup_data.callback_data = data;
5322 return rpc_run_task(&task_setup_data);
5325 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5327 struct inode *inode = state->inode;
5328 struct nfs4_state_owner *sp = state->owner;
5329 struct nfs_inode *nfsi = NFS_I(inode);
5330 struct nfs_seqid *seqid;
5331 struct nfs4_lock_state *lsp;
5332 struct rpc_task *task;
5334 unsigned char fl_flags = request->fl_flags;
5336 status = nfs4_set_lock_state(state, request);
5337 /* Unlock _before_ we do the RPC call */
5338 request->fl_flags |= FL_EXISTS;
5339 /* Exclude nfs_delegation_claim_locks() */
5340 mutex_lock(&sp->so_delegreturn_mutex);
5341 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5342 down_read(&nfsi->rwsem);
5343 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5344 up_read(&nfsi->rwsem);
5345 mutex_unlock(&sp->so_delegreturn_mutex);
5348 up_read(&nfsi->rwsem);
5349 mutex_unlock(&sp->so_delegreturn_mutex);
5352 /* Is this a delegated lock? */
5353 lsp = request->fl_u.nfs4_fl.owner;
5354 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5356 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5360 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5361 status = PTR_ERR(task);
5364 status = nfs4_wait_for_completion_rpc_task(task);
5367 request->fl_flags = fl_flags;
5368 trace_nfs4_unlock(request, state, F_SETLK, status);
5372 struct nfs4_lockdata {
5373 struct nfs_lock_args arg;
5374 struct nfs_lock_res res;
5375 struct nfs4_lock_state *lsp;
5376 struct nfs_open_context *ctx;
5377 struct file_lock fl;
5378 unsigned long timestamp;
5381 struct nfs_server *server;
5384 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5385 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5388 struct nfs4_lockdata *p;
5389 struct inode *inode = lsp->ls_state->inode;
5390 struct nfs_server *server = NFS_SERVER(inode);
5392 p = kzalloc(sizeof(*p), gfp_mask);
5396 p->arg.fh = NFS_FH(inode);
5398 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5399 if (p->arg.open_seqid == NULL)
5401 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5402 if (p->arg.lock_seqid == NULL)
5403 goto out_free_seqid;
5404 p->arg.lock_stateid = &lsp->ls_stateid;
5405 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5406 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5407 p->arg.lock_owner.s_dev = server->s_dev;
5408 p->res.lock_seqid = p->arg.lock_seqid;
5411 atomic_inc(&lsp->ls_count);
5412 p->ctx = get_nfs_open_context(ctx);
5413 memcpy(&p->fl, fl, sizeof(p->fl));
5416 nfs_free_seqid(p->arg.open_seqid);
5422 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5424 struct nfs4_lockdata *data = calldata;
5425 struct nfs4_state *state = data->lsp->ls_state;
5427 dprintk("%s: begin!\n", __func__);
5428 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5430 /* Do we need to do an open_to_lock_owner? */
5431 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5432 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5433 goto out_release_lock_seqid;
5435 data->arg.open_stateid = &state->open_stateid;
5436 data->arg.new_lock_owner = 1;
5437 data->res.open_seqid = data->arg.open_seqid;
5439 data->arg.new_lock_owner = 0;
5440 if (!nfs4_valid_open_stateid(state)) {
5441 data->rpc_status = -EBADF;
5442 task->tk_action = NULL;
5443 goto out_release_open_seqid;
5445 data->timestamp = jiffies;
5446 if (nfs4_setup_sequence(data->server,
5447 &data->arg.seq_args,
5451 out_release_open_seqid:
5452 nfs_release_seqid(data->arg.open_seqid);
5453 out_release_lock_seqid:
5454 nfs_release_seqid(data->arg.lock_seqid);
5456 nfs4_sequence_done(task, &data->res.seq_res);
5457 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5460 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5462 struct nfs4_lockdata *data = calldata;
5464 dprintk("%s: begin!\n", __func__);
5466 if (!nfs4_sequence_done(task, &data->res.seq_res))
5469 data->rpc_status = task->tk_status;
5470 if (data->arg.new_lock_owner != 0) {
5471 if (data->rpc_status == 0)
5472 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5476 if (data->rpc_status == 0) {
5477 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5478 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5479 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5482 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5485 static void nfs4_lock_release(void *calldata)
5487 struct nfs4_lockdata *data = calldata;
5489 dprintk("%s: begin!\n", __func__);
5490 nfs_free_seqid(data->arg.open_seqid);
5491 if (data->cancelled != 0) {
5492 struct rpc_task *task;
5493 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5494 data->arg.lock_seqid);
5496 rpc_put_task_async(task);
5497 dprintk("%s: cancelling lock!\n", __func__);
5499 nfs_free_seqid(data->arg.lock_seqid);
5500 nfs4_put_lock_state(data->lsp);
5501 put_nfs_open_context(data->ctx);
5503 dprintk("%s: done!\n", __func__);
5506 static const struct rpc_call_ops nfs4_lock_ops = {
5507 .rpc_call_prepare = nfs4_lock_prepare,
5508 .rpc_call_done = nfs4_lock_done,
5509 .rpc_release = nfs4_lock_release,
5512 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5515 case -NFS4ERR_ADMIN_REVOKED:
5516 case -NFS4ERR_BAD_STATEID:
5517 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5518 if (new_lock_owner != 0 ||
5519 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5520 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5522 case -NFS4ERR_STALE_STATEID:
5523 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5524 case -NFS4ERR_EXPIRED:
5525 nfs4_schedule_lease_recovery(server->nfs_client);
5529 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5531 struct nfs4_lockdata *data;
5532 struct rpc_task *task;
5533 struct rpc_message msg = {
5534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5535 .rpc_cred = state->owner->so_cred,
5537 struct rpc_task_setup task_setup_data = {
5538 .rpc_client = NFS_CLIENT(state->inode),
5539 .rpc_message = &msg,
5540 .callback_ops = &nfs4_lock_ops,
5541 .workqueue = nfsiod_workqueue,
5542 .flags = RPC_TASK_ASYNC,
5546 dprintk("%s: begin!\n", __func__);
5547 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5548 fl->fl_u.nfs4_fl.owner,
5549 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5553 data->arg.block = 1;
5554 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5555 msg.rpc_argp = &data->arg;
5556 msg.rpc_resp = &data->res;
5557 task_setup_data.callback_data = data;
5558 if (recovery_type > NFS_LOCK_NEW) {
5559 if (recovery_type == NFS_LOCK_RECLAIM)
5560 data->arg.reclaim = NFS_LOCK_RECLAIM;
5561 nfs4_set_sequence_privileged(&data->arg.seq_args);
5563 task = rpc_run_task(&task_setup_data);
5565 return PTR_ERR(task);
5566 ret = nfs4_wait_for_completion_rpc_task(task);
5568 ret = data->rpc_status;
5570 nfs4_handle_setlk_error(data->server, data->lsp,
5571 data->arg.new_lock_owner, ret);
5573 data->cancelled = 1;
5575 dprintk("%s: done, ret = %d!\n", __func__, ret);
5579 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5581 struct nfs_server *server = NFS_SERVER(state->inode);
5582 struct nfs4_exception exception = {
5583 .inode = state->inode,
5588 /* Cache the lock if possible... */
5589 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5591 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5592 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5593 if (err != -NFS4ERR_DELAY)
5595 nfs4_handle_exception(server, err, &exception);
5596 } while (exception.retry);
5600 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5602 struct nfs_server *server = NFS_SERVER(state->inode);
5603 struct nfs4_exception exception = {
5604 .inode = state->inode,
5608 err = nfs4_set_lock_state(state, request);
5611 if (!recover_lost_locks) {
5612 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5616 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5618 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5619 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5623 case -NFS4ERR_GRACE:
5624 case -NFS4ERR_DELAY:
5625 nfs4_handle_exception(server, err, &exception);
5628 } while (exception.retry);
5633 #if defined(CONFIG_NFS_V4_1)
5635 * nfs41_check_expired_locks - possibly free a lock stateid
5637 * @state: NFSv4 state for an inode
5639 * Returns NFS_OK if recovery for this stateid is now finished.
5640 * Otherwise a negative NFS4ERR value is returned.
5642 static int nfs41_check_expired_locks(struct nfs4_state *state)
5644 int status, ret = -NFS4ERR_BAD_STATEID;
5645 struct nfs4_lock_state *lsp;
5646 struct nfs_server *server = NFS_SERVER(state->inode);
5648 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5649 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5650 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5652 status = nfs41_test_stateid(server,
5655 trace_nfs4_test_lock_stateid(state, lsp, status);
5656 if (status != NFS_OK) {
5657 /* Free the stateid unless the server
5658 * informs us the stateid is unrecognized. */
5659 if (status != -NFS4ERR_BAD_STATEID)
5660 nfs41_free_stateid(server,
5663 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5672 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5674 int status = NFS_OK;
5676 if (test_bit(LK_STATE_IN_USE, &state->flags))
5677 status = nfs41_check_expired_locks(state);
5678 if (status != NFS_OK)
5679 status = nfs4_lock_expired(state, request);
5684 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5686 struct nfs4_state_owner *sp = state->owner;
5687 struct nfs_inode *nfsi = NFS_I(state->inode);
5688 unsigned char fl_flags = request->fl_flags;
5690 int status = -ENOLCK;
5692 if ((fl_flags & FL_POSIX) &&
5693 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5695 /* Is this a delegated open? */
5696 status = nfs4_set_lock_state(state, request);
5699 request->fl_flags |= FL_ACCESS;
5700 status = do_vfs_lock(request->fl_file, request);
5703 down_read(&nfsi->rwsem);
5704 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5705 /* Yes: cache locks! */
5706 /* ...but avoid races with delegation recall... */
5707 request->fl_flags = fl_flags & ~FL_SLEEP;
5708 status = do_vfs_lock(request->fl_file, request);
5711 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5712 up_read(&nfsi->rwsem);
5713 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5716 down_read(&nfsi->rwsem);
5717 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5718 status = -NFS4ERR_DELAY;
5721 /* Note: we always want to sleep here! */
5722 request->fl_flags = fl_flags | FL_SLEEP;
5723 if (do_vfs_lock(request->fl_file, request) < 0)
5724 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5725 "manager!\n", __func__);
5727 up_read(&nfsi->rwsem);
5729 request->fl_flags = fl_flags;
5733 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5735 struct nfs4_exception exception = {
5737 .inode = state->inode,
5742 err = _nfs4_proc_setlk(state, cmd, request);
5743 trace_nfs4_set_lock(request, state, cmd, err);
5744 if (err == -NFS4ERR_DENIED)
5746 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5748 } while (exception.retry);
5753 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5755 struct nfs_open_context *ctx;
5756 struct nfs4_state *state;
5757 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5760 /* verify open state */
5761 ctx = nfs_file_open_context(filp);
5764 if (request->fl_start < 0 || request->fl_end < 0)
5767 if (IS_GETLK(cmd)) {
5769 return nfs4_proc_getlk(state, F_GETLK, request);
5773 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5776 if (request->fl_type == F_UNLCK) {
5778 return nfs4_proc_unlck(state, cmd, request);
5785 * Don't rely on the VFS having checked the file open mode,
5786 * since it won't do this for flock() locks.
5788 switch (request->fl_type) {
5790 if (!(filp->f_mode & FMODE_READ))
5794 if (!(filp->f_mode & FMODE_WRITE))
5799 status = nfs4_proc_setlk(state, cmd, request);
5800 if ((status != -EAGAIN) || IS_SETLK(cmd))
5802 timeout = nfs4_set_lock_task_retry(timeout);
5803 status = -ERESTARTSYS;
5806 } while(status < 0);
5810 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5812 struct nfs_server *server = NFS_SERVER(state->inode);
5815 err = nfs4_set_lock_state(state, fl);
5818 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5819 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5822 struct nfs_release_lockowner_data {
5823 struct nfs4_lock_state *lsp;
5824 struct nfs_server *server;
5825 struct nfs_release_lockowner_args args;
5826 struct nfs4_sequence_args seq_args;
5827 struct nfs4_sequence_res seq_res;
5828 unsigned long timestamp;
5831 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5833 struct nfs_release_lockowner_data *data = calldata;
5834 nfs40_setup_sequence(data->server,
5835 &data->seq_args, &data->seq_res, task);
5836 data->timestamp = jiffies;
5839 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5841 struct nfs_release_lockowner_data *data = calldata;
5842 struct nfs_server *server = data->server;
5844 nfs40_sequence_done(task, &data->seq_res);
5846 switch (task->tk_status) {
5848 renew_lease(server, data->timestamp);
5850 case -NFS4ERR_STALE_CLIENTID:
5851 case -NFS4ERR_EXPIRED:
5852 case -NFS4ERR_LEASE_MOVED:
5853 case -NFS4ERR_DELAY:
5854 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5855 rpc_restart_call_prepare(task);
5859 static void nfs4_release_lockowner_release(void *calldata)
5861 struct nfs_release_lockowner_data *data = calldata;
5862 nfs4_free_lock_state(data->server, data->lsp);
5866 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5867 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5868 .rpc_call_done = nfs4_release_lockowner_done,
5869 .rpc_release = nfs4_release_lockowner_release,
5872 static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
5874 struct nfs_release_lockowner_data *data;
5875 struct rpc_message msg = {
5876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5879 if (server->nfs_client->cl_mvops->minor_version != 0)
5882 data = kmalloc(sizeof(*data), GFP_NOFS);
5885 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
5887 data->server = server;
5888 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5889 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5890 data->args.lock_owner.s_dev = server->s_dev;
5892 msg.rpc_argp = &data->args;
5893 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5897 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5899 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5900 const void *buf, size_t buflen,
5901 int flags, int type)
5903 if (strcmp(key, "") != 0)
5906 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5909 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5910 void *buf, size_t buflen, int type)
5912 if (strcmp(key, "") != 0)
5915 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5918 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5919 size_t list_len, const char *name,
5920 size_t name_len, int type)
5922 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5924 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5927 if (list && len <= list_len)
5928 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5932 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5933 static inline int nfs4_server_supports_labels(struct nfs_server *server)
5935 return server->caps & NFS_CAP_SECURITY_LABEL;
5938 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5939 const void *buf, size_t buflen,
5940 int flags, int type)
5942 if (security_ismaclabel(key))
5943 return nfs4_set_security_label(dentry, buf, buflen);
5948 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5949 void *buf, size_t buflen, int type)
5951 if (security_ismaclabel(key))
5952 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5956 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5957 size_t list_len, const char *name,
5958 size_t name_len, int type)
5962 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5963 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5964 if (list && len <= list_len)
5965 security_inode_listsecurity(dentry->d_inode, list, len);
5970 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5971 .prefix = XATTR_SECURITY_PREFIX,
5972 .list = nfs4_xattr_list_nfs4_label,
5973 .get = nfs4_xattr_get_nfs4_label,
5974 .set = nfs4_xattr_set_nfs4_label,
5980 * nfs_fhget will use either the mounted_on_fileid or the fileid
5982 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5984 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5985 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5986 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5987 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5990 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5991 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5992 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5996 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5997 const struct qstr *name,
5998 struct nfs4_fs_locations *fs_locations,
6001 struct nfs_server *server = NFS_SERVER(dir);
6003 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6005 struct nfs4_fs_locations_arg args = {
6006 .dir_fh = NFS_FH(dir),
6011 struct nfs4_fs_locations_res res = {
6012 .fs_locations = fs_locations,
6014 struct rpc_message msg = {
6015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6021 dprintk("%s: start\n", __func__);
6023 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6024 * is not supported */
6025 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6026 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6028 bitmask[0] |= FATTR4_WORD0_FILEID;
6030 nfs_fattr_init(&fs_locations->fattr);
6031 fs_locations->server = server;
6032 fs_locations->nlocations = 0;
6033 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6034 dprintk("%s: returned status = %d\n", __func__, status);
6038 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6039 const struct qstr *name,
6040 struct nfs4_fs_locations *fs_locations,
6043 struct nfs4_exception exception = { };
6046 err = _nfs4_proc_fs_locations(client, dir, name,
6047 fs_locations, page);
6048 trace_nfs4_get_fs_locations(dir, name, err);
6049 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6051 } while (exception.retry);
6056 * This operation also signals the server that this client is
6057 * performing migration recovery. The server can stop returning
6058 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6059 * appended to this compound to identify the client ID which is
6060 * performing recovery.
6062 static int _nfs40_proc_get_locations(struct inode *inode,
6063 struct nfs4_fs_locations *locations,
6064 struct page *page, struct rpc_cred *cred)
6066 struct nfs_server *server = NFS_SERVER(inode);
6067 struct rpc_clnt *clnt = server->client;
6069 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6071 struct nfs4_fs_locations_arg args = {
6072 .clientid = server->nfs_client->cl_clientid,
6073 .fh = NFS_FH(inode),
6076 .migration = 1, /* skip LOOKUP */
6077 .renew = 1, /* append RENEW */
6079 struct nfs4_fs_locations_res res = {
6080 .fs_locations = locations,
6084 struct rpc_message msg = {
6085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6090 unsigned long now = jiffies;
6093 nfs_fattr_init(&locations->fattr);
6094 locations->server = server;
6095 locations->nlocations = 0;
6097 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6098 nfs4_set_sequence_privileged(&args.seq_args);
6099 status = nfs4_call_sync_sequence(clnt, server, &msg,
6100 &args.seq_args, &res.seq_res);
6104 renew_lease(server, now);
6108 #ifdef CONFIG_NFS_V4_1
6111 * This operation also signals the server that this client is
6112 * performing migration recovery. The server can stop asserting
6113 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6114 * performing this operation is identified in the SEQUENCE
6115 * operation in this compound.
6117 * When the client supports GETATTR(fs_locations_info), it can
6118 * be plumbed in here.
6120 static int _nfs41_proc_get_locations(struct inode *inode,
6121 struct nfs4_fs_locations *locations,
6122 struct page *page, struct rpc_cred *cred)
6124 struct nfs_server *server = NFS_SERVER(inode);
6125 struct rpc_clnt *clnt = server->client;
6127 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6129 struct nfs4_fs_locations_arg args = {
6130 .fh = NFS_FH(inode),
6133 .migration = 1, /* skip LOOKUP */
6135 struct nfs4_fs_locations_res res = {
6136 .fs_locations = locations,
6139 struct rpc_message msg = {
6140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6147 nfs_fattr_init(&locations->fattr);
6148 locations->server = server;
6149 locations->nlocations = 0;
6151 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6152 nfs4_set_sequence_privileged(&args.seq_args);
6153 status = nfs4_call_sync_sequence(clnt, server, &msg,
6154 &args.seq_args, &res.seq_res);
6155 if (status == NFS4_OK &&
6156 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6157 status = -NFS4ERR_LEASE_MOVED;
6161 #endif /* CONFIG_NFS_V4_1 */
6164 * nfs4_proc_get_locations - discover locations for a migrated FSID
6165 * @inode: inode on FSID that is migrating
6166 * @locations: result of query
6168 * @cred: credential to use for this operation
6170 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6171 * operation failed, or a negative errno if a local error occurred.
6173 * On success, "locations" is filled in, but if the server has
6174 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6177 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6178 * from this client that require migration recovery.
6180 int nfs4_proc_get_locations(struct inode *inode,
6181 struct nfs4_fs_locations *locations,
6182 struct page *page, struct rpc_cred *cred)
6184 struct nfs_server *server = NFS_SERVER(inode);
6185 struct nfs_client *clp = server->nfs_client;
6186 const struct nfs4_mig_recovery_ops *ops =
6187 clp->cl_mvops->mig_recovery_ops;
6188 struct nfs4_exception exception = { };
6191 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6192 (unsigned long long)server->fsid.major,
6193 (unsigned long long)server->fsid.minor,
6195 nfs_display_fhandle(NFS_FH(inode), __func__);
6198 status = ops->get_locations(inode, locations, page, cred);
6199 if (status != -NFS4ERR_DELAY)
6201 nfs4_handle_exception(server, status, &exception);
6202 } while (exception.retry);
6207 * This operation also signals the server that this client is
6208 * performing "lease moved" recovery. The server can stop
6209 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6210 * is appended to this compound to identify the client ID which is
6211 * performing recovery.
6213 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6215 struct nfs_server *server = NFS_SERVER(inode);
6216 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6217 struct rpc_clnt *clnt = server->client;
6218 struct nfs4_fsid_present_arg args = {
6219 .fh = NFS_FH(inode),
6220 .clientid = clp->cl_clientid,
6221 .renew = 1, /* append RENEW */
6223 struct nfs4_fsid_present_res res = {
6226 struct rpc_message msg = {
6227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6232 unsigned long now = jiffies;
6235 res.fh = nfs_alloc_fhandle();
6239 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6240 nfs4_set_sequence_privileged(&args.seq_args);
6241 status = nfs4_call_sync_sequence(clnt, server, &msg,
6242 &args.seq_args, &res.seq_res);
6243 nfs_free_fhandle(res.fh);
6247 do_renew_lease(clp, now);
6251 #ifdef CONFIG_NFS_V4_1
6254 * This operation also signals the server that this client is
6255 * performing "lease moved" recovery. The server can stop asserting
6256 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6257 * this operation is identified in the SEQUENCE operation in this
6260 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6262 struct nfs_server *server = NFS_SERVER(inode);
6263 struct rpc_clnt *clnt = server->client;
6264 struct nfs4_fsid_present_arg args = {
6265 .fh = NFS_FH(inode),
6267 struct nfs4_fsid_present_res res = {
6269 struct rpc_message msg = {
6270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6277 res.fh = nfs_alloc_fhandle();
6281 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6282 nfs4_set_sequence_privileged(&args.seq_args);
6283 status = nfs4_call_sync_sequence(clnt, server, &msg,
6284 &args.seq_args, &res.seq_res);
6285 nfs_free_fhandle(res.fh);
6286 if (status == NFS4_OK &&
6287 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6288 status = -NFS4ERR_LEASE_MOVED;
6292 #endif /* CONFIG_NFS_V4_1 */
6295 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6296 * @inode: inode on FSID to check
6297 * @cred: credential to use for this operation
6299 * Server indicates whether the FSID is present, moved, or not
6300 * recognized. This operation is necessary to clear a LEASE_MOVED
6301 * condition for this client ID.
6303 * Returns NFS4_OK if the FSID is present on this server,
6304 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6305 * NFS4ERR code if some error occurred on the server, or a
6306 * negative errno if a local failure occurred.
6308 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6310 struct nfs_server *server = NFS_SERVER(inode);
6311 struct nfs_client *clp = server->nfs_client;
6312 const struct nfs4_mig_recovery_ops *ops =
6313 clp->cl_mvops->mig_recovery_ops;
6314 struct nfs4_exception exception = { };
6317 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6318 (unsigned long long)server->fsid.major,
6319 (unsigned long long)server->fsid.minor,
6321 nfs_display_fhandle(NFS_FH(inode), __func__);
6324 status = ops->fsid_present(inode, cred);
6325 if (status != -NFS4ERR_DELAY)
6327 nfs4_handle_exception(server, status, &exception);
6328 } while (exception.retry);
6333 * If 'use_integrity' is true and the state managment nfs_client
6334 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6335 * and the machine credential as per RFC3530bis and RFC5661 Security
6336 * Considerations sections. Otherwise, just use the user cred with the
6337 * filesystem's rpc_client.
6339 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6342 struct nfs4_secinfo_arg args = {
6343 .dir_fh = NFS_FH(dir),
6346 struct nfs4_secinfo_res res = {
6349 struct rpc_message msg = {
6350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6354 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6355 struct rpc_cred *cred = NULL;
6357 if (use_integrity) {
6358 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6359 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6360 msg.rpc_cred = cred;
6363 dprintk("NFS call secinfo %s\n", name->name);
6365 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6366 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6368 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6370 dprintk("NFS reply secinfo: %d\n", status);
6378 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6379 struct nfs4_secinfo_flavors *flavors)
6381 struct nfs4_exception exception = { };
6384 err = -NFS4ERR_WRONGSEC;
6386 /* try to use integrity protection with machine cred */
6387 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6388 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6391 * if unable to use integrity protection, or SECINFO with
6392 * integrity protection returns NFS4ERR_WRONGSEC (which is
6393 * disallowed by spec, but exists in deployed servers) use
6394 * the current filesystem's rpc_client and the user cred.
6396 if (err == -NFS4ERR_WRONGSEC)
6397 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6399 trace_nfs4_secinfo(dir, name, err);
6400 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6402 } while (exception.retry);
6406 #ifdef CONFIG_NFS_V4_1
6408 * Check the exchange flags returned by the server for invalid flags, having
6409 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6412 static int nfs4_check_cl_exchange_flags(u32 flags)
6414 if (flags & ~EXCHGID4_FLAG_MASK_R)
6416 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6417 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6419 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6423 return -NFS4ERR_INVAL;
6427 nfs41_same_server_scope(struct nfs41_server_scope *a,
6428 struct nfs41_server_scope *b)
6430 if (a->server_scope_sz == b->server_scope_sz &&
6431 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6438 * nfs4_proc_bind_conn_to_session()
6440 * The 4.1 client currently uses the same TCP connection for the
6441 * fore and backchannel.
6443 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6446 struct nfs41_bind_conn_to_session_res res;
6447 struct rpc_message msg = {
6449 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6455 dprintk("--> %s\n", __func__);
6457 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6458 if (unlikely(res.session == NULL)) {
6463 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6464 trace_nfs4_bind_conn_to_session(clp, status);
6466 if (memcmp(res.session->sess_id.data,
6467 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6468 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6472 if (res.dir != NFS4_CDFS4_BOTH) {
6473 dprintk("NFS: %s: Unexpected direction from server\n",
6478 if (res.use_conn_in_rdma_mode) {
6479 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6488 dprintk("<-- %s status= %d\n", __func__, status);
6493 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6494 * and operations we'd like to see to enable certain features in the allow map
6496 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6497 .how = SP4_MACH_CRED,
6498 .enforce.u.words = {
6499 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6500 1 << (OP_EXCHANGE_ID - 32) |
6501 1 << (OP_CREATE_SESSION - 32) |
6502 1 << (OP_DESTROY_SESSION - 32) |
6503 1 << (OP_DESTROY_CLIENTID - 32)
6506 [0] = 1 << (OP_CLOSE) |
6509 [1] = 1 << (OP_SECINFO - 32) |
6510 1 << (OP_SECINFO_NO_NAME - 32) |
6511 1 << (OP_TEST_STATEID - 32) |
6512 1 << (OP_FREE_STATEID - 32) |
6513 1 << (OP_WRITE - 32)
6518 * Select the state protection mode for client `clp' given the server results
6519 * from exchange_id in `sp'.
6521 * Returns 0 on success, negative errno otherwise.
6523 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6524 struct nfs41_state_protection *sp)
6526 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6527 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6528 1 << (OP_EXCHANGE_ID - 32) |
6529 1 << (OP_CREATE_SESSION - 32) |
6530 1 << (OP_DESTROY_SESSION - 32) |
6531 1 << (OP_DESTROY_CLIENTID - 32)
6535 if (sp->how == SP4_MACH_CRED) {
6536 /* Print state protect result */
6537 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6538 for (i = 0; i <= LAST_NFS4_OP; i++) {
6539 if (test_bit(i, sp->enforce.u.longs))
6540 dfprintk(MOUNT, " enforce op %d\n", i);
6541 if (test_bit(i, sp->allow.u.longs))
6542 dfprintk(MOUNT, " allow op %d\n", i);
6545 /* make sure nothing is on enforce list that isn't supported */
6546 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6547 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6548 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6554 * Minimal mode - state operations are allowed to use machine
6555 * credential. Note this already happens by default, so the
6556 * client doesn't have to do anything more than the negotiation.
6558 * NOTE: we don't care if EXCHANGE_ID is in the list -
6559 * we're already using the machine cred for exchange_id
6560 * and will never use a different cred.
6562 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6563 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6564 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6565 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6566 dfprintk(MOUNT, "sp4_mach_cred:\n");
6567 dfprintk(MOUNT, " minimal mode enabled\n");
6568 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6570 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6574 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6575 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6576 dfprintk(MOUNT, " cleanup mode enabled\n");
6577 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6580 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6581 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6582 dfprintk(MOUNT, " secinfo mode enabled\n");
6583 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6586 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6587 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6588 dfprintk(MOUNT, " stateid mode enabled\n");
6589 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6592 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6593 dfprintk(MOUNT, " write mode enabled\n");
6594 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6597 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6598 dfprintk(MOUNT, " commit mode enabled\n");
6599 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6607 * _nfs4_proc_exchange_id()
6609 * Wrapper for EXCHANGE_ID operation.
6611 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6614 nfs4_verifier verifier;
6615 struct nfs41_exchange_id_args args = {
6616 .verifier = &verifier,
6618 #ifdef CONFIG_NFS_V4_1_MIGRATION
6619 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6620 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6621 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6623 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6624 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6627 struct nfs41_exchange_id_res res = {
6631 struct rpc_message msg = {
6632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6638 nfs4_init_boot_verifier(clp, &verifier);
6639 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6641 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6642 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6643 args.id_len, args.id);
6645 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6647 if (unlikely(res.server_owner == NULL)) {
6652 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6654 if (unlikely(res.server_scope == NULL)) {
6656 goto out_server_owner;
6659 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6660 if (unlikely(res.impl_id == NULL)) {
6662 goto out_server_scope;
6667 args.state_protect.how = SP4_NONE;
6671 args.state_protect = nfs4_sp4_mach_cred_request;
6678 goto out_server_scope;
6681 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6682 trace_nfs4_exchange_id(clp, status);
6684 status = nfs4_check_cl_exchange_flags(res.flags);
6687 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6690 clp->cl_clientid = res.clientid;
6691 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6692 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6693 clp->cl_seqid = res.seqid;
6695 kfree(clp->cl_serverowner);
6696 clp->cl_serverowner = res.server_owner;
6697 res.server_owner = NULL;
6699 /* use the most recent implementation id */
6700 kfree(clp->cl_implid);
6701 clp->cl_implid = res.impl_id;
6703 if (clp->cl_serverscope != NULL &&
6704 !nfs41_same_server_scope(clp->cl_serverscope,
6705 res.server_scope)) {
6706 dprintk("%s: server_scope mismatch detected\n",
6708 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6709 kfree(clp->cl_serverscope);
6710 clp->cl_serverscope = NULL;
6713 if (clp->cl_serverscope == NULL) {
6714 clp->cl_serverscope = res.server_scope;
6721 kfree(res.server_owner);
6723 kfree(res.server_scope);
6725 if (clp->cl_implid != NULL)
6726 dprintk("NFS reply exchange_id: Server Implementation ID: "
6727 "domain: %s, name: %s, date: %llu,%u\n",
6728 clp->cl_implid->domain, clp->cl_implid->name,
6729 clp->cl_implid->date.seconds,
6730 clp->cl_implid->date.nseconds);
6731 dprintk("NFS reply exchange_id: %d\n", status);
6736 * nfs4_proc_exchange_id()
6738 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6740 * Since the clientid has expired, all compounds using sessions
6741 * associated with the stale clientid will be returning
6742 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6743 * be in some phase of session reset.
6745 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6747 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6749 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6752 /* try SP4_MACH_CRED if krb5i/p */
6753 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6754 authflavor == RPC_AUTH_GSS_KRB5P) {
6755 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6761 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6764 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6765 struct rpc_cred *cred)
6767 struct rpc_message msg = {
6768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6774 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6775 trace_nfs4_destroy_clientid(clp, status);
6777 dprintk("NFS: Got error %d from the server %s on "
6778 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6782 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6783 struct rpc_cred *cred)
6788 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6789 ret = _nfs4_proc_destroy_clientid(clp, cred);
6791 case -NFS4ERR_DELAY:
6792 case -NFS4ERR_CLIENTID_BUSY:
6802 int nfs4_destroy_clientid(struct nfs_client *clp)
6804 struct rpc_cred *cred;
6807 if (clp->cl_mvops->minor_version < 1)
6809 if (clp->cl_exchange_flags == 0)
6811 if (clp->cl_preserve_clid)
6813 cred = nfs4_get_clid_cred(clp);
6814 ret = nfs4_proc_destroy_clientid(clp, cred);
6819 case -NFS4ERR_STALE_CLIENTID:
6820 clp->cl_exchange_flags = 0;
6826 struct nfs4_get_lease_time_data {
6827 struct nfs4_get_lease_time_args *args;
6828 struct nfs4_get_lease_time_res *res;
6829 struct nfs_client *clp;
6832 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6835 struct nfs4_get_lease_time_data *data =
6836 (struct nfs4_get_lease_time_data *)calldata;
6838 dprintk("--> %s\n", __func__);
6839 /* just setup sequence, do not trigger session recovery
6840 since we're invoked within one */
6841 nfs41_setup_sequence(data->clp->cl_session,
6842 &data->args->la_seq_args,
6843 &data->res->lr_seq_res,
6845 dprintk("<-- %s\n", __func__);
6849 * Called from nfs4_state_manager thread for session setup, so don't recover
6850 * from sequence operation or clientid errors.
6852 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6854 struct nfs4_get_lease_time_data *data =
6855 (struct nfs4_get_lease_time_data *)calldata;
6857 dprintk("--> %s\n", __func__);
6858 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6860 switch (task->tk_status) {
6861 case -NFS4ERR_DELAY:
6862 case -NFS4ERR_GRACE:
6863 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6864 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6865 task->tk_status = 0;
6867 case -NFS4ERR_RETRY_UNCACHED_REP:
6868 rpc_restart_call_prepare(task);
6871 dprintk("<-- %s\n", __func__);
6874 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
6875 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6876 .rpc_call_done = nfs4_get_lease_time_done,
6879 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6881 struct rpc_task *task;
6882 struct nfs4_get_lease_time_args args;
6883 struct nfs4_get_lease_time_res res = {
6884 .lr_fsinfo = fsinfo,
6886 struct nfs4_get_lease_time_data data = {
6891 struct rpc_message msg = {
6892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6896 struct rpc_task_setup task_setup = {
6897 .rpc_client = clp->cl_rpcclient,
6898 .rpc_message = &msg,
6899 .callback_ops = &nfs4_get_lease_time_ops,
6900 .callback_data = &data,
6901 .flags = RPC_TASK_TIMEOUT,
6905 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
6906 nfs4_set_sequence_privileged(&args.la_seq_args);
6907 dprintk("--> %s\n", __func__);
6908 task = rpc_run_task(&task_setup);
6911 status = PTR_ERR(task);
6913 status = task->tk_status;
6916 dprintk("<-- %s return %d\n", __func__, status);
6922 * Initialize the values to be used by the client in CREATE_SESSION
6923 * If nfs4_init_session set the fore channel request and response sizes,
6926 * Set the back channel max_resp_sz_cached to zero to force the client to
6927 * always set csa_cachethis to FALSE because the current implementation
6928 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6930 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6932 unsigned int max_rqst_sz, max_resp_sz;
6934 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6935 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6937 /* Fore channel attributes */
6938 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6939 args->fc_attrs.max_resp_sz = max_resp_sz;
6940 args->fc_attrs.max_ops = NFS4_MAX_OPS;
6941 args->fc_attrs.max_reqs = max_session_slots;
6943 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6944 "max_ops=%u max_reqs=%u\n",
6946 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
6947 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
6949 /* Back channel attributes */
6950 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6951 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6952 args->bc_attrs.max_resp_sz_cached = 0;
6953 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6954 args->bc_attrs.max_reqs = 1;
6956 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6957 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6959 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6960 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6961 args->bc_attrs.max_reqs);
6964 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6966 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6967 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6969 if (rcvd->max_resp_sz > sent->max_resp_sz)
6972 * Our requested max_ops is the minimum we need; we're not
6973 * prepared to break up compounds into smaller pieces than that.
6974 * So, no point even trying to continue if the server won't
6977 if (rcvd->max_ops < sent->max_ops)
6979 if (rcvd->max_reqs == 0)
6981 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6982 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
6986 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6988 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6989 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
6991 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6993 if (rcvd->max_resp_sz < sent->max_resp_sz)
6995 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6997 /* These would render the backchannel useless: */
6998 if (rcvd->max_ops != sent->max_ops)
7000 if (rcvd->max_reqs != sent->max_reqs)
7005 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7006 struct nfs4_session *session)
7010 ret = nfs4_verify_fore_channel_attrs(args, session);
7013 return nfs4_verify_back_channel_attrs(args, session);
7016 static int _nfs4_proc_create_session(struct nfs_client *clp,
7017 struct rpc_cred *cred)
7019 struct nfs4_session *session = clp->cl_session;
7020 struct nfs41_create_session_args args = {
7022 .cb_program = NFS4_CALLBACK,
7024 struct nfs41_create_session_res res = {
7027 struct rpc_message msg = {
7028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7035 nfs4_init_channel_attrs(&args);
7036 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7038 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7039 trace_nfs4_create_session(clp, status);
7042 /* Verify the session's negotiated channel_attrs values */
7043 status = nfs4_verify_channel_attrs(&args, session);
7044 /* Increment the clientid slot sequence id */
7052 * Issues a CREATE_SESSION operation to the server.
7053 * It is the responsibility of the caller to verify the session is
7054 * expired before calling this routine.
7056 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7060 struct nfs4_session *session = clp->cl_session;
7062 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7064 status = _nfs4_proc_create_session(clp, cred);
7068 /* Init or reset the session slot tables */
7069 status = nfs4_setup_session_slot_tables(session);
7070 dprintk("slot table setup returned %d\n", status);
7074 ptr = (unsigned *)&session->sess_id.data[0];
7075 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7076 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7078 dprintk("<-- %s\n", __func__);
7083 * Issue the over-the-wire RPC DESTROY_SESSION.
7084 * The caller must serialize access to this routine.
7086 int nfs4_proc_destroy_session(struct nfs4_session *session,
7087 struct rpc_cred *cred)
7089 struct rpc_message msg = {
7090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7091 .rpc_argp = session,
7096 dprintk("--> nfs4_proc_destroy_session\n");
7098 /* session is still being setup */
7099 if (session->clp->cl_cons_state != NFS_CS_READY)
7102 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7103 trace_nfs4_destroy_session(session->clp, status);
7106 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7107 "Session has been destroyed regardless...\n", status);
7109 dprintk("<-- nfs4_proc_destroy_session\n");
7114 * Renew the cl_session lease.
7116 struct nfs4_sequence_data {
7117 struct nfs_client *clp;
7118 struct nfs4_sequence_args args;
7119 struct nfs4_sequence_res res;
7122 static void nfs41_sequence_release(void *data)
7124 struct nfs4_sequence_data *calldata = data;
7125 struct nfs_client *clp = calldata->clp;
7127 if (atomic_read(&clp->cl_count) > 1)
7128 nfs4_schedule_state_renewal(clp);
7129 nfs_put_client(clp);
7133 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7135 switch(task->tk_status) {
7136 case -NFS4ERR_DELAY:
7137 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7140 nfs4_schedule_lease_recovery(clp);
7145 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7147 struct nfs4_sequence_data *calldata = data;
7148 struct nfs_client *clp = calldata->clp;
7150 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7153 trace_nfs4_sequence(clp, task->tk_status);
7154 if (task->tk_status < 0) {
7155 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7156 if (atomic_read(&clp->cl_count) == 1)
7159 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7160 rpc_restart_call_prepare(task);
7164 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7166 dprintk("<-- %s\n", __func__);
7169 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7171 struct nfs4_sequence_data *calldata = data;
7172 struct nfs_client *clp = calldata->clp;
7173 struct nfs4_sequence_args *args;
7174 struct nfs4_sequence_res *res;
7176 args = task->tk_msg.rpc_argp;
7177 res = task->tk_msg.rpc_resp;
7179 nfs41_setup_sequence(clp->cl_session, args, res, task);
7182 static const struct rpc_call_ops nfs41_sequence_ops = {
7183 .rpc_call_done = nfs41_sequence_call_done,
7184 .rpc_call_prepare = nfs41_sequence_prepare,
7185 .rpc_release = nfs41_sequence_release,
7188 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7189 struct rpc_cred *cred,
7192 struct nfs4_sequence_data *calldata;
7193 struct rpc_message msg = {
7194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7197 struct rpc_task_setup task_setup_data = {
7198 .rpc_client = clp->cl_rpcclient,
7199 .rpc_message = &msg,
7200 .callback_ops = &nfs41_sequence_ops,
7201 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7204 if (!atomic_inc_not_zero(&clp->cl_count))
7205 return ERR_PTR(-EIO);
7206 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7207 if (calldata == NULL) {
7208 nfs_put_client(clp);
7209 return ERR_PTR(-ENOMEM);
7211 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7213 nfs4_set_sequence_privileged(&calldata->args);
7214 msg.rpc_argp = &calldata->args;
7215 msg.rpc_resp = &calldata->res;
7216 calldata->clp = clp;
7217 task_setup_data.callback_data = calldata;
7219 return rpc_run_task(&task_setup_data);
7222 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7224 struct rpc_task *task;
7227 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7229 task = _nfs41_proc_sequence(clp, cred, false);
7231 ret = PTR_ERR(task);
7233 rpc_put_task_async(task);
7234 dprintk("<-- %s status=%d\n", __func__, ret);
7238 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7240 struct rpc_task *task;
7243 task = _nfs41_proc_sequence(clp, cred, true);
7245 ret = PTR_ERR(task);
7248 ret = rpc_wait_for_completion_task(task);
7250 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7252 if (task->tk_status == 0)
7253 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7254 ret = task->tk_status;
7258 dprintk("<-- %s status=%d\n", __func__, ret);
7262 struct nfs4_reclaim_complete_data {
7263 struct nfs_client *clp;
7264 struct nfs41_reclaim_complete_args arg;
7265 struct nfs41_reclaim_complete_res res;
7268 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7270 struct nfs4_reclaim_complete_data *calldata = data;
7272 nfs41_setup_sequence(calldata->clp->cl_session,
7273 &calldata->arg.seq_args,
7274 &calldata->res.seq_res,
7278 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7280 switch(task->tk_status) {
7282 case -NFS4ERR_COMPLETE_ALREADY:
7283 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7285 case -NFS4ERR_DELAY:
7286 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7288 case -NFS4ERR_RETRY_UNCACHED_REP:
7291 nfs4_schedule_lease_recovery(clp);
7296 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7298 struct nfs4_reclaim_complete_data *calldata = data;
7299 struct nfs_client *clp = calldata->clp;
7300 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7302 dprintk("--> %s\n", __func__);
7303 if (!nfs41_sequence_done(task, res))
7306 trace_nfs4_reclaim_complete(clp, task->tk_status);
7307 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7308 rpc_restart_call_prepare(task);
7311 dprintk("<-- %s\n", __func__);
7314 static void nfs4_free_reclaim_complete_data(void *data)
7316 struct nfs4_reclaim_complete_data *calldata = data;
7321 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7322 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7323 .rpc_call_done = nfs4_reclaim_complete_done,
7324 .rpc_release = nfs4_free_reclaim_complete_data,
7328 * Issue a global reclaim complete.
7330 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7331 struct rpc_cred *cred)
7333 struct nfs4_reclaim_complete_data *calldata;
7334 struct rpc_task *task;
7335 struct rpc_message msg = {
7336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7339 struct rpc_task_setup task_setup_data = {
7340 .rpc_client = clp->cl_rpcclient,
7341 .rpc_message = &msg,
7342 .callback_ops = &nfs4_reclaim_complete_call_ops,
7343 .flags = RPC_TASK_ASYNC,
7345 int status = -ENOMEM;
7347 dprintk("--> %s\n", __func__);
7348 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7349 if (calldata == NULL)
7351 calldata->clp = clp;
7352 calldata->arg.one_fs = 0;
7354 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7355 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7356 msg.rpc_argp = &calldata->arg;
7357 msg.rpc_resp = &calldata->res;
7358 task_setup_data.callback_data = calldata;
7359 task = rpc_run_task(&task_setup_data);
7361 status = PTR_ERR(task);
7364 status = nfs4_wait_for_completion_rpc_task(task);
7366 status = task->tk_status;
7370 dprintk("<-- %s status=%d\n", __func__, status);
7375 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7377 struct nfs4_layoutget *lgp = calldata;
7378 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7379 struct nfs4_session *session = nfs4_get_session(server);
7381 dprintk("--> %s\n", __func__);
7382 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7383 * right now covering the LAYOUTGET we are about to send.
7384 * However, that is not so catastrophic, and there seems
7385 * to be no way to prevent it completely.
7387 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7388 &lgp->res.seq_res, task))
7390 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7391 NFS_I(lgp->args.inode)->layout,
7392 lgp->args.ctx->state)) {
7393 rpc_exit(task, NFS4_OK);
7397 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7399 struct nfs4_layoutget *lgp = calldata;
7400 struct inode *inode = lgp->args.inode;
7401 struct nfs_server *server = NFS_SERVER(inode);
7402 struct pnfs_layout_hdr *lo;
7403 struct nfs4_state *state = NULL;
7404 unsigned long timeo, giveup;
7406 dprintk("--> %s\n", __func__);
7408 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7411 switch (task->tk_status) {
7414 case -NFS4ERR_LAYOUTTRYLATER:
7415 case -NFS4ERR_RECALLCONFLICT:
7416 timeo = rpc_get_timeout(task->tk_client);
7417 giveup = lgp->args.timestamp + timeo;
7418 if (time_after(giveup, jiffies))
7419 task->tk_status = -NFS4ERR_DELAY;
7421 case -NFS4ERR_EXPIRED:
7422 case -NFS4ERR_BAD_STATEID:
7423 spin_lock(&inode->i_lock);
7424 lo = NFS_I(inode)->layout;
7425 if (!lo || list_empty(&lo->plh_segs)) {
7426 spin_unlock(&inode->i_lock);
7427 /* If the open stateid was bad, then recover it. */
7428 state = lgp->args.ctx->state;
7432 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7433 spin_unlock(&inode->i_lock);
7434 /* Mark the bad layout state as invalid, then
7435 * retry using the open stateid. */
7436 pnfs_free_lseg_list(&head);
7439 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7440 rpc_restart_call_prepare(task);
7442 dprintk("<-- %s\n", __func__);
7445 static size_t max_response_pages(struct nfs_server *server)
7447 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7448 return nfs_page_array_len(0, max_resp_sz);
7451 static void nfs4_free_pages(struct page **pages, size_t size)
7458 for (i = 0; i < size; i++) {
7461 __free_page(pages[i]);
7466 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7468 struct page **pages;
7471 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7473 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7477 for (i = 0; i < size; i++) {
7478 pages[i] = alloc_page(gfp_flags);
7480 dprintk("%s: failed to allocate page\n", __func__);
7481 nfs4_free_pages(pages, size);
7489 static void nfs4_layoutget_release(void *calldata)
7491 struct nfs4_layoutget *lgp = calldata;
7492 struct inode *inode = lgp->args.inode;
7493 struct nfs_server *server = NFS_SERVER(inode);
7494 size_t max_pages = max_response_pages(server);
7496 dprintk("--> %s\n", __func__);
7497 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7498 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7499 put_nfs_open_context(lgp->args.ctx);
7501 dprintk("<-- %s\n", __func__);
7504 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7505 .rpc_call_prepare = nfs4_layoutget_prepare,
7506 .rpc_call_done = nfs4_layoutget_done,
7507 .rpc_release = nfs4_layoutget_release,
7510 struct pnfs_layout_segment *
7511 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7513 struct inode *inode = lgp->args.inode;
7514 struct nfs_server *server = NFS_SERVER(inode);
7515 size_t max_pages = max_response_pages(server);
7516 struct rpc_task *task;
7517 struct rpc_message msg = {
7518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7519 .rpc_argp = &lgp->args,
7520 .rpc_resp = &lgp->res,
7521 .rpc_cred = lgp->cred,
7523 struct rpc_task_setup task_setup_data = {
7524 .rpc_client = server->client,
7525 .rpc_message = &msg,
7526 .callback_ops = &nfs4_layoutget_call_ops,
7527 .callback_data = lgp,
7528 .flags = RPC_TASK_ASYNC,
7530 struct pnfs_layout_segment *lseg = NULL;
7533 dprintk("--> %s\n", __func__);
7535 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7536 if (!lgp->args.layout.pages) {
7537 nfs4_layoutget_release(lgp);
7538 return ERR_PTR(-ENOMEM);
7540 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7541 lgp->args.timestamp = jiffies;
7543 lgp->res.layoutp = &lgp->args.layout;
7544 lgp->res.seq_res.sr_slot = NULL;
7545 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7547 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7548 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7550 task = rpc_run_task(&task_setup_data);
7552 return ERR_CAST(task);
7553 status = nfs4_wait_for_completion_rpc_task(task);
7555 status = task->tk_status;
7556 trace_nfs4_layoutget(lgp->args.ctx,
7560 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7561 if (status == 0 && lgp->res.layoutp->len)
7562 lseg = pnfs_layout_process(lgp);
7564 dprintk("<-- %s status=%d\n", __func__, status);
7566 return ERR_PTR(status);
7571 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7573 struct nfs4_layoutreturn *lrp = calldata;
7575 dprintk("--> %s\n", __func__);
7576 nfs41_setup_sequence(lrp->clp->cl_session,
7577 &lrp->args.seq_args,
7582 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7584 struct nfs4_layoutreturn *lrp = calldata;
7585 struct nfs_server *server;
7587 dprintk("--> %s\n", __func__);
7589 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7592 server = NFS_SERVER(lrp->args.inode);
7593 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7594 rpc_restart_call_prepare(task);
7597 dprintk("<-- %s\n", __func__);
7600 static void nfs4_layoutreturn_release(void *calldata)
7602 struct nfs4_layoutreturn *lrp = calldata;
7603 struct pnfs_layout_hdr *lo = lrp->args.layout;
7605 dprintk("--> %s\n", __func__);
7606 spin_lock(&lo->plh_inode->i_lock);
7607 if (lrp->res.lrs_present)
7608 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7609 lo->plh_block_lgets--;
7610 spin_unlock(&lo->plh_inode->i_lock);
7611 pnfs_put_layout_hdr(lrp->args.layout);
7613 dprintk("<-- %s\n", __func__);
7616 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7617 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7618 .rpc_call_done = nfs4_layoutreturn_done,
7619 .rpc_release = nfs4_layoutreturn_release,
7622 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7624 struct rpc_task *task;
7625 struct rpc_message msg = {
7626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7627 .rpc_argp = &lrp->args,
7628 .rpc_resp = &lrp->res,
7629 .rpc_cred = lrp->cred,
7631 struct rpc_task_setup task_setup_data = {
7632 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7633 .rpc_message = &msg,
7634 .callback_ops = &nfs4_layoutreturn_call_ops,
7635 .callback_data = lrp,
7639 dprintk("--> %s\n", __func__);
7640 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7641 task = rpc_run_task(&task_setup_data);
7643 return PTR_ERR(task);
7644 status = task->tk_status;
7645 trace_nfs4_layoutreturn(lrp->args.inode, status);
7646 dprintk("<-- %s status=%d\n", __func__, status);
7652 * Retrieve the list of Data Server devices from the MDS.
7654 static int _nfs4_getdevicelist(struct nfs_server *server,
7655 const struct nfs_fh *fh,
7656 struct pnfs_devicelist *devlist)
7658 struct nfs4_getdevicelist_args args = {
7660 .layoutclass = server->pnfs_curr_ld->id,
7662 struct nfs4_getdevicelist_res res = {
7665 struct rpc_message msg = {
7666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7672 dprintk("--> %s\n", __func__);
7673 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7675 dprintk("<-- %s status=%d\n", __func__, status);
7679 int nfs4_proc_getdevicelist(struct nfs_server *server,
7680 const struct nfs_fh *fh,
7681 struct pnfs_devicelist *devlist)
7683 struct nfs4_exception exception = { };
7687 err = nfs4_handle_exception(server,
7688 _nfs4_getdevicelist(server, fh, devlist),
7690 } while (exception.retry);
7692 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7693 err, devlist->num_devs);
7697 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7700 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7701 struct pnfs_device *pdev,
7702 struct rpc_cred *cred)
7704 struct nfs4_getdeviceinfo_args args = {
7707 struct nfs4_getdeviceinfo_res res = {
7710 struct rpc_message msg = {
7711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7718 dprintk("--> %s\n", __func__);
7719 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7720 dprintk("<-- %s status=%d\n", __func__, status);
7725 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7726 struct pnfs_device *pdev,
7727 struct rpc_cred *cred)
7729 struct nfs4_exception exception = { };
7733 err = nfs4_handle_exception(server,
7734 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7736 } while (exception.retry);
7739 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7741 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7743 struct nfs4_layoutcommit_data *data = calldata;
7744 struct nfs_server *server = NFS_SERVER(data->args.inode);
7745 struct nfs4_session *session = nfs4_get_session(server);
7747 nfs41_setup_sequence(session,
7748 &data->args.seq_args,
7754 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7756 struct nfs4_layoutcommit_data *data = calldata;
7757 struct nfs_server *server = NFS_SERVER(data->args.inode);
7759 if (!nfs41_sequence_done(task, &data->res.seq_res))
7762 switch (task->tk_status) { /* Just ignore these failures */
7763 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7764 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7765 case -NFS4ERR_BADLAYOUT: /* no layout */
7766 case -NFS4ERR_GRACE: /* loca_recalim always false */
7767 task->tk_status = 0;
7770 nfs_post_op_update_inode_force_wcc(data->args.inode,
7774 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7775 rpc_restart_call_prepare(task);
7781 static void nfs4_layoutcommit_release(void *calldata)
7783 struct nfs4_layoutcommit_data *data = calldata;
7785 pnfs_cleanup_layoutcommit(data);
7786 put_rpccred(data->cred);
7790 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7791 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7792 .rpc_call_done = nfs4_layoutcommit_done,
7793 .rpc_release = nfs4_layoutcommit_release,
7797 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7799 struct rpc_message msg = {
7800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7801 .rpc_argp = &data->args,
7802 .rpc_resp = &data->res,
7803 .rpc_cred = data->cred,
7805 struct rpc_task_setup task_setup_data = {
7806 .task = &data->task,
7807 .rpc_client = NFS_CLIENT(data->args.inode),
7808 .rpc_message = &msg,
7809 .callback_ops = &nfs4_layoutcommit_ops,
7810 .callback_data = data,
7811 .flags = RPC_TASK_ASYNC,
7813 struct rpc_task *task;
7816 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7817 "lbw: %llu inode %lu\n",
7818 data->task.tk_pid, sync,
7819 data->args.lastbytewritten,
7820 data->args.inode->i_ino);
7822 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7823 task = rpc_run_task(&task_setup_data);
7825 return PTR_ERR(task);
7828 status = nfs4_wait_for_completion_rpc_task(task);
7831 status = task->tk_status;
7832 trace_nfs4_layoutcommit(data->args.inode, status);
7834 dprintk("%s: status %d\n", __func__, status);
7840 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7841 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7844 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7845 struct nfs_fsinfo *info,
7846 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7848 struct nfs41_secinfo_no_name_args args = {
7849 .style = SECINFO_STYLE_CURRENT_FH,
7851 struct nfs4_secinfo_res res = {
7854 struct rpc_message msg = {
7855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7859 struct rpc_clnt *clnt = server->client;
7860 struct rpc_cred *cred = NULL;
7863 if (use_integrity) {
7864 clnt = server->nfs_client->cl_rpcclient;
7865 cred = nfs4_get_clid_cred(server->nfs_client);
7866 msg.rpc_cred = cred;
7869 dprintk("--> %s\n", __func__);
7870 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7872 dprintk("<-- %s status=%d\n", __func__, status);
7881 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7882 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7884 struct nfs4_exception exception = { };
7887 /* first try using integrity protection */
7888 err = -NFS4ERR_WRONGSEC;
7890 /* try to use integrity protection with machine cred */
7891 if (_nfs4_is_integrity_protected(server->nfs_client))
7892 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7896 * if unable to use integrity protection, or SECINFO with
7897 * integrity protection returns NFS4ERR_WRONGSEC (which is
7898 * disallowed by spec, but exists in deployed servers) use
7899 * the current filesystem's rpc_client and the user cred.
7901 if (err == -NFS4ERR_WRONGSEC)
7902 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7907 case -NFS4ERR_WRONGSEC:
7908 case -NFS4ERR_NOTSUPP:
7911 err = nfs4_handle_exception(server, err, &exception);
7913 } while (exception.retry);
7919 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7920 struct nfs_fsinfo *info)
7924 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
7925 struct nfs4_secinfo_flavors *flavors;
7926 struct nfs4_secinfo4 *secinfo;
7929 page = alloc_page(GFP_KERNEL);
7935 flavors = page_address(page);
7936 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7939 * Fall back on "guess and check" method if
7940 * the server doesn't support SECINFO_NO_NAME
7942 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7943 err = nfs4_find_root_sec(server, fhandle, info);
7949 for (i = 0; i < flavors->num_flavors; i++) {
7950 secinfo = &flavors->flavors[i];
7952 switch (secinfo->flavor) {
7956 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7957 &secinfo->flavor_info);
7960 flavor = RPC_AUTH_MAXFLAVOR;
7964 if (!nfs_auth_info_match(&server->auth_info, flavor))
7965 flavor = RPC_AUTH_MAXFLAVOR;
7967 if (flavor != RPC_AUTH_MAXFLAVOR) {
7968 err = nfs4_lookup_root_sec(server, fhandle,
7975 if (flavor == RPC_AUTH_MAXFLAVOR)
7986 static int _nfs41_test_stateid(struct nfs_server *server,
7987 nfs4_stateid *stateid,
7988 struct rpc_cred *cred)
7991 struct nfs41_test_stateid_args args = {
7994 struct nfs41_test_stateid_res res;
7995 struct rpc_message msg = {
7996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8001 struct rpc_clnt *rpc_client = server->client;
8003 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8006 dprintk("NFS call test_stateid %p\n", stateid);
8007 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8008 nfs4_set_sequence_privileged(&args.seq_args);
8009 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8010 &args.seq_args, &res.seq_res);
8011 if (status != NFS_OK) {
8012 dprintk("NFS reply test_stateid: failed, %d\n", status);
8015 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8020 * nfs41_test_stateid - perform a TEST_STATEID operation
8022 * @server: server / transport on which to perform the operation
8023 * @stateid: state ID to test
8026 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8027 * Otherwise a negative NFS4ERR value is returned if the operation
8028 * failed or the state ID is not currently valid.
8030 static int nfs41_test_stateid(struct nfs_server *server,
8031 nfs4_stateid *stateid,
8032 struct rpc_cred *cred)
8034 struct nfs4_exception exception = { };
8037 err = _nfs41_test_stateid(server, stateid, cred);
8038 if (err != -NFS4ERR_DELAY)
8040 nfs4_handle_exception(server, err, &exception);
8041 } while (exception.retry);
8045 struct nfs_free_stateid_data {
8046 struct nfs_server *server;
8047 struct nfs41_free_stateid_args args;
8048 struct nfs41_free_stateid_res res;
8051 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8053 struct nfs_free_stateid_data *data = calldata;
8054 nfs41_setup_sequence(nfs4_get_session(data->server),
8055 &data->args.seq_args,
8060 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8062 struct nfs_free_stateid_data *data = calldata;
8064 nfs41_sequence_done(task, &data->res.seq_res);
8066 switch (task->tk_status) {
8067 case -NFS4ERR_DELAY:
8068 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8069 rpc_restart_call_prepare(task);
8073 static void nfs41_free_stateid_release(void *calldata)
8078 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8079 .rpc_call_prepare = nfs41_free_stateid_prepare,
8080 .rpc_call_done = nfs41_free_stateid_done,
8081 .rpc_release = nfs41_free_stateid_release,
8084 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8085 nfs4_stateid *stateid,
8086 struct rpc_cred *cred,
8089 struct rpc_message msg = {
8090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8093 struct rpc_task_setup task_setup = {
8094 .rpc_client = server->client,
8095 .rpc_message = &msg,
8096 .callback_ops = &nfs41_free_stateid_ops,
8097 .flags = RPC_TASK_ASYNC,
8099 struct nfs_free_stateid_data *data;
8101 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8102 &task_setup.rpc_client, &msg);
8104 dprintk("NFS call free_stateid %p\n", stateid);
8105 data = kmalloc(sizeof(*data), GFP_NOFS);
8107 return ERR_PTR(-ENOMEM);
8108 data->server = server;
8109 nfs4_stateid_copy(&data->args.stateid, stateid);
8111 task_setup.callback_data = data;
8113 msg.rpc_argp = &data->args;
8114 msg.rpc_resp = &data->res;
8115 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8117 nfs4_set_sequence_privileged(&data->args.seq_args);
8119 return rpc_run_task(&task_setup);
8123 * nfs41_free_stateid - perform a FREE_STATEID operation
8125 * @server: server / transport on which to perform the operation
8126 * @stateid: state ID to release
8129 * Returns NFS_OK if the server freed "stateid". Otherwise a
8130 * negative NFS4ERR value is returned.
8132 static int nfs41_free_stateid(struct nfs_server *server,
8133 nfs4_stateid *stateid,
8134 struct rpc_cred *cred)
8136 struct rpc_task *task;
8139 task = _nfs41_free_stateid(server, stateid, cred, true);
8141 return PTR_ERR(task);
8142 ret = rpc_wait_for_completion_task(task);
8144 ret = task->tk_status;
8149 static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8151 struct rpc_task *task;
8152 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8154 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8155 nfs4_free_lock_state(server, lsp);
8157 return PTR_ERR(task);
8162 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8163 const nfs4_stateid *s2)
8165 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8168 if (s1->seqid == s2->seqid)
8170 if (s1->seqid == 0 || s2->seqid == 0)
8176 #endif /* CONFIG_NFS_V4_1 */
8178 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8179 const nfs4_stateid *s2)
8181 return nfs4_stateid_match(s1, s2);
8185 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8186 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8187 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8188 .recover_open = nfs4_open_reclaim,
8189 .recover_lock = nfs4_lock_reclaim,
8190 .establish_clid = nfs4_init_clientid,
8191 .detect_trunking = nfs40_discover_server_trunking,
8194 #if defined(CONFIG_NFS_V4_1)
8195 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8196 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8197 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8198 .recover_open = nfs4_open_reclaim,
8199 .recover_lock = nfs4_lock_reclaim,
8200 .establish_clid = nfs41_init_clientid,
8201 .reclaim_complete = nfs41_proc_reclaim_complete,
8202 .detect_trunking = nfs41_discover_server_trunking,
8204 #endif /* CONFIG_NFS_V4_1 */
8206 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8207 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8208 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8209 .recover_open = nfs4_open_expired,
8210 .recover_lock = nfs4_lock_expired,
8211 .establish_clid = nfs4_init_clientid,
8214 #if defined(CONFIG_NFS_V4_1)
8215 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8216 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8217 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8218 .recover_open = nfs41_open_expired,
8219 .recover_lock = nfs41_lock_expired,
8220 .establish_clid = nfs41_init_clientid,
8222 #endif /* CONFIG_NFS_V4_1 */
8224 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8225 .sched_state_renewal = nfs4_proc_async_renew,
8226 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8227 .renew_lease = nfs4_proc_renew,
8230 #if defined(CONFIG_NFS_V4_1)
8231 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8232 .sched_state_renewal = nfs41_proc_async_sequence,
8233 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8234 .renew_lease = nfs4_proc_sequence,
8238 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8239 .get_locations = _nfs40_proc_get_locations,
8240 .fsid_present = _nfs40_proc_fsid_present,
8243 #if defined(CONFIG_NFS_V4_1)
8244 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8245 .get_locations = _nfs41_proc_get_locations,
8246 .fsid_present = _nfs41_proc_fsid_present,
8248 #endif /* CONFIG_NFS_V4_1 */
8250 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8252 .init_caps = NFS_CAP_READDIRPLUS
8253 | NFS_CAP_ATOMIC_OPEN
8254 | NFS_CAP_CHANGE_ATTR
8255 | NFS_CAP_POSIX_LOCK,
8256 .init_client = nfs40_init_client,
8257 .shutdown_client = nfs40_shutdown_client,
8258 .match_stateid = nfs4_match_stateid,
8259 .find_root_sec = nfs4_find_root_sec,
8260 .free_lock_state = nfs4_release_lockowner,
8261 .call_sync_ops = &nfs40_call_sync_ops,
8262 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8263 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8264 .state_renewal_ops = &nfs40_state_renewal_ops,
8265 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8268 #if defined(CONFIG_NFS_V4_1)
8269 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8271 .init_caps = NFS_CAP_READDIRPLUS
8272 | NFS_CAP_ATOMIC_OPEN
8273 | NFS_CAP_CHANGE_ATTR
8274 | NFS_CAP_POSIX_LOCK
8275 | NFS_CAP_STATEID_NFSV41
8276 | NFS_CAP_ATOMIC_OPEN_V1,
8277 .init_client = nfs41_init_client,
8278 .shutdown_client = nfs41_shutdown_client,
8279 .match_stateid = nfs41_match_stateid,
8280 .find_root_sec = nfs41_find_root_sec,
8281 .free_lock_state = nfs41_free_lock_state,
8282 .call_sync_ops = &nfs41_call_sync_ops,
8283 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8284 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8285 .state_renewal_ops = &nfs41_state_renewal_ops,
8286 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8290 #if defined(CONFIG_NFS_V4_2)
8291 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8293 .init_caps = NFS_CAP_READDIRPLUS
8294 | NFS_CAP_ATOMIC_OPEN
8295 | NFS_CAP_CHANGE_ATTR
8296 | NFS_CAP_POSIX_LOCK
8297 | NFS_CAP_STATEID_NFSV41
8298 | NFS_CAP_ATOMIC_OPEN_V1,
8299 .init_client = nfs41_init_client,
8300 .shutdown_client = nfs41_shutdown_client,
8301 .match_stateid = nfs41_match_stateid,
8302 .find_root_sec = nfs41_find_root_sec,
8303 .free_lock_state = nfs41_free_lock_state,
8304 .call_sync_ops = &nfs41_call_sync_ops,
8305 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8306 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8307 .state_renewal_ops = &nfs41_state_renewal_ops,
8311 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8312 [0] = &nfs_v4_0_minor_ops,
8313 #if defined(CONFIG_NFS_V4_1)
8314 [1] = &nfs_v4_1_minor_ops,
8316 #if defined(CONFIG_NFS_V4_2)
8317 [2] = &nfs_v4_2_minor_ops,
8321 static const struct inode_operations nfs4_dir_inode_operations = {
8322 .create = nfs_create,
8323 .lookup = nfs_lookup,
8324 .atomic_open = nfs_atomic_open,
8326 .unlink = nfs_unlink,
8327 .symlink = nfs_symlink,
8331 .rename = nfs_rename,
8332 .permission = nfs_permission,
8333 .getattr = nfs_getattr,
8334 .setattr = nfs_setattr,
8335 .getxattr = generic_getxattr,
8336 .setxattr = generic_setxattr,
8337 .listxattr = generic_listxattr,
8338 .removexattr = generic_removexattr,
8341 static const struct inode_operations nfs4_file_inode_operations = {
8342 .permission = nfs_permission,
8343 .getattr = nfs_getattr,
8344 .setattr = nfs_setattr,
8345 .getxattr = generic_getxattr,
8346 .setxattr = generic_setxattr,
8347 .listxattr = generic_listxattr,
8348 .removexattr = generic_removexattr,
8351 const struct nfs_rpc_ops nfs_v4_clientops = {
8352 .version = 4, /* protocol version */
8353 .dentry_ops = &nfs4_dentry_operations,
8354 .dir_inode_ops = &nfs4_dir_inode_operations,
8355 .file_inode_ops = &nfs4_file_inode_operations,
8356 .file_ops = &nfs4_file_operations,
8357 .getroot = nfs4_proc_get_root,
8358 .submount = nfs4_submount,
8359 .try_mount = nfs4_try_mount,
8360 .getattr = nfs4_proc_getattr,
8361 .setattr = nfs4_proc_setattr,
8362 .lookup = nfs4_proc_lookup,
8363 .access = nfs4_proc_access,
8364 .readlink = nfs4_proc_readlink,
8365 .create = nfs4_proc_create,
8366 .remove = nfs4_proc_remove,
8367 .unlink_setup = nfs4_proc_unlink_setup,
8368 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8369 .unlink_done = nfs4_proc_unlink_done,
8370 .rename = nfs4_proc_rename,
8371 .rename_setup = nfs4_proc_rename_setup,
8372 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8373 .rename_done = nfs4_proc_rename_done,
8374 .link = nfs4_proc_link,
8375 .symlink = nfs4_proc_symlink,
8376 .mkdir = nfs4_proc_mkdir,
8377 .rmdir = nfs4_proc_remove,
8378 .readdir = nfs4_proc_readdir,
8379 .mknod = nfs4_proc_mknod,
8380 .statfs = nfs4_proc_statfs,
8381 .fsinfo = nfs4_proc_fsinfo,
8382 .pathconf = nfs4_proc_pathconf,
8383 .set_capabilities = nfs4_server_capabilities,
8384 .decode_dirent = nfs4_decode_dirent,
8385 .read_setup = nfs4_proc_read_setup,
8386 .read_pageio_init = pnfs_pageio_init_read,
8387 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
8388 .read_done = nfs4_read_done,
8389 .write_setup = nfs4_proc_write_setup,
8390 .write_pageio_init = pnfs_pageio_init_write,
8391 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
8392 .write_done = nfs4_write_done,
8393 .commit_setup = nfs4_proc_commit_setup,
8394 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8395 .commit_done = nfs4_commit_done,
8396 .lock = nfs4_proc_lock,
8397 .clear_acl_cache = nfs4_zap_acl_attr,
8398 .close_context = nfs4_close_context,
8399 .open_context = nfs4_atomic_open,
8400 .have_delegation = nfs4_have_delegation,
8401 .return_delegation = nfs4_inode_return_delegation,
8402 .alloc_client = nfs4_alloc_client,
8403 .init_client = nfs4_init_client,
8404 .free_client = nfs4_free_client,
8405 .create_server = nfs4_create_server,
8406 .clone_server = nfs_clone_server,
8409 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8410 .prefix = XATTR_NAME_NFSV4_ACL,
8411 .list = nfs4_xattr_list_nfs4_acl,
8412 .get = nfs4_xattr_get_nfs4_acl,
8413 .set = nfs4_xattr_set_nfs4_acl,
8416 const struct xattr_handler *nfs4_xattr_handlers[] = {
8417 &nfs4_xattr_nfs4_acl_handler,
8418 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8419 &nfs4_xattr_nfs4_label_handler,