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 nfs_mark_delegation_referenced(delegation);
903 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
912 case FMODE_READ|FMODE_WRITE:
915 nfs4_state_set_mode_locked(state, state->state | fmode);
918 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
920 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
921 nfs4_stateid_copy(&state->stateid, stateid);
922 nfs4_stateid_copy(&state->open_stateid, stateid);
925 set_bit(NFS_O_RDONLY_STATE, &state->flags);
928 set_bit(NFS_O_WRONLY_STATE, &state->flags);
930 case FMODE_READ|FMODE_WRITE:
931 set_bit(NFS_O_RDWR_STATE, &state->flags);
935 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
937 write_seqlock(&state->seqlock);
938 nfs_set_open_stateid_locked(state, stateid, fmode);
939 write_sequnlock(&state->seqlock);
942 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
945 * Protect the call to nfs4_state_set_mode_locked and
946 * serialise the stateid update
948 write_seqlock(&state->seqlock);
949 if (deleg_stateid != NULL) {
950 nfs4_stateid_copy(&state->stateid, deleg_stateid);
951 set_bit(NFS_DELEGATED_STATE, &state->flags);
953 if (open_stateid != NULL)
954 nfs_set_open_stateid_locked(state, open_stateid, fmode);
955 write_sequnlock(&state->seqlock);
956 spin_lock(&state->owner->so_lock);
957 update_open_stateflags(state, fmode);
958 spin_unlock(&state->owner->so_lock);
961 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
963 struct nfs_inode *nfsi = NFS_I(state->inode);
964 struct nfs_delegation *deleg_cur;
967 fmode &= (FMODE_READ|FMODE_WRITE);
970 deleg_cur = rcu_dereference(nfsi->delegation);
971 if (deleg_cur == NULL)
974 spin_lock(&deleg_cur->lock);
975 if (nfsi->delegation != deleg_cur ||
976 (deleg_cur->type & fmode) != fmode)
977 goto no_delegation_unlock;
979 if (delegation == NULL)
980 delegation = &deleg_cur->stateid;
981 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
982 goto no_delegation_unlock;
984 nfs_mark_delegation_referenced(deleg_cur);
985 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
987 no_delegation_unlock:
988 spin_unlock(&deleg_cur->lock);
992 if (!ret && open_stateid != NULL) {
993 __update_open_stateid(state, open_stateid, NULL, fmode);
1001 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1003 struct nfs_delegation *delegation;
1006 delegation = rcu_dereference(NFS_I(inode)->delegation);
1007 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1012 nfs4_inode_return_delegation(inode);
1015 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1017 struct nfs4_state *state = opendata->state;
1018 struct nfs_inode *nfsi = NFS_I(state->inode);
1019 struct nfs_delegation *delegation;
1020 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1021 fmode_t fmode = opendata->o_arg.fmode;
1022 nfs4_stateid stateid;
1026 if (can_open_cached(state, fmode, open_mode)) {
1027 spin_lock(&state->owner->so_lock);
1028 if (can_open_cached(state, fmode, open_mode)) {
1029 update_open_stateflags(state, fmode);
1030 spin_unlock(&state->owner->so_lock);
1031 goto out_return_state;
1033 spin_unlock(&state->owner->so_lock);
1036 delegation = rcu_dereference(nfsi->delegation);
1037 if (!can_open_delegated(delegation, fmode)) {
1041 /* Save the delegation */
1042 nfs4_stateid_copy(&stateid, &delegation->stateid);
1044 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1049 /* Try to update the stateid using the delegation */
1050 if (update_open_stateid(state, NULL, &stateid, fmode))
1051 goto out_return_state;
1054 return ERR_PTR(ret);
1056 atomic_inc(&state->count);
1061 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1063 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1064 struct nfs_delegation *delegation;
1065 int delegation_flags = 0;
1068 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1070 delegation_flags = delegation->flags;
1072 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1073 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1074 "returning a delegation for "
1075 "OPEN(CLAIM_DELEGATE_CUR)\n",
1077 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1078 nfs_inode_set_delegation(state->inode,
1079 data->owner->so_cred,
1082 nfs_inode_reclaim_delegation(state->inode,
1083 data->owner->so_cred,
1088 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1089 * and update the nfs4_state.
1091 static struct nfs4_state *
1092 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1094 struct inode *inode = data->state->inode;
1095 struct nfs4_state *state = data->state;
1098 if (!data->rpc_done) {
1099 ret = data->rpc_status;
1104 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1105 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1106 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1110 state = nfs4_get_open_state(inode, data->owner);
1114 ret = nfs_refresh_inode(inode, &data->f_attr);
1118 if (data->o_res.delegation_type != 0)
1119 nfs4_opendata_check_deleg(data, state);
1120 update_open_stateid(state, &data->o_res.stateid, NULL,
1125 return ERR_PTR(ret);
1129 static struct nfs4_state *
1130 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1132 struct inode *inode;
1133 struct nfs4_state *state = NULL;
1136 if (!data->rpc_done) {
1137 state = nfs4_try_open_cached(data);
1142 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1144 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1145 ret = PTR_ERR(inode);
1149 state = nfs4_get_open_state(inode, data->owner);
1152 if (data->o_res.delegation_type != 0)
1153 nfs4_opendata_check_deleg(data, state);
1154 update_open_stateid(state, &data->o_res.stateid, NULL,
1162 return ERR_PTR(ret);
1165 static struct nfs4_state *
1166 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1168 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1169 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1170 return _nfs4_opendata_to_nfs4_state(data);
1173 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1175 struct nfs_inode *nfsi = NFS_I(state->inode);
1176 struct nfs_open_context *ctx;
1178 spin_lock(&state->inode->i_lock);
1179 list_for_each_entry(ctx, &nfsi->open_files, list) {
1180 if (ctx->state != state)
1182 get_nfs_open_context(ctx);
1183 spin_unlock(&state->inode->i_lock);
1186 spin_unlock(&state->inode->i_lock);
1187 return ERR_PTR(-ENOENT);
1190 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1192 struct nfs4_opendata *opendata;
1194 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1195 if (opendata == NULL)
1196 return ERR_PTR(-ENOMEM);
1197 opendata->state = state;
1198 atomic_inc(&state->count);
1202 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1204 struct nfs4_state *newstate;
1207 opendata->o_arg.open_flags = 0;
1208 opendata->o_arg.fmode = fmode;
1209 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1210 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1211 nfs4_init_opendata_res(opendata);
1212 ret = _nfs4_recover_proc_open(opendata);
1215 newstate = nfs4_opendata_to_nfs4_state(opendata);
1216 if (IS_ERR(newstate))
1217 return PTR_ERR(newstate);
1218 nfs4_close_state(newstate, fmode);
1223 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1225 struct nfs4_state *newstate;
1228 /* memory barrier prior to reading state->n_* */
1229 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1231 if (state->n_rdwr != 0) {
1232 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1233 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1236 if (newstate != state)
1239 if (state->n_wronly != 0) {
1240 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1241 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1244 if (newstate != state)
1247 if (state->n_rdonly != 0) {
1248 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1249 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1252 if (newstate != state)
1256 * We may have performed cached opens for all three recoveries.
1257 * Check if we need to update the current stateid.
1259 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1260 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1261 write_seqlock(&state->seqlock);
1262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1263 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1264 write_sequnlock(&state->seqlock);
1271 * reclaim state on the server after a reboot.
1273 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1275 struct nfs_delegation *delegation;
1276 struct nfs4_opendata *opendata;
1277 fmode_t delegation_type = 0;
1280 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1281 if (IS_ERR(opendata))
1282 return PTR_ERR(opendata);
1283 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1284 opendata->o_arg.fh = NFS_FH(state->inode);
1286 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1287 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1288 delegation_type = delegation->type;
1290 opendata->o_arg.u.delegation_type = delegation_type;
1291 status = nfs4_open_recover(opendata, state);
1292 nfs4_opendata_put(opendata);
1296 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1298 struct nfs_server *server = NFS_SERVER(state->inode);
1299 struct nfs4_exception exception = { };
1302 err = _nfs4_do_open_reclaim(ctx, state);
1303 if (err != -NFS4ERR_DELAY)
1305 nfs4_handle_exception(server, err, &exception);
1306 } while (exception.retry);
1310 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1312 struct nfs_open_context *ctx;
1315 ctx = nfs4_state_find_open_context(state);
1317 return PTR_ERR(ctx);
1318 ret = nfs4_do_open_reclaim(ctx, state);
1319 put_nfs_open_context(ctx);
1323 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1325 struct nfs4_opendata *opendata;
1328 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1329 if (IS_ERR(opendata))
1330 return PTR_ERR(opendata);
1331 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1332 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1333 ret = nfs4_open_recover(opendata, state);
1334 nfs4_opendata_put(opendata);
1338 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1340 struct nfs4_exception exception = { };
1341 struct nfs_server *server = NFS_SERVER(state->inode);
1344 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1350 case -NFS4ERR_BADSESSION:
1351 case -NFS4ERR_BADSLOT:
1352 case -NFS4ERR_BAD_HIGH_SLOT:
1353 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1354 case -NFS4ERR_DEADSESSION:
1355 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1357 case -NFS4ERR_STALE_CLIENTID:
1358 case -NFS4ERR_STALE_STATEID:
1359 case -NFS4ERR_EXPIRED:
1360 /* Don't recall a delegation if it was lost */
1361 nfs4_schedule_lease_recovery(server->nfs_client);
1365 * The show must go on: exit, but mark the
1366 * stateid as needing recovery.
1368 case -NFS4ERR_DELEG_REVOKED:
1369 case -NFS4ERR_ADMIN_REVOKED:
1370 case -NFS4ERR_BAD_STATEID:
1371 nfs_inode_find_state_and_recover(state->inode,
1373 nfs4_schedule_stateid_recovery(server, state);
1377 case -NFS4ERR_DELAY:
1378 case -NFS4ERR_GRACE:
1379 set_bit(NFS_DELEGATED_STATE, &state->flags);
1384 err = nfs4_handle_exception(server, err, &exception);
1385 } while (exception.retry);
1390 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1392 struct nfs4_opendata *data = calldata;
1394 data->rpc_status = task->tk_status;
1395 if (data->rpc_status == 0) {
1396 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1397 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1398 renew_lease(data->o_res.server, data->timestamp);
1403 static void nfs4_open_confirm_release(void *calldata)
1405 struct nfs4_opendata *data = calldata;
1406 struct nfs4_state *state = NULL;
1408 /* If this request hasn't been cancelled, do nothing */
1409 if (data->cancelled == 0)
1411 /* In case of error, no cleanup! */
1412 if (!data->rpc_done)
1414 state = nfs4_opendata_to_nfs4_state(data);
1416 nfs4_close_state(state, data->o_arg.fmode);
1418 nfs4_opendata_put(data);
1421 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1422 .rpc_call_done = nfs4_open_confirm_done,
1423 .rpc_release = nfs4_open_confirm_release,
1427 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1429 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1431 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1432 struct rpc_task *task;
1433 struct rpc_message msg = {
1434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1435 .rpc_argp = &data->c_arg,
1436 .rpc_resp = &data->c_res,
1437 .rpc_cred = data->owner->so_cred,
1439 struct rpc_task_setup task_setup_data = {
1440 .rpc_client = server->client,
1441 .rpc_message = &msg,
1442 .callback_ops = &nfs4_open_confirm_ops,
1443 .callback_data = data,
1444 .workqueue = nfsiod_workqueue,
1445 .flags = RPC_TASK_ASYNC,
1449 kref_get(&data->kref);
1451 data->rpc_status = 0;
1452 data->timestamp = jiffies;
1453 task = rpc_run_task(&task_setup_data);
1455 return PTR_ERR(task);
1456 status = nfs4_wait_for_completion_rpc_task(task);
1458 data->cancelled = 1;
1461 status = data->rpc_status;
1466 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1468 struct nfs4_opendata *data = calldata;
1469 struct nfs4_state_owner *sp = data->owner;
1471 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1474 * Check if we still need to send an OPEN call, or if we can use
1475 * a delegation instead.
1477 if (data->state != NULL) {
1478 struct nfs_delegation *delegation;
1480 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1483 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1484 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1485 can_open_delegated(delegation, data->o_arg.fmode))
1486 goto unlock_no_action;
1489 /* Update client id. */
1490 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1491 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1492 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1493 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1494 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1496 data->timestamp = jiffies;
1497 if (nfs4_setup_sequence(data->o_arg.server,
1498 &data->o_arg.seq_args,
1499 &data->o_res.seq_res,
1501 nfs_release_seqid(data->o_arg.seqid);
1506 task->tk_action = NULL;
1508 nfs4_sequence_done(task, &data->o_res.seq_res);
1511 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1513 struct nfs4_opendata *data = calldata;
1515 data->rpc_status = task->tk_status;
1517 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1520 if (task->tk_status == 0) {
1521 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1522 switch (data->o_res.f_attr->mode & S_IFMT) {
1526 data->rpc_status = -ELOOP;
1529 data->rpc_status = -EISDIR;
1532 data->rpc_status = -ENOTDIR;
1535 renew_lease(data->o_res.server, data->timestamp);
1536 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1537 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1542 static void nfs4_open_release(void *calldata)
1544 struct nfs4_opendata *data = calldata;
1545 struct nfs4_state *state = NULL;
1547 /* If this request hasn't been cancelled, do nothing */
1548 if (data->cancelled == 0)
1550 /* In case of error, no cleanup! */
1551 if (data->rpc_status != 0 || !data->rpc_done)
1553 /* In case we need an open_confirm, no cleanup! */
1554 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1556 state = nfs4_opendata_to_nfs4_state(data);
1558 nfs4_close_state(state, data->o_arg.fmode);
1560 nfs4_opendata_put(data);
1563 static const struct rpc_call_ops nfs4_open_ops = {
1564 .rpc_call_prepare = nfs4_open_prepare,
1565 .rpc_call_done = nfs4_open_done,
1566 .rpc_release = nfs4_open_release,
1569 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1571 struct inode *dir = data->dir->d_inode;
1572 struct nfs_server *server = NFS_SERVER(dir);
1573 struct nfs_openargs *o_arg = &data->o_arg;
1574 struct nfs_openres *o_res = &data->o_res;
1575 struct rpc_task *task;
1576 struct rpc_message msg = {
1577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1580 .rpc_cred = data->owner->so_cred,
1582 struct rpc_task_setup task_setup_data = {
1583 .rpc_client = server->client,
1584 .rpc_message = &msg,
1585 .callback_ops = &nfs4_open_ops,
1586 .callback_data = data,
1587 .workqueue = nfsiod_workqueue,
1588 .flags = RPC_TASK_ASYNC,
1592 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1593 kref_get(&data->kref);
1595 data->rpc_status = 0;
1596 data->cancelled = 0;
1598 nfs4_set_sequence_privileged(&o_arg->seq_args);
1599 task = rpc_run_task(&task_setup_data);
1601 return PTR_ERR(task);
1602 status = nfs4_wait_for_completion_rpc_task(task);
1604 data->cancelled = 1;
1607 status = data->rpc_status;
1613 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1615 struct inode *dir = data->dir->d_inode;
1616 struct nfs_openres *o_res = &data->o_res;
1619 status = nfs4_run_open_task(data, 1);
1620 if (status != 0 || !data->rpc_done)
1623 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1625 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1626 status = _nfs4_proc_open_confirm(data);
1634 static int nfs4_opendata_access(struct rpc_cred *cred,
1635 struct nfs4_opendata *opendata,
1636 struct nfs4_state *state, fmode_t fmode,
1639 struct nfs_access_entry cache;
1642 /* access call failed or for some reason the server doesn't
1643 * support any access modes -- defer access call until later */
1644 if (opendata->o_res.access_supported == 0)
1648 /* don't check MAY_WRITE - a newly created file may not have
1649 * write mode bits, but POSIX allows the creating process to write.
1650 * use openflags to check for exec, because fmode won't
1651 * always have FMODE_EXEC set when file open for exec. */
1652 if (openflags & __FMODE_EXEC) {
1653 /* ONLY check for exec rights */
1655 } else if (fmode & FMODE_READ)
1659 cache.jiffies = jiffies;
1660 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1661 nfs_access_add_cache(state->inode, &cache);
1663 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1666 /* even though OPEN succeeded, access is denied. Close the file */
1667 nfs4_close_state(state, fmode);
1672 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1674 static int _nfs4_proc_open(struct nfs4_opendata *data)
1676 struct inode *dir = data->dir->d_inode;
1677 struct nfs_server *server = NFS_SERVER(dir);
1678 struct nfs_openargs *o_arg = &data->o_arg;
1679 struct nfs_openres *o_res = &data->o_res;
1682 status = nfs4_run_open_task(data, 0);
1683 if (!data->rpc_done)
1686 if (status == -NFS4ERR_BADNAME &&
1687 !(o_arg->open_flags & O_CREAT))
1692 nfs_fattr_map_and_free_names(server, &data->f_attr);
1694 if (o_arg->open_flags & O_CREAT)
1695 update_changeattr(dir, &o_res->cinfo);
1696 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1697 server->caps &= ~NFS_CAP_POSIX_LOCK;
1698 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1699 status = _nfs4_proc_open_confirm(data);
1703 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1704 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1708 static int nfs4_recover_expired_lease(struct nfs_server *server)
1710 return nfs4_client_recover_expired_lease(server->nfs_client);
1715 * reclaim state on the server after a network partition.
1716 * Assumes caller holds the appropriate lock
1718 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1720 struct nfs4_opendata *opendata;
1723 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1724 if (IS_ERR(opendata))
1725 return PTR_ERR(opendata);
1726 ret = nfs4_open_recover(opendata, state);
1728 d_drop(ctx->dentry);
1729 nfs4_opendata_put(opendata);
1733 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1735 struct nfs_server *server = NFS_SERVER(state->inode);
1736 struct nfs4_exception exception = { };
1740 err = _nfs4_open_expired(ctx, state);
1744 case -NFS4ERR_GRACE:
1745 case -NFS4ERR_DELAY:
1746 nfs4_handle_exception(server, err, &exception);
1749 } while (exception.retry);
1754 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1756 struct nfs_open_context *ctx;
1759 ctx = nfs4_state_find_open_context(state);
1761 return PTR_ERR(ctx);
1762 ret = nfs4_do_open_expired(ctx, state);
1763 put_nfs_open_context(ctx);
1767 #if defined(CONFIG_NFS_V4_1)
1768 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1770 struct nfs_server *server = NFS_SERVER(state->inode);
1771 nfs4_stateid *stateid = &state->stateid;
1774 /* If a state reset has been done, test_stateid is unneeded */
1775 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1778 status = nfs41_test_stateid(server, stateid);
1779 if (status != NFS_OK) {
1780 /* Free the stateid unless the server explicitly
1781 * informs us the stateid is unrecognized. */
1782 if (status != -NFS4ERR_BAD_STATEID)
1783 nfs41_free_stateid(server, stateid);
1784 nfs_remove_bad_delegation(state->inode);
1786 write_seqlock(&state->seqlock);
1787 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1788 write_sequnlock(&state->seqlock);
1789 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1794 * nfs41_check_open_stateid - possibly free an open stateid
1796 * @state: NFSv4 state for an inode
1798 * Returns NFS_OK if recovery for this stateid is now finished.
1799 * Otherwise a negative NFS4ERR value is returned.
1801 static int nfs41_check_open_stateid(struct nfs4_state *state)
1803 struct nfs_server *server = NFS_SERVER(state->inode);
1804 nfs4_stateid *stateid = &state->open_stateid;
1807 /* If a state reset has been done, test_stateid is unneeded */
1808 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1809 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1810 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1811 return -NFS4ERR_BAD_STATEID;
1813 status = nfs41_test_stateid(server, stateid);
1814 if (status != NFS_OK) {
1815 /* Free the stateid unless the server explicitly
1816 * informs us the stateid is unrecognized. */
1817 if (status != -NFS4ERR_BAD_STATEID)
1818 nfs41_free_stateid(server, stateid);
1820 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1821 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1822 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1827 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1831 nfs41_clear_delegation_stateid(state);
1832 status = nfs41_check_open_stateid(state);
1833 if (status != NFS_OK)
1834 status = nfs4_open_expired(sp, state);
1840 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1841 * fields corresponding to attributes that were used to store the verifier.
1842 * Make sure we clobber those fields in the later setattr call
1844 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1846 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1847 !(sattr->ia_valid & ATTR_ATIME_SET))
1848 sattr->ia_valid |= ATTR_ATIME;
1850 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1851 !(sattr->ia_valid & ATTR_MTIME_SET))
1852 sattr->ia_valid |= ATTR_MTIME;
1856 * Returns a referenced nfs4_state
1858 static int _nfs4_do_open(struct inode *dir,
1859 struct dentry *dentry,
1862 struct iattr *sattr,
1863 struct rpc_cred *cred,
1864 struct nfs4_state **res,
1865 struct nfs4_threshold **ctx_th)
1867 struct nfs4_state_owner *sp;
1868 struct nfs4_state *state = NULL;
1869 struct nfs_server *server = NFS_SERVER(dir);
1870 struct nfs4_opendata *opendata;
1873 /* Protect against reboot recovery conflicts */
1875 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1877 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1880 status = nfs4_recover_expired_lease(server);
1882 goto err_put_state_owner;
1883 if (dentry->d_inode != NULL)
1884 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1886 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1887 if (opendata == NULL)
1888 goto err_put_state_owner;
1890 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1891 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1892 if (!opendata->f_attr.mdsthreshold)
1893 goto err_opendata_put;
1894 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1896 if (dentry->d_inode != NULL)
1897 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1899 status = _nfs4_proc_open(opendata);
1901 goto err_opendata_put;
1903 state = nfs4_opendata_to_nfs4_state(opendata);
1904 status = PTR_ERR(state);
1906 goto err_opendata_put;
1907 if (server->caps & NFS_CAP_POSIX_LOCK)
1908 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1910 status = nfs4_opendata_access(cred, opendata, state, fmode, flags);
1912 goto err_opendata_put;
1914 if (opendata->o_arg.open_flags & O_EXCL) {
1915 nfs4_exclusive_attrset(opendata, sattr);
1917 nfs_fattr_init(opendata->o_res.f_attr);
1918 status = nfs4_do_setattr(state->inode, cred,
1919 opendata->o_res.f_attr, sattr,
1922 nfs_setattr_update_inode(state->inode, sattr);
1923 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1926 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1927 *ctx_th = opendata->f_attr.mdsthreshold;
1929 kfree(opendata->f_attr.mdsthreshold);
1930 opendata->f_attr.mdsthreshold = NULL;
1932 nfs4_opendata_put(opendata);
1933 nfs4_put_state_owner(sp);
1937 kfree(opendata->f_attr.mdsthreshold);
1938 nfs4_opendata_put(opendata);
1939 err_put_state_owner:
1940 nfs4_put_state_owner(sp);
1947 static struct nfs4_state *nfs4_do_open(struct inode *dir,
1948 struct dentry *dentry,
1951 struct iattr *sattr,
1952 struct rpc_cred *cred,
1953 struct nfs4_threshold **ctx_th)
1955 struct nfs4_exception exception = { };
1956 struct nfs4_state *res;
1959 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
1961 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1965 /* NOTE: BAD_SEQID means the server and client disagree about the
1966 * book-keeping w.r.t. state-changing operations
1967 * (OPEN/CLOSE/LOCK/LOCKU...)
1968 * It is actually a sign of a bug on the client or on the server.
1970 * If we receive a BAD_SEQID error in the particular case of
1971 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1972 * have unhashed the old state_owner for us, and that we can
1973 * therefore safely retry using a new one. We should still warn
1974 * the user though...
1976 if (status == -NFS4ERR_BAD_SEQID) {
1977 pr_warn_ratelimited("NFS: v4 server %s "
1978 " returned a bad sequence-id error!\n",
1979 NFS_SERVER(dir)->nfs_client->cl_hostname);
1980 exception.retry = 1;
1984 * BAD_STATEID on OPEN means that the server cancelled our
1985 * state before it received the OPEN_CONFIRM.
1986 * Recover by retrying the request as per the discussion
1987 * on Page 181 of RFC3530.
1989 if (status == -NFS4ERR_BAD_STATEID) {
1990 exception.retry = 1;
1993 if (status == -EAGAIN) {
1994 /* We must have found a delegation */
1995 exception.retry = 1;
1998 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1999 status, &exception));
2000 } while (exception.retry);
2004 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2005 struct nfs_fattr *fattr, struct iattr *sattr,
2006 struct nfs4_state *state)
2008 struct nfs_server *server = NFS_SERVER(inode);
2009 struct nfs_setattrargs arg = {
2010 .fh = NFS_FH(inode),
2013 .bitmask = server->attr_bitmask,
2015 struct nfs_setattrres res = {
2019 struct rpc_message msg = {
2020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2025 unsigned long timestamp = jiffies;
2028 nfs_fattr_init(fattr);
2030 if (state != NULL) {
2031 struct nfs_lockowner lockowner = {
2032 .l_owner = current->files,
2033 .l_pid = current->tgid,
2035 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2037 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2039 /* Use that stateid */
2041 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2043 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2044 if (status == 0 && state != NULL)
2045 renew_lease(server, timestamp);
2049 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2050 struct nfs_fattr *fattr, struct iattr *sattr,
2051 struct nfs4_state *state)
2053 struct nfs_server *server = NFS_SERVER(inode);
2054 struct nfs4_exception exception = {
2060 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2062 case -NFS4ERR_OPENMODE:
2063 if (state && !(state->state & FMODE_WRITE)) {
2065 if (sattr->ia_valid & ATTR_OPEN)
2070 err = nfs4_handle_exception(server, err, &exception);
2071 } while (exception.retry);
2076 struct nfs4_closedata {
2077 struct inode *inode;
2078 struct nfs4_state *state;
2079 struct nfs_closeargs arg;
2080 struct nfs_closeres res;
2081 struct nfs_fattr fattr;
2082 unsigned long timestamp;
2087 static void nfs4_free_closedata(void *data)
2089 struct nfs4_closedata *calldata = data;
2090 struct nfs4_state_owner *sp = calldata->state->owner;
2091 struct super_block *sb = calldata->state->inode->i_sb;
2094 pnfs_roc_release(calldata->state->inode);
2095 nfs4_put_open_state(calldata->state);
2096 nfs_free_seqid(calldata->arg.seqid);
2097 nfs4_put_state_owner(sp);
2098 nfs_sb_deactive_async(sb);
2102 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2105 spin_lock(&state->owner->so_lock);
2106 if (!(fmode & FMODE_READ))
2107 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2108 if (!(fmode & FMODE_WRITE))
2109 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2110 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2111 spin_unlock(&state->owner->so_lock);
2114 static void nfs4_close_done(struct rpc_task *task, void *data)
2116 struct nfs4_closedata *calldata = data;
2117 struct nfs4_state *state = calldata->state;
2118 struct nfs_server *server = NFS_SERVER(calldata->inode);
2120 dprintk("%s: begin!\n", __func__);
2121 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2123 /* hmm. we are done with the inode, and in the process of freeing
2124 * the state_owner. we keep this around to process errors
2126 switch (task->tk_status) {
2129 pnfs_roc_set_barrier(state->inode,
2130 calldata->roc_barrier);
2131 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2132 renew_lease(server, calldata->timestamp);
2133 nfs4_close_clear_stateid_flags(state,
2134 calldata->arg.fmode);
2136 case -NFS4ERR_STALE_STATEID:
2137 case -NFS4ERR_OLD_STATEID:
2138 case -NFS4ERR_BAD_STATEID:
2139 case -NFS4ERR_EXPIRED:
2140 if (calldata->arg.fmode == 0)
2143 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2144 rpc_restart_call_prepare(task);
2146 nfs_release_seqid(calldata->arg.seqid);
2147 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2148 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2151 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2153 struct nfs4_closedata *calldata = data;
2154 struct nfs4_state *state = calldata->state;
2155 struct inode *inode = calldata->inode;
2158 dprintk("%s: begin!\n", __func__);
2159 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2162 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2163 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2164 spin_lock(&state->owner->so_lock);
2165 /* Calculate the change in open mode */
2166 if (state->n_rdwr == 0) {
2167 if (state->n_rdonly == 0) {
2168 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2169 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2170 calldata->arg.fmode &= ~FMODE_READ;
2172 if (state->n_wronly == 0) {
2173 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2174 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2175 calldata->arg.fmode &= ~FMODE_WRITE;
2178 spin_unlock(&state->owner->so_lock);
2181 /* Note: exit _without_ calling nfs4_close_done */
2185 if (calldata->arg.fmode == 0) {
2186 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2187 if (calldata->roc &&
2188 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2192 nfs_fattr_init(calldata->res.fattr);
2193 calldata->timestamp = jiffies;
2194 if (nfs4_setup_sequence(NFS_SERVER(inode),
2195 &calldata->arg.seq_args,
2196 &calldata->res.seq_res,
2198 nfs_release_seqid(calldata->arg.seqid);
2199 dprintk("%s: done!\n", __func__);
2202 task->tk_action = NULL;
2204 nfs4_sequence_done(task, &calldata->res.seq_res);
2207 static const struct rpc_call_ops nfs4_close_ops = {
2208 .rpc_call_prepare = nfs4_close_prepare,
2209 .rpc_call_done = nfs4_close_done,
2210 .rpc_release = nfs4_free_closedata,
2214 * It is possible for data to be read/written from a mem-mapped file
2215 * after the sys_close call (which hits the vfs layer as a flush).
2216 * This means that we can't safely call nfsv4 close on a file until
2217 * the inode is cleared. This in turn means that we are not good
2218 * NFSv4 citizens - we do not indicate to the server to update the file's
2219 * share state even when we are done with one of the three share
2220 * stateid's in the inode.
2222 * NOTE: Caller must be holding the sp->so_owner semaphore!
2224 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2226 struct nfs_server *server = NFS_SERVER(state->inode);
2227 struct nfs4_closedata *calldata;
2228 struct nfs4_state_owner *sp = state->owner;
2229 struct rpc_task *task;
2230 struct rpc_message msg = {
2231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2232 .rpc_cred = state->owner->so_cred,
2234 struct rpc_task_setup task_setup_data = {
2235 .rpc_client = server->client,
2236 .rpc_message = &msg,
2237 .callback_ops = &nfs4_close_ops,
2238 .workqueue = nfsiod_workqueue,
2239 .flags = RPC_TASK_ASYNC,
2241 int status = -ENOMEM;
2243 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2244 if (calldata == NULL)
2246 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2247 calldata->inode = state->inode;
2248 calldata->state = state;
2249 calldata->arg.fh = NFS_FH(state->inode);
2250 calldata->arg.stateid = &state->open_stateid;
2251 /* Serialization for the sequence id */
2252 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2253 if (calldata->arg.seqid == NULL)
2254 goto out_free_calldata;
2255 calldata->arg.fmode = 0;
2256 calldata->arg.bitmask = server->cache_consistency_bitmask;
2257 calldata->res.fattr = &calldata->fattr;
2258 calldata->res.seqid = calldata->arg.seqid;
2259 calldata->res.server = server;
2260 calldata->roc = pnfs_roc(state->inode);
2261 nfs_sb_active(calldata->inode->i_sb);
2263 msg.rpc_argp = &calldata->arg;
2264 msg.rpc_resp = &calldata->res;
2265 task_setup_data.callback_data = calldata;
2266 task = rpc_run_task(&task_setup_data);
2268 return PTR_ERR(task);
2271 status = rpc_wait_for_completion_task(task);
2277 nfs4_put_open_state(state);
2278 nfs4_put_state_owner(sp);
2282 static struct inode *
2283 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2285 struct nfs4_state *state;
2287 /* Protect against concurrent sillydeletes */
2288 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2289 ctx->cred, &ctx->mdsthreshold);
2291 return ERR_CAST(state);
2293 return igrab(state->inode);
2296 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2298 if (ctx->state == NULL)
2301 nfs4_close_sync(ctx->state, ctx->mode);
2303 nfs4_close_state(ctx->state, ctx->mode);
2306 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2308 struct nfs4_server_caps_arg args = {
2311 struct nfs4_server_caps_res res = {};
2312 struct rpc_message msg = {
2313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2319 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2321 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2322 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2323 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2324 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2325 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2326 NFS_CAP_CTIME|NFS_CAP_MTIME);
2327 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2328 server->caps |= NFS_CAP_ACLS;
2329 if (res.has_links != 0)
2330 server->caps |= NFS_CAP_HARDLINKS;
2331 if (res.has_symlinks != 0)
2332 server->caps |= NFS_CAP_SYMLINKS;
2333 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2334 server->caps |= NFS_CAP_FILEID;
2335 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2336 server->caps |= NFS_CAP_MODE;
2337 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2338 server->caps |= NFS_CAP_NLINK;
2339 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2340 server->caps |= NFS_CAP_OWNER;
2341 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2342 server->caps |= NFS_CAP_OWNER_GROUP;
2343 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2344 server->caps |= NFS_CAP_ATIME;
2345 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2346 server->caps |= NFS_CAP_CTIME;
2347 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2348 server->caps |= NFS_CAP_MTIME;
2350 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2351 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2352 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2353 server->acl_bitmask = res.acl_bitmask;
2354 server->fh_expire_type = res.fh_expire_type;
2360 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2362 struct nfs4_exception exception = { };
2365 err = nfs4_handle_exception(server,
2366 _nfs4_server_capabilities(server, fhandle),
2368 } while (exception.retry);
2372 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2373 struct nfs_fsinfo *info)
2375 struct nfs4_lookup_root_arg args = {
2376 .bitmask = nfs4_fattr_bitmap,
2378 struct nfs4_lookup_res res = {
2380 .fattr = info->fattr,
2383 struct rpc_message msg = {
2384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2389 nfs_fattr_init(info->fattr);
2390 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2393 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2394 struct nfs_fsinfo *info)
2396 struct nfs4_exception exception = { };
2399 err = _nfs4_lookup_root(server, fhandle, info);
2402 case -NFS4ERR_WRONGSEC:
2405 err = nfs4_handle_exception(server, err, &exception);
2407 } while (exception.retry);
2412 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2413 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2415 struct rpc_auth *auth;
2418 auth = rpcauth_create(flavor, server->client);
2423 ret = nfs4_lookup_root(server, fhandle, info);
2428 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2429 struct nfs_fsinfo *info)
2431 int i, len, status = 0;
2432 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2434 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2438 for (i = 0; i < len; i++) {
2439 /* AUTH_UNIX is the default flavor if none was specified,
2440 * thus has already been tried. */
2441 if (flav_array[i] == RPC_AUTH_UNIX)
2444 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2445 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2450 * -EACCESS could mean that the user doesn't have correct permissions
2451 * to access the mount. It could also mean that we tried to mount
2452 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2453 * existing mount programs don't handle -EACCES very well so it should
2454 * be mapped to -EPERM instead.
2456 if (status == -EACCES)
2462 * get the file handle for the "/" directory on the server
2464 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2465 struct nfs_fsinfo *info)
2467 int minor_version = server->nfs_client->cl_minorversion;
2468 int status = nfs4_lookup_root(server, fhandle, info);
2469 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2471 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2472 * by nfs4_map_errors() as this function exits.
2474 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2476 status = nfs4_server_capabilities(server, fhandle);
2478 status = nfs4_do_fsinfo(server, fhandle, info);
2479 return nfs4_map_errors(status);
2482 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2483 struct nfs_fsinfo *info)
2486 struct nfs_fattr *fattr = info->fattr;
2488 error = nfs4_server_capabilities(server, mntfh);
2490 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2494 error = nfs4_proc_getattr(server, mntfh, fattr);
2496 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2500 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2501 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2502 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2508 * Get locations and (maybe) other attributes of a referral.
2509 * Note that we'll actually follow the referral later when
2510 * we detect fsid mismatch in inode revalidation
2512 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2513 const struct qstr *name, struct nfs_fattr *fattr,
2514 struct nfs_fh *fhandle)
2516 int status = -ENOMEM;
2517 struct page *page = NULL;
2518 struct nfs4_fs_locations *locations = NULL;
2520 page = alloc_page(GFP_KERNEL);
2523 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2524 if (locations == NULL)
2527 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2530 /* Make sure server returned a different fsid for the referral */
2531 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2532 dprintk("%s: server did not return a different fsid for"
2533 " a referral at %s\n", __func__, name->name);
2537 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2538 nfs_fixup_referral_attributes(&locations->fattr);
2540 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2541 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2542 memset(fhandle, 0, sizeof(struct nfs_fh));
2550 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2552 struct nfs4_getattr_arg args = {
2554 .bitmask = server->attr_bitmask,
2556 struct nfs4_getattr_res res = {
2560 struct rpc_message msg = {
2561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2566 nfs_fattr_init(fattr);
2567 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2570 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2572 struct nfs4_exception exception = { };
2575 err = nfs4_handle_exception(server,
2576 _nfs4_proc_getattr(server, fhandle, fattr),
2578 } while (exception.retry);
2583 * The file is not closed if it is opened due to the a request to change
2584 * the size of the file. The open call will not be needed once the
2585 * VFS layer lookup-intents are implemented.
2587 * Close is called when the inode is destroyed.
2588 * If we haven't opened the file for O_WRONLY, we
2589 * need to in the size_change case to obtain a stateid.
2592 * Because OPEN is always done by name in nfsv4, it is
2593 * possible that we opened a different file by the same
2594 * name. We can recognize this race condition, but we
2595 * can't do anything about it besides returning an error.
2597 * This will be fixed with VFS changes (lookup-intent).
2600 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2601 struct iattr *sattr)
2603 struct inode *inode = dentry->d_inode;
2604 struct rpc_cred *cred = NULL;
2605 struct nfs4_state *state = NULL;
2608 if (pnfs_ld_layoutret_on_setattr(inode))
2609 pnfs_return_layout(inode);
2611 nfs_fattr_init(fattr);
2613 /* Deal with open(O_TRUNC) */
2614 if (sattr->ia_valid & ATTR_OPEN)
2615 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2617 /* Optimization: if the end result is no change, don't RPC */
2618 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2621 /* Search for an existing open(O_WRITE) file */
2622 if (sattr->ia_valid & ATTR_FILE) {
2623 struct nfs_open_context *ctx;
2625 ctx = nfs_file_open_context(sattr->ia_file);
2632 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2634 nfs_setattr_update_inode(inode, sattr);
2638 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2639 const struct qstr *name, struct nfs_fh *fhandle,
2640 struct nfs_fattr *fattr)
2642 struct nfs_server *server = NFS_SERVER(dir);
2644 struct nfs4_lookup_arg args = {
2645 .bitmask = server->attr_bitmask,
2646 .dir_fh = NFS_FH(dir),
2649 struct nfs4_lookup_res res = {
2654 struct rpc_message msg = {
2655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2660 nfs_fattr_init(fattr);
2662 dprintk("NFS call lookup %s\n", name->name);
2663 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2664 dprintk("NFS reply lookup: %d\n", status);
2668 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2670 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2671 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2672 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2676 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2677 struct qstr *name, struct nfs_fh *fhandle,
2678 struct nfs_fattr *fattr)
2680 struct nfs4_exception exception = { };
2681 struct rpc_clnt *client = *clnt;
2684 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2686 case -NFS4ERR_BADNAME:
2689 case -NFS4ERR_MOVED:
2690 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2692 case -NFS4ERR_WRONGSEC:
2694 if (client != *clnt)
2697 client = nfs4_create_sec_client(client, dir, name);
2699 return PTR_ERR(client);
2701 exception.retry = 1;
2704 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2706 } while (exception.retry);
2711 else if (client != *clnt)
2712 rpc_shutdown_client(client);
2717 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2718 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2721 struct rpc_clnt *client = NFS_CLIENT(dir);
2723 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2724 if (client != NFS_CLIENT(dir)) {
2725 rpc_shutdown_client(client);
2726 nfs_fixup_secinfo_attributes(fattr);
2732 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2733 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2736 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2738 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2740 rpc_shutdown_client(client);
2741 return ERR_PTR(status);
2746 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2748 struct nfs_server *server = NFS_SERVER(inode);
2749 struct nfs4_accessargs args = {
2750 .fh = NFS_FH(inode),
2751 .bitmask = server->cache_consistency_bitmask,
2753 struct nfs4_accessres res = {
2756 struct rpc_message msg = {
2757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2760 .rpc_cred = entry->cred,
2762 int mode = entry->mask;
2766 * Determine which access bits we want to ask for...
2768 if (mode & MAY_READ)
2769 args.access |= NFS4_ACCESS_READ;
2770 if (S_ISDIR(inode->i_mode)) {
2771 if (mode & MAY_WRITE)
2772 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2773 if (mode & MAY_EXEC)
2774 args.access |= NFS4_ACCESS_LOOKUP;
2776 if (mode & MAY_WRITE)
2777 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2778 if (mode & MAY_EXEC)
2779 args.access |= NFS4_ACCESS_EXECUTE;
2782 res.fattr = nfs_alloc_fattr();
2783 if (res.fattr == NULL)
2786 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2788 nfs_access_set_mask(entry, res.access);
2789 nfs_refresh_inode(inode, res.fattr);
2791 nfs_free_fattr(res.fattr);
2795 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2797 struct nfs4_exception exception = { };
2800 err = nfs4_handle_exception(NFS_SERVER(inode),
2801 _nfs4_proc_access(inode, entry),
2803 } while (exception.retry);
2808 * TODO: For the time being, we don't try to get any attributes
2809 * along with any of the zero-copy operations READ, READDIR,
2812 * In the case of the first three, we want to put the GETATTR
2813 * after the read-type operation -- this is because it is hard
2814 * to predict the length of a GETATTR response in v4, and thus
2815 * align the READ data correctly. This means that the GETATTR
2816 * may end up partially falling into the page cache, and we should
2817 * shift it into the 'tail' of the xdr_buf before processing.
2818 * To do this efficiently, we need to know the total length
2819 * of data received, which doesn't seem to be available outside
2822 * In the case of WRITE, we also want to put the GETATTR after
2823 * the operation -- in this case because we want to make sure
2824 * we get the post-operation mtime and size.
2826 * Both of these changes to the XDR layer would in fact be quite
2827 * minor, but I decided to leave them for a subsequent patch.
2829 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2830 unsigned int pgbase, unsigned int pglen)
2832 struct nfs4_readlink args = {
2833 .fh = NFS_FH(inode),
2838 struct nfs4_readlink_res res;
2839 struct rpc_message msg = {
2840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2845 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2848 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2849 unsigned int pgbase, unsigned int pglen)
2851 struct nfs4_exception exception = { };
2854 err = nfs4_handle_exception(NFS_SERVER(inode),
2855 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2857 } while (exception.retry);
2862 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2865 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2868 struct nfs_open_context *ctx;
2869 struct nfs4_state *state;
2872 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2874 return PTR_ERR(ctx);
2876 sattr->ia_mode &= ~current_umask();
2877 state = nfs4_do_open(dir, dentry, ctx->mode,
2878 flags, sattr, ctx->cred,
2879 &ctx->mdsthreshold);
2881 if (IS_ERR(state)) {
2882 status = PTR_ERR(state);
2885 d_add(dentry, igrab(state->inode));
2886 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2889 put_nfs_open_context(ctx);
2893 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2895 struct nfs_server *server = NFS_SERVER(dir);
2896 struct nfs_removeargs args = {
2900 struct nfs_removeres res = {
2903 struct rpc_message msg = {
2904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2910 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2912 update_changeattr(dir, &res.cinfo);
2916 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2918 struct nfs4_exception exception = { };
2921 err = nfs4_handle_exception(NFS_SERVER(dir),
2922 _nfs4_proc_remove(dir, name),
2924 } while (exception.retry);
2928 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2930 struct nfs_server *server = NFS_SERVER(dir);
2931 struct nfs_removeargs *args = msg->rpc_argp;
2932 struct nfs_removeres *res = msg->rpc_resp;
2934 res->server = server;
2935 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2936 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2939 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2941 nfs4_setup_sequence(NFS_SERVER(data->dir),
2942 &data->args.seq_args,
2947 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2949 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2951 if (!nfs4_sequence_done(task, &res->seq_res))
2953 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2955 update_changeattr(dir, &res->cinfo);
2959 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2961 struct nfs_server *server = NFS_SERVER(dir);
2962 struct nfs_renameargs *arg = msg->rpc_argp;
2963 struct nfs_renameres *res = msg->rpc_resp;
2965 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2966 res->server = server;
2967 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2970 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2972 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2973 &data->args.seq_args,
2978 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2979 struct inode *new_dir)
2981 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2983 if (!nfs4_sequence_done(task, &res->seq_res))
2985 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2988 update_changeattr(old_dir, &res->old_cinfo);
2989 update_changeattr(new_dir, &res->new_cinfo);
2993 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2994 struct inode *new_dir, struct qstr *new_name)
2996 struct nfs_server *server = NFS_SERVER(old_dir);
2997 struct nfs_renameargs arg = {
2998 .old_dir = NFS_FH(old_dir),
2999 .new_dir = NFS_FH(new_dir),
3000 .old_name = old_name,
3001 .new_name = new_name,
3003 struct nfs_renameres res = {
3006 struct rpc_message msg = {
3007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3011 int status = -ENOMEM;
3013 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3015 update_changeattr(old_dir, &res.old_cinfo);
3016 update_changeattr(new_dir, &res.new_cinfo);
3021 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3022 struct inode *new_dir, struct qstr *new_name)
3024 struct nfs4_exception exception = { };
3027 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3028 _nfs4_proc_rename(old_dir, old_name,
3031 } while (exception.retry);
3035 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3037 struct nfs_server *server = NFS_SERVER(inode);
3038 struct nfs4_link_arg arg = {
3039 .fh = NFS_FH(inode),
3040 .dir_fh = NFS_FH(dir),
3042 .bitmask = server->attr_bitmask,
3044 struct nfs4_link_res res = {
3047 struct rpc_message msg = {
3048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3052 int status = -ENOMEM;
3054 res.fattr = nfs_alloc_fattr();
3055 if (res.fattr == NULL)
3058 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3060 update_changeattr(dir, &res.cinfo);
3061 nfs_post_op_update_inode(inode, res.fattr);
3064 nfs_free_fattr(res.fattr);
3068 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3070 struct nfs4_exception exception = { };
3073 err = nfs4_handle_exception(NFS_SERVER(inode),
3074 _nfs4_proc_link(inode, dir, name),
3076 } while (exception.retry);
3080 struct nfs4_createdata {
3081 struct rpc_message msg;
3082 struct nfs4_create_arg arg;
3083 struct nfs4_create_res res;
3085 struct nfs_fattr fattr;
3088 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3089 struct qstr *name, struct iattr *sattr, u32 ftype)
3091 struct nfs4_createdata *data;
3093 data = kzalloc(sizeof(*data), GFP_KERNEL);
3095 struct nfs_server *server = NFS_SERVER(dir);
3097 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3098 data->msg.rpc_argp = &data->arg;
3099 data->msg.rpc_resp = &data->res;
3100 data->arg.dir_fh = NFS_FH(dir);
3101 data->arg.server = server;
3102 data->arg.name = name;
3103 data->arg.attrs = sattr;
3104 data->arg.ftype = ftype;
3105 data->arg.bitmask = server->attr_bitmask;
3106 data->res.server = server;
3107 data->res.fh = &data->fh;
3108 data->res.fattr = &data->fattr;
3109 nfs_fattr_init(data->res.fattr);
3114 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3116 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3117 &data->arg.seq_args, &data->res.seq_res, 1);
3119 update_changeattr(dir, &data->res.dir_cinfo);
3120 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3125 static void nfs4_free_createdata(struct nfs4_createdata *data)
3130 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3131 struct page *page, unsigned int len, struct iattr *sattr)
3133 struct nfs4_createdata *data;
3134 int status = -ENAMETOOLONG;
3136 if (len > NFS4_MAXPATHLEN)
3140 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3144 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3145 data->arg.u.symlink.pages = &page;
3146 data->arg.u.symlink.len = len;
3148 status = nfs4_do_create(dir, dentry, data);
3150 nfs4_free_createdata(data);
3155 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3156 struct page *page, unsigned int len, struct iattr *sattr)
3158 struct nfs4_exception exception = { };
3161 err = nfs4_handle_exception(NFS_SERVER(dir),
3162 _nfs4_proc_symlink(dir, dentry, page,
3165 } while (exception.retry);
3169 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3170 struct iattr *sattr)
3172 struct nfs4_createdata *data;
3173 int status = -ENOMEM;
3175 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3179 status = nfs4_do_create(dir, dentry, data);
3181 nfs4_free_createdata(data);
3186 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3187 struct iattr *sattr)
3189 struct nfs4_exception exception = { };
3192 sattr->ia_mode &= ~current_umask();
3194 err = nfs4_handle_exception(NFS_SERVER(dir),
3195 _nfs4_proc_mkdir(dir, dentry, sattr),
3197 } while (exception.retry);
3201 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3202 u64 cookie, struct page **pages, unsigned int count, int plus)
3204 struct inode *dir = dentry->d_inode;
3205 struct nfs4_readdir_arg args = {
3210 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3213 struct nfs4_readdir_res res;
3214 struct rpc_message msg = {
3215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3222 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3223 dentry->d_parent->d_name.name,
3224 dentry->d_name.name,
3225 (unsigned long long)cookie);
3226 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3227 res.pgbase = args.pgbase;
3228 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3230 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3231 status += args.pgbase;
3234 nfs_invalidate_atime(dir);
3236 dprintk("%s: returns %d\n", __func__, status);
3240 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3241 u64 cookie, struct page **pages, unsigned int count, int plus)
3243 struct nfs4_exception exception = { };
3246 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3247 _nfs4_proc_readdir(dentry, cred, cookie,
3248 pages, count, plus),
3250 } while (exception.retry);
3254 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3255 struct iattr *sattr, dev_t rdev)
3257 struct nfs4_createdata *data;
3258 int mode = sattr->ia_mode;
3259 int status = -ENOMEM;
3261 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3266 data->arg.ftype = NF4FIFO;
3267 else if (S_ISBLK(mode)) {
3268 data->arg.ftype = NF4BLK;
3269 data->arg.u.device.specdata1 = MAJOR(rdev);
3270 data->arg.u.device.specdata2 = MINOR(rdev);
3272 else if (S_ISCHR(mode)) {
3273 data->arg.ftype = NF4CHR;
3274 data->arg.u.device.specdata1 = MAJOR(rdev);
3275 data->arg.u.device.specdata2 = MINOR(rdev);
3276 } else if (!S_ISSOCK(mode)) {
3281 status = nfs4_do_create(dir, dentry, data);
3283 nfs4_free_createdata(data);
3288 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3289 struct iattr *sattr, dev_t rdev)
3291 struct nfs4_exception exception = { };
3294 sattr->ia_mode &= ~current_umask();
3296 err = nfs4_handle_exception(NFS_SERVER(dir),
3297 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3299 } while (exception.retry);
3303 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3304 struct nfs_fsstat *fsstat)
3306 struct nfs4_statfs_arg args = {
3308 .bitmask = server->attr_bitmask,
3310 struct nfs4_statfs_res res = {
3313 struct rpc_message msg = {
3314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3319 nfs_fattr_init(fsstat->fattr);
3320 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3323 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3325 struct nfs4_exception exception = { };
3328 err = nfs4_handle_exception(server,
3329 _nfs4_proc_statfs(server, fhandle, fsstat),
3331 } while (exception.retry);
3335 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3336 struct nfs_fsinfo *fsinfo)
3338 struct nfs4_fsinfo_arg args = {
3340 .bitmask = server->attr_bitmask,
3342 struct nfs4_fsinfo_res res = {
3345 struct rpc_message msg = {
3346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3351 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3354 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3356 struct nfs4_exception exception = { };
3360 err = nfs4_handle_exception(server,
3361 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3363 } while (exception.retry);
3367 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3371 nfs_fattr_init(fsinfo->fattr);
3372 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3374 /* block layout checks this! */
3375 server->pnfs_blksize = fsinfo->blksize;
3376 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3382 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3383 struct nfs_pathconf *pathconf)
3385 struct nfs4_pathconf_arg args = {
3387 .bitmask = server->attr_bitmask,
3389 struct nfs4_pathconf_res res = {
3390 .pathconf = pathconf,
3392 struct rpc_message msg = {
3393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3398 /* None of the pathconf attributes are mandatory to implement */
3399 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3400 memset(pathconf, 0, sizeof(*pathconf));
3404 nfs_fattr_init(pathconf->fattr);
3405 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3408 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3409 struct nfs_pathconf *pathconf)
3411 struct nfs4_exception exception = { };
3415 err = nfs4_handle_exception(server,
3416 _nfs4_proc_pathconf(server, fhandle, pathconf),
3418 } while (exception.retry);
3422 void __nfs4_read_done_cb(struct nfs_read_data *data)
3424 nfs_invalidate_atime(data->header->inode);
3427 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3429 struct nfs_server *server = NFS_SERVER(data->header->inode);
3431 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3432 rpc_restart_call_prepare(task);
3436 __nfs4_read_done_cb(data);
3437 if (task->tk_status > 0)
3438 renew_lease(server, data->timestamp);
3442 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3445 dprintk("--> %s\n", __func__);
3447 if (!nfs4_sequence_done(task, &data->res.seq_res))
3450 return data->read_done_cb ? data->read_done_cb(task, data) :
3451 nfs4_read_done_cb(task, data);
3454 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3456 data->timestamp = jiffies;
3457 data->read_done_cb = nfs4_read_done_cb;
3458 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3459 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3462 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3464 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3465 &data->args.seq_args,
3470 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3472 struct inode *inode = data->header->inode;
3474 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3475 rpc_restart_call_prepare(task);
3478 if (task->tk_status >= 0) {
3479 renew_lease(NFS_SERVER(inode), data->timestamp);
3480 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3485 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3487 if (!nfs4_sequence_done(task, &data->res.seq_res))
3489 return data->write_done_cb ? data->write_done_cb(task, data) :
3490 nfs4_write_done_cb(task, data);
3494 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3496 const struct nfs_pgio_header *hdr = data->header;
3498 /* Don't request attributes for pNFS or O_DIRECT writes */
3499 if (data->ds_clp != NULL || hdr->dreq != NULL)
3501 /* Otherwise, request attributes if and only if we don't hold
3504 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3507 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3509 struct nfs_server *server = NFS_SERVER(data->header->inode);
3511 if (!nfs4_write_need_cache_consistency_data(data)) {
3512 data->args.bitmask = NULL;
3513 data->res.fattr = NULL;
3515 data->args.bitmask = server->cache_consistency_bitmask;
3517 if (!data->write_done_cb)
3518 data->write_done_cb = nfs4_write_done_cb;
3519 data->res.server = server;
3520 data->timestamp = jiffies;
3522 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3523 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3526 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3528 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3529 &data->args.seq_args,
3534 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3536 nfs4_setup_sequence(NFS_SERVER(data->inode),
3537 &data->args.seq_args,
3542 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3544 struct inode *inode = data->inode;
3546 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3547 rpc_restart_call_prepare(task);
3553 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3555 if (!nfs4_sequence_done(task, &data->res.seq_res))
3557 return data->commit_done_cb(task, data);
3560 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3562 struct nfs_server *server = NFS_SERVER(data->inode);
3564 if (data->commit_done_cb == NULL)
3565 data->commit_done_cb = nfs4_commit_done_cb;
3566 data->res.server = server;
3567 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3568 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3571 struct nfs4_renewdata {
3572 struct nfs_client *client;
3573 unsigned long timestamp;
3577 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3578 * standalone procedure for queueing an asynchronous RENEW.
3580 static void nfs4_renew_release(void *calldata)
3582 struct nfs4_renewdata *data = calldata;
3583 struct nfs_client *clp = data->client;
3585 if (atomic_read(&clp->cl_count) > 1)
3586 nfs4_schedule_state_renewal(clp);
3587 nfs_put_client(clp);
3591 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3593 struct nfs4_renewdata *data = calldata;
3594 struct nfs_client *clp = data->client;
3595 unsigned long timestamp = data->timestamp;
3597 if (task->tk_status < 0) {
3598 /* Unless we're shutting down, schedule state recovery! */
3599 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3601 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3602 nfs4_schedule_lease_recovery(clp);
3605 nfs4_schedule_path_down_recovery(clp);
3607 do_renew_lease(clp, timestamp);
3610 static const struct rpc_call_ops nfs4_renew_ops = {
3611 .rpc_call_done = nfs4_renew_done,
3612 .rpc_release = nfs4_renew_release,
3615 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3617 struct rpc_message msg = {
3618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3622 struct nfs4_renewdata *data;
3624 if (renew_flags == 0)
3626 if (!atomic_inc_not_zero(&clp->cl_count))
3628 data = kmalloc(sizeof(*data), GFP_NOFS);
3632 data->timestamp = jiffies;
3633 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3634 &nfs4_renew_ops, data);
3637 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3639 struct rpc_message msg = {
3640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3644 unsigned long now = jiffies;
3647 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3650 do_renew_lease(clp, now);
3654 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3656 return (server->caps & NFS_CAP_ACLS)
3657 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3658 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3661 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3662 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3665 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3667 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3668 struct page **pages, unsigned int *pgbase)
3670 struct page *newpage, **spages;
3676 len = min_t(size_t, PAGE_SIZE, buflen);
3677 newpage = alloc_page(GFP_KERNEL);
3679 if (newpage == NULL)
3681 memcpy(page_address(newpage), buf, len);
3686 } while (buflen != 0);
3692 __free_page(spages[rc-1]);
3696 struct nfs4_cached_acl {
3702 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3704 struct nfs_inode *nfsi = NFS_I(inode);
3706 spin_lock(&inode->i_lock);
3707 kfree(nfsi->nfs4_acl);
3708 nfsi->nfs4_acl = acl;
3709 spin_unlock(&inode->i_lock);
3712 static void nfs4_zap_acl_attr(struct inode *inode)
3714 nfs4_set_cached_acl(inode, NULL);
3717 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3719 struct nfs_inode *nfsi = NFS_I(inode);
3720 struct nfs4_cached_acl *acl;
3723 spin_lock(&inode->i_lock);
3724 acl = nfsi->nfs4_acl;
3727 if (buf == NULL) /* user is just asking for length */
3729 if (acl->cached == 0)
3731 ret = -ERANGE; /* see getxattr(2) man page */
3732 if (acl->len > buflen)
3734 memcpy(buf, acl->data, acl->len);
3738 spin_unlock(&inode->i_lock);
3742 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3744 struct nfs4_cached_acl *acl;
3745 size_t buflen = sizeof(*acl) + acl_len;
3747 if (buflen <= PAGE_SIZE) {
3748 acl = kmalloc(buflen, GFP_KERNEL);
3752 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3754 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3761 nfs4_set_cached_acl(inode, acl);
3765 * The getxattr API returns the required buffer length when called with a
3766 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3767 * the required buf. On a NULL buf, we send a page of data to the server
3768 * guessing that the ACL request can be serviced by a page. If so, we cache
3769 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3770 * the cache. If not so, we throw away the page, and cache the required
3771 * length. The next getxattr call will then produce another round trip to
3772 * the server, this time with the input buf of the required size.
3774 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3776 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3777 struct nfs_getaclargs args = {
3778 .fh = NFS_FH(inode),
3782 struct nfs_getaclres res = {
3785 struct rpc_message msg = {
3786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3790 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3791 int ret = -ENOMEM, i;
3793 /* As long as we're doing a round trip to the server anyway,
3794 * let's be prepared for a page of acl data. */
3797 if (npages > ARRAY_SIZE(pages))
3800 for (i = 0; i < npages; i++) {
3801 pages[i] = alloc_page(GFP_KERNEL);
3806 /* for decoding across pages */
3807 res.acl_scratch = alloc_page(GFP_KERNEL);
3808 if (!res.acl_scratch)
3811 args.acl_len = npages * PAGE_SIZE;
3812 args.acl_pgbase = 0;
3814 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3815 __func__, buf, buflen, npages, args.acl_len);
3816 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3817 &msg, &args.seq_args, &res.seq_res, 0);
3821 /* Handle the case where the passed-in buffer is too short */
3822 if (res.acl_flags & NFS4_ACL_TRUNC) {
3823 /* Did the user only issue a request for the acl length? */
3829 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3831 if (res.acl_len > buflen) {
3835 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3840 for (i = 0; i < npages; i++)
3842 __free_page(pages[i]);
3843 if (res.acl_scratch)
3844 __free_page(res.acl_scratch);
3848 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3850 struct nfs4_exception exception = { };
3853 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3856 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3857 } while (exception.retry);
3861 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3863 struct nfs_server *server = NFS_SERVER(inode);
3866 if (!nfs4_server_supports_acls(server))
3868 ret = nfs_revalidate_inode(server, inode);
3871 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3872 nfs_zap_acl_cache(inode);
3873 ret = nfs4_read_cached_acl(inode, buf, buflen);
3875 /* -ENOENT is returned if there is no ACL or if there is an ACL
3876 * but no cached acl data, just the acl length */
3878 return nfs4_get_acl_uncached(inode, buf, buflen);
3881 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3883 struct nfs_server *server = NFS_SERVER(inode);
3884 struct page *pages[NFS4ACL_MAXPAGES];
3885 struct nfs_setaclargs arg = {
3886 .fh = NFS_FH(inode),
3890 struct nfs_setaclres res;
3891 struct rpc_message msg = {
3892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3896 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3899 if (!nfs4_server_supports_acls(server))
3901 if (npages > ARRAY_SIZE(pages))
3903 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3906 nfs4_inode_return_delegation(inode);
3907 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3910 * Free each page after tx, so the only ref left is
3911 * held by the network stack
3914 put_page(pages[i-1]);
3917 * Acl update can result in inode attribute update.
3918 * so mark the attribute cache invalid.
3920 spin_lock(&inode->i_lock);
3921 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3922 spin_unlock(&inode->i_lock);
3923 nfs_access_zap_cache(inode);
3924 nfs_zap_acl_cache(inode);
3928 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3930 struct nfs4_exception exception = { };
3933 err = nfs4_handle_exception(NFS_SERVER(inode),
3934 __nfs4_proc_set_acl(inode, buf, buflen),
3936 } while (exception.retry);
3941 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3943 struct nfs_client *clp = server->nfs_client;
3945 if (task->tk_status >= 0)
3947 switch(task->tk_status) {
3948 case -NFS4ERR_DELEG_REVOKED:
3949 case -NFS4ERR_ADMIN_REVOKED:
3950 case -NFS4ERR_BAD_STATEID:
3953 nfs_remove_bad_delegation(state->inode);
3954 case -NFS4ERR_OPENMODE:
3957 nfs4_schedule_stateid_recovery(server, state);
3958 goto wait_on_recovery;
3959 case -NFS4ERR_EXPIRED:
3961 nfs4_schedule_stateid_recovery(server, state);
3962 case -NFS4ERR_STALE_STATEID:
3963 case -NFS4ERR_STALE_CLIENTID:
3964 nfs4_schedule_lease_recovery(clp);
3965 goto wait_on_recovery;
3966 #if defined(CONFIG_NFS_V4_1)
3967 case -NFS4ERR_BADSESSION:
3968 case -NFS4ERR_BADSLOT:
3969 case -NFS4ERR_BAD_HIGH_SLOT:
3970 case -NFS4ERR_DEADSESSION:
3971 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3972 case -NFS4ERR_SEQ_FALSE_RETRY:
3973 case -NFS4ERR_SEQ_MISORDERED:
3974 dprintk("%s ERROR %d, Reset session\n", __func__,
3976 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
3977 task->tk_status = 0;
3979 #endif /* CONFIG_NFS_V4_1 */
3980 case -NFS4ERR_DELAY:
3981 nfs_inc_server_stats(server, NFSIOS_DELAY);
3982 case -NFS4ERR_GRACE:
3983 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3984 task->tk_status = 0;
3986 case -NFS4ERR_RETRY_UNCACHED_REP:
3987 case -NFS4ERR_OLD_STATEID:
3988 task->tk_status = 0;
3991 task->tk_status = nfs4_map_errors(task->tk_status);
3994 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3995 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3996 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3997 task->tk_status = 0;
4001 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4002 nfs4_verifier *bootverf)
4006 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4007 /* An impossible timestamp guarantees this value
4008 * will never match a generated boot time. */
4010 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4012 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4013 verf[0] = (__be32)nn->boot_time.tv_sec;
4014 verf[1] = (__be32)nn->boot_time.tv_nsec;
4016 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4020 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4021 char *buf, size_t len)
4023 unsigned int result;
4026 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4028 rpc_peeraddr2str(clp->cl_rpcclient,
4030 rpc_peeraddr2str(clp->cl_rpcclient,
4031 RPC_DISPLAY_PROTO));
4037 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4038 char *buf, size_t len)
4040 char *nodename = clp->cl_rpcclient->cl_nodename;
4042 if (nfs4_client_id_uniquifier[0] != '\0')
4043 nodename = nfs4_client_id_uniquifier;
4044 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4045 clp->rpc_ops->version, clp->cl_minorversion,
4050 * nfs4_proc_setclientid - Negotiate client ID
4051 * @clp: state data structure
4052 * @program: RPC program for NFSv4 callback service
4053 * @port: IP port number for NFS4 callback service
4054 * @cred: RPC credential to use for this call
4055 * @res: where to place the result
4057 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4059 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4060 unsigned short port, struct rpc_cred *cred,
4061 struct nfs4_setclientid_res *res)
4063 nfs4_verifier sc_verifier;
4064 struct nfs4_setclientid setclientid = {
4065 .sc_verifier = &sc_verifier,
4067 .sc_cb_ident = clp->cl_cb_ident,
4069 struct rpc_message msg = {
4070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4071 .rpc_argp = &setclientid,
4077 /* nfs_client_id4 */
4078 nfs4_init_boot_verifier(clp, &sc_verifier);
4079 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4080 setclientid.sc_name_len =
4081 nfs4_init_uniform_client_string(clp,
4082 setclientid.sc_name,
4083 sizeof(setclientid.sc_name));
4085 setclientid.sc_name_len =
4086 nfs4_init_nonuniform_client_string(clp,
4087 setclientid.sc_name,
4088 sizeof(setclientid.sc_name));
4091 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4092 sizeof(setclientid.sc_netid),
4093 rpc_peeraddr2str(clp->cl_rpcclient,
4094 RPC_DISPLAY_NETID));
4096 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4097 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4098 clp->cl_ipaddr, port >> 8, port & 255);
4100 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4101 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4102 setclientid.sc_name_len, setclientid.sc_name);
4103 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4104 dprintk("NFS reply setclientid: %d\n", status);
4109 * nfs4_proc_setclientid_confirm - Confirm client ID
4110 * @clp: state data structure
4111 * @res: result of a previous SETCLIENTID
4112 * @cred: RPC credential to use for this call
4114 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4116 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4117 struct nfs4_setclientid_res *arg,
4118 struct rpc_cred *cred)
4120 struct nfs_fsinfo fsinfo;
4121 struct rpc_message msg = {
4122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4124 .rpc_resp = &fsinfo,
4130 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4131 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4134 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4136 spin_lock(&clp->cl_lock);
4137 clp->cl_lease_time = fsinfo.lease_time * HZ;
4138 clp->cl_last_renewal = now;
4139 spin_unlock(&clp->cl_lock);
4141 dprintk("NFS reply setclientid_confirm: %d\n", status);
4145 struct nfs4_delegreturndata {
4146 struct nfs4_delegreturnargs args;
4147 struct nfs4_delegreturnres res;
4149 nfs4_stateid stateid;
4150 unsigned long timestamp;
4151 struct nfs_fattr fattr;
4155 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4157 struct nfs4_delegreturndata *data = calldata;
4159 if (!nfs4_sequence_done(task, &data->res.seq_res))
4162 switch (task->tk_status) {
4163 case -NFS4ERR_STALE_STATEID:
4164 case -NFS4ERR_EXPIRED:
4166 renew_lease(data->res.server, data->timestamp);
4169 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4171 rpc_restart_call_prepare(task);
4175 data->rpc_status = task->tk_status;
4178 static void nfs4_delegreturn_release(void *calldata)
4183 #if defined(CONFIG_NFS_V4_1)
4184 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4186 struct nfs4_delegreturndata *d_data;
4188 d_data = (struct nfs4_delegreturndata *)data;
4190 nfs4_setup_sequence(d_data->res.server,
4191 &d_data->args.seq_args,
4192 &d_data->res.seq_res,
4195 #endif /* CONFIG_NFS_V4_1 */
4197 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4198 #if defined(CONFIG_NFS_V4_1)
4199 .rpc_call_prepare = nfs4_delegreturn_prepare,
4200 #endif /* CONFIG_NFS_V4_1 */
4201 .rpc_call_done = nfs4_delegreturn_done,
4202 .rpc_release = nfs4_delegreturn_release,
4205 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4207 struct nfs4_delegreturndata *data;
4208 struct nfs_server *server = NFS_SERVER(inode);
4209 struct rpc_task *task;
4210 struct rpc_message msg = {
4211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4214 struct rpc_task_setup task_setup_data = {
4215 .rpc_client = server->client,
4216 .rpc_message = &msg,
4217 .callback_ops = &nfs4_delegreturn_ops,
4218 .flags = RPC_TASK_ASYNC,
4222 data = kzalloc(sizeof(*data), GFP_NOFS);
4225 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4226 data->args.fhandle = &data->fh;
4227 data->args.stateid = &data->stateid;
4228 data->args.bitmask = server->cache_consistency_bitmask;
4229 nfs_copy_fh(&data->fh, NFS_FH(inode));
4230 nfs4_stateid_copy(&data->stateid, stateid);
4231 data->res.fattr = &data->fattr;
4232 data->res.server = server;
4233 nfs_fattr_init(data->res.fattr);
4234 data->timestamp = jiffies;
4235 data->rpc_status = 0;
4237 task_setup_data.callback_data = data;
4238 msg.rpc_argp = &data->args;
4239 msg.rpc_resp = &data->res;
4240 task = rpc_run_task(&task_setup_data);
4242 return PTR_ERR(task);
4245 status = nfs4_wait_for_completion_rpc_task(task);
4248 status = data->rpc_status;
4250 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4252 nfs_refresh_inode(inode, &data->fattr);
4258 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4260 struct nfs_server *server = NFS_SERVER(inode);
4261 struct nfs4_exception exception = { };
4264 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4266 case -NFS4ERR_STALE_STATEID:
4267 case -NFS4ERR_EXPIRED:
4271 err = nfs4_handle_exception(server, err, &exception);
4272 } while (exception.retry);
4276 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4277 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4280 * sleep, with exponential backoff, and retry the LOCK operation.
4282 static unsigned long
4283 nfs4_set_lock_task_retry(unsigned long timeout)
4285 freezable_schedule_timeout_killable(timeout);
4287 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4288 return NFS4_LOCK_MAXTIMEOUT;
4292 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4294 struct inode *inode = state->inode;
4295 struct nfs_server *server = NFS_SERVER(inode);
4296 struct nfs_client *clp = server->nfs_client;
4297 struct nfs_lockt_args arg = {
4298 .fh = NFS_FH(inode),
4301 struct nfs_lockt_res res = {
4304 struct rpc_message msg = {
4305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4308 .rpc_cred = state->owner->so_cred,
4310 struct nfs4_lock_state *lsp;
4313 arg.lock_owner.clientid = clp->cl_clientid;
4314 status = nfs4_set_lock_state(state, request);
4317 lsp = request->fl_u.nfs4_fl.owner;
4318 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4319 arg.lock_owner.s_dev = server->s_dev;
4320 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4323 request->fl_type = F_UNLCK;
4325 case -NFS4ERR_DENIED:
4328 request->fl_ops->fl_release_private(request);
4333 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4335 struct nfs4_exception exception = { };
4339 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4340 _nfs4_proc_getlk(state, cmd, request),
4342 } while (exception.retry);
4346 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4349 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4351 res = posix_lock_file_wait(file, fl);
4354 res = flock_lock_file_wait(file, fl);
4362 struct nfs4_unlockdata {
4363 struct nfs_locku_args arg;
4364 struct nfs_locku_res res;
4365 struct nfs4_lock_state *lsp;
4366 struct nfs_open_context *ctx;
4367 struct file_lock fl;
4368 const struct nfs_server *server;
4369 unsigned long timestamp;
4372 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4373 struct nfs_open_context *ctx,
4374 struct nfs4_lock_state *lsp,
4375 struct nfs_seqid *seqid)
4377 struct nfs4_unlockdata *p;
4378 struct inode *inode = lsp->ls_state->inode;
4380 p = kzalloc(sizeof(*p), GFP_NOFS);
4383 p->arg.fh = NFS_FH(inode);
4385 p->arg.seqid = seqid;
4386 p->res.seqid = seqid;
4387 p->arg.stateid = &lsp->ls_stateid;
4389 atomic_inc(&lsp->ls_count);
4390 /* Ensure we don't close file until we're done freeing locks! */
4391 p->ctx = get_nfs_open_context(ctx);
4392 memcpy(&p->fl, fl, sizeof(p->fl));
4393 p->server = NFS_SERVER(inode);
4397 static void nfs4_locku_release_calldata(void *data)
4399 struct nfs4_unlockdata *calldata = data;
4400 nfs_free_seqid(calldata->arg.seqid);
4401 nfs4_put_lock_state(calldata->lsp);
4402 put_nfs_open_context(calldata->ctx);
4406 static void nfs4_locku_done(struct rpc_task *task, void *data)
4408 struct nfs4_unlockdata *calldata = data;
4410 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4412 switch (task->tk_status) {
4414 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4415 &calldata->res.stateid);
4416 renew_lease(calldata->server, calldata->timestamp);
4418 case -NFS4ERR_BAD_STATEID:
4419 case -NFS4ERR_OLD_STATEID:
4420 case -NFS4ERR_STALE_STATEID:
4421 case -NFS4ERR_EXPIRED:
4424 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4425 rpc_restart_call_prepare(task);
4427 nfs_release_seqid(calldata->arg.seqid);
4430 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4432 struct nfs4_unlockdata *calldata = data;
4434 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4436 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4437 /* Note: exit _without_ running nfs4_locku_done */
4440 calldata->timestamp = jiffies;
4441 if (nfs4_setup_sequence(calldata->server,
4442 &calldata->arg.seq_args,
4443 &calldata->res.seq_res,
4445 nfs_release_seqid(calldata->arg.seqid);
4448 task->tk_action = NULL;
4450 nfs4_sequence_done(task, &calldata->res.seq_res);
4453 static const struct rpc_call_ops nfs4_locku_ops = {
4454 .rpc_call_prepare = nfs4_locku_prepare,
4455 .rpc_call_done = nfs4_locku_done,
4456 .rpc_release = nfs4_locku_release_calldata,
4459 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4460 struct nfs_open_context *ctx,
4461 struct nfs4_lock_state *lsp,
4462 struct nfs_seqid *seqid)
4464 struct nfs4_unlockdata *data;
4465 struct rpc_message msg = {
4466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4467 .rpc_cred = ctx->cred,
4469 struct rpc_task_setup task_setup_data = {
4470 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4471 .rpc_message = &msg,
4472 .callback_ops = &nfs4_locku_ops,
4473 .workqueue = nfsiod_workqueue,
4474 .flags = RPC_TASK_ASYNC,
4477 /* Ensure this is an unlock - when canceling a lock, the
4478 * canceled lock is passed in, and it won't be an unlock.
4480 fl->fl_type = F_UNLCK;
4482 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4484 nfs_free_seqid(seqid);
4485 return ERR_PTR(-ENOMEM);
4488 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4489 msg.rpc_argp = &data->arg;
4490 msg.rpc_resp = &data->res;
4491 task_setup_data.callback_data = data;
4492 return rpc_run_task(&task_setup_data);
4495 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4497 struct nfs_inode *nfsi = NFS_I(state->inode);
4498 struct nfs_seqid *seqid;
4499 struct nfs4_lock_state *lsp;
4500 struct rpc_task *task;
4502 unsigned char fl_flags = request->fl_flags;
4504 status = nfs4_set_lock_state(state, request);
4505 /* Unlock _before_ we do the RPC call */
4506 request->fl_flags |= FL_EXISTS;
4507 down_read(&nfsi->rwsem);
4508 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4509 up_read(&nfsi->rwsem);
4512 up_read(&nfsi->rwsem);
4515 /* Is this a delegated lock? */
4516 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4518 lsp = request->fl_u.nfs4_fl.owner;
4519 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4523 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4524 status = PTR_ERR(task);
4527 status = nfs4_wait_for_completion_rpc_task(task);
4530 request->fl_flags = fl_flags;
4534 struct nfs4_lockdata {
4535 struct nfs_lock_args arg;
4536 struct nfs_lock_res res;
4537 struct nfs4_lock_state *lsp;
4538 struct nfs_open_context *ctx;
4539 struct file_lock fl;
4540 unsigned long timestamp;
4543 struct nfs_server *server;
4546 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4547 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4550 struct nfs4_lockdata *p;
4551 struct inode *inode = lsp->ls_state->inode;
4552 struct nfs_server *server = NFS_SERVER(inode);
4554 p = kzalloc(sizeof(*p), gfp_mask);
4558 p->arg.fh = NFS_FH(inode);
4560 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4561 if (p->arg.open_seqid == NULL)
4563 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4564 if (p->arg.lock_seqid == NULL)
4565 goto out_free_seqid;
4566 p->arg.lock_stateid = &lsp->ls_stateid;
4567 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4568 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4569 p->arg.lock_owner.s_dev = server->s_dev;
4570 p->res.lock_seqid = p->arg.lock_seqid;
4573 atomic_inc(&lsp->ls_count);
4574 p->ctx = get_nfs_open_context(ctx);
4575 memcpy(&p->fl, fl, sizeof(p->fl));
4578 nfs_free_seqid(p->arg.open_seqid);
4584 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4586 struct nfs4_lockdata *data = calldata;
4587 struct nfs4_state *state = data->lsp->ls_state;
4589 dprintk("%s: begin!\n", __func__);
4590 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4592 /* Do we need to do an open_to_lock_owner? */
4593 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4594 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4595 goto out_release_lock_seqid;
4597 data->arg.open_stateid = &state->stateid;
4598 data->arg.new_lock_owner = 1;
4599 data->res.open_seqid = data->arg.open_seqid;
4601 data->arg.new_lock_owner = 0;
4602 data->timestamp = jiffies;
4603 if (nfs4_setup_sequence(data->server,
4604 &data->arg.seq_args,
4608 nfs_release_seqid(data->arg.open_seqid);
4609 out_release_lock_seqid:
4610 nfs_release_seqid(data->arg.lock_seqid);
4612 nfs4_sequence_done(task, &data->res.seq_res);
4613 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4616 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4618 struct nfs4_lockdata *data = calldata;
4620 dprintk("%s: begin!\n", __func__);
4622 if (!nfs4_sequence_done(task, &data->res.seq_res))
4625 data->rpc_status = task->tk_status;
4626 if (data->arg.new_lock_owner != 0) {
4627 if (data->rpc_status == 0)
4628 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4632 if (data->rpc_status == 0) {
4633 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4634 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4635 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4638 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4641 static void nfs4_lock_release(void *calldata)
4643 struct nfs4_lockdata *data = calldata;
4645 dprintk("%s: begin!\n", __func__);
4646 nfs_free_seqid(data->arg.open_seqid);
4647 if (data->cancelled != 0) {
4648 struct rpc_task *task;
4649 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4650 data->arg.lock_seqid);
4652 rpc_put_task_async(task);
4653 dprintk("%s: cancelling lock!\n", __func__);
4655 nfs_free_seqid(data->arg.lock_seqid);
4656 nfs4_put_lock_state(data->lsp);
4657 put_nfs_open_context(data->ctx);
4659 dprintk("%s: done!\n", __func__);
4662 static const struct rpc_call_ops nfs4_lock_ops = {
4663 .rpc_call_prepare = nfs4_lock_prepare,
4664 .rpc_call_done = nfs4_lock_done,
4665 .rpc_release = nfs4_lock_release,
4668 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4671 case -NFS4ERR_ADMIN_REVOKED:
4672 case -NFS4ERR_BAD_STATEID:
4673 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4674 if (new_lock_owner != 0 ||
4675 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4676 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4678 case -NFS4ERR_STALE_STATEID:
4679 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4680 case -NFS4ERR_EXPIRED:
4681 nfs4_schedule_lease_recovery(server->nfs_client);
4685 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4687 struct nfs4_lockdata *data;
4688 struct rpc_task *task;
4689 struct rpc_message msg = {
4690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4691 .rpc_cred = state->owner->so_cred,
4693 struct rpc_task_setup task_setup_data = {
4694 .rpc_client = NFS_CLIENT(state->inode),
4695 .rpc_message = &msg,
4696 .callback_ops = &nfs4_lock_ops,
4697 .workqueue = nfsiod_workqueue,
4698 .flags = RPC_TASK_ASYNC,
4702 dprintk("%s: begin!\n", __func__);
4703 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4704 fl->fl_u.nfs4_fl.owner,
4705 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4709 data->arg.block = 1;
4710 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4711 msg.rpc_argp = &data->arg;
4712 msg.rpc_resp = &data->res;
4713 task_setup_data.callback_data = data;
4714 if (recovery_type > NFS_LOCK_NEW) {
4715 if (recovery_type == NFS_LOCK_RECLAIM)
4716 data->arg.reclaim = NFS_LOCK_RECLAIM;
4717 nfs4_set_sequence_privileged(&data->arg.seq_args);
4719 task = rpc_run_task(&task_setup_data);
4721 return PTR_ERR(task);
4722 ret = nfs4_wait_for_completion_rpc_task(task);
4724 ret = data->rpc_status;
4726 nfs4_handle_setlk_error(data->server, data->lsp,
4727 data->arg.new_lock_owner, ret);
4729 data->cancelled = 1;
4731 dprintk("%s: done, ret = %d!\n", __func__, ret);
4735 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4737 struct nfs_server *server = NFS_SERVER(state->inode);
4738 struct nfs4_exception exception = {
4739 .inode = state->inode,
4744 /* Cache the lock if possible... */
4745 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4747 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4748 if (err != -NFS4ERR_DELAY)
4750 nfs4_handle_exception(server, err, &exception);
4751 } while (exception.retry);
4755 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4757 struct nfs_server *server = NFS_SERVER(state->inode);
4758 struct nfs4_exception exception = {
4759 .inode = state->inode,
4763 err = nfs4_set_lock_state(state, request);
4767 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4769 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4773 case -NFS4ERR_GRACE:
4774 case -NFS4ERR_DELAY:
4775 nfs4_handle_exception(server, err, &exception);
4778 } while (exception.retry);
4783 #if defined(CONFIG_NFS_V4_1)
4785 * nfs41_check_expired_locks - possibly free a lock stateid
4787 * @state: NFSv4 state for an inode
4789 * Returns NFS_OK if recovery for this stateid is now finished.
4790 * Otherwise a negative NFS4ERR value is returned.
4792 static int nfs41_check_expired_locks(struct nfs4_state *state)
4794 int status, ret = -NFS4ERR_BAD_STATEID;
4795 struct nfs4_lock_state *lsp;
4796 struct nfs_server *server = NFS_SERVER(state->inode);
4798 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4799 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4800 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4801 if (status != NFS_OK) {
4802 /* Free the stateid unless the server
4803 * informs us the stateid is unrecognized. */
4804 if (status != -NFS4ERR_BAD_STATEID)
4805 nfs41_free_stateid(server,
4807 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4816 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4818 int status = NFS_OK;
4820 if (test_bit(LK_STATE_IN_USE, &state->flags))
4821 status = nfs41_check_expired_locks(state);
4822 if (status != NFS_OK)
4823 status = nfs4_lock_expired(state, request);
4828 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4830 struct nfs_inode *nfsi = NFS_I(state->inode);
4831 unsigned char fl_flags = request->fl_flags;
4832 int status = -ENOLCK;
4834 if ((fl_flags & FL_POSIX) &&
4835 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4837 /* Is this a delegated open? */
4838 status = nfs4_set_lock_state(state, request);
4841 request->fl_flags |= FL_ACCESS;
4842 status = do_vfs_lock(request->fl_file, request);
4845 down_read(&nfsi->rwsem);
4846 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4847 /* Yes: cache locks! */
4848 /* ...but avoid races with delegation recall... */
4849 request->fl_flags = fl_flags & ~FL_SLEEP;
4850 status = do_vfs_lock(request->fl_file, request);
4853 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4856 /* Note: we always want to sleep here! */
4857 request->fl_flags = fl_flags | FL_SLEEP;
4858 if (do_vfs_lock(request->fl_file, request) < 0)
4859 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4860 "manager!\n", __func__);
4862 up_read(&nfsi->rwsem);
4864 request->fl_flags = fl_flags;
4868 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4870 struct nfs4_exception exception = {
4872 .inode = state->inode,
4877 err = _nfs4_proc_setlk(state, cmd, request);
4878 if (err == -NFS4ERR_DENIED)
4880 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4882 } while (exception.retry);
4887 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4889 struct nfs_open_context *ctx;
4890 struct nfs4_state *state;
4891 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4894 /* verify open state */
4895 ctx = nfs_file_open_context(filp);
4898 if (request->fl_start < 0 || request->fl_end < 0)
4901 if (IS_GETLK(cmd)) {
4903 return nfs4_proc_getlk(state, F_GETLK, request);
4907 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4910 if (request->fl_type == F_UNLCK) {
4912 return nfs4_proc_unlck(state, cmd, request);
4919 * Don't rely on the VFS having checked the file open mode,
4920 * since it won't do this for flock() locks.
4922 switch (request->fl_type) {
4924 if (!(filp->f_mode & FMODE_READ))
4928 if (!(filp->f_mode & FMODE_WRITE))
4933 status = nfs4_proc_setlk(state, cmd, request);
4934 if ((status != -EAGAIN) || IS_SETLK(cmd))
4936 timeout = nfs4_set_lock_task_retry(timeout);
4937 status = -ERESTARTSYS;
4940 } while(status < 0);
4944 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4946 struct nfs_server *server = NFS_SERVER(state->inode);
4947 struct nfs4_exception exception = { };
4950 err = nfs4_set_lock_state(state, fl);
4954 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4957 printk(KERN_ERR "NFS: %s: unhandled error "
4958 "%d.\n", __func__, err);
4962 case -NFS4ERR_EXPIRED:
4963 nfs4_schedule_stateid_recovery(server, state);
4964 case -NFS4ERR_STALE_CLIENTID:
4965 case -NFS4ERR_STALE_STATEID:
4966 nfs4_schedule_lease_recovery(server->nfs_client);
4968 case -NFS4ERR_BADSESSION:
4969 case -NFS4ERR_BADSLOT:
4970 case -NFS4ERR_BAD_HIGH_SLOT:
4971 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4972 case -NFS4ERR_DEADSESSION:
4973 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
4977 * The show must go on: exit, but mark the
4978 * stateid as needing recovery.
4980 case -NFS4ERR_DELEG_REVOKED:
4981 case -NFS4ERR_ADMIN_REVOKED:
4982 case -NFS4ERR_BAD_STATEID:
4983 case -NFS4ERR_OPENMODE:
4984 nfs4_schedule_stateid_recovery(server, state);
4988 case -NFS4ERR_DENIED:
4989 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4992 case -NFS4ERR_DELAY:
4995 err = nfs4_handle_exception(server, err, &exception);
4996 } while (exception.retry);
5001 struct nfs_release_lockowner_data {
5002 struct nfs4_lock_state *lsp;
5003 struct nfs_server *server;
5004 struct nfs_release_lockowner_args args;
5007 static void nfs4_release_lockowner_release(void *calldata)
5009 struct nfs_release_lockowner_data *data = calldata;
5010 nfs4_free_lock_state(data->server, data->lsp);
5014 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5015 .rpc_release = nfs4_release_lockowner_release,
5018 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5020 struct nfs_server *server = lsp->ls_state->owner->so_server;
5021 struct nfs_release_lockowner_data *data;
5022 struct rpc_message msg = {
5023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5026 if (server->nfs_client->cl_mvops->minor_version != 0)
5028 data = kmalloc(sizeof(*data), GFP_NOFS);
5032 data->server = server;
5033 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5034 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5035 data->args.lock_owner.s_dev = server->s_dev;
5036 msg.rpc_argp = &data->args;
5037 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5041 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5043 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5044 const void *buf, size_t buflen,
5045 int flags, int type)
5047 if (strcmp(key, "") != 0)
5050 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5053 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5054 void *buf, size_t buflen, int type)
5056 if (strcmp(key, "") != 0)
5059 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5062 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5063 size_t list_len, const char *name,
5064 size_t name_len, int type)
5066 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5068 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5071 if (list && len <= list_len)
5072 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5077 * nfs_fhget will use either the mounted_on_fileid or the fileid
5079 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5081 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5082 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5083 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5084 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5087 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5088 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5089 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5093 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5094 const struct qstr *name,
5095 struct nfs4_fs_locations *fs_locations,
5098 struct nfs_server *server = NFS_SERVER(dir);
5100 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5102 struct nfs4_fs_locations_arg args = {
5103 .dir_fh = NFS_FH(dir),
5108 struct nfs4_fs_locations_res res = {
5109 .fs_locations = fs_locations,
5111 struct rpc_message msg = {
5112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5118 dprintk("%s: start\n", __func__);
5120 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5121 * is not supported */
5122 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5123 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5125 bitmask[0] |= FATTR4_WORD0_FILEID;
5127 nfs_fattr_init(&fs_locations->fattr);
5128 fs_locations->server = server;
5129 fs_locations->nlocations = 0;
5130 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5131 dprintk("%s: returned status = %d\n", __func__, status);
5135 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5136 const struct qstr *name,
5137 struct nfs4_fs_locations *fs_locations,
5140 struct nfs4_exception exception = { };
5143 err = nfs4_handle_exception(NFS_SERVER(dir),
5144 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5146 } while (exception.retry);
5150 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5153 struct nfs4_secinfo_arg args = {
5154 .dir_fh = NFS_FH(dir),
5157 struct nfs4_secinfo_res res = {
5160 struct rpc_message msg = {
5161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5166 dprintk("NFS call secinfo %s\n", name->name);
5167 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5168 dprintk("NFS reply secinfo: %d\n", status);
5172 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5173 struct nfs4_secinfo_flavors *flavors)
5175 struct nfs4_exception exception = { };
5178 err = nfs4_handle_exception(NFS_SERVER(dir),
5179 _nfs4_proc_secinfo(dir, name, flavors),
5181 } while (exception.retry);
5185 #ifdef CONFIG_NFS_V4_1
5187 * Check the exchange flags returned by the server for invalid flags, having
5188 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5191 static int nfs4_check_cl_exchange_flags(u32 flags)
5193 if (flags & ~EXCHGID4_FLAG_MASK_R)
5195 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5196 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5198 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5202 return -NFS4ERR_INVAL;
5206 nfs41_same_server_scope(struct nfs41_server_scope *a,
5207 struct nfs41_server_scope *b)
5209 if (a->server_scope_sz == b->server_scope_sz &&
5210 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5217 * nfs4_proc_bind_conn_to_session()
5219 * The 4.1 client currently uses the same TCP connection for the
5220 * fore and backchannel.
5222 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5225 struct nfs41_bind_conn_to_session_res res;
5226 struct rpc_message msg = {
5228 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5234 dprintk("--> %s\n", __func__);
5236 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5237 if (unlikely(res.session == NULL)) {
5242 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5244 if (memcmp(res.session->sess_id.data,
5245 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5246 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5250 if (res.dir != NFS4_CDFS4_BOTH) {
5251 dprintk("NFS: %s: Unexpected direction from server\n",
5256 if (res.use_conn_in_rdma_mode) {
5257 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5266 dprintk("<-- %s status= %d\n", __func__, status);
5271 * nfs4_proc_exchange_id()
5273 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5275 * Since the clientid has expired, all compounds using sessions
5276 * associated with the stale clientid will be returning
5277 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5278 * be in some phase of session reset.
5280 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5282 nfs4_verifier verifier;
5283 struct nfs41_exchange_id_args args = {
5284 .verifier = &verifier,
5286 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5288 struct nfs41_exchange_id_res res = {
5292 struct rpc_message msg = {
5293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5299 nfs4_init_boot_verifier(clp, &verifier);
5300 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5302 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5303 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5304 args.id_len, args.id);
5306 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5308 if (unlikely(res.server_owner == NULL)) {
5313 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5315 if (unlikely(res.server_scope == NULL)) {
5317 goto out_server_owner;
5320 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5321 if (unlikely(res.impl_id == NULL)) {
5323 goto out_server_scope;
5326 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5328 status = nfs4_check_cl_exchange_flags(res.flags);
5331 clp->cl_clientid = res.clientid;
5332 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5333 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5334 clp->cl_seqid = res.seqid;
5336 kfree(clp->cl_serverowner);
5337 clp->cl_serverowner = res.server_owner;
5338 res.server_owner = NULL;
5340 /* use the most recent implementation id */
5341 kfree(clp->cl_implid);
5342 clp->cl_implid = res.impl_id;
5344 if (clp->cl_serverscope != NULL &&
5345 !nfs41_same_server_scope(clp->cl_serverscope,
5346 res.server_scope)) {
5347 dprintk("%s: server_scope mismatch detected\n",
5349 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5350 kfree(clp->cl_serverscope);
5351 clp->cl_serverscope = NULL;
5354 if (clp->cl_serverscope == NULL) {
5355 clp->cl_serverscope = res.server_scope;
5362 kfree(res.server_owner);
5364 kfree(res.server_scope);
5366 if (clp->cl_implid != NULL)
5367 dprintk("NFS reply exchange_id: Server Implementation ID: "
5368 "domain: %s, name: %s, date: %llu,%u\n",
5369 clp->cl_implid->domain, clp->cl_implid->name,
5370 clp->cl_implid->date.seconds,
5371 clp->cl_implid->date.nseconds);
5372 dprintk("NFS reply exchange_id: %d\n", status);
5376 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5377 struct rpc_cred *cred)
5379 struct rpc_message msg = {
5380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5386 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5388 dprintk("NFS: Got error %d from the server %s on "
5389 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5393 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5394 struct rpc_cred *cred)
5399 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5400 ret = _nfs4_proc_destroy_clientid(clp, cred);
5402 case -NFS4ERR_DELAY:
5403 case -NFS4ERR_CLIENTID_BUSY:
5413 int nfs4_destroy_clientid(struct nfs_client *clp)
5415 struct rpc_cred *cred;
5418 if (clp->cl_mvops->minor_version < 1)
5420 if (clp->cl_exchange_flags == 0)
5422 if (clp->cl_preserve_clid)
5424 cred = nfs4_get_exchange_id_cred(clp);
5425 ret = nfs4_proc_destroy_clientid(clp, cred);
5430 case -NFS4ERR_STALE_CLIENTID:
5431 clp->cl_exchange_flags = 0;
5437 struct nfs4_get_lease_time_data {
5438 struct nfs4_get_lease_time_args *args;
5439 struct nfs4_get_lease_time_res *res;
5440 struct nfs_client *clp;
5443 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5446 struct nfs4_get_lease_time_data *data =
5447 (struct nfs4_get_lease_time_data *)calldata;
5449 dprintk("--> %s\n", __func__);
5450 /* just setup sequence, do not trigger session recovery
5451 since we're invoked within one */
5452 nfs41_setup_sequence(data->clp->cl_session,
5453 &data->args->la_seq_args,
5454 &data->res->lr_seq_res,
5456 dprintk("<-- %s\n", __func__);
5460 * Called from nfs4_state_manager thread for session setup, so don't recover
5461 * from sequence operation or clientid errors.
5463 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5465 struct nfs4_get_lease_time_data *data =
5466 (struct nfs4_get_lease_time_data *)calldata;
5468 dprintk("--> %s\n", __func__);
5469 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5471 switch (task->tk_status) {
5472 case -NFS4ERR_DELAY:
5473 case -NFS4ERR_GRACE:
5474 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5475 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5476 task->tk_status = 0;
5478 case -NFS4ERR_RETRY_UNCACHED_REP:
5479 rpc_restart_call_prepare(task);
5482 dprintk("<-- %s\n", __func__);
5485 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5486 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5487 .rpc_call_done = nfs4_get_lease_time_done,
5490 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5492 struct rpc_task *task;
5493 struct nfs4_get_lease_time_args args;
5494 struct nfs4_get_lease_time_res res = {
5495 .lr_fsinfo = fsinfo,
5497 struct nfs4_get_lease_time_data data = {
5502 struct rpc_message msg = {
5503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5507 struct rpc_task_setup task_setup = {
5508 .rpc_client = clp->cl_rpcclient,
5509 .rpc_message = &msg,
5510 .callback_ops = &nfs4_get_lease_time_ops,
5511 .callback_data = &data,
5512 .flags = RPC_TASK_TIMEOUT,
5516 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5517 nfs4_set_sequence_privileged(&args.la_seq_args);
5518 dprintk("--> %s\n", __func__);
5519 task = rpc_run_task(&task_setup);
5522 status = PTR_ERR(task);
5524 status = task->tk_status;
5527 dprintk("<-- %s return %d\n", __func__, status);
5533 * Initialize the values to be used by the client in CREATE_SESSION
5534 * If nfs4_init_session set the fore channel request and response sizes,
5537 * Set the back channel max_resp_sz_cached to zero to force the client to
5538 * always set csa_cachethis to FALSE because the current implementation
5539 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5541 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5543 struct nfs4_session *session = args->client->cl_session;
5544 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5545 mxresp_sz = session->fc_target_max_resp_sz;
5548 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5550 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5551 /* Fore channel attributes */
5552 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5553 args->fc_attrs.max_resp_sz = mxresp_sz;
5554 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5555 args->fc_attrs.max_reqs = max_session_slots;
5557 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5558 "max_ops=%u max_reqs=%u\n",
5560 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5561 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5563 /* Back channel attributes */
5564 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5565 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5566 args->bc_attrs.max_resp_sz_cached = 0;
5567 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5568 args->bc_attrs.max_reqs = 1;
5570 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5571 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5573 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5574 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5575 args->bc_attrs.max_reqs);
5578 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5580 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5581 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5583 if (rcvd->max_resp_sz > sent->max_resp_sz)
5586 * Our requested max_ops is the minimum we need; we're not
5587 * prepared to break up compounds into smaller pieces than that.
5588 * So, no point even trying to continue if the server won't
5591 if (rcvd->max_ops < sent->max_ops)
5593 if (rcvd->max_reqs == 0)
5595 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5596 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5600 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5602 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5603 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5605 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5607 if (rcvd->max_resp_sz < sent->max_resp_sz)
5609 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5611 /* These would render the backchannel useless: */
5612 if (rcvd->max_ops != sent->max_ops)
5614 if (rcvd->max_reqs != sent->max_reqs)
5619 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5620 struct nfs4_session *session)
5624 ret = nfs4_verify_fore_channel_attrs(args, session);
5627 return nfs4_verify_back_channel_attrs(args, session);
5630 static int _nfs4_proc_create_session(struct nfs_client *clp,
5631 struct rpc_cred *cred)
5633 struct nfs4_session *session = clp->cl_session;
5634 struct nfs41_create_session_args args = {
5636 .cb_program = NFS4_CALLBACK,
5638 struct nfs41_create_session_res res = {
5641 struct rpc_message msg = {
5642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5649 nfs4_init_channel_attrs(&args);
5650 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5652 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5655 /* Verify the session's negotiated channel_attrs values */
5656 status = nfs4_verify_channel_attrs(&args, session);
5657 /* Increment the clientid slot sequence id */
5665 * Issues a CREATE_SESSION operation to the server.
5666 * It is the responsibility of the caller to verify the session is
5667 * expired before calling this routine.
5669 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5673 struct nfs4_session *session = clp->cl_session;
5675 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5677 status = _nfs4_proc_create_session(clp, cred);
5681 /* Init or reset the session slot tables */
5682 status = nfs4_setup_session_slot_tables(session);
5683 dprintk("slot table setup returned %d\n", status);
5687 ptr = (unsigned *)&session->sess_id.data[0];
5688 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5689 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5691 dprintk("<-- %s\n", __func__);
5696 * Issue the over-the-wire RPC DESTROY_SESSION.
5697 * The caller must serialize access to this routine.
5699 int nfs4_proc_destroy_session(struct nfs4_session *session,
5700 struct rpc_cred *cred)
5702 struct rpc_message msg = {
5703 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5704 .rpc_argp = session,
5709 dprintk("--> nfs4_proc_destroy_session\n");
5711 /* session is still being setup */
5712 if (session->clp->cl_cons_state != NFS_CS_READY)
5715 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5718 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5719 "Session has been destroyed regardless...\n", status);
5721 dprintk("<-- nfs4_proc_destroy_session\n");
5726 * Renew the cl_session lease.
5728 struct nfs4_sequence_data {
5729 struct nfs_client *clp;
5730 struct nfs4_sequence_args args;
5731 struct nfs4_sequence_res res;
5734 static void nfs41_sequence_release(void *data)
5736 struct nfs4_sequence_data *calldata = data;
5737 struct nfs_client *clp = calldata->clp;
5739 if (atomic_read(&clp->cl_count) > 1)
5740 nfs4_schedule_state_renewal(clp);
5741 nfs_put_client(clp);
5745 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5747 switch(task->tk_status) {
5748 case -NFS4ERR_DELAY:
5749 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5752 nfs4_schedule_lease_recovery(clp);
5757 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5759 struct nfs4_sequence_data *calldata = data;
5760 struct nfs_client *clp = calldata->clp;
5762 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5765 if (task->tk_status < 0) {
5766 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5767 if (atomic_read(&clp->cl_count) == 1)
5770 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5771 rpc_restart_call_prepare(task);
5775 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5777 dprintk("<-- %s\n", __func__);
5780 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5782 struct nfs4_sequence_data *calldata = data;
5783 struct nfs_client *clp = calldata->clp;
5784 struct nfs4_sequence_args *args;
5785 struct nfs4_sequence_res *res;
5787 args = task->tk_msg.rpc_argp;
5788 res = task->tk_msg.rpc_resp;
5790 nfs41_setup_sequence(clp->cl_session, args, res, task);
5793 static const struct rpc_call_ops nfs41_sequence_ops = {
5794 .rpc_call_done = nfs41_sequence_call_done,
5795 .rpc_call_prepare = nfs41_sequence_prepare,
5796 .rpc_release = nfs41_sequence_release,
5799 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5800 struct rpc_cred *cred,
5803 struct nfs4_sequence_data *calldata;
5804 struct rpc_message msg = {
5805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5808 struct rpc_task_setup task_setup_data = {
5809 .rpc_client = clp->cl_rpcclient,
5810 .rpc_message = &msg,
5811 .callback_ops = &nfs41_sequence_ops,
5812 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5815 if (!atomic_inc_not_zero(&clp->cl_count))
5816 return ERR_PTR(-EIO);
5817 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5818 if (calldata == NULL) {
5819 nfs_put_client(clp);
5820 return ERR_PTR(-ENOMEM);
5822 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5824 nfs4_set_sequence_privileged(&calldata->args);
5825 msg.rpc_argp = &calldata->args;
5826 msg.rpc_resp = &calldata->res;
5827 calldata->clp = clp;
5828 task_setup_data.callback_data = calldata;
5830 return rpc_run_task(&task_setup_data);
5833 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5835 struct rpc_task *task;
5838 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5840 task = _nfs41_proc_sequence(clp, cred, false);
5842 ret = PTR_ERR(task);
5844 rpc_put_task_async(task);
5845 dprintk("<-- %s status=%d\n", __func__, ret);
5849 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5851 struct rpc_task *task;
5854 task = _nfs41_proc_sequence(clp, cred, true);
5856 ret = PTR_ERR(task);
5859 ret = rpc_wait_for_completion_task(task);
5861 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5863 if (task->tk_status == 0)
5864 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5865 ret = task->tk_status;
5869 dprintk("<-- %s status=%d\n", __func__, ret);
5873 struct nfs4_reclaim_complete_data {
5874 struct nfs_client *clp;
5875 struct nfs41_reclaim_complete_args arg;
5876 struct nfs41_reclaim_complete_res res;
5879 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5881 struct nfs4_reclaim_complete_data *calldata = data;
5883 nfs41_setup_sequence(calldata->clp->cl_session,
5884 &calldata->arg.seq_args,
5885 &calldata->res.seq_res,
5889 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5891 switch(task->tk_status) {
5893 case -NFS4ERR_COMPLETE_ALREADY:
5894 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5896 case -NFS4ERR_DELAY:
5897 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5899 case -NFS4ERR_RETRY_UNCACHED_REP:
5902 nfs4_schedule_lease_recovery(clp);
5907 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5909 struct nfs4_reclaim_complete_data *calldata = data;
5910 struct nfs_client *clp = calldata->clp;
5911 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5913 dprintk("--> %s\n", __func__);
5914 if (!nfs41_sequence_done(task, res))
5917 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5918 rpc_restart_call_prepare(task);
5921 dprintk("<-- %s\n", __func__);
5924 static void nfs4_free_reclaim_complete_data(void *data)
5926 struct nfs4_reclaim_complete_data *calldata = data;
5931 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5932 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5933 .rpc_call_done = nfs4_reclaim_complete_done,
5934 .rpc_release = nfs4_free_reclaim_complete_data,
5938 * Issue a global reclaim complete.
5940 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5942 struct nfs4_reclaim_complete_data *calldata;
5943 struct rpc_task *task;
5944 struct rpc_message msg = {
5945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5947 struct rpc_task_setup task_setup_data = {
5948 .rpc_client = clp->cl_rpcclient,
5949 .rpc_message = &msg,
5950 .callback_ops = &nfs4_reclaim_complete_call_ops,
5951 .flags = RPC_TASK_ASYNC,
5953 int status = -ENOMEM;
5955 dprintk("--> %s\n", __func__);
5956 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5957 if (calldata == NULL)
5959 calldata->clp = clp;
5960 calldata->arg.one_fs = 0;
5962 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5963 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
5964 msg.rpc_argp = &calldata->arg;
5965 msg.rpc_resp = &calldata->res;
5966 task_setup_data.callback_data = calldata;
5967 task = rpc_run_task(&task_setup_data);
5969 status = PTR_ERR(task);
5972 status = nfs4_wait_for_completion_rpc_task(task);
5974 status = task->tk_status;
5978 dprintk("<-- %s status=%d\n", __func__, status);
5983 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5985 struct nfs4_layoutget *lgp = calldata;
5986 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5987 struct nfs4_session *session = nfs4_get_session(server);
5989 dprintk("--> %s\n", __func__);
5990 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5991 * right now covering the LAYOUTGET we are about to send.
5992 * However, that is not so catastrophic, and there seems
5993 * to be no way to prevent it completely.
5995 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
5996 &lgp->res.seq_res, task))
5998 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5999 NFS_I(lgp->args.inode)->layout,
6000 lgp->args.ctx->state)) {
6001 rpc_exit(task, NFS4_OK);
6005 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6007 struct nfs4_layoutget *lgp = calldata;
6008 struct inode *inode = lgp->args.inode;
6009 struct nfs_server *server = NFS_SERVER(inode);
6010 struct pnfs_layout_hdr *lo;
6011 struct nfs4_state *state = NULL;
6013 dprintk("--> %s\n", __func__);
6015 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6018 switch (task->tk_status) {
6021 case -NFS4ERR_LAYOUTTRYLATER:
6022 case -NFS4ERR_RECALLCONFLICT:
6023 task->tk_status = -NFS4ERR_DELAY;
6025 case -NFS4ERR_EXPIRED:
6026 case -NFS4ERR_BAD_STATEID:
6027 spin_lock(&inode->i_lock);
6028 lo = NFS_I(inode)->layout;
6029 if (!lo || list_empty(&lo->plh_segs)) {
6030 spin_unlock(&inode->i_lock);
6031 /* If the open stateid was bad, then recover it. */
6032 state = lgp->args.ctx->state;
6036 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6037 spin_unlock(&inode->i_lock);
6038 /* Mark the bad layout state as invalid, then
6039 * retry using the open stateid. */
6040 pnfs_free_lseg_list(&head);
6043 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6044 rpc_restart_call_prepare(task);
6046 dprintk("<-- %s\n", __func__);
6049 static size_t max_response_pages(struct nfs_server *server)
6051 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6052 return nfs_page_array_len(0, max_resp_sz);
6055 static void nfs4_free_pages(struct page **pages, size_t size)
6062 for (i = 0; i < size; i++) {
6065 __free_page(pages[i]);
6070 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6072 struct page **pages;
6075 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6077 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6081 for (i = 0; i < size; i++) {
6082 pages[i] = alloc_page(gfp_flags);
6084 dprintk("%s: failed to allocate page\n", __func__);
6085 nfs4_free_pages(pages, size);
6093 static void nfs4_layoutget_release(void *calldata)
6095 struct nfs4_layoutget *lgp = calldata;
6096 struct inode *inode = lgp->args.inode;
6097 struct nfs_server *server = NFS_SERVER(inode);
6098 size_t max_pages = max_response_pages(server);
6100 dprintk("--> %s\n", __func__);
6101 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6102 pnfs_put_layout_hdr(NFS_I(inode)->layout);
6103 put_nfs_open_context(lgp->args.ctx);
6105 dprintk("<-- %s\n", __func__);
6108 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6109 .rpc_call_prepare = nfs4_layoutget_prepare,
6110 .rpc_call_done = nfs4_layoutget_done,
6111 .rpc_release = nfs4_layoutget_release,
6114 struct pnfs_layout_segment *
6115 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6117 struct inode *inode = lgp->args.inode;
6118 struct nfs_server *server = NFS_SERVER(inode);
6119 size_t max_pages = max_response_pages(server);
6120 struct rpc_task *task;
6121 struct rpc_message msg = {
6122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6123 .rpc_argp = &lgp->args,
6124 .rpc_resp = &lgp->res,
6126 struct rpc_task_setup task_setup_data = {
6127 .rpc_client = server->client,
6128 .rpc_message = &msg,
6129 .callback_ops = &nfs4_layoutget_call_ops,
6130 .callback_data = lgp,
6131 .flags = RPC_TASK_ASYNC,
6133 struct pnfs_layout_segment *lseg = NULL;
6136 dprintk("--> %s\n", __func__);
6138 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6139 if (!lgp->args.layout.pages) {
6140 nfs4_layoutget_release(lgp);
6141 return ERR_PTR(-ENOMEM);
6143 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6145 lgp->res.layoutp = &lgp->args.layout;
6146 lgp->res.seq_res.sr_slot = NULL;
6147 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6149 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6150 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6152 task = rpc_run_task(&task_setup_data);
6154 return ERR_CAST(task);
6155 status = nfs4_wait_for_completion_rpc_task(task);
6157 status = task->tk_status;
6158 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6159 if (status == 0 && lgp->res.layoutp->len)
6160 lseg = pnfs_layout_process(lgp);
6162 dprintk("<-- %s status=%d\n", __func__, status);
6164 return ERR_PTR(status);
6169 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6171 struct nfs4_layoutreturn *lrp = calldata;
6173 dprintk("--> %s\n", __func__);
6174 nfs41_setup_sequence(lrp->clp->cl_session,
6175 &lrp->args.seq_args,
6180 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6182 struct nfs4_layoutreturn *lrp = calldata;
6183 struct nfs_server *server;
6185 dprintk("--> %s\n", __func__);
6187 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6190 server = NFS_SERVER(lrp->args.inode);
6191 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6192 rpc_restart_call_prepare(task);
6195 dprintk("<-- %s\n", __func__);
6198 static void nfs4_layoutreturn_release(void *calldata)
6200 struct nfs4_layoutreturn *lrp = calldata;
6201 struct pnfs_layout_hdr *lo = lrp->args.layout;
6203 dprintk("--> %s\n", __func__);
6204 spin_lock(&lo->plh_inode->i_lock);
6205 if (lrp->res.lrs_present)
6206 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6207 lo->plh_block_lgets--;
6208 spin_unlock(&lo->plh_inode->i_lock);
6209 pnfs_put_layout_hdr(lrp->args.layout);
6211 dprintk("<-- %s\n", __func__);
6214 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6215 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6216 .rpc_call_done = nfs4_layoutreturn_done,
6217 .rpc_release = nfs4_layoutreturn_release,
6220 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6222 struct rpc_task *task;
6223 struct rpc_message msg = {
6224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6225 .rpc_argp = &lrp->args,
6226 .rpc_resp = &lrp->res,
6228 struct rpc_task_setup task_setup_data = {
6229 .rpc_client = lrp->clp->cl_rpcclient,
6230 .rpc_message = &msg,
6231 .callback_ops = &nfs4_layoutreturn_call_ops,
6232 .callback_data = lrp,
6236 dprintk("--> %s\n", __func__);
6237 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6238 task = rpc_run_task(&task_setup_data);
6240 return PTR_ERR(task);
6241 status = task->tk_status;
6242 dprintk("<-- %s status=%d\n", __func__, status);
6248 * Retrieve the list of Data Server devices from the MDS.
6250 static int _nfs4_getdevicelist(struct nfs_server *server,
6251 const struct nfs_fh *fh,
6252 struct pnfs_devicelist *devlist)
6254 struct nfs4_getdevicelist_args args = {
6256 .layoutclass = server->pnfs_curr_ld->id,
6258 struct nfs4_getdevicelist_res res = {
6261 struct rpc_message msg = {
6262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6268 dprintk("--> %s\n", __func__);
6269 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6271 dprintk("<-- %s status=%d\n", __func__, status);
6275 int nfs4_proc_getdevicelist(struct nfs_server *server,
6276 const struct nfs_fh *fh,
6277 struct pnfs_devicelist *devlist)
6279 struct nfs4_exception exception = { };
6283 err = nfs4_handle_exception(server,
6284 _nfs4_getdevicelist(server, fh, devlist),
6286 } while (exception.retry);
6288 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6289 err, devlist->num_devs);
6293 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6296 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6298 struct nfs4_getdeviceinfo_args args = {
6301 struct nfs4_getdeviceinfo_res res = {
6304 struct rpc_message msg = {
6305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6311 dprintk("--> %s\n", __func__);
6312 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6313 dprintk("<-- %s status=%d\n", __func__, status);
6318 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6320 struct nfs4_exception exception = { };
6324 err = nfs4_handle_exception(server,
6325 _nfs4_proc_getdeviceinfo(server, pdev),
6327 } while (exception.retry);
6330 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6332 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6334 struct nfs4_layoutcommit_data *data = calldata;
6335 struct nfs_server *server = NFS_SERVER(data->args.inode);
6336 struct nfs4_session *session = nfs4_get_session(server);
6338 nfs41_setup_sequence(session,
6339 &data->args.seq_args,
6345 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6347 struct nfs4_layoutcommit_data *data = calldata;
6348 struct nfs_server *server = NFS_SERVER(data->args.inode);
6350 if (!nfs41_sequence_done(task, &data->res.seq_res))
6353 switch (task->tk_status) { /* Just ignore these failures */
6354 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6355 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6356 case -NFS4ERR_BADLAYOUT: /* no layout */
6357 case -NFS4ERR_GRACE: /* loca_recalim always false */
6358 task->tk_status = 0;
6361 nfs_post_op_update_inode_force_wcc(data->args.inode,
6365 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6366 rpc_restart_call_prepare(task);
6372 static void nfs4_layoutcommit_release(void *calldata)
6374 struct nfs4_layoutcommit_data *data = calldata;
6376 pnfs_cleanup_layoutcommit(data);
6377 put_rpccred(data->cred);
6381 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6382 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6383 .rpc_call_done = nfs4_layoutcommit_done,
6384 .rpc_release = nfs4_layoutcommit_release,
6388 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6390 struct rpc_message msg = {
6391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6392 .rpc_argp = &data->args,
6393 .rpc_resp = &data->res,
6394 .rpc_cred = data->cred,
6396 struct rpc_task_setup task_setup_data = {
6397 .task = &data->task,
6398 .rpc_client = NFS_CLIENT(data->args.inode),
6399 .rpc_message = &msg,
6400 .callback_ops = &nfs4_layoutcommit_ops,
6401 .callback_data = data,
6402 .flags = RPC_TASK_ASYNC,
6404 struct rpc_task *task;
6407 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6408 "lbw: %llu inode %lu\n",
6409 data->task.tk_pid, sync,
6410 data->args.lastbytewritten,
6411 data->args.inode->i_ino);
6413 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6414 task = rpc_run_task(&task_setup_data);
6416 return PTR_ERR(task);
6419 status = nfs4_wait_for_completion_rpc_task(task);
6422 status = task->tk_status;
6424 dprintk("%s: status %d\n", __func__, status);
6430 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6431 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6433 struct nfs41_secinfo_no_name_args args = {
6434 .style = SECINFO_STYLE_CURRENT_FH,
6436 struct nfs4_secinfo_res res = {
6439 struct rpc_message msg = {
6440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6444 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6448 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6449 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6451 struct nfs4_exception exception = { };
6454 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6457 case -NFS4ERR_WRONGSEC:
6458 case -NFS4ERR_NOTSUPP:
6461 err = nfs4_handle_exception(server, err, &exception);
6463 } while (exception.retry);
6469 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6470 struct nfs_fsinfo *info)
6474 rpc_authflavor_t flavor;
6475 struct nfs4_secinfo_flavors *flavors;
6477 page = alloc_page(GFP_KERNEL);
6483 flavors = page_address(page);
6484 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6487 * Fall back on "guess and check" method if
6488 * the server doesn't support SECINFO_NO_NAME
6490 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6491 err = nfs4_find_root_sec(server, fhandle, info);
6497 flavor = nfs_find_best_sec(flavors);
6499 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6509 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6512 struct nfs41_test_stateid_args args = {
6515 struct nfs41_test_stateid_res res;
6516 struct rpc_message msg = {
6517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6522 dprintk("NFS call test_stateid %p\n", stateid);
6523 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6524 nfs4_set_sequence_privileged(&args.seq_args);
6525 status = nfs4_call_sync_sequence(server->client, server, &msg,
6526 &args.seq_args, &res.seq_res);
6527 if (status != NFS_OK) {
6528 dprintk("NFS reply test_stateid: failed, %d\n", status);
6531 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6536 * nfs41_test_stateid - perform a TEST_STATEID operation
6538 * @server: server / transport on which to perform the operation
6539 * @stateid: state ID to test
6541 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6542 * Otherwise a negative NFS4ERR value is returned if the operation
6543 * failed or the state ID is not currently valid.
6545 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6547 struct nfs4_exception exception = { };
6550 err = _nfs41_test_stateid(server, stateid);
6551 if (err != -NFS4ERR_DELAY)
6553 nfs4_handle_exception(server, err, &exception);
6554 } while (exception.retry);
6558 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6560 struct nfs41_free_stateid_args args = {
6563 struct nfs41_free_stateid_res res;
6564 struct rpc_message msg = {
6565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6571 dprintk("NFS call free_stateid %p\n", stateid);
6572 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6573 nfs4_set_sequence_privileged(&args.seq_args);
6574 status = nfs4_call_sync_sequence(server->client, server, &msg,
6575 &args.seq_args, &res.seq_res);
6576 dprintk("NFS reply free_stateid: %d\n", status);
6581 * nfs41_free_stateid - perform a FREE_STATEID operation
6583 * @server: server / transport on which to perform the operation
6584 * @stateid: state ID to release
6586 * Returns NFS_OK if the server freed "stateid". Otherwise a
6587 * negative NFS4ERR value is returned.
6589 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6591 struct nfs4_exception exception = { };
6594 err = _nfs4_free_stateid(server, stateid);
6595 if (err != -NFS4ERR_DELAY)
6597 nfs4_handle_exception(server, err, &exception);
6598 } while (exception.retry);
6602 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6603 const nfs4_stateid *s2)
6605 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6608 if (s1->seqid == s2->seqid)
6610 if (s1->seqid == 0 || s2->seqid == 0)
6616 #endif /* CONFIG_NFS_V4_1 */
6618 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6619 const nfs4_stateid *s2)
6621 return nfs4_stateid_match(s1, s2);
6625 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6626 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6627 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6628 .recover_open = nfs4_open_reclaim,
6629 .recover_lock = nfs4_lock_reclaim,
6630 .establish_clid = nfs4_init_clientid,
6631 .get_clid_cred = nfs4_get_setclientid_cred,
6632 .detect_trunking = nfs40_discover_server_trunking,
6635 #if defined(CONFIG_NFS_V4_1)
6636 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6637 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6638 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6639 .recover_open = nfs4_open_reclaim,
6640 .recover_lock = nfs4_lock_reclaim,
6641 .establish_clid = nfs41_init_clientid,
6642 .get_clid_cred = nfs4_get_exchange_id_cred,
6643 .reclaim_complete = nfs41_proc_reclaim_complete,
6644 .detect_trunking = nfs41_discover_server_trunking,
6646 #endif /* CONFIG_NFS_V4_1 */
6648 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6649 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6650 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6651 .recover_open = nfs4_open_expired,
6652 .recover_lock = nfs4_lock_expired,
6653 .establish_clid = nfs4_init_clientid,
6654 .get_clid_cred = nfs4_get_setclientid_cred,
6657 #if defined(CONFIG_NFS_V4_1)
6658 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6659 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6660 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6661 .recover_open = nfs41_open_expired,
6662 .recover_lock = nfs41_lock_expired,
6663 .establish_clid = nfs41_init_clientid,
6664 .get_clid_cred = nfs4_get_exchange_id_cred,
6666 #endif /* CONFIG_NFS_V4_1 */
6668 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6669 .sched_state_renewal = nfs4_proc_async_renew,
6670 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6671 .renew_lease = nfs4_proc_renew,
6674 #if defined(CONFIG_NFS_V4_1)
6675 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6676 .sched_state_renewal = nfs41_proc_async_sequence,
6677 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6678 .renew_lease = nfs4_proc_sequence,
6682 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6684 .call_sync = _nfs4_call_sync,
6685 .match_stateid = nfs4_match_stateid,
6686 .find_root_sec = nfs4_find_root_sec,
6687 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6688 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6689 .state_renewal_ops = &nfs40_state_renewal_ops,
6692 #if defined(CONFIG_NFS_V4_1)
6693 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6695 .call_sync = nfs4_call_sync_sequence,
6696 .match_stateid = nfs41_match_stateid,
6697 .find_root_sec = nfs41_find_root_sec,
6698 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6699 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6700 .state_renewal_ops = &nfs41_state_renewal_ops,
6704 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6705 [0] = &nfs_v4_0_minor_ops,
6706 #if defined(CONFIG_NFS_V4_1)
6707 [1] = &nfs_v4_1_minor_ops,
6711 const struct inode_operations nfs4_dir_inode_operations = {
6712 .create = nfs_create,
6713 .lookup = nfs_lookup,
6714 .atomic_open = nfs_atomic_open,
6716 .unlink = nfs_unlink,
6717 .symlink = nfs_symlink,
6721 .rename = nfs_rename,
6722 .permission = nfs_permission,
6723 .getattr = nfs_getattr,
6724 .setattr = nfs_setattr,
6725 .getxattr = generic_getxattr,
6726 .setxattr = generic_setxattr,
6727 .listxattr = generic_listxattr,
6728 .removexattr = generic_removexattr,
6731 static const struct inode_operations nfs4_file_inode_operations = {
6732 .permission = nfs_permission,
6733 .getattr = nfs_getattr,
6734 .setattr = nfs_setattr,
6735 .getxattr = generic_getxattr,
6736 .setxattr = generic_setxattr,
6737 .listxattr = generic_listxattr,
6738 .removexattr = generic_removexattr,
6741 const struct nfs_rpc_ops nfs_v4_clientops = {
6742 .version = 4, /* protocol version */
6743 .dentry_ops = &nfs4_dentry_operations,
6744 .dir_inode_ops = &nfs4_dir_inode_operations,
6745 .file_inode_ops = &nfs4_file_inode_operations,
6746 .file_ops = &nfs4_file_operations,
6747 .getroot = nfs4_proc_get_root,
6748 .submount = nfs4_submount,
6749 .try_mount = nfs4_try_mount,
6750 .getattr = nfs4_proc_getattr,
6751 .setattr = nfs4_proc_setattr,
6752 .lookup = nfs4_proc_lookup,
6753 .access = nfs4_proc_access,
6754 .readlink = nfs4_proc_readlink,
6755 .create = nfs4_proc_create,
6756 .remove = nfs4_proc_remove,
6757 .unlink_setup = nfs4_proc_unlink_setup,
6758 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6759 .unlink_done = nfs4_proc_unlink_done,
6760 .rename = nfs4_proc_rename,
6761 .rename_setup = nfs4_proc_rename_setup,
6762 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6763 .rename_done = nfs4_proc_rename_done,
6764 .link = nfs4_proc_link,
6765 .symlink = nfs4_proc_symlink,
6766 .mkdir = nfs4_proc_mkdir,
6767 .rmdir = nfs4_proc_remove,
6768 .readdir = nfs4_proc_readdir,
6769 .mknod = nfs4_proc_mknod,
6770 .statfs = nfs4_proc_statfs,
6771 .fsinfo = nfs4_proc_fsinfo,
6772 .pathconf = nfs4_proc_pathconf,
6773 .set_capabilities = nfs4_server_capabilities,
6774 .decode_dirent = nfs4_decode_dirent,
6775 .read_setup = nfs4_proc_read_setup,
6776 .read_pageio_init = pnfs_pageio_init_read,
6777 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6778 .read_done = nfs4_read_done,
6779 .write_setup = nfs4_proc_write_setup,
6780 .write_pageio_init = pnfs_pageio_init_write,
6781 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6782 .write_done = nfs4_write_done,
6783 .commit_setup = nfs4_proc_commit_setup,
6784 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6785 .commit_done = nfs4_commit_done,
6786 .lock = nfs4_proc_lock,
6787 .clear_acl_cache = nfs4_zap_acl_attr,
6788 .close_context = nfs4_close_context,
6789 .open_context = nfs4_atomic_open,
6790 .have_delegation = nfs4_have_delegation,
6791 .return_delegation = nfs4_inode_return_delegation,
6792 .alloc_client = nfs4_alloc_client,
6793 .init_client = nfs4_init_client,
6794 .free_client = nfs4_free_client,
6795 .create_server = nfs4_create_server,
6796 .clone_server = nfs_clone_server,
6799 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6800 .prefix = XATTR_NAME_NFSV4_ACL,
6801 .list = nfs4_xattr_list_nfs4_acl,
6802 .get = nfs4_xattr_get_nfs4_acl,
6803 .set = nfs4_xattr_set_nfs4_acl,
6806 const struct xattr_handler *nfs4_xattr_handlers[] = {
6807 &nfs4_xattr_nfs4_acl_handler,