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 #define NFSDBG_FACILITY NFSDBG_PROC
71 #define NFS4_POLL_RETRY_MIN (HZ/10)
72 #define NFS4_POLL_RETRY_MAX (15*HZ)
75 static int _nfs4_proc_open(struct nfs4_opendata *data);
76 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
77 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
78 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
79 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
80 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
81 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
82 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
85 #ifdef CONFIG_NFS_V4_1
86 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
89 /* Prevent leaks of NFSv4 errors into userland */
90 static int nfs4_map_errors(int err)
95 case -NFS4ERR_RESOURCE:
96 case -NFS4ERR_LAYOUTTRYLATER:
97 case -NFS4ERR_RECALLCONFLICT:
99 case -NFS4ERR_WRONGSEC:
101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
104 case -NFS4ERR_SHARE_DENIED:
106 case -NFS4ERR_MINOR_VERS_MISMATCH:
107 return -EPROTONOSUPPORT;
108 case -NFS4ERR_ACCESS:
110 case -NFS4ERR_FILE_OPEN:
113 dprintk("%s could not handle NFSv4 error %d\n",
121 * This is our standard bitmap for GETATTR requests.
123 const u32 nfs4_fattr_bitmap[3] = {
125 | FATTR4_WORD0_CHANGE
128 | FATTR4_WORD0_FILEID,
130 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
140 static const u32 nfs4_pnfs_open_bitmap[3] = {
142 | FATTR4_WORD0_CHANGE
145 | FATTR4_WORD0_FILEID,
147 | FATTR4_WORD1_NUMLINKS
149 | FATTR4_WORD1_OWNER_GROUP
150 | FATTR4_WORD1_RAWDEV
151 | FATTR4_WORD1_SPACE_USED
152 | FATTR4_WORD1_TIME_ACCESS
153 | FATTR4_WORD1_TIME_METADATA
154 | FATTR4_WORD1_TIME_MODIFY,
155 FATTR4_WORD2_MDSTHRESHOLD
158 static const u32 nfs4_open_noattr_bitmap[3] = {
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_FILEID,
164 const u32 nfs4_statfs_bitmap[2] = {
165 FATTR4_WORD0_FILES_AVAIL
166 | FATTR4_WORD0_FILES_FREE
167 | FATTR4_WORD0_FILES_TOTAL,
168 FATTR4_WORD1_SPACE_AVAIL
169 | FATTR4_WORD1_SPACE_FREE
170 | FATTR4_WORD1_SPACE_TOTAL
173 const u32 nfs4_pathconf_bitmap[2] = {
175 | FATTR4_WORD0_MAXNAME,
179 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
180 | FATTR4_WORD0_MAXREAD
181 | FATTR4_WORD0_MAXWRITE
182 | FATTR4_WORD0_LEASE_TIME,
183 FATTR4_WORD1_TIME_DELTA
184 | FATTR4_WORD1_FS_LAYOUT_TYPES,
185 FATTR4_WORD2_LAYOUT_BLKSIZE
188 const u32 nfs4_fs_locations_bitmap[2] = {
190 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_FILEID
194 | FATTR4_WORD0_FS_LOCATIONS,
196 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID
207 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
208 struct nfs4_readdir_arg *readdir)
213 readdir->cookie = cookie;
214 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
219 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
224 * NFSv4 servers do not return entries for '.' and '..'
225 * Therefore, we fake these entries here. We let '.'
226 * have cookie 0 and '..' have cookie 1. Note that
227 * when talking to the server, we always send cookie 0
230 start = p = kmap_atomic(*readdir->pages);
233 *p++ = xdr_one; /* next */
234 *p++ = xdr_zero; /* cookie, first word */
235 *p++ = xdr_one; /* cookie, second word */
236 *p++ = xdr_one; /* entry len */
237 memcpy(p, ".\0\0\0", 4); /* entry */
239 *p++ = xdr_one; /* bitmap length */
240 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
241 *p++ = htonl(8); /* attribute buffer length */
242 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
245 *p++ = xdr_one; /* next */
246 *p++ = xdr_zero; /* cookie, first word */
247 *p++ = xdr_two; /* cookie, second word */
248 *p++ = xdr_two; /* entry len */
249 memcpy(p, "..\0\0", 4); /* entry */
251 *p++ = xdr_one; /* bitmap length */
252 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
253 *p++ = htonl(8); /* attribute buffer length */
254 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
256 readdir->pgbase = (char *)p - (char *)start;
257 readdir->count -= readdir->pgbase;
258 kunmap_atomic(start);
261 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
268 *timeout = NFS4_POLL_RETRY_MIN;
269 if (*timeout > NFS4_POLL_RETRY_MAX)
270 *timeout = NFS4_POLL_RETRY_MAX;
271 freezable_schedule_timeout_killable(*timeout);
272 if (fatal_signal_pending(current))
278 /* This is the error handling routine for processes that are allowed
281 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
283 struct nfs_client *clp = server->nfs_client;
284 struct nfs4_state *state = exception->state;
285 struct inode *inode = exception->inode;
288 exception->retry = 0;
292 case -NFS4ERR_OPENMODE:
293 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
294 nfs4_inode_return_delegation(inode);
295 exception->retry = 1;
300 ret = nfs4_schedule_stateid_recovery(server, state);
303 goto wait_on_recovery;
304 case -NFS4ERR_DELEG_REVOKED:
305 case -NFS4ERR_ADMIN_REVOKED:
306 case -NFS4ERR_BAD_STATEID:
309 nfs_remove_bad_delegation(state->inode);
310 ret = nfs4_schedule_stateid_recovery(server, state);
313 goto wait_on_recovery;
314 case -NFS4ERR_EXPIRED:
316 ret = nfs4_schedule_stateid_recovery(server, state);
320 case -NFS4ERR_STALE_STATEID:
321 case -NFS4ERR_STALE_CLIENTID:
322 nfs4_schedule_lease_recovery(clp);
323 goto wait_on_recovery;
324 #if defined(CONFIG_NFS_V4_1)
325 case -NFS4ERR_BADSESSION:
326 case -NFS4ERR_BADSLOT:
327 case -NFS4ERR_BAD_HIGH_SLOT:
328 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
329 case -NFS4ERR_DEADSESSION:
330 case -NFS4ERR_SEQ_FALSE_RETRY:
331 case -NFS4ERR_SEQ_MISORDERED:
332 dprintk("%s ERROR: %d Reset session\n", __func__,
334 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
335 goto wait_on_recovery;
336 #endif /* defined(CONFIG_NFS_V4_1) */
337 case -NFS4ERR_FILE_OPEN:
338 if (exception->timeout > HZ) {
339 /* We have retried a decent amount, time to
347 ret = nfs4_delay(server->client, &exception->timeout);
350 case -NFS4ERR_RETRY_UNCACHED_REP:
351 case -NFS4ERR_OLD_STATEID:
352 exception->retry = 1;
354 case -NFS4ERR_BADOWNER:
355 /* The following works around a Linux server bug! */
356 case -NFS4ERR_BADNAME:
357 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
358 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
359 exception->retry = 1;
360 printk(KERN_WARNING "NFS: v4 server %s "
361 "does not accept raw "
363 "Reenabling the idmapper.\n",
364 server->nfs_client->cl_hostname);
367 /* We failed to handle the error */
368 return nfs4_map_errors(ret);
370 ret = nfs4_wait_clnt_recover(clp);
372 exception->retry = 1;
377 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
379 spin_lock(&clp->cl_lock);
380 if (time_before(clp->cl_last_renewal,timestamp))
381 clp->cl_last_renewal = timestamp;
382 spin_unlock(&clp->cl_lock);
385 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
387 do_renew_lease(server->nfs_client, timestamp);
390 #if defined(CONFIG_NFS_V4_1)
392 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
394 struct nfs4_session *session;
395 struct nfs4_slot_table *tbl;
396 bool send_new_highest_used_slotid = false;
399 /* just wake up the next guy waiting since
400 * we may have not consumed a slot after all */
401 dprintk("%s: No slot\n", __func__);
404 tbl = res->sr_slot->table;
405 session = tbl->session;
407 spin_lock(&tbl->slot_tbl_lock);
408 /* Be nice to the server: try to ensure that the last transmitted
409 * value for highest_user_slotid <= target_highest_slotid
411 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
412 send_new_highest_used_slotid = true;
414 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
415 send_new_highest_used_slotid = false;
418 nfs4_free_slot(tbl, res->sr_slot);
420 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
421 send_new_highest_used_slotid = false;
423 spin_unlock(&tbl->slot_tbl_lock);
425 if (send_new_highest_used_slotid)
426 nfs41_server_notify_highest_slotid_update(session->clp);
429 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
431 struct nfs4_session *session;
432 struct nfs4_slot *slot;
433 struct nfs_client *clp;
434 bool interrupted = false;
437 /* don't increment the sequence number if the task wasn't sent */
438 if (!RPC_WAS_SENT(task))
442 session = slot->table->session;
444 if (slot->interrupted) {
445 slot->interrupted = 0;
449 /* Check the SEQUENCE operation status */
450 switch (res->sr_status) {
452 /* Update the slot's sequence and clientid lease timer */
455 do_renew_lease(clp, res->sr_timestamp);
456 /* Check sequence flags */
457 if (res->sr_status_flags != 0)
458 nfs4_schedule_lease_recovery(clp);
459 nfs41_update_target_slotid(slot->table, slot, res);
463 * sr_status remains 1 if an RPC level error occurred.
464 * The server may or may not have processed the sequence
466 * Mark the slot as having hosted an interrupted RPC call.
468 slot->interrupted = 1;
471 /* The server detected a resend of the RPC call and
472 * returned NFS4ERR_DELAY as per Section 2.10.6.2
475 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
480 case -NFS4ERR_BADSLOT:
482 * The slot id we used was probably retired. Try again
483 * using a different slot id.
486 case -NFS4ERR_SEQ_MISORDERED:
488 * Was the last operation on this sequence interrupted?
489 * If so, retry after bumping the sequence number.
496 * Could this slot have been previously retired?
497 * If so, then the server may be expecting seq_nr = 1!
499 if (slot->seq_nr != 1) {
504 case -NFS4ERR_SEQ_FALSE_RETRY:
508 /* Just update the slot sequence no. */
512 /* The session may be reset by one of the error handlers. */
513 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
514 nfs41_sequence_free_slot(res);
517 if (rpc_restart_call_prepare(task)) {
523 if (!rpc_restart_call(task))
525 rpc_delay(task, NFS4_POLL_RETRY_MAX);
529 static int nfs4_sequence_done(struct rpc_task *task,
530 struct nfs4_sequence_res *res)
532 if (res->sr_slot == NULL)
534 return nfs41_sequence_done(task, res);
537 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res, int cache_reply)
540 args->sa_slot = NULL;
541 args->sa_cache_this = 0;
542 args->sa_privileged = 0;
544 args->sa_cache_this = 1;
548 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
550 args->sa_privileged = 1;
553 int nfs41_setup_sequence(struct nfs4_session *session,
554 struct nfs4_sequence_args *args,
555 struct nfs4_sequence_res *res,
556 struct rpc_task *task)
558 struct nfs4_slot *slot;
559 struct nfs4_slot_table *tbl;
561 dprintk("--> %s\n", __func__);
562 /* slot already allocated? */
563 if (res->sr_slot != NULL)
566 tbl = &session->fc_slot_table;
568 task->tk_timeout = 0;
570 spin_lock(&tbl->slot_tbl_lock);
571 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
572 !args->sa_privileged) {
573 /* The state manager will wait until the slot table is empty */
574 dprintk("%s session is draining\n", __func__);
578 slot = nfs4_alloc_slot(tbl);
580 /* If out of memory, try again in 1/4 second */
581 if (slot == ERR_PTR(-ENOMEM))
582 task->tk_timeout = HZ >> 2;
583 dprintk("<-- %s: no free slots\n", __func__);
586 spin_unlock(&tbl->slot_tbl_lock);
588 args->sa_slot = slot;
590 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
591 slot->slot_nr, slot->seq_nr);
594 res->sr_timestamp = jiffies;
595 res->sr_status_flags = 0;
597 * sr_status is only set in decode_sequence, and so will remain
598 * set to 1 if an rpc level failure occurs.
602 rpc_call_start(task);
605 /* Privileged tasks are queued with top priority */
606 if (args->sa_privileged)
607 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
608 NULL, RPC_PRIORITY_PRIVILEGED);
610 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
611 spin_unlock(&tbl->slot_tbl_lock);
614 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
616 int nfs4_setup_sequence(const struct nfs_server *server,
617 struct nfs4_sequence_args *args,
618 struct nfs4_sequence_res *res,
619 struct rpc_task *task)
621 struct nfs4_session *session = nfs4_get_session(server);
624 if (session == NULL) {
625 rpc_call_start(task);
629 dprintk("--> %s clp %p session %p sr_slot %d\n",
630 __func__, session->clp, session, res->sr_slot ?
631 res->sr_slot->slot_nr : -1);
633 ret = nfs41_setup_sequence(session, args, res, task);
635 dprintk("<-- %s status=%d\n", __func__, ret);
639 struct nfs41_call_sync_data {
640 const struct nfs_server *seq_server;
641 struct nfs4_sequence_args *seq_args;
642 struct nfs4_sequence_res *seq_res;
645 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
647 struct nfs41_call_sync_data *data = calldata;
648 struct nfs4_session *session = nfs4_get_session(data->seq_server);
650 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
652 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
655 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
657 struct nfs41_call_sync_data *data = calldata;
659 nfs41_sequence_done(task, data->seq_res);
662 static const struct rpc_call_ops nfs41_call_sync_ops = {
663 .rpc_call_prepare = nfs41_call_sync_prepare,
664 .rpc_call_done = nfs41_call_sync_done,
667 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
668 struct nfs_server *server,
669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
671 struct nfs4_sequence_res *res)
674 struct rpc_task *task;
675 struct nfs41_call_sync_data data = {
676 .seq_server = server,
680 struct rpc_task_setup task_setup = {
683 .callback_ops = &nfs41_call_sync_ops,
684 .callback_data = &data
687 task = rpc_run_task(&task_setup);
691 ret = task->tk_status;
699 void nfs41_init_sequence(struct nfs4_sequence_args *args,
700 struct nfs4_sequence_res *res, int cache_reply)
704 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
709 static int nfs4_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
714 #endif /* CONFIG_NFS_V4_1 */
717 int _nfs4_call_sync(struct rpc_clnt *clnt,
718 struct nfs_server *server,
719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
721 struct nfs4_sequence_res *res)
723 return rpc_call_sync(clnt, msg, 0);
727 int nfs4_call_sync(struct rpc_clnt *clnt,
728 struct nfs_server *server,
729 struct rpc_message *msg,
730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
734 nfs41_init_sequence(args, res, cache_reply);
735 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
739 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
741 struct nfs_inode *nfsi = NFS_I(dir);
743 spin_lock(&dir->i_lock);
744 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
745 if (!cinfo->atomic || cinfo->before != dir->i_version)
746 nfs_force_lookup_revalidate(dir);
747 dir->i_version = cinfo->after;
748 nfs_fscache_invalidate(dir);
749 spin_unlock(&dir->i_lock);
752 struct nfs4_opendata {
754 struct nfs_openargs o_arg;
755 struct nfs_openres o_res;
756 struct nfs_open_confirmargs c_arg;
757 struct nfs_open_confirmres c_res;
758 struct nfs4_string owner_name;
759 struct nfs4_string group_name;
760 struct nfs_fattr f_attr;
762 struct dentry *dentry;
763 struct nfs4_state_owner *owner;
764 struct nfs4_state *state;
766 unsigned long timestamp;
767 unsigned int rpc_done : 1;
772 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
773 int err, struct nfs4_exception *exception)
777 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
779 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
780 exception->retry = 1;
784 static enum open_claim_type4
785 nfs4_map_atomic_open_claim(struct nfs_server *server,
786 enum open_claim_type4 claim)
788 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
793 case NFS4_OPEN_CLAIM_FH:
794 return NFS4_OPEN_CLAIM_NULL;
795 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
796 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
797 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
798 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
802 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
804 p->o_res.f_attr = &p->f_attr;
805 p->o_res.seqid = p->o_arg.seqid;
806 p->c_res.seqid = p->c_arg.seqid;
807 p->o_res.server = p->o_arg.server;
808 p->o_res.access_request = p->o_arg.access;
809 nfs_fattr_init(&p->f_attr);
810 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
813 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
814 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
815 const struct iattr *attrs,
816 enum open_claim_type4 claim,
819 struct dentry *parent = dget_parent(dentry);
820 struct inode *dir = parent->d_inode;
821 struct nfs_server *server = NFS_SERVER(dir);
822 struct nfs4_opendata *p;
824 p = kzalloc(sizeof(*p), gfp_mask);
827 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
828 if (p->o_arg.seqid == NULL)
830 nfs_sb_active(dentry->d_sb);
831 p->dentry = dget(dentry);
834 atomic_inc(&sp->so_count);
835 p->o_arg.open_flags = flags;
836 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
837 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
838 * will return permission denied for all bits until close */
839 if (!(flags & O_EXCL)) {
840 /* ask server to check for all possible rights as results
842 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
843 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
845 p->o_arg.clientid = server->nfs_client->cl_clientid;
846 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
847 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
848 p->o_arg.name = &dentry->d_name;
849 p->o_arg.server = server;
850 p->o_arg.bitmask = server->attr_bitmask;
851 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
852 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
853 switch (p->o_arg.claim) {
854 case NFS4_OPEN_CLAIM_NULL:
855 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
856 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
857 p->o_arg.fh = NFS_FH(dir);
859 case NFS4_OPEN_CLAIM_PREVIOUS:
860 case NFS4_OPEN_CLAIM_FH:
861 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
862 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
863 p->o_arg.fh = NFS_FH(dentry->d_inode);
865 if (attrs != NULL && attrs->ia_valid != 0) {
868 p->o_arg.u.attrs = &p->attrs;
869 memcpy(&p->attrs, attrs, sizeof(p->attrs));
872 verf[1] = current->pid;
873 memcpy(p->o_arg.u.verifier.data, verf,
874 sizeof(p->o_arg.u.verifier.data));
876 p->c_arg.fh = &p->o_res.fh;
877 p->c_arg.stateid = &p->o_res.stateid;
878 p->c_arg.seqid = p->o_arg.seqid;
879 nfs4_init_opendata_res(p);
889 static void nfs4_opendata_free(struct kref *kref)
891 struct nfs4_opendata *p = container_of(kref,
892 struct nfs4_opendata, kref);
893 struct super_block *sb = p->dentry->d_sb;
895 nfs_free_seqid(p->o_arg.seqid);
896 if (p->state != NULL)
897 nfs4_put_open_state(p->state);
898 nfs4_put_state_owner(p->owner);
902 nfs_fattr_free_names(&p->f_attr);
906 static void nfs4_opendata_put(struct nfs4_opendata *p)
909 kref_put(&p->kref, nfs4_opendata_free);
912 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
916 ret = rpc_wait_for_completion_task(task);
920 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
924 if (open_mode & (O_EXCL|O_TRUNC))
926 switch (mode & (FMODE_READ|FMODE_WRITE)) {
928 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
929 && state->n_rdonly != 0;
932 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
933 && state->n_wronly != 0;
935 case FMODE_READ|FMODE_WRITE:
936 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
937 && state->n_rdwr != 0;
943 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
945 if (delegation == NULL)
947 if ((delegation->type & fmode) != fmode)
949 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
951 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
953 nfs_mark_delegation_referenced(delegation);
957 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
966 case FMODE_READ|FMODE_WRITE:
969 nfs4_state_set_mode_locked(state, state->state | fmode);
972 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
974 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
975 nfs4_stateid_copy(&state->stateid, stateid);
976 nfs4_stateid_copy(&state->open_stateid, stateid);
979 set_bit(NFS_O_RDONLY_STATE, &state->flags);
982 set_bit(NFS_O_WRONLY_STATE, &state->flags);
984 case FMODE_READ|FMODE_WRITE:
985 set_bit(NFS_O_RDWR_STATE, &state->flags);
989 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
991 write_seqlock(&state->seqlock);
992 nfs_set_open_stateid_locked(state, stateid, fmode);
993 write_sequnlock(&state->seqlock);
996 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
999 * Protect the call to nfs4_state_set_mode_locked and
1000 * serialise the stateid update
1002 write_seqlock(&state->seqlock);
1003 if (deleg_stateid != NULL) {
1004 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1005 set_bit(NFS_DELEGATED_STATE, &state->flags);
1007 if (open_stateid != NULL)
1008 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1009 write_sequnlock(&state->seqlock);
1010 spin_lock(&state->owner->so_lock);
1011 update_open_stateflags(state, fmode);
1012 spin_unlock(&state->owner->so_lock);
1015 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *deleg_cur;
1021 fmode &= (FMODE_READ|FMODE_WRITE);
1024 deleg_cur = rcu_dereference(nfsi->delegation);
1025 if (deleg_cur == NULL)
1028 spin_lock(&deleg_cur->lock);
1029 if (nfsi->delegation != deleg_cur ||
1030 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1031 (deleg_cur->type & fmode) != fmode)
1032 goto no_delegation_unlock;
1034 if (delegation == NULL)
1035 delegation = &deleg_cur->stateid;
1036 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1037 goto no_delegation_unlock;
1039 nfs_mark_delegation_referenced(deleg_cur);
1040 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1042 no_delegation_unlock:
1043 spin_unlock(&deleg_cur->lock);
1047 if (!ret && open_stateid != NULL) {
1048 __update_open_stateid(state, open_stateid, NULL, fmode);
1056 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1058 struct nfs_delegation *delegation;
1061 delegation = rcu_dereference(NFS_I(inode)->delegation);
1062 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1067 nfs4_inode_return_delegation(inode);
1070 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1072 struct nfs4_state *state = opendata->state;
1073 struct nfs_inode *nfsi = NFS_I(state->inode);
1074 struct nfs_delegation *delegation;
1075 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1076 fmode_t fmode = opendata->o_arg.fmode;
1077 nfs4_stateid stateid;
1081 if (can_open_cached(state, fmode, open_mode)) {
1082 spin_lock(&state->owner->so_lock);
1083 if (can_open_cached(state, fmode, open_mode)) {
1084 update_open_stateflags(state, fmode);
1085 spin_unlock(&state->owner->so_lock);
1086 goto out_return_state;
1088 spin_unlock(&state->owner->so_lock);
1091 delegation = rcu_dereference(nfsi->delegation);
1092 if (!can_open_delegated(delegation, fmode)) {
1096 /* Save the delegation */
1097 nfs4_stateid_copy(&stateid, &delegation->stateid);
1099 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1104 /* Try to update the stateid using the delegation */
1105 if (update_open_stateid(state, NULL, &stateid, fmode))
1106 goto out_return_state;
1109 return ERR_PTR(ret);
1111 atomic_inc(&state->count);
1116 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1118 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1119 struct nfs_delegation *delegation;
1120 int delegation_flags = 0;
1123 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1125 delegation_flags = delegation->flags;
1127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
1132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1143 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1144 * and update the nfs4_state.
1146 static struct nfs4_state *
1147 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1149 struct inode *inode = data->state->inode;
1150 struct nfs4_state *state = data->state;
1153 if (!data->rpc_done) {
1154 ret = data->rpc_status;
1159 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1160 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1161 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1165 state = nfs4_get_open_state(inode, data->owner);
1169 ret = nfs_refresh_inode(inode, &data->f_attr);
1173 if (data->o_res.delegation_type != 0)
1174 nfs4_opendata_check_deleg(data, state);
1175 update_open_stateid(state, &data->o_res.stateid, NULL,
1180 return ERR_PTR(ret);
1184 static struct nfs4_state *
1185 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1187 struct inode *inode;
1188 struct nfs4_state *state = NULL;
1191 if (!data->rpc_done) {
1192 state = nfs4_try_open_cached(data);
1197 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1199 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1200 ret = PTR_ERR(inode);
1204 state = nfs4_get_open_state(inode, data->owner);
1207 if (data->o_res.delegation_type != 0)
1208 nfs4_opendata_check_deleg(data, state);
1209 update_open_stateid(state, &data->o_res.stateid, NULL,
1213 nfs_release_seqid(data->o_arg.seqid);
1218 return ERR_PTR(ret);
1221 static struct nfs4_state *
1222 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1224 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1225 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1226 return _nfs4_opendata_to_nfs4_state(data);
1229 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1231 struct nfs_inode *nfsi = NFS_I(state->inode);
1232 struct nfs_open_context *ctx;
1234 spin_lock(&state->inode->i_lock);
1235 list_for_each_entry(ctx, &nfsi->open_files, list) {
1236 if (ctx->state != state)
1238 get_nfs_open_context(ctx);
1239 spin_unlock(&state->inode->i_lock);
1242 spin_unlock(&state->inode->i_lock);
1243 return ERR_PTR(-ENOENT);
1246 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1247 struct nfs4_state *state, enum open_claim_type4 claim)
1249 struct nfs4_opendata *opendata;
1251 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1252 NULL, claim, GFP_NOFS);
1253 if (opendata == NULL)
1254 return ERR_PTR(-ENOMEM);
1255 opendata->state = state;
1256 atomic_inc(&state->count);
1260 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1262 struct nfs4_state *newstate;
1265 opendata->o_arg.open_flags = 0;
1266 opendata->o_arg.fmode = fmode;
1267 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1268 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1269 nfs4_init_opendata_res(opendata);
1270 ret = _nfs4_recover_proc_open(opendata);
1273 newstate = nfs4_opendata_to_nfs4_state(opendata);
1274 if (IS_ERR(newstate))
1275 return PTR_ERR(newstate);
1276 nfs4_close_state(newstate, fmode);
1281 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1283 struct nfs4_state *newstate;
1286 /* memory barrier prior to reading state->n_* */
1287 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1289 if (state->n_rdwr != 0) {
1290 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1291 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1294 if (newstate != state)
1297 if (state->n_wronly != 0) {
1298 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1299 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1302 if (newstate != state)
1305 if (state->n_rdonly != 0) {
1306 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1307 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1310 if (newstate != state)
1314 * We may have performed cached opens for all three recoveries.
1315 * Check if we need to update the current stateid.
1317 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1318 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1319 write_seqlock(&state->seqlock);
1320 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1321 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1322 write_sequnlock(&state->seqlock);
1329 * reclaim state on the server after a reboot.
1331 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1333 struct nfs_delegation *delegation;
1334 struct nfs4_opendata *opendata;
1335 fmode_t delegation_type = 0;
1338 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1339 NFS4_OPEN_CLAIM_PREVIOUS);
1340 if (IS_ERR(opendata))
1341 return PTR_ERR(opendata);
1343 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1344 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1345 delegation_type = delegation->type;
1347 opendata->o_arg.u.delegation_type = delegation_type;
1348 status = nfs4_open_recover(opendata, state);
1349 nfs4_opendata_put(opendata);
1353 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1355 struct nfs_server *server = NFS_SERVER(state->inode);
1356 struct nfs4_exception exception = { };
1359 err = _nfs4_do_open_reclaim(ctx, state);
1360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1362 if (err != -NFS4ERR_DELAY)
1364 nfs4_handle_exception(server, err, &exception);
1365 } while (exception.retry);
1369 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1371 struct nfs_open_context *ctx;
1374 ctx = nfs4_state_find_open_context(state);
1377 ret = nfs4_do_open_reclaim(ctx, state);
1378 put_nfs_open_context(ctx);
1382 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1384 struct nfs4_opendata *opendata;
1387 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1388 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1389 if (IS_ERR(opendata))
1390 return PTR_ERR(opendata);
1391 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1392 ret = nfs4_open_recover(opendata, state);
1393 nfs4_opendata_put(opendata);
1397 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1399 struct nfs4_exception exception = { };
1400 struct nfs_server *server = NFS_SERVER(state->inode);
1403 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1409 case -NFS4ERR_BADSESSION:
1410 case -NFS4ERR_BADSLOT:
1411 case -NFS4ERR_BAD_HIGH_SLOT:
1412 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1413 case -NFS4ERR_DEADSESSION:
1414 set_bit(NFS_DELEGATED_STATE, &state->flags);
1415 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1418 case -NFS4ERR_STALE_CLIENTID:
1419 case -NFS4ERR_STALE_STATEID:
1420 set_bit(NFS_DELEGATED_STATE, &state->flags);
1421 case -NFS4ERR_EXPIRED:
1422 /* Don't recall a delegation if it was lost */
1423 nfs4_schedule_lease_recovery(server->nfs_client);
1426 case -NFS4ERR_DELEG_REVOKED:
1427 case -NFS4ERR_ADMIN_REVOKED:
1428 case -NFS4ERR_BAD_STATEID:
1429 nfs_inode_find_state_and_recover(state->inode,
1431 nfs4_schedule_stateid_recovery(server, state);
1436 set_bit(NFS_DELEGATED_STATE, &state->flags);
1437 err = nfs4_handle_exception(server, err, &exception);
1438 } while (exception.retry);
1443 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1445 struct nfs4_opendata *data = calldata;
1447 data->rpc_status = task->tk_status;
1448 if (data->rpc_status == 0) {
1449 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1450 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1451 renew_lease(data->o_res.server, data->timestamp);
1456 static void nfs4_open_confirm_release(void *calldata)
1458 struct nfs4_opendata *data = calldata;
1459 struct nfs4_state *state = NULL;
1461 /* If this request hasn't been cancelled, do nothing */
1462 if (data->cancelled == 0)
1464 /* In case of error, no cleanup! */
1465 if (!data->rpc_done)
1467 state = nfs4_opendata_to_nfs4_state(data);
1469 nfs4_close_state(state, data->o_arg.fmode);
1471 nfs4_opendata_put(data);
1474 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1475 .rpc_call_done = nfs4_open_confirm_done,
1476 .rpc_release = nfs4_open_confirm_release,
1480 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1482 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1484 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1485 struct rpc_task *task;
1486 struct rpc_message msg = {
1487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1488 .rpc_argp = &data->c_arg,
1489 .rpc_resp = &data->c_res,
1490 .rpc_cred = data->owner->so_cred,
1492 struct rpc_task_setup task_setup_data = {
1493 .rpc_client = server->client,
1494 .rpc_message = &msg,
1495 .callback_ops = &nfs4_open_confirm_ops,
1496 .callback_data = data,
1497 .workqueue = nfsiod_workqueue,
1498 .flags = RPC_TASK_ASYNC,
1502 kref_get(&data->kref);
1504 data->rpc_status = 0;
1505 data->timestamp = jiffies;
1506 task = rpc_run_task(&task_setup_data);
1508 return PTR_ERR(task);
1509 status = nfs4_wait_for_completion_rpc_task(task);
1511 data->cancelled = 1;
1514 status = data->rpc_status;
1519 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1521 struct nfs4_opendata *data = calldata;
1522 struct nfs4_state_owner *sp = data->owner;
1524 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1527 * Check if we still need to send an OPEN call, or if we can use
1528 * a delegation instead.
1530 if (data->state != NULL) {
1531 struct nfs_delegation *delegation;
1533 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1536 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1537 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1538 can_open_delegated(delegation, data->o_arg.fmode))
1539 goto unlock_no_action;
1542 /* Update client id. */
1543 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1544 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1545 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1546 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1547 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1549 data->timestamp = jiffies;
1550 if (nfs4_setup_sequence(data->o_arg.server,
1551 &data->o_arg.seq_args,
1552 &data->o_res.seq_res,
1554 nfs_release_seqid(data->o_arg.seqid);
1559 task->tk_action = NULL;
1561 nfs4_sequence_done(task, &data->o_res.seq_res);
1564 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1566 struct nfs4_opendata *data = calldata;
1568 data->rpc_status = task->tk_status;
1570 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1573 if (task->tk_status == 0) {
1574 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1575 switch (data->o_res.f_attr->mode & S_IFMT) {
1579 data->rpc_status = -ELOOP;
1582 data->rpc_status = -EISDIR;
1585 data->rpc_status = -ENOTDIR;
1588 renew_lease(data->o_res.server, data->timestamp);
1589 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1590 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1595 static void nfs4_open_release(void *calldata)
1597 struct nfs4_opendata *data = calldata;
1598 struct nfs4_state *state = NULL;
1600 /* If this request hasn't been cancelled, do nothing */
1601 if (data->cancelled == 0)
1603 /* In case of error, no cleanup! */
1604 if (data->rpc_status != 0 || !data->rpc_done)
1606 /* In case we need an open_confirm, no cleanup! */
1607 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1609 state = nfs4_opendata_to_nfs4_state(data);
1611 nfs4_close_state(state, data->o_arg.fmode);
1613 nfs4_opendata_put(data);
1616 static const struct rpc_call_ops nfs4_open_ops = {
1617 .rpc_call_prepare = nfs4_open_prepare,
1618 .rpc_call_done = nfs4_open_done,
1619 .rpc_release = nfs4_open_release,
1622 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1624 struct inode *dir = data->dir->d_inode;
1625 struct nfs_server *server = NFS_SERVER(dir);
1626 struct nfs_openargs *o_arg = &data->o_arg;
1627 struct nfs_openres *o_res = &data->o_res;
1628 struct rpc_task *task;
1629 struct rpc_message msg = {
1630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1633 .rpc_cred = data->owner->so_cred,
1635 struct rpc_task_setup task_setup_data = {
1636 .rpc_client = server->client,
1637 .rpc_message = &msg,
1638 .callback_ops = &nfs4_open_ops,
1639 .callback_data = data,
1640 .workqueue = nfsiod_workqueue,
1641 .flags = RPC_TASK_ASYNC,
1645 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1646 kref_get(&data->kref);
1648 data->rpc_status = 0;
1649 data->cancelled = 0;
1651 nfs4_set_sequence_privileged(&o_arg->seq_args);
1652 task = rpc_run_task(&task_setup_data);
1654 return PTR_ERR(task);
1655 status = nfs4_wait_for_completion_rpc_task(task);
1657 data->cancelled = 1;
1660 status = data->rpc_status;
1666 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1668 struct inode *dir = data->dir->d_inode;
1669 struct nfs_openres *o_res = &data->o_res;
1672 status = nfs4_run_open_task(data, 1);
1673 if (status != 0 || !data->rpc_done)
1676 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1678 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1679 status = _nfs4_proc_open_confirm(data);
1687 static int nfs4_opendata_access(struct rpc_cred *cred,
1688 struct nfs4_opendata *opendata,
1689 struct nfs4_state *state, fmode_t fmode,
1692 struct nfs_access_entry cache;
1695 /* access call failed or for some reason the server doesn't
1696 * support any access modes -- defer access call until later */
1697 if (opendata->o_res.access_supported == 0)
1701 /* don't check MAY_WRITE - a newly created file may not have
1702 * write mode bits, but POSIX allows the creating process to write.
1703 * use openflags to check for exec, because fmode won't
1704 * always have FMODE_EXEC set when file open for exec. */
1705 if (openflags & __FMODE_EXEC) {
1706 /* ONLY check for exec rights */
1708 } else if (fmode & FMODE_READ)
1712 cache.jiffies = jiffies;
1713 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1714 nfs_access_add_cache(state->inode, &cache);
1716 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1719 /* even though OPEN succeeded, access is denied. Close the file */
1720 nfs4_close_state(state, fmode);
1725 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1727 static int _nfs4_proc_open(struct nfs4_opendata *data)
1729 struct inode *dir = data->dir->d_inode;
1730 struct nfs_server *server = NFS_SERVER(dir);
1731 struct nfs_openargs *o_arg = &data->o_arg;
1732 struct nfs_openres *o_res = &data->o_res;
1735 status = nfs4_run_open_task(data, 0);
1736 if (!data->rpc_done)
1739 if (status == -NFS4ERR_BADNAME &&
1740 !(o_arg->open_flags & O_CREAT))
1745 nfs_fattr_map_and_free_names(server, &data->f_attr);
1747 if (o_arg->open_flags & O_CREAT)
1748 update_changeattr(dir, &o_res->cinfo);
1749 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1750 server->caps &= ~NFS_CAP_POSIX_LOCK;
1751 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1752 status = _nfs4_proc_open_confirm(data);
1756 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1757 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1761 static int nfs4_recover_expired_lease(struct nfs_server *server)
1763 return nfs4_client_recover_expired_lease(server->nfs_client);
1768 * reclaim state on the server after a network partition.
1769 * Assumes caller holds the appropriate lock
1771 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1773 struct nfs4_opendata *opendata;
1776 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1777 NFS4_OPEN_CLAIM_FH);
1778 if (IS_ERR(opendata))
1779 return PTR_ERR(opendata);
1780 ret = nfs4_open_recover(opendata, state);
1782 d_drop(ctx->dentry);
1783 nfs4_opendata_put(opendata);
1787 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1789 struct nfs_server *server = NFS_SERVER(state->inode);
1790 struct nfs4_exception exception = { };
1794 err = _nfs4_open_expired(ctx, state);
1795 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1800 case -NFS4ERR_GRACE:
1801 case -NFS4ERR_DELAY:
1802 nfs4_handle_exception(server, err, &exception);
1805 } while (exception.retry);
1810 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1812 struct nfs_open_context *ctx;
1815 ctx = nfs4_state_find_open_context(state);
1818 ret = nfs4_do_open_expired(ctx, state);
1819 put_nfs_open_context(ctx);
1823 #if defined(CONFIG_NFS_V4_1)
1824 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1826 struct nfs_server *server = NFS_SERVER(state->inode);
1827 nfs4_stateid *stateid = &state->stateid;
1830 /* If a state reset has been done, test_stateid is unneeded */
1831 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1834 status = nfs41_test_stateid(server, stateid);
1835 if (status != NFS_OK) {
1836 /* Free the stateid unless the server explicitly
1837 * informs us the stateid is unrecognized. */
1838 if (status != -NFS4ERR_BAD_STATEID)
1839 nfs41_free_stateid(server, stateid);
1840 nfs_remove_bad_delegation(state->inode);
1842 write_seqlock(&state->seqlock);
1843 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1844 write_sequnlock(&state->seqlock);
1845 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1850 * nfs41_check_open_stateid - possibly free an open stateid
1852 * @state: NFSv4 state for an inode
1854 * Returns NFS_OK if recovery for this stateid is now finished.
1855 * Otherwise a negative NFS4ERR value is returned.
1857 static int nfs41_check_open_stateid(struct nfs4_state *state)
1859 struct nfs_server *server = NFS_SERVER(state->inode);
1860 nfs4_stateid *stateid = &state->open_stateid;
1863 /* If a state reset has been done, test_stateid is unneeded */
1864 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1865 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1866 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1867 return -NFS4ERR_BAD_STATEID;
1869 status = nfs41_test_stateid(server, stateid);
1870 if (status != NFS_OK) {
1871 /* Free the stateid unless the server explicitly
1872 * informs us the stateid is unrecognized. */
1873 if (status != -NFS4ERR_BAD_STATEID)
1874 nfs41_free_stateid(server, stateid);
1876 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1877 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1878 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1883 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1887 nfs41_clear_delegation_stateid(state);
1888 status = nfs41_check_open_stateid(state);
1889 if (status != NFS_OK)
1890 status = nfs4_open_expired(sp, state);
1896 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1897 * fields corresponding to attributes that were used to store the verifier.
1898 * Make sure we clobber those fields in the later setattr call
1900 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1902 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1903 !(sattr->ia_valid & ATTR_ATIME_SET))
1904 sattr->ia_valid |= ATTR_ATIME;
1906 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1907 !(sattr->ia_valid & ATTR_MTIME_SET))
1908 sattr->ia_valid |= ATTR_MTIME;
1911 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1914 struct nfs4_state **res)
1916 struct nfs4_state_owner *sp = opendata->owner;
1917 struct nfs_server *server = sp->so_server;
1918 struct nfs4_state *state;
1922 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1924 ret = _nfs4_proc_open(opendata);
1928 state = nfs4_opendata_to_nfs4_state(opendata);
1929 ret = PTR_ERR(state);
1932 if (server->caps & NFS_CAP_POSIX_LOCK)
1933 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1935 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1939 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
1940 nfs4_schedule_stateid_recovery(server, state);
1947 * Returns a referenced nfs4_state
1949 static int _nfs4_do_open(struct inode *dir,
1950 struct dentry *dentry,
1953 struct iattr *sattr,
1954 struct rpc_cred *cred,
1955 struct nfs4_state **res,
1956 struct nfs4_threshold **ctx_th)
1958 struct nfs4_state_owner *sp;
1959 struct nfs4_state *state = NULL;
1960 struct nfs_server *server = NFS_SERVER(dir);
1961 struct nfs4_opendata *opendata;
1962 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
1965 /* Protect against reboot recovery conflicts */
1967 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1969 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1972 status = nfs4_recover_expired_lease(server);
1974 goto err_put_state_owner;
1975 if (dentry->d_inode != NULL)
1976 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1978 if (dentry->d_inode)
1979 claim = NFS4_OPEN_CLAIM_FH;
1980 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
1982 if (opendata == NULL)
1983 goto err_put_state_owner;
1985 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1986 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1987 if (!opendata->f_attr.mdsthreshold)
1988 goto err_opendata_put;
1989 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1991 if (dentry->d_inode != NULL)
1992 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1994 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
1996 goto err_opendata_put;
1998 if (opendata->o_arg.open_flags & O_EXCL) {
1999 nfs4_exclusive_attrset(opendata, sattr);
2001 nfs_fattr_init(opendata->o_res.f_attr);
2002 status = nfs4_do_setattr(state->inode, cred,
2003 opendata->o_res.f_attr, sattr,
2006 nfs_setattr_update_inode(state->inode, sattr);
2007 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2010 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2011 *ctx_th = opendata->f_attr.mdsthreshold;
2013 kfree(opendata->f_attr.mdsthreshold);
2014 opendata->f_attr.mdsthreshold = NULL;
2016 nfs4_opendata_put(opendata);
2017 nfs4_put_state_owner(sp);
2021 kfree(opendata->f_attr.mdsthreshold);
2022 nfs4_opendata_put(opendata);
2023 err_put_state_owner:
2024 nfs4_put_state_owner(sp);
2031 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2032 struct dentry *dentry,
2035 struct iattr *sattr,
2036 struct rpc_cred *cred,
2037 struct nfs4_threshold **ctx_th)
2039 struct nfs_server *server = NFS_SERVER(dir);
2040 struct nfs4_exception exception = { };
2041 struct nfs4_state *res;
2044 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2046 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2050 /* NOTE: BAD_SEQID means the server and client disagree about the
2051 * book-keeping w.r.t. state-changing operations
2052 * (OPEN/CLOSE/LOCK/LOCKU...)
2053 * It is actually a sign of a bug on the client or on the server.
2055 * If we receive a BAD_SEQID error in the particular case of
2056 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2057 * have unhashed the old state_owner for us, and that we can
2058 * therefore safely retry using a new one. We should still warn
2059 * the user though...
2061 if (status == -NFS4ERR_BAD_SEQID) {
2062 pr_warn_ratelimited("NFS: v4 server %s "
2063 " returned a bad sequence-id error!\n",
2064 NFS_SERVER(dir)->nfs_client->cl_hostname);
2065 exception.retry = 1;
2069 * BAD_STATEID on OPEN means that the server cancelled our
2070 * state before it received the OPEN_CONFIRM.
2071 * Recover by retrying the request as per the discussion
2072 * on Page 181 of RFC3530.
2074 if (status == -NFS4ERR_BAD_STATEID) {
2075 exception.retry = 1;
2078 if (status == -EAGAIN) {
2079 /* We must have found a delegation */
2080 exception.retry = 1;
2083 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2085 res = ERR_PTR(nfs4_handle_exception(server,
2086 status, &exception));
2087 } while (exception.retry);
2091 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2092 struct nfs_fattr *fattr, struct iattr *sattr,
2093 struct nfs4_state *state)
2095 struct nfs_server *server = NFS_SERVER(inode);
2096 struct nfs_setattrargs arg = {
2097 .fh = NFS_FH(inode),
2100 .bitmask = server->attr_bitmask,
2102 struct nfs_setattrres res = {
2106 struct rpc_message msg = {
2107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2112 unsigned long timestamp = jiffies;
2115 nfs_fattr_init(fattr);
2117 if (state != NULL && nfs4_valid_open_stateid(state)) {
2118 struct nfs_lockowner lockowner = {
2119 .l_owner = current->files,
2120 .l_pid = current->tgid,
2122 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2124 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2126 /* Use that stateid */
2128 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2130 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2131 if (status == 0 && state != NULL)
2132 renew_lease(server, timestamp);
2136 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2137 struct nfs_fattr *fattr, struct iattr *sattr,
2138 struct nfs4_state *state)
2140 struct nfs_server *server = NFS_SERVER(inode);
2141 struct nfs4_exception exception = {
2147 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2149 case -NFS4ERR_OPENMODE:
2150 if (state && !(state->state & FMODE_WRITE)) {
2152 if (sattr->ia_valid & ATTR_OPEN)
2157 err = nfs4_handle_exception(server, err, &exception);
2158 } while (exception.retry);
2163 struct nfs4_closedata {
2164 struct inode *inode;
2165 struct nfs4_state *state;
2166 struct nfs_closeargs arg;
2167 struct nfs_closeres res;
2168 struct nfs_fattr fattr;
2169 unsigned long timestamp;
2174 static void nfs4_free_closedata(void *data)
2176 struct nfs4_closedata *calldata = data;
2177 struct nfs4_state_owner *sp = calldata->state->owner;
2178 struct super_block *sb = calldata->state->inode->i_sb;
2181 pnfs_roc_release(calldata->state->inode);
2182 nfs4_put_open_state(calldata->state);
2183 nfs_free_seqid(calldata->arg.seqid);
2184 nfs4_put_state_owner(sp);
2185 nfs_sb_deactive(sb);
2189 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2192 spin_lock(&state->owner->so_lock);
2193 if (!(fmode & FMODE_READ))
2194 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2195 if (!(fmode & FMODE_WRITE))
2196 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2197 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2198 spin_unlock(&state->owner->so_lock);
2201 static void nfs4_close_done(struct rpc_task *task, void *data)
2203 struct nfs4_closedata *calldata = data;
2204 struct nfs4_state *state = calldata->state;
2205 struct nfs_server *server = NFS_SERVER(calldata->inode);
2207 dprintk("%s: begin!\n", __func__);
2208 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2210 /* hmm. we are done with the inode, and in the process of freeing
2211 * the state_owner. we keep this around to process errors
2213 switch (task->tk_status) {
2216 pnfs_roc_set_barrier(state->inode,
2217 calldata->roc_barrier);
2218 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2219 renew_lease(server, calldata->timestamp);
2220 nfs4_close_clear_stateid_flags(state,
2221 calldata->arg.fmode);
2223 case -NFS4ERR_STALE_STATEID:
2224 case -NFS4ERR_OLD_STATEID:
2225 case -NFS4ERR_BAD_STATEID:
2226 case -NFS4ERR_EXPIRED:
2227 if (calldata->arg.fmode == 0)
2230 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2231 rpc_restart_call_prepare(task);
2233 nfs_release_seqid(calldata->arg.seqid);
2234 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2235 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2238 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2240 struct nfs4_closedata *calldata = data;
2241 struct nfs4_state *state = calldata->state;
2242 struct inode *inode = calldata->inode;
2245 dprintk("%s: begin!\n", __func__);
2246 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2249 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2250 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2251 spin_lock(&state->owner->so_lock);
2252 /* Calculate the change in open mode */
2253 if (state->n_rdwr == 0) {
2254 if (state->n_rdonly == 0) {
2255 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2256 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2257 calldata->arg.fmode &= ~FMODE_READ;
2259 if (state->n_wronly == 0) {
2260 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2261 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2262 calldata->arg.fmode &= ~FMODE_WRITE;
2265 if (!nfs4_valid_open_stateid(state))
2267 spin_unlock(&state->owner->so_lock);
2270 /* Note: exit _without_ calling nfs4_close_done */
2274 if (calldata->arg.fmode == 0) {
2275 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2276 if (calldata->roc &&
2277 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2281 nfs_fattr_init(calldata->res.fattr);
2282 calldata->timestamp = jiffies;
2283 if (nfs4_setup_sequence(NFS_SERVER(inode),
2284 &calldata->arg.seq_args,
2285 &calldata->res.seq_res,
2287 nfs_release_seqid(calldata->arg.seqid);
2288 dprintk("%s: done!\n", __func__);
2291 task->tk_action = NULL;
2293 nfs4_sequence_done(task, &calldata->res.seq_res);
2296 static const struct rpc_call_ops nfs4_close_ops = {
2297 .rpc_call_prepare = nfs4_close_prepare,
2298 .rpc_call_done = nfs4_close_done,
2299 .rpc_release = nfs4_free_closedata,
2303 * It is possible for data to be read/written from a mem-mapped file
2304 * after the sys_close call (which hits the vfs layer as a flush).
2305 * This means that we can't safely call nfsv4 close on a file until
2306 * the inode is cleared. This in turn means that we are not good
2307 * NFSv4 citizens - we do not indicate to the server to update the file's
2308 * share state even when we are done with one of the three share
2309 * stateid's in the inode.
2311 * NOTE: Caller must be holding the sp->so_owner semaphore!
2313 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2315 struct nfs_server *server = NFS_SERVER(state->inode);
2316 struct nfs4_closedata *calldata;
2317 struct nfs4_state_owner *sp = state->owner;
2318 struct rpc_task *task;
2319 struct rpc_message msg = {
2320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2321 .rpc_cred = state->owner->so_cred,
2323 struct rpc_task_setup task_setup_data = {
2324 .rpc_client = server->client,
2325 .rpc_message = &msg,
2326 .callback_ops = &nfs4_close_ops,
2327 .workqueue = nfsiod_workqueue,
2328 .flags = RPC_TASK_ASYNC,
2330 int status = -ENOMEM;
2332 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2333 if (calldata == NULL)
2335 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2336 calldata->inode = state->inode;
2337 calldata->state = state;
2338 calldata->arg.fh = NFS_FH(state->inode);
2339 calldata->arg.stateid = &state->open_stateid;
2340 /* Serialization for the sequence id */
2341 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2342 if (calldata->arg.seqid == NULL)
2343 goto out_free_calldata;
2344 calldata->arg.fmode = 0;
2345 calldata->arg.bitmask = server->cache_consistency_bitmask;
2346 calldata->res.fattr = &calldata->fattr;
2347 calldata->res.seqid = calldata->arg.seqid;
2348 calldata->res.server = server;
2349 calldata->roc = pnfs_roc(state->inode);
2350 nfs_sb_active(calldata->inode->i_sb);
2352 msg.rpc_argp = &calldata->arg;
2353 msg.rpc_resp = &calldata->res;
2354 task_setup_data.callback_data = calldata;
2355 task = rpc_run_task(&task_setup_data);
2357 return PTR_ERR(task);
2360 status = rpc_wait_for_completion_task(task);
2366 nfs4_put_open_state(state);
2367 nfs4_put_state_owner(sp);
2371 static struct inode *
2372 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2374 struct nfs4_state *state;
2376 /* Protect against concurrent sillydeletes */
2377 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2378 ctx->cred, &ctx->mdsthreshold);
2380 return ERR_CAST(state);
2382 return igrab(state->inode);
2385 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2387 if (ctx->state == NULL)
2390 nfs4_close_sync(ctx->state, ctx->mode);
2392 nfs4_close_state(ctx->state, ctx->mode);
2395 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2397 struct nfs4_server_caps_arg args = {
2400 struct nfs4_server_caps_res res = {};
2401 struct rpc_message msg = {
2402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2408 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2410 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2411 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2412 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2413 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2414 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2415 NFS_CAP_CTIME|NFS_CAP_MTIME);
2416 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2417 server->caps |= NFS_CAP_ACLS;
2418 if (res.has_links != 0)
2419 server->caps |= NFS_CAP_HARDLINKS;
2420 if (res.has_symlinks != 0)
2421 server->caps |= NFS_CAP_SYMLINKS;
2422 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2423 server->caps |= NFS_CAP_FILEID;
2424 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2425 server->caps |= NFS_CAP_MODE;
2426 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2427 server->caps |= NFS_CAP_NLINK;
2428 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2429 server->caps |= NFS_CAP_OWNER;
2430 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2431 server->caps |= NFS_CAP_OWNER_GROUP;
2432 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2433 server->caps |= NFS_CAP_ATIME;
2434 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2435 server->caps |= NFS_CAP_CTIME;
2436 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2437 server->caps |= NFS_CAP_MTIME;
2439 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2440 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2441 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2442 server->acl_bitmask = res.acl_bitmask;
2443 server->fh_expire_type = res.fh_expire_type;
2449 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2451 struct nfs4_exception exception = { };
2454 err = nfs4_handle_exception(server,
2455 _nfs4_server_capabilities(server, fhandle),
2457 } while (exception.retry);
2461 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2462 struct nfs_fsinfo *info)
2464 struct nfs4_lookup_root_arg args = {
2465 .bitmask = nfs4_fattr_bitmap,
2467 struct nfs4_lookup_res res = {
2469 .fattr = info->fattr,
2472 struct rpc_message msg = {
2473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2478 nfs_fattr_init(info->fattr);
2479 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2482 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2483 struct nfs_fsinfo *info)
2485 struct nfs4_exception exception = { };
2488 err = _nfs4_lookup_root(server, fhandle, info);
2491 case -NFS4ERR_WRONGSEC:
2494 err = nfs4_handle_exception(server, err, &exception);
2496 } while (exception.retry);
2501 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2502 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2504 struct rpc_auth *auth;
2507 auth = rpcauth_create(flavor, server->client);
2512 ret = nfs4_lookup_root(server, fhandle, info);
2517 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2518 struct nfs_fsinfo *info)
2520 int i, len, status = 0;
2521 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2523 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2527 for (i = 0; i < len; i++) {
2528 /* AUTH_UNIX is the default flavor if none was specified,
2529 * thus has already been tried. */
2530 if (flav_array[i] == RPC_AUTH_UNIX)
2533 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2534 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2539 * -EACCESS could mean that the user doesn't have correct permissions
2540 * to access the mount. It could also mean that we tried to mount
2541 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2542 * existing mount programs don't handle -EACCES very well so it should
2543 * be mapped to -EPERM instead.
2545 if (status == -EACCES)
2551 * get the file handle for the "/" directory on the server
2553 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2554 struct nfs_fsinfo *info)
2556 int minor_version = server->nfs_client->cl_minorversion;
2557 int status = nfs4_lookup_root(server, fhandle, info);
2558 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2560 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2561 * by nfs4_map_errors() as this function exits.
2563 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2565 status = nfs4_server_capabilities(server, fhandle);
2567 status = nfs4_do_fsinfo(server, fhandle, info);
2568 return nfs4_map_errors(status);
2571 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2572 struct nfs_fsinfo *info)
2575 struct nfs_fattr *fattr = info->fattr;
2577 error = nfs4_server_capabilities(server, mntfh);
2579 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2583 error = nfs4_proc_getattr(server, mntfh, fattr);
2585 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2589 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2590 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2591 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2597 * Get locations and (maybe) other attributes of a referral.
2598 * Note that we'll actually follow the referral later when
2599 * we detect fsid mismatch in inode revalidation
2601 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2602 const struct qstr *name, struct nfs_fattr *fattr,
2603 struct nfs_fh *fhandle)
2605 int status = -ENOMEM;
2606 struct page *page = NULL;
2607 struct nfs4_fs_locations *locations = NULL;
2609 page = alloc_page(GFP_KERNEL);
2612 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2613 if (locations == NULL)
2616 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2619 /* Make sure server returned a different fsid for the referral */
2620 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2621 dprintk("%s: server did not return a different fsid for"
2622 " a referral at %s\n", __func__, name->name);
2626 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2627 nfs_fixup_referral_attributes(&locations->fattr);
2629 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2630 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2631 memset(fhandle, 0, sizeof(struct nfs_fh));
2639 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2641 struct nfs4_getattr_arg args = {
2643 .bitmask = server->attr_bitmask,
2645 struct nfs4_getattr_res res = {
2649 struct rpc_message msg = {
2650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2655 nfs_fattr_init(fattr);
2656 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2659 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2661 struct nfs4_exception exception = { };
2664 err = nfs4_handle_exception(server,
2665 _nfs4_proc_getattr(server, fhandle, fattr),
2667 } while (exception.retry);
2672 * The file is not closed if it is opened due to the a request to change
2673 * the size of the file. The open call will not be needed once the
2674 * VFS layer lookup-intents are implemented.
2676 * Close is called when the inode is destroyed.
2677 * If we haven't opened the file for O_WRONLY, we
2678 * need to in the size_change case to obtain a stateid.
2681 * Because OPEN is always done by name in nfsv4, it is
2682 * possible that we opened a different file by the same
2683 * name. We can recognize this race condition, but we
2684 * can't do anything about it besides returning an error.
2686 * This will be fixed with VFS changes (lookup-intent).
2689 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2690 struct iattr *sattr)
2692 struct inode *inode = dentry->d_inode;
2693 struct rpc_cred *cred = NULL;
2694 struct nfs4_state *state = NULL;
2697 if (pnfs_ld_layoutret_on_setattr(inode))
2698 pnfs_commit_and_return_layout(inode);
2700 nfs_fattr_init(fattr);
2702 /* Deal with open(O_TRUNC) */
2703 if (sattr->ia_valid & ATTR_OPEN)
2704 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2706 /* Optimization: if the end result is no change, don't RPC */
2707 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2710 /* Search for an existing open(O_WRITE) file */
2711 if (sattr->ia_valid & ATTR_FILE) {
2712 struct nfs_open_context *ctx;
2714 ctx = nfs_file_open_context(sattr->ia_file);
2721 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2723 nfs_setattr_update_inode(inode, sattr);
2727 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2728 const struct qstr *name, struct nfs_fh *fhandle,
2729 struct nfs_fattr *fattr)
2731 struct nfs_server *server = NFS_SERVER(dir);
2733 struct nfs4_lookup_arg args = {
2734 .bitmask = server->attr_bitmask,
2735 .dir_fh = NFS_FH(dir),
2738 struct nfs4_lookup_res res = {
2743 struct rpc_message msg = {
2744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2749 nfs_fattr_init(fattr);
2751 dprintk("NFS call lookup %s\n", name->name);
2752 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2753 dprintk("NFS reply lookup: %d\n", status);
2757 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2759 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2760 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2761 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2765 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2766 struct qstr *name, struct nfs_fh *fhandle,
2767 struct nfs_fattr *fattr)
2769 struct nfs4_exception exception = { };
2770 struct rpc_clnt *client = *clnt;
2773 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2775 case -NFS4ERR_BADNAME:
2778 case -NFS4ERR_MOVED:
2779 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2781 case -NFS4ERR_WRONGSEC:
2783 if (client != *clnt)
2786 client = nfs4_create_sec_client(client, dir, name);
2788 return PTR_ERR(client);
2790 exception.retry = 1;
2793 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2795 } while (exception.retry);
2800 else if (client != *clnt)
2801 rpc_shutdown_client(client);
2806 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2807 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2810 struct rpc_clnt *client = NFS_CLIENT(dir);
2812 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2813 if (client != NFS_CLIENT(dir)) {
2814 rpc_shutdown_client(client);
2815 nfs_fixup_secinfo_attributes(fattr);
2821 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2822 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2825 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2827 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2829 rpc_shutdown_client(client);
2830 return ERR_PTR(status);
2835 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2837 struct nfs_server *server = NFS_SERVER(inode);
2838 struct nfs4_accessargs args = {
2839 .fh = NFS_FH(inode),
2840 .bitmask = server->cache_consistency_bitmask,
2842 struct nfs4_accessres res = {
2845 struct rpc_message msg = {
2846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2849 .rpc_cred = entry->cred,
2851 int mode = entry->mask;
2855 * Determine which access bits we want to ask for...
2857 if (mode & MAY_READ)
2858 args.access |= NFS4_ACCESS_READ;
2859 if (S_ISDIR(inode->i_mode)) {
2860 if (mode & MAY_WRITE)
2861 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2862 if (mode & MAY_EXEC)
2863 args.access |= NFS4_ACCESS_LOOKUP;
2865 if (mode & MAY_WRITE)
2866 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2867 if (mode & MAY_EXEC)
2868 args.access |= NFS4_ACCESS_EXECUTE;
2871 res.fattr = nfs_alloc_fattr();
2872 if (res.fattr == NULL)
2875 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2877 nfs_access_set_mask(entry, res.access);
2878 nfs_refresh_inode(inode, res.fattr);
2880 nfs_free_fattr(res.fattr);
2884 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2886 struct nfs4_exception exception = { };
2889 err = nfs4_handle_exception(NFS_SERVER(inode),
2890 _nfs4_proc_access(inode, entry),
2892 } while (exception.retry);
2897 * TODO: For the time being, we don't try to get any attributes
2898 * along with any of the zero-copy operations READ, READDIR,
2901 * In the case of the first three, we want to put the GETATTR
2902 * after the read-type operation -- this is because it is hard
2903 * to predict the length of a GETATTR response in v4, and thus
2904 * align the READ data correctly. This means that the GETATTR
2905 * may end up partially falling into the page cache, and we should
2906 * shift it into the 'tail' of the xdr_buf before processing.
2907 * To do this efficiently, we need to know the total length
2908 * of data received, which doesn't seem to be available outside
2911 * In the case of WRITE, we also want to put the GETATTR after
2912 * the operation -- in this case because we want to make sure
2913 * we get the post-operation mtime and size.
2915 * Both of these changes to the XDR layer would in fact be quite
2916 * minor, but I decided to leave them for a subsequent patch.
2918 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2919 unsigned int pgbase, unsigned int pglen)
2921 struct nfs4_readlink args = {
2922 .fh = NFS_FH(inode),
2927 struct nfs4_readlink_res res;
2928 struct rpc_message msg = {
2929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2934 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2937 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2938 unsigned int pgbase, unsigned int pglen)
2940 struct nfs4_exception exception = { };
2943 err = nfs4_handle_exception(NFS_SERVER(inode),
2944 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2946 } while (exception.retry);
2951 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2954 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2957 struct nfs_open_context *ctx;
2958 struct nfs4_state *state;
2961 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2963 return PTR_ERR(ctx);
2965 sattr->ia_mode &= ~current_umask();
2966 state = nfs4_do_open(dir, dentry, ctx->mode,
2967 flags, sattr, ctx->cred,
2968 &ctx->mdsthreshold);
2970 if (IS_ERR(state)) {
2971 status = PTR_ERR(state);
2974 d_add(dentry, igrab(state->inode));
2975 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2978 put_nfs_open_context(ctx);
2982 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2984 struct nfs_server *server = NFS_SERVER(dir);
2985 struct nfs_removeargs args = {
2989 struct nfs_removeres res = {
2992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2999 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3001 update_changeattr(dir, &res.cinfo);
3005 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3007 struct nfs4_exception exception = { };
3010 err = nfs4_handle_exception(NFS_SERVER(dir),
3011 _nfs4_proc_remove(dir, name),
3013 } while (exception.retry);
3017 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3019 struct nfs_server *server = NFS_SERVER(dir);
3020 struct nfs_removeargs *args = msg->rpc_argp;
3021 struct nfs_removeres *res = msg->rpc_resp;
3023 res->server = server;
3024 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3025 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3028 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3030 nfs4_setup_sequence(NFS_SERVER(data->dir),
3031 &data->args.seq_args,
3036 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3038 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3040 if (!nfs4_sequence_done(task, &res->seq_res))
3042 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3044 update_changeattr(dir, &res->cinfo);
3048 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3050 struct nfs_server *server = NFS_SERVER(dir);
3051 struct nfs_renameargs *arg = msg->rpc_argp;
3052 struct nfs_renameres *res = msg->rpc_resp;
3054 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3055 res->server = server;
3056 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3059 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3061 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3062 &data->args.seq_args,
3067 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3068 struct inode *new_dir)
3070 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3072 if (!nfs4_sequence_done(task, &res->seq_res))
3074 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3077 update_changeattr(old_dir, &res->old_cinfo);
3078 update_changeattr(new_dir, &res->new_cinfo);
3082 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3083 struct inode *new_dir, struct qstr *new_name)
3085 struct nfs_server *server = NFS_SERVER(old_dir);
3086 struct nfs_renameargs arg = {
3087 .old_dir = NFS_FH(old_dir),
3088 .new_dir = NFS_FH(new_dir),
3089 .old_name = old_name,
3090 .new_name = new_name,
3092 struct nfs_renameres res = {
3095 struct rpc_message msg = {
3096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3100 int status = -ENOMEM;
3102 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3104 update_changeattr(old_dir, &res.old_cinfo);
3105 update_changeattr(new_dir, &res.new_cinfo);
3110 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3111 struct inode *new_dir, struct qstr *new_name)
3113 struct nfs4_exception exception = { };
3116 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3117 _nfs4_proc_rename(old_dir, old_name,
3120 } while (exception.retry);
3124 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3126 struct nfs_server *server = NFS_SERVER(inode);
3127 struct nfs4_link_arg arg = {
3128 .fh = NFS_FH(inode),
3129 .dir_fh = NFS_FH(dir),
3131 .bitmask = server->attr_bitmask,
3133 struct nfs4_link_res res = {
3136 struct rpc_message msg = {
3137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3141 int status = -ENOMEM;
3143 res.fattr = nfs_alloc_fattr();
3144 if (res.fattr == NULL)
3147 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3149 update_changeattr(dir, &res.cinfo);
3150 nfs_post_op_update_inode(inode, res.fattr);
3153 nfs_free_fattr(res.fattr);
3157 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3159 struct nfs4_exception exception = { };
3162 err = nfs4_handle_exception(NFS_SERVER(inode),
3163 _nfs4_proc_link(inode, dir, name),
3165 } while (exception.retry);
3169 struct nfs4_createdata {
3170 struct rpc_message msg;
3171 struct nfs4_create_arg arg;
3172 struct nfs4_create_res res;
3174 struct nfs_fattr fattr;
3177 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3178 struct qstr *name, struct iattr *sattr, u32 ftype)
3180 struct nfs4_createdata *data;
3182 data = kzalloc(sizeof(*data), GFP_KERNEL);
3184 struct nfs_server *server = NFS_SERVER(dir);
3186 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3187 data->msg.rpc_argp = &data->arg;
3188 data->msg.rpc_resp = &data->res;
3189 data->arg.dir_fh = NFS_FH(dir);
3190 data->arg.server = server;
3191 data->arg.name = name;
3192 data->arg.attrs = sattr;
3193 data->arg.ftype = ftype;
3194 data->arg.bitmask = server->attr_bitmask;
3195 data->res.server = server;
3196 data->res.fh = &data->fh;
3197 data->res.fattr = &data->fattr;
3198 nfs_fattr_init(data->res.fattr);
3203 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3205 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3206 &data->arg.seq_args, &data->res.seq_res, 1);
3208 update_changeattr(dir, &data->res.dir_cinfo);
3209 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3214 static void nfs4_free_createdata(struct nfs4_createdata *data)
3219 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3220 struct page *page, unsigned int len, struct iattr *sattr)
3222 struct nfs4_createdata *data;
3223 int status = -ENAMETOOLONG;
3225 if (len > NFS4_MAXPATHLEN)
3229 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3233 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3234 data->arg.u.symlink.pages = &page;
3235 data->arg.u.symlink.len = len;
3237 status = nfs4_do_create(dir, dentry, data);
3239 nfs4_free_createdata(data);
3244 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3245 struct page *page, unsigned int len, struct iattr *sattr)
3247 struct nfs4_exception exception = { };
3250 err = nfs4_handle_exception(NFS_SERVER(dir),
3251 _nfs4_proc_symlink(dir, dentry, page,
3254 } while (exception.retry);
3258 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3259 struct iattr *sattr)
3261 struct nfs4_createdata *data;
3262 int status = -ENOMEM;
3264 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3268 status = nfs4_do_create(dir, dentry, data);
3270 nfs4_free_createdata(data);
3275 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3276 struct iattr *sattr)
3278 struct nfs4_exception exception = { };
3281 sattr->ia_mode &= ~current_umask();
3283 err = nfs4_handle_exception(NFS_SERVER(dir),
3284 _nfs4_proc_mkdir(dir, dentry, sattr),
3286 } while (exception.retry);
3290 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3291 u64 cookie, struct page **pages, unsigned int count, int plus)
3293 struct inode *dir = dentry->d_inode;
3294 struct nfs4_readdir_arg args = {
3299 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3302 struct nfs4_readdir_res res;
3303 struct rpc_message msg = {
3304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3311 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3312 dentry->d_parent->d_name.name,
3313 dentry->d_name.name,
3314 (unsigned long long)cookie);
3315 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3316 res.pgbase = args.pgbase;
3317 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3319 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3320 status += args.pgbase;
3323 nfs_invalidate_atime(dir);
3325 dprintk("%s: returns %d\n", __func__, status);
3329 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3330 u64 cookie, struct page **pages, unsigned int count, int plus)
3332 struct nfs4_exception exception = { };
3335 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3336 _nfs4_proc_readdir(dentry, cred, cookie,
3337 pages, count, plus),
3339 } while (exception.retry);
3343 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3344 struct iattr *sattr, dev_t rdev)
3346 struct nfs4_createdata *data;
3347 int mode = sattr->ia_mode;
3348 int status = -ENOMEM;
3350 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3355 data->arg.ftype = NF4FIFO;
3356 else if (S_ISBLK(mode)) {
3357 data->arg.ftype = NF4BLK;
3358 data->arg.u.device.specdata1 = MAJOR(rdev);
3359 data->arg.u.device.specdata2 = MINOR(rdev);
3361 else if (S_ISCHR(mode)) {
3362 data->arg.ftype = NF4CHR;
3363 data->arg.u.device.specdata1 = MAJOR(rdev);
3364 data->arg.u.device.specdata2 = MINOR(rdev);
3365 } else if (!S_ISSOCK(mode)) {
3370 status = nfs4_do_create(dir, dentry, data);
3372 nfs4_free_createdata(data);
3377 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3378 struct iattr *sattr, dev_t rdev)
3380 struct nfs4_exception exception = { };
3383 sattr->ia_mode &= ~current_umask();
3385 err = nfs4_handle_exception(NFS_SERVER(dir),
3386 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3388 } while (exception.retry);
3392 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3393 struct nfs_fsstat *fsstat)
3395 struct nfs4_statfs_arg args = {
3397 .bitmask = server->attr_bitmask,
3399 struct nfs4_statfs_res res = {
3402 struct rpc_message msg = {
3403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3408 nfs_fattr_init(fsstat->fattr);
3409 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3412 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3414 struct nfs4_exception exception = { };
3417 err = nfs4_handle_exception(server,
3418 _nfs4_proc_statfs(server, fhandle, fsstat),
3420 } while (exception.retry);
3424 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3425 struct nfs_fsinfo *fsinfo)
3427 struct nfs4_fsinfo_arg args = {
3429 .bitmask = server->attr_bitmask,
3431 struct nfs4_fsinfo_res res = {
3434 struct rpc_message msg = {
3435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3440 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3443 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3445 struct nfs4_exception exception = { };
3449 err = nfs4_handle_exception(server,
3450 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3452 } while (exception.retry);
3456 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3460 nfs_fattr_init(fsinfo->fattr);
3461 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3463 /* block layout checks this! */
3464 server->pnfs_blksize = fsinfo->blksize;
3465 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3471 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3472 struct nfs_pathconf *pathconf)
3474 struct nfs4_pathconf_arg args = {
3476 .bitmask = server->attr_bitmask,
3478 struct nfs4_pathconf_res res = {
3479 .pathconf = pathconf,
3481 struct rpc_message msg = {
3482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3487 /* None of the pathconf attributes are mandatory to implement */
3488 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3489 memset(pathconf, 0, sizeof(*pathconf));
3493 nfs_fattr_init(pathconf->fattr);
3494 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3497 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3498 struct nfs_pathconf *pathconf)
3500 struct nfs4_exception exception = { };
3504 err = nfs4_handle_exception(server,
3505 _nfs4_proc_pathconf(server, fhandle, pathconf),
3507 } while (exception.retry);
3511 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
3512 const struct nfs_open_context *ctx,
3513 const struct nfs_lock_context *l_ctx,
3516 const struct nfs_lockowner *lockowner = NULL;
3519 lockowner = &l_ctx->lockowner;
3520 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
3522 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3524 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3525 const struct nfs_open_context *ctx,
3526 const struct nfs_lock_context *l_ctx,
3529 nfs4_stateid current_stateid;
3531 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode))
3533 return nfs4_stateid_match(stateid, ¤t_stateid);
3536 static bool nfs4_error_stateid_expired(int err)
3539 case -NFS4ERR_DELEG_REVOKED:
3540 case -NFS4ERR_ADMIN_REVOKED:
3541 case -NFS4ERR_BAD_STATEID:
3542 case -NFS4ERR_STALE_STATEID:
3543 case -NFS4ERR_OLD_STATEID:
3544 case -NFS4ERR_OPENMODE:
3545 case -NFS4ERR_EXPIRED:
3551 void __nfs4_read_done_cb(struct nfs_read_data *data)
3553 nfs_invalidate_atime(data->header->inode);
3556 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3558 struct nfs_server *server = NFS_SERVER(data->header->inode);
3560 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3561 rpc_restart_call_prepare(task);
3565 __nfs4_read_done_cb(data);
3566 if (task->tk_status > 0)
3567 renew_lease(server, data->timestamp);
3571 static bool nfs4_read_stateid_changed(struct rpc_task *task,
3572 struct nfs_readargs *args)
3575 if (!nfs4_error_stateid_expired(task->tk_status) ||
3576 nfs4_stateid_is_current(&args->stateid,
3581 rpc_restart_call_prepare(task);
3585 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3588 dprintk("--> %s\n", __func__);
3590 if (!nfs4_sequence_done(task, &data->res.seq_res))
3592 if (nfs4_read_stateid_changed(task, &data->args))
3594 return data->read_done_cb ? data->read_done_cb(task, data) :
3595 nfs4_read_done_cb(task, data);
3598 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3600 data->timestamp = jiffies;
3601 data->read_done_cb = nfs4_read_done_cb;
3602 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3603 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3606 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3608 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3609 &data->args.seq_args,
3613 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3614 data->args.lock_context, FMODE_READ);
3617 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3619 struct inode *inode = data->header->inode;
3621 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3622 rpc_restart_call_prepare(task);
3625 if (task->tk_status >= 0) {
3626 renew_lease(NFS_SERVER(inode), data->timestamp);
3627 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3632 static bool nfs4_write_stateid_changed(struct rpc_task *task,
3633 struct nfs_writeargs *args)
3636 if (!nfs4_error_stateid_expired(task->tk_status) ||
3637 nfs4_stateid_is_current(&args->stateid,
3642 rpc_restart_call_prepare(task);
3646 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3648 if (!nfs4_sequence_done(task, &data->res.seq_res))
3650 if (nfs4_write_stateid_changed(task, &data->args))
3652 return data->write_done_cb ? data->write_done_cb(task, data) :
3653 nfs4_write_done_cb(task, data);
3657 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3659 const struct nfs_pgio_header *hdr = data->header;
3661 /* Don't request attributes for pNFS or O_DIRECT writes */
3662 if (data->ds_clp != NULL || hdr->dreq != NULL)
3664 /* Otherwise, request attributes if and only if we don't hold
3667 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3670 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3672 struct nfs_server *server = NFS_SERVER(data->header->inode);
3674 if (!nfs4_write_need_cache_consistency_data(data)) {
3675 data->args.bitmask = NULL;
3676 data->res.fattr = NULL;
3678 data->args.bitmask = server->cache_consistency_bitmask;
3680 if (!data->write_done_cb)
3681 data->write_done_cb = nfs4_write_done_cb;
3682 data->res.server = server;
3683 data->timestamp = jiffies;
3685 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3686 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3689 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3691 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3692 &data->args.seq_args,
3696 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3697 data->args.lock_context, FMODE_WRITE);
3700 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3702 nfs4_setup_sequence(NFS_SERVER(data->inode),
3703 &data->args.seq_args,
3708 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3710 struct inode *inode = data->inode;
3712 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3713 rpc_restart_call_prepare(task);
3719 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3721 if (!nfs4_sequence_done(task, &data->res.seq_res))
3723 return data->commit_done_cb(task, data);
3726 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3728 struct nfs_server *server = NFS_SERVER(data->inode);
3730 if (data->commit_done_cb == NULL)
3731 data->commit_done_cb = nfs4_commit_done_cb;
3732 data->res.server = server;
3733 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3734 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3737 struct nfs4_renewdata {
3738 struct nfs_client *client;
3739 unsigned long timestamp;
3743 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3744 * standalone procedure for queueing an asynchronous RENEW.
3746 static void nfs4_renew_release(void *calldata)
3748 struct nfs4_renewdata *data = calldata;
3749 struct nfs_client *clp = data->client;
3751 if (atomic_read(&clp->cl_count) > 1)
3752 nfs4_schedule_state_renewal(clp);
3753 nfs_put_client(clp);
3757 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3759 struct nfs4_renewdata *data = calldata;
3760 struct nfs_client *clp = data->client;
3761 unsigned long timestamp = data->timestamp;
3763 if (task->tk_status < 0) {
3764 /* Unless we're shutting down, schedule state recovery! */
3765 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3767 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3768 nfs4_schedule_lease_recovery(clp);
3771 nfs4_schedule_path_down_recovery(clp);
3773 do_renew_lease(clp, timestamp);
3776 static const struct rpc_call_ops nfs4_renew_ops = {
3777 .rpc_call_done = nfs4_renew_done,
3778 .rpc_release = nfs4_renew_release,
3781 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3783 struct rpc_message msg = {
3784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3788 struct nfs4_renewdata *data;
3790 if (renew_flags == 0)
3792 if (!atomic_inc_not_zero(&clp->cl_count))
3794 data = kmalloc(sizeof(*data), GFP_NOFS);
3798 data->timestamp = jiffies;
3799 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3800 &nfs4_renew_ops, data);
3803 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3805 struct rpc_message msg = {
3806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3810 unsigned long now = jiffies;
3813 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3816 do_renew_lease(clp, now);
3820 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3822 return (server->caps & NFS_CAP_ACLS)
3823 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3824 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3827 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3828 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3831 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3833 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3834 struct page **pages, unsigned int *pgbase)
3836 struct page *newpage, **spages;
3842 len = min_t(size_t, PAGE_SIZE, buflen);
3843 newpage = alloc_page(GFP_KERNEL);
3845 if (newpage == NULL)
3847 memcpy(page_address(newpage), buf, len);
3852 } while (buflen != 0);
3858 __free_page(spages[rc-1]);
3862 struct nfs4_cached_acl {
3868 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3870 struct nfs_inode *nfsi = NFS_I(inode);
3872 spin_lock(&inode->i_lock);
3873 kfree(nfsi->nfs4_acl);
3874 nfsi->nfs4_acl = acl;
3875 spin_unlock(&inode->i_lock);
3878 static void nfs4_zap_acl_attr(struct inode *inode)
3880 nfs4_set_cached_acl(inode, NULL);
3883 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3885 struct nfs_inode *nfsi = NFS_I(inode);
3886 struct nfs4_cached_acl *acl;
3889 spin_lock(&inode->i_lock);
3890 acl = nfsi->nfs4_acl;
3893 if (buf == NULL) /* user is just asking for length */
3895 if (acl->cached == 0)
3897 ret = -ERANGE; /* see getxattr(2) man page */
3898 if (acl->len > buflen)
3900 memcpy(buf, acl->data, acl->len);
3904 spin_unlock(&inode->i_lock);
3908 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3910 struct nfs4_cached_acl *acl;
3911 size_t buflen = sizeof(*acl) + acl_len;
3913 if (buflen <= PAGE_SIZE) {
3914 acl = kmalloc(buflen, GFP_KERNEL);
3918 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3920 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3927 nfs4_set_cached_acl(inode, acl);
3931 * The getxattr API returns the required buffer length when called with a
3932 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3933 * the required buf. On a NULL buf, we send a page of data to the server
3934 * guessing that the ACL request can be serviced by a page. If so, we cache
3935 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3936 * the cache. If not so, we throw away the page, and cache the required
3937 * length. The next getxattr call will then produce another round trip to
3938 * the server, this time with the input buf of the required size.
3940 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3942 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3943 struct nfs_getaclargs args = {
3944 .fh = NFS_FH(inode),
3948 struct nfs_getaclres res = {
3951 struct rpc_message msg = {
3952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3956 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3957 int ret = -ENOMEM, i;
3959 /* As long as we're doing a round trip to the server anyway,
3960 * let's be prepared for a page of acl data. */
3963 if (npages > ARRAY_SIZE(pages))
3966 for (i = 0; i < npages; i++) {
3967 pages[i] = alloc_page(GFP_KERNEL);
3972 /* for decoding across pages */
3973 res.acl_scratch = alloc_page(GFP_KERNEL);
3974 if (!res.acl_scratch)
3977 args.acl_len = npages * PAGE_SIZE;
3978 args.acl_pgbase = 0;
3980 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3981 __func__, buf, buflen, npages, args.acl_len);
3982 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3983 &msg, &args.seq_args, &res.seq_res, 0);
3987 /* Handle the case where the passed-in buffer is too short */
3988 if (res.acl_flags & NFS4_ACL_TRUNC) {
3989 /* Did the user only issue a request for the acl length? */
3995 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3997 if (res.acl_len > buflen) {
4001 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4006 for (i = 0; i < npages; i++)
4008 __free_page(pages[i]);
4009 if (res.acl_scratch)
4010 __free_page(res.acl_scratch);
4014 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4016 struct nfs4_exception exception = { };
4019 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4022 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4023 } while (exception.retry);
4027 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4029 struct nfs_server *server = NFS_SERVER(inode);
4032 if (!nfs4_server_supports_acls(server))
4034 ret = nfs_revalidate_inode(server, inode);
4037 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4038 nfs_zap_acl_cache(inode);
4039 ret = nfs4_read_cached_acl(inode, buf, buflen);
4041 /* -ENOENT is returned if there is no ACL or if there is an ACL
4042 * but no cached acl data, just the acl length */
4044 return nfs4_get_acl_uncached(inode, buf, buflen);
4047 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4049 struct nfs_server *server = NFS_SERVER(inode);
4050 struct page *pages[NFS4ACL_MAXPAGES];
4051 struct nfs_setaclargs arg = {
4052 .fh = NFS_FH(inode),
4056 struct nfs_setaclres res;
4057 struct rpc_message msg = {
4058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4062 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4065 if (!nfs4_server_supports_acls(server))
4067 if (npages > ARRAY_SIZE(pages))
4069 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4072 nfs4_inode_return_delegation(inode);
4073 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4076 * Free each page after tx, so the only ref left is
4077 * held by the network stack
4080 put_page(pages[i-1]);
4083 * Acl update can result in inode attribute update.
4084 * so mark the attribute cache invalid.
4086 spin_lock(&inode->i_lock);
4087 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4088 spin_unlock(&inode->i_lock);
4089 nfs_access_zap_cache(inode);
4090 nfs_zap_acl_cache(inode);
4094 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4096 struct nfs4_exception exception = { };
4099 err = nfs4_handle_exception(NFS_SERVER(inode),
4100 __nfs4_proc_set_acl(inode, buf, buflen),
4102 } while (exception.retry);
4107 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4109 struct nfs_client *clp = server->nfs_client;
4111 if (task->tk_status >= 0)
4113 switch(task->tk_status) {
4114 case -NFS4ERR_DELEG_REVOKED:
4115 case -NFS4ERR_ADMIN_REVOKED:
4116 case -NFS4ERR_BAD_STATEID:
4119 nfs_remove_bad_delegation(state->inode);
4120 case -NFS4ERR_OPENMODE:
4123 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4124 goto stateid_invalid;
4125 goto wait_on_recovery;
4126 case -NFS4ERR_EXPIRED:
4127 if (state != NULL) {
4128 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4129 goto stateid_invalid;
4131 case -NFS4ERR_STALE_STATEID:
4132 case -NFS4ERR_STALE_CLIENTID:
4133 nfs4_schedule_lease_recovery(clp);
4134 goto wait_on_recovery;
4135 #if defined(CONFIG_NFS_V4_1)
4136 case -NFS4ERR_BADSESSION:
4137 case -NFS4ERR_BADSLOT:
4138 case -NFS4ERR_BAD_HIGH_SLOT:
4139 case -NFS4ERR_DEADSESSION:
4140 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4141 case -NFS4ERR_SEQ_FALSE_RETRY:
4142 case -NFS4ERR_SEQ_MISORDERED:
4143 dprintk("%s ERROR %d, Reset session\n", __func__,
4145 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4146 task->tk_status = 0;
4148 #endif /* CONFIG_NFS_V4_1 */
4149 case -NFS4ERR_DELAY:
4150 nfs_inc_server_stats(server, NFSIOS_DELAY);
4151 case -NFS4ERR_GRACE:
4152 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4153 task->tk_status = 0;
4155 case -NFS4ERR_RETRY_UNCACHED_REP:
4156 case -NFS4ERR_OLD_STATEID:
4157 task->tk_status = 0;
4160 task->tk_status = nfs4_map_errors(task->tk_status);
4163 task->tk_status = -EIO;
4166 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4167 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4168 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4169 task->tk_status = 0;
4173 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4174 nfs4_verifier *bootverf)
4178 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4179 /* An impossible timestamp guarantees this value
4180 * will never match a generated boot time. */
4182 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4184 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4185 verf[0] = (__be32)nn->boot_time.tv_sec;
4186 verf[1] = (__be32)nn->boot_time.tv_nsec;
4188 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4192 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4193 char *buf, size_t len)
4195 unsigned int result;
4198 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4200 rpc_peeraddr2str(clp->cl_rpcclient,
4202 rpc_peeraddr2str(clp->cl_rpcclient,
4203 RPC_DISPLAY_PROTO));
4209 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4210 char *buf, size_t len)
4212 char *nodename = clp->cl_rpcclient->cl_nodename;
4214 if (nfs4_client_id_uniquifier[0] != '\0')
4215 nodename = nfs4_client_id_uniquifier;
4216 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4217 clp->rpc_ops->version, clp->cl_minorversion,
4222 * nfs4_proc_setclientid - Negotiate client ID
4223 * @clp: state data structure
4224 * @program: RPC program for NFSv4 callback service
4225 * @port: IP port number for NFS4 callback service
4226 * @cred: RPC credential to use for this call
4227 * @res: where to place the result
4229 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4231 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4232 unsigned short port, struct rpc_cred *cred,
4233 struct nfs4_setclientid_res *res)
4235 nfs4_verifier sc_verifier;
4236 struct nfs4_setclientid setclientid = {
4237 .sc_verifier = &sc_verifier,
4239 .sc_cb_ident = clp->cl_cb_ident,
4241 struct rpc_message msg = {
4242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4243 .rpc_argp = &setclientid,
4249 /* nfs_client_id4 */
4250 nfs4_init_boot_verifier(clp, &sc_verifier);
4251 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4252 setclientid.sc_name_len =
4253 nfs4_init_uniform_client_string(clp,
4254 setclientid.sc_name,
4255 sizeof(setclientid.sc_name));
4257 setclientid.sc_name_len =
4258 nfs4_init_nonuniform_client_string(clp,
4259 setclientid.sc_name,
4260 sizeof(setclientid.sc_name));
4263 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4264 sizeof(setclientid.sc_netid),
4265 rpc_peeraddr2str(clp->cl_rpcclient,
4266 RPC_DISPLAY_NETID));
4268 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4269 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4270 clp->cl_ipaddr, port >> 8, port & 255);
4272 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4273 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4274 setclientid.sc_name_len, setclientid.sc_name);
4275 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4276 dprintk("NFS reply setclientid: %d\n", status);
4281 * nfs4_proc_setclientid_confirm - Confirm client ID
4282 * @clp: state data structure
4283 * @res: result of a previous SETCLIENTID
4284 * @cred: RPC credential to use for this call
4286 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4288 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4289 struct nfs4_setclientid_res *arg,
4290 struct rpc_cred *cred)
4292 struct nfs_fsinfo fsinfo;
4293 struct rpc_message msg = {
4294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4296 .rpc_resp = &fsinfo,
4302 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4303 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4306 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4308 spin_lock(&clp->cl_lock);
4309 clp->cl_lease_time = fsinfo.lease_time * HZ;
4310 clp->cl_last_renewal = now;
4311 spin_unlock(&clp->cl_lock);
4313 dprintk("NFS reply setclientid_confirm: %d\n", status);
4317 struct nfs4_delegreturndata {
4318 struct nfs4_delegreturnargs args;
4319 struct nfs4_delegreturnres res;
4321 nfs4_stateid stateid;
4322 unsigned long timestamp;
4323 struct nfs_fattr fattr;
4327 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4329 struct nfs4_delegreturndata *data = calldata;
4331 if (!nfs4_sequence_done(task, &data->res.seq_res))
4334 switch (task->tk_status) {
4335 case -NFS4ERR_STALE_STATEID:
4336 case -NFS4ERR_EXPIRED:
4338 renew_lease(data->res.server, data->timestamp);
4341 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4343 rpc_restart_call_prepare(task);
4347 data->rpc_status = task->tk_status;
4350 static void nfs4_delegreturn_release(void *calldata)
4355 #if defined(CONFIG_NFS_V4_1)
4356 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4358 struct nfs4_delegreturndata *d_data;
4360 d_data = (struct nfs4_delegreturndata *)data;
4362 nfs4_setup_sequence(d_data->res.server,
4363 &d_data->args.seq_args,
4364 &d_data->res.seq_res,
4367 #endif /* CONFIG_NFS_V4_1 */
4369 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4370 #if defined(CONFIG_NFS_V4_1)
4371 .rpc_call_prepare = nfs4_delegreturn_prepare,
4372 #endif /* CONFIG_NFS_V4_1 */
4373 .rpc_call_done = nfs4_delegreturn_done,
4374 .rpc_release = nfs4_delegreturn_release,
4377 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4379 struct nfs4_delegreturndata *data;
4380 struct nfs_server *server = NFS_SERVER(inode);
4381 struct rpc_task *task;
4382 struct rpc_message msg = {
4383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4386 struct rpc_task_setup task_setup_data = {
4387 .rpc_client = server->client,
4388 .rpc_message = &msg,
4389 .callback_ops = &nfs4_delegreturn_ops,
4390 .flags = RPC_TASK_ASYNC,
4394 data = kzalloc(sizeof(*data), GFP_NOFS);
4397 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4398 data->args.fhandle = &data->fh;
4399 data->args.stateid = &data->stateid;
4400 data->args.bitmask = server->cache_consistency_bitmask;
4401 nfs_copy_fh(&data->fh, NFS_FH(inode));
4402 nfs4_stateid_copy(&data->stateid, stateid);
4403 data->res.fattr = &data->fattr;
4404 data->res.server = server;
4405 nfs_fattr_init(data->res.fattr);
4406 data->timestamp = jiffies;
4407 data->rpc_status = 0;
4409 task_setup_data.callback_data = data;
4410 msg.rpc_argp = &data->args;
4411 msg.rpc_resp = &data->res;
4412 task = rpc_run_task(&task_setup_data);
4414 return PTR_ERR(task);
4417 status = nfs4_wait_for_completion_rpc_task(task);
4420 status = data->rpc_status;
4422 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4424 nfs_refresh_inode(inode, &data->fattr);
4430 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4432 struct nfs_server *server = NFS_SERVER(inode);
4433 struct nfs4_exception exception = { };
4436 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4438 case -NFS4ERR_STALE_STATEID:
4439 case -NFS4ERR_EXPIRED:
4443 err = nfs4_handle_exception(server, err, &exception);
4444 } while (exception.retry);
4448 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4449 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4452 * sleep, with exponential backoff, and retry the LOCK operation.
4454 static unsigned long
4455 nfs4_set_lock_task_retry(unsigned long timeout)
4457 freezable_schedule_timeout_killable(timeout);
4459 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4460 return NFS4_LOCK_MAXTIMEOUT;
4464 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4466 struct inode *inode = state->inode;
4467 struct nfs_server *server = NFS_SERVER(inode);
4468 struct nfs_client *clp = server->nfs_client;
4469 struct nfs_lockt_args arg = {
4470 .fh = NFS_FH(inode),
4473 struct nfs_lockt_res res = {
4476 struct rpc_message msg = {
4477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4480 .rpc_cred = state->owner->so_cred,
4482 struct nfs4_lock_state *lsp;
4485 arg.lock_owner.clientid = clp->cl_clientid;
4486 status = nfs4_set_lock_state(state, request);
4489 lsp = request->fl_u.nfs4_fl.owner;
4490 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4491 arg.lock_owner.s_dev = server->s_dev;
4492 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4495 request->fl_type = F_UNLCK;
4497 case -NFS4ERR_DENIED:
4500 request->fl_ops->fl_release_private(request);
4505 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4507 struct nfs4_exception exception = { };
4511 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4512 _nfs4_proc_getlk(state, cmd, request),
4514 } while (exception.retry);
4518 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4521 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4523 res = posix_lock_file_wait(file, fl);
4526 res = flock_lock_file_wait(file, fl);
4534 struct nfs4_unlockdata {
4535 struct nfs_locku_args arg;
4536 struct nfs_locku_res res;
4537 struct nfs4_lock_state *lsp;
4538 struct nfs_open_context *ctx;
4539 struct file_lock fl;
4540 const struct nfs_server *server;
4541 unsigned long timestamp;
4544 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4545 struct nfs_open_context *ctx,
4546 struct nfs4_lock_state *lsp,
4547 struct nfs_seqid *seqid)
4549 struct nfs4_unlockdata *p;
4550 struct inode *inode = lsp->ls_state->inode;
4552 p = kzalloc(sizeof(*p), GFP_NOFS);
4555 p->arg.fh = NFS_FH(inode);
4557 p->arg.seqid = seqid;
4558 p->res.seqid = seqid;
4559 p->arg.stateid = &lsp->ls_stateid;
4561 atomic_inc(&lsp->ls_count);
4562 /* Ensure we don't close file until we're done freeing locks! */
4563 p->ctx = get_nfs_open_context(ctx);
4564 memcpy(&p->fl, fl, sizeof(p->fl));
4565 p->server = NFS_SERVER(inode);
4569 static void nfs4_locku_release_calldata(void *data)
4571 struct nfs4_unlockdata *calldata = data;
4572 nfs_free_seqid(calldata->arg.seqid);
4573 nfs4_put_lock_state(calldata->lsp);
4574 put_nfs_open_context(calldata->ctx);
4578 static void nfs4_locku_done(struct rpc_task *task, void *data)
4580 struct nfs4_unlockdata *calldata = data;
4582 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4584 switch (task->tk_status) {
4586 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4587 &calldata->res.stateid);
4588 renew_lease(calldata->server, calldata->timestamp);
4590 case -NFS4ERR_BAD_STATEID:
4591 case -NFS4ERR_OLD_STATEID:
4592 case -NFS4ERR_STALE_STATEID:
4593 case -NFS4ERR_EXPIRED:
4596 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4597 rpc_restart_call_prepare(task);
4599 nfs_release_seqid(calldata->arg.seqid);
4602 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4604 struct nfs4_unlockdata *calldata = data;
4606 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4608 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4609 /* Note: exit _without_ running nfs4_locku_done */
4612 calldata->timestamp = jiffies;
4613 if (nfs4_setup_sequence(calldata->server,
4614 &calldata->arg.seq_args,
4615 &calldata->res.seq_res,
4617 nfs_release_seqid(calldata->arg.seqid);
4620 task->tk_action = NULL;
4622 nfs4_sequence_done(task, &calldata->res.seq_res);
4625 static const struct rpc_call_ops nfs4_locku_ops = {
4626 .rpc_call_prepare = nfs4_locku_prepare,
4627 .rpc_call_done = nfs4_locku_done,
4628 .rpc_release = nfs4_locku_release_calldata,
4631 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4632 struct nfs_open_context *ctx,
4633 struct nfs4_lock_state *lsp,
4634 struct nfs_seqid *seqid)
4636 struct nfs4_unlockdata *data;
4637 struct rpc_message msg = {
4638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4639 .rpc_cred = ctx->cred,
4641 struct rpc_task_setup task_setup_data = {
4642 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4643 .rpc_message = &msg,
4644 .callback_ops = &nfs4_locku_ops,
4645 .workqueue = nfsiod_workqueue,
4646 .flags = RPC_TASK_ASYNC,
4649 /* Ensure this is an unlock - when canceling a lock, the
4650 * canceled lock is passed in, and it won't be an unlock.
4652 fl->fl_type = F_UNLCK;
4654 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4656 nfs_free_seqid(seqid);
4657 return ERR_PTR(-ENOMEM);
4660 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4661 msg.rpc_argp = &data->arg;
4662 msg.rpc_resp = &data->res;
4663 task_setup_data.callback_data = data;
4664 return rpc_run_task(&task_setup_data);
4667 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4669 struct inode *inode = state->inode;
4670 struct nfs4_state_owner *sp = state->owner;
4671 struct nfs_inode *nfsi = NFS_I(inode);
4672 struct nfs_seqid *seqid;
4673 struct nfs4_lock_state *lsp;
4674 struct rpc_task *task;
4676 unsigned char fl_flags = request->fl_flags;
4678 status = nfs4_set_lock_state(state, request);
4679 /* Unlock _before_ we do the RPC call */
4680 request->fl_flags |= FL_EXISTS;
4681 /* Exclude nfs_delegation_claim_locks() */
4682 mutex_lock(&sp->so_delegreturn_mutex);
4683 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
4684 down_read(&nfsi->rwsem);
4685 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4686 up_read(&nfsi->rwsem);
4687 mutex_unlock(&sp->so_delegreturn_mutex);
4690 up_read(&nfsi->rwsem);
4691 mutex_unlock(&sp->so_delegreturn_mutex);
4694 /* Is this a delegated lock? */
4695 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4697 lsp = request->fl_u.nfs4_fl.owner;
4698 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4702 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4703 status = PTR_ERR(task);
4706 status = nfs4_wait_for_completion_rpc_task(task);
4709 request->fl_flags = fl_flags;
4713 struct nfs4_lockdata {
4714 struct nfs_lock_args arg;
4715 struct nfs_lock_res res;
4716 struct nfs4_lock_state *lsp;
4717 struct nfs_open_context *ctx;
4718 struct file_lock fl;
4719 unsigned long timestamp;
4722 struct nfs_server *server;
4725 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4726 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4729 struct nfs4_lockdata *p;
4730 struct inode *inode = lsp->ls_state->inode;
4731 struct nfs_server *server = NFS_SERVER(inode);
4733 p = kzalloc(sizeof(*p), gfp_mask);
4737 p->arg.fh = NFS_FH(inode);
4739 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4740 if (p->arg.open_seqid == NULL)
4742 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4743 if (p->arg.lock_seqid == NULL)
4744 goto out_free_seqid;
4745 p->arg.lock_stateid = &lsp->ls_stateid;
4746 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4747 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4748 p->arg.lock_owner.s_dev = server->s_dev;
4749 p->res.lock_seqid = p->arg.lock_seqid;
4752 atomic_inc(&lsp->ls_count);
4753 p->ctx = get_nfs_open_context(ctx);
4754 memcpy(&p->fl, fl, sizeof(p->fl));
4757 nfs_free_seqid(p->arg.open_seqid);
4763 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4765 struct nfs4_lockdata *data = calldata;
4766 struct nfs4_state *state = data->lsp->ls_state;
4768 dprintk("%s: begin!\n", __func__);
4769 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4771 /* Do we need to do an open_to_lock_owner? */
4772 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4773 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4774 goto out_release_lock_seqid;
4776 data->arg.open_stateid = &state->stateid;
4777 data->arg.new_lock_owner = 1;
4778 data->res.open_seqid = data->arg.open_seqid;
4780 data->arg.new_lock_owner = 0;
4781 if (!nfs4_valid_open_stateid(state)) {
4782 data->rpc_status = -EBADF;
4783 task->tk_action = NULL;
4784 goto out_release_open_seqid;
4786 data->timestamp = jiffies;
4787 if (nfs4_setup_sequence(data->server,
4788 &data->arg.seq_args,
4792 out_release_open_seqid:
4793 nfs_release_seqid(data->arg.open_seqid);
4794 out_release_lock_seqid:
4795 nfs_release_seqid(data->arg.lock_seqid);
4797 nfs4_sequence_done(task, &data->res.seq_res);
4798 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4801 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4803 struct nfs4_lockdata *data = calldata;
4805 dprintk("%s: begin!\n", __func__);
4807 if (!nfs4_sequence_done(task, &data->res.seq_res))
4810 data->rpc_status = task->tk_status;
4811 if (data->arg.new_lock_owner != 0) {
4812 if (data->rpc_status == 0)
4813 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4817 if (data->rpc_status == 0) {
4818 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4819 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4820 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4823 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4826 static void nfs4_lock_release(void *calldata)
4828 struct nfs4_lockdata *data = calldata;
4830 dprintk("%s: begin!\n", __func__);
4831 nfs_free_seqid(data->arg.open_seqid);
4832 if (data->cancelled != 0) {
4833 struct rpc_task *task;
4834 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4835 data->arg.lock_seqid);
4837 rpc_put_task_async(task);
4838 dprintk("%s: cancelling lock!\n", __func__);
4840 nfs_free_seqid(data->arg.lock_seqid);
4841 nfs4_put_lock_state(data->lsp);
4842 put_nfs_open_context(data->ctx);
4844 dprintk("%s: done!\n", __func__);
4847 static const struct rpc_call_ops nfs4_lock_ops = {
4848 .rpc_call_prepare = nfs4_lock_prepare,
4849 .rpc_call_done = nfs4_lock_done,
4850 .rpc_release = nfs4_lock_release,
4853 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4856 case -NFS4ERR_ADMIN_REVOKED:
4857 case -NFS4ERR_BAD_STATEID:
4858 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4859 if (new_lock_owner != 0 ||
4860 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4861 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4863 case -NFS4ERR_STALE_STATEID:
4864 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4865 case -NFS4ERR_EXPIRED:
4866 nfs4_schedule_lease_recovery(server->nfs_client);
4870 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4872 struct nfs4_lockdata *data;
4873 struct rpc_task *task;
4874 struct rpc_message msg = {
4875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4876 .rpc_cred = state->owner->so_cred,
4878 struct rpc_task_setup task_setup_data = {
4879 .rpc_client = NFS_CLIENT(state->inode),
4880 .rpc_message = &msg,
4881 .callback_ops = &nfs4_lock_ops,
4882 .workqueue = nfsiod_workqueue,
4883 .flags = RPC_TASK_ASYNC,
4887 dprintk("%s: begin!\n", __func__);
4888 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4889 fl->fl_u.nfs4_fl.owner,
4890 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4894 data->arg.block = 1;
4895 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4896 msg.rpc_argp = &data->arg;
4897 msg.rpc_resp = &data->res;
4898 task_setup_data.callback_data = data;
4899 if (recovery_type > NFS_LOCK_NEW) {
4900 if (recovery_type == NFS_LOCK_RECLAIM)
4901 data->arg.reclaim = NFS_LOCK_RECLAIM;
4902 nfs4_set_sequence_privileged(&data->arg.seq_args);
4904 task = rpc_run_task(&task_setup_data);
4906 return PTR_ERR(task);
4907 ret = nfs4_wait_for_completion_rpc_task(task);
4909 ret = data->rpc_status;
4911 nfs4_handle_setlk_error(data->server, data->lsp,
4912 data->arg.new_lock_owner, ret);
4914 data->cancelled = 1;
4916 dprintk("%s: done, ret = %d!\n", __func__, ret);
4920 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4922 struct nfs_server *server = NFS_SERVER(state->inode);
4923 struct nfs4_exception exception = {
4924 .inode = state->inode,
4929 /* Cache the lock if possible... */
4930 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4932 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4933 if (err != -NFS4ERR_DELAY)
4935 nfs4_handle_exception(server, err, &exception);
4936 } while (exception.retry);
4940 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4942 struct nfs_server *server = NFS_SERVER(state->inode);
4943 struct nfs4_exception exception = {
4944 .inode = state->inode,
4948 err = nfs4_set_lock_state(state, request);
4952 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4954 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4958 case -NFS4ERR_GRACE:
4959 case -NFS4ERR_DELAY:
4960 nfs4_handle_exception(server, err, &exception);
4963 } while (exception.retry);
4968 #if defined(CONFIG_NFS_V4_1)
4970 * nfs41_check_expired_locks - possibly free a lock stateid
4972 * @state: NFSv4 state for an inode
4974 * Returns NFS_OK if recovery for this stateid is now finished.
4975 * Otherwise a negative NFS4ERR value is returned.
4977 static int nfs41_check_expired_locks(struct nfs4_state *state)
4979 int status, ret = -NFS4ERR_BAD_STATEID;
4980 struct nfs4_lock_state *lsp;
4981 struct nfs_server *server = NFS_SERVER(state->inode);
4983 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4984 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4985 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4986 if (status != NFS_OK) {
4987 /* Free the stateid unless the server
4988 * informs us the stateid is unrecognized. */
4989 if (status != -NFS4ERR_BAD_STATEID)
4990 nfs41_free_stateid(server,
4992 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5001 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5003 int status = NFS_OK;
5005 if (test_bit(LK_STATE_IN_USE, &state->flags))
5006 status = nfs41_check_expired_locks(state);
5007 if (status != NFS_OK)
5008 status = nfs4_lock_expired(state, request);
5013 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5015 struct nfs4_state_owner *sp = state->owner;
5016 struct nfs_inode *nfsi = NFS_I(state->inode);
5017 unsigned char fl_flags = request->fl_flags;
5019 int status = -ENOLCK;
5021 if ((fl_flags & FL_POSIX) &&
5022 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5024 /* Is this a delegated open? */
5025 status = nfs4_set_lock_state(state, request);
5028 request->fl_flags |= FL_ACCESS;
5029 status = do_vfs_lock(request->fl_file, request);
5032 down_read(&nfsi->rwsem);
5033 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5034 /* Yes: cache locks! */
5035 /* ...but avoid races with delegation recall... */
5036 request->fl_flags = fl_flags & ~FL_SLEEP;
5037 status = do_vfs_lock(request->fl_file, request);
5040 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5041 up_read(&nfsi->rwsem);
5042 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5045 down_read(&nfsi->rwsem);
5046 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5047 status = -NFS4ERR_DELAY;
5050 /* Note: we always want to sleep here! */
5051 request->fl_flags = fl_flags | FL_SLEEP;
5052 if (do_vfs_lock(request->fl_file, request) < 0)
5053 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5054 "manager!\n", __func__);
5056 up_read(&nfsi->rwsem);
5058 request->fl_flags = fl_flags;
5062 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5064 struct nfs4_exception exception = {
5066 .inode = state->inode,
5071 err = _nfs4_proc_setlk(state, cmd, request);
5072 if (err == -NFS4ERR_DENIED)
5074 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5076 } while (exception.retry);
5081 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5083 struct nfs_open_context *ctx;
5084 struct nfs4_state *state;
5085 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5088 /* verify open state */
5089 ctx = nfs_file_open_context(filp);
5092 if (request->fl_start < 0 || request->fl_end < 0)
5095 if (IS_GETLK(cmd)) {
5097 return nfs4_proc_getlk(state, F_GETLK, request);
5101 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5104 if (request->fl_type == F_UNLCK) {
5106 return nfs4_proc_unlck(state, cmd, request);
5113 * Don't rely on the VFS having checked the file open mode,
5114 * since it won't do this for flock() locks.
5116 switch (request->fl_type) {
5118 if (!(filp->f_mode & FMODE_READ))
5122 if (!(filp->f_mode & FMODE_WRITE))
5127 status = nfs4_proc_setlk(state, cmd, request);
5128 if ((status != -EAGAIN) || IS_SETLK(cmd))
5130 timeout = nfs4_set_lock_task_retry(timeout);
5131 status = -ERESTARTSYS;
5134 } while(status < 0);
5138 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5140 struct nfs_server *server = NFS_SERVER(state->inode);
5141 struct nfs4_exception exception = { };
5144 err = nfs4_set_lock_state(state, fl);
5148 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5151 printk(KERN_ERR "NFS: %s: unhandled error "
5152 "%d.\n", __func__, err);
5156 case -NFS4ERR_STALE_CLIENTID:
5157 case -NFS4ERR_STALE_STATEID:
5158 set_bit(NFS_DELEGATED_STATE, &state->flags);
5159 case -NFS4ERR_EXPIRED:
5160 nfs4_schedule_lease_recovery(server->nfs_client);
5163 case -NFS4ERR_BADSESSION:
5164 case -NFS4ERR_BADSLOT:
5165 case -NFS4ERR_BAD_HIGH_SLOT:
5166 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5167 case -NFS4ERR_DEADSESSION:
5168 set_bit(NFS_DELEGATED_STATE, &state->flags);
5169 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5172 case -NFS4ERR_DELEG_REVOKED:
5173 case -NFS4ERR_ADMIN_REVOKED:
5174 case -NFS4ERR_BAD_STATEID:
5175 case -NFS4ERR_OPENMODE:
5176 nfs4_schedule_stateid_recovery(server, state);
5180 case -NFS4ERR_DENIED:
5181 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5185 set_bit(NFS_DELEGATED_STATE, &state->flags);
5186 err = nfs4_handle_exception(server, err, &exception);
5187 } while (exception.retry);
5192 struct nfs_release_lockowner_data {
5193 struct nfs4_lock_state *lsp;
5194 struct nfs_server *server;
5195 struct nfs_release_lockowner_args args;
5198 static void nfs4_release_lockowner_release(void *calldata)
5200 struct nfs_release_lockowner_data *data = calldata;
5201 nfs4_free_lock_state(data->server, data->lsp);
5205 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5206 .rpc_release = nfs4_release_lockowner_release,
5209 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5211 struct nfs_server *server = lsp->ls_state->owner->so_server;
5212 struct nfs_release_lockowner_data *data;
5213 struct rpc_message msg = {
5214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5217 if (server->nfs_client->cl_mvops->minor_version != 0)
5219 data = kmalloc(sizeof(*data), GFP_NOFS);
5223 data->server = server;
5224 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5225 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5226 data->args.lock_owner.s_dev = server->s_dev;
5227 msg.rpc_argp = &data->args;
5228 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5232 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5234 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5235 const void *buf, size_t buflen,
5236 int flags, int type)
5238 if (strcmp(key, "") != 0)
5241 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5244 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5245 void *buf, size_t buflen, int type)
5247 if (strcmp(key, "") != 0)
5250 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5253 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5254 size_t list_len, const char *name,
5255 size_t name_len, int type)
5257 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5259 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5262 if (list && len <= list_len)
5263 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5268 * nfs_fhget will use either the mounted_on_fileid or the fileid
5270 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5272 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5273 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5274 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5275 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5278 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5279 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5280 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5284 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5285 const struct qstr *name,
5286 struct nfs4_fs_locations *fs_locations,
5289 struct nfs_server *server = NFS_SERVER(dir);
5291 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5293 struct nfs4_fs_locations_arg args = {
5294 .dir_fh = NFS_FH(dir),
5299 struct nfs4_fs_locations_res res = {
5300 .fs_locations = fs_locations,
5302 struct rpc_message msg = {
5303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5309 dprintk("%s: start\n", __func__);
5311 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5312 * is not supported */
5313 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5314 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5316 bitmask[0] |= FATTR4_WORD0_FILEID;
5318 nfs_fattr_init(&fs_locations->fattr);
5319 fs_locations->server = server;
5320 fs_locations->nlocations = 0;
5321 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5322 dprintk("%s: returned status = %d\n", __func__, status);
5326 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5327 const struct qstr *name,
5328 struct nfs4_fs_locations *fs_locations,
5331 struct nfs4_exception exception = { };
5334 err = nfs4_handle_exception(NFS_SERVER(dir),
5335 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5337 } while (exception.retry);
5341 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5344 struct nfs4_secinfo_arg args = {
5345 .dir_fh = NFS_FH(dir),
5348 struct nfs4_secinfo_res res = {
5351 struct rpc_message msg = {
5352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5357 dprintk("NFS call secinfo %s\n", name->name);
5358 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5359 dprintk("NFS reply secinfo: %d\n", status);
5363 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5364 struct nfs4_secinfo_flavors *flavors)
5366 struct nfs4_exception exception = { };
5369 err = nfs4_handle_exception(NFS_SERVER(dir),
5370 _nfs4_proc_secinfo(dir, name, flavors),
5372 } while (exception.retry);
5376 #ifdef CONFIG_NFS_V4_1
5378 * Check the exchange flags returned by the server for invalid flags, having
5379 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5382 static int nfs4_check_cl_exchange_flags(u32 flags)
5384 if (flags & ~EXCHGID4_FLAG_MASK_R)
5386 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5387 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5389 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5393 return -NFS4ERR_INVAL;
5397 nfs41_same_server_scope(struct nfs41_server_scope *a,
5398 struct nfs41_server_scope *b)
5400 if (a->server_scope_sz == b->server_scope_sz &&
5401 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5408 * nfs4_proc_bind_conn_to_session()
5410 * The 4.1 client currently uses the same TCP connection for the
5411 * fore and backchannel.
5413 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5416 struct nfs41_bind_conn_to_session_res res;
5417 struct rpc_message msg = {
5419 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5425 dprintk("--> %s\n", __func__);
5427 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5428 if (unlikely(res.session == NULL)) {
5433 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5435 if (memcmp(res.session->sess_id.data,
5436 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5437 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5441 if (res.dir != NFS4_CDFS4_BOTH) {
5442 dprintk("NFS: %s: Unexpected direction from server\n",
5447 if (res.use_conn_in_rdma_mode) {
5448 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5457 dprintk("<-- %s status= %d\n", __func__, status);
5462 * nfs4_proc_exchange_id()
5464 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5466 * Since the clientid has expired, all compounds using sessions
5467 * associated with the stale clientid will be returning
5468 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5469 * be in some phase of session reset.
5471 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5473 nfs4_verifier verifier;
5474 struct nfs41_exchange_id_args args = {
5475 .verifier = &verifier,
5477 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5479 struct nfs41_exchange_id_res res = {
5483 struct rpc_message msg = {
5484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5490 nfs4_init_boot_verifier(clp, &verifier);
5491 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5493 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5494 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5495 args.id_len, args.id);
5497 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5499 if (unlikely(res.server_owner == NULL)) {
5504 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5506 if (unlikely(res.server_scope == NULL)) {
5508 goto out_server_owner;
5511 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5512 if (unlikely(res.impl_id == NULL)) {
5514 goto out_server_scope;
5517 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5519 status = nfs4_check_cl_exchange_flags(res.flags);
5522 clp->cl_clientid = res.clientid;
5523 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5524 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5525 clp->cl_seqid = res.seqid;
5527 kfree(clp->cl_serverowner);
5528 clp->cl_serverowner = res.server_owner;
5529 res.server_owner = NULL;
5531 /* use the most recent implementation id */
5532 kfree(clp->cl_implid);
5533 clp->cl_implid = res.impl_id;
5535 if (clp->cl_serverscope != NULL &&
5536 !nfs41_same_server_scope(clp->cl_serverscope,
5537 res.server_scope)) {
5538 dprintk("%s: server_scope mismatch detected\n",
5540 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5541 kfree(clp->cl_serverscope);
5542 clp->cl_serverscope = NULL;
5545 if (clp->cl_serverscope == NULL) {
5546 clp->cl_serverscope = res.server_scope;
5553 kfree(res.server_owner);
5555 kfree(res.server_scope);
5557 if (clp->cl_implid != NULL)
5558 dprintk("NFS reply exchange_id: Server Implementation ID: "
5559 "domain: %s, name: %s, date: %llu,%u\n",
5560 clp->cl_implid->domain, clp->cl_implid->name,
5561 clp->cl_implid->date.seconds,
5562 clp->cl_implid->date.nseconds);
5563 dprintk("NFS reply exchange_id: %d\n", status);
5567 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5568 struct rpc_cred *cred)
5570 struct rpc_message msg = {
5571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5577 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5579 dprintk("NFS: Got error %d from the server %s on "
5580 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5584 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5585 struct rpc_cred *cred)
5590 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5591 ret = _nfs4_proc_destroy_clientid(clp, cred);
5593 case -NFS4ERR_DELAY:
5594 case -NFS4ERR_CLIENTID_BUSY:
5604 int nfs4_destroy_clientid(struct nfs_client *clp)
5606 struct rpc_cred *cred;
5609 if (clp->cl_mvops->minor_version < 1)
5611 if (clp->cl_exchange_flags == 0)
5613 if (clp->cl_preserve_clid)
5615 cred = nfs4_get_exchange_id_cred(clp);
5616 ret = nfs4_proc_destroy_clientid(clp, cred);
5621 case -NFS4ERR_STALE_CLIENTID:
5622 clp->cl_exchange_flags = 0;
5628 struct nfs4_get_lease_time_data {
5629 struct nfs4_get_lease_time_args *args;
5630 struct nfs4_get_lease_time_res *res;
5631 struct nfs_client *clp;
5634 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5637 struct nfs4_get_lease_time_data *data =
5638 (struct nfs4_get_lease_time_data *)calldata;
5640 dprintk("--> %s\n", __func__);
5641 /* just setup sequence, do not trigger session recovery
5642 since we're invoked within one */
5643 nfs41_setup_sequence(data->clp->cl_session,
5644 &data->args->la_seq_args,
5645 &data->res->lr_seq_res,
5647 dprintk("<-- %s\n", __func__);
5651 * Called from nfs4_state_manager thread for session setup, so don't recover
5652 * from sequence operation or clientid errors.
5654 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5656 struct nfs4_get_lease_time_data *data =
5657 (struct nfs4_get_lease_time_data *)calldata;
5659 dprintk("--> %s\n", __func__);
5660 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5662 switch (task->tk_status) {
5663 case -NFS4ERR_DELAY:
5664 case -NFS4ERR_GRACE:
5665 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5666 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5667 task->tk_status = 0;
5669 case -NFS4ERR_RETRY_UNCACHED_REP:
5670 rpc_restart_call_prepare(task);
5673 dprintk("<-- %s\n", __func__);
5676 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5677 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5678 .rpc_call_done = nfs4_get_lease_time_done,
5681 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5683 struct rpc_task *task;
5684 struct nfs4_get_lease_time_args args;
5685 struct nfs4_get_lease_time_res res = {
5686 .lr_fsinfo = fsinfo,
5688 struct nfs4_get_lease_time_data data = {
5693 struct rpc_message msg = {
5694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5698 struct rpc_task_setup task_setup = {
5699 .rpc_client = clp->cl_rpcclient,
5700 .rpc_message = &msg,
5701 .callback_ops = &nfs4_get_lease_time_ops,
5702 .callback_data = &data,
5703 .flags = RPC_TASK_TIMEOUT,
5707 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5708 nfs4_set_sequence_privileged(&args.la_seq_args);
5709 dprintk("--> %s\n", __func__);
5710 task = rpc_run_task(&task_setup);
5713 status = PTR_ERR(task);
5715 status = task->tk_status;
5718 dprintk("<-- %s return %d\n", __func__, status);
5724 * Initialize the values to be used by the client in CREATE_SESSION
5725 * If nfs4_init_session set the fore channel request and response sizes,
5728 * Set the back channel max_resp_sz_cached to zero to force the client to
5729 * always set csa_cachethis to FALSE because the current implementation
5730 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5732 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5734 struct nfs4_session *session = args->client->cl_session;
5735 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5736 mxresp_sz = session->fc_target_max_resp_sz;
5739 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5741 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5742 /* Fore channel attributes */
5743 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5744 args->fc_attrs.max_resp_sz = mxresp_sz;
5745 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5746 args->fc_attrs.max_reqs = max_session_slots;
5748 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5749 "max_ops=%u max_reqs=%u\n",
5751 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5752 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5754 /* Back channel attributes */
5755 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5756 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5757 args->bc_attrs.max_resp_sz_cached = 0;
5758 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5759 args->bc_attrs.max_reqs = 1;
5761 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5762 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5764 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5765 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5766 args->bc_attrs.max_reqs);
5769 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5771 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5772 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5774 if (rcvd->max_resp_sz > sent->max_resp_sz)
5777 * Our requested max_ops is the minimum we need; we're not
5778 * prepared to break up compounds into smaller pieces than that.
5779 * So, no point even trying to continue if the server won't
5782 if (rcvd->max_ops < sent->max_ops)
5784 if (rcvd->max_reqs == 0)
5786 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5787 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5791 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5793 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5794 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5796 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5798 if (rcvd->max_resp_sz < sent->max_resp_sz)
5800 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5802 /* These would render the backchannel useless: */
5803 if (rcvd->max_ops != sent->max_ops)
5805 if (rcvd->max_reqs != sent->max_reqs)
5810 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5811 struct nfs4_session *session)
5815 ret = nfs4_verify_fore_channel_attrs(args, session);
5818 return nfs4_verify_back_channel_attrs(args, session);
5821 static int _nfs4_proc_create_session(struct nfs_client *clp,
5822 struct rpc_cred *cred)
5824 struct nfs4_session *session = clp->cl_session;
5825 struct nfs41_create_session_args args = {
5827 .cb_program = NFS4_CALLBACK,
5829 struct nfs41_create_session_res res = {
5832 struct rpc_message msg = {
5833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5840 nfs4_init_channel_attrs(&args);
5841 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5843 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5846 /* Verify the session's negotiated channel_attrs values */
5847 status = nfs4_verify_channel_attrs(&args, session);
5848 /* Increment the clientid slot sequence id */
5856 * Issues a CREATE_SESSION operation to the server.
5857 * It is the responsibility of the caller to verify the session is
5858 * expired before calling this routine.
5860 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5864 struct nfs4_session *session = clp->cl_session;
5866 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5868 status = _nfs4_proc_create_session(clp, cred);
5872 /* Init or reset the session slot tables */
5873 status = nfs4_setup_session_slot_tables(session);
5874 dprintk("slot table setup returned %d\n", status);
5878 ptr = (unsigned *)&session->sess_id.data[0];
5879 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5880 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5882 dprintk("<-- %s\n", __func__);
5887 * Issue the over-the-wire RPC DESTROY_SESSION.
5888 * The caller must serialize access to this routine.
5890 int nfs4_proc_destroy_session(struct nfs4_session *session,
5891 struct rpc_cred *cred)
5893 struct rpc_message msg = {
5894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5895 .rpc_argp = session,
5900 dprintk("--> nfs4_proc_destroy_session\n");
5902 /* session is still being setup */
5903 if (session->clp->cl_cons_state != NFS_CS_READY)
5906 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5909 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5910 "Session has been destroyed regardless...\n", status);
5912 dprintk("<-- nfs4_proc_destroy_session\n");
5917 * Renew the cl_session lease.
5919 struct nfs4_sequence_data {
5920 struct nfs_client *clp;
5921 struct nfs4_sequence_args args;
5922 struct nfs4_sequence_res res;
5925 static void nfs41_sequence_release(void *data)
5927 struct nfs4_sequence_data *calldata = data;
5928 struct nfs_client *clp = calldata->clp;
5930 if (atomic_read(&clp->cl_count) > 1)
5931 nfs4_schedule_state_renewal(clp);
5932 nfs_put_client(clp);
5936 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5938 switch(task->tk_status) {
5939 case -NFS4ERR_DELAY:
5940 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5943 nfs4_schedule_lease_recovery(clp);
5948 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5950 struct nfs4_sequence_data *calldata = data;
5951 struct nfs_client *clp = calldata->clp;
5953 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5956 if (task->tk_status < 0) {
5957 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5958 if (atomic_read(&clp->cl_count) == 1)
5961 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5962 rpc_restart_call_prepare(task);
5966 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5968 dprintk("<-- %s\n", __func__);
5971 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5973 struct nfs4_sequence_data *calldata = data;
5974 struct nfs_client *clp = calldata->clp;
5975 struct nfs4_sequence_args *args;
5976 struct nfs4_sequence_res *res;
5978 args = task->tk_msg.rpc_argp;
5979 res = task->tk_msg.rpc_resp;
5981 nfs41_setup_sequence(clp->cl_session, args, res, task);
5984 static const struct rpc_call_ops nfs41_sequence_ops = {
5985 .rpc_call_done = nfs41_sequence_call_done,
5986 .rpc_call_prepare = nfs41_sequence_prepare,
5987 .rpc_release = nfs41_sequence_release,
5990 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5991 struct rpc_cred *cred,
5994 struct nfs4_sequence_data *calldata;
5995 struct rpc_message msg = {
5996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5999 struct rpc_task_setup task_setup_data = {
6000 .rpc_client = clp->cl_rpcclient,
6001 .rpc_message = &msg,
6002 .callback_ops = &nfs41_sequence_ops,
6003 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6006 if (!atomic_inc_not_zero(&clp->cl_count))
6007 return ERR_PTR(-EIO);
6008 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6009 if (calldata == NULL) {
6010 nfs_put_client(clp);
6011 return ERR_PTR(-ENOMEM);
6013 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6015 nfs4_set_sequence_privileged(&calldata->args);
6016 msg.rpc_argp = &calldata->args;
6017 msg.rpc_resp = &calldata->res;
6018 calldata->clp = clp;
6019 task_setup_data.callback_data = calldata;
6021 return rpc_run_task(&task_setup_data);
6024 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6026 struct rpc_task *task;
6029 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6031 task = _nfs41_proc_sequence(clp, cred, false);
6033 ret = PTR_ERR(task);
6035 rpc_put_task_async(task);
6036 dprintk("<-- %s status=%d\n", __func__, ret);
6040 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6042 struct rpc_task *task;
6045 task = _nfs41_proc_sequence(clp, cred, true);
6047 ret = PTR_ERR(task);
6050 ret = rpc_wait_for_completion_task(task);
6052 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6054 if (task->tk_status == 0)
6055 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6056 ret = task->tk_status;
6060 dprintk("<-- %s status=%d\n", __func__, ret);
6064 struct nfs4_reclaim_complete_data {
6065 struct nfs_client *clp;
6066 struct nfs41_reclaim_complete_args arg;
6067 struct nfs41_reclaim_complete_res res;
6070 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6072 struct nfs4_reclaim_complete_data *calldata = data;
6074 nfs41_setup_sequence(calldata->clp->cl_session,
6075 &calldata->arg.seq_args,
6076 &calldata->res.seq_res,
6080 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6082 switch(task->tk_status) {
6084 case -NFS4ERR_COMPLETE_ALREADY:
6085 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6087 case -NFS4ERR_DELAY:
6088 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6090 case -NFS4ERR_RETRY_UNCACHED_REP:
6093 nfs4_schedule_lease_recovery(clp);
6098 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6100 struct nfs4_reclaim_complete_data *calldata = data;
6101 struct nfs_client *clp = calldata->clp;
6102 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6104 dprintk("--> %s\n", __func__);
6105 if (!nfs41_sequence_done(task, res))
6108 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6109 rpc_restart_call_prepare(task);
6112 dprintk("<-- %s\n", __func__);
6115 static void nfs4_free_reclaim_complete_data(void *data)
6117 struct nfs4_reclaim_complete_data *calldata = data;
6122 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6123 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6124 .rpc_call_done = nfs4_reclaim_complete_done,
6125 .rpc_release = nfs4_free_reclaim_complete_data,
6129 * Issue a global reclaim complete.
6131 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6133 struct nfs4_reclaim_complete_data *calldata;
6134 struct rpc_task *task;
6135 struct rpc_message msg = {
6136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6138 struct rpc_task_setup task_setup_data = {
6139 .rpc_client = clp->cl_rpcclient,
6140 .rpc_message = &msg,
6141 .callback_ops = &nfs4_reclaim_complete_call_ops,
6142 .flags = RPC_TASK_ASYNC,
6144 int status = -ENOMEM;
6146 dprintk("--> %s\n", __func__);
6147 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6148 if (calldata == NULL)
6150 calldata->clp = clp;
6151 calldata->arg.one_fs = 0;
6153 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6154 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
6155 msg.rpc_argp = &calldata->arg;
6156 msg.rpc_resp = &calldata->res;
6157 task_setup_data.callback_data = calldata;
6158 task = rpc_run_task(&task_setup_data);
6160 status = PTR_ERR(task);
6163 status = nfs4_wait_for_completion_rpc_task(task);
6165 status = task->tk_status;
6169 dprintk("<-- %s status=%d\n", __func__, status);
6174 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6176 struct nfs4_layoutget *lgp = calldata;
6177 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6178 struct nfs4_session *session = nfs4_get_session(server);
6180 dprintk("--> %s\n", __func__);
6181 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6182 * right now covering the LAYOUTGET we are about to send.
6183 * However, that is not so catastrophic, and there seems
6184 * to be no way to prevent it completely.
6186 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
6187 &lgp->res.seq_res, task))
6189 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6190 NFS_I(lgp->args.inode)->layout,
6191 lgp->args.ctx->state)) {
6192 rpc_exit(task, NFS4_OK);
6196 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6198 struct nfs4_layoutget *lgp = calldata;
6199 struct inode *inode = lgp->args.inode;
6200 struct nfs_server *server = NFS_SERVER(inode);
6201 struct pnfs_layout_hdr *lo;
6202 struct nfs4_state *state = NULL;
6203 unsigned long timeo, giveup;
6205 dprintk("--> %s\n", __func__);
6207 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6210 switch (task->tk_status) {
6213 case -NFS4ERR_LAYOUTTRYLATER:
6214 case -NFS4ERR_RECALLCONFLICT:
6215 timeo = rpc_get_timeout(task->tk_client);
6216 giveup = lgp->args.timestamp + timeo;
6217 if (time_after(giveup, jiffies))
6218 task->tk_status = -NFS4ERR_DELAY;
6220 case -NFS4ERR_EXPIRED:
6221 case -NFS4ERR_BAD_STATEID:
6222 spin_lock(&inode->i_lock);
6223 lo = NFS_I(inode)->layout;
6224 if (!lo || list_empty(&lo->plh_segs)) {
6225 spin_unlock(&inode->i_lock);
6226 /* If the open stateid was bad, then recover it. */
6227 state = lgp->args.ctx->state;
6231 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6232 spin_unlock(&inode->i_lock);
6233 /* Mark the bad layout state as invalid, then
6234 * retry using the open stateid. */
6235 pnfs_free_lseg_list(&head);
6238 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6239 rpc_restart_call_prepare(task);
6241 dprintk("<-- %s\n", __func__);
6244 static size_t max_response_pages(struct nfs_server *server)
6246 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6247 return nfs_page_array_len(0, max_resp_sz);
6250 static void nfs4_free_pages(struct page **pages, size_t size)
6257 for (i = 0; i < size; i++) {
6260 __free_page(pages[i]);
6265 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6267 struct page **pages;
6270 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6272 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6276 for (i = 0; i < size; i++) {
6277 pages[i] = alloc_page(gfp_flags);
6279 dprintk("%s: failed to allocate page\n", __func__);
6280 nfs4_free_pages(pages, size);
6288 static void nfs4_layoutget_release(void *calldata)
6290 struct nfs4_layoutget *lgp = calldata;
6291 struct inode *inode = lgp->args.inode;
6292 struct nfs_server *server = NFS_SERVER(inode);
6293 size_t max_pages = max_response_pages(server);
6295 dprintk("--> %s\n", __func__);
6296 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6297 pnfs_put_layout_hdr(NFS_I(inode)->layout);
6298 put_nfs_open_context(lgp->args.ctx);
6300 dprintk("<-- %s\n", __func__);
6303 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6304 .rpc_call_prepare = nfs4_layoutget_prepare,
6305 .rpc_call_done = nfs4_layoutget_done,
6306 .rpc_release = nfs4_layoutget_release,
6309 struct pnfs_layout_segment *
6310 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6312 struct inode *inode = lgp->args.inode;
6313 struct nfs_server *server = NFS_SERVER(inode);
6314 size_t max_pages = max_response_pages(server);
6315 struct rpc_task *task;
6316 struct rpc_message msg = {
6317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6318 .rpc_argp = &lgp->args,
6319 .rpc_resp = &lgp->res,
6321 struct rpc_task_setup task_setup_data = {
6322 .rpc_client = server->client,
6323 .rpc_message = &msg,
6324 .callback_ops = &nfs4_layoutget_call_ops,
6325 .callback_data = lgp,
6326 .flags = RPC_TASK_ASYNC,
6328 struct pnfs_layout_segment *lseg = NULL;
6331 dprintk("--> %s\n", __func__);
6333 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6334 if (!lgp->args.layout.pages) {
6335 nfs4_layoutget_release(lgp);
6336 return ERR_PTR(-ENOMEM);
6338 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6339 lgp->args.timestamp = jiffies;
6341 lgp->res.layoutp = &lgp->args.layout;
6342 lgp->res.seq_res.sr_slot = NULL;
6343 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6345 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6346 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6348 task = rpc_run_task(&task_setup_data);
6350 return ERR_CAST(task);
6351 status = nfs4_wait_for_completion_rpc_task(task);
6353 status = task->tk_status;
6354 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6355 if (status == 0 && lgp->res.layoutp->len)
6356 lseg = pnfs_layout_process(lgp);
6358 dprintk("<-- %s status=%d\n", __func__, status);
6360 return ERR_PTR(status);
6365 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6367 struct nfs4_layoutreturn *lrp = calldata;
6369 dprintk("--> %s\n", __func__);
6370 nfs41_setup_sequence(lrp->clp->cl_session,
6371 &lrp->args.seq_args,
6376 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6378 struct nfs4_layoutreturn *lrp = calldata;
6379 struct nfs_server *server;
6381 dprintk("--> %s\n", __func__);
6383 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6386 server = NFS_SERVER(lrp->args.inode);
6387 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6388 rpc_restart_call_prepare(task);
6391 dprintk("<-- %s\n", __func__);
6394 static void nfs4_layoutreturn_release(void *calldata)
6396 struct nfs4_layoutreturn *lrp = calldata;
6397 struct pnfs_layout_hdr *lo = lrp->args.layout;
6399 dprintk("--> %s\n", __func__);
6400 spin_lock(&lo->plh_inode->i_lock);
6401 if (lrp->res.lrs_present)
6402 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6403 lo->plh_block_lgets--;
6404 spin_unlock(&lo->plh_inode->i_lock);
6405 pnfs_put_layout_hdr(lrp->args.layout);
6407 dprintk("<-- %s\n", __func__);
6410 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6411 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6412 .rpc_call_done = nfs4_layoutreturn_done,
6413 .rpc_release = nfs4_layoutreturn_release,
6416 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6418 struct rpc_task *task;
6419 struct rpc_message msg = {
6420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6421 .rpc_argp = &lrp->args,
6422 .rpc_resp = &lrp->res,
6424 struct rpc_task_setup task_setup_data = {
6425 .rpc_client = lrp->clp->cl_rpcclient,
6426 .rpc_message = &msg,
6427 .callback_ops = &nfs4_layoutreturn_call_ops,
6428 .callback_data = lrp,
6432 dprintk("--> %s\n", __func__);
6433 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6434 task = rpc_run_task(&task_setup_data);
6436 return PTR_ERR(task);
6437 status = task->tk_status;
6438 dprintk("<-- %s status=%d\n", __func__, status);
6444 * Retrieve the list of Data Server devices from the MDS.
6446 static int _nfs4_getdevicelist(struct nfs_server *server,
6447 const struct nfs_fh *fh,
6448 struct pnfs_devicelist *devlist)
6450 struct nfs4_getdevicelist_args args = {
6452 .layoutclass = server->pnfs_curr_ld->id,
6454 struct nfs4_getdevicelist_res res = {
6457 struct rpc_message msg = {
6458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6464 dprintk("--> %s\n", __func__);
6465 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6467 dprintk("<-- %s status=%d\n", __func__, status);
6471 int nfs4_proc_getdevicelist(struct nfs_server *server,
6472 const struct nfs_fh *fh,
6473 struct pnfs_devicelist *devlist)
6475 struct nfs4_exception exception = { };
6479 err = nfs4_handle_exception(server,
6480 _nfs4_getdevicelist(server, fh, devlist),
6482 } while (exception.retry);
6484 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6485 err, devlist->num_devs);
6489 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6492 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6494 struct nfs4_getdeviceinfo_args args = {
6497 struct nfs4_getdeviceinfo_res res = {
6500 struct rpc_message msg = {
6501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6507 dprintk("--> %s\n", __func__);
6508 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6509 dprintk("<-- %s status=%d\n", __func__, status);
6514 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6516 struct nfs4_exception exception = { };
6520 err = nfs4_handle_exception(server,
6521 _nfs4_proc_getdeviceinfo(server, pdev),
6523 } while (exception.retry);
6526 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6528 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6530 struct nfs4_layoutcommit_data *data = calldata;
6531 struct nfs_server *server = NFS_SERVER(data->args.inode);
6532 struct nfs4_session *session = nfs4_get_session(server);
6534 nfs41_setup_sequence(session,
6535 &data->args.seq_args,
6541 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6543 struct nfs4_layoutcommit_data *data = calldata;
6544 struct nfs_server *server = NFS_SERVER(data->args.inode);
6546 if (!nfs41_sequence_done(task, &data->res.seq_res))
6549 switch (task->tk_status) { /* Just ignore these failures */
6550 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6551 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6552 case -NFS4ERR_BADLAYOUT: /* no layout */
6553 case -NFS4ERR_GRACE: /* loca_recalim always false */
6554 task->tk_status = 0;
6557 nfs_post_op_update_inode_force_wcc(data->args.inode,
6561 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6562 rpc_restart_call_prepare(task);
6568 static void nfs4_layoutcommit_release(void *calldata)
6570 struct nfs4_layoutcommit_data *data = calldata;
6572 pnfs_cleanup_layoutcommit(data);
6573 put_rpccred(data->cred);
6577 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6578 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6579 .rpc_call_done = nfs4_layoutcommit_done,
6580 .rpc_release = nfs4_layoutcommit_release,
6584 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6586 struct rpc_message msg = {
6587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6588 .rpc_argp = &data->args,
6589 .rpc_resp = &data->res,
6590 .rpc_cred = data->cred,
6592 struct rpc_task_setup task_setup_data = {
6593 .task = &data->task,
6594 .rpc_client = NFS_CLIENT(data->args.inode),
6595 .rpc_message = &msg,
6596 .callback_ops = &nfs4_layoutcommit_ops,
6597 .callback_data = data,
6598 .flags = RPC_TASK_ASYNC,
6600 struct rpc_task *task;
6603 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6604 "lbw: %llu inode %lu\n",
6605 data->task.tk_pid, sync,
6606 data->args.lastbytewritten,
6607 data->args.inode->i_ino);
6609 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6610 task = rpc_run_task(&task_setup_data);
6612 return PTR_ERR(task);
6615 status = nfs4_wait_for_completion_rpc_task(task);
6618 status = task->tk_status;
6620 dprintk("%s: status %d\n", __func__, status);
6626 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6627 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6629 struct nfs41_secinfo_no_name_args args = {
6630 .style = SECINFO_STYLE_CURRENT_FH,
6632 struct nfs4_secinfo_res res = {
6635 struct rpc_message msg = {
6636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6640 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6644 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6645 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6647 struct nfs4_exception exception = { };
6650 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6653 case -NFS4ERR_WRONGSEC:
6654 case -NFS4ERR_NOTSUPP:
6657 err = nfs4_handle_exception(server, err, &exception);
6659 } while (exception.retry);
6665 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6666 struct nfs_fsinfo *info)
6670 rpc_authflavor_t flavor;
6671 struct nfs4_secinfo_flavors *flavors;
6673 page = alloc_page(GFP_KERNEL);
6679 flavors = page_address(page);
6680 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6683 * Fall back on "guess and check" method if
6684 * the server doesn't support SECINFO_NO_NAME
6686 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6687 err = nfs4_find_root_sec(server, fhandle, info);
6693 flavor = nfs_find_best_sec(flavors);
6695 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6705 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6708 struct nfs41_test_stateid_args args = {
6711 struct nfs41_test_stateid_res res;
6712 struct rpc_message msg = {
6713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6718 dprintk("NFS call test_stateid %p\n", stateid);
6719 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6720 nfs4_set_sequence_privileged(&args.seq_args);
6721 status = nfs4_call_sync_sequence(server->client, server, &msg,
6722 &args.seq_args, &res.seq_res);
6723 if (status != NFS_OK) {
6724 dprintk("NFS reply test_stateid: failed, %d\n", status);
6727 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6732 * nfs41_test_stateid - perform a TEST_STATEID operation
6734 * @server: server / transport on which to perform the operation
6735 * @stateid: state ID to test
6737 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6738 * Otherwise a negative NFS4ERR value is returned if the operation
6739 * failed or the state ID is not currently valid.
6741 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6743 struct nfs4_exception exception = { };
6746 err = _nfs41_test_stateid(server, stateid);
6747 if (err != -NFS4ERR_DELAY)
6749 nfs4_handle_exception(server, err, &exception);
6750 } while (exception.retry);
6754 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6756 struct nfs41_free_stateid_args args = {
6759 struct nfs41_free_stateid_res res;
6760 struct rpc_message msg = {
6761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6767 dprintk("NFS call free_stateid %p\n", stateid);
6768 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6769 nfs4_set_sequence_privileged(&args.seq_args);
6770 status = nfs4_call_sync_sequence(server->client, server, &msg,
6771 &args.seq_args, &res.seq_res);
6772 dprintk("NFS reply free_stateid: %d\n", status);
6777 * nfs41_free_stateid - perform a FREE_STATEID operation
6779 * @server: server / transport on which to perform the operation
6780 * @stateid: state ID to release
6782 * Returns NFS_OK if the server freed "stateid". Otherwise a
6783 * negative NFS4ERR value is returned.
6785 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6787 struct nfs4_exception exception = { };
6790 err = _nfs4_free_stateid(server, stateid);
6791 if (err != -NFS4ERR_DELAY)
6793 nfs4_handle_exception(server, err, &exception);
6794 } while (exception.retry);
6798 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6799 const nfs4_stateid *s2)
6801 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6804 if (s1->seqid == s2->seqid)
6806 if (s1->seqid == 0 || s2->seqid == 0)
6812 #endif /* CONFIG_NFS_V4_1 */
6814 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6815 const nfs4_stateid *s2)
6817 return nfs4_stateid_match(s1, s2);
6821 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6822 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6823 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6824 .recover_open = nfs4_open_reclaim,
6825 .recover_lock = nfs4_lock_reclaim,
6826 .establish_clid = nfs4_init_clientid,
6827 .get_clid_cred = nfs4_get_setclientid_cred,
6828 .detect_trunking = nfs40_discover_server_trunking,
6831 #if defined(CONFIG_NFS_V4_1)
6832 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6833 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6834 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6835 .recover_open = nfs4_open_reclaim,
6836 .recover_lock = nfs4_lock_reclaim,
6837 .establish_clid = nfs41_init_clientid,
6838 .get_clid_cred = nfs4_get_exchange_id_cred,
6839 .reclaim_complete = nfs41_proc_reclaim_complete,
6840 .detect_trunking = nfs41_discover_server_trunking,
6842 #endif /* CONFIG_NFS_V4_1 */
6844 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6845 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6846 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6847 .recover_open = nfs4_open_expired,
6848 .recover_lock = nfs4_lock_expired,
6849 .establish_clid = nfs4_init_clientid,
6850 .get_clid_cred = nfs4_get_setclientid_cred,
6853 #if defined(CONFIG_NFS_V4_1)
6854 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6855 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6856 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6857 .recover_open = nfs41_open_expired,
6858 .recover_lock = nfs41_lock_expired,
6859 .establish_clid = nfs41_init_clientid,
6860 .get_clid_cred = nfs4_get_exchange_id_cred,
6862 #endif /* CONFIG_NFS_V4_1 */
6864 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6865 .sched_state_renewal = nfs4_proc_async_renew,
6866 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6867 .renew_lease = nfs4_proc_renew,
6870 #if defined(CONFIG_NFS_V4_1)
6871 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6872 .sched_state_renewal = nfs41_proc_async_sequence,
6873 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6874 .renew_lease = nfs4_proc_sequence,
6878 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6880 .init_caps = NFS_CAP_READDIRPLUS
6881 | NFS_CAP_ATOMIC_OPEN
6882 | NFS_CAP_CHANGE_ATTR
6883 | NFS_CAP_POSIX_LOCK,
6884 .call_sync = _nfs4_call_sync,
6885 .match_stateid = nfs4_match_stateid,
6886 .find_root_sec = nfs4_find_root_sec,
6887 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6888 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6889 .state_renewal_ops = &nfs40_state_renewal_ops,
6892 #if defined(CONFIG_NFS_V4_1)
6893 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6895 .init_caps = NFS_CAP_READDIRPLUS
6896 | NFS_CAP_ATOMIC_OPEN
6897 | NFS_CAP_CHANGE_ATTR
6898 | NFS_CAP_POSIX_LOCK
6899 | NFS_CAP_STATEID_NFSV41
6900 | NFS_CAP_ATOMIC_OPEN_V1,
6901 .call_sync = nfs4_call_sync_sequence,
6902 .match_stateid = nfs41_match_stateid,
6903 .find_root_sec = nfs41_find_root_sec,
6904 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6905 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6906 .state_renewal_ops = &nfs41_state_renewal_ops,
6910 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6911 [0] = &nfs_v4_0_minor_ops,
6912 #if defined(CONFIG_NFS_V4_1)
6913 [1] = &nfs_v4_1_minor_ops,
6917 const struct inode_operations nfs4_dir_inode_operations = {
6918 .create = nfs_create,
6919 .lookup = nfs_lookup,
6920 .atomic_open = nfs_atomic_open,
6922 .unlink = nfs_unlink,
6923 .symlink = nfs_symlink,
6927 .rename = nfs_rename,
6928 .permission = nfs_permission,
6929 .getattr = nfs_getattr,
6930 .setattr = nfs_setattr,
6931 .getxattr = generic_getxattr,
6932 .setxattr = generic_setxattr,
6933 .listxattr = generic_listxattr,
6934 .removexattr = generic_removexattr,
6937 static const struct inode_operations nfs4_file_inode_operations = {
6938 .permission = nfs_permission,
6939 .getattr = nfs_getattr,
6940 .setattr = nfs_setattr,
6941 .getxattr = generic_getxattr,
6942 .setxattr = generic_setxattr,
6943 .listxattr = generic_listxattr,
6944 .removexattr = generic_removexattr,
6947 const struct nfs_rpc_ops nfs_v4_clientops = {
6948 .version = 4, /* protocol version */
6949 .dentry_ops = &nfs4_dentry_operations,
6950 .dir_inode_ops = &nfs4_dir_inode_operations,
6951 .file_inode_ops = &nfs4_file_inode_operations,
6952 .file_ops = &nfs4_file_operations,
6953 .getroot = nfs4_proc_get_root,
6954 .submount = nfs4_submount,
6955 .try_mount = nfs4_try_mount,
6956 .getattr = nfs4_proc_getattr,
6957 .setattr = nfs4_proc_setattr,
6958 .lookup = nfs4_proc_lookup,
6959 .access = nfs4_proc_access,
6960 .readlink = nfs4_proc_readlink,
6961 .create = nfs4_proc_create,
6962 .remove = nfs4_proc_remove,
6963 .unlink_setup = nfs4_proc_unlink_setup,
6964 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6965 .unlink_done = nfs4_proc_unlink_done,
6966 .rename = nfs4_proc_rename,
6967 .rename_setup = nfs4_proc_rename_setup,
6968 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6969 .rename_done = nfs4_proc_rename_done,
6970 .link = nfs4_proc_link,
6971 .symlink = nfs4_proc_symlink,
6972 .mkdir = nfs4_proc_mkdir,
6973 .rmdir = nfs4_proc_remove,
6974 .readdir = nfs4_proc_readdir,
6975 .mknod = nfs4_proc_mknod,
6976 .statfs = nfs4_proc_statfs,
6977 .fsinfo = nfs4_proc_fsinfo,
6978 .pathconf = nfs4_proc_pathconf,
6979 .set_capabilities = nfs4_server_capabilities,
6980 .decode_dirent = nfs4_decode_dirent,
6981 .read_setup = nfs4_proc_read_setup,
6982 .read_pageio_init = pnfs_pageio_init_read,
6983 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6984 .read_done = nfs4_read_done,
6985 .write_setup = nfs4_proc_write_setup,
6986 .write_pageio_init = pnfs_pageio_init_write,
6987 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6988 .write_done = nfs4_write_done,
6989 .commit_setup = nfs4_proc_commit_setup,
6990 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6991 .commit_done = nfs4_commit_done,
6992 .lock = nfs4_proc_lock,
6993 .clear_acl_cache = nfs4_zap_acl_attr,
6994 .close_context = nfs4_close_context,
6995 .open_context = nfs4_atomic_open,
6996 .have_delegation = nfs4_have_delegation,
6997 .return_delegation = nfs4_inode_return_delegation,
6998 .alloc_client = nfs4_alloc_client,
6999 .init_client = nfs4_init_client,
7000 .free_client = nfs4_free_client,
7001 .create_server = nfs4_create_server,
7002 .clone_server = nfs_clone_server,
7005 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7006 .prefix = XATTR_NAME_NFSV4_ACL,
7007 .list = nfs4_xattr_list_nfs4_acl,
7008 .get = nfs4_xattr_get_nfs4_acl,
7009 .set = nfs4_xattr_set_nfs4_acl,
7012 const struct xattr_handler *nfs4_xattr_handlers[] = {
7013 &nfs4_xattr_nfs4_acl_handler,