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:
97 case -NFS4ERR_WRONGSEC:
99 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
102 case -NFS4ERR_SHARE_DENIED:
104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
106 case -NFS4ERR_ACCESS:
109 dprintk("%s could not handle NFSv4 error %d\n",
117 * This is our standard bitmap for GETATTR requests.
119 const u32 nfs4_fattr_bitmap[3] = {
121 | FATTR4_WORD0_CHANGE
124 | FATTR4_WORD0_FILEID,
126 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
136 static const u32 nfs4_pnfs_open_bitmap[3] = {
138 | FATTR4_WORD0_CHANGE
141 | FATTR4_WORD0_FILEID,
143 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
154 static const u32 nfs4_open_noattr_bitmap[3] = {
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
160 const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
169 const u32 nfs4_pathconf_bitmap[2] = {
171 | FATTR4_WORD0_MAXNAME,
175 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
179 FATTR4_WORD1_TIME_DELTA
180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
184 const u32 nfs4_fs_locations_bitmap[2] = {
186 | FATTR4_WORD0_CHANGE
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
192 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
203 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
204 struct nfs4_readdir_arg *readdir)
209 readdir->cookie = cookie;
210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
226 start = p = kmap_atomic(*readdir->pages);
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
254 kunmap_atomic(start);
257 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
267 freezable_schedule_timeout_killable(*timeout);
268 if (fatal_signal_pending(current))
274 /* This is the error handling routine for processes that are allowed
277 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
279 struct nfs_client *clp = server->nfs_client;
280 struct nfs4_state *state = exception->state;
281 struct inode *inode = exception->inode;
284 exception->retry = 0;
288 case -NFS4ERR_OPENMODE:
289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
290 nfs4_inode_return_delegation(inode);
291 exception->retry = 1;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
298 case -NFS4ERR_DELEG_REVOKED:
299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
303 nfs_remove_bad_delegation(state->inode);
304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
306 case -NFS4ERR_EXPIRED:
308 nfs4_schedule_stateid_recovery(server, state);
309 case -NFS4ERR_STALE_STATEID:
310 case -NFS4ERR_STALE_CLIENTID:
311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
313 #if defined(CONFIG_NFS_V4_1)
314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
324 goto wait_on_recovery;
325 #endif /* defined(CONFIG_NFS_V4_1) */
326 case -NFS4ERR_FILE_OPEN:
327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
336 ret = nfs4_delay(server->client, &exception->timeout);
339 case -NFS4ERR_RETRY_UNCACHED_REP:
340 case -NFS4ERR_OLD_STATEID:
341 exception->retry = 1;
343 case -NFS4ERR_BADOWNER:
344 /* The following works around a Linux server bug! */
345 case -NFS4ERR_BADNAME:
346 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
347 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
348 exception->retry = 1;
349 printk(KERN_WARNING "NFS: v4 server %s "
350 "does not accept raw "
352 "Reenabling the idmapper.\n",
353 server->nfs_client->cl_hostname);
356 /* We failed to handle the error */
357 return nfs4_map_errors(ret);
359 ret = nfs4_wait_clnt_recover(clp);
361 exception->retry = 1;
366 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
368 spin_lock(&clp->cl_lock);
369 if (time_before(clp->cl_last_renewal,timestamp))
370 clp->cl_last_renewal = timestamp;
371 spin_unlock(&clp->cl_lock);
374 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376 do_renew_lease(server->nfs_client, timestamp);
379 #if defined(CONFIG_NFS_V4_1)
381 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
383 struct nfs4_session *session;
384 struct nfs4_slot_table *tbl;
385 bool send_new_highest_used_slotid = false;
388 /* just wake up the next guy waiting since
389 * we may have not consumed a slot after all */
390 dprintk("%s: No slot\n", __func__);
393 tbl = res->sr_slot->table;
394 session = tbl->session;
396 spin_lock(&tbl->slot_tbl_lock);
397 /* Be nice to the server: try to ensure that the last transmitted
398 * value for highest_user_slotid <= target_highest_slotid
400 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
401 send_new_highest_used_slotid = true;
403 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
404 send_new_highest_used_slotid = false;
407 nfs4_free_slot(tbl, res->sr_slot);
409 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
410 send_new_highest_used_slotid = false;
412 spin_unlock(&tbl->slot_tbl_lock);
414 if (send_new_highest_used_slotid)
415 nfs41_server_notify_highest_slotid_update(session->clp);
418 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
420 struct nfs4_session *session;
421 struct nfs4_slot *slot;
422 struct nfs_client *clp;
423 bool interrupted = false;
426 /* don't increment the sequence number if the task wasn't sent */
427 if (!RPC_WAS_SENT(task))
431 session = slot->table->session;
433 if (slot->interrupted) {
434 slot->interrupted = 0;
438 /* Check the SEQUENCE operation status */
439 switch (res->sr_status) {
441 /* Update the slot's sequence and clientid lease timer */
444 do_renew_lease(clp, res->sr_timestamp);
445 /* Check sequence flags */
446 if (res->sr_status_flags != 0)
447 nfs4_schedule_lease_recovery(clp);
448 nfs41_update_target_slotid(slot->table, slot, res);
452 * sr_status remains 1 if an RPC level error occurred.
453 * The server may or may not have processed the sequence
455 * Mark the slot as having hosted an interrupted RPC call.
457 slot->interrupted = 1;
460 /* The server detected a resend of the RPC call and
461 * returned NFS4ERR_DELAY as per Section 2.10.6.2
464 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
469 case -NFS4ERR_BADSLOT:
471 * The slot id we used was probably retired. Try again
472 * using a different slot id.
475 case -NFS4ERR_SEQ_MISORDERED:
477 * Was the last operation on this sequence interrupted?
478 * If so, retry after bumping the sequence number.
485 * Could this slot have been previously retired?
486 * If so, then the server may be expecting seq_nr = 1!
488 if (slot->seq_nr != 1) {
493 case -NFS4ERR_SEQ_FALSE_RETRY:
497 /* Just update the slot sequence no. */
501 /* The session may be reset by one of the error handlers. */
502 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
503 nfs41_sequence_free_slot(res);
506 if (rpc_restart_call_prepare(task)) {
512 if (!rpc_restart_call(task))
514 rpc_delay(task, NFS4_POLL_RETRY_MAX);
518 static int nfs4_sequence_done(struct rpc_task *task,
519 struct nfs4_sequence_res *res)
521 if (res->sr_slot == NULL)
523 return nfs41_sequence_done(task, res);
526 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
527 struct nfs4_sequence_res *res, int cache_reply)
529 args->sa_slot = NULL;
530 args->sa_cache_this = 0;
531 args->sa_privileged = 0;
533 args->sa_cache_this = 1;
537 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
539 args->sa_privileged = 1;
542 int nfs41_setup_sequence(struct nfs4_session *session,
543 struct nfs4_sequence_args *args,
544 struct nfs4_sequence_res *res,
545 struct rpc_task *task)
547 struct nfs4_slot *slot;
548 struct nfs4_slot_table *tbl;
550 dprintk("--> %s\n", __func__);
551 /* slot already allocated? */
552 if (res->sr_slot != NULL)
555 tbl = &session->fc_slot_table;
557 task->tk_timeout = 0;
559 spin_lock(&tbl->slot_tbl_lock);
560 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
561 !args->sa_privileged) {
562 /* The state manager will wait until the slot table is empty */
563 dprintk("%s session is draining\n", __func__);
567 slot = nfs4_alloc_slot(tbl);
569 /* If out of memory, try again in 1/4 second */
570 if (slot == ERR_PTR(-ENOMEM))
571 task->tk_timeout = HZ >> 2;
572 dprintk("<-- %s: no free slots\n", __func__);
575 spin_unlock(&tbl->slot_tbl_lock);
577 args->sa_slot = slot;
579 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
580 slot->slot_nr, slot->seq_nr);
583 res->sr_timestamp = jiffies;
584 res->sr_status_flags = 0;
586 * sr_status is only set in decode_sequence, and so will remain
587 * set to 1 if an rpc level failure occurs.
591 rpc_call_start(task);
594 /* Privileged tasks are queued with top priority */
595 if (args->sa_privileged)
596 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
597 NULL, RPC_PRIORITY_PRIVILEGED);
599 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
600 spin_unlock(&tbl->slot_tbl_lock);
603 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
605 int nfs4_setup_sequence(const struct nfs_server *server,
606 struct nfs4_sequence_args *args,
607 struct nfs4_sequence_res *res,
608 struct rpc_task *task)
610 struct nfs4_session *session = nfs4_get_session(server);
613 if (session == NULL) {
614 rpc_call_start(task);
618 dprintk("--> %s clp %p session %p sr_slot %d\n",
619 __func__, session->clp, session, res->sr_slot ?
620 res->sr_slot->slot_nr : -1);
622 ret = nfs41_setup_sequence(session, args, res, task);
624 dprintk("<-- %s status=%d\n", __func__, ret);
628 struct nfs41_call_sync_data {
629 const struct nfs_server *seq_server;
630 struct nfs4_sequence_args *seq_args;
631 struct nfs4_sequence_res *seq_res;
634 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
636 struct nfs41_call_sync_data *data = calldata;
637 struct nfs4_session *session = nfs4_get_session(data->seq_server);
639 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
641 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
644 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
646 struct nfs41_call_sync_data *data = calldata;
648 nfs41_sequence_done(task, data->seq_res);
651 static const struct rpc_call_ops nfs41_call_sync_ops = {
652 .rpc_call_prepare = nfs41_call_sync_prepare,
653 .rpc_call_done = nfs41_call_sync_done,
656 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
657 struct nfs_server *server,
658 struct rpc_message *msg,
659 struct nfs4_sequence_args *args,
660 struct nfs4_sequence_res *res)
663 struct rpc_task *task;
664 struct nfs41_call_sync_data data = {
665 .seq_server = server,
669 struct rpc_task_setup task_setup = {
672 .callback_ops = &nfs41_call_sync_ops,
673 .callback_data = &data
676 task = rpc_run_task(&task_setup);
680 ret = task->tk_status;
688 void nfs41_init_sequence(struct nfs4_sequence_args *args,
689 struct nfs4_sequence_res *res, int cache_reply)
693 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
698 static int nfs4_sequence_done(struct rpc_task *task,
699 struct nfs4_sequence_res *res)
703 #endif /* CONFIG_NFS_V4_1 */
706 int _nfs4_call_sync(struct rpc_clnt *clnt,
707 struct nfs_server *server,
708 struct rpc_message *msg,
709 struct nfs4_sequence_args *args,
710 struct nfs4_sequence_res *res)
712 return rpc_call_sync(clnt, msg, 0);
716 int nfs4_call_sync(struct rpc_clnt *clnt,
717 struct nfs_server *server,
718 struct rpc_message *msg,
719 struct nfs4_sequence_args *args,
720 struct nfs4_sequence_res *res,
723 nfs41_init_sequence(args, res, cache_reply);
724 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
728 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
730 struct nfs_inode *nfsi = NFS_I(dir);
732 spin_lock(&dir->i_lock);
733 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
734 if (!cinfo->atomic || cinfo->before != dir->i_version)
735 nfs_force_lookup_revalidate(dir);
736 dir->i_version = cinfo->after;
737 nfs_fscache_invalidate(dir);
738 spin_unlock(&dir->i_lock);
741 struct nfs4_opendata {
743 struct nfs_openargs o_arg;
744 struct nfs_openres o_res;
745 struct nfs_open_confirmargs c_arg;
746 struct nfs_open_confirmres c_res;
747 struct nfs4_string owner_name;
748 struct nfs4_string group_name;
749 struct nfs_fattr f_attr;
751 struct dentry *dentry;
752 struct nfs4_state_owner *owner;
753 struct nfs4_state *state;
755 unsigned long timestamp;
756 unsigned int rpc_done : 1;
762 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
764 p->o_res.f_attr = &p->f_attr;
765 p->o_res.seqid = p->o_arg.seqid;
766 p->c_res.seqid = p->c_arg.seqid;
767 p->o_res.server = p->o_arg.server;
768 p->o_res.access_request = p->o_arg.access;
769 nfs_fattr_init(&p->f_attr);
770 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
773 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
774 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
775 const struct iattr *attrs,
778 struct dentry *parent = dget_parent(dentry);
779 struct inode *dir = parent->d_inode;
780 struct nfs_server *server = NFS_SERVER(dir);
781 struct nfs4_opendata *p;
783 p = kzalloc(sizeof(*p), gfp_mask);
786 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
787 if (p->o_arg.seqid == NULL)
789 nfs_sb_active(dentry->d_sb);
790 p->dentry = dget(dentry);
793 atomic_inc(&sp->so_count);
794 p->o_arg.fh = NFS_FH(dir);
795 p->o_arg.open_flags = flags;
796 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
797 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
798 * will return permission denied for all bits until close */
799 if (!(flags & O_EXCL)) {
800 /* ask server to check for all possible rights as results
802 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
803 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
805 p->o_arg.clientid = server->nfs_client->cl_clientid;
806 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
807 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
808 p->o_arg.name = &dentry->d_name;
809 p->o_arg.server = server;
810 p->o_arg.bitmask = server->attr_bitmask;
811 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
812 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
813 if (attrs != NULL && attrs->ia_valid != 0) {
816 p->o_arg.u.attrs = &p->attrs;
817 memcpy(&p->attrs, attrs, sizeof(p->attrs));
820 verf[1] = current->pid;
821 memcpy(p->o_arg.u.verifier.data, verf,
822 sizeof(p->o_arg.u.verifier.data));
824 p->c_arg.fh = &p->o_res.fh;
825 p->c_arg.stateid = &p->o_res.stateid;
826 p->c_arg.seqid = p->o_arg.seqid;
827 nfs4_init_opendata_res(p);
837 static void nfs4_opendata_free(struct kref *kref)
839 struct nfs4_opendata *p = container_of(kref,
840 struct nfs4_opendata, kref);
841 struct super_block *sb = p->dentry->d_sb;
843 nfs_free_seqid(p->o_arg.seqid);
844 if (p->state != NULL)
845 nfs4_put_open_state(p->state);
846 nfs4_put_state_owner(p->owner);
850 nfs_fattr_free_names(&p->f_attr);
854 static void nfs4_opendata_put(struct nfs4_opendata *p)
857 kref_put(&p->kref, nfs4_opendata_free);
860 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
864 ret = rpc_wait_for_completion_task(task);
868 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
872 if (open_mode & (O_EXCL|O_TRUNC))
874 switch (mode & (FMODE_READ|FMODE_WRITE)) {
876 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
877 && state->n_rdonly != 0;
880 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
881 && state->n_wronly != 0;
883 case FMODE_READ|FMODE_WRITE:
884 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
885 && state->n_rdwr != 0;
891 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
893 if (delegation == NULL)
895 if ((delegation->type & fmode) != fmode)
897 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
899 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
901 nfs_mark_delegation_referenced(delegation);
905 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
914 case FMODE_READ|FMODE_WRITE:
917 nfs4_state_set_mode_locked(state, state->state | fmode);
920 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
923 nfs4_stateid_copy(&state->stateid, stateid);
924 nfs4_stateid_copy(&state->open_stateid, stateid);
927 set_bit(NFS_O_RDONLY_STATE, &state->flags);
930 set_bit(NFS_O_WRONLY_STATE, &state->flags);
932 case FMODE_READ|FMODE_WRITE:
933 set_bit(NFS_O_RDWR_STATE, &state->flags);
937 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
939 write_seqlock(&state->seqlock);
940 nfs_set_open_stateid_locked(state, stateid, fmode);
941 write_sequnlock(&state->seqlock);
944 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
947 * Protect the call to nfs4_state_set_mode_locked and
948 * serialise the stateid update
950 write_seqlock(&state->seqlock);
951 if (deleg_stateid != NULL) {
952 nfs4_stateid_copy(&state->stateid, deleg_stateid);
953 set_bit(NFS_DELEGATED_STATE, &state->flags);
955 if (open_stateid != NULL)
956 nfs_set_open_stateid_locked(state, open_stateid, fmode);
957 write_sequnlock(&state->seqlock);
958 spin_lock(&state->owner->so_lock);
959 update_open_stateflags(state, fmode);
960 spin_unlock(&state->owner->so_lock);
963 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
965 struct nfs_inode *nfsi = NFS_I(state->inode);
966 struct nfs_delegation *deleg_cur;
969 fmode &= (FMODE_READ|FMODE_WRITE);
972 deleg_cur = rcu_dereference(nfsi->delegation);
973 if (deleg_cur == NULL)
976 spin_lock(&deleg_cur->lock);
977 if (nfsi->delegation != deleg_cur ||
978 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
979 (deleg_cur->type & fmode) != fmode)
980 goto no_delegation_unlock;
982 if (delegation == NULL)
983 delegation = &deleg_cur->stateid;
984 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
985 goto no_delegation_unlock;
987 nfs_mark_delegation_referenced(deleg_cur);
988 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
990 no_delegation_unlock:
991 spin_unlock(&deleg_cur->lock);
995 if (!ret && open_stateid != NULL) {
996 __update_open_stateid(state, open_stateid, NULL, fmode);
1004 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1006 struct nfs_delegation *delegation;
1009 delegation = rcu_dereference(NFS_I(inode)->delegation);
1010 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1015 nfs4_inode_return_delegation(inode);
1018 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1020 struct nfs4_state *state = opendata->state;
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *delegation;
1023 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1024 fmode_t fmode = opendata->o_arg.fmode;
1025 nfs4_stateid stateid;
1029 if (can_open_cached(state, fmode, open_mode)) {
1030 spin_lock(&state->owner->so_lock);
1031 if (can_open_cached(state, fmode, open_mode)) {
1032 update_open_stateflags(state, fmode);
1033 spin_unlock(&state->owner->so_lock);
1034 goto out_return_state;
1036 spin_unlock(&state->owner->so_lock);
1039 delegation = rcu_dereference(nfsi->delegation);
1040 if (!can_open_delegated(delegation, fmode)) {
1044 /* Save the delegation */
1045 nfs4_stateid_copy(&stateid, &delegation->stateid);
1047 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1052 /* Try to update the stateid using the delegation */
1053 if (update_open_stateid(state, NULL, &stateid, fmode))
1054 goto out_return_state;
1057 return ERR_PTR(ret);
1059 atomic_inc(&state->count);
1064 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1066 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1067 struct nfs_delegation *delegation;
1068 int delegation_flags = 0;
1071 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1073 delegation_flags = delegation->flags;
1075 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1076 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1077 "returning a delegation for "
1078 "OPEN(CLAIM_DELEGATE_CUR)\n",
1080 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1081 nfs_inode_set_delegation(state->inode,
1082 data->owner->so_cred,
1085 nfs_inode_reclaim_delegation(state->inode,
1086 data->owner->so_cred,
1091 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1092 * and update the nfs4_state.
1094 static struct nfs4_state *
1095 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1097 struct inode *inode = data->state->inode;
1098 struct nfs4_state *state = data->state;
1101 if (!data->rpc_done) {
1102 ret = data->rpc_status;
1107 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1108 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1109 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1113 state = nfs4_get_open_state(inode, data->owner);
1117 ret = nfs_refresh_inode(inode, &data->f_attr);
1121 if (data->o_res.delegation_type != 0)
1122 nfs4_opendata_check_deleg(data, state);
1123 update_open_stateid(state, &data->o_res.stateid, NULL,
1128 return ERR_PTR(ret);
1132 static struct nfs4_state *
1133 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1135 struct inode *inode;
1136 struct nfs4_state *state = NULL;
1139 if (!data->rpc_done) {
1140 state = nfs4_try_open_cached(data);
1145 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1147 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1148 ret = PTR_ERR(inode);
1152 state = nfs4_get_open_state(inode, data->owner);
1155 if (data->o_res.delegation_type != 0)
1156 nfs4_opendata_check_deleg(data, state);
1157 update_open_stateid(state, &data->o_res.stateid, NULL,
1165 return ERR_PTR(ret);
1168 static struct nfs4_state *
1169 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1171 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1172 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1173 return _nfs4_opendata_to_nfs4_state(data);
1176 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1178 struct nfs_inode *nfsi = NFS_I(state->inode);
1179 struct nfs_open_context *ctx;
1181 spin_lock(&state->inode->i_lock);
1182 list_for_each_entry(ctx, &nfsi->open_files, list) {
1183 if (ctx->state != state)
1185 get_nfs_open_context(ctx);
1186 spin_unlock(&state->inode->i_lock);
1189 spin_unlock(&state->inode->i_lock);
1190 return ERR_PTR(-ENOENT);
1193 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1195 struct nfs4_opendata *opendata;
1197 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1198 if (opendata == NULL)
1199 return ERR_PTR(-ENOMEM);
1200 opendata->state = state;
1201 atomic_inc(&state->count);
1205 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1207 struct nfs4_state *newstate;
1210 opendata->o_arg.open_flags = 0;
1211 opendata->o_arg.fmode = fmode;
1212 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1213 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1214 nfs4_init_opendata_res(opendata);
1215 ret = _nfs4_recover_proc_open(opendata);
1218 newstate = nfs4_opendata_to_nfs4_state(opendata);
1219 if (IS_ERR(newstate))
1220 return PTR_ERR(newstate);
1221 nfs4_close_state(newstate, fmode);
1226 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1228 struct nfs4_state *newstate;
1231 /* memory barrier prior to reading state->n_* */
1232 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1234 if (state->n_rdwr != 0) {
1235 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1236 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1239 if (newstate != state)
1242 if (state->n_wronly != 0) {
1243 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1244 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1247 if (newstate != state)
1250 if (state->n_rdonly != 0) {
1251 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1252 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1255 if (newstate != state)
1259 * We may have performed cached opens for all three recoveries.
1260 * Check if we need to update the current stateid.
1262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1263 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1264 write_seqlock(&state->seqlock);
1265 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1266 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1267 write_sequnlock(&state->seqlock);
1274 * reclaim state on the server after a reboot.
1276 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1278 struct nfs_delegation *delegation;
1279 struct nfs4_opendata *opendata;
1280 fmode_t delegation_type = 0;
1283 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1284 if (IS_ERR(opendata))
1285 return PTR_ERR(opendata);
1286 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1287 opendata->o_arg.fh = NFS_FH(state->inode);
1289 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1290 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1291 delegation_type = delegation->type;
1293 opendata->o_arg.u.delegation_type = delegation_type;
1294 status = nfs4_open_recover(opendata, state);
1295 nfs4_opendata_put(opendata);
1299 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1301 struct nfs_server *server = NFS_SERVER(state->inode);
1302 struct nfs4_exception exception = { };
1305 err = _nfs4_do_open_reclaim(ctx, state);
1306 if (err != -NFS4ERR_DELAY)
1308 nfs4_handle_exception(server, err, &exception);
1309 } while (exception.retry);
1313 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1315 struct nfs_open_context *ctx;
1318 ctx = nfs4_state_find_open_context(state);
1320 return PTR_ERR(ctx);
1321 ret = nfs4_do_open_reclaim(ctx, state);
1322 put_nfs_open_context(ctx);
1326 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1328 struct nfs4_opendata *opendata;
1331 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1332 if (IS_ERR(opendata))
1333 return PTR_ERR(opendata);
1334 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1335 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1336 ret = nfs4_open_recover(opendata, state);
1337 nfs4_opendata_put(opendata);
1341 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1343 struct nfs4_exception exception = { };
1344 struct nfs_server *server = NFS_SERVER(state->inode);
1347 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1353 case -NFS4ERR_BADSESSION:
1354 case -NFS4ERR_BADSLOT:
1355 case -NFS4ERR_BAD_HIGH_SLOT:
1356 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1357 case -NFS4ERR_DEADSESSION:
1358 set_bit(NFS_DELEGATED_STATE, &state->flags);
1359 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1362 case -NFS4ERR_STALE_CLIENTID:
1363 case -NFS4ERR_STALE_STATEID:
1364 set_bit(NFS_DELEGATED_STATE, &state->flags);
1365 case -NFS4ERR_EXPIRED:
1366 /* Don't recall a delegation if it was lost */
1367 nfs4_schedule_lease_recovery(server->nfs_client);
1370 case -NFS4ERR_DELEG_REVOKED:
1371 case -NFS4ERR_ADMIN_REVOKED:
1372 case -NFS4ERR_BAD_STATEID:
1373 nfs_inode_find_state_and_recover(state->inode,
1375 nfs4_schedule_stateid_recovery(server, state);
1380 set_bit(NFS_DELEGATED_STATE, &state->flags);
1381 err = nfs4_handle_exception(server, err, &exception);
1382 } while (exception.retry);
1387 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1389 struct nfs4_opendata *data = calldata;
1391 data->rpc_status = task->tk_status;
1392 if (data->rpc_status == 0) {
1393 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1394 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1395 renew_lease(data->o_res.server, data->timestamp);
1400 static void nfs4_open_confirm_release(void *calldata)
1402 struct nfs4_opendata *data = calldata;
1403 struct nfs4_state *state = NULL;
1405 /* If this request hasn't been cancelled, do nothing */
1406 if (data->cancelled == 0)
1408 /* In case of error, no cleanup! */
1409 if (!data->rpc_done)
1411 state = nfs4_opendata_to_nfs4_state(data);
1413 nfs4_close_state(state, data->o_arg.fmode);
1415 nfs4_opendata_put(data);
1418 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1419 .rpc_call_done = nfs4_open_confirm_done,
1420 .rpc_release = nfs4_open_confirm_release,
1424 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1426 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1428 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1429 struct rpc_task *task;
1430 struct rpc_message msg = {
1431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1432 .rpc_argp = &data->c_arg,
1433 .rpc_resp = &data->c_res,
1434 .rpc_cred = data->owner->so_cred,
1436 struct rpc_task_setup task_setup_data = {
1437 .rpc_client = server->client,
1438 .rpc_message = &msg,
1439 .callback_ops = &nfs4_open_confirm_ops,
1440 .callback_data = data,
1441 .workqueue = nfsiod_workqueue,
1442 .flags = RPC_TASK_ASYNC,
1446 kref_get(&data->kref);
1448 data->rpc_status = 0;
1449 data->timestamp = jiffies;
1450 task = rpc_run_task(&task_setup_data);
1452 return PTR_ERR(task);
1453 status = nfs4_wait_for_completion_rpc_task(task);
1455 data->cancelled = 1;
1458 status = data->rpc_status;
1463 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1465 struct nfs4_opendata *data = calldata;
1466 struct nfs4_state_owner *sp = data->owner;
1468 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1471 * Check if we still need to send an OPEN call, or if we can use
1472 * a delegation instead.
1474 if (data->state != NULL) {
1475 struct nfs_delegation *delegation;
1477 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1480 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1481 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1482 can_open_delegated(delegation, data->o_arg.fmode))
1483 goto unlock_no_action;
1486 /* Update client id. */
1487 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1488 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1489 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1490 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1491 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1493 data->timestamp = jiffies;
1494 if (nfs4_setup_sequence(data->o_arg.server,
1495 &data->o_arg.seq_args,
1496 &data->o_res.seq_res,
1498 nfs_release_seqid(data->o_arg.seqid);
1503 task->tk_action = NULL;
1504 nfs4_sequence_done(task, &data->o_res.seq_res);
1507 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1509 struct nfs4_opendata *data = calldata;
1511 data->rpc_status = task->tk_status;
1513 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1516 if (task->tk_status == 0) {
1517 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1518 switch (data->o_res.f_attr->mode & S_IFMT) {
1522 data->rpc_status = -ELOOP;
1525 data->rpc_status = -EISDIR;
1528 data->rpc_status = -ENOTDIR;
1531 renew_lease(data->o_res.server, data->timestamp);
1532 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1533 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1538 static void nfs4_open_release(void *calldata)
1540 struct nfs4_opendata *data = calldata;
1541 struct nfs4_state *state = NULL;
1543 /* If this request hasn't been cancelled, do nothing */
1544 if (data->cancelled == 0)
1546 /* In case of error, no cleanup! */
1547 if (data->rpc_status != 0 || !data->rpc_done)
1549 /* In case we need an open_confirm, no cleanup! */
1550 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1552 state = nfs4_opendata_to_nfs4_state(data);
1554 nfs4_close_state(state, data->o_arg.fmode);
1556 nfs4_opendata_put(data);
1559 static const struct rpc_call_ops nfs4_open_ops = {
1560 .rpc_call_prepare = nfs4_open_prepare,
1561 .rpc_call_done = nfs4_open_done,
1562 .rpc_release = nfs4_open_release,
1565 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1567 struct inode *dir = data->dir->d_inode;
1568 struct nfs_server *server = NFS_SERVER(dir);
1569 struct nfs_openargs *o_arg = &data->o_arg;
1570 struct nfs_openres *o_res = &data->o_res;
1571 struct rpc_task *task;
1572 struct rpc_message msg = {
1573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1576 .rpc_cred = data->owner->so_cred,
1578 struct rpc_task_setup task_setup_data = {
1579 .rpc_client = server->client,
1580 .rpc_message = &msg,
1581 .callback_ops = &nfs4_open_ops,
1582 .callback_data = data,
1583 .workqueue = nfsiod_workqueue,
1584 .flags = RPC_TASK_ASYNC,
1588 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1589 kref_get(&data->kref);
1591 data->rpc_status = 0;
1592 data->cancelled = 0;
1594 nfs4_set_sequence_privileged(&o_arg->seq_args);
1595 task = rpc_run_task(&task_setup_data);
1597 return PTR_ERR(task);
1598 status = nfs4_wait_for_completion_rpc_task(task);
1600 data->cancelled = 1;
1603 status = data->rpc_status;
1609 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1611 struct inode *dir = data->dir->d_inode;
1612 struct nfs_openres *o_res = &data->o_res;
1615 status = nfs4_run_open_task(data, 1);
1616 if (status != 0 || !data->rpc_done)
1619 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1621 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1622 status = _nfs4_proc_open_confirm(data);
1630 static int nfs4_opendata_access(struct rpc_cred *cred,
1631 struct nfs4_opendata *opendata,
1632 struct nfs4_state *state, fmode_t fmode,
1635 struct nfs_access_entry cache;
1638 /* access call failed or for some reason the server doesn't
1639 * support any access modes -- defer access call until later */
1640 if (opendata->o_res.access_supported == 0)
1644 /* don't check MAY_WRITE - a newly created file may not have
1645 * write mode bits, but POSIX allows the creating process to write.
1646 * use openflags to check for exec, because fmode won't
1647 * always have FMODE_EXEC set when file open for exec. */
1648 if (openflags & __FMODE_EXEC) {
1649 /* ONLY check for exec rights */
1651 } else if (fmode & FMODE_READ)
1655 cache.jiffies = jiffies;
1656 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1657 nfs_access_add_cache(state->inode, &cache);
1659 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1662 /* even though OPEN succeeded, access is denied. Close the file */
1663 nfs4_close_state(state, fmode);
1668 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1670 static int _nfs4_proc_open(struct nfs4_opendata *data)
1672 struct inode *dir = data->dir->d_inode;
1673 struct nfs_server *server = NFS_SERVER(dir);
1674 struct nfs_openargs *o_arg = &data->o_arg;
1675 struct nfs_openres *o_res = &data->o_res;
1678 status = nfs4_run_open_task(data, 0);
1679 if (!data->rpc_done)
1682 if (status == -NFS4ERR_BADNAME &&
1683 !(o_arg->open_flags & O_CREAT))
1688 nfs_fattr_map_and_free_names(server, &data->f_attr);
1690 if (o_arg->open_flags & O_CREAT)
1691 update_changeattr(dir, &o_res->cinfo);
1692 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1693 server->caps &= ~NFS_CAP_POSIX_LOCK;
1694 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1695 status = _nfs4_proc_open_confirm(data);
1699 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1700 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1704 static int nfs4_recover_expired_lease(struct nfs_server *server)
1706 return nfs4_client_recover_expired_lease(server->nfs_client);
1711 * reclaim state on the server after a network partition.
1712 * Assumes caller holds the appropriate lock
1714 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1716 struct nfs4_opendata *opendata;
1719 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1720 if (IS_ERR(opendata))
1721 return PTR_ERR(opendata);
1722 ret = nfs4_open_recover(opendata, state);
1724 d_drop(ctx->dentry);
1725 nfs4_opendata_put(opendata);
1729 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1731 struct nfs_server *server = NFS_SERVER(state->inode);
1732 struct nfs4_exception exception = { };
1736 err = _nfs4_open_expired(ctx, state);
1740 case -NFS4ERR_GRACE:
1741 case -NFS4ERR_DELAY:
1742 nfs4_handle_exception(server, err, &exception);
1745 } while (exception.retry);
1750 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1752 struct nfs_open_context *ctx;
1755 ctx = nfs4_state_find_open_context(state);
1757 return PTR_ERR(ctx);
1758 ret = nfs4_do_open_expired(ctx, state);
1759 put_nfs_open_context(ctx);
1763 #if defined(CONFIG_NFS_V4_1)
1764 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1766 struct nfs_server *server = NFS_SERVER(state->inode);
1767 nfs4_stateid *stateid = &state->stateid;
1770 /* If a state reset has been done, test_stateid is unneeded */
1771 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1774 status = nfs41_test_stateid(server, stateid);
1775 if (status != NFS_OK) {
1776 /* Free the stateid unless the server explicitly
1777 * informs us the stateid is unrecognized. */
1778 if (status != -NFS4ERR_BAD_STATEID)
1779 nfs41_free_stateid(server, stateid);
1780 nfs_remove_bad_delegation(state->inode);
1782 write_seqlock(&state->seqlock);
1783 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1784 write_sequnlock(&state->seqlock);
1785 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1790 * nfs41_check_open_stateid - possibly free an open stateid
1792 * @state: NFSv4 state for an inode
1794 * Returns NFS_OK if recovery for this stateid is now finished.
1795 * Otherwise a negative NFS4ERR value is returned.
1797 static int nfs41_check_open_stateid(struct nfs4_state *state)
1799 struct nfs_server *server = NFS_SERVER(state->inode);
1800 nfs4_stateid *stateid = &state->open_stateid;
1803 /* If a state reset has been done, test_stateid is unneeded */
1804 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1805 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1806 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1807 return -NFS4ERR_BAD_STATEID;
1809 status = nfs41_test_stateid(server, stateid);
1810 if (status != NFS_OK) {
1811 /* Free the stateid unless the server explicitly
1812 * informs us the stateid is unrecognized. */
1813 if (status != -NFS4ERR_BAD_STATEID)
1814 nfs41_free_stateid(server, stateid);
1816 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1817 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1818 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1823 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1827 nfs41_clear_delegation_stateid(state);
1828 status = nfs41_check_open_stateid(state);
1829 if (status != NFS_OK)
1830 status = nfs4_open_expired(sp, state);
1836 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1837 * fields corresponding to attributes that were used to store the verifier.
1838 * Make sure we clobber those fields in the later setattr call
1840 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1842 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1843 !(sattr->ia_valid & ATTR_ATIME_SET))
1844 sattr->ia_valid |= ATTR_ATIME;
1846 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1847 !(sattr->ia_valid & ATTR_MTIME_SET))
1848 sattr->ia_valid |= ATTR_MTIME;
1852 * Returns a referenced nfs4_state
1854 static int _nfs4_do_open(struct inode *dir,
1855 struct dentry *dentry,
1858 struct iattr *sattr,
1859 struct rpc_cred *cred,
1860 struct nfs4_state **res,
1861 struct nfs4_threshold **ctx_th)
1863 struct nfs4_state_owner *sp;
1864 struct nfs4_state *state = NULL;
1865 struct nfs_server *server = NFS_SERVER(dir);
1866 struct nfs4_opendata *opendata;
1869 /* Protect against reboot recovery conflicts */
1871 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1873 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1876 status = nfs4_recover_expired_lease(server);
1878 goto err_put_state_owner;
1879 if (dentry->d_inode != NULL)
1880 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1882 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1883 if (opendata == NULL)
1884 goto err_put_state_owner;
1886 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1887 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1888 if (!opendata->f_attr.mdsthreshold)
1889 goto err_opendata_put;
1890 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1892 if (dentry->d_inode != NULL)
1893 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1895 status = _nfs4_proc_open(opendata);
1897 goto err_opendata_put;
1899 state = nfs4_opendata_to_nfs4_state(opendata);
1900 status = PTR_ERR(state);
1902 goto err_opendata_put;
1903 if (server->caps & NFS_CAP_POSIX_LOCK)
1904 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1906 status = nfs4_opendata_access(cred, opendata, state, fmode, flags);
1908 goto err_opendata_put;
1910 if (opendata->o_arg.open_flags & O_EXCL) {
1911 nfs4_exclusive_attrset(opendata, sattr);
1913 nfs_fattr_init(opendata->o_res.f_attr);
1914 status = nfs4_do_setattr(state->inode, cred,
1915 opendata->o_res.f_attr, sattr,
1918 nfs_setattr_update_inode(state->inode, sattr);
1919 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1922 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1923 *ctx_th = opendata->f_attr.mdsthreshold;
1925 kfree(opendata->f_attr.mdsthreshold);
1926 opendata->f_attr.mdsthreshold = NULL;
1928 nfs4_opendata_put(opendata);
1929 nfs4_put_state_owner(sp);
1933 kfree(opendata->f_attr.mdsthreshold);
1934 nfs4_opendata_put(opendata);
1935 err_put_state_owner:
1936 nfs4_put_state_owner(sp);
1943 static struct nfs4_state *nfs4_do_open(struct inode *dir,
1944 struct dentry *dentry,
1947 struct iattr *sattr,
1948 struct rpc_cred *cred,
1949 struct nfs4_threshold **ctx_th)
1951 struct nfs4_exception exception = { };
1952 struct nfs4_state *res;
1955 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
1957 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1961 /* NOTE: BAD_SEQID means the server and client disagree about the
1962 * book-keeping w.r.t. state-changing operations
1963 * (OPEN/CLOSE/LOCK/LOCKU...)
1964 * It is actually a sign of a bug on the client or on the server.
1966 * If we receive a BAD_SEQID error in the particular case of
1967 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1968 * have unhashed the old state_owner for us, and that we can
1969 * therefore safely retry using a new one. We should still warn
1970 * the user though...
1972 if (status == -NFS4ERR_BAD_SEQID) {
1973 pr_warn_ratelimited("NFS: v4 server %s "
1974 " returned a bad sequence-id error!\n",
1975 NFS_SERVER(dir)->nfs_client->cl_hostname);
1976 exception.retry = 1;
1980 * BAD_STATEID on OPEN means that the server cancelled our
1981 * state before it received the OPEN_CONFIRM.
1982 * Recover by retrying the request as per the discussion
1983 * on Page 181 of RFC3530.
1985 if (status == -NFS4ERR_BAD_STATEID) {
1986 exception.retry = 1;
1989 if (status == -EAGAIN) {
1990 /* We must have found a delegation */
1991 exception.retry = 1;
1994 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1995 status, &exception));
1996 } while (exception.retry);
2000 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2001 struct nfs_fattr *fattr, struct iattr *sattr,
2002 struct nfs4_state *state)
2004 struct nfs_server *server = NFS_SERVER(inode);
2005 struct nfs_setattrargs arg = {
2006 .fh = NFS_FH(inode),
2009 .bitmask = server->attr_bitmask,
2011 struct nfs_setattrres res = {
2015 struct rpc_message msg = {
2016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2021 unsigned long timestamp = jiffies;
2024 nfs_fattr_init(fattr);
2026 if (state != NULL) {
2027 struct nfs_lockowner lockowner = {
2028 .l_owner = current->files,
2029 .l_pid = current->tgid,
2031 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2033 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2035 /* Use that stateid */
2037 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2039 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2040 if (status == 0 && state != NULL)
2041 renew_lease(server, timestamp);
2045 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2046 struct nfs_fattr *fattr, struct iattr *sattr,
2047 struct nfs4_state *state)
2049 struct nfs_server *server = NFS_SERVER(inode);
2050 struct nfs4_exception exception = {
2056 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2058 case -NFS4ERR_OPENMODE:
2059 if (state && !(state->state & FMODE_WRITE)) {
2061 if (sattr->ia_valid & ATTR_OPEN)
2066 err = nfs4_handle_exception(server, err, &exception);
2067 } while (exception.retry);
2072 struct nfs4_closedata {
2073 struct inode *inode;
2074 struct nfs4_state *state;
2075 struct nfs_closeargs arg;
2076 struct nfs_closeres res;
2077 struct nfs_fattr fattr;
2078 unsigned long timestamp;
2083 static void nfs4_free_closedata(void *data)
2085 struct nfs4_closedata *calldata = data;
2086 struct nfs4_state_owner *sp = calldata->state->owner;
2087 struct super_block *sb = calldata->state->inode->i_sb;
2090 pnfs_roc_release(calldata->state->inode);
2091 nfs4_put_open_state(calldata->state);
2092 nfs_free_seqid(calldata->arg.seqid);
2093 nfs4_put_state_owner(sp);
2094 nfs_sb_deactive(sb);
2098 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2101 spin_lock(&state->owner->so_lock);
2102 if (!(fmode & FMODE_READ))
2103 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2104 if (!(fmode & FMODE_WRITE))
2105 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2106 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2107 spin_unlock(&state->owner->so_lock);
2110 static void nfs4_close_done(struct rpc_task *task, void *data)
2112 struct nfs4_closedata *calldata = data;
2113 struct nfs4_state *state = calldata->state;
2114 struct nfs_server *server = NFS_SERVER(calldata->inode);
2116 dprintk("%s: begin!\n", __func__);
2117 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2119 /* hmm. we are done with the inode, and in the process of freeing
2120 * the state_owner. we keep this around to process errors
2122 switch (task->tk_status) {
2125 pnfs_roc_set_barrier(state->inode,
2126 calldata->roc_barrier);
2127 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2128 renew_lease(server, calldata->timestamp);
2129 nfs4_close_clear_stateid_flags(state,
2130 calldata->arg.fmode);
2132 case -NFS4ERR_STALE_STATEID:
2133 case -NFS4ERR_OLD_STATEID:
2134 case -NFS4ERR_BAD_STATEID:
2135 case -NFS4ERR_EXPIRED:
2136 if (calldata->arg.fmode == 0)
2139 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2140 rpc_restart_call_prepare(task);
2142 nfs_release_seqid(calldata->arg.seqid);
2143 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2144 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2147 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2149 struct nfs4_closedata *calldata = data;
2150 struct nfs4_state *state = calldata->state;
2151 struct inode *inode = calldata->inode;
2154 dprintk("%s: begin!\n", __func__);
2155 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2158 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2159 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2160 spin_lock(&state->owner->so_lock);
2161 /* Calculate the change in open mode */
2162 if (state->n_rdwr == 0) {
2163 if (state->n_rdonly == 0) {
2164 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2165 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2166 calldata->arg.fmode &= ~FMODE_READ;
2168 if (state->n_wronly == 0) {
2169 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2170 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2171 calldata->arg.fmode &= ~FMODE_WRITE;
2174 spin_unlock(&state->owner->so_lock);
2177 /* Note: exit _without_ calling nfs4_close_done */
2178 task->tk_action = NULL;
2179 nfs4_sequence_done(task, &calldata->res.seq_res);
2183 if (calldata->arg.fmode == 0) {
2184 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2185 if (calldata->roc &&
2186 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2190 nfs_fattr_init(calldata->res.fattr);
2191 calldata->timestamp = jiffies;
2192 if (nfs4_setup_sequence(NFS_SERVER(inode),
2193 &calldata->arg.seq_args,
2194 &calldata->res.seq_res,
2196 nfs_release_seqid(calldata->arg.seqid);
2198 dprintk("%s: done!\n", __func__);
2201 static const struct rpc_call_ops nfs4_close_ops = {
2202 .rpc_call_prepare = nfs4_close_prepare,
2203 .rpc_call_done = nfs4_close_done,
2204 .rpc_release = nfs4_free_closedata,
2208 * It is possible for data to be read/written from a mem-mapped file
2209 * after the sys_close call (which hits the vfs layer as a flush).
2210 * This means that we can't safely call nfsv4 close on a file until
2211 * the inode is cleared. This in turn means that we are not good
2212 * NFSv4 citizens - we do not indicate to the server to update the file's
2213 * share state even when we are done with one of the three share
2214 * stateid's in the inode.
2216 * NOTE: Caller must be holding the sp->so_owner semaphore!
2218 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2220 struct nfs_server *server = NFS_SERVER(state->inode);
2221 struct nfs4_closedata *calldata;
2222 struct nfs4_state_owner *sp = state->owner;
2223 struct rpc_task *task;
2224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2226 .rpc_cred = state->owner->so_cred,
2228 struct rpc_task_setup task_setup_data = {
2229 .rpc_client = server->client,
2230 .rpc_message = &msg,
2231 .callback_ops = &nfs4_close_ops,
2232 .workqueue = nfsiod_workqueue,
2233 .flags = RPC_TASK_ASYNC,
2235 int status = -ENOMEM;
2237 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2238 if (calldata == NULL)
2240 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2241 calldata->inode = state->inode;
2242 calldata->state = state;
2243 calldata->arg.fh = NFS_FH(state->inode);
2244 calldata->arg.stateid = &state->open_stateid;
2245 /* Serialization for the sequence id */
2246 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2247 if (calldata->arg.seqid == NULL)
2248 goto out_free_calldata;
2249 calldata->arg.fmode = 0;
2250 calldata->arg.bitmask = server->cache_consistency_bitmask;
2251 calldata->res.fattr = &calldata->fattr;
2252 calldata->res.seqid = calldata->arg.seqid;
2253 calldata->res.server = server;
2254 calldata->roc = pnfs_roc(state->inode);
2255 nfs_sb_active(calldata->inode->i_sb);
2257 msg.rpc_argp = &calldata->arg;
2258 msg.rpc_resp = &calldata->res;
2259 task_setup_data.callback_data = calldata;
2260 task = rpc_run_task(&task_setup_data);
2262 return PTR_ERR(task);
2265 status = rpc_wait_for_completion_task(task);
2271 nfs4_put_open_state(state);
2272 nfs4_put_state_owner(sp);
2276 static struct inode *
2277 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2279 struct nfs4_state *state;
2281 /* Protect against concurrent sillydeletes */
2282 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2283 ctx->cred, &ctx->mdsthreshold);
2285 return ERR_CAST(state);
2287 return igrab(state->inode);
2290 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2292 if (ctx->state == NULL)
2295 nfs4_close_sync(ctx->state, ctx->mode);
2297 nfs4_close_state(ctx->state, ctx->mode);
2300 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2302 struct nfs4_server_caps_arg args = {
2305 struct nfs4_server_caps_res res = {};
2306 struct rpc_message msg = {
2307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2313 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2315 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2316 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2317 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2318 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2319 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2320 NFS_CAP_CTIME|NFS_CAP_MTIME);
2321 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2322 server->caps |= NFS_CAP_ACLS;
2323 if (res.has_links != 0)
2324 server->caps |= NFS_CAP_HARDLINKS;
2325 if (res.has_symlinks != 0)
2326 server->caps |= NFS_CAP_SYMLINKS;
2327 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2328 server->caps |= NFS_CAP_FILEID;
2329 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2330 server->caps |= NFS_CAP_MODE;
2331 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2332 server->caps |= NFS_CAP_NLINK;
2333 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2334 server->caps |= NFS_CAP_OWNER;
2335 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2336 server->caps |= NFS_CAP_OWNER_GROUP;
2337 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2338 server->caps |= NFS_CAP_ATIME;
2339 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2340 server->caps |= NFS_CAP_CTIME;
2341 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2342 server->caps |= NFS_CAP_MTIME;
2344 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2345 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2346 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2347 server->acl_bitmask = res.acl_bitmask;
2348 server->fh_expire_type = res.fh_expire_type;
2354 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2356 struct nfs4_exception exception = { };
2359 err = nfs4_handle_exception(server,
2360 _nfs4_server_capabilities(server, fhandle),
2362 } while (exception.retry);
2366 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2367 struct nfs_fsinfo *info)
2369 struct nfs4_lookup_root_arg args = {
2370 .bitmask = nfs4_fattr_bitmap,
2372 struct nfs4_lookup_res res = {
2374 .fattr = info->fattr,
2377 struct rpc_message msg = {
2378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2383 nfs_fattr_init(info->fattr);
2384 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2387 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2388 struct nfs_fsinfo *info)
2390 struct nfs4_exception exception = { };
2393 err = _nfs4_lookup_root(server, fhandle, info);
2396 case -NFS4ERR_WRONGSEC:
2399 err = nfs4_handle_exception(server, err, &exception);
2401 } while (exception.retry);
2406 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2407 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2409 struct rpc_auth *auth;
2412 auth = rpcauth_create(flavor, server->client);
2417 ret = nfs4_lookup_root(server, fhandle, info);
2422 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2423 struct nfs_fsinfo *info)
2425 int i, len, status = 0;
2426 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2428 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2432 for (i = 0; i < len; i++) {
2433 /* AUTH_UNIX is the default flavor if none was specified,
2434 * thus has already been tried. */
2435 if (flav_array[i] == RPC_AUTH_UNIX)
2438 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2439 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2444 * -EACCESS could mean that the user doesn't have correct permissions
2445 * to access the mount. It could also mean that we tried to mount
2446 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2447 * existing mount programs don't handle -EACCES very well so it should
2448 * be mapped to -EPERM instead.
2450 if (status == -EACCES)
2456 * get the file handle for the "/" directory on the server
2458 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2459 struct nfs_fsinfo *info)
2461 int minor_version = server->nfs_client->cl_minorversion;
2462 int status = nfs4_lookup_root(server, fhandle, info);
2463 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2465 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2466 * by nfs4_map_errors() as this function exits.
2468 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2470 status = nfs4_server_capabilities(server, fhandle);
2472 status = nfs4_do_fsinfo(server, fhandle, info);
2473 return nfs4_map_errors(status);
2476 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2477 struct nfs_fsinfo *info)
2480 struct nfs_fattr *fattr = info->fattr;
2482 error = nfs4_server_capabilities(server, mntfh);
2484 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2488 error = nfs4_proc_getattr(server, mntfh, fattr);
2490 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2494 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2495 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2496 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2502 * Get locations and (maybe) other attributes of a referral.
2503 * Note that we'll actually follow the referral later when
2504 * we detect fsid mismatch in inode revalidation
2506 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2507 const struct qstr *name, struct nfs_fattr *fattr,
2508 struct nfs_fh *fhandle)
2510 int status = -ENOMEM;
2511 struct page *page = NULL;
2512 struct nfs4_fs_locations *locations = NULL;
2514 page = alloc_page(GFP_KERNEL);
2517 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2518 if (locations == NULL)
2521 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2524 /* Make sure server returned a different fsid for the referral */
2525 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2526 dprintk("%s: server did not return a different fsid for"
2527 " a referral at %s\n", __func__, name->name);
2531 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2532 nfs_fixup_referral_attributes(&locations->fattr);
2534 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2535 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2536 memset(fhandle, 0, sizeof(struct nfs_fh));
2544 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2546 struct nfs4_getattr_arg args = {
2548 .bitmask = server->attr_bitmask,
2550 struct nfs4_getattr_res res = {
2554 struct rpc_message msg = {
2555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2560 nfs_fattr_init(fattr);
2561 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2564 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2566 struct nfs4_exception exception = { };
2569 err = nfs4_handle_exception(server,
2570 _nfs4_proc_getattr(server, fhandle, fattr),
2572 } while (exception.retry);
2577 * The file is not closed if it is opened due to the a request to change
2578 * the size of the file. The open call will not be needed once the
2579 * VFS layer lookup-intents are implemented.
2581 * Close is called when the inode is destroyed.
2582 * If we haven't opened the file for O_WRONLY, we
2583 * need to in the size_change case to obtain a stateid.
2586 * Because OPEN is always done by name in nfsv4, it is
2587 * possible that we opened a different file by the same
2588 * name. We can recognize this race condition, but we
2589 * can't do anything about it besides returning an error.
2591 * This will be fixed with VFS changes (lookup-intent).
2594 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2595 struct iattr *sattr)
2597 struct inode *inode = dentry->d_inode;
2598 struct rpc_cred *cred = NULL;
2599 struct nfs4_state *state = NULL;
2602 if (pnfs_ld_layoutret_on_setattr(inode))
2603 pnfs_return_layout(inode);
2605 nfs_fattr_init(fattr);
2607 /* Deal with open(O_TRUNC) */
2608 if (sattr->ia_valid & ATTR_OPEN)
2609 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2611 /* Optimization: if the end result is no change, don't RPC */
2612 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2615 /* Search for an existing open(O_WRITE) file */
2616 if (sattr->ia_valid & ATTR_FILE) {
2617 struct nfs_open_context *ctx;
2619 ctx = nfs_file_open_context(sattr->ia_file);
2626 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2628 nfs_setattr_update_inode(inode, sattr);
2632 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2633 const struct qstr *name, struct nfs_fh *fhandle,
2634 struct nfs_fattr *fattr)
2636 struct nfs_server *server = NFS_SERVER(dir);
2638 struct nfs4_lookup_arg args = {
2639 .bitmask = server->attr_bitmask,
2640 .dir_fh = NFS_FH(dir),
2643 struct nfs4_lookup_res res = {
2648 struct rpc_message msg = {
2649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2654 nfs_fattr_init(fattr);
2656 dprintk("NFS call lookup %s\n", name->name);
2657 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2658 dprintk("NFS reply lookup: %d\n", status);
2662 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2664 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2665 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2666 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2670 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2671 struct qstr *name, struct nfs_fh *fhandle,
2672 struct nfs_fattr *fattr)
2674 struct nfs4_exception exception = { };
2675 struct rpc_clnt *client = *clnt;
2678 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2680 case -NFS4ERR_BADNAME:
2683 case -NFS4ERR_MOVED:
2684 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2686 case -NFS4ERR_WRONGSEC:
2688 if (client != *clnt)
2691 client = nfs4_create_sec_client(client, dir, name);
2693 return PTR_ERR(client);
2695 exception.retry = 1;
2698 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2700 } while (exception.retry);
2705 else if (client != *clnt)
2706 rpc_shutdown_client(client);
2711 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2712 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2715 struct rpc_clnt *client = NFS_CLIENT(dir);
2717 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2718 if (client != NFS_CLIENT(dir)) {
2719 rpc_shutdown_client(client);
2720 nfs_fixup_secinfo_attributes(fattr);
2726 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2727 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2730 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2732 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2734 rpc_shutdown_client(client);
2735 return ERR_PTR(status);
2740 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2742 struct nfs_server *server = NFS_SERVER(inode);
2743 struct nfs4_accessargs args = {
2744 .fh = NFS_FH(inode),
2745 .bitmask = server->cache_consistency_bitmask,
2747 struct nfs4_accessres res = {
2750 struct rpc_message msg = {
2751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2754 .rpc_cred = entry->cred,
2756 int mode = entry->mask;
2760 * Determine which access bits we want to ask for...
2762 if (mode & MAY_READ)
2763 args.access |= NFS4_ACCESS_READ;
2764 if (S_ISDIR(inode->i_mode)) {
2765 if (mode & MAY_WRITE)
2766 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2767 if (mode & MAY_EXEC)
2768 args.access |= NFS4_ACCESS_LOOKUP;
2770 if (mode & MAY_WRITE)
2771 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2772 if (mode & MAY_EXEC)
2773 args.access |= NFS4_ACCESS_EXECUTE;
2776 res.fattr = nfs_alloc_fattr();
2777 if (res.fattr == NULL)
2780 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2782 nfs_access_set_mask(entry, res.access);
2783 nfs_refresh_inode(inode, res.fattr);
2785 nfs_free_fattr(res.fattr);
2789 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2791 struct nfs4_exception exception = { };
2794 err = nfs4_handle_exception(NFS_SERVER(inode),
2795 _nfs4_proc_access(inode, entry),
2797 } while (exception.retry);
2802 * TODO: For the time being, we don't try to get any attributes
2803 * along with any of the zero-copy operations READ, READDIR,
2806 * In the case of the first three, we want to put the GETATTR
2807 * after the read-type operation -- this is because it is hard
2808 * to predict the length of a GETATTR response in v4, and thus
2809 * align the READ data correctly. This means that the GETATTR
2810 * may end up partially falling into the page cache, and we should
2811 * shift it into the 'tail' of the xdr_buf before processing.
2812 * To do this efficiently, we need to know the total length
2813 * of data received, which doesn't seem to be available outside
2816 * In the case of WRITE, we also want to put the GETATTR after
2817 * the operation -- in this case because we want to make sure
2818 * we get the post-operation mtime and size.
2820 * Both of these changes to the XDR layer would in fact be quite
2821 * minor, but I decided to leave them for a subsequent patch.
2823 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2824 unsigned int pgbase, unsigned int pglen)
2826 struct nfs4_readlink args = {
2827 .fh = NFS_FH(inode),
2832 struct nfs4_readlink_res res;
2833 struct rpc_message msg = {
2834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2839 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2842 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2843 unsigned int pgbase, unsigned int pglen)
2845 struct nfs4_exception exception = { };
2848 err = nfs4_handle_exception(NFS_SERVER(inode),
2849 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2851 } while (exception.retry);
2856 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2859 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2862 struct nfs_open_context *ctx;
2863 struct nfs4_state *state;
2866 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2868 return PTR_ERR(ctx);
2870 sattr->ia_mode &= ~current_umask();
2871 state = nfs4_do_open(dir, dentry, ctx->mode,
2872 flags, sattr, ctx->cred,
2873 &ctx->mdsthreshold);
2875 if (IS_ERR(state)) {
2876 status = PTR_ERR(state);
2879 d_add(dentry, igrab(state->inode));
2880 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2883 put_nfs_open_context(ctx);
2887 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2889 struct nfs_server *server = NFS_SERVER(dir);
2890 struct nfs_removeargs args = {
2894 struct nfs_removeres res = {
2897 struct rpc_message msg = {
2898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2904 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2906 update_changeattr(dir, &res.cinfo);
2910 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2912 struct nfs4_exception exception = { };
2915 err = nfs4_handle_exception(NFS_SERVER(dir),
2916 _nfs4_proc_remove(dir, name),
2918 } while (exception.retry);
2922 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2924 struct nfs_server *server = NFS_SERVER(dir);
2925 struct nfs_removeargs *args = msg->rpc_argp;
2926 struct nfs_removeres *res = msg->rpc_resp;
2928 res->server = server;
2929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2930 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2933 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2935 nfs4_setup_sequence(NFS_SERVER(data->dir),
2936 &data->args.seq_args,
2941 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2943 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2945 if (!nfs4_sequence_done(task, &res->seq_res))
2947 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2949 update_changeattr(dir, &res->cinfo);
2953 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2955 struct nfs_server *server = NFS_SERVER(dir);
2956 struct nfs_renameargs *arg = msg->rpc_argp;
2957 struct nfs_renameres *res = msg->rpc_resp;
2959 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2960 res->server = server;
2961 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2964 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2966 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2967 &data->args.seq_args,
2972 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2973 struct inode *new_dir)
2975 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2977 if (!nfs4_sequence_done(task, &res->seq_res))
2979 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2982 update_changeattr(old_dir, &res->old_cinfo);
2983 update_changeattr(new_dir, &res->new_cinfo);
2987 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2988 struct inode *new_dir, struct qstr *new_name)
2990 struct nfs_server *server = NFS_SERVER(old_dir);
2991 struct nfs_renameargs arg = {
2992 .old_dir = NFS_FH(old_dir),
2993 .new_dir = NFS_FH(new_dir),
2994 .old_name = old_name,
2995 .new_name = new_name,
2997 struct nfs_renameres res = {
3000 struct rpc_message msg = {
3001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3005 int status = -ENOMEM;
3007 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3009 update_changeattr(old_dir, &res.old_cinfo);
3010 update_changeattr(new_dir, &res.new_cinfo);
3015 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3016 struct inode *new_dir, struct qstr *new_name)
3018 struct nfs4_exception exception = { };
3021 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3022 _nfs4_proc_rename(old_dir, old_name,
3025 } while (exception.retry);
3029 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3031 struct nfs_server *server = NFS_SERVER(inode);
3032 struct nfs4_link_arg arg = {
3033 .fh = NFS_FH(inode),
3034 .dir_fh = NFS_FH(dir),
3036 .bitmask = server->attr_bitmask,
3038 struct nfs4_link_res res = {
3041 struct rpc_message msg = {
3042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3046 int status = -ENOMEM;
3048 res.fattr = nfs_alloc_fattr();
3049 if (res.fattr == NULL)
3052 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3054 update_changeattr(dir, &res.cinfo);
3055 nfs_post_op_update_inode(inode, res.fattr);
3058 nfs_free_fattr(res.fattr);
3062 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3064 struct nfs4_exception exception = { };
3067 err = nfs4_handle_exception(NFS_SERVER(inode),
3068 _nfs4_proc_link(inode, dir, name),
3070 } while (exception.retry);
3074 struct nfs4_createdata {
3075 struct rpc_message msg;
3076 struct nfs4_create_arg arg;
3077 struct nfs4_create_res res;
3079 struct nfs_fattr fattr;
3082 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3083 struct qstr *name, struct iattr *sattr, u32 ftype)
3085 struct nfs4_createdata *data;
3087 data = kzalloc(sizeof(*data), GFP_KERNEL);
3089 struct nfs_server *server = NFS_SERVER(dir);
3091 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3092 data->msg.rpc_argp = &data->arg;
3093 data->msg.rpc_resp = &data->res;
3094 data->arg.dir_fh = NFS_FH(dir);
3095 data->arg.server = server;
3096 data->arg.name = name;
3097 data->arg.attrs = sattr;
3098 data->arg.ftype = ftype;
3099 data->arg.bitmask = server->attr_bitmask;
3100 data->res.server = server;
3101 data->res.fh = &data->fh;
3102 data->res.fattr = &data->fattr;
3103 nfs_fattr_init(data->res.fattr);
3108 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3110 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3111 &data->arg.seq_args, &data->res.seq_res, 1);
3113 update_changeattr(dir, &data->res.dir_cinfo);
3114 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3119 static void nfs4_free_createdata(struct nfs4_createdata *data)
3124 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3125 struct page *page, unsigned int len, struct iattr *sattr)
3127 struct nfs4_createdata *data;
3128 int status = -ENAMETOOLONG;
3130 if (len > NFS4_MAXPATHLEN)
3134 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3138 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3139 data->arg.u.symlink.pages = &page;
3140 data->arg.u.symlink.len = len;
3142 status = nfs4_do_create(dir, dentry, data);
3144 nfs4_free_createdata(data);
3149 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3150 struct page *page, unsigned int len, struct iattr *sattr)
3152 struct nfs4_exception exception = { };
3155 err = nfs4_handle_exception(NFS_SERVER(dir),
3156 _nfs4_proc_symlink(dir, dentry, page,
3159 } while (exception.retry);
3163 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3164 struct iattr *sattr)
3166 struct nfs4_createdata *data;
3167 int status = -ENOMEM;
3169 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3173 status = nfs4_do_create(dir, dentry, data);
3175 nfs4_free_createdata(data);
3180 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3181 struct iattr *sattr)
3183 struct nfs4_exception exception = { };
3186 sattr->ia_mode &= ~current_umask();
3188 err = nfs4_handle_exception(NFS_SERVER(dir),
3189 _nfs4_proc_mkdir(dir, dentry, sattr),
3191 } while (exception.retry);
3195 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3196 u64 cookie, struct page **pages, unsigned int count, int plus)
3198 struct inode *dir = dentry->d_inode;
3199 struct nfs4_readdir_arg args = {
3204 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3207 struct nfs4_readdir_res res;
3208 struct rpc_message msg = {
3209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3216 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3217 dentry->d_parent->d_name.name,
3218 dentry->d_name.name,
3219 (unsigned long long)cookie);
3220 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3221 res.pgbase = args.pgbase;
3222 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3224 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3225 status += args.pgbase;
3228 nfs_invalidate_atime(dir);
3230 dprintk("%s: returns %d\n", __func__, status);
3234 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3235 u64 cookie, struct page **pages, unsigned int count, int plus)
3237 struct nfs4_exception exception = { };
3240 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3241 _nfs4_proc_readdir(dentry, cred, cookie,
3242 pages, count, plus),
3244 } while (exception.retry);
3248 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3249 struct iattr *sattr, dev_t rdev)
3251 struct nfs4_createdata *data;
3252 int mode = sattr->ia_mode;
3253 int status = -ENOMEM;
3255 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3260 data->arg.ftype = NF4FIFO;
3261 else if (S_ISBLK(mode)) {
3262 data->arg.ftype = NF4BLK;
3263 data->arg.u.device.specdata1 = MAJOR(rdev);
3264 data->arg.u.device.specdata2 = MINOR(rdev);
3266 else if (S_ISCHR(mode)) {
3267 data->arg.ftype = NF4CHR;
3268 data->arg.u.device.specdata1 = MAJOR(rdev);
3269 data->arg.u.device.specdata2 = MINOR(rdev);
3270 } else if (!S_ISSOCK(mode)) {
3275 status = nfs4_do_create(dir, dentry, data);
3277 nfs4_free_createdata(data);
3282 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3283 struct iattr *sattr, dev_t rdev)
3285 struct nfs4_exception exception = { };
3288 sattr->ia_mode &= ~current_umask();
3290 err = nfs4_handle_exception(NFS_SERVER(dir),
3291 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3293 } while (exception.retry);
3297 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3298 struct nfs_fsstat *fsstat)
3300 struct nfs4_statfs_arg args = {
3302 .bitmask = server->attr_bitmask,
3304 struct nfs4_statfs_res res = {
3307 struct rpc_message msg = {
3308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3313 nfs_fattr_init(fsstat->fattr);
3314 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3317 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3319 struct nfs4_exception exception = { };
3322 err = nfs4_handle_exception(server,
3323 _nfs4_proc_statfs(server, fhandle, fsstat),
3325 } while (exception.retry);
3329 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3330 struct nfs_fsinfo *fsinfo)
3332 struct nfs4_fsinfo_arg args = {
3334 .bitmask = server->attr_bitmask,
3336 struct nfs4_fsinfo_res res = {
3339 struct rpc_message msg = {
3340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3345 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3348 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3350 struct nfs4_exception exception = { };
3354 err = nfs4_handle_exception(server,
3355 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3357 } while (exception.retry);
3361 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3365 nfs_fattr_init(fsinfo->fattr);
3366 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3368 /* block layout checks this! */
3369 server->pnfs_blksize = fsinfo->blksize;
3370 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3376 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3377 struct nfs_pathconf *pathconf)
3379 struct nfs4_pathconf_arg args = {
3381 .bitmask = server->attr_bitmask,
3383 struct nfs4_pathconf_res res = {
3384 .pathconf = pathconf,
3386 struct rpc_message msg = {
3387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3392 /* None of the pathconf attributes are mandatory to implement */
3393 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3394 memset(pathconf, 0, sizeof(*pathconf));
3398 nfs_fattr_init(pathconf->fattr);
3399 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3402 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3403 struct nfs_pathconf *pathconf)
3405 struct nfs4_exception exception = { };
3409 err = nfs4_handle_exception(server,
3410 _nfs4_proc_pathconf(server, fhandle, pathconf),
3412 } while (exception.retry);
3416 void __nfs4_read_done_cb(struct nfs_read_data *data)
3418 nfs_invalidate_atime(data->header->inode);
3421 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3423 struct nfs_server *server = NFS_SERVER(data->header->inode);
3425 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3426 rpc_restart_call_prepare(task);
3430 __nfs4_read_done_cb(data);
3431 if (task->tk_status > 0)
3432 renew_lease(server, data->timestamp);
3436 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3439 dprintk("--> %s\n", __func__);
3441 if (!nfs4_sequence_done(task, &data->res.seq_res))
3444 return data->read_done_cb ? data->read_done_cb(task, data) :
3445 nfs4_read_done_cb(task, data);
3448 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3450 data->timestamp = jiffies;
3451 data->read_done_cb = nfs4_read_done_cb;
3452 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3453 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3456 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3458 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3459 &data->args.seq_args,
3464 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3466 struct inode *inode = data->header->inode;
3468 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3469 rpc_restart_call_prepare(task);
3472 if (task->tk_status >= 0) {
3473 renew_lease(NFS_SERVER(inode), data->timestamp);
3474 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3479 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3481 if (!nfs4_sequence_done(task, &data->res.seq_res))
3483 return data->write_done_cb ? data->write_done_cb(task, data) :
3484 nfs4_write_done_cb(task, data);
3488 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3490 const struct nfs_pgio_header *hdr = data->header;
3492 /* Don't request attributes for pNFS or O_DIRECT writes */
3493 if (data->ds_clp != NULL || hdr->dreq != NULL)
3495 /* Otherwise, request attributes if and only if we don't hold
3498 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3501 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3503 struct nfs_server *server = NFS_SERVER(data->header->inode);
3505 if (!nfs4_write_need_cache_consistency_data(data)) {
3506 data->args.bitmask = NULL;
3507 data->res.fattr = NULL;
3509 data->args.bitmask = server->cache_consistency_bitmask;
3511 if (!data->write_done_cb)
3512 data->write_done_cb = nfs4_write_done_cb;
3513 data->res.server = server;
3514 data->timestamp = jiffies;
3516 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3517 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3520 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3522 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3523 &data->args.seq_args,
3528 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3530 nfs4_setup_sequence(NFS_SERVER(data->inode),
3531 &data->args.seq_args,
3536 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3538 struct inode *inode = data->inode;
3540 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3541 rpc_restart_call_prepare(task);
3547 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3549 if (!nfs4_sequence_done(task, &data->res.seq_res))
3551 return data->commit_done_cb(task, data);
3554 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3556 struct nfs_server *server = NFS_SERVER(data->inode);
3558 if (data->commit_done_cb == NULL)
3559 data->commit_done_cb = nfs4_commit_done_cb;
3560 data->res.server = server;
3561 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3562 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3565 struct nfs4_renewdata {
3566 struct nfs_client *client;
3567 unsigned long timestamp;
3571 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3572 * standalone procedure for queueing an asynchronous RENEW.
3574 static void nfs4_renew_release(void *calldata)
3576 struct nfs4_renewdata *data = calldata;
3577 struct nfs_client *clp = data->client;
3579 if (atomic_read(&clp->cl_count) > 1)
3580 nfs4_schedule_state_renewal(clp);
3581 nfs_put_client(clp);
3585 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3587 struct nfs4_renewdata *data = calldata;
3588 struct nfs_client *clp = data->client;
3589 unsigned long timestamp = data->timestamp;
3591 if (task->tk_status < 0) {
3592 /* Unless we're shutting down, schedule state recovery! */
3593 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3595 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3596 nfs4_schedule_lease_recovery(clp);
3599 nfs4_schedule_path_down_recovery(clp);
3601 do_renew_lease(clp, timestamp);
3604 static const struct rpc_call_ops nfs4_renew_ops = {
3605 .rpc_call_done = nfs4_renew_done,
3606 .rpc_release = nfs4_renew_release,
3609 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3611 struct rpc_message msg = {
3612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3616 struct nfs4_renewdata *data;
3618 if (renew_flags == 0)
3620 if (!atomic_inc_not_zero(&clp->cl_count))
3622 data = kmalloc(sizeof(*data), GFP_NOFS);
3626 data->timestamp = jiffies;
3627 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3628 &nfs4_renew_ops, data);
3631 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3633 struct rpc_message msg = {
3634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3638 unsigned long now = jiffies;
3641 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3644 do_renew_lease(clp, now);
3648 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3650 return (server->caps & NFS_CAP_ACLS)
3651 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3652 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3655 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3656 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3659 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3661 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3662 struct page **pages, unsigned int *pgbase)
3664 struct page *newpage, **spages;
3670 len = min_t(size_t, PAGE_SIZE, buflen);
3671 newpage = alloc_page(GFP_KERNEL);
3673 if (newpage == NULL)
3675 memcpy(page_address(newpage), buf, len);
3680 } while (buflen != 0);
3686 __free_page(spages[rc-1]);
3690 struct nfs4_cached_acl {
3696 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3698 struct nfs_inode *nfsi = NFS_I(inode);
3700 spin_lock(&inode->i_lock);
3701 kfree(nfsi->nfs4_acl);
3702 nfsi->nfs4_acl = acl;
3703 spin_unlock(&inode->i_lock);
3706 static void nfs4_zap_acl_attr(struct inode *inode)
3708 nfs4_set_cached_acl(inode, NULL);
3711 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3713 struct nfs_inode *nfsi = NFS_I(inode);
3714 struct nfs4_cached_acl *acl;
3717 spin_lock(&inode->i_lock);
3718 acl = nfsi->nfs4_acl;
3721 if (buf == NULL) /* user is just asking for length */
3723 if (acl->cached == 0)
3725 ret = -ERANGE; /* see getxattr(2) man page */
3726 if (acl->len > buflen)
3728 memcpy(buf, acl->data, acl->len);
3732 spin_unlock(&inode->i_lock);
3736 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3738 struct nfs4_cached_acl *acl;
3739 size_t buflen = sizeof(*acl) + acl_len;
3741 if (buflen <= PAGE_SIZE) {
3742 acl = kmalloc(buflen, GFP_KERNEL);
3746 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3748 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3755 nfs4_set_cached_acl(inode, acl);
3759 * The getxattr API returns the required buffer length when called with a
3760 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3761 * the required buf. On a NULL buf, we send a page of data to the server
3762 * guessing that the ACL request can be serviced by a page. If so, we cache
3763 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3764 * the cache. If not so, we throw away the page, and cache the required
3765 * length. The next getxattr call will then produce another round trip to
3766 * the server, this time with the input buf of the required size.
3768 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3770 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3771 struct nfs_getaclargs args = {
3772 .fh = NFS_FH(inode),
3776 struct nfs_getaclres res = {
3779 struct rpc_message msg = {
3780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3784 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3785 int ret = -ENOMEM, i;
3787 /* As long as we're doing a round trip to the server anyway,
3788 * let's be prepared for a page of acl data. */
3791 if (npages > ARRAY_SIZE(pages))
3794 for (i = 0; i < npages; i++) {
3795 pages[i] = alloc_page(GFP_KERNEL);
3800 /* for decoding across pages */
3801 res.acl_scratch = alloc_page(GFP_KERNEL);
3802 if (!res.acl_scratch)
3805 args.acl_len = npages * PAGE_SIZE;
3806 args.acl_pgbase = 0;
3808 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3809 __func__, buf, buflen, npages, args.acl_len);
3810 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3811 &msg, &args.seq_args, &res.seq_res, 0);
3815 /* Handle the case where the passed-in buffer is too short */
3816 if (res.acl_flags & NFS4_ACL_TRUNC) {
3817 /* Did the user only issue a request for the acl length? */
3823 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3825 if (res.acl_len > buflen) {
3829 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3834 for (i = 0; i < npages; i++)
3836 __free_page(pages[i]);
3837 if (res.acl_scratch)
3838 __free_page(res.acl_scratch);
3842 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3844 struct nfs4_exception exception = { };
3847 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3850 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3851 } while (exception.retry);
3855 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3857 struct nfs_server *server = NFS_SERVER(inode);
3860 if (!nfs4_server_supports_acls(server))
3862 ret = nfs_revalidate_inode(server, inode);
3865 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3866 nfs_zap_acl_cache(inode);
3867 ret = nfs4_read_cached_acl(inode, buf, buflen);
3869 /* -ENOENT is returned if there is no ACL or if there is an ACL
3870 * but no cached acl data, just the acl length */
3872 return nfs4_get_acl_uncached(inode, buf, buflen);
3875 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3877 struct nfs_server *server = NFS_SERVER(inode);
3878 struct page *pages[NFS4ACL_MAXPAGES];
3879 struct nfs_setaclargs arg = {
3880 .fh = NFS_FH(inode),
3884 struct nfs_setaclres res;
3885 struct rpc_message msg = {
3886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3890 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3893 if (!nfs4_server_supports_acls(server))
3895 if (npages > ARRAY_SIZE(pages))
3897 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3900 nfs4_inode_return_delegation(inode);
3901 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3904 * Free each page after tx, so the only ref left is
3905 * held by the network stack
3908 put_page(pages[i-1]);
3911 * Acl update can result in inode attribute update.
3912 * so mark the attribute cache invalid.
3914 spin_lock(&inode->i_lock);
3915 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3916 spin_unlock(&inode->i_lock);
3917 nfs_access_zap_cache(inode);
3918 nfs_zap_acl_cache(inode);
3922 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3924 struct nfs4_exception exception = { };
3927 err = nfs4_handle_exception(NFS_SERVER(inode),
3928 __nfs4_proc_set_acl(inode, buf, buflen),
3930 } while (exception.retry);
3935 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3937 struct nfs_client *clp = server->nfs_client;
3939 if (task->tk_status >= 0)
3941 switch(task->tk_status) {
3942 case -NFS4ERR_DELEG_REVOKED:
3943 case -NFS4ERR_ADMIN_REVOKED:
3944 case -NFS4ERR_BAD_STATEID:
3947 nfs_remove_bad_delegation(state->inode);
3948 case -NFS4ERR_OPENMODE:
3951 nfs4_schedule_stateid_recovery(server, state);
3952 goto wait_on_recovery;
3953 case -NFS4ERR_EXPIRED:
3955 nfs4_schedule_stateid_recovery(server, state);
3956 case -NFS4ERR_STALE_STATEID:
3957 case -NFS4ERR_STALE_CLIENTID:
3958 nfs4_schedule_lease_recovery(clp);
3959 goto wait_on_recovery;
3960 #if defined(CONFIG_NFS_V4_1)
3961 case -NFS4ERR_BADSESSION:
3962 case -NFS4ERR_BADSLOT:
3963 case -NFS4ERR_BAD_HIGH_SLOT:
3964 case -NFS4ERR_DEADSESSION:
3965 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3966 case -NFS4ERR_SEQ_FALSE_RETRY:
3967 case -NFS4ERR_SEQ_MISORDERED:
3968 dprintk("%s ERROR %d, Reset session\n", __func__,
3970 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
3971 task->tk_status = 0;
3973 #endif /* CONFIG_NFS_V4_1 */
3974 case -NFS4ERR_DELAY:
3975 nfs_inc_server_stats(server, NFSIOS_DELAY);
3976 case -NFS4ERR_GRACE:
3977 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3978 task->tk_status = 0;
3980 case -NFS4ERR_RETRY_UNCACHED_REP:
3981 case -NFS4ERR_OLD_STATEID:
3982 task->tk_status = 0;
3985 task->tk_status = nfs4_map_errors(task->tk_status);
3988 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3989 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3990 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3991 task->tk_status = 0;
3995 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3996 nfs4_verifier *bootverf)
4000 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4001 /* An impossible timestamp guarantees this value
4002 * will never match a generated boot time. */
4004 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4006 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4007 verf[0] = (__be32)nn->boot_time.tv_sec;
4008 verf[1] = (__be32)nn->boot_time.tv_nsec;
4010 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4014 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4015 char *buf, size_t len)
4017 unsigned int result;
4020 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4022 rpc_peeraddr2str(clp->cl_rpcclient,
4024 rpc_peeraddr2str(clp->cl_rpcclient,
4025 RPC_DISPLAY_PROTO));
4031 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4032 char *buf, size_t len)
4034 char *nodename = clp->cl_rpcclient->cl_nodename;
4036 if (nfs4_client_id_uniquifier[0] != '\0')
4037 nodename = nfs4_client_id_uniquifier;
4038 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4039 clp->rpc_ops->version, clp->cl_minorversion,
4044 * nfs4_proc_setclientid - Negotiate client ID
4045 * @clp: state data structure
4046 * @program: RPC program for NFSv4 callback service
4047 * @port: IP port number for NFS4 callback service
4048 * @cred: RPC credential to use for this call
4049 * @res: where to place the result
4051 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4053 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4054 unsigned short port, struct rpc_cred *cred,
4055 struct nfs4_setclientid_res *res)
4057 nfs4_verifier sc_verifier;
4058 struct nfs4_setclientid setclientid = {
4059 .sc_verifier = &sc_verifier,
4061 .sc_cb_ident = clp->cl_cb_ident,
4063 struct rpc_message msg = {
4064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4065 .rpc_argp = &setclientid,
4071 /* nfs_client_id4 */
4072 nfs4_init_boot_verifier(clp, &sc_verifier);
4073 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4074 setclientid.sc_name_len =
4075 nfs4_init_uniform_client_string(clp,
4076 setclientid.sc_name,
4077 sizeof(setclientid.sc_name));
4079 setclientid.sc_name_len =
4080 nfs4_init_nonuniform_client_string(clp,
4081 setclientid.sc_name,
4082 sizeof(setclientid.sc_name));
4085 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4086 sizeof(setclientid.sc_netid),
4087 rpc_peeraddr2str(clp->cl_rpcclient,
4088 RPC_DISPLAY_NETID));
4090 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4091 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4092 clp->cl_ipaddr, port >> 8, port & 255);
4094 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4095 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4096 setclientid.sc_name_len, setclientid.sc_name);
4097 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4098 dprintk("NFS reply setclientid: %d\n", status);
4103 * nfs4_proc_setclientid_confirm - Confirm client ID
4104 * @clp: state data structure
4105 * @res: result of a previous SETCLIENTID
4106 * @cred: RPC credential to use for this call
4108 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4110 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4111 struct nfs4_setclientid_res *arg,
4112 struct rpc_cred *cred)
4114 struct nfs_fsinfo fsinfo;
4115 struct rpc_message msg = {
4116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4118 .rpc_resp = &fsinfo,
4124 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4125 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4128 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4130 spin_lock(&clp->cl_lock);
4131 clp->cl_lease_time = fsinfo.lease_time * HZ;
4132 clp->cl_last_renewal = now;
4133 spin_unlock(&clp->cl_lock);
4135 dprintk("NFS reply setclientid_confirm: %d\n", status);
4139 struct nfs4_delegreturndata {
4140 struct nfs4_delegreturnargs args;
4141 struct nfs4_delegreturnres res;
4143 nfs4_stateid stateid;
4144 unsigned long timestamp;
4145 struct nfs_fattr fattr;
4149 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4151 struct nfs4_delegreturndata *data = calldata;
4153 if (!nfs4_sequence_done(task, &data->res.seq_res))
4156 switch (task->tk_status) {
4157 case -NFS4ERR_STALE_STATEID:
4158 case -NFS4ERR_EXPIRED:
4160 renew_lease(data->res.server, data->timestamp);
4163 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4165 rpc_restart_call_prepare(task);
4169 data->rpc_status = task->tk_status;
4172 static void nfs4_delegreturn_release(void *calldata)
4177 #if defined(CONFIG_NFS_V4_1)
4178 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4180 struct nfs4_delegreturndata *d_data;
4182 d_data = (struct nfs4_delegreturndata *)data;
4184 nfs4_setup_sequence(d_data->res.server,
4185 &d_data->args.seq_args,
4186 &d_data->res.seq_res,
4189 #endif /* CONFIG_NFS_V4_1 */
4191 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4192 #if defined(CONFIG_NFS_V4_1)
4193 .rpc_call_prepare = nfs4_delegreturn_prepare,
4194 #endif /* CONFIG_NFS_V4_1 */
4195 .rpc_call_done = nfs4_delegreturn_done,
4196 .rpc_release = nfs4_delegreturn_release,
4199 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4201 struct nfs4_delegreturndata *data;
4202 struct nfs_server *server = NFS_SERVER(inode);
4203 struct rpc_task *task;
4204 struct rpc_message msg = {
4205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4208 struct rpc_task_setup task_setup_data = {
4209 .rpc_client = server->client,
4210 .rpc_message = &msg,
4211 .callback_ops = &nfs4_delegreturn_ops,
4212 .flags = RPC_TASK_ASYNC,
4216 data = kzalloc(sizeof(*data), GFP_NOFS);
4219 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4220 data->args.fhandle = &data->fh;
4221 data->args.stateid = &data->stateid;
4222 data->args.bitmask = server->cache_consistency_bitmask;
4223 nfs_copy_fh(&data->fh, NFS_FH(inode));
4224 nfs4_stateid_copy(&data->stateid, stateid);
4225 data->res.fattr = &data->fattr;
4226 data->res.server = server;
4227 nfs_fattr_init(data->res.fattr);
4228 data->timestamp = jiffies;
4229 data->rpc_status = 0;
4231 task_setup_data.callback_data = data;
4232 msg.rpc_argp = &data->args;
4233 msg.rpc_resp = &data->res;
4234 task = rpc_run_task(&task_setup_data);
4236 return PTR_ERR(task);
4239 status = nfs4_wait_for_completion_rpc_task(task);
4242 status = data->rpc_status;
4244 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4246 nfs_refresh_inode(inode, &data->fattr);
4252 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4254 struct nfs_server *server = NFS_SERVER(inode);
4255 struct nfs4_exception exception = { };
4258 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4260 case -NFS4ERR_STALE_STATEID:
4261 case -NFS4ERR_EXPIRED:
4265 err = nfs4_handle_exception(server, err, &exception);
4266 } while (exception.retry);
4270 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4271 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4274 * sleep, with exponential backoff, and retry the LOCK operation.
4276 static unsigned long
4277 nfs4_set_lock_task_retry(unsigned long timeout)
4279 freezable_schedule_timeout_killable(timeout);
4281 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4282 return NFS4_LOCK_MAXTIMEOUT;
4286 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4288 struct inode *inode = state->inode;
4289 struct nfs_server *server = NFS_SERVER(inode);
4290 struct nfs_client *clp = server->nfs_client;
4291 struct nfs_lockt_args arg = {
4292 .fh = NFS_FH(inode),
4295 struct nfs_lockt_res res = {
4298 struct rpc_message msg = {
4299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4302 .rpc_cred = state->owner->so_cred,
4304 struct nfs4_lock_state *lsp;
4307 arg.lock_owner.clientid = clp->cl_clientid;
4308 status = nfs4_set_lock_state(state, request);
4311 lsp = request->fl_u.nfs4_fl.owner;
4312 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4313 arg.lock_owner.s_dev = server->s_dev;
4314 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4317 request->fl_type = F_UNLCK;
4319 case -NFS4ERR_DENIED:
4322 request->fl_ops->fl_release_private(request);
4327 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4329 struct nfs4_exception exception = { };
4333 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4334 _nfs4_proc_getlk(state, cmd, request),
4336 } while (exception.retry);
4340 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4343 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4345 res = posix_lock_file_wait(file, fl);
4348 res = flock_lock_file_wait(file, fl);
4356 struct nfs4_unlockdata {
4357 struct nfs_locku_args arg;
4358 struct nfs_locku_res res;
4359 struct nfs4_lock_state *lsp;
4360 struct nfs_open_context *ctx;
4361 struct file_lock fl;
4362 const struct nfs_server *server;
4363 unsigned long timestamp;
4366 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4367 struct nfs_open_context *ctx,
4368 struct nfs4_lock_state *lsp,
4369 struct nfs_seqid *seqid)
4371 struct nfs4_unlockdata *p;
4372 struct inode *inode = lsp->ls_state->inode;
4374 p = kzalloc(sizeof(*p), GFP_NOFS);
4377 p->arg.fh = NFS_FH(inode);
4379 p->arg.seqid = seqid;
4380 p->res.seqid = seqid;
4381 p->arg.stateid = &lsp->ls_stateid;
4383 atomic_inc(&lsp->ls_count);
4384 /* Ensure we don't close file until we're done freeing locks! */
4385 p->ctx = get_nfs_open_context(ctx);
4386 memcpy(&p->fl, fl, sizeof(p->fl));
4387 p->server = NFS_SERVER(inode);
4391 static void nfs4_locku_release_calldata(void *data)
4393 struct nfs4_unlockdata *calldata = data;
4394 nfs_free_seqid(calldata->arg.seqid);
4395 nfs4_put_lock_state(calldata->lsp);
4396 put_nfs_open_context(calldata->ctx);
4400 static void nfs4_locku_done(struct rpc_task *task, void *data)
4402 struct nfs4_unlockdata *calldata = data;
4404 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4406 switch (task->tk_status) {
4408 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4409 &calldata->res.stateid);
4410 renew_lease(calldata->server, calldata->timestamp);
4412 case -NFS4ERR_BAD_STATEID:
4413 case -NFS4ERR_OLD_STATEID:
4414 case -NFS4ERR_STALE_STATEID:
4415 case -NFS4ERR_EXPIRED:
4418 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4419 rpc_restart_call_prepare(task);
4421 nfs_release_seqid(calldata->arg.seqid);
4424 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4426 struct nfs4_unlockdata *calldata = data;
4428 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4430 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4431 /* Note: exit _without_ running nfs4_locku_done */
4432 task->tk_action = NULL;
4433 nfs4_sequence_done(task, &calldata->res.seq_res);
4436 calldata->timestamp = jiffies;
4437 if (nfs4_setup_sequence(calldata->server,
4438 &calldata->arg.seq_args,
4439 &calldata->res.seq_res,
4441 nfs_release_seqid(calldata->arg.seqid);
4444 static const struct rpc_call_ops nfs4_locku_ops = {
4445 .rpc_call_prepare = nfs4_locku_prepare,
4446 .rpc_call_done = nfs4_locku_done,
4447 .rpc_release = nfs4_locku_release_calldata,
4450 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4451 struct nfs_open_context *ctx,
4452 struct nfs4_lock_state *lsp,
4453 struct nfs_seqid *seqid)
4455 struct nfs4_unlockdata *data;
4456 struct rpc_message msg = {
4457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4458 .rpc_cred = ctx->cred,
4460 struct rpc_task_setup task_setup_data = {
4461 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4462 .rpc_message = &msg,
4463 .callback_ops = &nfs4_locku_ops,
4464 .workqueue = nfsiod_workqueue,
4465 .flags = RPC_TASK_ASYNC,
4468 /* Ensure this is an unlock - when canceling a lock, the
4469 * canceled lock is passed in, and it won't be an unlock.
4471 fl->fl_type = F_UNLCK;
4473 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4475 nfs_free_seqid(seqid);
4476 return ERR_PTR(-ENOMEM);
4479 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4480 msg.rpc_argp = &data->arg;
4481 msg.rpc_resp = &data->res;
4482 task_setup_data.callback_data = data;
4483 return rpc_run_task(&task_setup_data);
4486 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4488 struct nfs_inode *nfsi = NFS_I(state->inode);
4489 struct nfs_seqid *seqid;
4490 struct nfs4_lock_state *lsp;
4491 struct rpc_task *task;
4493 unsigned char fl_flags = request->fl_flags;
4495 status = nfs4_set_lock_state(state, request);
4496 /* Unlock _before_ we do the RPC call */
4497 request->fl_flags |= FL_EXISTS;
4498 down_read(&nfsi->rwsem);
4499 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4500 up_read(&nfsi->rwsem);
4503 up_read(&nfsi->rwsem);
4506 /* Is this a delegated lock? */
4507 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4509 lsp = request->fl_u.nfs4_fl.owner;
4510 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4514 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4515 status = PTR_ERR(task);
4518 status = nfs4_wait_for_completion_rpc_task(task);
4521 request->fl_flags = fl_flags;
4525 struct nfs4_lockdata {
4526 struct nfs_lock_args arg;
4527 struct nfs_lock_res res;
4528 struct nfs4_lock_state *lsp;
4529 struct nfs_open_context *ctx;
4530 struct file_lock fl;
4531 unsigned long timestamp;
4534 struct nfs_server *server;
4537 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4538 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4541 struct nfs4_lockdata *p;
4542 struct inode *inode = lsp->ls_state->inode;
4543 struct nfs_server *server = NFS_SERVER(inode);
4545 p = kzalloc(sizeof(*p), gfp_mask);
4549 p->arg.fh = NFS_FH(inode);
4551 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4552 if (p->arg.open_seqid == NULL)
4554 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4555 if (p->arg.lock_seqid == NULL)
4556 goto out_free_seqid;
4557 p->arg.lock_stateid = &lsp->ls_stateid;
4558 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4559 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4560 p->arg.lock_owner.s_dev = server->s_dev;
4561 p->res.lock_seqid = p->arg.lock_seqid;
4564 atomic_inc(&lsp->ls_count);
4565 p->ctx = get_nfs_open_context(ctx);
4566 memcpy(&p->fl, fl, sizeof(p->fl));
4569 nfs_free_seqid(p->arg.open_seqid);
4575 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4577 struct nfs4_lockdata *data = calldata;
4578 struct nfs4_state *state = data->lsp->ls_state;
4580 dprintk("%s: begin!\n", __func__);
4581 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4583 /* Do we need to do an open_to_lock_owner? */
4584 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4585 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4586 goto out_release_lock_seqid;
4588 data->arg.open_stateid = &state->stateid;
4589 data->arg.new_lock_owner = 1;
4590 data->res.open_seqid = data->arg.open_seqid;
4592 data->arg.new_lock_owner = 0;
4593 data->timestamp = jiffies;
4594 if (nfs4_setup_sequence(data->server,
4595 &data->arg.seq_args,
4599 nfs_release_seqid(data->arg.open_seqid);
4600 out_release_lock_seqid:
4601 nfs_release_seqid(data->arg.lock_seqid);
4602 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4605 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4607 struct nfs4_lockdata *data = calldata;
4609 dprintk("%s: begin!\n", __func__);
4611 if (!nfs4_sequence_done(task, &data->res.seq_res))
4614 data->rpc_status = task->tk_status;
4615 if (data->arg.new_lock_owner != 0) {
4616 if (data->rpc_status == 0)
4617 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4621 if (data->rpc_status == 0) {
4622 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4623 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4624 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4627 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4630 static void nfs4_lock_release(void *calldata)
4632 struct nfs4_lockdata *data = calldata;
4634 dprintk("%s: begin!\n", __func__);
4635 nfs_free_seqid(data->arg.open_seqid);
4636 if (data->cancelled != 0) {
4637 struct rpc_task *task;
4638 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4639 data->arg.lock_seqid);
4641 rpc_put_task_async(task);
4642 dprintk("%s: cancelling lock!\n", __func__);
4644 nfs_free_seqid(data->arg.lock_seqid);
4645 nfs4_put_lock_state(data->lsp);
4646 put_nfs_open_context(data->ctx);
4648 dprintk("%s: done!\n", __func__);
4651 static const struct rpc_call_ops nfs4_lock_ops = {
4652 .rpc_call_prepare = nfs4_lock_prepare,
4653 .rpc_call_done = nfs4_lock_done,
4654 .rpc_release = nfs4_lock_release,
4657 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4660 case -NFS4ERR_ADMIN_REVOKED:
4661 case -NFS4ERR_BAD_STATEID:
4662 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4663 if (new_lock_owner != 0 ||
4664 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4665 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4667 case -NFS4ERR_STALE_STATEID:
4668 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4669 case -NFS4ERR_EXPIRED:
4670 nfs4_schedule_lease_recovery(server->nfs_client);
4674 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4676 struct nfs4_lockdata *data;
4677 struct rpc_task *task;
4678 struct rpc_message msg = {
4679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4680 .rpc_cred = state->owner->so_cred,
4682 struct rpc_task_setup task_setup_data = {
4683 .rpc_client = NFS_CLIENT(state->inode),
4684 .rpc_message = &msg,
4685 .callback_ops = &nfs4_lock_ops,
4686 .workqueue = nfsiod_workqueue,
4687 .flags = RPC_TASK_ASYNC,
4691 dprintk("%s: begin!\n", __func__);
4692 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4693 fl->fl_u.nfs4_fl.owner,
4694 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4698 data->arg.block = 1;
4699 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4700 msg.rpc_argp = &data->arg;
4701 msg.rpc_resp = &data->res;
4702 task_setup_data.callback_data = data;
4703 if (recovery_type > NFS_LOCK_NEW) {
4704 if (recovery_type == NFS_LOCK_RECLAIM)
4705 data->arg.reclaim = NFS_LOCK_RECLAIM;
4706 nfs4_set_sequence_privileged(&data->arg.seq_args);
4708 task = rpc_run_task(&task_setup_data);
4710 return PTR_ERR(task);
4711 ret = nfs4_wait_for_completion_rpc_task(task);
4713 ret = data->rpc_status;
4715 nfs4_handle_setlk_error(data->server, data->lsp,
4716 data->arg.new_lock_owner, ret);
4718 data->cancelled = 1;
4720 dprintk("%s: done, ret = %d!\n", __func__, ret);
4724 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4726 struct nfs_server *server = NFS_SERVER(state->inode);
4727 struct nfs4_exception exception = {
4728 .inode = state->inode,
4733 /* Cache the lock if possible... */
4734 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4736 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4737 if (err != -NFS4ERR_DELAY)
4739 nfs4_handle_exception(server, err, &exception);
4740 } while (exception.retry);
4744 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4746 struct nfs_server *server = NFS_SERVER(state->inode);
4747 struct nfs4_exception exception = {
4748 .inode = state->inode,
4752 err = nfs4_set_lock_state(state, request);
4756 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4758 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4762 case -NFS4ERR_GRACE:
4763 case -NFS4ERR_DELAY:
4764 nfs4_handle_exception(server, err, &exception);
4767 } while (exception.retry);
4772 #if defined(CONFIG_NFS_V4_1)
4774 * nfs41_check_expired_locks - possibly free a lock stateid
4776 * @state: NFSv4 state for an inode
4778 * Returns NFS_OK if recovery for this stateid is now finished.
4779 * Otherwise a negative NFS4ERR value is returned.
4781 static int nfs41_check_expired_locks(struct nfs4_state *state)
4783 int status, ret = -NFS4ERR_BAD_STATEID;
4784 struct nfs4_lock_state *lsp;
4785 struct nfs_server *server = NFS_SERVER(state->inode);
4787 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4788 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4789 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4790 if (status != NFS_OK) {
4791 /* Free the stateid unless the server
4792 * informs us the stateid is unrecognized. */
4793 if (status != -NFS4ERR_BAD_STATEID)
4794 nfs41_free_stateid(server,
4796 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4805 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4807 int status = NFS_OK;
4809 if (test_bit(LK_STATE_IN_USE, &state->flags))
4810 status = nfs41_check_expired_locks(state);
4811 if (status != NFS_OK)
4812 status = nfs4_lock_expired(state, request);
4817 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4819 struct nfs4_state_owner *sp = state->owner;
4820 struct nfs_inode *nfsi = NFS_I(state->inode);
4821 unsigned char fl_flags = request->fl_flags;
4823 int status = -ENOLCK;
4825 if ((fl_flags & FL_POSIX) &&
4826 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4828 /* Is this a delegated open? */
4829 status = nfs4_set_lock_state(state, request);
4832 request->fl_flags |= FL_ACCESS;
4833 status = do_vfs_lock(request->fl_file, request);
4836 down_read(&nfsi->rwsem);
4837 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4838 /* Yes: cache locks! */
4839 /* ...but avoid races with delegation recall... */
4840 request->fl_flags = fl_flags & ~FL_SLEEP;
4841 status = do_vfs_lock(request->fl_file, request);
4844 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4845 up_read(&nfsi->rwsem);
4846 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4849 down_read(&nfsi->rwsem);
4850 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4851 status = -NFS4ERR_DELAY;
4854 /* Note: we always want to sleep here! */
4855 request->fl_flags = fl_flags | FL_SLEEP;
4856 if (do_vfs_lock(request->fl_file, request) < 0)
4857 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4858 "manager!\n", __func__);
4860 up_read(&nfsi->rwsem);
4862 request->fl_flags = fl_flags;
4866 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4868 struct nfs4_exception exception = {
4870 .inode = state->inode,
4875 err = _nfs4_proc_setlk(state, cmd, request);
4876 if (err == -NFS4ERR_DENIED)
4878 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4880 } while (exception.retry);
4885 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4887 struct nfs_open_context *ctx;
4888 struct nfs4_state *state;
4889 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4892 /* verify open state */
4893 ctx = nfs_file_open_context(filp);
4896 if (request->fl_start < 0 || request->fl_end < 0)
4899 if (IS_GETLK(cmd)) {
4901 return nfs4_proc_getlk(state, F_GETLK, request);
4905 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4908 if (request->fl_type == F_UNLCK) {
4910 return nfs4_proc_unlck(state, cmd, request);
4917 * Don't rely on the VFS having checked the file open mode,
4918 * since it won't do this for flock() locks.
4920 switch (request->fl_type) {
4922 if (!(filp->f_mode & FMODE_READ))
4926 if (!(filp->f_mode & FMODE_WRITE))
4931 status = nfs4_proc_setlk(state, cmd, request);
4932 if ((status != -EAGAIN) || IS_SETLK(cmd))
4934 timeout = nfs4_set_lock_task_retry(timeout);
4935 status = -ERESTARTSYS;
4938 } while(status < 0);
4942 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4944 struct nfs_server *server = NFS_SERVER(state->inode);
4945 struct nfs4_exception exception = { };
4948 err = nfs4_set_lock_state(state, fl);
4952 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4955 printk(KERN_ERR "NFS: %s: unhandled error "
4956 "%d.\n", __func__, err);
4960 case -NFS4ERR_STALE_CLIENTID:
4961 case -NFS4ERR_STALE_STATEID:
4962 set_bit(NFS_DELEGATED_STATE, &state->flags);
4963 case -NFS4ERR_EXPIRED:
4964 nfs4_schedule_lease_recovery(server->nfs_client);
4967 case -NFS4ERR_BADSESSION:
4968 case -NFS4ERR_BADSLOT:
4969 case -NFS4ERR_BAD_HIGH_SLOT:
4970 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4971 case -NFS4ERR_DEADSESSION:
4972 set_bit(NFS_DELEGATED_STATE, &state->flags);
4973 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
4976 case -NFS4ERR_DELEG_REVOKED:
4977 case -NFS4ERR_ADMIN_REVOKED:
4978 case -NFS4ERR_BAD_STATEID:
4979 case -NFS4ERR_OPENMODE:
4980 nfs4_schedule_stateid_recovery(server, state);
4984 case -NFS4ERR_DENIED:
4985 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4989 set_bit(NFS_DELEGATED_STATE, &state->flags);
4990 err = nfs4_handle_exception(server, err, &exception);
4991 } while (exception.retry);
4996 struct nfs_release_lockowner_data {
4997 struct nfs4_lock_state *lsp;
4998 struct nfs_server *server;
4999 struct nfs_release_lockowner_args args;
5002 static void nfs4_release_lockowner_release(void *calldata)
5004 struct nfs_release_lockowner_data *data = calldata;
5005 nfs4_free_lock_state(data->server, data->lsp);
5009 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5010 .rpc_release = nfs4_release_lockowner_release,
5013 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5015 struct nfs_server *server = lsp->ls_state->owner->so_server;
5016 struct nfs_release_lockowner_data *data;
5017 struct rpc_message msg = {
5018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5021 if (server->nfs_client->cl_mvops->minor_version != 0)
5023 data = kmalloc(sizeof(*data), GFP_NOFS);
5027 data->server = server;
5028 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5029 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5030 data->args.lock_owner.s_dev = server->s_dev;
5031 msg.rpc_argp = &data->args;
5032 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5036 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5038 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5039 const void *buf, size_t buflen,
5040 int flags, int type)
5042 if (strcmp(key, "") != 0)
5045 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5048 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5049 void *buf, size_t buflen, int type)
5051 if (strcmp(key, "") != 0)
5054 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5057 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5058 size_t list_len, const char *name,
5059 size_t name_len, int type)
5061 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5063 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5066 if (list && len <= list_len)
5067 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5072 * nfs_fhget will use either the mounted_on_fileid or the fileid
5074 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5076 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5077 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5078 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5079 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5082 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5083 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5084 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5088 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5089 const struct qstr *name,
5090 struct nfs4_fs_locations *fs_locations,
5093 struct nfs_server *server = NFS_SERVER(dir);
5095 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5097 struct nfs4_fs_locations_arg args = {
5098 .dir_fh = NFS_FH(dir),
5103 struct nfs4_fs_locations_res res = {
5104 .fs_locations = fs_locations,
5106 struct rpc_message msg = {
5107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5113 dprintk("%s: start\n", __func__);
5115 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5116 * is not supported */
5117 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5118 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5120 bitmask[0] |= FATTR4_WORD0_FILEID;
5122 nfs_fattr_init(&fs_locations->fattr);
5123 fs_locations->server = server;
5124 fs_locations->nlocations = 0;
5125 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5126 dprintk("%s: returned status = %d\n", __func__, status);
5130 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5131 const struct qstr *name,
5132 struct nfs4_fs_locations *fs_locations,
5135 struct nfs4_exception exception = { };
5138 err = nfs4_handle_exception(NFS_SERVER(dir),
5139 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5141 } while (exception.retry);
5145 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5148 struct nfs4_secinfo_arg args = {
5149 .dir_fh = NFS_FH(dir),
5152 struct nfs4_secinfo_res res = {
5155 struct rpc_message msg = {
5156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5161 dprintk("NFS call secinfo %s\n", name->name);
5162 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5163 dprintk("NFS reply secinfo: %d\n", status);
5167 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5168 struct nfs4_secinfo_flavors *flavors)
5170 struct nfs4_exception exception = { };
5173 err = nfs4_handle_exception(NFS_SERVER(dir),
5174 _nfs4_proc_secinfo(dir, name, flavors),
5176 } while (exception.retry);
5180 #ifdef CONFIG_NFS_V4_1
5182 * Check the exchange flags returned by the server for invalid flags, having
5183 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5186 static int nfs4_check_cl_exchange_flags(u32 flags)
5188 if (flags & ~EXCHGID4_FLAG_MASK_R)
5190 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5191 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5193 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5197 return -NFS4ERR_INVAL;
5201 nfs41_same_server_scope(struct nfs41_server_scope *a,
5202 struct nfs41_server_scope *b)
5204 if (a->server_scope_sz == b->server_scope_sz &&
5205 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5212 * nfs4_proc_bind_conn_to_session()
5214 * The 4.1 client currently uses the same TCP connection for the
5215 * fore and backchannel.
5217 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5220 struct nfs41_bind_conn_to_session_res res;
5221 struct rpc_message msg = {
5223 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5229 dprintk("--> %s\n", __func__);
5231 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5232 if (unlikely(res.session == NULL)) {
5237 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5239 if (memcmp(res.session->sess_id.data,
5240 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5241 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5245 if (res.dir != NFS4_CDFS4_BOTH) {
5246 dprintk("NFS: %s: Unexpected direction from server\n",
5251 if (res.use_conn_in_rdma_mode) {
5252 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5261 dprintk("<-- %s status= %d\n", __func__, status);
5266 * nfs4_proc_exchange_id()
5268 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5270 * Since the clientid has expired, all compounds using sessions
5271 * associated with the stale clientid will be returning
5272 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5273 * be in some phase of session reset.
5275 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5277 nfs4_verifier verifier;
5278 struct nfs41_exchange_id_args args = {
5279 .verifier = &verifier,
5281 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5283 struct nfs41_exchange_id_res res = {
5287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5294 nfs4_init_boot_verifier(clp, &verifier);
5295 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5297 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5298 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5299 args.id_len, args.id);
5301 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5303 if (unlikely(res.server_owner == NULL)) {
5308 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5310 if (unlikely(res.server_scope == NULL)) {
5312 goto out_server_owner;
5315 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5316 if (unlikely(res.impl_id == NULL)) {
5318 goto out_server_scope;
5321 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5323 status = nfs4_check_cl_exchange_flags(res.flags);
5326 clp->cl_clientid = res.clientid;
5327 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5328 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5329 clp->cl_seqid = res.seqid;
5331 kfree(clp->cl_serverowner);
5332 clp->cl_serverowner = res.server_owner;
5333 res.server_owner = NULL;
5335 /* use the most recent implementation id */
5336 kfree(clp->cl_implid);
5337 clp->cl_implid = res.impl_id;
5339 if (clp->cl_serverscope != NULL &&
5340 !nfs41_same_server_scope(clp->cl_serverscope,
5341 res.server_scope)) {
5342 dprintk("%s: server_scope mismatch detected\n",
5344 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5345 kfree(clp->cl_serverscope);
5346 clp->cl_serverscope = NULL;
5349 if (clp->cl_serverscope == NULL) {
5350 clp->cl_serverscope = res.server_scope;
5357 kfree(res.server_owner);
5359 kfree(res.server_scope);
5361 if (clp->cl_implid != NULL)
5362 dprintk("NFS reply exchange_id: Server Implementation ID: "
5363 "domain: %s, name: %s, date: %llu,%u\n",
5364 clp->cl_implid->domain, clp->cl_implid->name,
5365 clp->cl_implid->date.seconds,
5366 clp->cl_implid->date.nseconds);
5367 dprintk("NFS reply exchange_id: %d\n", status);
5371 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5372 struct rpc_cred *cred)
5374 struct rpc_message msg = {
5375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5381 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5383 dprintk("NFS: Got error %d from the server %s on "
5384 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5388 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5389 struct rpc_cred *cred)
5394 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5395 ret = _nfs4_proc_destroy_clientid(clp, cred);
5397 case -NFS4ERR_DELAY:
5398 case -NFS4ERR_CLIENTID_BUSY:
5408 int nfs4_destroy_clientid(struct nfs_client *clp)
5410 struct rpc_cred *cred;
5413 if (clp->cl_mvops->minor_version < 1)
5415 if (clp->cl_exchange_flags == 0)
5417 if (clp->cl_preserve_clid)
5419 cred = nfs4_get_exchange_id_cred(clp);
5420 ret = nfs4_proc_destroy_clientid(clp, cred);
5425 case -NFS4ERR_STALE_CLIENTID:
5426 clp->cl_exchange_flags = 0;
5432 struct nfs4_get_lease_time_data {
5433 struct nfs4_get_lease_time_args *args;
5434 struct nfs4_get_lease_time_res *res;
5435 struct nfs_client *clp;
5438 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5441 struct nfs4_get_lease_time_data *data =
5442 (struct nfs4_get_lease_time_data *)calldata;
5444 dprintk("--> %s\n", __func__);
5445 /* just setup sequence, do not trigger session recovery
5446 since we're invoked within one */
5447 nfs41_setup_sequence(data->clp->cl_session,
5448 &data->args->la_seq_args,
5449 &data->res->lr_seq_res,
5451 dprintk("<-- %s\n", __func__);
5455 * Called from nfs4_state_manager thread for session setup, so don't recover
5456 * from sequence operation or clientid errors.
5458 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5460 struct nfs4_get_lease_time_data *data =
5461 (struct nfs4_get_lease_time_data *)calldata;
5463 dprintk("--> %s\n", __func__);
5464 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5466 switch (task->tk_status) {
5467 case -NFS4ERR_DELAY:
5468 case -NFS4ERR_GRACE:
5469 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5470 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5471 task->tk_status = 0;
5473 case -NFS4ERR_RETRY_UNCACHED_REP:
5474 rpc_restart_call_prepare(task);
5477 dprintk("<-- %s\n", __func__);
5480 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5481 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5482 .rpc_call_done = nfs4_get_lease_time_done,
5485 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5487 struct rpc_task *task;
5488 struct nfs4_get_lease_time_args args;
5489 struct nfs4_get_lease_time_res res = {
5490 .lr_fsinfo = fsinfo,
5492 struct nfs4_get_lease_time_data data = {
5497 struct rpc_message msg = {
5498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5502 struct rpc_task_setup task_setup = {
5503 .rpc_client = clp->cl_rpcclient,
5504 .rpc_message = &msg,
5505 .callback_ops = &nfs4_get_lease_time_ops,
5506 .callback_data = &data,
5507 .flags = RPC_TASK_TIMEOUT,
5511 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5512 nfs4_set_sequence_privileged(&args.la_seq_args);
5513 dprintk("--> %s\n", __func__);
5514 task = rpc_run_task(&task_setup);
5517 status = PTR_ERR(task);
5519 status = task->tk_status;
5522 dprintk("<-- %s return %d\n", __func__, status);
5528 * Initialize the values to be used by the client in CREATE_SESSION
5529 * If nfs4_init_session set the fore channel request and response sizes,
5532 * Set the back channel max_resp_sz_cached to zero to force the client to
5533 * always set csa_cachethis to FALSE because the current implementation
5534 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5536 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5538 struct nfs4_session *session = args->client->cl_session;
5539 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5540 mxresp_sz = session->fc_target_max_resp_sz;
5543 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5545 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5546 /* Fore channel attributes */
5547 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5548 args->fc_attrs.max_resp_sz = mxresp_sz;
5549 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5550 args->fc_attrs.max_reqs = max_session_slots;
5552 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5553 "max_ops=%u max_reqs=%u\n",
5555 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5556 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5558 /* Back channel attributes */
5559 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5560 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5561 args->bc_attrs.max_resp_sz_cached = 0;
5562 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5563 args->bc_attrs.max_reqs = 1;
5565 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5566 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5568 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5569 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5570 args->bc_attrs.max_reqs);
5573 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5575 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5576 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5578 if (rcvd->max_resp_sz > sent->max_resp_sz)
5581 * Our requested max_ops is the minimum we need; we're not
5582 * prepared to break up compounds into smaller pieces than that.
5583 * So, no point even trying to continue if the server won't
5586 if (rcvd->max_ops < sent->max_ops)
5588 if (rcvd->max_reqs == 0)
5590 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5591 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5595 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5597 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5598 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5600 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5602 if (rcvd->max_resp_sz < sent->max_resp_sz)
5604 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5606 /* These would render the backchannel useless: */
5607 if (rcvd->max_ops != sent->max_ops)
5609 if (rcvd->max_reqs != sent->max_reqs)
5614 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5615 struct nfs4_session *session)
5619 ret = nfs4_verify_fore_channel_attrs(args, session);
5622 return nfs4_verify_back_channel_attrs(args, session);
5625 static int _nfs4_proc_create_session(struct nfs_client *clp,
5626 struct rpc_cred *cred)
5628 struct nfs4_session *session = clp->cl_session;
5629 struct nfs41_create_session_args args = {
5631 .cb_program = NFS4_CALLBACK,
5633 struct nfs41_create_session_res res = {
5636 struct rpc_message msg = {
5637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5644 nfs4_init_channel_attrs(&args);
5645 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5647 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5650 /* Verify the session's negotiated channel_attrs values */
5651 status = nfs4_verify_channel_attrs(&args, session);
5652 /* Increment the clientid slot sequence id */
5660 * Issues a CREATE_SESSION operation to the server.
5661 * It is the responsibility of the caller to verify the session is
5662 * expired before calling this routine.
5664 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5668 struct nfs4_session *session = clp->cl_session;
5670 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5672 status = _nfs4_proc_create_session(clp, cred);
5676 /* Init or reset the session slot tables */
5677 status = nfs4_setup_session_slot_tables(session);
5678 dprintk("slot table setup returned %d\n", status);
5682 ptr = (unsigned *)&session->sess_id.data[0];
5683 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5684 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5686 dprintk("<-- %s\n", __func__);
5691 * Issue the over-the-wire RPC DESTROY_SESSION.
5692 * The caller must serialize access to this routine.
5694 int nfs4_proc_destroy_session(struct nfs4_session *session,
5695 struct rpc_cred *cred)
5697 struct rpc_message msg = {
5698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5699 .rpc_argp = session,
5704 dprintk("--> nfs4_proc_destroy_session\n");
5706 /* session is still being setup */
5707 if (session->clp->cl_cons_state != NFS_CS_READY)
5710 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5713 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5714 "Session has been destroyed regardless...\n", status);
5716 dprintk("<-- nfs4_proc_destroy_session\n");
5721 * Renew the cl_session lease.
5723 struct nfs4_sequence_data {
5724 struct nfs_client *clp;
5725 struct nfs4_sequence_args args;
5726 struct nfs4_sequence_res res;
5729 static void nfs41_sequence_release(void *data)
5731 struct nfs4_sequence_data *calldata = data;
5732 struct nfs_client *clp = calldata->clp;
5734 if (atomic_read(&clp->cl_count) > 1)
5735 nfs4_schedule_state_renewal(clp);
5736 nfs_put_client(clp);
5740 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5742 switch(task->tk_status) {
5743 case -NFS4ERR_DELAY:
5744 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5747 nfs4_schedule_lease_recovery(clp);
5752 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5754 struct nfs4_sequence_data *calldata = data;
5755 struct nfs_client *clp = calldata->clp;
5757 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5760 if (task->tk_status < 0) {
5761 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5762 if (atomic_read(&clp->cl_count) == 1)
5765 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5766 rpc_restart_call_prepare(task);
5770 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5772 dprintk("<-- %s\n", __func__);
5775 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5777 struct nfs4_sequence_data *calldata = data;
5778 struct nfs_client *clp = calldata->clp;
5779 struct nfs4_sequence_args *args;
5780 struct nfs4_sequence_res *res;
5782 args = task->tk_msg.rpc_argp;
5783 res = task->tk_msg.rpc_resp;
5785 nfs41_setup_sequence(clp->cl_session, args, res, task);
5788 static const struct rpc_call_ops nfs41_sequence_ops = {
5789 .rpc_call_done = nfs41_sequence_call_done,
5790 .rpc_call_prepare = nfs41_sequence_prepare,
5791 .rpc_release = nfs41_sequence_release,
5794 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5795 struct rpc_cred *cred,
5798 struct nfs4_sequence_data *calldata;
5799 struct rpc_message msg = {
5800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5803 struct rpc_task_setup task_setup_data = {
5804 .rpc_client = clp->cl_rpcclient,
5805 .rpc_message = &msg,
5806 .callback_ops = &nfs41_sequence_ops,
5807 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5810 if (!atomic_inc_not_zero(&clp->cl_count))
5811 return ERR_PTR(-EIO);
5812 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5813 if (calldata == NULL) {
5814 nfs_put_client(clp);
5815 return ERR_PTR(-ENOMEM);
5817 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5819 nfs4_set_sequence_privileged(&calldata->args);
5820 msg.rpc_argp = &calldata->args;
5821 msg.rpc_resp = &calldata->res;
5822 calldata->clp = clp;
5823 task_setup_data.callback_data = calldata;
5825 return rpc_run_task(&task_setup_data);
5828 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5830 struct rpc_task *task;
5833 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5835 task = _nfs41_proc_sequence(clp, cred, false);
5837 ret = PTR_ERR(task);
5839 rpc_put_task_async(task);
5840 dprintk("<-- %s status=%d\n", __func__, ret);
5844 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5846 struct rpc_task *task;
5849 task = _nfs41_proc_sequence(clp, cred, true);
5851 ret = PTR_ERR(task);
5854 ret = rpc_wait_for_completion_task(task);
5856 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5858 if (task->tk_status == 0)
5859 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5860 ret = task->tk_status;
5864 dprintk("<-- %s status=%d\n", __func__, ret);
5868 struct nfs4_reclaim_complete_data {
5869 struct nfs_client *clp;
5870 struct nfs41_reclaim_complete_args arg;
5871 struct nfs41_reclaim_complete_res res;
5874 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5876 struct nfs4_reclaim_complete_data *calldata = data;
5878 nfs41_setup_sequence(calldata->clp->cl_session,
5879 &calldata->arg.seq_args,
5880 &calldata->res.seq_res,
5884 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5886 switch(task->tk_status) {
5888 case -NFS4ERR_COMPLETE_ALREADY:
5889 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5891 case -NFS4ERR_DELAY:
5892 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5894 case -NFS4ERR_RETRY_UNCACHED_REP:
5897 nfs4_schedule_lease_recovery(clp);
5902 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5904 struct nfs4_reclaim_complete_data *calldata = data;
5905 struct nfs_client *clp = calldata->clp;
5906 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5908 dprintk("--> %s\n", __func__);
5909 if (!nfs41_sequence_done(task, res))
5912 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5913 rpc_restart_call_prepare(task);
5916 dprintk("<-- %s\n", __func__);
5919 static void nfs4_free_reclaim_complete_data(void *data)
5921 struct nfs4_reclaim_complete_data *calldata = data;
5926 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5927 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5928 .rpc_call_done = nfs4_reclaim_complete_done,
5929 .rpc_release = nfs4_free_reclaim_complete_data,
5933 * Issue a global reclaim complete.
5935 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5937 struct nfs4_reclaim_complete_data *calldata;
5938 struct rpc_task *task;
5939 struct rpc_message msg = {
5940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5942 struct rpc_task_setup task_setup_data = {
5943 .rpc_client = clp->cl_rpcclient,
5944 .rpc_message = &msg,
5945 .callback_ops = &nfs4_reclaim_complete_call_ops,
5946 .flags = RPC_TASK_ASYNC,
5948 int status = -ENOMEM;
5950 dprintk("--> %s\n", __func__);
5951 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5952 if (calldata == NULL)
5954 calldata->clp = clp;
5955 calldata->arg.one_fs = 0;
5957 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5958 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
5959 msg.rpc_argp = &calldata->arg;
5960 msg.rpc_resp = &calldata->res;
5961 task_setup_data.callback_data = calldata;
5962 task = rpc_run_task(&task_setup_data);
5964 status = PTR_ERR(task);
5967 status = nfs4_wait_for_completion_rpc_task(task);
5969 status = task->tk_status;
5973 dprintk("<-- %s status=%d\n", __func__, status);
5978 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5980 struct nfs4_layoutget *lgp = calldata;
5981 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5982 struct nfs4_session *session = nfs4_get_session(server);
5984 dprintk("--> %s\n", __func__);
5985 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5986 * right now covering the LAYOUTGET we are about to send.
5987 * However, that is not so catastrophic, and there seems
5988 * to be no way to prevent it completely.
5990 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
5991 &lgp->res.seq_res, task))
5993 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5994 NFS_I(lgp->args.inode)->layout,
5995 lgp->args.ctx->state)) {
5996 rpc_exit(task, NFS4_OK);
6000 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6002 struct nfs4_layoutget *lgp = calldata;
6003 struct inode *inode = lgp->args.inode;
6004 struct nfs_server *server = NFS_SERVER(inode);
6005 struct pnfs_layout_hdr *lo;
6006 struct nfs4_state *state = NULL;
6008 dprintk("--> %s\n", __func__);
6010 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6013 switch (task->tk_status) {
6016 case -NFS4ERR_LAYOUTTRYLATER:
6017 case -NFS4ERR_RECALLCONFLICT:
6018 task->tk_status = -NFS4ERR_DELAY;
6020 case -NFS4ERR_EXPIRED:
6021 case -NFS4ERR_BAD_STATEID:
6022 spin_lock(&inode->i_lock);
6023 lo = NFS_I(inode)->layout;
6024 if (!lo || list_empty(&lo->plh_segs)) {
6025 spin_unlock(&inode->i_lock);
6026 /* If the open stateid was bad, then recover it. */
6027 state = lgp->args.ctx->state;
6031 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6032 spin_unlock(&inode->i_lock);
6033 /* Mark the bad layout state as invalid, then
6034 * retry using the open stateid. */
6035 pnfs_free_lseg_list(&head);
6038 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6039 rpc_restart_call_prepare(task);
6041 dprintk("<-- %s\n", __func__);
6044 static size_t max_response_pages(struct nfs_server *server)
6046 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6047 return nfs_page_array_len(0, max_resp_sz);
6050 static void nfs4_free_pages(struct page **pages, size_t size)
6057 for (i = 0; i < size; i++) {
6060 __free_page(pages[i]);
6065 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6067 struct page **pages;
6070 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6072 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6076 for (i = 0; i < size; i++) {
6077 pages[i] = alloc_page(gfp_flags);
6079 dprintk("%s: failed to allocate page\n", __func__);
6080 nfs4_free_pages(pages, size);
6088 static void nfs4_layoutget_release(void *calldata)
6090 struct nfs4_layoutget *lgp = calldata;
6091 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6092 size_t max_pages = max_response_pages(server);
6094 dprintk("--> %s\n", __func__);
6095 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6096 put_nfs_open_context(lgp->args.ctx);
6098 dprintk("<-- %s\n", __func__);
6101 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6102 .rpc_call_prepare = nfs4_layoutget_prepare,
6103 .rpc_call_done = nfs4_layoutget_done,
6104 .rpc_release = nfs4_layoutget_release,
6107 struct pnfs_layout_segment *
6108 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6110 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6111 size_t max_pages = max_response_pages(server);
6112 struct rpc_task *task;
6113 struct rpc_message msg = {
6114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6115 .rpc_argp = &lgp->args,
6116 .rpc_resp = &lgp->res,
6118 struct rpc_task_setup task_setup_data = {
6119 .rpc_client = server->client,
6120 .rpc_message = &msg,
6121 .callback_ops = &nfs4_layoutget_call_ops,
6122 .callback_data = lgp,
6123 .flags = RPC_TASK_ASYNC,
6125 struct pnfs_layout_segment *lseg = NULL;
6128 dprintk("--> %s\n", __func__);
6130 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6131 if (!lgp->args.layout.pages) {
6132 nfs4_layoutget_release(lgp);
6133 return ERR_PTR(-ENOMEM);
6135 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6137 lgp->res.layoutp = &lgp->args.layout;
6138 lgp->res.seq_res.sr_slot = NULL;
6139 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6140 task = rpc_run_task(&task_setup_data);
6142 return ERR_CAST(task);
6143 status = nfs4_wait_for_completion_rpc_task(task);
6145 status = task->tk_status;
6147 lseg = pnfs_layout_process(lgp);
6149 dprintk("<-- %s status=%d\n", __func__, status);
6151 return ERR_PTR(status);
6156 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6158 struct nfs4_layoutreturn *lrp = calldata;
6160 dprintk("--> %s\n", __func__);
6161 nfs41_setup_sequence(lrp->clp->cl_session,
6162 &lrp->args.seq_args,
6167 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6169 struct nfs4_layoutreturn *lrp = calldata;
6170 struct nfs_server *server;
6172 dprintk("--> %s\n", __func__);
6174 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6177 server = NFS_SERVER(lrp->args.inode);
6178 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6179 rpc_restart_call_prepare(task);
6182 dprintk("<-- %s\n", __func__);
6185 static void nfs4_layoutreturn_release(void *calldata)
6187 struct nfs4_layoutreturn *lrp = calldata;
6188 struct pnfs_layout_hdr *lo = lrp->args.layout;
6190 dprintk("--> %s\n", __func__);
6191 spin_lock(&lo->plh_inode->i_lock);
6192 if (lrp->res.lrs_present)
6193 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6194 lo->plh_block_lgets--;
6195 spin_unlock(&lo->plh_inode->i_lock);
6196 pnfs_put_layout_hdr(lrp->args.layout);
6198 dprintk("<-- %s\n", __func__);
6201 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6202 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6203 .rpc_call_done = nfs4_layoutreturn_done,
6204 .rpc_release = nfs4_layoutreturn_release,
6207 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6209 struct rpc_task *task;
6210 struct rpc_message msg = {
6211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6212 .rpc_argp = &lrp->args,
6213 .rpc_resp = &lrp->res,
6215 struct rpc_task_setup task_setup_data = {
6216 .rpc_client = lrp->clp->cl_rpcclient,
6217 .rpc_message = &msg,
6218 .callback_ops = &nfs4_layoutreturn_call_ops,
6219 .callback_data = lrp,
6223 dprintk("--> %s\n", __func__);
6224 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6225 task = rpc_run_task(&task_setup_data);
6227 return PTR_ERR(task);
6228 status = task->tk_status;
6229 dprintk("<-- %s status=%d\n", __func__, status);
6235 * Retrieve the list of Data Server devices from the MDS.
6237 static int _nfs4_getdevicelist(struct nfs_server *server,
6238 const struct nfs_fh *fh,
6239 struct pnfs_devicelist *devlist)
6241 struct nfs4_getdevicelist_args args = {
6243 .layoutclass = server->pnfs_curr_ld->id,
6245 struct nfs4_getdevicelist_res res = {
6248 struct rpc_message msg = {
6249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6255 dprintk("--> %s\n", __func__);
6256 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6258 dprintk("<-- %s status=%d\n", __func__, status);
6262 int nfs4_proc_getdevicelist(struct nfs_server *server,
6263 const struct nfs_fh *fh,
6264 struct pnfs_devicelist *devlist)
6266 struct nfs4_exception exception = { };
6270 err = nfs4_handle_exception(server,
6271 _nfs4_getdevicelist(server, fh, devlist),
6273 } while (exception.retry);
6275 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6276 err, devlist->num_devs);
6280 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6283 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6285 struct nfs4_getdeviceinfo_args args = {
6288 struct nfs4_getdeviceinfo_res res = {
6291 struct rpc_message msg = {
6292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6298 dprintk("--> %s\n", __func__);
6299 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6300 dprintk("<-- %s status=%d\n", __func__, status);
6305 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6307 struct nfs4_exception exception = { };
6311 err = nfs4_handle_exception(server,
6312 _nfs4_proc_getdeviceinfo(server, pdev),
6314 } while (exception.retry);
6317 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6319 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6321 struct nfs4_layoutcommit_data *data = calldata;
6322 struct nfs_server *server = NFS_SERVER(data->args.inode);
6323 struct nfs4_session *session = nfs4_get_session(server);
6325 nfs41_setup_sequence(session,
6326 &data->args.seq_args,
6332 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6334 struct nfs4_layoutcommit_data *data = calldata;
6335 struct nfs_server *server = NFS_SERVER(data->args.inode);
6337 if (!nfs41_sequence_done(task, &data->res.seq_res))
6340 switch (task->tk_status) { /* Just ignore these failures */
6341 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6342 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6343 case -NFS4ERR_BADLAYOUT: /* no layout */
6344 case -NFS4ERR_GRACE: /* loca_recalim always false */
6345 task->tk_status = 0;
6348 nfs_post_op_update_inode_force_wcc(data->args.inode,
6352 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6353 rpc_restart_call_prepare(task);
6359 static void nfs4_layoutcommit_release(void *calldata)
6361 struct nfs4_layoutcommit_data *data = calldata;
6362 struct pnfs_layout_segment *lseg, *tmp;
6363 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6365 pnfs_cleanup_layoutcommit(data);
6366 /* Matched by references in pnfs_set_layoutcommit */
6367 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6368 list_del_init(&lseg->pls_lc_list);
6369 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6371 pnfs_put_lseg(lseg);
6374 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6375 smp_mb__after_clear_bit();
6376 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6378 put_rpccred(data->cred);
6382 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6383 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6384 .rpc_call_done = nfs4_layoutcommit_done,
6385 .rpc_release = nfs4_layoutcommit_release,
6389 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6391 struct rpc_message msg = {
6392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6393 .rpc_argp = &data->args,
6394 .rpc_resp = &data->res,
6395 .rpc_cred = data->cred,
6397 struct rpc_task_setup task_setup_data = {
6398 .task = &data->task,
6399 .rpc_client = NFS_CLIENT(data->args.inode),
6400 .rpc_message = &msg,
6401 .callback_ops = &nfs4_layoutcommit_ops,
6402 .callback_data = data,
6403 .flags = RPC_TASK_ASYNC,
6405 struct rpc_task *task;
6408 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6409 "lbw: %llu inode %lu\n",
6410 data->task.tk_pid, sync,
6411 data->args.lastbytewritten,
6412 data->args.inode->i_ino);
6414 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6415 task = rpc_run_task(&task_setup_data);
6417 return PTR_ERR(task);
6420 status = nfs4_wait_for_completion_rpc_task(task);
6423 status = task->tk_status;
6425 dprintk("%s: status %d\n", __func__, status);
6431 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6432 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6434 struct nfs41_secinfo_no_name_args args = {
6435 .style = SECINFO_STYLE_CURRENT_FH,
6437 struct nfs4_secinfo_res res = {
6440 struct rpc_message msg = {
6441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6445 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6449 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6450 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6452 struct nfs4_exception exception = { };
6455 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6458 case -NFS4ERR_WRONGSEC:
6459 case -NFS4ERR_NOTSUPP:
6462 err = nfs4_handle_exception(server, err, &exception);
6464 } while (exception.retry);
6470 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6471 struct nfs_fsinfo *info)
6475 rpc_authflavor_t flavor;
6476 struct nfs4_secinfo_flavors *flavors;
6478 page = alloc_page(GFP_KERNEL);
6484 flavors = page_address(page);
6485 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6488 * Fall back on "guess and check" method if
6489 * the server doesn't support SECINFO_NO_NAME
6491 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6492 err = nfs4_find_root_sec(server, fhandle, info);
6498 flavor = nfs_find_best_sec(flavors);
6500 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6510 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6513 struct nfs41_test_stateid_args args = {
6516 struct nfs41_test_stateid_res res;
6517 struct rpc_message msg = {
6518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6523 dprintk("NFS call test_stateid %p\n", stateid);
6524 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6525 nfs4_set_sequence_privileged(&args.seq_args);
6526 status = nfs4_call_sync_sequence(server->client, server, &msg,
6527 &args.seq_args, &res.seq_res);
6528 if (status != NFS_OK) {
6529 dprintk("NFS reply test_stateid: failed, %d\n", status);
6532 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6537 * nfs41_test_stateid - perform a TEST_STATEID operation
6539 * @server: server / transport on which to perform the operation
6540 * @stateid: state ID to test
6542 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6543 * Otherwise a negative NFS4ERR value is returned if the operation
6544 * failed or the state ID is not currently valid.
6546 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6548 struct nfs4_exception exception = { };
6551 err = _nfs41_test_stateid(server, stateid);
6552 if (err != -NFS4ERR_DELAY)
6554 nfs4_handle_exception(server, err, &exception);
6555 } while (exception.retry);
6559 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6561 struct nfs41_free_stateid_args args = {
6564 struct nfs41_free_stateid_res res;
6565 struct rpc_message msg = {
6566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6572 dprintk("NFS call free_stateid %p\n", stateid);
6573 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6574 nfs4_set_sequence_privileged(&args.seq_args);
6575 status = nfs4_call_sync_sequence(server->client, server, &msg,
6576 &args.seq_args, &res.seq_res);
6577 dprintk("NFS reply free_stateid: %d\n", status);
6582 * nfs41_free_stateid - perform a FREE_STATEID operation
6584 * @server: server / transport on which to perform the operation
6585 * @stateid: state ID to release
6587 * Returns NFS_OK if the server freed "stateid". Otherwise a
6588 * negative NFS4ERR value is returned.
6590 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6592 struct nfs4_exception exception = { };
6595 err = _nfs4_free_stateid(server, stateid);
6596 if (err != -NFS4ERR_DELAY)
6598 nfs4_handle_exception(server, err, &exception);
6599 } while (exception.retry);
6603 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6604 const nfs4_stateid *s2)
6606 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6609 if (s1->seqid == s2->seqid)
6611 if (s1->seqid == 0 || s2->seqid == 0)
6617 #endif /* CONFIG_NFS_V4_1 */
6619 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6620 const nfs4_stateid *s2)
6622 return nfs4_stateid_match(s1, s2);
6626 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6627 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6628 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6629 .recover_open = nfs4_open_reclaim,
6630 .recover_lock = nfs4_lock_reclaim,
6631 .establish_clid = nfs4_init_clientid,
6632 .get_clid_cred = nfs4_get_setclientid_cred,
6633 .detect_trunking = nfs40_discover_server_trunking,
6636 #if defined(CONFIG_NFS_V4_1)
6637 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6638 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6639 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6640 .recover_open = nfs4_open_reclaim,
6641 .recover_lock = nfs4_lock_reclaim,
6642 .establish_clid = nfs41_init_clientid,
6643 .get_clid_cred = nfs4_get_exchange_id_cred,
6644 .reclaim_complete = nfs41_proc_reclaim_complete,
6645 .detect_trunking = nfs41_discover_server_trunking,
6647 #endif /* CONFIG_NFS_V4_1 */
6649 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6650 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6651 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6652 .recover_open = nfs4_open_expired,
6653 .recover_lock = nfs4_lock_expired,
6654 .establish_clid = nfs4_init_clientid,
6655 .get_clid_cred = nfs4_get_setclientid_cred,
6658 #if defined(CONFIG_NFS_V4_1)
6659 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6660 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6661 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6662 .recover_open = nfs41_open_expired,
6663 .recover_lock = nfs41_lock_expired,
6664 .establish_clid = nfs41_init_clientid,
6665 .get_clid_cred = nfs4_get_exchange_id_cred,
6667 #endif /* CONFIG_NFS_V4_1 */
6669 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6670 .sched_state_renewal = nfs4_proc_async_renew,
6671 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6672 .renew_lease = nfs4_proc_renew,
6675 #if defined(CONFIG_NFS_V4_1)
6676 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6677 .sched_state_renewal = nfs41_proc_async_sequence,
6678 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6679 .renew_lease = nfs4_proc_sequence,
6683 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6685 .call_sync = _nfs4_call_sync,
6686 .match_stateid = nfs4_match_stateid,
6687 .find_root_sec = nfs4_find_root_sec,
6688 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6689 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6690 .state_renewal_ops = &nfs40_state_renewal_ops,
6693 #if defined(CONFIG_NFS_V4_1)
6694 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6696 .call_sync = nfs4_call_sync_sequence,
6697 .match_stateid = nfs41_match_stateid,
6698 .find_root_sec = nfs41_find_root_sec,
6699 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6700 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6701 .state_renewal_ops = &nfs41_state_renewal_ops,
6705 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6706 [0] = &nfs_v4_0_minor_ops,
6707 #if defined(CONFIG_NFS_V4_1)
6708 [1] = &nfs_v4_1_minor_ops,
6712 const struct inode_operations nfs4_dir_inode_operations = {
6713 .create = nfs_create,
6714 .lookup = nfs_lookup,
6715 .atomic_open = nfs_atomic_open,
6717 .unlink = nfs_unlink,
6718 .symlink = nfs_symlink,
6722 .rename = nfs_rename,
6723 .permission = nfs_permission,
6724 .getattr = nfs_getattr,
6725 .setattr = nfs_setattr,
6726 .getxattr = generic_getxattr,
6727 .setxattr = generic_setxattr,
6728 .listxattr = generic_listxattr,
6729 .removexattr = generic_removexattr,
6732 static const struct inode_operations nfs4_file_inode_operations = {
6733 .permission = nfs_permission,
6734 .getattr = nfs_getattr,
6735 .setattr = nfs_setattr,
6736 .getxattr = generic_getxattr,
6737 .setxattr = generic_setxattr,
6738 .listxattr = generic_listxattr,
6739 .removexattr = generic_removexattr,
6742 const struct nfs_rpc_ops nfs_v4_clientops = {
6743 .version = 4, /* protocol version */
6744 .dentry_ops = &nfs4_dentry_operations,
6745 .dir_inode_ops = &nfs4_dir_inode_operations,
6746 .file_inode_ops = &nfs4_file_inode_operations,
6747 .file_ops = &nfs4_file_operations,
6748 .getroot = nfs4_proc_get_root,
6749 .submount = nfs4_submount,
6750 .try_mount = nfs4_try_mount,
6751 .getattr = nfs4_proc_getattr,
6752 .setattr = nfs4_proc_setattr,
6753 .lookup = nfs4_proc_lookup,
6754 .access = nfs4_proc_access,
6755 .readlink = nfs4_proc_readlink,
6756 .create = nfs4_proc_create,
6757 .remove = nfs4_proc_remove,
6758 .unlink_setup = nfs4_proc_unlink_setup,
6759 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6760 .unlink_done = nfs4_proc_unlink_done,
6761 .rename = nfs4_proc_rename,
6762 .rename_setup = nfs4_proc_rename_setup,
6763 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6764 .rename_done = nfs4_proc_rename_done,
6765 .link = nfs4_proc_link,
6766 .symlink = nfs4_proc_symlink,
6767 .mkdir = nfs4_proc_mkdir,
6768 .rmdir = nfs4_proc_remove,
6769 .readdir = nfs4_proc_readdir,
6770 .mknod = nfs4_proc_mknod,
6771 .statfs = nfs4_proc_statfs,
6772 .fsinfo = nfs4_proc_fsinfo,
6773 .pathconf = nfs4_proc_pathconf,
6774 .set_capabilities = nfs4_server_capabilities,
6775 .decode_dirent = nfs4_decode_dirent,
6776 .read_setup = nfs4_proc_read_setup,
6777 .read_pageio_init = pnfs_pageio_init_read,
6778 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6779 .read_done = nfs4_read_done,
6780 .write_setup = nfs4_proc_write_setup,
6781 .write_pageio_init = pnfs_pageio_init_write,
6782 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6783 .write_done = nfs4_write_done,
6784 .commit_setup = nfs4_proc_commit_setup,
6785 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6786 .commit_done = nfs4_commit_done,
6787 .lock = nfs4_proc_lock,
6788 .clear_acl_cache = nfs4_zap_acl_attr,
6789 .close_context = nfs4_close_context,
6790 .open_context = nfs4_atomic_open,
6791 .have_delegation = nfs4_have_delegation,
6792 .return_delegation = nfs4_inode_return_delegation,
6793 .alloc_client = nfs4_alloc_client,
6794 .init_client = nfs4_init_client,
6795 .free_client = nfs4_free_client,
6796 .create_server = nfs4_create_server,
6797 .clone_server = nfs_clone_server,
6800 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6801 .prefix = XATTR_NAME_NFSV4_ACL,
6802 .list = nfs4_xattr_list_nfs4_acl,
6803 .get = nfs4_xattr_get_nfs4_acl,
6804 .set = nfs4_xattr_set_nfs4_acl,
6807 const struct xattr_handler *nfs4_xattr_handlers[] = {
6808 &nfs4_xattr_nfs4_acl_handler,