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