4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/namei.h>
49 #include <linux/mount.h>
50 #include <linux/module.h>
51 #include <linux/sunrpc/bc_xprt.h>
52 #include <linux/xattr.h>
53 #include <linux/utsname.h>
56 #include "delegation.h"
62 #define NFSDBG_FACILITY NFSDBG_PROC
64 #define NFS4_POLL_RETRY_MIN (HZ/10)
65 #define NFS4_POLL_RETRY_MAX (15*HZ)
67 #define NFS4_MAX_LOOP_ON_RECOVER (10)
70 static int _nfs4_proc_open(struct nfs4_opendata *data);
71 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
72 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
73 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
74 static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
75 const struct qstr *name, struct nfs_fh *fhandle,
76 struct nfs_fattr *fattr);
77 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
78 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
79 struct nfs_fattr *fattr, struct iattr *sattr,
80 struct nfs4_state *state);
82 /* Prevent leaks of NFSv4 errors into userland */
83 static int nfs4_map_errors(int err)
88 case -NFS4ERR_RESOURCE:
90 case -NFS4ERR_BADOWNER:
91 case -NFS4ERR_BADNAME:
94 dprintk("%s could not handle NFSv4 error %d\n",
102 * This is our standard bitmap for GETATTR requests.
104 const u32 nfs4_fattr_bitmap[2] = {
106 | FATTR4_WORD0_CHANGE
109 | FATTR4_WORD0_FILEID,
111 | FATTR4_WORD1_NUMLINKS
113 | FATTR4_WORD1_OWNER_GROUP
114 | FATTR4_WORD1_RAWDEV
115 | FATTR4_WORD1_SPACE_USED
116 | FATTR4_WORD1_TIME_ACCESS
117 | FATTR4_WORD1_TIME_METADATA
118 | FATTR4_WORD1_TIME_MODIFY
121 const u32 nfs4_statfs_bitmap[2] = {
122 FATTR4_WORD0_FILES_AVAIL
123 | FATTR4_WORD0_FILES_FREE
124 | FATTR4_WORD0_FILES_TOTAL,
125 FATTR4_WORD1_SPACE_AVAIL
126 | FATTR4_WORD1_SPACE_FREE
127 | FATTR4_WORD1_SPACE_TOTAL
130 const u32 nfs4_pathconf_bitmap[2] = {
132 | FATTR4_WORD0_MAXNAME,
136 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
137 | FATTR4_WORD0_MAXREAD
138 | FATTR4_WORD0_MAXWRITE
139 | FATTR4_WORD0_LEASE_TIME,
140 FATTR4_WORD1_TIME_DELTA
141 | FATTR4_WORD1_FS_LAYOUT_TYPES
144 const u32 nfs4_fs_locations_bitmap[2] = {
146 | FATTR4_WORD0_CHANGE
149 | FATTR4_WORD0_FILEID
150 | FATTR4_WORD0_FS_LOCATIONS,
152 | FATTR4_WORD1_NUMLINKS
154 | FATTR4_WORD1_OWNER_GROUP
155 | FATTR4_WORD1_RAWDEV
156 | FATTR4_WORD1_SPACE_USED
157 | FATTR4_WORD1_TIME_ACCESS
158 | FATTR4_WORD1_TIME_METADATA
159 | FATTR4_WORD1_TIME_MODIFY
160 | FATTR4_WORD1_MOUNTED_ON_FILEID
163 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
164 struct nfs4_readdir_arg *readdir)
168 BUG_ON(readdir->count < 80);
170 readdir->cookie = cookie;
171 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
176 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
181 * NFSv4 servers do not return entries for '.' and '..'
182 * Therefore, we fake these entries here. We let '.'
183 * have cookie 0 and '..' have cookie 1. Note that
184 * when talking to the server, we always send cookie 0
187 start = p = kmap_atomic(*readdir->pages, KM_USER0);
190 *p++ = xdr_one; /* next */
191 *p++ = xdr_zero; /* cookie, first word */
192 *p++ = xdr_one; /* cookie, second word */
193 *p++ = xdr_one; /* entry len */
194 memcpy(p, ".\0\0\0", 4); /* entry */
196 *p++ = xdr_one; /* bitmap length */
197 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
198 *p++ = htonl(8); /* attribute buffer length */
199 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
202 *p++ = xdr_one; /* next */
203 *p++ = xdr_zero; /* cookie, first word */
204 *p++ = xdr_two; /* cookie, second word */
205 *p++ = xdr_two; /* entry len */
206 memcpy(p, "..\0\0", 4); /* entry */
208 *p++ = xdr_one; /* bitmap length */
209 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
210 *p++ = htonl(8); /* attribute buffer length */
211 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
213 readdir->pgbase = (char *)p - (char *)start;
214 readdir->count -= readdir->pgbase;
215 kunmap_atomic(start, KM_USER0);
218 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
224 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
225 nfs_wait_bit_killable, TASK_KILLABLE);
229 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
236 *timeout = NFS4_POLL_RETRY_MIN;
237 if (*timeout > NFS4_POLL_RETRY_MAX)
238 *timeout = NFS4_POLL_RETRY_MAX;
239 schedule_timeout_killable(*timeout);
240 if (fatal_signal_pending(current))
246 /* This is the error handling routine for processes that are allowed
249 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
251 struct nfs_client *clp = server->nfs_client;
252 struct nfs4_state *state = exception->state;
255 exception->retry = 0;
259 case -NFS4ERR_ADMIN_REVOKED:
260 case -NFS4ERR_BAD_STATEID:
261 case -NFS4ERR_OPENMODE:
264 nfs4_schedule_stateid_recovery(server, state);
265 goto wait_on_recovery;
266 case -NFS4ERR_STALE_STATEID:
267 case -NFS4ERR_STALE_CLIENTID:
268 case -NFS4ERR_EXPIRED:
269 nfs4_schedule_lease_recovery(clp);
270 goto wait_on_recovery;
271 #if defined(CONFIG_NFS_V4_1)
272 case -NFS4ERR_BADSESSION:
273 case -NFS4ERR_BADSLOT:
274 case -NFS4ERR_BAD_HIGH_SLOT:
275 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
276 case -NFS4ERR_DEADSESSION:
277 case -NFS4ERR_SEQ_FALSE_RETRY:
278 case -NFS4ERR_SEQ_MISORDERED:
279 dprintk("%s ERROR: %d Reset session\n", __func__,
281 nfs4_schedule_session_recovery(clp->cl_session);
282 exception->retry = 1;
284 #endif /* defined(CONFIG_NFS_V4_1) */
285 case -NFS4ERR_FILE_OPEN:
286 if (exception->timeout > HZ) {
287 /* We have retried a decent amount, time to
296 ret = nfs4_delay(server->client, &exception->timeout);
299 case -NFS4ERR_OLD_STATEID:
300 exception->retry = 1;
302 case -NFS4ERR_BADOWNER:
303 /* The following works around a Linux server bug! */
304 case -NFS4ERR_BADNAME:
305 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
306 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
307 exception->retry = 1;
308 printk(KERN_WARNING "NFS: v4 server %s "
309 "does not accept raw "
311 "Reenabling the idmapper.\n",
312 server->nfs_client->cl_hostname);
315 /* We failed to handle the error */
316 return nfs4_map_errors(ret);
318 ret = nfs4_wait_clnt_recover(clp);
320 exception->retry = 1;
325 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
327 spin_lock(&clp->cl_lock);
328 if (time_before(clp->cl_last_renewal,timestamp))
329 clp->cl_last_renewal = timestamp;
330 spin_unlock(&clp->cl_lock);
333 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
335 do_renew_lease(server->nfs_client, timestamp);
338 #if defined(CONFIG_NFS_V4_1)
341 * nfs4_free_slot - free a slot and efficiently update slot table.
343 * freeing a slot is trivially done by clearing its respective bit
345 * If the freed slotid equals highest_used_slotid we want to update it
346 * so that the server would be able to size down the slot table if needed,
347 * otherwise we know that the highest_used_slotid is still in use.
348 * When updating highest_used_slotid there may be "holes" in the bitmap
349 * so we need to scan down from highest_used_slotid to 0 looking for the now
350 * highest slotid in use.
351 * If none found, highest_used_slotid is set to -1.
353 * Must be called while holding tbl->slot_tbl_lock
356 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
358 int free_slotid = free_slot - tbl->slots;
359 int slotid = free_slotid;
361 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
362 /* clear used bit in bitmap */
363 __clear_bit(slotid, tbl->used_slots);
365 /* update highest_used_slotid when it is freed */
366 if (slotid == tbl->highest_used_slotid) {
367 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
368 if (slotid < tbl->max_slots)
369 tbl->highest_used_slotid = slotid;
371 tbl->highest_used_slotid = -1;
373 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
374 free_slotid, tbl->highest_used_slotid);
378 * Signal state manager thread if session fore channel is drained
380 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
382 struct rpc_task *task;
384 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
385 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
387 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
391 if (ses->fc_slot_table.highest_used_slotid != -1)
394 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
395 complete(&ses->fc_slot_table.complete);
399 * Signal state manager thread if session back channel is drained
401 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
403 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
404 ses->bc_slot_table.highest_used_slotid != -1)
406 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
407 complete(&ses->bc_slot_table.complete);
410 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
412 struct nfs4_slot_table *tbl;
414 tbl = &res->sr_session->fc_slot_table;
416 /* just wake up the next guy waiting since
417 * we may have not consumed a slot after all */
418 dprintk("%s: No slot\n", __func__);
422 spin_lock(&tbl->slot_tbl_lock);
423 nfs4_free_slot(tbl, res->sr_slot);
424 nfs4_check_drain_fc_complete(res->sr_session);
425 spin_unlock(&tbl->slot_tbl_lock);
429 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
431 unsigned long timestamp;
432 struct nfs_client *clp;
435 * sr_status remains 1 if an RPC level error occurred. The server
436 * may or may not have processed the sequence operation..
437 * Proceed as if the server received and processed the sequence
440 if (res->sr_status == 1)
441 res->sr_status = NFS_OK;
443 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
447 /* Check the SEQUENCE operation status */
448 switch (res->sr_status) {
450 /* Update the slot's sequence and clientid lease timer */
451 ++res->sr_slot->seq_nr;
452 timestamp = res->sr_renewal_time;
453 clp = res->sr_session->clp;
454 do_renew_lease(clp, timestamp);
455 /* Check sequence flags */
456 if (res->sr_status_flags != 0)
457 nfs4_schedule_lease_recovery(clp);
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=%td seq=%d: Operation in progress\n",
466 res->sr_slot - res->sr_session->fc_slot_table.slots,
467 res->sr_slot->seq_nr);
470 /* Just update the slot sequence no. */
471 ++res->sr_slot->seq_nr;
474 /* The session may be reset by one of the error handlers. */
475 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
476 nfs41_sequence_free_slot(res);
479 if (!rpc_restart_call(task))
481 rpc_delay(task, NFS4_POLL_RETRY_MAX);
485 static int nfs4_sequence_done(struct rpc_task *task,
486 struct nfs4_sequence_res *res)
488 if (res->sr_session == NULL)
490 return nfs41_sequence_done(task, res);
494 * nfs4_find_slot - efficiently look for a free slot
496 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
497 * If found, we mark the slot as used, update the highest_used_slotid,
498 * and respectively set up the sequence operation args.
499 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
501 * Note: must be called with under the slot_tbl_lock.
504 nfs4_find_slot(struct nfs4_slot_table *tbl)
507 u8 ret_id = NFS4_MAX_SLOT_TABLE;
508 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
510 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
511 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
513 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
514 if (slotid >= tbl->max_slots)
516 __set_bit(slotid, tbl->used_slots);
517 if (slotid > tbl->highest_used_slotid)
518 tbl->highest_used_slotid = slotid;
521 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
522 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
526 int nfs41_setup_sequence(struct nfs4_session *session,
527 struct nfs4_sequence_args *args,
528 struct nfs4_sequence_res *res,
530 struct rpc_task *task)
532 struct nfs4_slot *slot;
533 struct nfs4_slot_table *tbl;
536 dprintk("--> %s\n", __func__);
537 /* slot already allocated? */
538 if (res->sr_slot != NULL)
541 tbl = &session->fc_slot_table;
543 spin_lock(&tbl->slot_tbl_lock);
544 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
545 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
547 * The state manager will wait until the slot table is empty.
548 * Schedule the reset thread
550 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
551 spin_unlock(&tbl->slot_tbl_lock);
552 dprintk("%s Schedule Session Reset\n", __func__);
556 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
557 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
558 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
559 spin_unlock(&tbl->slot_tbl_lock);
560 dprintk("%s enforce FIFO order\n", __func__);
564 slotid = nfs4_find_slot(tbl);
565 if (slotid == NFS4_MAX_SLOT_TABLE) {
566 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
567 spin_unlock(&tbl->slot_tbl_lock);
568 dprintk("<-- %s: no free slots\n", __func__);
571 spin_unlock(&tbl->slot_tbl_lock);
573 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
574 slot = tbl->slots + slotid;
575 args->sa_session = session;
576 args->sa_slotid = slotid;
577 args->sa_cache_this = cache_reply;
579 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
581 res->sr_session = session;
583 res->sr_renewal_time = 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.
592 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
594 int nfs4_setup_sequence(const struct nfs_server *server,
595 struct nfs4_sequence_args *args,
596 struct nfs4_sequence_res *res,
598 struct rpc_task *task)
600 struct nfs4_session *session = nfs4_get_session(server);
603 if (session == NULL) {
604 args->sa_session = NULL;
605 res->sr_session = NULL;
609 dprintk("--> %s clp %p session %p sr_slot %td\n",
610 __func__, session->clp, session, res->sr_slot ?
611 res->sr_slot - session->fc_slot_table.slots : -1);
613 ret = nfs41_setup_sequence(session, args, res, cache_reply,
616 dprintk("<-- %s status=%d\n", __func__, ret);
620 struct nfs41_call_sync_data {
621 const struct nfs_server *seq_server;
622 struct nfs4_sequence_args *seq_args;
623 struct nfs4_sequence_res *seq_res;
627 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
629 struct nfs41_call_sync_data *data = calldata;
631 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
633 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
634 data->seq_res, data->cache_reply, task))
636 rpc_call_start(task);
639 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
641 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
642 nfs41_call_sync_prepare(task, calldata);
645 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
647 struct nfs41_call_sync_data *data = calldata;
649 nfs41_sequence_done(task, data->seq_res);
652 struct rpc_call_ops nfs41_call_sync_ops = {
653 .rpc_call_prepare = nfs41_call_sync_prepare,
654 .rpc_call_done = nfs41_call_sync_done,
657 struct rpc_call_ops nfs41_call_priv_sync_ops = {
658 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
659 .rpc_call_done = nfs41_call_sync_done,
662 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
663 struct nfs_server *server,
664 struct rpc_message *msg,
665 struct nfs4_sequence_args *args,
666 struct nfs4_sequence_res *res,
671 struct rpc_task *task;
672 struct nfs41_call_sync_data data = {
673 .seq_server = server,
676 .cache_reply = cache_reply,
678 struct rpc_task_setup task_setup = {
681 .callback_ops = &nfs41_call_sync_ops,
682 .callback_data = &data
687 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
688 task = rpc_run_task(&task_setup);
692 ret = task->tk_status;
698 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
699 struct nfs_server *server,
700 struct rpc_message *msg,
701 struct nfs4_sequence_args *args,
702 struct nfs4_sequence_res *res,
705 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
709 static int nfs4_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
714 #endif /* CONFIG_NFS_V4_1 */
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 args->sa_session = res->sr_session = NULL;
724 return rpc_call_sync(clnt, msg, 0);
728 int nfs4_call_sync(struct rpc_clnt *clnt,
729 struct nfs_server *server,
730 struct rpc_message *msg,
731 struct nfs4_sequence_args *args,
732 struct nfs4_sequence_res *res,
735 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
736 args, res, cache_reply);
739 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
741 struct nfs_inode *nfsi = NFS_I(dir);
743 spin_lock(&dir->i_lock);
744 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
745 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
746 nfs_force_lookup_revalidate(dir);
747 nfsi->change_attr = cinfo->after;
748 spin_unlock(&dir->i_lock);
751 struct nfs4_opendata {
753 struct nfs_openargs o_arg;
754 struct nfs_openres o_res;
755 struct nfs_open_confirmargs c_arg;
756 struct nfs_open_confirmres c_res;
757 struct nfs_fattr f_attr;
758 struct nfs_fattr dir_attr;
761 struct nfs4_state_owner *owner;
762 struct nfs4_state *state;
764 unsigned long timestamp;
765 unsigned int rpc_done : 1;
771 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
773 p->o_res.f_attr = &p->f_attr;
774 p->o_res.dir_attr = &p->dir_attr;
775 p->o_res.seqid = p->o_arg.seqid;
776 p->c_res.seqid = p->c_arg.seqid;
777 p->o_res.server = p->o_arg.server;
778 nfs_fattr_init(&p->f_attr);
779 nfs_fattr_init(&p->dir_attr);
782 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
783 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
784 const struct iattr *attrs,
787 struct dentry *parent = dget_parent(path->dentry);
788 struct inode *dir = parent->d_inode;
789 struct nfs_server *server = NFS_SERVER(dir);
790 struct nfs4_opendata *p;
792 p = kzalloc(sizeof(*p), gfp_mask);
795 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
796 if (p->o_arg.seqid == NULL)
802 atomic_inc(&sp->so_count);
803 p->o_arg.fh = NFS_FH(dir);
804 p->o_arg.open_flags = flags;
805 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
806 p->o_arg.clientid = server->nfs_client->cl_clientid;
807 p->o_arg.id = sp->so_owner_id.id;
808 p->o_arg.name = &p->path.dentry->d_name;
809 p->o_arg.server = server;
810 p->o_arg.bitmask = server->attr_bitmask;
811 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
812 if (flags & O_CREAT) {
815 p->o_arg.u.attrs = &p->attrs;
816 memcpy(&p->attrs, attrs, sizeof(p->attrs));
817 s = (u32 *) p->o_arg.u.verifier.data;
821 p->c_arg.fh = &p->o_res.fh;
822 p->c_arg.stateid = &p->o_res.stateid;
823 p->c_arg.seqid = p->o_arg.seqid;
824 nfs4_init_opendata_res(p);
834 static void nfs4_opendata_free(struct kref *kref)
836 struct nfs4_opendata *p = container_of(kref,
837 struct nfs4_opendata, kref);
839 nfs_free_seqid(p->o_arg.seqid);
840 if (p->state != NULL)
841 nfs4_put_open_state(p->state);
842 nfs4_put_state_owner(p->owner);
848 static void nfs4_opendata_put(struct nfs4_opendata *p)
851 kref_put(&p->kref, nfs4_opendata_free);
854 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
858 ret = rpc_wait_for_completion_task(task);
862 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
866 if (open_mode & O_EXCL)
868 switch (mode & (FMODE_READ|FMODE_WRITE)) {
870 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
871 && state->n_rdonly != 0;
874 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
875 && state->n_wronly != 0;
877 case FMODE_READ|FMODE_WRITE:
878 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
879 && state->n_rdwr != 0;
885 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
887 if ((delegation->type & fmode) != fmode)
889 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
891 nfs_mark_delegation_referenced(delegation);
895 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
904 case FMODE_READ|FMODE_WRITE:
907 nfs4_state_set_mode_locked(state, state->state | fmode);
910 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
912 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
913 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
914 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
917 set_bit(NFS_O_RDONLY_STATE, &state->flags);
920 set_bit(NFS_O_WRONLY_STATE, &state->flags);
922 case FMODE_READ|FMODE_WRITE:
923 set_bit(NFS_O_RDWR_STATE, &state->flags);
927 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
929 write_seqlock(&state->seqlock);
930 nfs_set_open_stateid_locked(state, stateid, fmode);
931 write_sequnlock(&state->seqlock);
934 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
937 * Protect the call to nfs4_state_set_mode_locked and
938 * serialise the stateid update
940 write_seqlock(&state->seqlock);
941 if (deleg_stateid != NULL) {
942 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
943 set_bit(NFS_DELEGATED_STATE, &state->flags);
945 if (open_stateid != NULL)
946 nfs_set_open_stateid_locked(state, open_stateid, fmode);
947 write_sequnlock(&state->seqlock);
948 spin_lock(&state->owner->so_lock);
949 update_open_stateflags(state, fmode);
950 spin_unlock(&state->owner->so_lock);
953 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
955 struct nfs_inode *nfsi = NFS_I(state->inode);
956 struct nfs_delegation *deleg_cur;
959 fmode &= (FMODE_READ|FMODE_WRITE);
962 deleg_cur = rcu_dereference(nfsi->delegation);
963 if (deleg_cur == NULL)
966 spin_lock(&deleg_cur->lock);
967 if (nfsi->delegation != deleg_cur ||
968 (deleg_cur->type & fmode) != fmode)
969 goto no_delegation_unlock;
971 if (delegation == NULL)
972 delegation = &deleg_cur->stateid;
973 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
974 goto no_delegation_unlock;
976 nfs_mark_delegation_referenced(deleg_cur);
977 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
979 no_delegation_unlock:
980 spin_unlock(&deleg_cur->lock);
984 if (!ret && open_stateid != NULL) {
985 __update_open_stateid(state, open_stateid, NULL, fmode);
993 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
995 struct nfs_delegation *delegation;
998 delegation = rcu_dereference(NFS_I(inode)->delegation);
999 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1004 nfs_inode_return_delegation(inode);
1007 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1009 struct nfs4_state *state = opendata->state;
1010 struct nfs_inode *nfsi = NFS_I(state->inode);
1011 struct nfs_delegation *delegation;
1012 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1013 fmode_t fmode = opendata->o_arg.fmode;
1014 nfs4_stateid stateid;
1018 if (can_open_cached(state, fmode, open_mode)) {
1019 spin_lock(&state->owner->so_lock);
1020 if (can_open_cached(state, fmode, open_mode)) {
1021 update_open_stateflags(state, fmode);
1022 spin_unlock(&state->owner->so_lock);
1023 goto out_return_state;
1025 spin_unlock(&state->owner->so_lock);
1028 delegation = rcu_dereference(nfsi->delegation);
1029 if (delegation == NULL ||
1030 !can_open_delegated(delegation, fmode)) {
1034 /* Save the delegation */
1035 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1037 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1042 /* Try to update the stateid using the delegation */
1043 if (update_open_stateid(state, NULL, &stateid, fmode))
1044 goto out_return_state;
1047 return ERR_PTR(ret);
1049 atomic_inc(&state->count);
1053 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1055 struct inode *inode;
1056 struct nfs4_state *state = NULL;
1057 struct nfs_delegation *delegation;
1060 if (!data->rpc_done) {
1061 state = nfs4_try_open_cached(data);
1066 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1068 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1069 ret = PTR_ERR(inode);
1073 state = nfs4_get_open_state(inode, data->owner);
1076 if (data->o_res.delegation_type != 0) {
1077 int delegation_flags = 0;
1080 delegation = rcu_dereference(NFS_I(inode)->delegation);
1082 delegation_flags = delegation->flags;
1084 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1085 nfs_inode_set_delegation(state->inode,
1086 data->owner->so_cred,
1089 nfs_inode_reclaim_delegation(state->inode,
1090 data->owner->so_cred,
1094 update_open_stateid(state, &data->o_res.stateid, NULL,
1102 return ERR_PTR(ret);
1105 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1107 struct nfs_inode *nfsi = NFS_I(state->inode);
1108 struct nfs_open_context *ctx;
1110 spin_lock(&state->inode->i_lock);
1111 list_for_each_entry(ctx, &nfsi->open_files, list) {
1112 if (ctx->state != state)
1114 get_nfs_open_context(ctx);
1115 spin_unlock(&state->inode->i_lock);
1118 spin_unlock(&state->inode->i_lock);
1119 return ERR_PTR(-ENOENT);
1122 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1124 struct nfs4_opendata *opendata;
1126 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1127 if (opendata == NULL)
1128 return ERR_PTR(-ENOMEM);
1129 opendata->state = state;
1130 atomic_inc(&state->count);
1134 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1136 struct nfs4_state *newstate;
1139 opendata->o_arg.open_flags = 0;
1140 opendata->o_arg.fmode = fmode;
1141 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1142 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1143 nfs4_init_opendata_res(opendata);
1144 ret = _nfs4_recover_proc_open(opendata);
1147 newstate = nfs4_opendata_to_nfs4_state(opendata);
1148 if (IS_ERR(newstate))
1149 return PTR_ERR(newstate);
1150 nfs4_close_state(&opendata->path, newstate, fmode);
1155 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1157 struct nfs4_state *newstate;
1160 /* memory barrier prior to reading state->n_* */
1161 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1163 if (state->n_rdwr != 0) {
1164 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1165 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1168 if (newstate != state)
1171 if (state->n_wronly != 0) {
1172 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1173 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1176 if (newstate != state)
1179 if (state->n_rdonly != 0) {
1180 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1181 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1184 if (newstate != state)
1188 * We may have performed cached opens for all three recoveries.
1189 * Check if we need to update the current stateid.
1191 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1192 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1193 write_seqlock(&state->seqlock);
1194 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1195 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1196 write_sequnlock(&state->seqlock);
1203 * reclaim state on the server after a reboot.
1205 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1207 struct nfs_delegation *delegation;
1208 struct nfs4_opendata *opendata;
1209 fmode_t delegation_type = 0;
1212 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1213 if (IS_ERR(opendata))
1214 return PTR_ERR(opendata);
1215 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1216 opendata->o_arg.fh = NFS_FH(state->inode);
1218 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1219 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1220 delegation_type = delegation->type;
1222 opendata->o_arg.u.delegation_type = delegation_type;
1223 status = nfs4_open_recover(opendata, state);
1224 nfs4_opendata_put(opendata);
1228 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1230 struct nfs_server *server = NFS_SERVER(state->inode);
1231 struct nfs4_exception exception = { };
1234 err = _nfs4_do_open_reclaim(ctx, state);
1235 if (err != -NFS4ERR_DELAY)
1237 nfs4_handle_exception(server, err, &exception);
1238 } while (exception.retry);
1242 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1244 struct nfs_open_context *ctx;
1247 ctx = nfs4_state_find_open_context(state);
1249 return PTR_ERR(ctx);
1250 ret = nfs4_do_open_reclaim(ctx, state);
1251 put_nfs_open_context(ctx);
1255 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1257 struct nfs4_opendata *opendata;
1260 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1261 if (IS_ERR(opendata))
1262 return PTR_ERR(opendata);
1263 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1264 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1265 sizeof(opendata->o_arg.u.delegation.data));
1266 ret = nfs4_open_recover(opendata, state);
1267 nfs4_opendata_put(opendata);
1271 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1273 struct nfs4_exception exception = { };
1274 struct nfs_server *server = NFS_SERVER(state->inode);
1277 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1283 case -NFS4ERR_BADSESSION:
1284 case -NFS4ERR_BADSLOT:
1285 case -NFS4ERR_BAD_HIGH_SLOT:
1286 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1287 case -NFS4ERR_DEADSESSION:
1288 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1290 case -NFS4ERR_STALE_CLIENTID:
1291 case -NFS4ERR_STALE_STATEID:
1292 case -NFS4ERR_EXPIRED:
1293 /* Don't recall a delegation if it was lost */
1294 nfs4_schedule_lease_recovery(server->nfs_client);
1298 * The show must go on: exit, but mark the
1299 * stateid as needing recovery.
1301 case -NFS4ERR_ADMIN_REVOKED:
1302 case -NFS4ERR_BAD_STATEID:
1303 nfs4_schedule_stateid_recovery(server, state);
1306 * User RPCSEC_GSS context has expired.
1307 * We cannot recover this stateid now, so
1308 * skip it and allow recovery thread to
1315 err = nfs4_handle_exception(server, err, &exception);
1316 } while (exception.retry);
1321 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1323 struct nfs4_opendata *data = calldata;
1325 data->rpc_status = task->tk_status;
1326 if (data->rpc_status == 0) {
1327 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1328 sizeof(data->o_res.stateid.data));
1329 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1330 renew_lease(data->o_res.server, data->timestamp);
1335 static void nfs4_open_confirm_release(void *calldata)
1337 struct nfs4_opendata *data = calldata;
1338 struct nfs4_state *state = NULL;
1340 /* If this request hasn't been cancelled, do nothing */
1341 if (data->cancelled == 0)
1343 /* In case of error, no cleanup! */
1344 if (!data->rpc_done)
1346 state = nfs4_opendata_to_nfs4_state(data);
1348 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1350 nfs4_opendata_put(data);
1353 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1354 .rpc_call_done = nfs4_open_confirm_done,
1355 .rpc_release = nfs4_open_confirm_release,
1359 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1361 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1363 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1364 struct rpc_task *task;
1365 struct rpc_message msg = {
1366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1367 .rpc_argp = &data->c_arg,
1368 .rpc_resp = &data->c_res,
1369 .rpc_cred = data->owner->so_cred,
1371 struct rpc_task_setup task_setup_data = {
1372 .rpc_client = server->client,
1373 .rpc_message = &msg,
1374 .callback_ops = &nfs4_open_confirm_ops,
1375 .callback_data = data,
1376 .workqueue = nfsiod_workqueue,
1377 .flags = RPC_TASK_ASYNC,
1381 kref_get(&data->kref);
1383 data->rpc_status = 0;
1384 data->timestamp = jiffies;
1385 task = rpc_run_task(&task_setup_data);
1387 return PTR_ERR(task);
1388 status = nfs4_wait_for_completion_rpc_task(task);
1390 data->cancelled = 1;
1393 status = data->rpc_status;
1398 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1400 struct nfs4_opendata *data = calldata;
1401 struct nfs4_state_owner *sp = data->owner;
1403 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1406 * Check if we still need to send an OPEN call, or if we can use
1407 * a delegation instead.
1409 if (data->state != NULL) {
1410 struct nfs_delegation *delegation;
1412 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1415 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1416 if (delegation != NULL &&
1417 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1423 /* Update sequence id. */
1424 data->o_arg.id = sp->so_owner_id.id;
1425 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1426 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1427 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1428 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1430 data->timestamp = jiffies;
1431 if (nfs4_setup_sequence(data->o_arg.server,
1432 &data->o_arg.seq_args,
1433 &data->o_res.seq_res, 1, task))
1435 rpc_call_start(task);
1438 task->tk_action = NULL;
1442 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1444 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1445 nfs4_open_prepare(task, calldata);
1448 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1450 struct nfs4_opendata *data = calldata;
1452 data->rpc_status = task->tk_status;
1454 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1457 if (task->tk_status == 0) {
1458 switch (data->o_res.f_attr->mode & S_IFMT) {
1462 data->rpc_status = -ELOOP;
1465 data->rpc_status = -EISDIR;
1468 data->rpc_status = -ENOTDIR;
1470 renew_lease(data->o_res.server, data->timestamp);
1471 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1472 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1477 static void nfs4_open_release(void *calldata)
1479 struct nfs4_opendata *data = calldata;
1480 struct nfs4_state *state = NULL;
1482 /* If this request hasn't been cancelled, do nothing */
1483 if (data->cancelled == 0)
1485 /* In case of error, no cleanup! */
1486 if (data->rpc_status != 0 || !data->rpc_done)
1488 /* In case we need an open_confirm, no cleanup! */
1489 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1491 state = nfs4_opendata_to_nfs4_state(data);
1493 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1495 nfs4_opendata_put(data);
1498 static const struct rpc_call_ops nfs4_open_ops = {
1499 .rpc_call_prepare = nfs4_open_prepare,
1500 .rpc_call_done = nfs4_open_done,
1501 .rpc_release = nfs4_open_release,
1504 static const struct rpc_call_ops nfs4_recover_open_ops = {
1505 .rpc_call_prepare = nfs4_recover_open_prepare,
1506 .rpc_call_done = nfs4_open_done,
1507 .rpc_release = nfs4_open_release,
1510 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1512 struct inode *dir = data->dir->d_inode;
1513 struct nfs_server *server = NFS_SERVER(dir);
1514 struct nfs_openargs *o_arg = &data->o_arg;
1515 struct nfs_openres *o_res = &data->o_res;
1516 struct rpc_task *task;
1517 struct rpc_message msg = {
1518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1521 .rpc_cred = data->owner->so_cred,
1523 struct rpc_task_setup task_setup_data = {
1524 .rpc_client = server->client,
1525 .rpc_message = &msg,
1526 .callback_ops = &nfs4_open_ops,
1527 .callback_data = data,
1528 .workqueue = nfsiod_workqueue,
1529 .flags = RPC_TASK_ASYNC,
1533 kref_get(&data->kref);
1535 data->rpc_status = 0;
1536 data->cancelled = 0;
1538 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1539 task = rpc_run_task(&task_setup_data);
1541 return PTR_ERR(task);
1542 status = nfs4_wait_for_completion_rpc_task(task);
1544 data->cancelled = 1;
1547 status = data->rpc_status;
1553 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1555 struct inode *dir = data->dir->d_inode;
1556 struct nfs_openres *o_res = &data->o_res;
1559 status = nfs4_run_open_task(data, 1);
1560 if (status != 0 || !data->rpc_done)
1563 nfs_refresh_inode(dir, o_res->dir_attr);
1565 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1566 status = _nfs4_proc_open_confirm(data);
1575 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1577 static int _nfs4_proc_open(struct nfs4_opendata *data)
1579 struct inode *dir = data->dir->d_inode;
1580 struct nfs_server *server = NFS_SERVER(dir);
1581 struct nfs_openargs *o_arg = &data->o_arg;
1582 struct nfs_openres *o_res = &data->o_res;
1585 status = nfs4_run_open_task(data, 0);
1586 if (status != 0 || !data->rpc_done)
1589 if (o_arg->open_flags & O_CREAT) {
1590 update_changeattr(dir, &o_res->cinfo);
1591 nfs_post_op_update_inode(dir, o_res->dir_attr);
1593 nfs_refresh_inode(dir, o_res->dir_attr);
1594 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1595 server->caps &= ~NFS_CAP_POSIX_LOCK;
1596 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1597 status = _nfs4_proc_open_confirm(data);
1601 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1602 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1606 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1611 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1612 ret = nfs4_wait_clnt_recover(clp);
1615 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1616 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1618 nfs4_schedule_state_manager(clp);
1624 static int nfs4_recover_expired_lease(struct nfs_server *server)
1626 return nfs4_client_recover_expired_lease(server->nfs_client);
1631 * reclaim state on the server after a network partition.
1632 * Assumes caller holds the appropriate lock
1634 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1636 struct nfs4_opendata *opendata;
1639 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1640 if (IS_ERR(opendata))
1641 return PTR_ERR(opendata);
1642 ret = nfs4_open_recover(opendata, state);
1644 d_drop(ctx->path.dentry);
1645 nfs4_opendata_put(opendata);
1649 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1651 struct nfs_server *server = NFS_SERVER(state->inode);
1652 struct nfs4_exception exception = { };
1656 err = _nfs4_open_expired(ctx, state);
1660 case -NFS4ERR_GRACE:
1661 case -NFS4ERR_DELAY:
1662 nfs4_handle_exception(server, err, &exception);
1665 } while (exception.retry);
1670 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1672 struct nfs_open_context *ctx;
1675 ctx = nfs4_state_find_open_context(state);
1677 return PTR_ERR(ctx);
1678 ret = nfs4_do_open_expired(ctx, state);
1679 put_nfs_open_context(ctx);
1684 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1685 * fields corresponding to attributes that were used to store the verifier.
1686 * Make sure we clobber those fields in the later setattr call
1688 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1690 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1691 !(sattr->ia_valid & ATTR_ATIME_SET))
1692 sattr->ia_valid |= ATTR_ATIME;
1694 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1695 !(sattr->ia_valid & ATTR_MTIME_SET))
1696 sattr->ia_valid |= ATTR_MTIME;
1700 * Returns a referenced nfs4_state
1702 static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1704 struct nfs4_state_owner *sp;
1705 struct nfs4_state *state = NULL;
1706 struct nfs_server *server = NFS_SERVER(dir);
1707 struct nfs4_opendata *opendata;
1710 /* Protect against reboot recovery conflicts */
1712 if (!(sp = nfs4_get_state_owner(server, cred))) {
1713 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1716 status = nfs4_recover_expired_lease(server);
1718 goto err_put_state_owner;
1719 if (path->dentry->d_inode != NULL)
1720 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1722 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1723 if (opendata == NULL)
1724 goto err_put_state_owner;
1726 if (path->dentry->d_inode != NULL)
1727 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1729 status = _nfs4_proc_open(opendata);
1731 goto err_opendata_put;
1733 state = nfs4_opendata_to_nfs4_state(opendata);
1734 status = PTR_ERR(state);
1736 goto err_opendata_put;
1737 if (server->caps & NFS_CAP_POSIX_LOCK)
1738 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1740 if (opendata->o_arg.open_flags & O_EXCL) {
1741 nfs4_exclusive_attrset(opendata, sattr);
1743 nfs_fattr_init(opendata->o_res.f_attr);
1744 status = nfs4_do_setattr(state->inode, cred,
1745 opendata->o_res.f_attr, sattr,
1748 nfs_setattr_update_inode(state->inode, sattr);
1749 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1751 nfs4_opendata_put(opendata);
1752 nfs4_put_state_owner(sp);
1756 nfs4_opendata_put(opendata);
1757 err_put_state_owner:
1758 nfs4_put_state_owner(sp);
1765 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1767 struct nfs4_exception exception = { };
1768 struct nfs4_state *res;
1772 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1775 /* NOTE: BAD_SEQID means the server and client disagree about the
1776 * book-keeping w.r.t. state-changing operations
1777 * (OPEN/CLOSE/LOCK/LOCKU...)
1778 * It is actually a sign of a bug on the client or on the server.
1780 * If we receive a BAD_SEQID error in the particular case of
1781 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1782 * have unhashed the old state_owner for us, and that we can
1783 * therefore safely retry using a new one. We should still warn
1784 * the user though...
1786 if (status == -NFS4ERR_BAD_SEQID) {
1787 printk(KERN_WARNING "NFS: v4 server %s "
1788 " returned a bad sequence-id error!\n",
1789 NFS_SERVER(dir)->nfs_client->cl_hostname);
1790 exception.retry = 1;
1794 * BAD_STATEID on OPEN means that the server cancelled our
1795 * state before it received the OPEN_CONFIRM.
1796 * Recover by retrying the request as per the discussion
1797 * on Page 181 of RFC3530.
1799 if (status == -NFS4ERR_BAD_STATEID) {
1800 exception.retry = 1;
1803 if (status == -EAGAIN) {
1804 /* We must have found a delegation */
1805 exception.retry = 1;
1808 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1809 status, &exception));
1810 } while (exception.retry);
1814 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1815 struct nfs_fattr *fattr, struct iattr *sattr,
1816 struct nfs4_state *state)
1818 struct nfs_server *server = NFS_SERVER(inode);
1819 struct nfs_setattrargs arg = {
1820 .fh = NFS_FH(inode),
1823 .bitmask = server->attr_bitmask,
1825 struct nfs_setattrres res = {
1829 struct rpc_message msg = {
1830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1835 unsigned long timestamp = jiffies;
1838 nfs_fattr_init(fattr);
1840 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1841 /* Use that stateid */
1842 } else if (state != NULL) {
1843 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1845 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1847 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1848 if (status == 0 && state != NULL)
1849 renew_lease(server, timestamp);
1853 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1854 struct nfs_fattr *fattr, struct iattr *sattr,
1855 struct nfs4_state *state)
1857 struct nfs_server *server = NFS_SERVER(inode);
1858 struct nfs4_exception exception = { };
1861 err = nfs4_handle_exception(server,
1862 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1864 } while (exception.retry);
1868 struct nfs4_closedata {
1870 struct inode *inode;
1871 struct nfs4_state *state;
1872 struct nfs_closeargs arg;
1873 struct nfs_closeres res;
1874 struct nfs_fattr fattr;
1875 unsigned long timestamp;
1880 static void nfs4_free_closedata(void *data)
1882 struct nfs4_closedata *calldata = data;
1883 struct nfs4_state_owner *sp = calldata->state->owner;
1886 pnfs_roc_release(calldata->state->inode);
1887 nfs4_put_open_state(calldata->state);
1888 nfs_free_seqid(calldata->arg.seqid);
1889 nfs4_put_state_owner(sp);
1890 path_put(&calldata->path);
1894 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1897 spin_lock(&state->owner->so_lock);
1898 if (!(fmode & FMODE_READ))
1899 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1900 if (!(fmode & FMODE_WRITE))
1901 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1902 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1903 spin_unlock(&state->owner->so_lock);
1906 static void nfs4_close_done(struct rpc_task *task, void *data)
1908 struct nfs4_closedata *calldata = data;
1909 struct nfs4_state *state = calldata->state;
1910 struct nfs_server *server = NFS_SERVER(calldata->inode);
1912 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1914 /* hmm. we are done with the inode, and in the process of freeing
1915 * the state_owner. we keep this around to process errors
1917 switch (task->tk_status) {
1920 pnfs_roc_set_barrier(state->inode,
1921 calldata->roc_barrier);
1922 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1923 renew_lease(server, calldata->timestamp);
1924 nfs4_close_clear_stateid_flags(state,
1925 calldata->arg.fmode);
1927 case -NFS4ERR_STALE_STATEID:
1928 case -NFS4ERR_OLD_STATEID:
1929 case -NFS4ERR_BAD_STATEID:
1930 case -NFS4ERR_EXPIRED:
1931 if (calldata->arg.fmode == 0)
1934 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1935 rpc_restart_call_prepare(task);
1937 nfs_release_seqid(calldata->arg.seqid);
1938 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1941 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1943 struct nfs4_closedata *calldata = data;
1944 struct nfs4_state *state = calldata->state;
1947 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1950 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1951 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1952 spin_lock(&state->owner->so_lock);
1953 /* Calculate the change in open mode */
1954 if (state->n_rdwr == 0) {
1955 if (state->n_rdonly == 0) {
1956 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1957 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1958 calldata->arg.fmode &= ~FMODE_READ;
1960 if (state->n_wronly == 0) {
1961 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1962 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1963 calldata->arg.fmode &= ~FMODE_WRITE;
1966 spin_unlock(&state->owner->so_lock);
1969 /* Note: exit _without_ calling nfs4_close_done */
1970 task->tk_action = NULL;
1974 if (calldata->arg.fmode == 0) {
1975 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1976 if (calldata->roc &&
1977 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1978 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1984 nfs_fattr_init(calldata->res.fattr);
1985 calldata->timestamp = jiffies;
1986 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1987 &calldata->arg.seq_args, &calldata->res.seq_res,
1990 rpc_call_start(task);
1993 static const struct rpc_call_ops nfs4_close_ops = {
1994 .rpc_call_prepare = nfs4_close_prepare,
1995 .rpc_call_done = nfs4_close_done,
1996 .rpc_release = nfs4_free_closedata,
2000 * It is possible for data to be read/written from a mem-mapped file
2001 * after the sys_close call (which hits the vfs layer as a flush).
2002 * This means that we can't safely call nfsv4 close on a file until
2003 * the inode is cleared. This in turn means that we are not good
2004 * NFSv4 citizens - we do not indicate to the server to update the file's
2005 * share state even when we are done with one of the three share
2006 * stateid's in the inode.
2008 * NOTE: Caller must be holding the sp->so_owner semaphore!
2010 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2012 struct nfs_server *server = NFS_SERVER(state->inode);
2013 struct nfs4_closedata *calldata;
2014 struct nfs4_state_owner *sp = state->owner;
2015 struct rpc_task *task;
2016 struct rpc_message msg = {
2017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2018 .rpc_cred = state->owner->so_cred,
2020 struct rpc_task_setup task_setup_data = {
2021 .rpc_client = server->client,
2022 .rpc_message = &msg,
2023 .callback_ops = &nfs4_close_ops,
2024 .workqueue = nfsiod_workqueue,
2025 .flags = RPC_TASK_ASYNC,
2027 int status = -ENOMEM;
2029 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2030 if (calldata == NULL)
2032 calldata->inode = state->inode;
2033 calldata->state = state;
2034 calldata->arg.fh = NFS_FH(state->inode);
2035 calldata->arg.stateid = &state->open_stateid;
2036 /* Serialization for the sequence id */
2037 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2038 if (calldata->arg.seqid == NULL)
2039 goto out_free_calldata;
2040 calldata->arg.fmode = 0;
2041 calldata->arg.bitmask = server->cache_consistency_bitmask;
2042 calldata->res.fattr = &calldata->fattr;
2043 calldata->res.seqid = calldata->arg.seqid;
2044 calldata->res.server = server;
2045 calldata->roc = roc;
2047 calldata->path = *path;
2049 msg.rpc_argp = &calldata->arg;
2050 msg.rpc_resp = &calldata->res;
2051 task_setup_data.callback_data = calldata;
2052 task = rpc_run_task(&task_setup_data);
2054 return PTR_ERR(task);
2057 status = rpc_wait_for_completion_task(task);
2064 pnfs_roc_release(state->inode);
2065 nfs4_put_open_state(state);
2066 nfs4_put_state_owner(sp);
2070 static struct inode *
2071 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2073 struct nfs4_state *state;
2075 /* Protect against concurrent sillydeletes */
2076 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2078 return ERR_CAST(state);
2080 return igrab(state->inode);
2083 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2085 if (ctx->state == NULL)
2088 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2090 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2093 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2095 struct nfs4_server_caps_arg args = {
2098 struct nfs4_server_caps_res res = {};
2099 struct rpc_message msg = {
2100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2106 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2108 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2109 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2110 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2111 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2112 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2113 NFS_CAP_CTIME|NFS_CAP_MTIME);
2114 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2115 server->caps |= NFS_CAP_ACLS;
2116 if (res.has_links != 0)
2117 server->caps |= NFS_CAP_HARDLINKS;
2118 if (res.has_symlinks != 0)
2119 server->caps |= NFS_CAP_SYMLINKS;
2120 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2121 server->caps |= NFS_CAP_FILEID;
2122 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2123 server->caps |= NFS_CAP_MODE;
2124 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2125 server->caps |= NFS_CAP_NLINK;
2126 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2127 server->caps |= NFS_CAP_OWNER;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2129 server->caps |= NFS_CAP_OWNER_GROUP;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2131 server->caps |= NFS_CAP_ATIME;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2133 server->caps |= NFS_CAP_CTIME;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2135 server->caps |= NFS_CAP_MTIME;
2137 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2138 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2139 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2140 server->acl_bitmask = res.acl_bitmask;
2146 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2148 struct nfs4_exception exception = { };
2151 err = nfs4_handle_exception(server,
2152 _nfs4_server_capabilities(server, fhandle),
2154 } while (exception.retry);
2158 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2159 struct nfs_fsinfo *info)
2161 struct nfs4_lookup_root_arg args = {
2162 .bitmask = nfs4_fattr_bitmap,
2164 struct nfs4_lookup_res res = {
2166 .fattr = info->fattr,
2169 struct rpc_message msg = {
2170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2175 nfs_fattr_init(info->fattr);
2176 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2179 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2180 struct nfs_fsinfo *info)
2182 struct nfs4_exception exception = { };
2185 err = nfs4_handle_exception(server,
2186 _nfs4_lookup_root(server, fhandle, info),
2188 } while (exception.retry);
2193 * get the file handle for the "/" directory on the server
2195 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2196 struct nfs_fsinfo *info)
2200 status = nfs4_lookup_root(server, fhandle, info);
2202 status = nfs4_server_capabilities(server, fhandle);
2204 status = nfs4_do_fsinfo(server, fhandle, info);
2205 return nfs4_map_errors(status);
2209 * Get locations and (maybe) other attributes of a referral.
2210 * Note that we'll actually follow the referral later when
2211 * we detect fsid mismatch in inode revalidation
2213 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2215 int status = -ENOMEM;
2216 struct page *page = NULL;
2217 struct nfs4_fs_locations *locations = NULL;
2219 page = alloc_page(GFP_KERNEL);
2222 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2223 if (locations == NULL)
2226 status = nfs4_proc_fs_locations(dir, name, locations, page);
2229 /* Make sure server returned a different fsid for the referral */
2230 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2231 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2236 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2237 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2239 fattr->mode = S_IFDIR;
2240 memset(fhandle, 0, sizeof(struct nfs_fh));
2248 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2250 struct nfs4_getattr_arg args = {
2252 .bitmask = server->attr_bitmask,
2254 struct nfs4_getattr_res res = {
2258 struct rpc_message msg = {
2259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2264 nfs_fattr_init(fattr);
2265 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2268 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2270 struct nfs4_exception exception = { };
2273 err = nfs4_handle_exception(server,
2274 _nfs4_proc_getattr(server, fhandle, fattr),
2276 } while (exception.retry);
2281 * The file is not closed if it is opened due to the a request to change
2282 * the size of the file. The open call will not be needed once the
2283 * VFS layer lookup-intents are implemented.
2285 * Close is called when the inode is destroyed.
2286 * If we haven't opened the file for O_WRONLY, we
2287 * need to in the size_change case to obtain a stateid.
2290 * Because OPEN is always done by name in nfsv4, it is
2291 * possible that we opened a different file by the same
2292 * name. We can recognize this race condition, but we
2293 * can't do anything about it besides returning an error.
2295 * This will be fixed with VFS changes (lookup-intent).
2298 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2299 struct iattr *sattr)
2301 struct inode *inode = dentry->d_inode;
2302 struct rpc_cred *cred = NULL;
2303 struct nfs4_state *state = NULL;
2306 nfs_fattr_init(fattr);
2308 /* Search for an existing open(O_WRITE) file */
2309 if (sattr->ia_valid & ATTR_FILE) {
2310 struct nfs_open_context *ctx;
2312 ctx = nfs_file_open_context(sattr->ia_file);
2319 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2321 nfs_setattr_update_inode(inode, sattr);
2325 static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2326 const struct nfs_fh *dirfh, const struct qstr *name,
2327 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2330 struct nfs4_lookup_arg args = {
2331 .bitmask = server->attr_bitmask,
2335 struct nfs4_lookup_res res = {
2340 struct rpc_message msg = {
2341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2346 nfs_fattr_init(fattr);
2348 dprintk("NFS call lookupfh %s\n", name->name);
2349 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2350 dprintk("NFS reply lookupfh: %d\n", status);
2354 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2355 struct qstr *name, struct nfs_fh *fhandle,
2356 struct nfs_fattr *fattr)
2358 struct nfs4_exception exception = { };
2361 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
2363 if (err == -NFS4ERR_MOVED) {
2367 err = nfs4_handle_exception(server, err, &exception);
2368 } while (exception.retry);
2372 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2373 const struct qstr *name, struct nfs_fh *fhandle,
2374 struct nfs_fattr *fattr)
2378 dprintk("NFS call lookup %s\n", name->name);
2379 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2380 if (status == -NFS4ERR_MOVED)
2381 status = nfs4_get_referral(dir, name, fattr, fhandle);
2382 dprintk("NFS reply lookup: %d\n", status);
2386 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2387 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2389 struct nfs4_exception exception = { };
2392 err = nfs4_handle_exception(NFS_SERVER(dir),
2393 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
2395 } while (exception.retry);
2399 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2401 struct nfs_server *server = NFS_SERVER(inode);
2402 struct nfs4_accessargs args = {
2403 .fh = NFS_FH(inode),
2404 .bitmask = server->attr_bitmask,
2406 struct nfs4_accessres res = {
2409 struct rpc_message msg = {
2410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2413 .rpc_cred = entry->cred,
2415 int mode = entry->mask;
2419 * Determine which access bits we want to ask for...
2421 if (mode & MAY_READ)
2422 args.access |= NFS4_ACCESS_READ;
2423 if (S_ISDIR(inode->i_mode)) {
2424 if (mode & MAY_WRITE)
2425 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2426 if (mode & MAY_EXEC)
2427 args.access |= NFS4_ACCESS_LOOKUP;
2429 if (mode & MAY_WRITE)
2430 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2431 if (mode & MAY_EXEC)
2432 args.access |= NFS4_ACCESS_EXECUTE;
2435 res.fattr = nfs_alloc_fattr();
2436 if (res.fattr == NULL)
2439 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2442 if (res.access & NFS4_ACCESS_READ)
2443 entry->mask |= MAY_READ;
2444 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2445 entry->mask |= MAY_WRITE;
2446 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2447 entry->mask |= MAY_EXEC;
2448 nfs_refresh_inode(inode, res.fattr);
2450 nfs_free_fattr(res.fattr);
2454 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2456 struct nfs4_exception exception = { };
2459 err = nfs4_handle_exception(NFS_SERVER(inode),
2460 _nfs4_proc_access(inode, entry),
2462 } while (exception.retry);
2467 * TODO: For the time being, we don't try to get any attributes
2468 * along with any of the zero-copy operations READ, READDIR,
2471 * In the case of the first three, we want to put the GETATTR
2472 * after the read-type operation -- this is because it is hard
2473 * to predict the length of a GETATTR response in v4, and thus
2474 * align the READ data correctly. This means that the GETATTR
2475 * may end up partially falling into the page cache, and we should
2476 * shift it into the 'tail' of the xdr_buf before processing.
2477 * To do this efficiently, we need to know the total length
2478 * of data received, which doesn't seem to be available outside
2481 * In the case of WRITE, we also want to put the GETATTR after
2482 * the operation -- in this case because we want to make sure
2483 * we get the post-operation mtime and size. This means that
2484 * we can't use xdr_encode_pages() as written: we need a variant
2485 * of it which would leave room in the 'tail' iovec.
2487 * Both of these changes to the XDR layer would in fact be quite
2488 * minor, but I decided to leave them for a subsequent patch.
2490 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2491 unsigned int pgbase, unsigned int pglen)
2493 struct nfs4_readlink args = {
2494 .fh = NFS_FH(inode),
2499 struct nfs4_readlink_res res;
2500 struct rpc_message msg = {
2501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2506 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2509 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2510 unsigned int pgbase, unsigned int pglen)
2512 struct nfs4_exception exception = { };
2515 err = nfs4_handle_exception(NFS_SERVER(inode),
2516 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2518 } while (exception.retry);
2524 * We will need to arrange for the VFS layer to provide an atomic open.
2525 * Until then, this create/open method is prone to inefficiency and race
2526 * conditions due to the lookup, create, and open VFS calls from sys_open()
2527 * placed on the wire.
2529 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2530 * The file will be opened again in the subsequent VFS open call
2531 * (nfs4_proc_file_open).
2533 * The open for read will just hang around to be used by any process that
2534 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2538 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2539 int flags, struct nfs_open_context *ctx)
2541 struct path my_path = {
2544 struct path *path = &my_path;
2545 struct nfs4_state *state;
2546 struct rpc_cred *cred = NULL;
2555 sattr->ia_mode &= ~current_umask();
2556 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2558 if (IS_ERR(state)) {
2559 status = PTR_ERR(state);
2562 d_add(dentry, igrab(state->inode));
2563 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2567 nfs4_close_sync(path, state, fmode);
2572 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2574 struct nfs_server *server = NFS_SERVER(dir);
2575 struct nfs_removeargs args = {
2577 .name.len = name->len,
2578 .name.name = name->name,
2579 .bitmask = server->attr_bitmask,
2581 struct nfs_removeres res = {
2584 struct rpc_message msg = {
2585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2589 int status = -ENOMEM;
2591 res.dir_attr = nfs_alloc_fattr();
2592 if (res.dir_attr == NULL)
2595 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2597 update_changeattr(dir, &res.cinfo);
2598 nfs_post_op_update_inode(dir, res.dir_attr);
2600 nfs_free_fattr(res.dir_attr);
2605 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2607 struct nfs4_exception exception = { };
2610 err = nfs4_handle_exception(NFS_SERVER(dir),
2611 _nfs4_proc_remove(dir, name),
2613 } while (exception.retry);
2617 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2619 struct nfs_server *server = NFS_SERVER(dir);
2620 struct nfs_removeargs *args = msg->rpc_argp;
2621 struct nfs_removeres *res = msg->rpc_resp;
2623 args->bitmask = server->cache_consistency_bitmask;
2624 res->server = server;
2625 res->seq_res.sr_slot = NULL;
2626 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2629 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2631 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2633 if (!nfs4_sequence_done(task, &res->seq_res))
2635 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2637 update_changeattr(dir, &res->cinfo);
2638 nfs_post_op_update_inode(dir, res->dir_attr);
2642 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2644 struct nfs_server *server = NFS_SERVER(dir);
2645 struct nfs_renameargs *arg = msg->rpc_argp;
2646 struct nfs_renameres *res = msg->rpc_resp;
2648 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2649 arg->bitmask = server->attr_bitmask;
2650 res->server = server;
2653 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2654 struct inode *new_dir)
2656 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2658 if (!nfs4_sequence_done(task, &res->seq_res))
2660 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2663 update_changeattr(old_dir, &res->old_cinfo);
2664 nfs_post_op_update_inode(old_dir, res->old_fattr);
2665 update_changeattr(new_dir, &res->new_cinfo);
2666 nfs_post_op_update_inode(new_dir, res->new_fattr);
2670 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2671 struct inode *new_dir, struct qstr *new_name)
2673 struct nfs_server *server = NFS_SERVER(old_dir);
2674 struct nfs_renameargs arg = {
2675 .old_dir = NFS_FH(old_dir),
2676 .new_dir = NFS_FH(new_dir),
2677 .old_name = old_name,
2678 .new_name = new_name,
2679 .bitmask = server->attr_bitmask,
2681 struct nfs_renameres res = {
2684 struct rpc_message msg = {
2685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2689 int status = -ENOMEM;
2691 res.old_fattr = nfs_alloc_fattr();
2692 res.new_fattr = nfs_alloc_fattr();
2693 if (res.old_fattr == NULL || res.new_fattr == NULL)
2696 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2698 update_changeattr(old_dir, &res.old_cinfo);
2699 nfs_post_op_update_inode(old_dir, res.old_fattr);
2700 update_changeattr(new_dir, &res.new_cinfo);
2701 nfs_post_op_update_inode(new_dir, res.new_fattr);
2704 nfs_free_fattr(res.new_fattr);
2705 nfs_free_fattr(res.old_fattr);
2709 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2710 struct inode *new_dir, struct qstr *new_name)
2712 struct nfs4_exception exception = { };
2715 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2716 _nfs4_proc_rename(old_dir, old_name,
2719 } while (exception.retry);
2723 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2725 struct nfs_server *server = NFS_SERVER(inode);
2726 struct nfs4_link_arg arg = {
2727 .fh = NFS_FH(inode),
2728 .dir_fh = NFS_FH(dir),
2730 .bitmask = server->attr_bitmask,
2732 struct nfs4_link_res res = {
2735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2740 int status = -ENOMEM;
2742 res.fattr = nfs_alloc_fattr();
2743 res.dir_attr = nfs_alloc_fattr();
2744 if (res.fattr == NULL || res.dir_attr == NULL)
2747 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2749 update_changeattr(dir, &res.cinfo);
2750 nfs_post_op_update_inode(dir, res.dir_attr);
2751 nfs_post_op_update_inode(inode, res.fattr);
2754 nfs_free_fattr(res.dir_attr);
2755 nfs_free_fattr(res.fattr);
2759 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2761 struct nfs4_exception exception = { };
2764 err = nfs4_handle_exception(NFS_SERVER(inode),
2765 _nfs4_proc_link(inode, dir, name),
2767 } while (exception.retry);
2771 struct nfs4_createdata {
2772 struct rpc_message msg;
2773 struct nfs4_create_arg arg;
2774 struct nfs4_create_res res;
2776 struct nfs_fattr fattr;
2777 struct nfs_fattr dir_fattr;
2780 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2781 struct qstr *name, struct iattr *sattr, u32 ftype)
2783 struct nfs4_createdata *data;
2785 data = kzalloc(sizeof(*data), GFP_KERNEL);
2787 struct nfs_server *server = NFS_SERVER(dir);
2789 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2790 data->msg.rpc_argp = &data->arg;
2791 data->msg.rpc_resp = &data->res;
2792 data->arg.dir_fh = NFS_FH(dir);
2793 data->arg.server = server;
2794 data->arg.name = name;
2795 data->arg.attrs = sattr;
2796 data->arg.ftype = ftype;
2797 data->arg.bitmask = server->attr_bitmask;
2798 data->res.server = server;
2799 data->res.fh = &data->fh;
2800 data->res.fattr = &data->fattr;
2801 data->res.dir_fattr = &data->dir_fattr;
2802 nfs_fattr_init(data->res.fattr);
2803 nfs_fattr_init(data->res.dir_fattr);
2808 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2810 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2811 &data->arg.seq_args, &data->res.seq_res, 1);
2813 update_changeattr(dir, &data->res.dir_cinfo);
2814 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2815 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2820 static void nfs4_free_createdata(struct nfs4_createdata *data)
2825 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2826 struct page *page, unsigned int len, struct iattr *sattr)
2828 struct nfs4_createdata *data;
2829 int status = -ENAMETOOLONG;
2831 if (len > NFS4_MAXPATHLEN)
2835 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2839 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2840 data->arg.u.symlink.pages = &page;
2841 data->arg.u.symlink.len = len;
2843 status = nfs4_do_create(dir, dentry, data);
2845 nfs4_free_createdata(data);
2850 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2851 struct page *page, unsigned int len, struct iattr *sattr)
2853 struct nfs4_exception exception = { };
2856 err = nfs4_handle_exception(NFS_SERVER(dir),
2857 _nfs4_proc_symlink(dir, dentry, page,
2860 } while (exception.retry);
2864 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2865 struct iattr *sattr)
2867 struct nfs4_createdata *data;
2868 int status = -ENOMEM;
2870 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2874 status = nfs4_do_create(dir, dentry, data);
2876 nfs4_free_createdata(data);
2881 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2882 struct iattr *sattr)
2884 struct nfs4_exception exception = { };
2887 sattr->ia_mode &= ~current_umask();
2889 err = nfs4_handle_exception(NFS_SERVER(dir),
2890 _nfs4_proc_mkdir(dir, dentry, sattr),
2892 } while (exception.retry);
2896 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2897 u64 cookie, struct page **pages, unsigned int count, int plus)
2899 struct inode *dir = dentry->d_inode;
2900 struct nfs4_readdir_arg args = {
2905 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2908 struct nfs4_readdir_res res;
2909 struct rpc_message msg = {
2910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2917 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2918 dentry->d_parent->d_name.name,
2919 dentry->d_name.name,
2920 (unsigned long long)cookie);
2921 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2922 res.pgbase = args.pgbase;
2923 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
2925 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2926 status += args.pgbase;
2929 nfs_invalidate_atime(dir);
2931 dprintk("%s: returns %d\n", __func__, status);
2935 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2936 u64 cookie, struct page **pages, unsigned int count, int plus)
2938 struct nfs4_exception exception = { };
2941 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2942 _nfs4_proc_readdir(dentry, cred, cookie,
2943 pages, count, plus),
2945 } while (exception.retry);
2949 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2950 struct iattr *sattr, dev_t rdev)
2952 struct nfs4_createdata *data;
2953 int mode = sattr->ia_mode;
2954 int status = -ENOMEM;
2956 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2957 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2959 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2964 data->arg.ftype = NF4FIFO;
2965 else if (S_ISBLK(mode)) {
2966 data->arg.ftype = NF4BLK;
2967 data->arg.u.device.specdata1 = MAJOR(rdev);
2968 data->arg.u.device.specdata2 = MINOR(rdev);
2970 else if (S_ISCHR(mode)) {
2971 data->arg.ftype = NF4CHR;
2972 data->arg.u.device.specdata1 = MAJOR(rdev);
2973 data->arg.u.device.specdata2 = MINOR(rdev);
2976 status = nfs4_do_create(dir, dentry, data);
2978 nfs4_free_createdata(data);
2983 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2984 struct iattr *sattr, dev_t rdev)
2986 struct nfs4_exception exception = { };
2989 sattr->ia_mode &= ~current_umask();
2991 err = nfs4_handle_exception(NFS_SERVER(dir),
2992 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2994 } while (exception.retry);
2998 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2999 struct nfs_fsstat *fsstat)
3001 struct nfs4_statfs_arg args = {
3003 .bitmask = server->attr_bitmask,
3005 struct nfs4_statfs_res res = {
3008 struct rpc_message msg = {
3009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3014 nfs_fattr_init(fsstat->fattr);
3015 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3018 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3020 struct nfs4_exception exception = { };
3023 err = nfs4_handle_exception(server,
3024 _nfs4_proc_statfs(server, fhandle, fsstat),
3026 } while (exception.retry);
3030 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3031 struct nfs_fsinfo *fsinfo)
3033 struct nfs4_fsinfo_arg args = {
3035 .bitmask = server->attr_bitmask,
3037 struct nfs4_fsinfo_res res = {
3040 struct rpc_message msg = {
3041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3046 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3049 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3051 struct nfs4_exception exception = { };
3055 err = nfs4_handle_exception(server,
3056 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3058 } while (exception.retry);
3062 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3064 nfs_fattr_init(fsinfo->fattr);
3065 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3068 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3069 struct nfs_pathconf *pathconf)
3071 struct nfs4_pathconf_arg args = {
3073 .bitmask = server->attr_bitmask,
3075 struct nfs4_pathconf_res res = {
3076 .pathconf = pathconf,
3078 struct rpc_message msg = {
3079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3084 /* None of the pathconf attributes are mandatory to implement */
3085 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3086 memset(pathconf, 0, sizeof(*pathconf));
3090 nfs_fattr_init(pathconf->fattr);
3091 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3094 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3095 struct nfs_pathconf *pathconf)
3097 struct nfs4_exception exception = { };
3101 err = nfs4_handle_exception(server,
3102 _nfs4_proc_pathconf(server, fhandle, pathconf),
3104 } while (exception.retry);
3108 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3110 struct nfs_server *server = NFS_SERVER(data->inode);
3112 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3113 nfs_restart_rpc(task, server->nfs_client);
3117 nfs_invalidate_atime(data->inode);
3118 if (task->tk_status > 0)
3119 renew_lease(server, data->timestamp);
3123 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3126 dprintk("--> %s\n", __func__);
3128 if (!nfs4_sequence_done(task, &data->res.seq_res))
3131 return data->read_done_cb(task, data);
3134 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3136 data->timestamp = jiffies;
3137 data->read_done_cb = nfs4_read_done_cb;
3138 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3141 /* Reset the the nfs_read_data to send the read to the MDS. */
3142 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3144 dprintk("%s Reset task for i/o through\n", __func__);
3145 put_lseg(data->lseg);
3147 /* offsets will differ in the dense stripe case */
3148 data->args.offset = data->mds_offset;
3149 data->ds_clp = NULL;
3150 data->args.fh = NFS_FH(data->inode);
3151 data->read_done_cb = nfs4_read_done_cb;
3152 task->tk_ops = data->mds_ops;
3153 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3155 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3157 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3159 struct inode *inode = data->inode;
3161 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3162 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3165 if (task->tk_status >= 0) {
3166 renew_lease(NFS_SERVER(inode), data->timestamp);
3167 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3172 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3174 if (!nfs4_sequence_done(task, &data->res.seq_res))
3176 return data->write_done_cb(task, data);
3179 /* Reset the the nfs_write_data to send the write to the MDS. */
3180 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3182 dprintk("%s Reset task for i/o through\n", __func__);
3183 put_lseg(data->lseg);
3185 data->ds_clp = NULL;
3186 data->write_done_cb = nfs4_write_done_cb;
3187 data->args.fh = NFS_FH(data->inode);
3188 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3189 data->args.offset = data->mds_offset;
3190 data->res.fattr = &data->fattr;
3191 task->tk_ops = data->mds_ops;
3192 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3194 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3196 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3198 struct nfs_server *server = NFS_SERVER(data->inode);
3201 data->args.bitmask = NULL;
3202 data->res.fattr = NULL;
3204 data->args.bitmask = server->cache_consistency_bitmask;
3205 if (!data->write_done_cb)
3206 data->write_done_cb = nfs4_write_done_cb;
3207 data->res.server = server;
3208 data->timestamp = jiffies;
3210 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3213 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3215 struct inode *inode = data->inode;
3217 if (!nfs4_sequence_done(task, &data->res.seq_res))
3220 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3221 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3224 nfs_refresh_inode(inode, data->res.fattr);
3228 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3230 struct nfs_server *server = NFS_SERVER(data->inode);
3232 data->args.bitmask = server->cache_consistency_bitmask;
3233 data->res.server = server;
3234 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3237 struct nfs4_renewdata {
3238 struct nfs_client *client;
3239 unsigned long timestamp;
3243 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3244 * standalone procedure for queueing an asynchronous RENEW.
3246 static void nfs4_renew_release(void *calldata)
3248 struct nfs4_renewdata *data = calldata;
3249 struct nfs_client *clp = data->client;
3251 if (atomic_read(&clp->cl_count) > 1)
3252 nfs4_schedule_state_renewal(clp);
3253 nfs_put_client(clp);
3257 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3259 struct nfs4_renewdata *data = calldata;
3260 struct nfs_client *clp = data->client;
3261 unsigned long timestamp = data->timestamp;
3263 if (task->tk_status < 0) {
3264 /* Unless we're shutting down, schedule state recovery! */
3265 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3266 nfs4_schedule_lease_recovery(clp);
3269 do_renew_lease(clp, timestamp);
3272 static const struct rpc_call_ops nfs4_renew_ops = {
3273 .rpc_call_done = nfs4_renew_done,
3274 .rpc_release = nfs4_renew_release,
3277 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3279 struct rpc_message msg = {
3280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3284 struct nfs4_renewdata *data;
3286 if (!atomic_inc_not_zero(&clp->cl_count))
3288 data = kmalloc(sizeof(*data), GFP_KERNEL);
3292 data->timestamp = jiffies;
3293 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3294 &nfs4_renew_ops, data);
3297 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3299 struct rpc_message msg = {
3300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3304 unsigned long now = jiffies;
3307 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3310 do_renew_lease(clp, now);
3314 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3316 return (server->caps & NFS_CAP_ACLS)
3317 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3318 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3321 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3322 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3325 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3327 static void buf_to_pages(const void *buf, size_t buflen,
3328 struct page **pages, unsigned int *pgbase)
3330 const void *p = buf;
3332 *pgbase = offset_in_page(buf);
3334 while (p < buf + buflen) {
3335 *(pages++) = virt_to_page(p);
3336 p += PAGE_CACHE_SIZE;
3340 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3341 struct page **pages, unsigned int *pgbase)
3343 struct page *newpage, **spages;
3349 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3350 newpage = alloc_page(GFP_KERNEL);
3352 if (newpage == NULL)
3354 memcpy(page_address(newpage), buf, len);
3359 } while (buflen != 0);
3365 __free_page(spages[rc-1]);
3369 struct nfs4_cached_acl {
3375 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3377 struct nfs_inode *nfsi = NFS_I(inode);
3379 spin_lock(&inode->i_lock);
3380 kfree(nfsi->nfs4_acl);
3381 nfsi->nfs4_acl = acl;
3382 spin_unlock(&inode->i_lock);
3385 static void nfs4_zap_acl_attr(struct inode *inode)
3387 nfs4_set_cached_acl(inode, NULL);
3390 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3392 struct nfs_inode *nfsi = NFS_I(inode);
3393 struct nfs4_cached_acl *acl;
3396 spin_lock(&inode->i_lock);
3397 acl = nfsi->nfs4_acl;
3400 if (buf == NULL) /* user is just asking for length */
3402 if (acl->cached == 0)
3404 ret = -ERANGE; /* see getxattr(2) man page */
3405 if (acl->len > buflen)
3407 memcpy(buf, acl->data, acl->len);
3411 spin_unlock(&inode->i_lock);
3415 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3417 struct nfs4_cached_acl *acl;
3419 if (buf && acl_len <= PAGE_SIZE) {
3420 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3424 memcpy(acl->data, buf, acl_len);
3426 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3433 nfs4_set_cached_acl(inode, acl);
3436 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3438 struct page *pages[NFS4ACL_MAXPAGES];
3439 struct nfs_getaclargs args = {
3440 .fh = NFS_FH(inode),
3444 struct nfs_getaclres res = {
3448 struct rpc_message msg = {
3449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3453 struct page *localpage = NULL;
3456 if (buflen < PAGE_SIZE) {
3457 /* As long as we're doing a round trip to the server anyway,
3458 * let's be prepared for a page of acl data. */
3459 localpage = alloc_page(GFP_KERNEL);
3460 resp_buf = page_address(localpage);
3461 if (localpage == NULL)
3463 args.acl_pages[0] = localpage;
3464 args.acl_pgbase = 0;
3465 args.acl_len = PAGE_SIZE;
3468 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3470 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3473 if (res.acl_len > args.acl_len)
3474 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3476 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3479 if (res.acl_len > buflen)
3482 memcpy(buf, resp_buf, res.acl_len);
3487 __free_page(localpage);
3491 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3493 struct nfs4_exception exception = { };
3496 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3499 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3500 } while (exception.retry);
3504 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3506 struct nfs_server *server = NFS_SERVER(inode);
3509 if (!nfs4_server_supports_acls(server))
3511 ret = nfs_revalidate_inode(server, inode);
3514 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3515 nfs_zap_acl_cache(inode);
3516 ret = nfs4_read_cached_acl(inode, buf, buflen);
3519 return nfs4_get_acl_uncached(inode, buf, buflen);
3522 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3524 struct nfs_server *server = NFS_SERVER(inode);
3525 struct page *pages[NFS4ACL_MAXPAGES];
3526 struct nfs_setaclargs arg = {
3527 .fh = NFS_FH(inode),
3531 struct nfs_setaclres res;
3532 struct rpc_message msg = {
3533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3539 if (!nfs4_server_supports_acls(server))
3541 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3544 nfs_inode_return_delegation(inode);
3545 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3548 * Free each page after tx, so the only ref left is
3549 * held by the network stack
3552 put_page(pages[i-1]);
3555 * Acl update can result in inode attribute update.
3556 * so mark the attribute cache invalid.
3558 spin_lock(&inode->i_lock);
3559 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3560 spin_unlock(&inode->i_lock);
3561 nfs_access_zap_cache(inode);
3562 nfs_zap_acl_cache(inode);
3566 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3568 struct nfs4_exception exception = { };
3571 err = nfs4_handle_exception(NFS_SERVER(inode),
3572 __nfs4_proc_set_acl(inode, buf, buflen),
3574 } while (exception.retry);
3579 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3581 struct nfs_client *clp = server->nfs_client;
3583 if (task->tk_status >= 0)
3585 switch(task->tk_status) {
3586 case -NFS4ERR_ADMIN_REVOKED:
3587 case -NFS4ERR_BAD_STATEID:
3588 case -NFS4ERR_OPENMODE:
3591 nfs4_schedule_stateid_recovery(server, state);
3592 goto wait_on_recovery;
3593 case -NFS4ERR_STALE_STATEID:
3594 case -NFS4ERR_STALE_CLIENTID:
3595 case -NFS4ERR_EXPIRED:
3596 nfs4_schedule_lease_recovery(clp);
3597 goto wait_on_recovery;
3598 #if defined(CONFIG_NFS_V4_1)
3599 case -NFS4ERR_BADSESSION:
3600 case -NFS4ERR_BADSLOT:
3601 case -NFS4ERR_BAD_HIGH_SLOT:
3602 case -NFS4ERR_DEADSESSION:
3603 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3604 case -NFS4ERR_SEQ_FALSE_RETRY:
3605 case -NFS4ERR_SEQ_MISORDERED:
3606 dprintk("%s ERROR %d, Reset session\n", __func__,
3608 nfs4_schedule_session_recovery(clp->cl_session);
3609 task->tk_status = 0;
3611 #endif /* CONFIG_NFS_V4_1 */
3612 case -NFS4ERR_DELAY:
3613 nfs_inc_server_stats(server, NFSIOS_DELAY);
3614 case -NFS4ERR_GRACE:
3616 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3617 task->tk_status = 0;
3619 case -NFS4ERR_OLD_STATEID:
3620 task->tk_status = 0;
3623 task->tk_status = nfs4_map_errors(task->tk_status);
3626 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3627 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3628 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3629 task->tk_status = 0;
3633 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3634 unsigned short port, struct rpc_cred *cred,
3635 struct nfs4_setclientid_res *res)
3637 nfs4_verifier sc_verifier;
3638 struct nfs4_setclientid setclientid = {
3639 .sc_verifier = &sc_verifier,
3641 .sc_cb_ident = clp->cl_cb_ident,
3643 struct rpc_message msg = {
3644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3645 .rpc_argp = &setclientid,
3653 p = (__be32*)sc_verifier.data;
3654 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3655 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3658 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3659 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3661 rpc_peeraddr2str(clp->cl_rpcclient,
3663 rpc_peeraddr2str(clp->cl_rpcclient,
3665 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3666 clp->cl_id_uniquifier);
3667 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3668 sizeof(setclientid.sc_netid),
3669 rpc_peeraddr2str(clp->cl_rpcclient,
3670 RPC_DISPLAY_NETID));
3671 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3672 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3673 clp->cl_ipaddr, port >> 8, port & 255);
3675 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3676 if (status != -NFS4ERR_CLID_INUSE)
3681 ssleep(clp->cl_lease_time / HZ + 1);
3683 if (++clp->cl_id_uniquifier == 0)
3689 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3690 struct nfs4_setclientid_res *arg,
3691 struct rpc_cred *cred)
3693 struct nfs_fsinfo fsinfo;
3694 struct rpc_message msg = {
3695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3697 .rpc_resp = &fsinfo,
3704 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3706 spin_lock(&clp->cl_lock);
3707 clp->cl_lease_time = fsinfo.lease_time * HZ;
3708 clp->cl_last_renewal = now;
3709 spin_unlock(&clp->cl_lock);
3714 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3715 struct nfs4_setclientid_res *arg,
3716 struct rpc_cred *cred)
3721 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3725 case -NFS4ERR_RESOURCE:
3726 /* The IBM lawyers misread another document! */
3727 case -NFS4ERR_DELAY:
3728 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3734 struct nfs4_delegreturndata {
3735 struct nfs4_delegreturnargs args;
3736 struct nfs4_delegreturnres res;
3738 nfs4_stateid stateid;
3739 unsigned long timestamp;
3740 struct nfs_fattr fattr;
3744 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3746 struct nfs4_delegreturndata *data = calldata;
3748 if (!nfs4_sequence_done(task, &data->res.seq_res))
3751 switch (task->tk_status) {
3752 case -NFS4ERR_STALE_STATEID:
3753 case -NFS4ERR_EXPIRED:
3755 renew_lease(data->res.server, data->timestamp);
3758 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3760 nfs_restart_rpc(task, data->res.server->nfs_client);
3764 data->rpc_status = task->tk_status;
3767 static void nfs4_delegreturn_release(void *calldata)
3772 #if defined(CONFIG_NFS_V4_1)
3773 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3775 struct nfs4_delegreturndata *d_data;
3777 d_data = (struct nfs4_delegreturndata *)data;
3779 if (nfs4_setup_sequence(d_data->res.server,
3780 &d_data->args.seq_args,
3781 &d_data->res.seq_res, 1, task))
3783 rpc_call_start(task);
3785 #endif /* CONFIG_NFS_V4_1 */
3787 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3788 #if defined(CONFIG_NFS_V4_1)
3789 .rpc_call_prepare = nfs4_delegreturn_prepare,
3790 #endif /* CONFIG_NFS_V4_1 */
3791 .rpc_call_done = nfs4_delegreturn_done,
3792 .rpc_release = nfs4_delegreturn_release,
3795 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3797 struct nfs4_delegreturndata *data;
3798 struct nfs_server *server = NFS_SERVER(inode);
3799 struct rpc_task *task;
3800 struct rpc_message msg = {
3801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3804 struct rpc_task_setup task_setup_data = {
3805 .rpc_client = server->client,
3806 .rpc_message = &msg,
3807 .callback_ops = &nfs4_delegreturn_ops,
3808 .flags = RPC_TASK_ASYNC,
3812 data = kzalloc(sizeof(*data), GFP_NOFS);
3815 data->args.fhandle = &data->fh;
3816 data->args.stateid = &data->stateid;
3817 data->args.bitmask = server->attr_bitmask;
3818 nfs_copy_fh(&data->fh, NFS_FH(inode));
3819 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3820 data->res.fattr = &data->fattr;
3821 data->res.server = server;
3822 nfs_fattr_init(data->res.fattr);
3823 data->timestamp = jiffies;
3824 data->rpc_status = 0;
3826 task_setup_data.callback_data = data;
3827 msg.rpc_argp = &data->args;
3828 msg.rpc_resp = &data->res;
3829 task = rpc_run_task(&task_setup_data);
3831 return PTR_ERR(task);
3834 status = nfs4_wait_for_completion_rpc_task(task);
3837 status = data->rpc_status;
3840 nfs_refresh_inode(inode, &data->fattr);
3846 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3848 struct nfs_server *server = NFS_SERVER(inode);
3849 struct nfs4_exception exception = { };
3852 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3854 case -NFS4ERR_STALE_STATEID:
3855 case -NFS4ERR_EXPIRED:
3859 err = nfs4_handle_exception(server, err, &exception);
3860 } while (exception.retry);
3864 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3865 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3868 * sleep, with exponential backoff, and retry the LOCK operation.
3870 static unsigned long
3871 nfs4_set_lock_task_retry(unsigned long timeout)
3873 schedule_timeout_killable(timeout);
3875 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3876 return NFS4_LOCK_MAXTIMEOUT;
3880 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3882 struct inode *inode = state->inode;
3883 struct nfs_server *server = NFS_SERVER(inode);
3884 struct nfs_client *clp = server->nfs_client;
3885 struct nfs_lockt_args arg = {
3886 .fh = NFS_FH(inode),
3889 struct nfs_lockt_res res = {
3892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3896 .rpc_cred = state->owner->so_cred,
3898 struct nfs4_lock_state *lsp;
3901 arg.lock_owner.clientid = clp->cl_clientid;
3902 status = nfs4_set_lock_state(state, request);
3905 lsp = request->fl_u.nfs4_fl.owner;
3906 arg.lock_owner.id = lsp->ls_id.id;
3907 arg.lock_owner.s_dev = server->s_dev;
3908 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3911 request->fl_type = F_UNLCK;
3913 case -NFS4ERR_DENIED:
3916 request->fl_ops->fl_release_private(request);
3921 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3923 struct nfs4_exception exception = { };
3927 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3928 _nfs4_proc_getlk(state, cmd, request),
3930 } while (exception.retry);
3934 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3937 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3939 res = posix_lock_file_wait(file, fl);
3942 res = flock_lock_file_wait(file, fl);
3950 struct nfs4_unlockdata {
3951 struct nfs_locku_args arg;
3952 struct nfs_locku_res res;
3953 struct nfs4_lock_state *lsp;
3954 struct nfs_open_context *ctx;
3955 struct file_lock fl;
3956 const struct nfs_server *server;
3957 unsigned long timestamp;
3960 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3961 struct nfs_open_context *ctx,
3962 struct nfs4_lock_state *lsp,
3963 struct nfs_seqid *seqid)
3965 struct nfs4_unlockdata *p;
3966 struct inode *inode = lsp->ls_state->inode;
3968 p = kzalloc(sizeof(*p), GFP_NOFS);
3971 p->arg.fh = NFS_FH(inode);
3973 p->arg.seqid = seqid;
3974 p->res.seqid = seqid;
3975 p->arg.stateid = &lsp->ls_stateid;
3977 atomic_inc(&lsp->ls_count);
3978 /* Ensure we don't close file until we're done freeing locks! */
3979 p->ctx = get_nfs_open_context(ctx);
3980 memcpy(&p->fl, fl, sizeof(p->fl));
3981 p->server = NFS_SERVER(inode);
3985 static void nfs4_locku_release_calldata(void *data)
3987 struct nfs4_unlockdata *calldata = data;
3988 nfs_free_seqid(calldata->arg.seqid);
3989 nfs4_put_lock_state(calldata->lsp);
3990 put_nfs_open_context(calldata->ctx);
3994 static void nfs4_locku_done(struct rpc_task *task, void *data)
3996 struct nfs4_unlockdata *calldata = data;
3998 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4000 switch (task->tk_status) {
4002 memcpy(calldata->lsp->ls_stateid.data,
4003 calldata->res.stateid.data,
4004 sizeof(calldata->lsp->ls_stateid.data));
4005 renew_lease(calldata->server, calldata->timestamp);
4007 case -NFS4ERR_BAD_STATEID:
4008 case -NFS4ERR_OLD_STATEID:
4009 case -NFS4ERR_STALE_STATEID:
4010 case -NFS4ERR_EXPIRED:
4013 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4014 nfs_restart_rpc(task,
4015 calldata->server->nfs_client);
4019 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4021 struct nfs4_unlockdata *calldata = data;
4023 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4025 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4026 /* Note: exit _without_ running nfs4_locku_done */
4027 task->tk_action = NULL;
4030 calldata->timestamp = jiffies;
4031 if (nfs4_setup_sequence(calldata->server,
4032 &calldata->arg.seq_args,
4033 &calldata->res.seq_res, 1, task))
4035 rpc_call_start(task);
4038 static const struct rpc_call_ops nfs4_locku_ops = {
4039 .rpc_call_prepare = nfs4_locku_prepare,
4040 .rpc_call_done = nfs4_locku_done,
4041 .rpc_release = nfs4_locku_release_calldata,
4044 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4045 struct nfs_open_context *ctx,
4046 struct nfs4_lock_state *lsp,
4047 struct nfs_seqid *seqid)
4049 struct nfs4_unlockdata *data;
4050 struct rpc_message msg = {
4051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4052 .rpc_cred = ctx->cred,
4054 struct rpc_task_setup task_setup_data = {
4055 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4056 .rpc_message = &msg,
4057 .callback_ops = &nfs4_locku_ops,
4058 .workqueue = nfsiod_workqueue,
4059 .flags = RPC_TASK_ASYNC,
4062 /* Ensure this is an unlock - when canceling a lock, the
4063 * canceled lock is passed in, and it won't be an unlock.
4065 fl->fl_type = F_UNLCK;
4067 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4069 nfs_free_seqid(seqid);
4070 return ERR_PTR(-ENOMEM);
4073 msg.rpc_argp = &data->arg;
4074 msg.rpc_resp = &data->res;
4075 task_setup_data.callback_data = data;
4076 return rpc_run_task(&task_setup_data);
4079 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4081 struct nfs_inode *nfsi = NFS_I(state->inode);
4082 struct nfs_seqid *seqid;
4083 struct nfs4_lock_state *lsp;
4084 struct rpc_task *task;
4086 unsigned char fl_flags = request->fl_flags;
4088 status = nfs4_set_lock_state(state, request);
4089 /* Unlock _before_ we do the RPC call */
4090 request->fl_flags |= FL_EXISTS;
4091 down_read(&nfsi->rwsem);
4092 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4093 up_read(&nfsi->rwsem);
4096 up_read(&nfsi->rwsem);
4099 /* Is this a delegated lock? */
4100 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4102 lsp = request->fl_u.nfs4_fl.owner;
4103 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4107 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4108 status = PTR_ERR(task);
4111 status = nfs4_wait_for_completion_rpc_task(task);
4114 request->fl_flags = fl_flags;
4118 struct nfs4_lockdata {
4119 struct nfs_lock_args arg;
4120 struct nfs_lock_res res;
4121 struct nfs4_lock_state *lsp;
4122 struct nfs_open_context *ctx;
4123 struct file_lock fl;
4124 unsigned long timestamp;
4127 struct nfs_server *server;
4130 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4131 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4134 struct nfs4_lockdata *p;
4135 struct inode *inode = lsp->ls_state->inode;
4136 struct nfs_server *server = NFS_SERVER(inode);
4138 p = kzalloc(sizeof(*p), gfp_mask);
4142 p->arg.fh = NFS_FH(inode);
4144 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4145 if (p->arg.open_seqid == NULL)
4147 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4148 if (p->arg.lock_seqid == NULL)
4149 goto out_free_seqid;
4150 p->arg.lock_stateid = &lsp->ls_stateid;
4151 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4152 p->arg.lock_owner.id = lsp->ls_id.id;
4153 p->arg.lock_owner.s_dev = server->s_dev;
4154 p->res.lock_seqid = p->arg.lock_seqid;
4157 atomic_inc(&lsp->ls_count);
4158 p->ctx = get_nfs_open_context(ctx);
4159 memcpy(&p->fl, fl, sizeof(p->fl));
4162 nfs_free_seqid(p->arg.open_seqid);
4168 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4170 struct nfs4_lockdata *data = calldata;
4171 struct nfs4_state *state = data->lsp->ls_state;
4173 dprintk("%s: begin!\n", __func__);
4174 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4176 /* Do we need to do an open_to_lock_owner? */
4177 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4178 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4180 data->arg.open_stateid = &state->stateid;
4181 data->arg.new_lock_owner = 1;
4182 data->res.open_seqid = data->arg.open_seqid;
4184 data->arg.new_lock_owner = 0;
4185 data->timestamp = jiffies;
4186 if (nfs4_setup_sequence(data->server,
4187 &data->arg.seq_args,
4188 &data->res.seq_res, 1, task))
4190 rpc_call_start(task);
4191 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4194 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4196 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4197 nfs4_lock_prepare(task, calldata);
4200 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4202 struct nfs4_lockdata *data = calldata;
4204 dprintk("%s: begin!\n", __func__);
4206 if (!nfs4_sequence_done(task, &data->res.seq_res))
4209 data->rpc_status = task->tk_status;
4210 if (data->arg.new_lock_owner != 0) {
4211 if (data->rpc_status == 0)
4212 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4216 if (data->rpc_status == 0) {
4217 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4218 sizeof(data->lsp->ls_stateid.data));
4219 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4220 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4223 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4226 static void nfs4_lock_release(void *calldata)
4228 struct nfs4_lockdata *data = calldata;
4230 dprintk("%s: begin!\n", __func__);
4231 nfs_free_seqid(data->arg.open_seqid);
4232 if (data->cancelled != 0) {
4233 struct rpc_task *task;
4234 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4235 data->arg.lock_seqid);
4237 rpc_put_task_async(task);
4238 dprintk("%s: cancelling lock!\n", __func__);
4240 nfs_free_seqid(data->arg.lock_seqid);
4241 nfs4_put_lock_state(data->lsp);
4242 put_nfs_open_context(data->ctx);
4244 dprintk("%s: done!\n", __func__);
4247 static const struct rpc_call_ops nfs4_lock_ops = {
4248 .rpc_call_prepare = nfs4_lock_prepare,
4249 .rpc_call_done = nfs4_lock_done,
4250 .rpc_release = nfs4_lock_release,
4253 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4254 .rpc_call_prepare = nfs4_recover_lock_prepare,
4255 .rpc_call_done = nfs4_lock_done,
4256 .rpc_release = nfs4_lock_release,
4259 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4262 case -NFS4ERR_ADMIN_REVOKED:
4263 case -NFS4ERR_BAD_STATEID:
4264 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4265 if (new_lock_owner != 0 ||
4266 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4267 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4269 case -NFS4ERR_STALE_STATEID:
4270 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4271 case -NFS4ERR_EXPIRED:
4272 nfs4_schedule_lease_recovery(server->nfs_client);
4276 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4278 struct nfs4_lockdata *data;
4279 struct rpc_task *task;
4280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4282 .rpc_cred = state->owner->so_cred,
4284 struct rpc_task_setup task_setup_data = {
4285 .rpc_client = NFS_CLIENT(state->inode),
4286 .rpc_message = &msg,
4287 .callback_ops = &nfs4_lock_ops,
4288 .workqueue = nfsiod_workqueue,
4289 .flags = RPC_TASK_ASYNC,
4293 dprintk("%s: begin!\n", __func__);
4294 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4295 fl->fl_u.nfs4_fl.owner,
4296 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4300 data->arg.block = 1;
4301 if (recovery_type > NFS_LOCK_NEW) {
4302 if (recovery_type == NFS_LOCK_RECLAIM)
4303 data->arg.reclaim = NFS_LOCK_RECLAIM;
4304 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4306 msg.rpc_argp = &data->arg;
4307 msg.rpc_resp = &data->res;
4308 task_setup_data.callback_data = data;
4309 task = rpc_run_task(&task_setup_data);
4311 return PTR_ERR(task);
4312 ret = nfs4_wait_for_completion_rpc_task(task);
4314 ret = data->rpc_status;
4316 nfs4_handle_setlk_error(data->server, data->lsp,
4317 data->arg.new_lock_owner, ret);
4319 data->cancelled = 1;
4321 dprintk("%s: done, ret = %d!\n", __func__, ret);
4325 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4327 struct nfs_server *server = NFS_SERVER(state->inode);
4328 struct nfs4_exception exception = { };
4332 /* Cache the lock if possible... */
4333 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4335 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4336 if (err != -NFS4ERR_DELAY)
4338 nfs4_handle_exception(server, err, &exception);
4339 } while (exception.retry);
4343 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4345 struct nfs_server *server = NFS_SERVER(state->inode);
4346 struct nfs4_exception exception = { };
4349 err = nfs4_set_lock_state(state, request);
4353 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4355 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4359 case -NFS4ERR_GRACE:
4360 case -NFS4ERR_DELAY:
4361 nfs4_handle_exception(server, err, &exception);
4364 } while (exception.retry);
4369 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4371 struct nfs_inode *nfsi = NFS_I(state->inode);
4372 unsigned char fl_flags = request->fl_flags;
4373 int status = -ENOLCK;
4375 if ((fl_flags & FL_POSIX) &&
4376 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4378 /* Is this a delegated open? */
4379 status = nfs4_set_lock_state(state, request);
4382 request->fl_flags |= FL_ACCESS;
4383 status = do_vfs_lock(request->fl_file, request);
4386 down_read(&nfsi->rwsem);
4387 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4388 /* Yes: cache locks! */
4389 /* ...but avoid races with delegation recall... */
4390 request->fl_flags = fl_flags & ~FL_SLEEP;
4391 status = do_vfs_lock(request->fl_file, request);
4394 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4397 /* Note: we always want to sleep here! */
4398 request->fl_flags = fl_flags | FL_SLEEP;
4399 if (do_vfs_lock(request->fl_file, request) < 0)
4400 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4402 up_read(&nfsi->rwsem);
4404 request->fl_flags = fl_flags;
4408 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4410 struct nfs4_exception exception = { };
4414 err = _nfs4_proc_setlk(state, cmd, request);
4415 if (err == -NFS4ERR_DENIED)
4417 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4419 } while (exception.retry);
4424 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4426 struct nfs_open_context *ctx;
4427 struct nfs4_state *state;
4428 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4431 /* verify open state */
4432 ctx = nfs_file_open_context(filp);
4435 if (request->fl_start < 0 || request->fl_end < 0)
4438 if (IS_GETLK(cmd)) {
4440 return nfs4_proc_getlk(state, F_GETLK, request);
4444 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4447 if (request->fl_type == F_UNLCK) {
4449 return nfs4_proc_unlck(state, cmd, request);
4456 status = nfs4_proc_setlk(state, cmd, request);
4457 if ((status != -EAGAIN) || IS_SETLK(cmd))
4459 timeout = nfs4_set_lock_task_retry(timeout);
4460 status = -ERESTARTSYS;
4463 } while(status < 0);
4467 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4469 struct nfs_server *server = NFS_SERVER(state->inode);
4470 struct nfs4_exception exception = { };
4473 err = nfs4_set_lock_state(state, fl);
4477 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4480 printk(KERN_ERR "%s: unhandled error %d.\n",
4485 case -NFS4ERR_EXPIRED:
4486 case -NFS4ERR_STALE_CLIENTID:
4487 case -NFS4ERR_STALE_STATEID:
4488 nfs4_schedule_lease_recovery(server->nfs_client);
4490 case -NFS4ERR_BADSESSION:
4491 case -NFS4ERR_BADSLOT:
4492 case -NFS4ERR_BAD_HIGH_SLOT:
4493 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4494 case -NFS4ERR_DEADSESSION:
4495 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4499 * The show must go on: exit, but mark the
4500 * stateid as needing recovery.
4502 case -NFS4ERR_ADMIN_REVOKED:
4503 case -NFS4ERR_BAD_STATEID:
4504 case -NFS4ERR_OPENMODE:
4505 nfs4_schedule_stateid_recovery(server, state);
4510 * User RPCSEC_GSS context has expired.
4511 * We cannot recover this stateid now, so
4512 * skip it and allow recovery thread to
4518 case -NFS4ERR_DENIED:
4519 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4522 case -NFS4ERR_DELAY:
4525 err = nfs4_handle_exception(server, err, &exception);
4526 } while (exception.retry);
4531 static void nfs4_release_lockowner_release(void *calldata)
4536 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4537 .rpc_release = nfs4_release_lockowner_release,
4540 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4542 struct nfs_server *server = lsp->ls_state->owner->so_server;
4543 struct nfs_release_lockowner_args *args;
4544 struct rpc_message msg = {
4545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4548 if (server->nfs_client->cl_mvops->minor_version != 0)
4550 args = kmalloc(sizeof(*args), GFP_NOFS);
4553 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4554 args->lock_owner.id = lsp->ls_id.id;
4555 args->lock_owner.s_dev = server->s_dev;
4556 msg.rpc_argp = args;
4557 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4560 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4562 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4563 const void *buf, size_t buflen,
4564 int flags, int type)
4566 if (strcmp(key, "") != 0)
4569 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4572 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4573 void *buf, size_t buflen, int type)
4575 if (strcmp(key, "") != 0)
4578 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4581 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4582 size_t list_len, const char *name,
4583 size_t name_len, int type)
4585 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4587 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4590 if (list && len <= list_len)
4591 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4595 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4597 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4598 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4599 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4602 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4603 NFS_ATTR_FATTR_NLINK;
4604 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4608 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4609 struct nfs4_fs_locations *fs_locations, struct page *page)
4611 struct nfs_server *server = NFS_SERVER(dir);
4613 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4614 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4616 struct nfs4_fs_locations_arg args = {
4617 .dir_fh = NFS_FH(dir),
4622 struct nfs4_fs_locations_res res = {
4623 .fs_locations = fs_locations,
4625 struct rpc_message msg = {
4626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4632 dprintk("%s: start\n", __func__);
4633 nfs_fattr_init(&fs_locations->fattr);
4634 fs_locations->server = server;
4635 fs_locations->nlocations = 0;
4636 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4637 nfs_fixup_referral_attributes(&fs_locations->fattr);
4638 dprintk("%s: returned status = %d\n", __func__, status);
4642 #ifdef CONFIG_NFS_V4_1
4644 * Check the exchange flags returned by the server for invalid flags, having
4645 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4648 static int nfs4_check_cl_exchange_flags(u32 flags)
4650 if (flags & ~EXCHGID4_FLAG_MASK_R)
4652 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4653 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4655 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4659 return -NFS4ERR_INVAL;
4663 * nfs4_proc_exchange_id()
4665 * Since the clientid has expired, all compounds using sessions
4666 * associated with the stale clientid will be returning
4667 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4668 * be in some phase of session reset.
4670 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4672 nfs4_verifier verifier;
4673 struct nfs41_exchange_id_args args = {
4675 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4677 struct nfs41_exchange_id_res res = {
4681 struct rpc_message msg = {
4682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4689 dprintk("--> %s\n", __func__);
4690 BUG_ON(clp == NULL);
4692 p = (u32 *)verifier.data;
4693 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4694 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4695 args.verifier = &verifier;
4697 args.id_len = scnprintf(args.id, sizeof(args.id),
4700 init_utsname()->nodename,
4701 init_utsname()->domainname,
4702 clp->cl_rpcclient->cl_auth->au_flavor);
4704 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4706 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4707 dprintk("<-- %s status= %d\n", __func__, status);
4711 struct nfs4_get_lease_time_data {
4712 struct nfs4_get_lease_time_args *args;
4713 struct nfs4_get_lease_time_res *res;
4714 struct nfs_client *clp;
4717 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4721 struct nfs4_get_lease_time_data *data =
4722 (struct nfs4_get_lease_time_data *)calldata;
4724 dprintk("--> %s\n", __func__);
4725 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4726 /* just setup sequence, do not trigger session recovery
4727 since we're invoked within one */
4728 ret = nfs41_setup_sequence(data->clp->cl_session,
4729 &data->args->la_seq_args,
4730 &data->res->lr_seq_res, 0, task);
4732 BUG_ON(ret == -EAGAIN);
4733 rpc_call_start(task);
4734 dprintk("<-- %s\n", __func__);
4738 * Called from nfs4_state_manager thread for session setup, so don't recover
4739 * from sequence operation or clientid errors.
4741 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4743 struct nfs4_get_lease_time_data *data =
4744 (struct nfs4_get_lease_time_data *)calldata;
4746 dprintk("--> %s\n", __func__);
4747 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4749 switch (task->tk_status) {
4750 case -NFS4ERR_DELAY:
4751 case -NFS4ERR_GRACE:
4752 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4753 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4754 task->tk_status = 0;
4755 nfs_restart_rpc(task, data->clp);
4758 dprintk("<-- %s\n", __func__);
4761 struct rpc_call_ops nfs4_get_lease_time_ops = {
4762 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4763 .rpc_call_done = nfs4_get_lease_time_done,
4766 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4768 struct rpc_task *task;
4769 struct nfs4_get_lease_time_args args;
4770 struct nfs4_get_lease_time_res res = {
4771 .lr_fsinfo = fsinfo,
4773 struct nfs4_get_lease_time_data data = {
4778 struct rpc_message msg = {
4779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4783 struct rpc_task_setup task_setup = {
4784 .rpc_client = clp->cl_rpcclient,
4785 .rpc_message = &msg,
4786 .callback_ops = &nfs4_get_lease_time_ops,
4787 .callback_data = &data
4791 dprintk("--> %s\n", __func__);
4792 task = rpc_run_task(&task_setup);
4795 status = PTR_ERR(task);
4797 status = task->tk_status;
4800 dprintk("<-- %s return %d\n", __func__, status);
4806 * Reset a slot table
4808 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4811 struct nfs4_slot *new = NULL;
4815 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4816 max_reqs, tbl->max_slots);
4818 /* Does the newly negotiated max_reqs match the existing slot table? */
4819 if (max_reqs != tbl->max_slots) {
4821 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4828 spin_lock(&tbl->slot_tbl_lock);
4831 tbl->max_slots = max_reqs;
4833 for (i = 0; i < tbl->max_slots; ++i)
4834 tbl->slots[i].seq_nr = ivalue;
4835 spin_unlock(&tbl->slot_tbl_lock);
4836 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4837 tbl, tbl->slots, tbl->max_slots);
4839 dprintk("<-- %s: return %d\n", __func__, ret);
4844 * Reset the forechannel and backchannel slot tables
4846 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4850 status = nfs4_reset_slot_table(&session->fc_slot_table,
4851 session->fc_attrs.max_reqs, 1);
4855 status = nfs4_reset_slot_table(&session->bc_slot_table,
4856 session->bc_attrs.max_reqs, 0);
4860 /* Destroy the slot table */
4861 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4863 if (session->fc_slot_table.slots != NULL) {
4864 kfree(session->fc_slot_table.slots);
4865 session->fc_slot_table.slots = NULL;
4867 if (session->bc_slot_table.slots != NULL) {
4868 kfree(session->bc_slot_table.slots);
4869 session->bc_slot_table.slots = NULL;
4875 * Initialize slot table
4877 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4878 int max_slots, int ivalue)
4880 struct nfs4_slot *slot;
4883 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4885 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4887 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4892 spin_lock(&tbl->slot_tbl_lock);
4893 tbl->max_slots = max_slots;
4895 tbl->highest_used_slotid = -1; /* no slot is currently used */
4896 spin_unlock(&tbl->slot_tbl_lock);
4897 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4898 tbl, tbl->slots, tbl->max_slots);
4900 dprintk("<-- %s: return %d\n", __func__, ret);
4905 * Initialize the forechannel and backchannel tables
4907 static int nfs4_init_slot_tables(struct nfs4_session *session)
4909 struct nfs4_slot_table *tbl;
4912 tbl = &session->fc_slot_table;
4913 if (tbl->slots == NULL) {
4914 status = nfs4_init_slot_table(tbl,
4915 session->fc_attrs.max_reqs, 1);
4920 tbl = &session->bc_slot_table;
4921 if (tbl->slots == NULL) {
4922 status = nfs4_init_slot_table(tbl,
4923 session->bc_attrs.max_reqs, 0);
4925 nfs4_destroy_slot_tables(session);
4931 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4933 struct nfs4_session *session;
4934 struct nfs4_slot_table *tbl;
4936 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
4940 tbl = &session->fc_slot_table;
4941 tbl->highest_used_slotid = -1;
4942 spin_lock_init(&tbl->slot_tbl_lock);
4943 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4944 init_completion(&tbl->complete);
4946 tbl = &session->bc_slot_table;
4947 tbl->highest_used_slotid = -1;
4948 spin_lock_init(&tbl->slot_tbl_lock);
4949 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4950 init_completion(&tbl->complete);
4952 session->session_state = 1<<NFS4_SESSION_INITING;
4958 void nfs4_destroy_session(struct nfs4_session *session)
4960 nfs4_proc_destroy_session(session);
4961 dprintk("%s Destroy backchannel for xprt %p\n",
4962 __func__, session->clp->cl_rpcclient->cl_xprt);
4963 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4964 NFS41_BC_MIN_CALLBACKS);
4965 nfs4_destroy_slot_tables(session);
4970 * Initialize the values to be used by the client in CREATE_SESSION
4971 * If nfs4_init_session set the fore channel request and response sizes,
4974 * Set the back channel max_resp_sz_cached to zero to force the client to
4975 * always set csa_cachethis to FALSE because the current implementation
4976 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4978 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4980 struct nfs4_session *session = args->client->cl_session;
4981 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4982 mxresp_sz = session->fc_attrs.max_resp_sz;
4985 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4987 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4988 /* Fore channel attributes */
4989 args->fc_attrs.headerpadsz = 0;
4990 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4991 args->fc_attrs.max_resp_sz = mxresp_sz;
4992 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4993 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4995 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4996 "max_ops=%u max_reqs=%u\n",
4998 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4999 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5001 /* Back channel attributes */
5002 args->bc_attrs.headerpadsz = 0;
5003 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5004 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5005 args->bc_attrs.max_resp_sz_cached = 0;
5006 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5007 args->bc_attrs.max_reqs = 1;
5009 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5010 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5012 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5013 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5014 args->bc_attrs.max_reqs);
5017 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5019 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5020 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5022 if (rcvd->headerpadsz > sent->headerpadsz)
5024 if (rcvd->max_resp_sz > sent->max_resp_sz)
5027 * Our requested max_ops is the minimum we need; we're not
5028 * prepared to break up compounds into smaller pieces than that.
5029 * So, no point even trying to continue if the server won't
5032 if (rcvd->max_ops < sent->max_ops)
5034 if (rcvd->max_reqs == 0)
5039 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5041 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5042 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5044 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5046 if (rcvd->max_resp_sz < sent->max_resp_sz)
5048 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5050 /* These would render the backchannel useless: */
5051 if (rcvd->max_ops == 0)
5053 if (rcvd->max_reqs == 0)
5058 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5059 struct nfs4_session *session)
5063 ret = nfs4_verify_fore_channel_attrs(args, session);
5066 return nfs4_verify_back_channel_attrs(args, session);
5069 static int _nfs4_proc_create_session(struct nfs_client *clp)
5071 struct nfs4_session *session = clp->cl_session;
5072 struct nfs41_create_session_args args = {
5074 .cb_program = NFS4_CALLBACK,
5076 struct nfs41_create_session_res res = {
5079 struct rpc_message msg = {
5080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5086 nfs4_init_channel_attrs(&args);
5087 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5089 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5092 /* Verify the session's negotiated channel_attrs values */
5093 status = nfs4_verify_channel_attrs(&args, session);
5095 /* Increment the clientid slot sequence id */
5103 * Issues a CREATE_SESSION operation to the server.
5104 * It is the responsibility of the caller to verify the session is
5105 * expired before calling this routine.
5107 int nfs4_proc_create_session(struct nfs_client *clp)
5111 struct nfs4_session *session = clp->cl_session;
5115 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5118 status = _nfs4_proc_create_session(clp);
5119 if (status == -NFS4ERR_DELAY) {
5120 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5124 } while (status == -NFS4ERR_DELAY);
5129 /* Init and reset the fore channel */
5130 status = nfs4_init_slot_tables(session);
5131 dprintk("slot table initialization returned %d\n", status);
5134 status = nfs4_reset_slot_tables(session);
5135 dprintk("slot table reset returned %d\n", status);
5139 ptr = (unsigned *)&session->sess_id.data[0];
5140 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5141 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5143 dprintk("<-- %s\n", __func__);
5148 * Issue the over-the-wire RPC DESTROY_SESSION.
5149 * The caller must serialize access to this routine.
5151 int nfs4_proc_destroy_session(struct nfs4_session *session)
5154 struct rpc_message msg;
5156 dprintk("--> nfs4_proc_destroy_session\n");
5158 /* session is still being setup */
5159 if (session->clp->cl_cons_state != NFS_CS_READY)
5162 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5163 msg.rpc_argp = session;
5164 msg.rpc_resp = NULL;
5165 msg.rpc_cred = NULL;
5166 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5170 "Got error %d from the server on DESTROY_SESSION. "
5171 "Session has been destroyed regardless...\n", status);
5173 dprintk("<-- nfs4_proc_destroy_session\n");
5177 int nfs4_init_session(struct nfs_server *server)
5179 struct nfs_client *clp = server->nfs_client;
5180 struct nfs4_session *session;
5181 unsigned int rsize, wsize;
5184 if (!nfs4_has_session(clp))
5187 session = clp->cl_session;
5188 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5191 rsize = server->rsize;
5193 rsize = NFS_MAX_FILE_IO_SIZE;
5194 wsize = server->wsize;
5196 wsize = NFS_MAX_FILE_IO_SIZE;
5198 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5199 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5201 ret = nfs4_recover_expired_lease(server);
5203 ret = nfs4_check_client_ready(clp);
5207 int nfs4_init_ds_session(struct nfs_client *clp)
5209 struct nfs4_session *session = clp->cl_session;
5212 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5215 ret = nfs4_client_recover_expired_lease(clp);
5217 /* Test for the DS role */
5218 if (!is_ds_client(clp))
5221 ret = nfs4_check_client_ready(clp);
5225 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5229 * Renew the cl_session lease.
5231 struct nfs4_sequence_data {
5232 struct nfs_client *clp;
5233 struct nfs4_sequence_args args;
5234 struct nfs4_sequence_res res;
5237 static void nfs41_sequence_release(void *data)
5239 struct nfs4_sequence_data *calldata = data;
5240 struct nfs_client *clp = calldata->clp;
5242 if (atomic_read(&clp->cl_count) > 1)
5243 nfs4_schedule_state_renewal(clp);
5244 nfs_put_client(clp);
5248 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5250 switch(task->tk_status) {
5251 case -NFS4ERR_DELAY:
5252 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5255 nfs4_schedule_lease_recovery(clp);
5260 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5262 struct nfs4_sequence_data *calldata = data;
5263 struct nfs_client *clp = calldata->clp;
5265 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5268 if (task->tk_status < 0) {
5269 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5270 if (atomic_read(&clp->cl_count) == 1)
5273 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5274 rpc_restart_call_prepare(task);
5278 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5280 dprintk("<-- %s\n", __func__);
5283 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5285 struct nfs4_sequence_data *calldata = data;
5286 struct nfs_client *clp = calldata->clp;
5287 struct nfs4_sequence_args *args;
5288 struct nfs4_sequence_res *res;
5290 args = task->tk_msg.rpc_argp;
5291 res = task->tk_msg.rpc_resp;
5293 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5295 rpc_call_start(task);
5298 static const struct rpc_call_ops nfs41_sequence_ops = {
5299 .rpc_call_done = nfs41_sequence_call_done,
5300 .rpc_call_prepare = nfs41_sequence_prepare,
5301 .rpc_release = nfs41_sequence_release,
5304 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5306 struct nfs4_sequence_data *calldata;
5307 struct rpc_message msg = {
5308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5311 struct rpc_task_setup task_setup_data = {
5312 .rpc_client = clp->cl_rpcclient,
5313 .rpc_message = &msg,
5314 .callback_ops = &nfs41_sequence_ops,
5315 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5318 if (!atomic_inc_not_zero(&clp->cl_count))
5319 return ERR_PTR(-EIO);
5320 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5321 if (calldata == NULL) {
5322 nfs_put_client(clp);
5323 return ERR_PTR(-ENOMEM);
5325 msg.rpc_argp = &calldata->args;
5326 msg.rpc_resp = &calldata->res;
5327 calldata->clp = clp;
5328 task_setup_data.callback_data = calldata;
5330 return rpc_run_task(&task_setup_data);
5333 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5335 struct rpc_task *task;
5338 task = _nfs41_proc_sequence(clp, cred);
5340 ret = PTR_ERR(task);
5342 rpc_put_task_async(task);
5343 dprintk("<-- %s status=%d\n", __func__, ret);
5347 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5349 struct rpc_task *task;
5352 task = _nfs41_proc_sequence(clp, cred);
5354 ret = PTR_ERR(task);
5357 ret = rpc_wait_for_completion_task(task);
5359 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5361 if (task->tk_status == 0)
5362 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5363 ret = task->tk_status;
5367 dprintk("<-- %s status=%d\n", __func__, ret);
5371 struct nfs4_reclaim_complete_data {
5372 struct nfs_client *clp;
5373 struct nfs41_reclaim_complete_args arg;
5374 struct nfs41_reclaim_complete_res res;
5377 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5379 struct nfs4_reclaim_complete_data *calldata = data;
5381 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5382 if (nfs41_setup_sequence(calldata->clp->cl_session,
5383 &calldata->arg.seq_args,
5384 &calldata->res.seq_res, 0, task))
5387 rpc_call_start(task);
5390 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5392 switch(task->tk_status) {
5394 case -NFS4ERR_COMPLETE_ALREADY:
5395 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5397 case -NFS4ERR_DELAY:
5398 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5401 nfs4_schedule_lease_recovery(clp);
5406 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5408 struct nfs4_reclaim_complete_data *calldata = data;
5409 struct nfs_client *clp = calldata->clp;
5410 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5412 dprintk("--> %s\n", __func__);
5413 if (!nfs41_sequence_done(task, res))
5416 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5417 rpc_restart_call_prepare(task);
5420 dprintk("<-- %s\n", __func__);
5423 static void nfs4_free_reclaim_complete_data(void *data)
5425 struct nfs4_reclaim_complete_data *calldata = data;
5430 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5431 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5432 .rpc_call_done = nfs4_reclaim_complete_done,
5433 .rpc_release = nfs4_free_reclaim_complete_data,
5437 * Issue a global reclaim complete.
5439 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5441 struct nfs4_reclaim_complete_data *calldata;
5442 struct rpc_task *task;
5443 struct rpc_message msg = {
5444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5446 struct rpc_task_setup task_setup_data = {
5447 .rpc_client = clp->cl_rpcclient,
5448 .rpc_message = &msg,
5449 .callback_ops = &nfs4_reclaim_complete_call_ops,
5450 .flags = RPC_TASK_ASYNC,
5452 int status = -ENOMEM;
5454 dprintk("--> %s\n", __func__);
5455 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5456 if (calldata == NULL)
5458 calldata->clp = clp;
5459 calldata->arg.one_fs = 0;
5461 msg.rpc_argp = &calldata->arg;
5462 msg.rpc_resp = &calldata->res;
5463 task_setup_data.callback_data = calldata;
5464 task = rpc_run_task(&task_setup_data);
5466 status = PTR_ERR(task);
5469 status = nfs4_wait_for_completion_rpc_task(task);
5471 status = task->tk_status;
5475 dprintk("<-- %s status=%d\n", __func__, status);
5480 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5482 struct nfs4_layoutget *lgp = calldata;
5483 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5485 dprintk("--> %s\n", __func__);
5486 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5487 * right now covering the LAYOUTGET we are about to send.
5488 * However, that is not so catastrophic, and there seems
5489 * to be no way to prevent it completely.
5491 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5492 &lgp->res.seq_res, 0, task))
5494 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5495 NFS_I(lgp->args.inode)->layout,
5496 lgp->args.ctx->state)) {
5497 rpc_exit(task, NFS4_OK);
5500 rpc_call_start(task);
5503 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5505 struct nfs4_layoutget *lgp = calldata;
5506 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5508 dprintk("--> %s\n", __func__);
5510 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5513 switch (task->tk_status) {
5516 case -NFS4ERR_LAYOUTTRYLATER:
5517 case -NFS4ERR_RECALLCONFLICT:
5518 task->tk_status = -NFS4ERR_DELAY;
5521 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5522 rpc_restart_call_prepare(task);
5526 dprintk("<-- %s\n", __func__);
5529 static void nfs4_layoutget_release(void *calldata)
5531 struct nfs4_layoutget *lgp = calldata;
5533 dprintk("--> %s\n", __func__);
5534 if (lgp->res.layout.buf != NULL)
5535 free_page((unsigned long) lgp->res.layout.buf);
5536 put_nfs_open_context(lgp->args.ctx);
5538 dprintk("<-- %s\n", __func__);
5541 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5542 .rpc_call_prepare = nfs4_layoutget_prepare,
5543 .rpc_call_done = nfs4_layoutget_done,
5544 .rpc_release = nfs4_layoutget_release,
5547 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5549 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5550 struct rpc_task *task;
5551 struct rpc_message msg = {
5552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5553 .rpc_argp = &lgp->args,
5554 .rpc_resp = &lgp->res,
5556 struct rpc_task_setup task_setup_data = {
5557 .rpc_client = server->client,
5558 .rpc_message = &msg,
5559 .callback_ops = &nfs4_layoutget_call_ops,
5560 .callback_data = lgp,
5561 .flags = RPC_TASK_ASYNC,
5565 dprintk("--> %s\n", __func__);
5567 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5568 if (lgp->res.layout.buf == NULL) {
5569 nfs4_layoutget_release(lgp);
5573 lgp->res.seq_res.sr_slot = NULL;
5574 task = rpc_run_task(&task_setup_data);
5576 return PTR_ERR(task);
5577 status = nfs4_wait_for_completion_rpc_task(task);
5579 status = task->tk_status;
5581 status = pnfs_layout_process(lgp);
5583 dprintk("<-- %s status=%d\n", __func__, status);
5588 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5590 struct nfs4_getdeviceinfo_args args = {
5593 struct nfs4_getdeviceinfo_res res = {
5596 struct rpc_message msg = {
5597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5603 dprintk("--> %s\n", __func__);
5604 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5605 dprintk("<-- %s status=%d\n", __func__, status);
5610 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5612 struct nfs4_exception exception = { };
5616 err = nfs4_handle_exception(server,
5617 _nfs4_proc_getdeviceinfo(server, pdev),
5619 } while (exception.retry);
5622 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5624 #endif /* CONFIG_NFS_V4_1 */
5626 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5627 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5628 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5629 .recover_open = nfs4_open_reclaim,
5630 .recover_lock = nfs4_lock_reclaim,
5631 .establish_clid = nfs4_init_clientid,
5632 .get_clid_cred = nfs4_get_setclientid_cred,
5635 #if defined(CONFIG_NFS_V4_1)
5636 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5637 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5638 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5639 .recover_open = nfs4_open_reclaim,
5640 .recover_lock = nfs4_lock_reclaim,
5641 .establish_clid = nfs41_init_clientid,
5642 .get_clid_cred = nfs4_get_exchange_id_cred,
5643 .reclaim_complete = nfs41_proc_reclaim_complete,
5645 #endif /* CONFIG_NFS_V4_1 */
5647 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5648 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5649 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5650 .recover_open = nfs4_open_expired,
5651 .recover_lock = nfs4_lock_expired,
5652 .establish_clid = nfs4_init_clientid,
5653 .get_clid_cred = nfs4_get_setclientid_cred,
5656 #if defined(CONFIG_NFS_V4_1)
5657 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5658 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5659 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5660 .recover_open = nfs4_open_expired,
5661 .recover_lock = nfs4_lock_expired,
5662 .establish_clid = nfs41_init_clientid,
5663 .get_clid_cred = nfs4_get_exchange_id_cred,
5665 #endif /* CONFIG_NFS_V4_1 */
5667 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5668 .sched_state_renewal = nfs4_proc_async_renew,
5669 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5670 .renew_lease = nfs4_proc_renew,
5673 #if defined(CONFIG_NFS_V4_1)
5674 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5675 .sched_state_renewal = nfs41_proc_async_sequence,
5676 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5677 .renew_lease = nfs4_proc_sequence,
5681 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5683 .call_sync = _nfs4_call_sync,
5684 .validate_stateid = nfs4_validate_delegation_stateid,
5685 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5686 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5687 .state_renewal_ops = &nfs40_state_renewal_ops,
5690 #if defined(CONFIG_NFS_V4_1)
5691 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5693 .call_sync = _nfs4_call_sync_session,
5694 .validate_stateid = nfs41_validate_delegation_stateid,
5695 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5696 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5697 .state_renewal_ops = &nfs41_state_renewal_ops,
5701 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5702 [0] = &nfs_v4_0_minor_ops,
5703 #if defined(CONFIG_NFS_V4_1)
5704 [1] = &nfs_v4_1_minor_ops,
5708 static const struct inode_operations nfs4_file_inode_operations = {
5709 .permission = nfs_permission,
5710 .getattr = nfs_getattr,
5711 .setattr = nfs_setattr,
5712 .getxattr = generic_getxattr,
5713 .setxattr = generic_setxattr,
5714 .listxattr = generic_listxattr,
5715 .removexattr = generic_removexattr,
5718 const struct nfs_rpc_ops nfs_v4_clientops = {
5719 .version = 4, /* protocol version */
5720 .dentry_ops = &nfs4_dentry_operations,
5721 .dir_inode_ops = &nfs4_dir_inode_operations,
5722 .file_inode_ops = &nfs4_file_inode_operations,
5723 .getroot = nfs4_proc_get_root,
5724 .getattr = nfs4_proc_getattr,
5725 .setattr = nfs4_proc_setattr,
5726 .lookupfh = nfs4_proc_lookupfh,
5727 .lookup = nfs4_proc_lookup,
5728 .access = nfs4_proc_access,
5729 .readlink = nfs4_proc_readlink,
5730 .create = nfs4_proc_create,
5731 .remove = nfs4_proc_remove,
5732 .unlink_setup = nfs4_proc_unlink_setup,
5733 .unlink_done = nfs4_proc_unlink_done,
5734 .rename = nfs4_proc_rename,
5735 .rename_setup = nfs4_proc_rename_setup,
5736 .rename_done = nfs4_proc_rename_done,
5737 .link = nfs4_proc_link,
5738 .symlink = nfs4_proc_symlink,
5739 .mkdir = nfs4_proc_mkdir,
5740 .rmdir = nfs4_proc_remove,
5741 .readdir = nfs4_proc_readdir,
5742 .mknod = nfs4_proc_mknod,
5743 .statfs = nfs4_proc_statfs,
5744 .fsinfo = nfs4_proc_fsinfo,
5745 .pathconf = nfs4_proc_pathconf,
5746 .set_capabilities = nfs4_server_capabilities,
5747 .decode_dirent = nfs4_decode_dirent,
5748 .read_setup = nfs4_proc_read_setup,
5749 .read_done = nfs4_read_done,
5750 .write_setup = nfs4_proc_write_setup,
5751 .write_done = nfs4_write_done,
5752 .commit_setup = nfs4_proc_commit_setup,
5753 .commit_done = nfs4_commit_done,
5754 .lock = nfs4_proc_lock,
5755 .clear_acl_cache = nfs4_zap_acl_attr,
5756 .close_context = nfs4_close_context,
5757 .open_context = nfs4_atomic_open,
5758 .init_client = nfs4_init_client,
5761 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5762 .prefix = XATTR_NAME_NFSV4_ACL,
5763 .list = nfs4_xattr_list_nfs4_acl,
5764 .get = nfs4_xattr_get_nfs4_acl,
5765 .set = nfs4_xattr_set_nfs4_acl,
5768 const struct xattr_handler *nfs4_xattr_handlers[] = {
5769 &nfs4_xattr_nfs4_acl_handler,