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>
55 #include "delegation.h"
61 #define NFSDBG_FACILITY NFSDBG_PROC
63 #define NFS4_POLL_RETRY_MIN (HZ/10)
64 #define NFS4_POLL_RETRY_MAX (15*HZ)
66 #define NFS4_MAX_LOOP_ON_RECOVER (10)
69 static int _nfs4_proc_open(struct nfs4_opendata *data);
70 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
71 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
72 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
73 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
74 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
75 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
76 struct nfs_fattr *fattr, struct iattr *sattr,
77 struct nfs4_state *state);
79 /* Prevent leaks of NFSv4 errors into userland */
80 static int nfs4_map_errors(int err)
85 case -NFS4ERR_RESOURCE:
88 dprintk("%s could not handle NFSv4 error %d\n",
96 * This is our standard bitmap for GETATTR requests.
98 const u32 nfs4_fattr_bitmap[2] = {
100 | FATTR4_WORD0_CHANGE
103 | FATTR4_WORD0_FILEID,
105 | FATTR4_WORD1_NUMLINKS
107 | FATTR4_WORD1_OWNER_GROUP
108 | FATTR4_WORD1_RAWDEV
109 | FATTR4_WORD1_SPACE_USED
110 | FATTR4_WORD1_TIME_ACCESS
111 | FATTR4_WORD1_TIME_METADATA
112 | FATTR4_WORD1_TIME_MODIFY
115 const u32 nfs4_statfs_bitmap[2] = {
116 FATTR4_WORD0_FILES_AVAIL
117 | FATTR4_WORD0_FILES_FREE
118 | FATTR4_WORD0_FILES_TOTAL,
119 FATTR4_WORD1_SPACE_AVAIL
120 | FATTR4_WORD1_SPACE_FREE
121 | FATTR4_WORD1_SPACE_TOTAL
124 const u32 nfs4_pathconf_bitmap[2] = {
126 | FATTR4_WORD0_MAXNAME,
130 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
131 | FATTR4_WORD0_MAXREAD
132 | FATTR4_WORD0_MAXWRITE
133 | FATTR4_WORD0_LEASE_TIME,
134 FATTR4_WORD1_TIME_DELTA
135 | FATTR4_WORD1_FS_LAYOUT_TYPES
138 const u32 nfs4_fs_locations_bitmap[2] = {
140 | FATTR4_WORD0_CHANGE
143 | FATTR4_WORD0_FILEID
144 | FATTR4_WORD0_FS_LOCATIONS,
146 | FATTR4_WORD1_NUMLINKS
148 | FATTR4_WORD1_OWNER_GROUP
149 | FATTR4_WORD1_RAWDEV
150 | FATTR4_WORD1_SPACE_USED
151 | FATTR4_WORD1_TIME_ACCESS
152 | FATTR4_WORD1_TIME_METADATA
153 | FATTR4_WORD1_TIME_MODIFY
154 | FATTR4_WORD1_MOUNTED_ON_FILEID
157 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
158 struct nfs4_readdir_arg *readdir)
162 BUG_ON(readdir->count < 80);
164 readdir->cookie = cookie;
165 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
170 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
175 * NFSv4 servers do not return entries for '.' and '..'
176 * Therefore, we fake these entries here. We let '.'
177 * have cookie 0 and '..' have cookie 1. Note that
178 * when talking to the server, we always send cookie 0
181 start = p = kmap_atomic(*readdir->pages, KM_USER0);
184 *p++ = xdr_one; /* next */
185 *p++ = xdr_zero; /* cookie, first word */
186 *p++ = xdr_one; /* cookie, second word */
187 *p++ = xdr_one; /* entry len */
188 memcpy(p, ".\0\0\0", 4); /* entry */
190 *p++ = xdr_one; /* bitmap length */
191 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
192 *p++ = htonl(8); /* attribute buffer length */
193 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
196 *p++ = xdr_one; /* next */
197 *p++ = xdr_zero; /* cookie, first word */
198 *p++ = xdr_two; /* cookie, second word */
199 *p++ = xdr_two; /* entry len */
200 memcpy(p, "..\0\0", 4); /* entry */
202 *p++ = xdr_one; /* bitmap length */
203 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
204 *p++ = htonl(8); /* attribute buffer length */
205 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
207 readdir->pgbase = (char *)p - (char *)start;
208 readdir->count -= readdir->pgbase;
209 kunmap_atomic(start, KM_USER0);
212 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
218 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
219 nfs_wait_bit_killable, TASK_KILLABLE);
223 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
230 *timeout = NFS4_POLL_RETRY_MIN;
231 if (*timeout > NFS4_POLL_RETRY_MAX)
232 *timeout = NFS4_POLL_RETRY_MAX;
233 schedule_timeout_killable(*timeout);
234 if (fatal_signal_pending(current))
240 /* This is the error handling routine for processes that are allowed
243 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
245 struct nfs_client *clp = server->nfs_client;
246 struct nfs4_state *state = exception->state;
249 exception->retry = 0;
253 case -NFS4ERR_ADMIN_REVOKED:
254 case -NFS4ERR_BAD_STATEID:
255 case -NFS4ERR_OPENMODE:
258 nfs4_state_mark_reclaim_nograce(clp, state);
259 goto do_state_recovery;
260 case -NFS4ERR_STALE_STATEID:
261 case -NFS4ERR_STALE_CLIENTID:
262 case -NFS4ERR_EXPIRED:
263 goto do_state_recovery;
264 #if defined(CONFIG_NFS_V4_1)
265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
274 nfs4_schedule_state_recovery(clp);
275 exception->retry = 1;
277 #endif /* defined(CONFIG_NFS_V4_1) */
278 case -NFS4ERR_FILE_OPEN:
279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
289 ret = nfs4_delay(server->client, &exception->timeout);
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
301 exception->retry = 1;
306 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
308 spin_lock(&clp->cl_lock);
309 if (time_before(clp->cl_last_renewal,timestamp))
310 clp->cl_last_renewal = timestamp;
311 spin_unlock(&clp->cl_lock);
314 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
316 do_renew_lease(server->nfs_client, timestamp);
319 #if defined(CONFIG_NFS_V4_1)
322 * nfs4_free_slot - free a slot and efficiently update slot table.
324 * freeing a slot is trivially done by clearing its respective bit
326 * If the freed slotid equals highest_used_slotid we want to update it
327 * so that the server would be able to size down the slot table if needed,
328 * otherwise we know that the highest_used_slotid is still in use.
329 * When updating highest_used_slotid there may be "holes" in the bitmap
330 * so we need to scan down from highest_used_slotid to 0 looking for the now
331 * highest slotid in use.
332 * If none found, highest_used_slotid is set to -1.
334 * Must be called while holding tbl->slot_tbl_lock
337 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
339 int free_slotid = free_slot - tbl->slots;
340 int slotid = free_slotid;
342 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
343 /* clear used bit in bitmap */
344 __clear_bit(slotid, tbl->used_slots);
346 /* update highest_used_slotid when it is freed */
347 if (slotid == tbl->highest_used_slotid) {
348 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
349 if (slotid < tbl->max_slots)
350 tbl->highest_used_slotid = slotid;
352 tbl->highest_used_slotid = -1;
354 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
355 free_slotid, tbl->highest_used_slotid);
359 * Signal state manager thread if session is drained
361 static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
363 struct rpc_task *task;
365 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
366 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
368 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
372 if (ses->fc_slot_table.highest_used_slotid != -1)
375 dprintk("%s COMPLETE: Session Drained\n", __func__);
376 complete(&ses->complete);
379 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
381 struct nfs4_slot_table *tbl;
383 tbl = &res->sr_session->fc_slot_table;
385 /* just wake up the next guy waiting since
386 * we may have not consumed a slot after all */
387 dprintk("%s: No slot\n", __func__);
391 spin_lock(&tbl->slot_tbl_lock);
392 nfs4_free_slot(tbl, res->sr_slot);
393 nfs41_check_drain_session_complete(res->sr_session);
394 spin_unlock(&tbl->slot_tbl_lock);
398 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
400 unsigned long timestamp;
401 struct nfs_client *clp;
404 * sr_status remains 1 if an RPC level error occurred. The server
405 * may or may not have processed the sequence operation..
406 * Proceed as if the server received and processed the sequence
409 if (res->sr_status == 1)
410 res->sr_status = NFS_OK;
412 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
416 /* Check the SEQUENCE operation status */
417 switch (res->sr_status) {
419 /* Update the slot's sequence and clientid lease timer */
420 ++res->sr_slot->seq_nr;
421 timestamp = res->sr_renewal_time;
422 clp = res->sr_session->clp;
423 do_renew_lease(clp, timestamp);
424 /* Check sequence flags */
425 if (atomic_read(&clp->cl_count) > 1)
426 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
429 /* The server detected a resend of the RPC call and
430 * returned NFS4ERR_DELAY as per Section 2.10.6.2
433 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
435 res->sr_slot - res->sr_session->fc_slot_table.slots,
436 res->sr_slot->seq_nr);
439 /* Just update the slot sequence no. */
440 ++res->sr_slot->seq_nr;
443 /* The session may be reset by one of the error handlers. */
444 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
445 nfs41_sequence_free_slot(res);
448 if (!rpc_restart_call(task))
450 rpc_delay(task, NFS4_POLL_RETRY_MAX);
454 static int nfs4_sequence_done(struct rpc_task *task,
455 struct nfs4_sequence_res *res)
457 if (res->sr_session == NULL)
459 return nfs41_sequence_done(task, res);
463 * nfs4_find_slot - efficiently look for a free slot
465 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
466 * If found, we mark the slot as used, update the highest_used_slotid,
467 * and respectively set up the sequence operation args.
468 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
470 * Note: must be called with under the slot_tbl_lock.
473 nfs4_find_slot(struct nfs4_slot_table *tbl)
476 u8 ret_id = NFS4_MAX_SLOT_TABLE;
477 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
479 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
480 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
482 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
483 if (slotid >= tbl->max_slots)
485 __set_bit(slotid, tbl->used_slots);
486 if (slotid > tbl->highest_used_slotid)
487 tbl->highest_used_slotid = slotid;
490 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
491 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
495 static int nfs41_setup_sequence(struct nfs4_session *session,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
499 struct rpc_task *task)
501 struct nfs4_slot *slot;
502 struct nfs4_slot_table *tbl;
505 dprintk("--> %s\n", __func__);
506 /* slot already allocated? */
507 if (res->sr_slot != NULL)
510 tbl = &session->fc_slot_table;
512 spin_lock(&tbl->slot_tbl_lock);
513 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
514 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
516 * The state manager will wait until the slot table is empty.
517 * Schedule the reset thread
519 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
520 spin_unlock(&tbl->slot_tbl_lock);
521 dprintk("%s Schedule Session Reset\n", __func__);
525 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
526 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
527 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
528 spin_unlock(&tbl->slot_tbl_lock);
529 dprintk("%s enforce FIFO order\n", __func__);
533 slotid = nfs4_find_slot(tbl);
534 if (slotid == NFS4_MAX_SLOT_TABLE) {
535 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
536 spin_unlock(&tbl->slot_tbl_lock);
537 dprintk("<-- %s: no free slots\n", __func__);
540 spin_unlock(&tbl->slot_tbl_lock);
542 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
543 slot = tbl->slots + slotid;
544 args->sa_session = session;
545 args->sa_slotid = slotid;
546 args->sa_cache_this = cache_reply;
548 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
550 res->sr_session = session;
552 res->sr_renewal_time = jiffies;
553 res->sr_status_flags = 0;
555 * sr_status is only set in decode_sequence, and so will remain
556 * set to 1 if an rpc level failure occurs.
562 int nfs4_setup_sequence(const struct nfs_server *server,
563 struct nfs4_sequence_args *args,
564 struct nfs4_sequence_res *res,
566 struct rpc_task *task)
568 struct nfs4_session *session = nfs4_get_session(server);
571 if (session == NULL) {
572 args->sa_session = NULL;
573 res->sr_session = NULL;
577 dprintk("--> %s clp %p session %p sr_slot %td\n",
578 __func__, session->clp, session, res->sr_slot ?
579 res->sr_slot - session->fc_slot_table.slots : -1);
581 ret = nfs41_setup_sequence(session, args, res, cache_reply,
584 dprintk("<-- %s status=%d\n", __func__, ret);
588 struct nfs41_call_sync_data {
589 const struct nfs_server *seq_server;
590 struct nfs4_sequence_args *seq_args;
591 struct nfs4_sequence_res *seq_res;
595 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
597 struct nfs41_call_sync_data *data = calldata;
599 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
601 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
602 data->seq_res, data->cache_reply, task))
604 rpc_call_start(task);
607 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
609 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
610 nfs41_call_sync_prepare(task, calldata);
613 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
615 struct nfs41_call_sync_data *data = calldata;
617 nfs41_sequence_done(task, data->seq_res);
620 struct rpc_call_ops nfs41_call_sync_ops = {
621 .rpc_call_prepare = nfs41_call_sync_prepare,
622 .rpc_call_done = nfs41_call_sync_done,
625 struct rpc_call_ops nfs41_call_priv_sync_ops = {
626 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
627 .rpc_call_done = nfs41_call_sync_done,
630 static int nfs4_call_sync_sequence(struct nfs_server *server,
631 struct rpc_message *msg,
632 struct nfs4_sequence_args *args,
633 struct nfs4_sequence_res *res,
638 struct rpc_task *task;
639 struct nfs41_call_sync_data data = {
640 .seq_server = server,
643 .cache_reply = cache_reply,
645 struct rpc_task_setup task_setup = {
646 .rpc_client = server->client,
648 .callback_ops = &nfs41_call_sync_ops,
649 .callback_data = &data
654 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
655 task = rpc_run_task(&task_setup);
659 ret = task->tk_status;
665 int _nfs4_call_sync_session(struct nfs_server *server,
666 struct rpc_message *msg,
667 struct nfs4_sequence_args *args,
668 struct nfs4_sequence_res *res,
671 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
675 static int nfs4_sequence_done(struct rpc_task *task,
676 struct nfs4_sequence_res *res)
680 #endif /* CONFIG_NFS_V4_1 */
682 int _nfs4_call_sync(struct nfs_server *server,
683 struct rpc_message *msg,
684 struct nfs4_sequence_args *args,
685 struct nfs4_sequence_res *res,
688 args->sa_session = res->sr_session = NULL;
689 return rpc_call_sync(server->client, msg, 0);
692 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
693 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
694 &(res)->seq_res, (cache_reply))
696 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
698 struct nfs_inode *nfsi = NFS_I(dir);
700 spin_lock(&dir->i_lock);
701 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
702 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
703 nfs_force_lookup_revalidate(dir);
704 nfsi->change_attr = cinfo->after;
705 spin_unlock(&dir->i_lock);
708 struct nfs4_opendata {
710 struct nfs_openargs o_arg;
711 struct nfs_openres o_res;
712 struct nfs_open_confirmargs c_arg;
713 struct nfs_open_confirmres c_res;
714 struct nfs_fattr f_attr;
715 struct nfs_fattr dir_attr;
718 struct nfs4_state_owner *owner;
719 struct nfs4_state *state;
721 unsigned long timestamp;
722 unsigned int rpc_done : 1;
728 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
730 p->o_res.f_attr = &p->f_attr;
731 p->o_res.dir_attr = &p->dir_attr;
732 p->o_res.seqid = p->o_arg.seqid;
733 p->c_res.seqid = p->c_arg.seqid;
734 p->o_res.server = p->o_arg.server;
735 nfs_fattr_init(&p->f_attr);
736 nfs_fattr_init(&p->dir_attr);
739 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
740 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
741 const struct iattr *attrs,
744 struct dentry *parent = dget_parent(path->dentry);
745 struct inode *dir = parent->d_inode;
746 struct nfs_server *server = NFS_SERVER(dir);
747 struct nfs4_opendata *p;
749 p = kzalloc(sizeof(*p), gfp_mask);
752 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
753 if (p->o_arg.seqid == NULL)
759 atomic_inc(&sp->so_count);
760 p->o_arg.fh = NFS_FH(dir);
761 p->o_arg.open_flags = flags;
762 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
763 p->o_arg.clientid = server->nfs_client->cl_clientid;
764 p->o_arg.id = sp->so_owner_id.id;
765 p->o_arg.name = &p->path.dentry->d_name;
766 p->o_arg.server = server;
767 p->o_arg.bitmask = server->attr_bitmask;
768 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
769 if (flags & O_CREAT) {
772 p->o_arg.u.attrs = &p->attrs;
773 memcpy(&p->attrs, attrs, sizeof(p->attrs));
774 s = (u32 *) p->o_arg.u.verifier.data;
778 p->c_arg.fh = &p->o_res.fh;
779 p->c_arg.stateid = &p->o_res.stateid;
780 p->c_arg.seqid = p->o_arg.seqid;
781 nfs4_init_opendata_res(p);
791 static void nfs4_opendata_free(struct kref *kref)
793 struct nfs4_opendata *p = container_of(kref,
794 struct nfs4_opendata, kref);
796 nfs_free_seqid(p->o_arg.seqid);
797 if (p->state != NULL)
798 nfs4_put_open_state(p->state);
799 nfs4_put_state_owner(p->owner);
805 static void nfs4_opendata_put(struct nfs4_opendata *p)
808 kref_put(&p->kref, nfs4_opendata_free);
811 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
815 ret = rpc_wait_for_completion_task(task);
819 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
823 if (open_mode & O_EXCL)
825 switch (mode & (FMODE_READ|FMODE_WRITE)) {
827 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
828 && state->n_rdonly != 0;
831 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
832 && state->n_wronly != 0;
834 case FMODE_READ|FMODE_WRITE:
835 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
836 && state->n_rdwr != 0;
842 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
844 if ((delegation->type & fmode) != fmode)
846 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
848 nfs_mark_delegation_referenced(delegation);
852 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
861 case FMODE_READ|FMODE_WRITE:
864 nfs4_state_set_mode_locked(state, state->state | fmode);
867 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
869 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
870 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
871 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
874 set_bit(NFS_O_RDONLY_STATE, &state->flags);
877 set_bit(NFS_O_WRONLY_STATE, &state->flags);
879 case FMODE_READ|FMODE_WRITE:
880 set_bit(NFS_O_RDWR_STATE, &state->flags);
884 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
886 write_seqlock(&state->seqlock);
887 nfs_set_open_stateid_locked(state, stateid, fmode);
888 write_sequnlock(&state->seqlock);
891 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
894 * Protect the call to nfs4_state_set_mode_locked and
895 * serialise the stateid update
897 write_seqlock(&state->seqlock);
898 if (deleg_stateid != NULL) {
899 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
900 set_bit(NFS_DELEGATED_STATE, &state->flags);
902 if (open_stateid != NULL)
903 nfs_set_open_stateid_locked(state, open_stateid, fmode);
904 write_sequnlock(&state->seqlock);
905 spin_lock(&state->owner->so_lock);
906 update_open_stateflags(state, fmode);
907 spin_unlock(&state->owner->so_lock);
910 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
912 struct nfs_inode *nfsi = NFS_I(state->inode);
913 struct nfs_delegation *deleg_cur;
916 fmode &= (FMODE_READ|FMODE_WRITE);
919 deleg_cur = rcu_dereference(nfsi->delegation);
920 if (deleg_cur == NULL)
923 spin_lock(&deleg_cur->lock);
924 if (nfsi->delegation != deleg_cur ||
925 (deleg_cur->type & fmode) != fmode)
926 goto no_delegation_unlock;
928 if (delegation == NULL)
929 delegation = &deleg_cur->stateid;
930 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
931 goto no_delegation_unlock;
933 nfs_mark_delegation_referenced(deleg_cur);
934 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
936 no_delegation_unlock:
937 spin_unlock(&deleg_cur->lock);
941 if (!ret && open_stateid != NULL) {
942 __update_open_stateid(state, open_stateid, NULL, fmode);
950 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
952 struct nfs_delegation *delegation;
955 delegation = rcu_dereference(NFS_I(inode)->delegation);
956 if (delegation == NULL || (delegation->type & fmode) == fmode) {
961 nfs_inode_return_delegation(inode);
964 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
966 struct nfs4_state *state = opendata->state;
967 struct nfs_inode *nfsi = NFS_I(state->inode);
968 struct nfs_delegation *delegation;
969 int open_mode = opendata->o_arg.open_flags & O_EXCL;
970 fmode_t fmode = opendata->o_arg.fmode;
971 nfs4_stateid stateid;
975 if (can_open_cached(state, fmode, open_mode)) {
976 spin_lock(&state->owner->so_lock);
977 if (can_open_cached(state, fmode, open_mode)) {
978 update_open_stateflags(state, fmode);
979 spin_unlock(&state->owner->so_lock);
980 goto out_return_state;
982 spin_unlock(&state->owner->so_lock);
985 delegation = rcu_dereference(nfsi->delegation);
986 if (delegation == NULL ||
987 !can_open_delegated(delegation, fmode)) {
991 /* Save the delegation */
992 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
994 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
999 /* Try to update the stateid using the delegation */
1000 if (update_open_stateid(state, NULL, &stateid, fmode))
1001 goto out_return_state;
1004 return ERR_PTR(ret);
1006 atomic_inc(&state->count);
1010 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1012 struct inode *inode;
1013 struct nfs4_state *state = NULL;
1014 struct nfs_delegation *delegation;
1017 if (!data->rpc_done) {
1018 state = nfs4_try_open_cached(data);
1023 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1025 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1026 ret = PTR_ERR(inode);
1030 state = nfs4_get_open_state(inode, data->owner);
1033 if (data->o_res.delegation_type != 0) {
1034 int delegation_flags = 0;
1037 delegation = rcu_dereference(NFS_I(inode)->delegation);
1039 delegation_flags = delegation->flags;
1041 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1042 nfs_inode_set_delegation(state->inode,
1043 data->owner->so_cred,
1046 nfs_inode_reclaim_delegation(state->inode,
1047 data->owner->so_cred,
1051 update_open_stateid(state, &data->o_res.stateid, NULL,
1059 return ERR_PTR(ret);
1062 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1064 struct nfs_inode *nfsi = NFS_I(state->inode);
1065 struct nfs_open_context *ctx;
1067 spin_lock(&state->inode->i_lock);
1068 list_for_each_entry(ctx, &nfsi->open_files, list) {
1069 if (ctx->state != state)
1071 get_nfs_open_context(ctx);
1072 spin_unlock(&state->inode->i_lock);
1075 spin_unlock(&state->inode->i_lock);
1076 return ERR_PTR(-ENOENT);
1079 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1081 struct nfs4_opendata *opendata;
1083 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1084 if (opendata == NULL)
1085 return ERR_PTR(-ENOMEM);
1086 opendata->state = state;
1087 atomic_inc(&state->count);
1091 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1093 struct nfs4_state *newstate;
1096 opendata->o_arg.open_flags = 0;
1097 opendata->o_arg.fmode = fmode;
1098 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1099 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1100 nfs4_init_opendata_res(opendata);
1101 ret = _nfs4_recover_proc_open(opendata);
1104 newstate = nfs4_opendata_to_nfs4_state(opendata);
1105 if (IS_ERR(newstate))
1106 return PTR_ERR(newstate);
1107 nfs4_close_state(&opendata->path, newstate, fmode);
1112 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1114 struct nfs4_state *newstate;
1117 /* memory barrier prior to reading state->n_* */
1118 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1120 if (state->n_rdwr != 0) {
1121 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1122 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1125 if (newstate != state)
1128 if (state->n_wronly != 0) {
1129 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1130 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1133 if (newstate != state)
1136 if (state->n_rdonly != 0) {
1137 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1138 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1141 if (newstate != state)
1145 * We may have performed cached opens for all three recoveries.
1146 * Check if we need to update the current stateid.
1148 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1149 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1150 write_seqlock(&state->seqlock);
1151 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1152 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1153 write_sequnlock(&state->seqlock);
1160 * reclaim state on the server after a reboot.
1162 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1164 struct nfs_delegation *delegation;
1165 struct nfs4_opendata *opendata;
1166 fmode_t delegation_type = 0;
1169 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1170 if (IS_ERR(opendata))
1171 return PTR_ERR(opendata);
1172 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1173 opendata->o_arg.fh = NFS_FH(state->inode);
1175 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1176 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1177 delegation_type = delegation->type;
1179 opendata->o_arg.u.delegation_type = delegation_type;
1180 status = nfs4_open_recover(opendata, state);
1181 nfs4_opendata_put(opendata);
1185 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1187 struct nfs_server *server = NFS_SERVER(state->inode);
1188 struct nfs4_exception exception = { };
1191 err = _nfs4_do_open_reclaim(ctx, state);
1192 if (err != -NFS4ERR_DELAY)
1194 nfs4_handle_exception(server, err, &exception);
1195 } while (exception.retry);
1199 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1201 struct nfs_open_context *ctx;
1204 ctx = nfs4_state_find_open_context(state);
1206 return PTR_ERR(ctx);
1207 ret = nfs4_do_open_reclaim(ctx, state);
1208 put_nfs_open_context(ctx);
1212 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1214 struct nfs4_opendata *opendata;
1217 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1218 if (IS_ERR(opendata))
1219 return PTR_ERR(opendata);
1220 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1221 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1222 sizeof(opendata->o_arg.u.delegation.data));
1223 ret = nfs4_open_recover(opendata, state);
1224 nfs4_opendata_put(opendata);
1228 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1230 struct nfs4_exception exception = { };
1231 struct nfs_server *server = NFS_SERVER(state->inode);
1234 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1240 case -NFS4ERR_BADSESSION:
1241 case -NFS4ERR_BADSLOT:
1242 case -NFS4ERR_BAD_HIGH_SLOT:
1243 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1244 case -NFS4ERR_DEADSESSION:
1245 nfs4_schedule_state_recovery(
1246 server->nfs_client);
1248 case -NFS4ERR_STALE_CLIENTID:
1249 case -NFS4ERR_STALE_STATEID:
1250 case -NFS4ERR_EXPIRED:
1251 /* Don't recall a delegation if it was lost */
1252 nfs4_schedule_state_recovery(server->nfs_client);
1256 * The show must go on: exit, but mark the
1257 * stateid as needing recovery.
1259 case -NFS4ERR_ADMIN_REVOKED:
1260 case -NFS4ERR_BAD_STATEID:
1261 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1264 * User RPCSEC_GSS context has expired.
1265 * We cannot recover this stateid now, so
1266 * skip it and allow recovery thread to
1273 err = nfs4_handle_exception(server, err, &exception);
1274 } while (exception.retry);
1279 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1281 struct nfs4_opendata *data = calldata;
1283 data->rpc_status = task->tk_status;
1284 if (data->rpc_status == 0) {
1285 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1286 sizeof(data->o_res.stateid.data));
1287 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1288 renew_lease(data->o_res.server, data->timestamp);
1293 static void nfs4_open_confirm_release(void *calldata)
1295 struct nfs4_opendata *data = calldata;
1296 struct nfs4_state *state = NULL;
1298 /* If this request hasn't been cancelled, do nothing */
1299 if (data->cancelled == 0)
1301 /* In case of error, no cleanup! */
1302 if (!data->rpc_done)
1304 state = nfs4_opendata_to_nfs4_state(data);
1306 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1308 nfs4_opendata_put(data);
1311 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1312 .rpc_call_done = nfs4_open_confirm_done,
1313 .rpc_release = nfs4_open_confirm_release,
1317 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1319 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1321 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1322 struct rpc_task *task;
1323 struct rpc_message msg = {
1324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1325 .rpc_argp = &data->c_arg,
1326 .rpc_resp = &data->c_res,
1327 .rpc_cred = data->owner->so_cred,
1329 struct rpc_task_setup task_setup_data = {
1330 .rpc_client = server->client,
1331 .rpc_message = &msg,
1332 .callback_ops = &nfs4_open_confirm_ops,
1333 .callback_data = data,
1334 .workqueue = nfsiod_workqueue,
1335 .flags = RPC_TASK_ASYNC,
1339 kref_get(&data->kref);
1341 data->rpc_status = 0;
1342 data->timestamp = jiffies;
1343 task = rpc_run_task(&task_setup_data);
1345 return PTR_ERR(task);
1346 status = nfs4_wait_for_completion_rpc_task(task);
1348 data->cancelled = 1;
1351 status = data->rpc_status;
1356 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1358 struct nfs4_opendata *data = calldata;
1359 struct nfs4_state_owner *sp = data->owner;
1361 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1364 * Check if we still need to send an OPEN call, or if we can use
1365 * a delegation instead.
1367 if (data->state != NULL) {
1368 struct nfs_delegation *delegation;
1370 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1373 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1374 if (delegation != NULL &&
1375 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1381 /* Update sequence id. */
1382 data->o_arg.id = sp->so_owner_id.id;
1383 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1384 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1385 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1386 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1388 data->timestamp = jiffies;
1389 if (nfs4_setup_sequence(data->o_arg.server,
1390 &data->o_arg.seq_args,
1391 &data->o_res.seq_res, 1, task))
1393 rpc_call_start(task);
1396 task->tk_action = NULL;
1400 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1402 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1403 nfs4_open_prepare(task, calldata);
1406 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1408 struct nfs4_opendata *data = calldata;
1410 data->rpc_status = task->tk_status;
1412 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1415 if (task->tk_status == 0) {
1416 switch (data->o_res.f_attr->mode & S_IFMT) {
1420 data->rpc_status = -ELOOP;
1423 data->rpc_status = -EISDIR;
1426 data->rpc_status = -ENOTDIR;
1428 renew_lease(data->o_res.server, data->timestamp);
1429 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1430 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1435 static void nfs4_open_release(void *calldata)
1437 struct nfs4_opendata *data = calldata;
1438 struct nfs4_state *state = NULL;
1440 /* If this request hasn't been cancelled, do nothing */
1441 if (data->cancelled == 0)
1443 /* In case of error, no cleanup! */
1444 if (data->rpc_status != 0 || !data->rpc_done)
1446 /* In case we need an open_confirm, no cleanup! */
1447 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1449 state = nfs4_opendata_to_nfs4_state(data);
1451 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1453 nfs4_opendata_put(data);
1456 static const struct rpc_call_ops nfs4_open_ops = {
1457 .rpc_call_prepare = nfs4_open_prepare,
1458 .rpc_call_done = nfs4_open_done,
1459 .rpc_release = nfs4_open_release,
1462 static const struct rpc_call_ops nfs4_recover_open_ops = {
1463 .rpc_call_prepare = nfs4_recover_open_prepare,
1464 .rpc_call_done = nfs4_open_done,
1465 .rpc_release = nfs4_open_release,
1468 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1470 struct inode *dir = data->dir->d_inode;
1471 struct nfs_server *server = NFS_SERVER(dir);
1472 struct nfs_openargs *o_arg = &data->o_arg;
1473 struct nfs_openres *o_res = &data->o_res;
1474 struct rpc_task *task;
1475 struct rpc_message msg = {
1476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1479 .rpc_cred = data->owner->so_cred,
1481 struct rpc_task_setup task_setup_data = {
1482 .rpc_client = server->client,
1483 .rpc_message = &msg,
1484 .callback_ops = &nfs4_open_ops,
1485 .callback_data = data,
1486 .workqueue = nfsiod_workqueue,
1487 .flags = RPC_TASK_ASYNC,
1491 kref_get(&data->kref);
1493 data->rpc_status = 0;
1494 data->cancelled = 0;
1496 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1497 task = rpc_run_task(&task_setup_data);
1499 return PTR_ERR(task);
1500 status = nfs4_wait_for_completion_rpc_task(task);
1502 data->cancelled = 1;
1505 status = data->rpc_status;
1511 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1513 struct inode *dir = data->dir->d_inode;
1514 struct nfs_openres *o_res = &data->o_res;
1517 status = nfs4_run_open_task(data, 1);
1518 if (status != 0 || !data->rpc_done)
1521 nfs_refresh_inode(dir, o_res->dir_attr);
1523 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1524 status = _nfs4_proc_open_confirm(data);
1533 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1535 static int _nfs4_proc_open(struct nfs4_opendata *data)
1537 struct inode *dir = data->dir->d_inode;
1538 struct nfs_server *server = NFS_SERVER(dir);
1539 struct nfs_openargs *o_arg = &data->o_arg;
1540 struct nfs_openres *o_res = &data->o_res;
1543 status = nfs4_run_open_task(data, 0);
1544 if (status != 0 || !data->rpc_done)
1547 if (o_arg->open_flags & O_CREAT) {
1548 update_changeattr(dir, &o_res->cinfo);
1549 nfs_post_op_update_inode(dir, o_res->dir_attr);
1551 nfs_refresh_inode(dir, o_res->dir_attr);
1552 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1553 server->caps &= ~NFS_CAP_POSIX_LOCK;
1554 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1555 status = _nfs4_proc_open_confirm(data);
1559 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1560 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1564 static int nfs4_recover_expired_lease(struct nfs_server *server)
1566 struct nfs_client *clp = server->nfs_client;
1570 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1571 ret = nfs4_wait_clnt_recover(clp);
1574 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1575 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1577 nfs4_schedule_state_recovery(clp);
1585 * reclaim state on the server after a network partition.
1586 * Assumes caller holds the appropriate lock
1588 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1590 struct nfs4_opendata *opendata;
1593 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1594 if (IS_ERR(opendata))
1595 return PTR_ERR(opendata);
1596 ret = nfs4_open_recover(opendata, state);
1598 d_drop(ctx->path.dentry);
1599 nfs4_opendata_put(opendata);
1603 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1605 struct nfs_server *server = NFS_SERVER(state->inode);
1606 struct nfs4_exception exception = { };
1610 err = _nfs4_open_expired(ctx, state);
1614 case -NFS4ERR_GRACE:
1615 case -NFS4ERR_DELAY:
1616 nfs4_handle_exception(server, err, &exception);
1619 } while (exception.retry);
1624 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1626 struct nfs_open_context *ctx;
1629 ctx = nfs4_state_find_open_context(state);
1631 return PTR_ERR(ctx);
1632 ret = nfs4_do_open_expired(ctx, state);
1633 put_nfs_open_context(ctx);
1638 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1639 * fields corresponding to attributes that were used to store the verifier.
1640 * Make sure we clobber those fields in the later setattr call
1642 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1644 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1645 !(sattr->ia_valid & ATTR_ATIME_SET))
1646 sattr->ia_valid |= ATTR_ATIME;
1648 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1649 !(sattr->ia_valid & ATTR_MTIME_SET))
1650 sattr->ia_valid |= ATTR_MTIME;
1654 * Returns a referenced nfs4_state
1656 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)
1658 struct nfs4_state_owner *sp;
1659 struct nfs4_state *state = NULL;
1660 struct nfs_server *server = NFS_SERVER(dir);
1661 struct nfs4_opendata *opendata;
1664 /* Protect against reboot recovery conflicts */
1666 if (!(sp = nfs4_get_state_owner(server, cred))) {
1667 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1670 status = nfs4_recover_expired_lease(server);
1672 goto err_put_state_owner;
1673 if (path->dentry->d_inode != NULL)
1674 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1676 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1677 if (opendata == NULL)
1678 goto err_put_state_owner;
1680 if (path->dentry->d_inode != NULL)
1681 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1683 status = _nfs4_proc_open(opendata);
1685 goto err_opendata_put;
1687 state = nfs4_opendata_to_nfs4_state(opendata);
1688 status = PTR_ERR(state);
1690 goto err_opendata_put;
1691 if (server->caps & NFS_CAP_POSIX_LOCK)
1692 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1694 if (opendata->o_arg.open_flags & O_EXCL) {
1695 nfs4_exclusive_attrset(opendata, sattr);
1697 nfs_fattr_init(opendata->o_res.f_attr);
1698 status = nfs4_do_setattr(state->inode, cred,
1699 opendata->o_res.f_attr, sattr,
1702 nfs_setattr_update_inode(state->inode, sattr);
1703 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1705 nfs4_opendata_put(opendata);
1706 nfs4_put_state_owner(sp);
1710 nfs4_opendata_put(opendata);
1711 err_put_state_owner:
1712 nfs4_put_state_owner(sp);
1719 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)
1721 struct nfs4_exception exception = { };
1722 struct nfs4_state *res;
1726 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1729 /* NOTE: BAD_SEQID means the server and client disagree about the
1730 * book-keeping w.r.t. state-changing operations
1731 * (OPEN/CLOSE/LOCK/LOCKU...)
1732 * It is actually a sign of a bug on the client or on the server.
1734 * If we receive a BAD_SEQID error in the particular case of
1735 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1736 * have unhashed the old state_owner for us, and that we can
1737 * therefore safely retry using a new one. We should still warn
1738 * the user though...
1740 if (status == -NFS4ERR_BAD_SEQID) {
1741 printk(KERN_WARNING "NFS: v4 server %s "
1742 " returned a bad sequence-id error!\n",
1743 NFS_SERVER(dir)->nfs_client->cl_hostname);
1744 exception.retry = 1;
1748 * BAD_STATEID on OPEN means that the server cancelled our
1749 * state before it received the OPEN_CONFIRM.
1750 * Recover by retrying the request as per the discussion
1751 * on Page 181 of RFC3530.
1753 if (status == -NFS4ERR_BAD_STATEID) {
1754 exception.retry = 1;
1757 if (status == -EAGAIN) {
1758 /* We must have found a delegation */
1759 exception.retry = 1;
1762 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1763 status, &exception));
1764 } while (exception.retry);
1768 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1769 struct nfs_fattr *fattr, struct iattr *sattr,
1770 struct nfs4_state *state)
1772 struct nfs_server *server = NFS_SERVER(inode);
1773 struct nfs_setattrargs arg = {
1774 .fh = NFS_FH(inode),
1777 .bitmask = server->attr_bitmask,
1779 struct nfs_setattrres res = {
1783 struct rpc_message msg = {
1784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1789 unsigned long timestamp = jiffies;
1792 nfs_fattr_init(fattr);
1794 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1795 /* Use that stateid */
1796 } else if (state != NULL) {
1797 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1799 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1801 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1802 if (status == 0 && state != NULL)
1803 renew_lease(server, timestamp);
1807 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1808 struct nfs_fattr *fattr, struct iattr *sattr,
1809 struct nfs4_state *state)
1811 struct nfs_server *server = NFS_SERVER(inode);
1812 struct nfs4_exception exception = { };
1815 err = nfs4_handle_exception(server,
1816 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1818 } while (exception.retry);
1822 struct nfs4_closedata {
1824 struct inode *inode;
1825 struct nfs4_state *state;
1826 struct nfs_closeargs arg;
1827 struct nfs_closeres res;
1828 struct nfs_fattr fattr;
1829 unsigned long timestamp;
1832 static void nfs4_free_closedata(void *data)
1834 struct nfs4_closedata *calldata = data;
1835 struct nfs4_state_owner *sp = calldata->state->owner;
1837 nfs4_put_open_state(calldata->state);
1838 nfs_free_seqid(calldata->arg.seqid);
1839 nfs4_put_state_owner(sp);
1840 path_put(&calldata->path);
1844 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1847 spin_lock(&state->owner->so_lock);
1848 if (!(fmode & FMODE_READ))
1849 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1850 if (!(fmode & FMODE_WRITE))
1851 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1852 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1853 spin_unlock(&state->owner->so_lock);
1856 static void nfs4_close_done(struct rpc_task *task, void *data)
1858 struct nfs4_closedata *calldata = data;
1859 struct nfs4_state *state = calldata->state;
1860 struct nfs_server *server = NFS_SERVER(calldata->inode);
1862 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1864 /* hmm. we are done with the inode, and in the process of freeing
1865 * the state_owner. we keep this around to process errors
1867 switch (task->tk_status) {
1869 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1870 renew_lease(server, calldata->timestamp);
1871 nfs4_close_clear_stateid_flags(state,
1872 calldata->arg.fmode);
1874 case -NFS4ERR_STALE_STATEID:
1875 case -NFS4ERR_OLD_STATEID:
1876 case -NFS4ERR_BAD_STATEID:
1877 case -NFS4ERR_EXPIRED:
1878 if (calldata->arg.fmode == 0)
1881 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1882 rpc_restart_call_prepare(task);
1884 nfs_release_seqid(calldata->arg.seqid);
1885 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1888 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1890 struct nfs4_closedata *calldata = data;
1891 struct nfs4_state *state = calldata->state;
1894 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1897 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1898 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1899 spin_lock(&state->owner->so_lock);
1900 /* Calculate the change in open mode */
1901 if (state->n_rdwr == 0) {
1902 if (state->n_rdonly == 0) {
1903 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1904 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1905 calldata->arg.fmode &= ~FMODE_READ;
1907 if (state->n_wronly == 0) {
1908 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1909 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1910 calldata->arg.fmode &= ~FMODE_WRITE;
1913 spin_unlock(&state->owner->so_lock);
1916 /* Note: exit _without_ calling nfs4_close_done */
1917 task->tk_action = NULL;
1921 if (calldata->arg.fmode == 0)
1922 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1924 nfs_fattr_init(calldata->res.fattr);
1925 calldata->timestamp = jiffies;
1926 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1927 &calldata->arg.seq_args, &calldata->res.seq_res,
1930 rpc_call_start(task);
1933 static const struct rpc_call_ops nfs4_close_ops = {
1934 .rpc_call_prepare = nfs4_close_prepare,
1935 .rpc_call_done = nfs4_close_done,
1936 .rpc_release = nfs4_free_closedata,
1940 * It is possible for data to be read/written from a mem-mapped file
1941 * after the sys_close call (which hits the vfs layer as a flush).
1942 * This means that we can't safely call nfsv4 close on a file until
1943 * the inode is cleared. This in turn means that we are not good
1944 * NFSv4 citizens - we do not indicate to the server to update the file's
1945 * share state even when we are done with one of the three share
1946 * stateid's in the inode.
1948 * NOTE: Caller must be holding the sp->so_owner semaphore!
1950 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
1952 struct nfs_server *server = NFS_SERVER(state->inode);
1953 struct nfs4_closedata *calldata;
1954 struct nfs4_state_owner *sp = state->owner;
1955 struct rpc_task *task;
1956 struct rpc_message msg = {
1957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1958 .rpc_cred = state->owner->so_cred,
1960 struct rpc_task_setup task_setup_data = {
1961 .rpc_client = server->client,
1962 .rpc_message = &msg,
1963 .callback_ops = &nfs4_close_ops,
1964 .workqueue = nfsiod_workqueue,
1965 .flags = RPC_TASK_ASYNC,
1967 int status = -ENOMEM;
1969 calldata = kzalloc(sizeof(*calldata), gfp_mask);
1970 if (calldata == NULL)
1972 calldata->inode = state->inode;
1973 calldata->state = state;
1974 calldata->arg.fh = NFS_FH(state->inode);
1975 calldata->arg.stateid = &state->open_stateid;
1976 /* Serialization for the sequence id */
1977 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
1978 if (calldata->arg.seqid == NULL)
1979 goto out_free_calldata;
1980 calldata->arg.fmode = 0;
1981 calldata->arg.bitmask = server->cache_consistency_bitmask;
1982 calldata->res.fattr = &calldata->fattr;
1983 calldata->res.seqid = calldata->arg.seqid;
1984 calldata->res.server = server;
1986 calldata->path = *path;
1988 msg.rpc_argp = &calldata->arg;
1989 msg.rpc_resp = &calldata->res;
1990 task_setup_data.callback_data = calldata;
1991 task = rpc_run_task(&task_setup_data);
1993 return PTR_ERR(task);
1996 status = rpc_wait_for_completion_task(task);
2002 nfs4_put_open_state(state);
2003 nfs4_put_state_owner(sp);
2007 static struct inode *
2008 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2010 struct nfs4_state *state;
2012 /* Protect against concurrent sillydeletes */
2013 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2015 return ERR_CAST(state);
2017 return igrab(state->inode);
2020 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2022 if (ctx->state == NULL)
2025 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2027 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2030 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2032 struct nfs4_server_caps_arg args = {
2035 struct nfs4_server_caps_res res = {};
2036 struct rpc_message msg = {
2037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2043 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2045 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2046 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2047 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2048 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2049 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2050 NFS_CAP_CTIME|NFS_CAP_MTIME);
2051 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2052 server->caps |= NFS_CAP_ACLS;
2053 if (res.has_links != 0)
2054 server->caps |= NFS_CAP_HARDLINKS;
2055 if (res.has_symlinks != 0)
2056 server->caps |= NFS_CAP_SYMLINKS;
2057 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2058 server->caps |= NFS_CAP_FILEID;
2059 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2060 server->caps |= NFS_CAP_MODE;
2061 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2062 server->caps |= NFS_CAP_NLINK;
2063 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2064 server->caps |= NFS_CAP_OWNER;
2065 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2066 server->caps |= NFS_CAP_OWNER_GROUP;
2067 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2068 server->caps |= NFS_CAP_ATIME;
2069 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2070 server->caps |= NFS_CAP_CTIME;
2071 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2072 server->caps |= NFS_CAP_MTIME;
2074 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2075 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2076 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2077 server->acl_bitmask = res.acl_bitmask;
2083 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2085 struct nfs4_exception exception = { };
2088 err = nfs4_handle_exception(server,
2089 _nfs4_server_capabilities(server, fhandle),
2091 } while (exception.retry);
2095 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2096 struct nfs_fsinfo *info)
2098 struct nfs4_lookup_root_arg args = {
2099 .bitmask = nfs4_fattr_bitmap,
2101 struct nfs4_lookup_res res = {
2103 .fattr = info->fattr,
2106 struct rpc_message msg = {
2107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2112 nfs_fattr_init(info->fattr);
2113 return nfs4_call_sync(server, &msg, &args, &res, 0);
2116 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2117 struct nfs_fsinfo *info)
2119 struct nfs4_exception exception = { };
2122 err = nfs4_handle_exception(server,
2123 _nfs4_lookup_root(server, fhandle, info),
2125 } while (exception.retry);
2130 * get the file handle for the "/" directory on the server
2132 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2133 struct nfs_fsinfo *info)
2137 status = nfs4_lookup_root(server, fhandle, info);
2139 status = nfs4_server_capabilities(server, fhandle);
2141 status = nfs4_do_fsinfo(server, fhandle, info);
2142 return nfs4_map_errors(status);
2146 * Get locations and (maybe) other attributes of a referral.
2147 * Note that we'll actually follow the referral later when
2148 * we detect fsid mismatch in inode revalidation
2150 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2152 int status = -ENOMEM;
2153 struct page *page = NULL;
2154 struct nfs4_fs_locations *locations = NULL;
2156 page = alloc_page(GFP_KERNEL);
2159 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2160 if (locations == NULL)
2163 status = nfs4_proc_fs_locations(dir, name, locations, page);
2166 /* Make sure server returned a different fsid for the referral */
2167 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2168 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2173 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2174 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2176 fattr->mode = S_IFDIR;
2177 memset(fhandle, 0, sizeof(struct nfs_fh));
2185 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2187 struct nfs4_getattr_arg args = {
2189 .bitmask = server->attr_bitmask,
2191 struct nfs4_getattr_res res = {
2195 struct rpc_message msg = {
2196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2201 nfs_fattr_init(fattr);
2202 return nfs4_call_sync(server, &msg, &args, &res, 0);
2205 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2207 struct nfs4_exception exception = { };
2210 err = nfs4_handle_exception(server,
2211 _nfs4_proc_getattr(server, fhandle, fattr),
2213 } while (exception.retry);
2218 * The file is not closed if it is opened due to the a request to change
2219 * the size of the file. The open call will not be needed once the
2220 * VFS layer lookup-intents are implemented.
2222 * Close is called when the inode is destroyed.
2223 * If we haven't opened the file for O_WRONLY, we
2224 * need to in the size_change case to obtain a stateid.
2227 * Because OPEN is always done by name in nfsv4, it is
2228 * possible that we opened a different file by the same
2229 * name. We can recognize this race condition, but we
2230 * can't do anything about it besides returning an error.
2232 * This will be fixed with VFS changes (lookup-intent).
2235 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2236 struct iattr *sattr)
2238 struct inode *inode = dentry->d_inode;
2239 struct rpc_cred *cred = NULL;
2240 struct nfs4_state *state = NULL;
2243 nfs_fattr_init(fattr);
2245 /* Search for an existing open(O_WRITE) file */
2246 if (sattr->ia_valid & ATTR_FILE) {
2247 struct nfs_open_context *ctx;
2249 ctx = nfs_file_open_context(sattr->ia_file);
2256 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2258 nfs_setattr_update_inode(inode, sattr);
2262 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2263 const struct qstr *name, struct nfs_fh *fhandle,
2264 struct nfs_fattr *fattr)
2267 struct nfs4_lookup_arg args = {
2268 .bitmask = server->attr_bitmask,
2272 struct nfs4_lookup_res res = {
2277 struct rpc_message msg = {
2278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2283 nfs_fattr_init(fattr);
2285 dprintk("NFS call lookupfh %s\n", name->name);
2286 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2287 dprintk("NFS reply lookupfh: %d\n", status);
2291 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2292 struct qstr *name, struct nfs_fh *fhandle,
2293 struct nfs_fattr *fattr)
2295 struct nfs4_exception exception = { };
2298 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2300 if (err == -NFS4ERR_MOVED) {
2304 err = nfs4_handle_exception(server, err, &exception);
2305 } while (exception.retry);
2309 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2310 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2314 dprintk("NFS call lookup %s\n", name->name);
2315 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2316 if (status == -NFS4ERR_MOVED)
2317 status = nfs4_get_referral(dir, name, fattr, fhandle);
2318 dprintk("NFS reply lookup: %d\n", status);
2322 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2324 struct nfs4_exception exception = { };
2327 err = nfs4_handle_exception(NFS_SERVER(dir),
2328 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2330 } while (exception.retry);
2334 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2336 struct nfs_server *server = NFS_SERVER(inode);
2337 struct nfs4_accessargs args = {
2338 .fh = NFS_FH(inode),
2339 .bitmask = server->attr_bitmask,
2341 struct nfs4_accessres res = {
2344 struct rpc_message msg = {
2345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2348 .rpc_cred = entry->cred,
2350 int mode = entry->mask;
2354 * Determine which access bits we want to ask for...
2356 if (mode & MAY_READ)
2357 args.access |= NFS4_ACCESS_READ;
2358 if (S_ISDIR(inode->i_mode)) {
2359 if (mode & MAY_WRITE)
2360 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2361 if (mode & MAY_EXEC)
2362 args.access |= NFS4_ACCESS_LOOKUP;
2364 if (mode & MAY_WRITE)
2365 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2366 if (mode & MAY_EXEC)
2367 args.access |= NFS4_ACCESS_EXECUTE;
2370 res.fattr = nfs_alloc_fattr();
2371 if (res.fattr == NULL)
2374 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2377 if (res.access & NFS4_ACCESS_READ)
2378 entry->mask |= MAY_READ;
2379 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2380 entry->mask |= MAY_WRITE;
2381 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2382 entry->mask |= MAY_EXEC;
2383 nfs_refresh_inode(inode, res.fattr);
2385 nfs_free_fattr(res.fattr);
2389 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2391 struct nfs4_exception exception = { };
2394 err = nfs4_handle_exception(NFS_SERVER(inode),
2395 _nfs4_proc_access(inode, entry),
2397 } while (exception.retry);
2402 * TODO: For the time being, we don't try to get any attributes
2403 * along with any of the zero-copy operations READ, READDIR,
2406 * In the case of the first three, we want to put the GETATTR
2407 * after the read-type operation -- this is because it is hard
2408 * to predict the length of a GETATTR response in v4, and thus
2409 * align the READ data correctly. This means that the GETATTR
2410 * may end up partially falling into the page cache, and we should
2411 * shift it into the 'tail' of the xdr_buf before processing.
2412 * To do this efficiently, we need to know the total length
2413 * of data received, which doesn't seem to be available outside
2416 * In the case of WRITE, we also want to put the GETATTR after
2417 * the operation -- in this case because we want to make sure
2418 * we get the post-operation mtime and size. This means that
2419 * we can't use xdr_encode_pages() as written: we need a variant
2420 * of it which would leave room in the 'tail' iovec.
2422 * Both of these changes to the XDR layer would in fact be quite
2423 * minor, but I decided to leave them for a subsequent patch.
2425 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2426 unsigned int pgbase, unsigned int pglen)
2428 struct nfs4_readlink args = {
2429 .fh = NFS_FH(inode),
2434 struct nfs4_readlink_res res;
2435 struct rpc_message msg = {
2436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2441 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2444 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2445 unsigned int pgbase, unsigned int pglen)
2447 struct nfs4_exception exception = { };
2450 err = nfs4_handle_exception(NFS_SERVER(inode),
2451 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2453 } while (exception.retry);
2459 * We will need to arrange for the VFS layer to provide an atomic open.
2460 * Until then, this create/open method is prone to inefficiency and race
2461 * conditions due to the lookup, create, and open VFS calls from sys_open()
2462 * placed on the wire.
2464 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2465 * The file will be opened again in the subsequent VFS open call
2466 * (nfs4_proc_file_open).
2468 * The open for read will just hang around to be used by any process that
2469 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2473 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2474 int flags, struct nfs_open_context *ctx)
2476 struct path my_path = {
2479 struct path *path = &my_path;
2480 struct nfs4_state *state;
2481 struct rpc_cred *cred = NULL;
2490 sattr->ia_mode &= ~current_umask();
2491 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2493 if (IS_ERR(state)) {
2494 status = PTR_ERR(state);
2497 d_add(dentry, igrab(state->inode));
2498 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2502 nfs4_close_sync(path, state, fmode);
2507 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2509 struct nfs_server *server = NFS_SERVER(dir);
2510 struct nfs_removeargs args = {
2512 .name.len = name->len,
2513 .name.name = name->name,
2514 .bitmask = server->attr_bitmask,
2516 struct nfs_removeres res = {
2519 struct rpc_message msg = {
2520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2524 int status = -ENOMEM;
2526 res.dir_attr = nfs_alloc_fattr();
2527 if (res.dir_attr == NULL)
2530 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2532 update_changeattr(dir, &res.cinfo);
2533 nfs_post_op_update_inode(dir, res.dir_attr);
2535 nfs_free_fattr(res.dir_attr);
2540 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2542 struct nfs4_exception exception = { };
2545 err = nfs4_handle_exception(NFS_SERVER(dir),
2546 _nfs4_proc_remove(dir, name),
2548 } while (exception.retry);
2552 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2554 struct nfs_server *server = NFS_SERVER(dir);
2555 struct nfs_removeargs *args = msg->rpc_argp;
2556 struct nfs_removeres *res = msg->rpc_resp;
2558 args->bitmask = server->cache_consistency_bitmask;
2559 res->server = server;
2560 res->seq_res.sr_slot = NULL;
2561 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2564 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2566 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2568 if (!nfs4_sequence_done(task, &res->seq_res))
2570 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2572 update_changeattr(dir, &res->cinfo);
2573 nfs_post_op_update_inode(dir, res->dir_attr);
2577 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2579 struct nfs_server *server = NFS_SERVER(dir);
2580 struct nfs_renameargs *arg = msg->rpc_argp;
2581 struct nfs_renameres *res = msg->rpc_resp;
2583 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2584 arg->bitmask = server->attr_bitmask;
2585 res->server = server;
2588 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2589 struct inode *new_dir)
2591 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2593 if (!nfs4_sequence_done(task, &res->seq_res))
2595 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2598 update_changeattr(old_dir, &res->old_cinfo);
2599 nfs_post_op_update_inode(old_dir, res->old_fattr);
2600 update_changeattr(new_dir, &res->new_cinfo);
2601 nfs_post_op_update_inode(new_dir, res->new_fattr);
2605 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2606 struct inode *new_dir, struct qstr *new_name)
2608 struct nfs_server *server = NFS_SERVER(old_dir);
2609 struct nfs_renameargs arg = {
2610 .old_dir = NFS_FH(old_dir),
2611 .new_dir = NFS_FH(new_dir),
2612 .old_name = old_name,
2613 .new_name = new_name,
2614 .bitmask = server->attr_bitmask,
2616 struct nfs_renameres res = {
2619 struct rpc_message msg = {
2620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2624 int status = -ENOMEM;
2626 res.old_fattr = nfs_alloc_fattr();
2627 res.new_fattr = nfs_alloc_fattr();
2628 if (res.old_fattr == NULL || res.new_fattr == NULL)
2631 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2633 update_changeattr(old_dir, &res.old_cinfo);
2634 nfs_post_op_update_inode(old_dir, res.old_fattr);
2635 update_changeattr(new_dir, &res.new_cinfo);
2636 nfs_post_op_update_inode(new_dir, res.new_fattr);
2639 nfs_free_fattr(res.new_fattr);
2640 nfs_free_fattr(res.old_fattr);
2644 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2645 struct inode *new_dir, struct qstr *new_name)
2647 struct nfs4_exception exception = { };
2650 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2651 _nfs4_proc_rename(old_dir, old_name,
2654 } while (exception.retry);
2658 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2660 struct nfs_server *server = NFS_SERVER(inode);
2661 struct nfs4_link_arg arg = {
2662 .fh = NFS_FH(inode),
2663 .dir_fh = NFS_FH(dir),
2665 .bitmask = server->attr_bitmask,
2667 struct nfs4_link_res res = {
2670 struct rpc_message msg = {
2671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2675 int status = -ENOMEM;
2677 res.fattr = nfs_alloc_fattr();
2678 res.dir_attr = nfs_alloc_fattr();
2679 if (res.fattr == NULL || res.dir_attr == NULL)
2682 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2684 update_changeattr(dir, &res.cinfo);
2685 nfs_post_op_update_inode(dir, res.dir_attr);
2686 nfs_post_op_update_inode(inode, res.fattr);
2689 nfs_free_fattr(res.dir_attr);
2690 nfs_free_fattr(res.fattr);
2694 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2696 struct nfs4_exception exception = { };
2699 err = nfs4_handle_exception(NFS_SERVER(inode),
2700 _nfs4_proc_link(inode, dir, name),
2702 } while (exception.retry);
2706 struct nfs4_createdata {
2707 struct rpc_message msg;
2708 struct nfs4_create_arg arg;
2709 struct nfs4_create_res res;
2711 struct nfs_fattr fattr;
2712 struct nfs_fattr dir_fattr;
2715 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2716 struct qstr *name, struct iattr *sattr, u32 ftype)
2718 struct nfs4_createdata *data;
2720 data = kzalloc(sizeof(*data), GFP_KERNEL);
2722 struct nfs_server *server = NFS_SERVER(dir);
2724 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2725 data->msg.rpc_argp = &data->arg;
2726 data->msg.rpc_resp = &data->res;
2727 data->arg.dir_fh = NFS_FH(dir);
2728 data->arg.server = server;
2729 data->arg.name = name;
2730 data->arg.attrs = sattr;
2731 data->arg.ftype = ftype;
2732 data->arg.bitmask = server->attr_bitmask;
2733 data->res.server = server;
2734 data->res.fh = &data->fh;
2735 data->res.fattr = &data->fattr;
2736 data->res.dir_fattr = &data->dir_fattr;
2737 nfs_fattr_init(data->res.fattr);
2738 nfs_fattr_init(data->res.dir_fattr);
2743 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2745 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2746 &data->arg, &data->res, 1);
2748 update_changeattr(dir, &data->res.dir_cinfo);
2749 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2750 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2755 static void nfs4_free_createdata(struct nfs4_createdata *data)
2760 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2761 struct page *page, unsigned int len, struct iattr *sattr)
2763 struct nfs4_createdata *data;
2764 int status = -ENAMETOOLONG;
2766 if (len > NFS4_MAXPATHLEN)
2770 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2774 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2775 data->arg.u.symlink.pages = &page;
2776 data->arg.u.symlink.len = len;
2778 status = nfs4_do_create(dir, dentry, data);
2780 nfs4_free_createdata(data);
2785 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2786 struct page *page, unsigned int len, struct iattr *sattr)
2788 struct nfs4_exception exception = { };
2791 err = nfs4_handle_exception(NFS_SERVER(dir),
2792 _nfs4_proc_symlink(dir, dentry, page,
2795 } while (exception.retry);
2799 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2800 struct iattr *sattr)
2802 struct nfs4_createdata *data;
2803 int status = -ENOMEM;
2805 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2809 status = nfs4_do_create(dir, dentry, data);
2811 nfs4_free_createdata(data);
2816 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2817 struct iattr *sattr)
2819 struct nfs4_exception exception = { };
2822 sattr->ia_mode &= ~current_umask();
2824 err = nfs4_handle_exception(NFS_SERVER(dir),
2825 _nfs4_proc_mkdir(dir, dentry, sattr),
2827 } while (exception.retry);
2831 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2832 u64 cookie, struct page **pages, unsigned int count, int plus)
2834 struct inode *dir = dentry->d_inode;
2835 struct nfs4_readdir_arg args = {
2840 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2843 struct nfs4_readdir_res res;
2844 struct rpc_message msg = {
2845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2852 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2853 dentry->d_parent->d_name.name,
2854 dentry->d_name.name,
2855 (unsigned long long)cookie);
2856 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2857 res.pgbase = args.pgbase;
2858 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2860 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2861 status += args.pgbase;
2864 nfs_invalidate_atime(dir);
2866 dprintk("%s: returns %d\n", __func__, status);
2870 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2871 u64 cookie, struct page **pages, unsigned int count, int plus)
2873 struct nfs4_exception exception = { };
2876 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2877 _nfs4_proc_readdir(dentry, cred, cookie,
2878 pages, count, plus),
2880 } while (exception.retry);
2884 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2885 struct iattr *sattr, dev_t rdev)
2887 struct nfs4_createdata *data;
2888 int mode = sattr->ia_mode;
2889 int status = -ENOMEM;
2891 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2892 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2894 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2899 data->arg.ftype = NF4FIFO;
2900 else if (S_ISBLK(mode)) {
2901 data->arg.ftype = NF4BLK;
2902 data->arg.u.device.specdata1 = MAJOR(rdev);
2903 data->arg.u.device.specdata2 = MINOR(rdev);
2905 else if (S_ISCHR(mode)) {
2906 data->arg.ftype = NF4CHR;
2907 data->arg.u.device.specdata1 = MAJOR(rdev);
2908 data->arg.u.device.specdata2 = MINOR(rdev);
2911 status = nfs4_do_create(dir, dentry, data);
2913 nfs4_free_createdata(data);
2918 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2919 struct iattr *sattr, dev_t rdev)
2921 struct nfs4_exception exception = { };
2924 sattr->ia_mode &= ~current_umask();
2926 err = nfs4_handle_exception(NFS_SERVER(dir),
2927 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2929 } while (exception.retry);
2933 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2934 struct nfs_fsstat *fsstat)
2936 struct nfs4_statfs_arg args = {
2938 .bitmask = server->attr_bitmask,
2940 struct nfs4_statfs_res res = {
2943 struct rpc_message msg = {
2944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2949 nfs_fattr_init(fsstat->fattr);
2950 return nfs4_call_sync(server, &msg, &args, &res, 0);
2953 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2955 struct nfs4_exception exception = { };
2958 err = nfs4_handle_exception(server,
2959 _nfs4_proc_statfs(server, fhandle, fsstat),
2961 } while (exception.retry);
2965 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2966 struct nfs_fsinfo *fsinfo)
2968 struct nfs4_fsinfo_arg args = {
2970 .bitmask = server->attr_bitmask,
2972 struct nfs4_fsinfo_res res = {
2975 struct rpc_message msg = {
2976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2981 return nfs4_call_sync(server, &msg, &args, &res, 0);
2984 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2986 struct nfs4_exception exception = { };
2990 err = nfs4_handle_exception(server,
2991 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2993 } while (exception.retry);
2997 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2999 nfs_fattr_init(fsinfo->fattr);
3000 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3003 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3004 struct nfs_pathconf *pathconf)
3006 struct nfs4_pathconf_arg args = {
3008 .bitmask = server->attr_bitmask,
3010 struct nfs4_pathconf_res res = {
3011 .pathconf = pathconf,
3013 struct rpc_message msg = {
3014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3019 /* None of the pathconf attributes are mandatory to implement */
3020 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3021 memset(pathconf, 0, sizeof(*pathconf));
3025 nfs_fattr_init(pathconf->fattr);
3026 return nfs4_call_sync(server, &msg, &args, &res, 0);
3029 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3030 struct nfs_pathconf *pathconf)
3032 struct nfs4_exception exception = { };
3036 err = nfs4_handle_exception(server,
3037 _nfs4_proc_pathconf(server, fhandle, pathconf),
3039 } while (exception.retry);
3043 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3045 struct nfs_server *server = NFS_SERVER(data->inode);
3047 dprintk("--> %s\n", __func__);
3049 if (!nfs4_sequence_done(task, &data->res.seq_res))
3052 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3053 nfs_restart_rpc(task, server->nfs_client);
3057 nfs_invalidate_atime(data->inode);
3058 if (task->tk_status > 0)
3059 renew_lease(server, data->timestamp);
3063 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3065 data->timestamp = jiffies;
3066 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3069 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3071 struct inode *inode = data->inode;
3073 if (!nfs4_sequence_done(task, &data->res.seq_res))
3076 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3077 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3080 if (task->tk_status >= 0) {
3081 renew_lease(NFS_SERVER(inode), data->timestamp);
3082 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3087 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3089 struct nfs_server *server = NFS_SERVER(data->inode);
3091 data->args.bitmask = server->cache_consistency_bitmask;
3092 data->res.server = server;
3093 data->timestamp = jiffies;
3095 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3098 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3100 struct inode *inode = data->inode;
3102 if (!nfs4_sequence_done(task, &data->res.seq_res))
3105 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3106 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3109 nfs_refresh_inode(inode, data->res.fattr);
3113 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3115 struct nfs_server *server = NFS_SERVER(data->inode);
3117 data->args.bitmask = server->cache_consistency_bitmask;
3118 data->res.server = server;
3119 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3122 struct nfs4_renewdata {
3123 struct nfs_client *client;
3124 unsigned long timestamp;
3128 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3129 * standalone procedure for queueing an asynchronous RENEW.
3131 static void nfs4_renew_release(void *calldata)
3133 struct nfs4_renewdata *data = calldata;
3134 struct nfs_client *clp = data->client;
3136 if (atomic_read(&clp->cl_count) > 1)
3137 nfs4_schedule_state_renewal(clp);
3138 nfs_put_client(clp);
3142 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3144 struct nfs4_renewdata *data = calldata;
3145 struct nfs_client *clp = data->client;
3146 unsigned long timestamp = data->timestamp;
3148 if (task->tk_status < 0) {
3149 /* Unless we're shutting down, schedule state recovery! */
3150 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3151 nfs4_schedule_state_recovery(clp);
3154 do_renew_lease(clp, timestamp);
3157 static const struct rpc_call_ops nfs4_renew_ops = {
3158 .rpc_call_done = nfs4_renew_done,
3159 .rpc_release = nfs4_renew_release,
3162 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3164 struct rpc_message msg = {
3165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3169 struct nfs4_renewdata *data;
3171 if (!atomic_inc_not_zero(&clp->cl_count))
3173 data = kmalloc(sizeof(*data), GFP_KERNEL);
3177 data->timestamp = jiffies;
3178 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3179 &nfs4_renew_ops, data);
3182 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3184 struct rpc_message msg = {
3185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3189 unsigned long now = jiffies;
3192 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3195 do_renew_lease(clp, now);
3199 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3201 return (server->caps & NFS_CAP_ACLS)
3202 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3203 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3206 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3207 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3210 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3212 static void buf_to_pages(const void *buf, size_t buflen,
3213 struct page **pages, unsigned int *pgbase)
3215 const void *p = buf;
3217 *pgbase = offset_in_page(buf);
3219 while (p < buf + buflen) {
3220 *(pages++) = virt_to_page(p);
3221 p += PAGE_CACHE_SIZE;
3225 struct nfs4_cached_acl {
3231 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3233 struct nfs_inode *nfsi = NFS_I(inode);
3235 spin_lock(&inode->i_lock);
3236 kfree(nfsi->nfs4_acl);
3237 nfsi->nfs4_acl = acl;
3238 spin_unlock(&inode->i_lock);
3241 static void nfs4_zap_acl_attr(struct inode *inode)
3243 nfs4_set_cached_acl(inode, NULL);
3246 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3248 struct nfs_inode *nfsi = NFS_I(inode);
3249 struct nfs4_cached_acl *acl;
3252 spin_lock(&inode->i_lock);
3253 acl = nfsi->nfs4_acl;
3256 if (buf == NULL) /* user is just asking for length */
3258 if (acl->cached == 0)
3260 ret = -ERANGE; /* see getxattr(2) man page */
3261 if (acl->len > buflen)
3263 memcpy(buf, acl->data, acl->len);
3267 spin_unlock(&inode->i_lock);
3271 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3273 struct nfs4_cached_acl *acl;
3275 if (buf && acl_len <= PAGE_SIZE) {
3276 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3280 memcpy(acl->data, buf, acl_len);
3282 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3289 nfs4_set_cached_acl(inode, acl);
3292 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3294 struct page *pages[NFS4ACL_MAXPAGES];
3295 struct nfs_getaclargs args = {
3296 .fh = NFS_FH(inode),
3300 struct nfs_getaclres res = {
3304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3309 struct page *localpage = NULL;
3312 if (buflen < PAGE_SIZE) {
3313 /* As long as we're doing a round trip to the server anyway,
3314 * let's be prepared for a page of acl data. */
3315 localpage = alloc_page(GFP_KERNEL);
3316 resp_buf = page_address(localpage);
3317 if (localpage == NULL)
3319 args.acl_pages[0] = localpage;
3320 args.acl_pgbase = 0;
3321 args.acl_len = PAGE_SIZE;
3324 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3326 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3329 if (res.acl_len > args.acl_len)
3330 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3332 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3335 if (res.acl_len > buflen)
3338 memcpy(buf, resp_buf, res.acl_len);
3343 __free_page(localpage);
3347 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3349 struct nfs4_exception exception = { };
3352 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3355 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3356 } while (exception.retry);
3360 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3362 struct nfs_server *server = NFS_SERVER(inode);
3365 if (!nfs4_server_supports_acls(server))
3367 ret = nfs_revalidate_inode(server, inode);
3370 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3371 nfs_zap_acl_cache(inode);
3372 ret = nfs4_read_cached_acl(inode, buf, buflen);
3375 return nfs4_get_acl_uncached(inode, buf, buflen);
3378 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3380 struct nfs_server *server = NFS_SERVER(inode);
3381 struct page *pages[NFS4ACL_MAXPAGES];
3382 struct nfs_setaclargs arg = {
3383 .fh = NFS_FH(inode),
3387 struct nfs_setaclres res;
3388 struct rpc_message msg = {
3389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3395 if (!nfs4_server_supports_acls(server))
3397 nfs_inode_return_delegation(inode);
3398 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3399 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3401 * Acl update can result in inode attribute update.
3402 * so mark the attribute cache invalid.
3404 spin_lock(&inode->i_lock);
3405 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3406 spin_unlock(&inode->i_lock);
3407 nfs_access_zap_cache(inode);
3408 nfs_zap_acl_cache(inode);
3412 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3414 struct nfs4_exception exception = { };
3417 err = nfs4_handle_exception(NFS_SERVER(inode),
3418 __nfs4_proc_set_acl(inode, buf, buflen),
3420 } while (exception.retry);
3425 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3427 struct nfs_client *clp = server->nfs_client;
3429 if (task->tk_status >= 0)
3431 switch(task->tk_status) {
3432 case -NFS4ERR_ADMIN_REVOKED:
3433 case -NFS4ERR_BAD_STATEID:
3434 case -NFS4ERR_OPENMODE:
3437 nfs4_state_mark_reclaim_nograce(clp, state);
3438 goto do_state_recovery;
3439 case -NFS4ERR_STALE_STATEID:
3440 case -NFS4ERR_STALE_CLIENTID:
3441 case -NFS4ERR_EXPIRED:
3442 goto do_state_recovery;
3443 #if defined(CONFIG_NFS_V4_1)
3444 case -NFS4ERR_BADSESSION:
3445 case -NFS4ERR_BADSLOT:
3446 case -NFS4ERR_BAD_HIGH_SLOT:
3447 case -NFS4ERR_DEADSESSION:
3448 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3449 case -NFS4ERR_SEQ_FALSE_RETRY:
3450 case -NFS4ERR_SEQ_MISORDERED:
3451 dprintk("%s ERROR %d, Reset session\n", __func__,
3453 nfs4_schedule_state_recovery(clp);
3454 task->tk_status = 0;
3456 #endif /* CONFIG_NFS_V4_1 */
3457 case -NFS4ERR_DELAY:
3458 nfs_inc_server_stats(server, NFSIOS_DELAY);
3459 case -NFS4ERR_GRACE:
3461 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3462 task->tk_status = 0;
3464 case -NFS4ERR_OLD_STATEID:
3465 task->tk_status = 0;
3468 task->tk_status = nfs4_map_errors(task->tk_status);
3471 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3472 nfs4_schedule_state_recovery(clp);
3473 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3474 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3475 task->tk_status = 0;
3479 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3480 unsigned short port, struct rpc_cred *cred,
3481 struct nfs4_setclientid_res *res)
3483 nfs4_verifier sc_verifier;
3484 struct nfs4_setclientid setclientid = {
3485 .sc_verifier = &sc_verifier,
3487 .sc_cb_ident = clp->cl_cb_ident,
3489 struct rpc_message msg = {
3490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3491 .rpc_argp = &setclientid,
3499 p = (__be32*)sc_verifier.data;
3500 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3501 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3504 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3505 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3507 rpc_peeraddr2str(clp->cl_rpcclient,
3509 rpc_peeraddr2str(clp->cl_rpcclient,
3511 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3512 clp->cl_id_uniquifier);
3513 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3514 sizeof(setclientid.sc_netid),
3515 rpc_peeraddr2str(clp->cl_rpcclient,
3516 RPC_DISPLAY_NETID));
3517 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3518 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3519 clp->cl_ipaddr, port >> 8, port & 255);
3521 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3522 if (status != -NFS4ERR_CLID_INUSE)
3527 ssleep(clp->cl_lease_time / HZ + 1);
3529 if (++clp->cl_id_uniquifier == 0)
3535 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3536 struct nfs4_setclientid_res *arg,
3537 struct rpc_cred *cred)
3539 struct nfs_fsinfo fsinfo;
3540 struct rpc_message msg = {
3541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3543 .rpc_resp = &fsinfo,
3550 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3552 spin_lock(&clp->cl_lock);
3553 clp->cl_lease_time = fsinfo.lease_time * HZ;
3554 clp->cl_last_renewal = now;
3555 spin_unlock(&clp->cl_lock);
3560 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3561 struct nfs4_setclientid_res *arg,
3562 struct rpc_cred *cred)
3567 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3571 case -NFS4ERR_RESOURCE:
3572 /* The IBM lawyers misread another document! */
3573 case -NFS4ERR_DELAY:
3574 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3580 struct nfs4_delegreturndata {
3581 struct nfs4_delegreturnargs args;
3582 struct nfs4_delegreturnres res;
3584 nfs4_stateid stateid;
3585 unsigned long timestamp;
3586 struct nfs_fattr fattr;
3590 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3592 struct nfs4_delegreturndata *data = calldata;
3594 if (!nfs4_sequence_done(task, &data->res.seq_res))
3597 switch (task->tk_status) {
3598 case -NFS4ERR_STALE_STATEID:
3599 case -NFS4ERR_EXPIRED:
3601 renew_lease(data->res.server, data->timestamp);
3604 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3606 nfs_restart_rpc(task, data->res.server->nfs_client);
3610 data->rpc_status = task->tk_status;
3613 static void nfs4_delegreturn_release(void *calldata)
3618 #if defined(CONFIG_NFS_V4_1)
3619 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3621 struct nfs4_delegreturndata *d_data;
3623 d_data = (struct nfs4_delegreturndata *)data;
3625 if (nfs4_setup_sequence(d_data->res.server,
3626 &d_data->args.seq_args,
3627 &d_data->res.seq_res, 1, task))
3629 rpc_call_start(task);
3631 #endif /* CONFIG_NFS_V4_1 */
3633 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3634 #if defined(CONFIG_NFS_V4_1)
3635 .rpc_call_prepare = nfs4_delegreturn_prepare,
3636 #endif /* CONFIG_NFS_V4_1 */
3637 .rpc_call_done = nfs4_delegreturn_done,
3638 .rpc_release = nfs4_delegreturn_release,
3641 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3643 struct nfs4_delegreturndata *data;
3644 struct nfs_server *server = NFS_SERVER(inode);
3645 struct rpc_task *task;
3646 struct rpc_message msg = {
3647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3650 struct rpc_task_setup task_setup_data = {
3651 .rpc_client = server->client,
3652 .rpc_message = &msg,
3653 .callback_ops = &nfs4_delegreturn_ops,
3654 .flags = RPC_TASK_ASYNC,
3658 data = kzalloc(sizeof(*data), GFP_NOFS);
3661 data->args.fhandle = &data->fh;
3662 data->args.stateid = &data->stateid;
3663 data->args.bitmask = server->attr_bitmask;
3664 nfs_copy_fh(&data->fh, NFS_FH(inode));
3665 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3666 data->res.fattr = &data->fattr;
3667 data->res.server = server;
3668 nfs_fattr_init(data->res.fattr);
3669 data->timestamp = jiffies;
3670 data->rpc_status = 0;
3672 task_setup_data.callback_data = data;
3673 msg.rpc_argp = &data->args;
3674 msg.rpc_resp = &data->res;
3675 task = rpc_run_task(&task_setup_data);
3677 return PTR_ERR(task);
3680 status = nfs4_wait_for_completion_rpc_task(task);
3683 status = data->rpc_status;
3686 nfs_refresh_inode(inode, &data->fattr);
3692 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3694 struct nfs_server *server = NFS_SERVER(inode);
3695 struct nfs4_exception exception = { };
3698 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3700 case -NFS4ERR_STALE_STATEID:
3701 case -NFS4ERR_EXPIRED:
3705 err = nfs4_handle_exception(server, err, &exception);
3706 } while (exception.retry);
3710 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3711 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3714 * sleep, with exponential backoff, and retry the LOCK operation.
3716 static unsigned long
3717 nfs4_set_lock_task_retry(unsigned long timeout)
3719 schedule_timeout_killable(timeout);
3721 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3722 return NFS4_LOCK_MAXTIMEOUT;
3726 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3728 struct inode *inode = state->inode;
3729 struct nfs_server *server = NFS_SERVER(inode);
3730 struct nfs_client *clp = server->nfs_client;
3731 struct nfs_lockt_args arg = {
3732 .fh = NFS_FH(inode),
3735 struct nfs_lockt_res res = {
3738 struct rpc_message msg = {
3739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3742 .rpc_cred = state->owner->so_cred,
3744 struct nfs4_lock_state *lsp;
3747 arg.lock_owner.clientid = clp->cl_clientid;
3748 status = nfs4_set_lock_state(state, request);
3751 lsp = request->fl_u.nfs4_fl.owner;
3752 arg.lock_owner.id = lsp->ls_id.id;
3753 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3756 request->fl_type = F_UNLCK;
3758 case -NFS4ERR_DENIED:
3761 request->fl_ops->fl_release_private(request);
3766 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3768 struct nfs4_exception exception = { };
3772 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3773 _nfs4_proc_getlk(state, cmd, request),
3775 } while (exception.retry);
3779 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3782 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3784 res = posix_lock_file_wait(file, fl);
3787 res = flock_lock_file_wait(file, fl);
3795 struct nfs4_unlockdata {
3796 struct nfs_locku_args arg;
3797 struct nfs_locku_res res;
3798 struct nfs4_lock_state *lsp;
3799 struct nfs_open_context *ctx;
3800 struct file_lock fl;
3801 const struct nfs_server *server;
3802 unsigned long timestamp;
3805 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3806 struct nfs_open_context *ctx,
3807 struct nfs4_lock_state *lsp,
3808 struct nfs_seqid *seqid)
3810 struct nfs4_unlockdata *p;
3811 struct inode *inode = lsp->ls_state->inode;
3813 p = kzalloc(sizeof(*p), GFP_NOFS);
3816 p->arg.fh = NFS_FH(inode);
3818 p->arg.seqid = seqid;
3819 p->res.seqid = seqid;
3820 p->arg.stateid = &lsp->ls_stateid;
3822 atomic_inc(&lsp->ls_count);
3823 /* Ensure we don't close file until we're done freeing locks! */
3824 p->ctx = get_nfs_open_context(ctx);
3825 memcpy(&p->fl, fl, sizeof(p->fl));
3826 p->server = NFS_SERVER(inode);
3830 static void nfs4_locku_release_calldata(void *data)
3832 struct nfs4_unlockdata *calldata = data;
3833 nfs_free_seqid(calldata->arg.seqid);
3834 nfs4_put_lock_state(calldata->lsp);
3835 put_nfs_open_context(calldata->ctx);
3839 static void nfs4_locku_done(struct rpc_task *task, void *data)
3841 struct nfs4_unlockdata *calldata = data;
3843 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3845 switch (task->tk_status) {
3847 memcpy(calldata->lsp->ls_stateid.data,
3848 calldata->res.stateid.data,
3849 sizeof(calldata->lsp->ls_stateid.data));
3850 renew_lease(calldata->server, calldata->timestamp);
3852 case -NFS4ERR_BAD_STATEID:
3853 case -NFS4ERR_OLD_STATEID:
3854 case -NFS4ERR_STALE_STATEID:
3855 case -NFS4ERR_EXPIRED:
3858 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3859 nfs_restart_rpc(task,
3860 calldata->server->nfs_client);
3864 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3866 struct nfs4_unlockdata *calldata = data;
3868 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3870 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3871 /* Note: exit _without_ running nfs4_locku_done */
3872 task->tk_action = NULL;
3875 calldata->timestamp = jiffies;
3876 if (nfs4_setup_sequence(calldata->server,
3877 &calldata->arg.seq_args,
3878 &calldata->res.seq_res, 1, task))
3880 rpc_call_start(task);
3883 static const struct rpc_call_ops nfs4_locku_ops = {
3884 .rpc_call_prepare = nfs4_locku_prepare,
3885 .rpc_call_done = nfs4_locku_done,
3886 .rpc_release = nfs4_locku_release_calldata,
3889 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3890 struct nfs_open_context *ctx,
3891 struct nfs4_lock_state *lsp,
3892 struct nfs_seqid *seqid)
3894 struct nfs4_unlockdata *data;
3895 struct rpc_message msg = {
3896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3897 .rpc_cred = ctx->cred,
3899 struct rpc_task_setup task_setup_data = {
3900 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3901 .rpc_message = &msg,
3902 .callback_ops = &nfs4_locku_ops,
3903 .workqueue = nfsiod_workqueue,
3904 .flags = RPC_TASK_ASYNC,
3907 /* Ensure this is an unlock - when canceling a lock, the
3908 * canceled lock is passed in, and it won't be an unlock.
3910 fl->fl_type = F_UNLCK;
3912 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3914 nfs_free_seqid(seqid);
3915 return ERR_PTR(-ENOMEM);
3918 msg.rpc_argp = &data->arg;
3919 msg.rpc_resp = &data->res;
3920 task_setup_data.callback_data = data;
3921 return rpc_run_task(&task_setup_data);
3924 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3926 struct nfs_inode *nfsi = NFS_I(state->inode);
3927 struct nfs_seqid *seqid;
3928 struct nfs4_lock_state *lsp;
3929 struct rpc_task *task;
3931 unsigned char fl_flags = request->fl_flags;
3933 status = nfs4_set_lock_state(state, request);
3934 /* Unlock _before_ we do the RPC call */
3935 request->fl_flags |= FL_EXISTS;
3936 down_read(&nfsi->rwsem);
3937 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3938 up_read(&nfsi->rwsem);
3941 up_read(&nfsi->rwsem);
3944 /* Is this a delegated lock? */
3945 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3947 lsp = request->fl_u.nfs4_fl.owner;
3948 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
3952 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3953 status = PTR_ERR(task);
3956 status = nfs4_wait_for_completion_rpc_task(task);
3959 request->fl_flags = fl_flags;
3963 struct nfs4_lockdata {
3964 struct nfs_lock_args arg;
3965 struct nfs_lock_res res;
3966 struct nfs4_lock_state *lsp;
3967 struct nfs_open_context *ctx;
3968 struct file_lock fl;
3969 unsigned long timestamp;
3972 struct nfs_server *server;
3975 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3976 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3979 struct nfs4_lockdata *p;
3980 struct inode *inode = lsp->ls_state->inode;
3981 struct nfs_server *server = NFS_SERVER(inode);
3983 p = kzalloc(sizeof(*p), gfp_mask);
3987 p->arg.fh = NFS_FH(inode);
3989 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
3990 if (p->arg.open_seqid == NULL)
3992 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
3993 if (p->arg.lock_seqid == NULL)
3994 goto out_free_seqid;
3995 p->arg.lock_stateid = &lsp->ls_stateid;
3996 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3997 p->arg.lock_owner.id = lsp->ls_id.id;
3998 p->res.lock_seqid = p->arg.lock_seqid;
4001 atomic_inc(&lsp->ls_count);
4002 p->ctx = get_nfs_open_context(ctx);
4003 memcpy(&p->fl, fl, sizeof(p->fl));
4006 nfs_free_seqid(p->arg.open_seqid);
4012 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4014 struct nfs4_lockdata *data = calldata;
4015 struct nfs4_state *state = data->lsp->ls_state;
4017 dprintk("%s: begin!\n", __func__);
4018 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4020 /* Do we need to do an open_to_lock_owner? */
4021 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4022 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4024 data->arg.open_stateid = &state->stateid;
4025 data->arg.new_lock_owner = 1;
4026 data->res.open_seqid = data->arg.open_seqid;
4028 data->arg.new_lock_owner = 0;
4029 data->timestamp = jiffies;
4030 if (nfs4_setup_sequence(data->server,
4031 &data->arg.seq_args,
4032 &data->res.seq_res, 1, task))
4034 rpc_call_start(task);
4035 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4038 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4040 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4041 nfs4_lock_prepare(task, calldata);
4044 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4046 struct nfs4_lockdata *data = calldata;
4048 dprintk("%s: begin!\n", __func__);
4050 if (!nfs4_sequence_done(task, &data->res.seq_res))
4053 data->rpc_status = task->tk_status;
4054 if (data->arg.new_lock_owner != 0) {
4055 if (data->rpc_status == 0)
4056 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4060 if (data->rpc_status == 0) {
4061 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4062 sizeof(data->lsp->ls_stateid.data));
4063 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4064 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4067 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4070 static void nfs4_lock_release(void *calldata)
4072 struct nfs4_lockdata *data = calldata;
4074 dprintk("%s: begin!\n", __func__);
4075 nfs_free_seqid(data->arg.open_seqid);
4076 if (data->cancelled != 0) {
4077 struct rpc_task *task;
4078 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4079 data->arg.lock_seqid);
4082 dprintk("%s: cancelling lock!\n", __func__);
4084 nfs_free_seqid(data->arg.lock_seqid);
4085 nfs4_put_lock_state(data->lsp);
4086 put_nfs_open_context(data->ctx);
4088 dprintk("%s: done!\n", __func__);
4091 static const struct rpc_call_ops nfs4_lock_ops = {
4092 .rpc_call_prepare = nfs4_lock_prepare,
4093 .rpc_call_done = nfs4_lock_done,
4094 .rpc_release = nfs4_lock_release,
4097 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4098 .rpc_call_prepare = nfs4_recover_lock_prepare,
4099 .rpc_call_done = nfs4_lock_done,
4100 .rpc_release = nfs4_lock_release,
4103 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4105 struct nfs_client *clp = server->nfs_client;
4106 struct nfs4_state *state = lsp->ls_state;
4109 case -NFS4ERR_ADMIN_REVOKED:
4110 case -NFS4ERR_BAD_STATEID:
4111 case -NFS4ERR_EXPIRED:
4112 if (new_lock_owner != 0 ||
4113 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4114 nfs4_state_mark_reclaim_nograce(clp, state);
4115 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4117 case -NFS4ERR_STALE_STATEID:
4118 if (new_lock_owner != 0 ||
4119 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4120 nfs4_state_mark_reclaim_reboot(clp, state);
4121 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4125 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4127 struct nfs4_lockdata *data;
4128 struct rpc_task *task;
4129 struct rpc_message msg = {
4130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4131 .rpc_cred = state->owner->so_cred,
4133 struct rpc_task_setup task_setup_data = {
4134 .rpc_client = NFS_CLIENT(state->inode),
4135 .rpc_message = &msg,
4136 .callback_ops = &nfs4_lock_ops,
4137 .workqueue = nfsiod_workqueue,
4138 .flags = RPC_TASK_ASYNC,
4142 dprintk("%s: begin!\n", __func__);
4143 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4144 fl->fl_u.nfs4_fl.owner,
4145 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4149 data->arg.block = 1;
4150 if (recovery_type > NFS_LOCK_NEW) {
4151 if (recovery_type == NFS_LOCK_RECLAIM)
4152 data->arg.reclaim = NFS_LOCK_RECLAIM;
4153 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4155 msg.rpc_argp = &data->arg;
4156 msg.rpc_resp = &data->res;
4157 task_setup_data.callback_data = data;
4158 task = rpc_run_task(&task_setup_data);
4160 return PTR_ERR(task);
4161 ret = nfs4_wait_for_completion_rpc_task(task);
4163 ret = data->rpc_status;
4165 nfs4_handle_setlk_error(data->server, data->lsp,
4166 data->arg.new_lock_owner, ret);
4168 data->cancelled = 1;
4170 dprintk("%s: done, ret = %d!\n", __func__, ret);
4174 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4176 struct nfs_server *server = NFS_SERVER(state->inode);
4177 struct nfs4_exception exception = { };
4181 /* Cache the lock if possible... */
4182 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4184 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4185 if (err != -NFS4ERR_DELAY)
4187 nfs4_handle_exception(server, err, &exception);
4188 } while (exception.retry);
4192 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4194 struct nfs_server *server = NFS_SERVER(state->inode);
4195 struct nfs4_exception exception = { };
4198 err = nfs4_set_lock_state(state, request);
4202 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4204 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4208 case -NFS4ERR_GRACE:
4209 case -NFS4ERR_DELAY:
4210 nfs4_handle_exception(server, err, &exception);
4213 } while (exception.retry);
4218 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4220 struct nfs_inode *nfsi = NFS_I(state->inode);
4221 unsigned char fl_flags = request->fl_flags;
4222 int status = -ENOLCK;
4224 if ((fl_flags & FL_POSIX) &&
4225 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4227 /* Is this a delegated open? */
4228 status = nfs4_set_lock_state(state, request);
4231 request->fl_flags |= FL_ACCESS;
4232 status = do_vfs_lock(request->fl_file, request);
4235 down_read(&nfsi->rwsem);
4236 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4237 /* Yes: cache locks! */
4238 /* ...but avoid races with delegation recall... */
4239 request->fl_flags = fl_flags & ~FL_SLEEP;
4240 status = do_vfs_lock(request->fl_file, request);
4243 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4246 /* Note: we always want to sleep here! */
4247 request->fl_flags = fl_flags | FL_SLEEP;
4248 if (do_vfs_lock(request->fl_file, request) < 0)
4249 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4251 up_read(&nfsi->rwsem);
4253 request->fl_flags = fl_flags;
4257 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4259 struct nfs4_exception exception = { };
4263 err = _nfs4_proc_setlk(state, cmd, request);
4264 if (err == -NFS4ERR_DENIED)
4266 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4268 } while (exception.retry);
4273 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4275 struct nfs_open_context *ctx;
4276 struct nfs4_state *state;
4277 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4280 /* verify open state */
4281 ctx = nfs_file_open_context(filp);
4284 if (request->fl_start < 0 || request->fl_end < 0)
4287 if (IS_GETLK(cmd)) {
4289 return nfs4_proc_getlk(state, F_GETLK, request);
4293 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4296 if (request->fl_type == F_UNLCK) {
4298 return nfs4_proc_unlck(state, cmd, request);
4305 status = nfs4_proc_setlk(state, cmd, request);
4306 if ((status != -EAGAIN) || IS_SETLK(cmd))
4308 timeout = nfs4_set_lock_task_retry(timeout);
4309 status = -ERESTARTSYS;
4312 } while(status < 0);
4316 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4318 struct nfs_server *server = NFS_SERVER(state->inode);
4319 struct nfs4_exception exception = { };
4322 err = nfs4_set_lock_state(state, fl);
4326 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4329 printk(KERN_ERR "%s: unhandled error %d.\n",
4334 case -NFS4ERR_EXPIRED:
4335 case -NFS4ERR_STALE_CLIENTID:
4336 case -NFS4ERR_STALE_STATEID:
4337 case -NFS4ERR_BADSESSION:
4338 case -NFS4ERR_BADSLOT:
4339 case -NFS4ERR_BAD_HIGH_SLOT:
4340 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4341 case -NFS4ERR_DEADSESSION:
4342 nfs4_schedule_state_recovery(server->nfs_client);
4346 * The show must go on: exit, but mark the
4347 * stateid as needing recovery.
4349 case -NFS4ERR_ADMIN_REVOKED:
4350 case -NFS4ERR_BAD_STATEID:
4351 case -NFS4ERR_OPENMODE:
4352 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4357 * User RPCSEC_GSS context has expired.
4358 * We cannot recover this stateid now, so
4359 * skip it and allow recovery thread to
4365 case -NFS4ERR_DENIED:
4366 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4369 case -NFS4ERR_DELAY:
4372 err = nfs4_handle_exception(server, err, &exception);
4373 } while (exception.retry);
4378 static void nfs4_release_lockowner_release(void *calldata)
4383 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4384 .rpc_release = nfs4_release_lockowner_release,
4387 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4389 struct nfs_server *server = lsp->ls_state->owner->so_server;
4390 struct nfs_release_lockowner_args *args;
4391 struct rpc_message msg = {
4392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4395 if (server->nfs_client->cl_mvops->minor_version != 0)
4397 args = kmalloc(sizeof(*args), GFP_NOFS);
4400 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4401 args->lock_owner.id = lsp->ls_id.id;
4402 msg.rpc_argp = args;
4403 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4406 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4408 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4409 const void *buf, size_t buflen,
4410 int flags, int type)
4412 if (strcmp(key, "") != 0)
4415 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4418 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4419 void *buf, size_t buflen, int type)
4421 if (strcmp(key, "") != 0)
4424 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4427 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4428 size_t list_len, const char *name,
4429 size_t name_len, int type)
4431 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4433 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4436 if (list && len <= list_len)
4437 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4441 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4443 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4444 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4445 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4448 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4449 NFS_ATTR_FATTR_NLINK;
4450 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4454 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4455 struct nfs4_fs_locations *fs_locations, struct page *page)
4457 struct nfs_server *server = NFS_SERVER(dir);
4459 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4460 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4462 struct nfs4_fs_locations_arg args = {
4463 .dir_fh = NFS_FH(dir),
4468 struct nfs4_fs_locations_res res = {
4469 .fs_locations = fs_locations,
4471 struct rpc_message msg = {
4472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4478 dprintk("%s: start\n", __func__);
4479 nfs_fattr_init(&fs_locations->fattr);
4480 fs_locations->server = server;
4481 fs_locations->nlocations = 0;
4482 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4483 nfs_fixup_referral_attributes(&fs_locations->fattr);
4484 dprintk("%s: returned status = %d\n", __func__, status);
4488 #ifdef CONFIG_NFS_V4_1
4490 * nfs4_proc_exchange_id()
4492 * Since the clientid has expired, all compounds using sessions
4493 * associated with the stale clientid will be returning
4494 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4495 * be in some phase of session reset.
4497 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4499 nfs4_verifier verifier;
4500 struct nfs41_exchange_id_args args = {
4502 .flags = clp->cl_exchange_flags,
4504 struct nfs41_exchange_id_res res = {
4508 struct rpc_message msg = {
4509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4516 dprintk("--> %s\n", __func__);
4517 BUG_ON(clp == NULL);
4519 /* Remove server-only flags */
4520 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4522 p = (u32 *)verifier.data;
4523 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4524 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4525 args.verifier = &verifier;
4528 args.id_len = scnprintf(args.id, sizeof(args.id),
4531 rpc_peeraddr2str(clp->cl_rpcclient,
4533 clp->cl_id_uniquifier);
4535 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4537 if (status != -NFS4ERR_CLID_INUSE)
4543 if (++clp->cl_id_uniquifier == 0)
4547 dprintk("<-- %s status= %d\n", __func__, status);
4551 struct nfs4_get_lease_time_data {
4552 struct nfs4_get_lease_time_args *args;
4553 struct nfs4_get_lease_time_res *res;
4554 struct nfs_client *clp;
4557 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4561 struct nfs4_get_lease_time_data *data =
4562 (struct nfs4_get_lease_time_data *)calldata;
4564 dprintk("--> %s\n", __func__);
4565 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4566 /* just setup sequence, do not trigger session recovery
4567 since we're invoked within one */
4568 ret = nfs41_setup_sequence(data->clp->cl_session,
4569 &data->args->la_seq_args,
4570 &data->res->lr_seq_res, 0, task);
4572 BUG_ON(ret == -EAGAIN);
4573 rpc_call_start(task);
4574 dprintk("<-- %s\n", __func__);
4578 * Called from nfs4_state_manager thread for session setup, so don't recover
4579 * from sequence operation or clientid errors.
4581 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4583 struct nfs4_get_lease_time_data *data =
4584 (struct nfs4_get_lease_time_data *)calldata;
4586 dprintk("--> %s\n", __func__);
4587 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4589 switch (task->tk_status) {
4590 case -NFS4ERR_DELAY:
4591 case -NFS4ERR_GRACE:
4592 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4593 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4594 task->tk_status = 0;
4595 nfs_restart_rpc(task, data->clp);
4598 dprintk("<-- %s\n", __func__);
4601 struct rpc_call_ops nfs4_get_lease_time_ops = {
4602 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4603 .rpc_call_done = nfs4_get_lease_time_done,
4606 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4608 struct rpc_task *task;
4609 struct nfs4_get_lease_time_args args;
4610 struct nfs4_get_lease_time_res res = {
4611 .lr_fsinfo = fsinfo,
4613 struct nfs4_get_lease_time_data data = {
4618 struct rpc_message msg = {
4619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4623 struct rpc_task_setup task_setup = {
4624 .rpc_client = clp->cl_rpcclient,
4625 .rpc_message = &msg,
4626 .callback_ops = &nfs4_get_lease_time_ops,
4627 .callback_data = &data
4631 dprintk("--> %s\n", __func__);
4632 task = rpc_run_task(&task_setup);
4635 status = PTR_ERR(task);
4637 status = task->tk_status;
4640 dprintk("<-- %s return %d\n", __func__, status);
4646 * Reset a slot table
4648 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4651 struct nfs4_slot *new = NULL;
4655 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4656 max_reqs, tbl->max_slots);
4658 /* Does the newly negotiated max_reqs match the existing slot table? */
4659 if (max_reqs != tbl->max_slots) {
4661 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4668 spin_lock(&tbl->slot_tbl_lock);
4671 tbl->max_slots = max_reqs;
4673 for (i = 0; i < tbl->max_slots; ++i)
4674 tbl->slots[i].seq_nr = ivalue;
4675 spin_unlock(&tbl->slot_tbl_lock);
4676 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4677 tbl, tbl->slots, tbl->max_slots);
4679 dprintk("<-- %s: return %d\n", __func__, ret);
4684 * Reset the forechannel and backchannel slot tables
4686 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4690 status = nfs4_reset_slot_table(&session->fc_slot_table,
4691 session->fc_attrs.max_reqs, 1);
4695 status = nfs4_reset_slot_table(&session->bc_slot_table,
4696 session->bc_attrs.max_reqs, 0);
4700 /* Destroy the slot table */
4701 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4703 if (session->fc_slot_table.slots != NULL) {
4704 kfree(session->fc_slot_table.slots);
4705 session->fc_slot_table.slots = NULL;
4707 if (session->bc_slot_table.slots != NULL) {
4708 kfree(session->bc_slot_table.slots);
4709 session->bc_slot_table.slots = NULL;
4715 * Initialize slot table
4717 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4718 int max_slots, int ivalue)
4720 struct nfs4_slot *slot;
4723 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4725 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4727 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4732 spin_lock(&tbl->slot_tbl_lock);
4733 tbl->max_slots = max_slots;
4735 tbl->highest_used_slotid = -1; /* no slot is currently used */
4736 spin_unlock(&tbl->slot_tbl_lock);
4737 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4738 tbl, tbl->slots, tbl->max_slots);
4740 dprintk("<-- %s: return %d\n", __func__, ret);
4745 * Initialize the forechannel and backchannel tables
4747 static int nfs4_init_slot_tables(struct nfs4_session *session)
4749 struct nfs4_slot_table *tbl;
4752 tbl = &session->fc_slot_table;
4753 if (tbl->slots == NULL) {
4754 status = nfs4_init_slot_table(tbl,
4755 session->fc_attrs.max_reqs, 1);
4760 tbl = &session->bc_slot_table;
4761 if (tbl->slots == NULL) {
4762 status = nfs4_init_slot_table(tbl,
4763 session->bc_attrs.max_reqs, 0);
4765 nfs4_destroy_slot_tables(session);
4771 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4773 struct nfs4_session *session;
4774 struct nfs4_slot_table *tbl;
4776 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
4780 init_completion(&session->complete);
4782 tbl = &session->fc_slot_table;
4783 tbl->highest_used_slotid = -1;
4784 spin_lock_init(&tbl->slot_tbl_lock);
4785 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4787 tbl = &session->bc_slot_table;
4788 tbl->highest_used_slotid = -1;
4789 spin_lock_init(&tbl->slot_tbl_lock);
4790 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4792 session->session_state = 1<<NFS4_SESSION_INITING;
4798 void nfs4_destroy_session(struct nfs4_session *session)
4800 nfs4_proc_destroy_session(session);
4801 dprintk("%s Destroy backchannel for xprt %p\n",
4802 __func__, session->clp->cl_rpcclient->cl_xprt);
4803 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4804 NFS41_BC_MIN_CALLBACKS);
4805 nfs4_destroy_slot_tables(session);
4810 * Initialize the values to be used by the client in CREATE_SESSION
4811 * If nfs4_init_session set the fore channel request and response sizes,
4814 * Set the back channel max_resp_sz_cached to zero to force the client to
4815 * always set csa_cachethis to FALSE because the current implementation
4816 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4818 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4820 struct nfs4_session *session = args->client->cl_session;
4821 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4822 mxresp_sz = session->fc_attrs.max_resp_sz;
4825 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4827 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4828 /* Fore channel attributes */
4829 args->fc_attrs.headerpadsz = 0;
4830 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4831 args->fc_attrs.max_resp_sz = mxresp_sz;
4832 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4833 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4835 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4836 "max_ops=%u max_reqs=%u\n",
4838 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4839 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
4841 /* Back channel attributes */
4842 args->bc_attrs.headerpadsz = 0;
4843 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4844 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4845 args->bc_attrs.max_resp_sz_cached = 0;
4846 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4847 args->bc_attrs.max_reqs = 1;
4849 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4850 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4852 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4853 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4854 args->bc_attrs.max_reqs);
4857 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4859 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4860 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4862 if (rcvd->headerpadsz > sent->headerpadsz)
4864 if (rcvd->max_resp_sz > sent->max_resp_sz)
4867 * Our requested max_ops is the minimum we need; we're not
4868 * prepared to break up compounds into smaller pieces than that.
4869 * So, no point even trying to continue if the server won't
4872 if (rcvd->max_ops < sent->max_ops)
4874 if (rcvd->max_reqs == 0)
4879 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4881 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4882 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
4884 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4886 if (rcvd->max_resp_sz < sent->max_resp_sz)
4888 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
4890 /* These would render the backchannel useless: */
4891 if (rcvd->max_ops == 0)
4893 if (rcvd->max_reqs == 0)
4898 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4899 struct nfs4_session *session)
4903 ret = nfs4_verify_fore_channel_attrs(args, session);
4906 return nfs4_verify_back_channel_attrs(args, session);
4909 static int _nfs4_proc_create_session(struct nfs_client *clp)
4911 struct nfs4_session *session = clp->cl_session;
4912 struct nfs41_create_session_args args = {
4914 .cb_program = NFS4_CALLBACK,
4916 struct nfs41_create_session_res res = {
4919 struct rpc_message msg = {
4920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4926 nfs4_init_channel_attrs(&args);
4927 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4929 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4932 /* Verify the session's negotiated channel_attrs values */
4933 status = nfs4_verify_channel_attrs(&args, session);
4935 /* Increment the clientid slot sequence id */
4943 * Issues a CREATE_SESSION operation to the server.
4944 * It is the responsibility of the caller to verify the session is
4945 * expired before calling this routine.
4947 int nfs4_proc_create_session(struct nfs_client *clp)
4951 struct nfs4_session *session = clp->cl_session;
4953 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4955 status = _nfs4_proc_create_session(clp);
4959 /* Init and reset the fore channel */
4960 status = nfs4_init_slot_tables(session);
4961 dprintk("slot table initialization returned %d\n", status);
4964 status = nfs4_reset_slot_tables(session);
4965 dprintk("slot table reset returned %d\n", status);
4969 ptr = (unsigned *)&session->sess_id.data[0];
4970 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4971 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4973 dprintk("<-- %s\n", __func__);
4978 * Issue the over-the-wire RPC DESTROY_SESSION.
4979 * The caller must serialize access to this routine.
4981 int nfs4_proc_destroy_session(struct nfs4_session *session)
4984 struct rpc_message msg;
4986 dprintk("--> nfs4_proc_destroy_session\n");
4988 /* session is still being setup */
4989 if (session->clp->cl_cons_state != NFS_CS_READY)
4992 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4993 msg.rpc_argp = session;
4994 msg.rpc_resp = NULL;
4995 msg.rpc_cred = NULL;
4996 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5000 "Got error %d from the server on DESTROY_SESSION. "
5001 "Session has been destroyed regardless...\n", status);
5003 dprintk("<-- nfs4_proc_destroy_session\n");
5007 int nfs4_init_session(struct nfs_server *server)
5009 struct nfs_client *clp = server->nfs_client;
5010 struct nfs4_session *session;
5011 unsigned int rsize, wsize;
5014 if (!nfs4_has_session(clp))
5017 session = clp->cl_session;
5018 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5021 rsize = server->rsize;
5023 rsize = NFS_MAX_FILE_IO_SIZE;
5024 wsize = server->wsize;
5026 wsize = NFS_MAX_FILE_IO_SIZE;
5028 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5029 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5031 ret = nfs4_recover_expired_lease(server);
5033 ret = nfs4_check_client_ready(clp);
5038 * Renew the cl_session lease.
5040 struct nfs4_sequence_data {
5041 struct nfs_client *clp;
5042 struct nfs4_sequence_args args;
5043 struct nfs4_sequence_res res;
5046 static void nfs41_sequence_release(void *data)
5048 struct nfs4_sequence_data *calldata = data;
5049 struct nfs_client *clp = calldata->clp;
5051 if (atomic_read(&clp->cl_count) > 1)
5052 nfs4_schedule_state_renewal(clp);
5053 nfs_put_client(clp);
5057 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5059 switch(task->tk_status) {
5060 case -NFS4ERR_DELAY:
5061 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5064 nfs4_schedule_state_recovery(clp);
5069 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5071 struct nfs4_sequence_data *calldata = data;
5072 struct nfs_client *clp = calldata->clp;
5074 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5077 if (task->tk_status < 0) {
5078 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5079 if (atomic_read(&clp->cl_count) == 1)
5082 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5083 rpc_restart_call_prepare(task);
5087 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5089 dprintk("<-- %s\n", __func__);
5092 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5094 struct nfs4_sequence_data *calldata = data;
5095 struct nfs_client *clp = calldata->clp;
5096 struct nfs4_sequence_args *args;
5097 struct nfs4_sequence_res *res;
5099 args = task->tk_msg.rpc_argp;
5100 res = task->tk_msg.rpc_resp;
5102 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5104 rpc_call_start(task);
5107 static const struct rpc_call_ops nfs41_sequence_ops = {
5108 .rpc_call_done = nfs41_sequence_call_done,
5109 .rpc_call_prepare = nfs41_sequence_prepare,
5110 .rpc_release = nfs41_sequence_release,
5113 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5115 struct nfs4_sequence_data *calldata;
5116 struct rpc_message msg = {
5117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5120 struct rpc_task_setup task_setup_data = {
5121 .rpc_client = clp->cl_rpcclient,
5122 .rpc_message = &msg,
5123 .callback_ops = &nfs41_sequence_ops,
5124 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5127 if (!atomic_inc_not_zero(&clp->cl_count))
5128 return ERR_PTR(-EIO);
5129 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5130 if (calldata == NULL) {
5131 nfs_put_client(clp);
5132 return ERR_PTR(-ENOMEM);
5134 msg.rpc_argp = &calldata->args;
5135 msg.rpc_resp = &calldata->res;
5136 calldata->clp = clp;
5137 task_setup_data.callback_data = calldata;
5139 return rpc_run_task(&task_setup_data);
5142 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5144 struct rpc_task *task;
5147 task = _nfs41_proc_sequence(clp, cred);
5149 ret = PTR_ERR(task);
5152 dprintk("<-- %s status=%d\n", __func__, ret);
5156 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5158 struct rpc_task *task;
5161 task = _nfs41_proc_sequence(clp, cred);
5163 ret = PTR_ERR(task);
5166 ret = rpc_wait_for_completion_task(task);
5168 ret = task->tk_status;
5171 dprintk("<-- %s status=%d\n", __func__, ret);
5175 struct nfs4_reclaim_complete_data {
5176 struct nfs_client *clp;
5177 struct nfs41_reclaim_complete_args arg;
5178 struct nfs41_reclaim_complete_res res;
5181 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5183 struct nfs4_reclaim_complete_data *calldata = data;
5185 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5186 if (nfs41_setup_sequence(calldata->clp->cl_session,
5187 &calldata->arg.seq_args,
5188 &calldata->res.seq_res, 0, task))
5191 rpc_call_start(task);
5194 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5196 switch(task->tk_status) {
5198 case -NFS4ERR_COMPLETE_ALREADY:
5199 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5201 case -NFS4ERR_DELAY:
5202 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5205 nfs4_schedule_state_recovery(clp);
5210 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5212 struct nfs4_reclaim_complete_data *calldata = data;
5213 struct nfs_client *clp = calldata->clp;
5214 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5216 dprintk("--> %s\n", __func__);
5217 if (!nfs41_sequence_done(task, res))
5220 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5221 rpc_restart_call_prepare(task);
5224 dprintk("<-- %s\n", __func__);
5227 static void nfs4_free_reclaim_complete_data(void *data)
5229 struct nfs4_reclaim_complete_data *calldata = data;
5234 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5235 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5236 .rpc_call_done = nfs4_reclaim_complete_done,
5237 .rpc_release = nfs4_free_reclaim_complete_data,
5241 * Issue a global reclaim complete.
5243 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5245 struct nfs4_reclaim_complete_data *calldata;
5246 struct rpc_task *task;
5247 struct rpc_message msg = {
5248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5250 struct rpc_task_setup task_setup_data = {
5251 .rpc_client = clp->cl_rpcclient,
5252 .rpc_message = &msg,
5253 .callback_ops = &nfs4_reclaim_complete_call_ops,
5254 .flags = RPC_TASK_ASYNC,
5256 int status = -ENOMEM;
5258 dprintk("--> %s\n", __func__);
5259 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5260 if (calldata == NULL)
5262 calldata->clp = clp;
5263 calldata->arg.one_fs = 0;
5265 msg.rpc_argp = &calldata->arg;
5266 msg.rpc_resp = &calldata->res;
5267 task_setup_data.callback_data = calldata;
5268 task = rpc_run_task(&task_setup_data);
5270 status = PTR_ERR(task);
5276 dprintk("<-- %s status=%d\n", __func__, status);
5281 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5283 struct nfs4_layoutget *lgp = calldata;
5284 struct inode *ino = lgp->args.inode;
5285 struct nfs_server *server = NFS_SERVER(ino);
5287 dprintk("--> %s\n", __func__);
5288 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5289 &lgp->res.seq_res, 0, task))
5291 rpc_call_start(task);
5294 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5296 struct nfs4_layoutget *lgp = calldata;
5297 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5299 dprintk("--> %s\n", __func__);
5301 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5304 switch (task->tk_status) {
5307 case -NFS4ERR_LAYOUTTRYLATER:
5308 case -NFS4ERR_RECALLCONFLICT:
5309 task->tk_status = -NFS4ERR_DELAY;
5312 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5313 rpc_restart_call_prepare(task);
5317 lgp->status = task->tk_status;
5318 dprintk("<-- %s\n", __func__);
5321 static void nfs4_layoutget_release(void *calldata)
5323 struct nfs4_layoutget *lgp = calldata;
5325 dprintk("--> %s\n", __func__);
5326 put_layout_hdr(lgp->args.inode);
5327 if (lgp->res.layout.buf != NULL)
5328 free_page((unsigned long) lgp->res.layout.buf);
5329 put_nfs_open_context(lgp->args.ctx);
5331 dprintk("<-- %s\n", __func__);
5334 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5335 .rpc_call_prepare = nfs4_layoutget_prepare,
5336 .rpc_call_done = nfs4_layoutget_done,
5337 .rpc_release = nfs4_layoutget_release,
5340 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5342 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5343 struct rpc_task *task;
5344 struct rpc_message msg = {
5345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5346 .rpc_argp = &lgp->args,
5347 .rpc_resp = &lgp->res,
5349 struct rpc_task_setup task_setup_data = {
5350 .rpc_client = server->client,
5351 .rpc_message = &msg,
5352 .callback_ops = &nfs4_layoutget_call_ops,
5353 .callback_data = lgp,
5354 .flags = RPC_TASK_ASYNC,
5358 dprintk("--> %s\n", __func__);
5360 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5361 if (lgp->res.layout.buf == NULL) {
5362 nfs4_layoutget_release(lgp);
5366 lgp->res.seq_res.sr_slot = NULL;
5367 task = rpc_run_task(&task_setup_data);
5369 return PTR_ERR(task);
5370 status = nfs4_wait_for_completion_rpc_task(task);
5373 status = lgp->status;
5376 status = pnfs_layout_process(lgp);
5379 dprintk("<-- %s status=%d\n", __func__, status);
5384 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5386 struct nfs4_getdeviceinfo_args args = {
5389 struct nfs4_getdeviceinfo_res res = {
5392 struct rpc_message msg = {
5393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5399 dprintk("--> %s\n", __func__);
5400 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5401 dprintk("<-- %s status=%d\n", __func__, status);
5406 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5408 struct nfs4_exception exception = { };
5412 err = nfs4_handle_exception(server,
5413 _nfs4_proc_getdeviceinfo(server, pdev),
5415 } while (exception.retry);
5418 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5420 #endif /* CONFIG_NFS_V4_1 */
5422 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5423 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5424 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5425 .recover_open = nfs4_open_reclaim,
5426 .recover_lock = nfs4_lock_reclaim,
5427 .establish_clid = nfs4_init_clientid,
5428 .get_clid_cred = nfs4_get_setclientid_cred,
5431 #if defined(CONFIG_NFS_V4_1)
5432 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5433 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5434 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5435 .recover_open = nfs4_open_reclaim,
5436 .recover_lock = nfs4_lock_reclaim,
5437 .establish_clid = nfs41_init_clientid,
5438 .get_clid_cred = nfs4_get_exchange_id_cred,
5439 .reclaim_complete = nfs41_proc_reclaim_complete,
5441 #endif /* CONFIG_NFS_V4_1 */
5443 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5444 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5445 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5446 .recover_open = nfs4_open_expired,
5447 .recover_lock = nfs4_lock_expired,
5448 .establish_clid = nfs4_init_clientid,
5449 .get_clid_cred = nfs4_get_setclientid_cred,
5452 #if defined(CONFIG_NFS_V4_1)
5453 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5454 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5455 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5456 .recover_open = nfs4_open_expired,
5457 .recover_lock = nfs4_lock_expired,
5458 .establish_clid = nfs41_init_clientid,
5459 .get_clid_cred = nfs4_get_exchange_id_cred,
5461 #endif /* CONFIG_NFS_V4_1 */
5463 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5464 .sched_state_renewal = nfs4_proc_async_renew,
5465 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5466 .renew_lease = nfs4_proc_renew,
5469 #if defined(CONFIG_NFS_V4_1)
5470 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5471 .sched_state_renewal = nfs41_proc_async_sequence,
5472 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5473 .renew_lease = nfs4_proc_sequence,
5477 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5479 .call_sync = _nfs4_call_sync,
5480 .validate_stateid = nfs4_validate_delegation_stateid,
5481 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5482 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5483 .state_renewal_ops = &nfs40_state_renewal_ops,
5486 #if defined(CONFIG_NFS_V4_1)
5487 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5489 .call_sync = _nfs4_call_sync_session,
5490 .validate_stateid = nfs41_validate_delegation_stateid,
5491 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5492 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5493 .state_renewal_ops = &nfs41_state_renewal_ops,
5497 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5498 [0] = &nfs_v4_0_minor_ops,
5499 #if defined(CONFIG_NFS_V4_1)
5500 [1] = &nfs_v4_1_minor_ops,
5504 static const struct inode_operations nfs4_file_inode_operations = {
5505 .permission = nfs_permission,
5506 .getattr = nfs_getattr,
5507 .setattr = nfs_setattr,
5508 .getxattr = generic_getxattr,
5509 .setxattr = generic_setxattr,
5510 .listxattr = generic_listxattr,
5511 .removexattr = generic_removexattr,
5514 const struct nfs_rpc_ops nfs_v4_clientops = {
5515 .version = 4, /* protocol version */
5516 .dentry_ops = &nfs4_dentry_operations,
5517 .dir_inode_ops = &nfs4_dir_inode_operations,
5518 .file_inode_ops = &nfs4_file_inode_operations,
5519 .getroot = nfs4_proc_get_root,
5520 .getattr = nfs4_proc_getattr,
5521 .setattr = nfs4_proc_setattr,
5522 .lookupfh = nfs4_proc_lookupfh,
5523 .lookup = nfs4_proc_lookup,
5524 .access = nfs4_proc_access,
5525 .readlink = nfs4_proc_readlink,
5526 .create = nfs4_proc_create,
5527 .remove = nfs4_proc_remove,
5528 .unlink_setup = nfs4_proc_unlink_setup,
5529 .unlink_done = nfs4_proc_unlink_done,
5530 .rename = nfs4_proc_rename,
5531 .rename_setup = nfs4_proc_rename_setup,
5532 .rename_done = nfs4_proc_rename_done,
5533 .link = nfs4_proc_link,
5534 .symlink = nfs4_proc_symlink,
5535 .mkdir = nfs4_proc_mkdir,
5536 .rmdir = nfs4_proc_remove,
5537 .readdir = nfs4_proc_readdir,
5538 .mknod = nfs4_proc_mknod,
5539 .statfs = nfs4_proc_statfs,
5540 .fsinfo = nfs4_proc_fsinfo,
5541 .pathconf = nfs4_proc_pathconf,
5542 .set_capabilities = nfs4_server_capabilities,
5543 .decode_dirent = nfs4_decode_dirent,
5544 .read_setup = nfs4_proc_read_setup,
5545 .read_done = nfs4_read_done,
5546 .write_setup = nfs4_proc_write_setup,
5547 .write_done = nfs4_write_done,
5548 .commit_setup = nfs4_proc_commit_setup,
5549 .commit_done = nfs4_commit_done,
5550 .lock = nfs4_proc_lock,
5551 .clear_acl_cache = nfs4_zap_acl_attr,
5552 .close_context = nfs4_close_context,
5553 .open_context = nfs4_atomic_open,
5556 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5557 .prefix = XATTR_NAME_NFSV4_ACL,
5558 .list = nfs4_xattr_list_nfs4_acl,
5559 .get = nfs4_xattr_get_nfs4_acl,
5560 .set = nfs4_xattr_set_nfs4_acl,
5563 const struct xattr_handler *nfs4_xattr_handlers[] = {
5564 &nfs4_xattr_nfs4_acl_handler,