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>
54 #include "delegation.h"
59 #define NFSDBG_FACILITY NFSDBG_PROC
61 #define NFS4_POLL_RETRY_MIN (HZ/10)
62 #define NFS4_POLL_RETRY_MAX (15*HZ)
64 #define NFS4_MAX_LOOP_ON_RECOVER (10)
67 static int _nfs4_proc_open(struct nfs4_opendata *data);
68 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
69 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
70 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
71 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
73 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
77 /* Prevent leaks of NFSv4 errors into userland */
78 static int nfs4_map_errors(int err)
83 case -NFS4ERR_RESOURCE:
86 dprintk("%s could not handle NFSv4 error %d\n",
94 * This is our standard bitmap for GETATTR requests.
96 const u32 nfs4_fattr_bitmap[2] = {
101 | FATTR4_WORD0_FILEID,
103 | FATTR4_WORD1_NUMLINKS
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
113 const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
122 const u32 nfs4_pathconf_bitmap[2] = {
124 | FATTR4_WORD0_MAXNAME,
128 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
135 const u32 nfs4_fs_locations_bitmap[2] = {
137 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
143 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
154 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
155 struct nfs4_readdir_arg *readdir)
159 BUG_ON(readdir->count < 80);
161 readdir->cookie = cookie;
162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
209 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
216 nfs_wait_bit_killable, TASK_KILLABLE);
220 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
237 /* This is the error handling routine for processes that are allowed
240 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
242 struct nfs_client *clp = server->nfs_client;
243 struct nfs4_state *state = exception->state;
246 exception->retry = 0;
250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
255 nfs4_state_mark_reclaim_nograce(clp, state);
256 goto do_state_recovery;
257 case -NFS4ERR_STALE_STATEID:
260 nfs4_state_mark_reclaim_reboot(clp, state);
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 renew_lease(const struct nfs_server *server, unsigned long timestamp)
308 struct nfs_client *clp = server->nfs_client;
309 spin_lock(&clp->cl_lock);
310 if (time_before(clp->cl_last_renewal,timestamp))
311 clp->cl_last_renewal = timestamp;
312 spin_unlock(&clp->cl_lock);
315 #if defined(CONFIG_NFS_V4_1)
318 * nfs4_free_slot - free a slot and efficiently update slot table.
320 * freeing a slot is trivially done by clearing its respective bit
322 * If the freed slotid equals highest_used_slotid we want to update it
323 * so that the server would be able to size down the slot table if needed,
324 * otherwise we know that the highest_used_slotid is still in use.
325 * When updating highest_used_slotid there may be "holes" in the bitmap
326 * so we need to scan down from highest_used_slotid to 0 looking for the now
327 * highest slotid in use.
328 * If none found, highest_used_slotid is set to -1.
330 * Must be called while holding tbl->slot_tbl_lock
333 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
335 int slotid = free_slotid;
337 /* clear used bit in bitmap */
338 __clear_bit(slotid, tbl->used_slots);
340 /* update highest_used_slotid when it is freed */
341 if (slotid == tbl->highest_used_slotid) {
342 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
343 if (slotid < tbl->max_slots)
344 tbl->highest_used_slotid = slotid;
346 tbl->highest_used_slotid = -1;
348 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
349 free_slotid, tbl->highest_used_slotid);
353 * Signal state manager thread if session is drained
355 static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
357 struct rpc_task *task;
359 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
360 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
362 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
366 if (ses->fc_slot_table.highest_used_slotid != -1)
369 dprintk("%s COMPLETE: Session Drained\n", __func__);
370 complete(&ses->complete);
373 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
375 struct nfs4_slot_table *tbl;
377 tbl = &res->sr_session->fc_slot_table;
378 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
379 /* just wake up the next guy waiting since
380 * we may have not consumed a slot after all */
381 dprintk("%s: No slot\n", __func__);
385 spin_lock(&tbl->slot_tbl_lock);
386 nfs4_free_slot(tbl, res->sr_slotid);
387 nfs41_check_drain_session_complete(res->sr_session);
388 spin_unlock(&tbl->slot_tbl_lock);
389 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
392 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
394 unsigned long timestamp;
395 struct nfs4_slot_table *tbl;
396 struct nfs4_slot *slot;
397 struct nfs_client *clp;
400 * sr_status remains 1 if an RPC level error occurred. The server
401 * may or may not have processed the sequence operation..
402 * Proceed as if the server received and processed the sequence
405 if (res->sr_status == 1)
406 res->sr_status = NFS_OK;
408 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
409 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
412 tbl = &res->sr_session->fc_slot_table;
413 slot = tbl->slots + res->sr_slotid;
415 /* Check the SEQUENCE operation status */
416 switch (res->sr_status) {
418 /* Update the slot's sequence and clientid lease timer */
420 timestamp = res->sr_renewal_time;
421 clp = res->sr_session->clp;
422 spin_lock(&clp->cl_lock);
423 if (time_before(clp->cl_last_renewal, timestamp))
424 clp->cl_last_renewal = timestamp;
425 spin_unlock(&clp->cl_lock);
426 /* Check sequence flags */
427 if (atomic_read(&clp->cl_count) > 1)
428 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
431 /* The server detected a resend of the RPC call and
432 * returned NFS4ERR_DELAY as per Section 2.10.6.2
435 dprintk("%s: slot=%d seq=%d: Operation in progress\n",
436 __func__, res->sr_slotid, slot->seq_nr);
439 /* Just update the slot sequence no. */
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 rpc_restart_call(task);
449 /* FIXME: rpc_restart_call() should be made to return success/fail */
450 if (task->tk_action == NULL)
452 rpc_delay(task, NFS4_POLL_RETRY_MAX);
456 static int nfs4_sequence_done(struct rpc_task *task,
457 struct nfs4_sequence_res *res)
459 if (res->sr_session == NULL)
461 return nfs41_sequence_done(task, res);
465 * nfs4_find_slot - efficiently look for a free slot
467 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
468 * If found, we mark the slot as used, update the highest_used_slotid,
469 * and respectively set up the sequence operation args.
470 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
472 * Note: must be called with under the slot_tbl_lock.
475 nfs4_find_slot(struct nfs4_slot_table *tbl)
478 u8 ret_id = NFS4_MAX_SLOT_TABLE;
479 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
481 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
482 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
484 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
485 if (slotid >= tbl->max_slots)
487 __set_bit(slotid, tbl->used_slots);
488 if (slotid > tbl->highest_used_slotid)
489 tbl->highest_used_slotid = slotid;
492 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
493 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
497 static int nfs41_setup_sequence(struct nfs4_session *session,
498 struct nfs4_sequence_args *args,
499 struct nfs4_sequence_res *res,
501 struct rpc_task *task)
503 struct nfs4_slot *slot;
504 struct nfs4_slot_table *tbl;
507 dprintk("--> %s\n", __func__);
508 /* slot already allocated? */
509 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
512 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
513 tbl = &session->fc_slot_table;
515 spin_lock(&tbl->slot_tbl_lock);
516 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
517 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
519 * The state manager will wait until the slot table is empty.
520 * Schedule the reset thread
522 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
523 spin_unlock(&tbl->slot_tbl_lock);
524 dprintk("%s Schedule Session Reset\n", __func__);
528 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
529 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
530 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
531 spin_unlock(&tbl->slot_tbl_lock);
532 dprintk("%s enforce FIFO order\n", __func__);
536 slotid = nfs4_find_slot(tbl);
537 if (slotid == NFS4_MAX_SLOT_TABLE) {
538 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
539 spin_unlock(&tbl->slot_tbl_lock);
540 dprintk("<-- %s: no free slots\n", __func__);
543 spin_unlock(&tbl->slot_tbl_lock);
545 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
546 slot = tbl->slots + slotid;
547 args->sa_session = session;
548 args->sa_slotid = slotid;
549 args->sa_cache_this = cache_reply;
551 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
553 res->sr_session = session;
554 res->sr_slotid = slotid;
555 res->sr_renewal_time = jiffies;
556 res->sr_status_flags = 0;
558 * sr_status is only set in decode_sequence, and so will remain
559 * set to 1 if an rpc level failure occurs.
565 int nfs4_setup_sequence(const struct nfs_server *server,
566 struct nfs4_sequence_args *args,
567 struct nfs4_sequence_res *res,
569 struct rpc_task *task)
571 struct nfs4_session *session = nfs4_get_session(server);
574 if (session == NULL) {
575 args->sa_session = NULL;
576 res->sr_session = NULL;
580 dprintk("--> %s clp %p session %p sr_slotid %d\n",
581 __func__, session->clp, session, res->sr_slotid);
583 ret = nfs41_setup_sequence(session, args, res, cache_reply,
586 dprintk("<-- %s status=%d\n", __func__, ret);
590 struct nfs41_call_sync_data {
591 const struct nfs_server *seq_server;
592 struct nfs4_sequence_args *seq_args;
593 struct nfs4_sequence_res *seq_res;
597 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
599 struct nfs41_call_sync_data *data = calldata;
601 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
603 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
604 data->seq_res, data->cache_reply, task))
606 rpc_call_start(task);
609 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
611 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
612 nfs41_call_sync_prepare(task, calldata);
615 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
617 struct nfs41_call_sync_data *data = calldata;
619 nfs41_sequence_done(task, data->seq_res);
622 struct rpc_call_ops nfs41_call_sync_ops = {
623 .rpc_call_prepare = nfs41_call_sync_prepare,
624 .rpc_call_done = nfs41_call_sync_done,
627 struct rpc_call_ops nfs41_call_priv_sync_ops = {
628 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
629 .rpc_call_done = nfs41_call_sync_done,
632 static int nfs4_call_sync_sequence(struct nfs_server *server,
633 struct rpc_message *msg,
634 struct nfs4_sequence_args *args,
635 struct nfs4_sequence_res *res,
640 struct rpc_task *task;
641 struct nfs41_call_sync_data data = {
642 .seq_server = server,
645 .cache_reply = cache_reply,
647 struct rpc_task_setup task_setup = {
648 .rpc_client = server->client,
650 .callback_ops = &nfs41_call_sync_ops,
651 .callback_data = &data
654 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
656 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
657 task = rpc_run_task(&task_setup);
661 ret = task->tk_status;
667 int _nfs4_call_sync_session(struct nfs_server *server,
668 struct rpc_message *msg,
669 struct nfs4_sequence_args *args,
670 struct nfs4_sequence_res *res,
673 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
677 static int nfs4_sequence_done(struct rpc_task *task,
678 struct nfs4_sequence_res *res)
682 #endif /* CONFIG_NFS_V4_1 */
684 int _nfs4_call_sync(struct nfs_server *server,
685 struct rpc_message *msg,
686 struct nfs4_sequence_args *args,
687 struct nfs4_sequence_res *res,
690 args->sa_session = res->sr_session = NULL;
691 return rpc_call_sync(server->client, msg, 0);
694 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
695 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
696 &(res)->seq_res, (cache_reply))
698 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
700 struct nfs_inode *nfsi = NFS_I(dir);
702 spin_lock(&dir->i_lock);
703 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
704 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
705 nfs_force_lookup_revalidate(dir);
706 nfsi->change_attr = cinfo->after;
707 spin_unlock(&dir->i_lock);
710 struct nfs4_opendata {
712 struct nfs_openargs o_arg;
713 struct nfs_openres o_res;
714 struct nfs_open_confirmargs c_arg;
715 struct nfs_open_confirmres c_res;
716 struct nfs_fattr f_attr;
717 struct nfs_fattr dir_attr;
720 struct nfs4_state_owner *owner;
721 struct nfs4_state *state;
723 unsigned long timestamp;
724 unsigned int rpc_done : 1;
730 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
732 p->o_res.f_attr = &p->f_attr;
733 p->o_res.dir_attr = &p->dir_attr;
734 p->o_res.seqid = p->o_arg.seqid;
735 p->c_res.seqid = p->c_arg.seqid;
736 p->o_res.server = p->o_arg.server;
737 nfs_fattr_init(&p->f_attr);
738 nfs_fattr_init(&p->dir_attr);
739 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
742 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
743 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
744 const struct iattr *attrs,
747 struct dentry *parent = dget_parent(path->dentry);
748 struct inode *dir = parent->d_inode;
749 struct nfs_server *server = NFS_SERVER(dir);
750 struct nfs4_opendata *p;
752 p = kzalloc(sizeof(*p), gfp_mask);
755 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
756 if (p->o_arg.seqid == NULL)
762 atomic_inc(&sp->so_count);
763 p->o_arg.fh = NFS_FH(dir);
764 p->o_arg.open_flags = flags;
765 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
766 p->o_arg.clientid = server->nfs_client->cl_clientid;
767 p->o_arg.id = sp->so_owner_id.id;
768 p->o_arg.name = &p->path.dentry->d_name;
769 p->o_arg.server = server;
770 p->o_arg.bitmask = server->attr_bitmask;
771 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
772 if (flags & O_CREAT) {
775 p->o_arg.u.attrs = &p->attrs;
776 memcpy(&p->attrs, attrs, sizeof(p->attrs));
777 s = (u32 *) p->o_arg.u.verifier.data;
781 p->c_arg.fh = &p->o_res.fh;
782 p->c_arg.stateid = &p->o_res.stateid;
783 p->c_arg.seqid = p->o_arg.seqid;
784 nfs4_init_opendata_res(p);
794 static void nfs4_opendata_free(struct kref *kref)
796 struct nfs4_opendata *p = container_of(kref,
797 struct nfs4_opendata, kref);
799 nfs_free_seqid(p->o_arg.seqid);
800 if (p->state != NULL)
801 nfs4_put_open_state(p->state);
802 nfs4_put_state_owner(p->owner);
808 static void nfs4_opendata_put(struct nfs4_opendata *p)
811 kref_put(&p->kref, nfs4_opendata_free);
814 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
818 ret = rpc_wait_for_completion_task(task);
822 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
826 if (open_mode & O_EXCL)
828 switch (mode & (FMODE_READ|FMODE_WRITE)) {
830 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
831 && state->n_rdonly != 0;
834 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
835 && state->n_wronly != 0;
837 case FMODE_READ|FMODE_WRITE:
838 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
839 && state->n_rdwr != 0;
845 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
847 if ((delegation->type & fmode) != fmode)
849 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
851 nfs_mark_delegation_referenced(delegation);
855 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
864 case FMODE_READ|FMODE_WRITE:
867 nfs4_state_set_mode_locked(state, state->state | fmode);
870 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
872 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
873 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
874 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
877 set_bit(NFS_O_RDONLY_STATE, &state->flags);
880 set_bit(NFS_O_WRONLY_STATE, &state->flags);
882 case FMODE_READ|FMODE_WRITE:
883 set_bit(NFS_O_RDWR_STATE, &state->flags);
887 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
889 write_seqlock(&state->seqlock);
890 nfs_set_open_stateid_locked(state, stateid, fmode);
891 write_sequnlock(&state->seqlock);
894 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
897 * Protect the call to nfs4_state_set_mode_locked and
898 * serialise the stateid update
900 write_seqlock(&state->seqlock);
901 if (deleg_stateid != NULL) {
902 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
903 set_bit(NFS_DELEGATED_STATE, &state->flags);
905 if (open_stateid != NULL)
906 nfs_set_open_stateid_locked(state, open_stateid, fmode);
907 write_sequnlock(&state->seqlock);
908 spin_lock(&state->owner->so_lock);
909 update_open_stateflags(state, fmode);
910 spin_unlock(&state->owner->so_lock);
913 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
915 struct nfs_inode *nfsi = NFS_I(state->inode);
916 struct nfs_delegation *deleg_cur;
919 fmode &= (FMODE_READ|FMODE_WRITE);
922 deleg_cur = rcu_dereference(nfsi->delegation);
923 if (deleg_cur == NULL)
926 spin_lock(&deleg_cur->lock);
927 if (nfsi->delegation != deleg_cur ||
928 (deleg_cur->type & fmode) != fmode)
929 goto no_delegation_unlock;
931 if (delegation == NULL)
932 delegation = &deleg_cur->stateid;
933 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
934 goto no_delegation_unlock;
936 nfs_mark_delegation_referenced(deleg_cur);
937 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
939 no_delegation_unlock:
940 spin_unlock(&deleg_cur->lock);
944 if (!ret && open_stateid != NULL) {
945 __update_open_stateid(state, open_stateid, NULL, fmode);
953 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
955 struct nfs_delegation *delegation;
958 delegation = rcu_dereference(NFS_I(inode)->delegation);
959 if (delegation == NULL || (delegation->type & fmode) == fmode) {
964 nfs_inode_return_delegation(inode);
967 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
969 struct nfs4_state *state = opendata->state;
970 struct nfs_inode *nfsi = NFS_I(state->inode);
971 struct nfs_delegation *delegation;
972 int open_mode = opendata->o_arg.open_flags & O_EXCL;
973 fmode_t fmode = opendata->o_arg.fmode;
974 nfs4_stateid stateid;
978 if (can_open_cached(state, fmode, open_mode)) {
979 spin_lock(&state->owner->so_lock);
980 if (can_open_cached(state, fmode, open_mode)) {
981 update_open_stateflags(state, fmode);
982 spin_unlock(&state->owner->so_lock);
983 goto out_return_state;
985 spin_unlock(&state->owner->so_lock);
988 delegation = rcu_dereference(nfsi->delegation);
989 if (delegation == NULL ||
990 !can_open_delegated(delegation, fmode)) {
994 /* Save the delegation */
995 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
997 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1002 /* Try to update the stateid using the delegation */
1003 if (update_open_stateid(state, NULL, &stateid, fmode))
1004 goto out_return_state;
1007 return ERR_PTR(ret);
1009 atomic_inc(&state->count);
1013 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1015 struct inode *inode;
1016 struct nfs4_state *state = NULL;
1017 struct nfs_delegation *delegation;
1020 if (!data->rpc_done) {
1021 state = nfs4_try_open_cached(data);
1026 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1028 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1029 ret = PTR_ERR(inode);
1033 state = nfs4_get_open_state(inode, data->owner);
1036 if (data->o_res.delegation_type != 0) {
1037 int delegation_flags = 0;
1040 delegation = rcu_dereference(NFS_I(inode)->delegation);
1042 delegation_flags = delegation->flags;
1044 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1045 nfs_inode_set_delegation(state->inode,
1046 data->owner->so_cred,
1049 nfs_inode_reclaim_delegation(state->inode,
1050 data->owner->so_cred,
1054 update_open_stateid(state, &data->o_res.stateid, NULL,
1062 return ERR_PTR(ret);
1065 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1067 struct nfs_inode *nfsi = NFS_I(state->inode);
1068 struct nfs_open_context *ctx;
1070 spin_lock(&state->inode->i_lock);
1071 list_for_each_entry(ctx, &nfsi->open_files, list) {
1072 if (ctx->state != state)
1074 get_nfs_open_context(ctx);
1075 spin_unlock(&state->inode->i_lock);
1078 spin_unlock(&state->inode->i_lock);
1079 return ERR_PTR(-ENOENT);
1082 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1084 struct nfs4_opendata *opendata;
1086 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1087 if (opendata == NULL)
1088 return ERR_PTR(-ENOMEM);
1089 opendata->state = state;
1090 atomic_inc(&state->count);
1094 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1096 struct nfs4_state *newstate;
1099 opendata->o_arg.open_flags = 0;
1100 opendata->o_arg.fmode = fmode;
1101 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1102 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1103 nfs4_init_opendata_res(opendata);
1104 ret = _nfs4_recover_proc_open(opendata);
1107 newstate = nfs4_opendata_to_nfs4_state(opendata);
1108 if (IS_ERR(newstate))
1109 return PTR_ERR(newstate);
1110 nfs4_close_state(&opendata->path, newstate, fmode);
1115 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1117 struct nfs4_state *newstate;
1120 /* memory barrier prior to reading state->n_* */
1121 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1123 if (state->n_rdwr != 0) {
1124 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1127 if (newstate != state)
1130 if (state->n_wronly != 0) {
1131 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1134 if (newstate != state)
1137 if (state->n_rdonly != 0) {
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 && err != -EKEYEXPIRED)
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);
1266 err = nfs4_handle_exception(server, err, &exception);
1267 } while (exception.retry);
1272 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1274 struct nfs4_opendata *data = calldata;
1276 data->rpc_status = task->tk_status;
1277 if (RPC_ASSASSINATED(task))
1279 if (data->rpc_status == 0) {
1280 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1281 sizeof(data->o_res.stateid.data));
1282 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1283 renew_lease(data->o_res.server, data->timestamp);
1288 static void nfs4_open_confirm_release(void *calldata)
1290 struct nfs4_opendata *data = calldata;
1291 struct nfs4_state *state = NULL;
1293 /* If this request hasn't been cancelled, do nothing */
1294 if (data->cancelled == 0)
1296 /* In case of error, no cleanup! */
1297 if (!data->rpc_done)
1299 state = nfs4_opendata_to_nfs4_state(data);
1301 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1303 nfs4_opendata_put(data);
1306 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1307 .rpc_call_done = nfs4_open_confirm_done,
1308 .rpc_release = nfs4_open_confirm_release,
1312 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1314 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1316 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1317 struct rpc_task *task;
1318 struct rpc_message msg = {
1319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1320 .rpc_argp = &data->c_arg,
1321 .rpc_resp = &data->c_res,
1322 .rpc_cred = data->owner->so_cred,
1324 struct rpc_task_setup task_setup_data = {
1325 .rpc_client = server->client,
1326 .rpc_message = &msg,
1327 .callback_ops = &nfs4_open_confirm_ops,
1328 .callback_data = data,
1329 .workqueue = nfsiod_workqueue,
1330 .flags = RPC_TASK_ASYNC,
1334 kref_get(&data->kref);
1336 data->rpc_status = 0;
1337 data->timestamp = jiffies;
1338 task = rpc_run_task(&task_setup_data);
1340 return PTR_ERR(task);
1341 status = nfs4_wait_for_completion_rpc_task(task);
1343 data->cancelled = 1;
1346 status = data->rpc_status;
1351 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1353 struct nfs4_opendata *data = calldata;
1354 struct nfs4_state_owner *sp = data->owner;
1356 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1359 * Check if we still need to send an OPEN call, or if we can use
1360 * a delegation instead.
1362 if (data->state != NULL) {
1363 struct nfs_delegation *delegation;
1365 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1368 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1369 if (delegation != NULL &&
1370 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1376 /* Update sequence id. */
1377 data->o_arg.id = sp->so_owner_id.id;
1378 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1379 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1380 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1381 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1383 data->timestamp = jiffies;
1384 if (nfs4_setup_sequence(data->o_arg.server,
1385 &data->o_arg.seq_args,
1386 &data->o_res.seq_res, 1, task))
1388 rpc_call_start(task);
1391 task->tk_action = NULL;
1395 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1397 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1398 nfs4_open_prepare(task, calldata);
1401 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1403 struct nfs4_opendata *data = calldata;
1405 data->rpc_status = task->tk_status;
1407 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1410 if (RPC_ASSASSINATED(task))
1412 if (task->tk_status == 0) {
1413 switch (data->o_res.f_attr->mode & S_IFMT) {
1417 data->rpc_status = -ELOOP;
1420 data->rpc_status = -EISDIR;
1423 data->rpc_status = -ENOTDIR;
1425 renew_lease(data->o_res.server, data->timestamp);
1426 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1427 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1432 static void nfs4_open_release(void *calldata)
1434 struct nfs4_opendata *data = calldata;
1435 struct nfs4_state *state = NULL;
1437 /* If this request hasn't been cancelled, do nothing */
1438 if (data->cancelled == 0)
1440 /* In case of error, no cleanup! */
1441 if (data->rpc_status != 0 || !data->rpc_done)
1443 /* In case we need an open_confirm, no cleanup! */
1444 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1446 state = nfs4_opendata_to_nfs4_state(data);
1448 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1450 nfs4_opendata_put(data);
1453 static const struct rpc_call_ops nfs4_open_ops = {
1454 .rpc_call_prepare = nfs4_open_prepare,
1455 .rpc_call_done = nfs4_open_done,
1456 .rpc_release = nfs4_open_release,
1459 static const struct rpc_call_ops nfs4_recover_open_ops = {
1460 .rpc_call_prepare = nfs4_recover_open_prepare,
1461 .rpc_call_done = nfs4_open_done,
1462 .rpc_release = nfs4_open_release,
1465 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1467 struct inode *dir = data->dir->d_inode;
1468 struct nfs_server *server = NFS_SERVER(dir);
1469 struct nfs_openargs *o_arg = &data->o_arg;
1470 struct nfs_openres *o_res = &data->o_res;
1471 struct rpc_task *task;
1472 struct rpc_message msg = {
1473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1476 .rpc_cred = data->owner->so_cred,
1478 struct rpc_task_setup task_setup_data = {
1479 .rpc_client = server->client,
1480 .rpc_message = &msg,
1481 .callback_ops = &nfs4_open_ops,
1482 .callback_data = data,
1483 .workqueue = nfsiod_workqueue,
1484 .flags = RPC_TASK_ASYNC,
1488 kref_get(&data->kref);
1490 data->rpc_status = 0;
1491 data->cancelled = 0;
1493 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1494 task = rpc_run_task(&task_setup_data);
1496 return PTR_ERR(task);
1497 status = nfs4_wait_for_completion_rpc_task(task);
1499 data->cancelled = 1;
1502 status = data->rpc_status;
1508 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1510 struct inode *dir = data->dir->d_inode;
1511 struct nfs_openres *o_res = &data->o_res;
1514 status = nfs4_run_open_task(data, 1);
1515 if (status != 0 || !data->rpc_done)
1518 nfs_refresh_inode(dir, o_res->dir_attr);
1520 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1521 status = _nfs4_proc_open_confirm(data);
1530 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1532 static int _nfs4_proc_open(struct nfs4_opendata *data)
1534 struct inode *dir = data->dir->d_inode;
1535 struct nfs_server *server = NFS_SERVER(dir);
1536 struct nfs_openargs *o_arg = &data->o_arg;
1537 struct nfs_openres *o_res = &data->o_res;
1540 status = nfs4_run_open_task(data, 0);
1541 if (status != 0 || !data->rpc_done)
1544 if (o_arg->open_flags & O_CREAT) {
1545 update_changeattr(dir, &o_res->cinfo);
1546 nfs_post_op_update_inode(dir, o_res->dir_attr);
1548 nfs_refresh_inode(dir, o_res->dir_attr);
1549 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1550 server->caps &= ~NFS_CAP_POSIX_LOCK;
1551 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1552 status = _nfs4_proc_open_confirm(data);
1556 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1557 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1561 static int nfs4_recover_expired_lease(struct nfs_server *server)
1563 struct nfs_client *clp = server->nfs_client;
1567 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1568 ret = nfs4_wait_clnt_recover(clp);
1571 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1572 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1574 nfs4_schedule_state_recovery(clp);
1582 * reclaim state on the server after a network partition.
1583 * Assumes caller holds the appropriate lock
1585 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1587 struct nfs4_opendata *opendata;
1590 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1591 if (IS_ERR(opendata))
1592 return PTR_ERR(opendata);
1593 ret = nfs4_open_recover(opendata, state);
1595 d_drop(ctx->path.dentry);
1596 nfs4_opendata_put(opendata);
1600 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1602 struct nfs_server *server = NFS_SERVER(state->inode);
1603 struct nfs4_exception exception = { };
1607 err = _nfs4_open_expired(ctx, state);
1611 case -NFS4ERR_GRACE:
1612 case -NFS4ERR_DELAY:
1614 nfs4_handle_exception(server, err, &exception);
1617 } while (exception.retry);
1622 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1624 struct nfs_open_context *ctx;
1627 ctx = nfs4_state_find_open_context(state);
1629 return PTR_ERR(ctx);
1630 ret = nfs4_do_open_expired(ctx, state);
1631 put_nfs_open_context(ctx);
1636 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1637 * fields corresponding to attributes that were used to store the verifier.
1638 * Make sure we clobber those fields in the later setattr call
1640 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1642 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1643 !(sattr->ia_valid & ATTR_ATIME_SET))
1644 sattr->ia_valid |= ATTR_ATIME;
1646 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1647 !(sattr->ia_valid & ATTR_MTIME_SET))
1648 sattr->ia_valid |= ATTR_MTIME;
1652 * Returns a referenced nfs4_state
1654 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)
1656 struct nfs4_state_owner *sp;
1657 struct nfs4_state *state = NULL;
1658 struct nfs_server *server = NFS_SERVER(dir);
1659 struct nfs4_opendata *opendata;
1662 /* Protect against reboot recovery conflicts */
1664 if (!(sp = nfs4_get_state_owner(server, cred))) {
1665 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1668 status = nfs4_recover_expired_lease(server);
1670 goto err_put_state_owner;
1671 if (path->dentry->d_inode != NULL)
1672 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1674 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1675 if (opendata == NULL)
1676 goto err_put_state_owner;
1678 if (path->dentry->d_inode != NULL)
1679 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1681 status = _nfs4_proc_open(opendata);
1683 goto err_opendata_put;
1685 state = nfs4_opendata_to_nfs4_state(opendata);
1686 status = PTR_ERR(state);
1688 goto err_opendata_put;
1689 if (server->caps & NFS_CAP_POSIX_LOCK)
1690 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1692 if (opendata->o_arg.open_flags & O_EXCL) {
1693 nfs4_exclusive_attrset(opendata, sattr);
1695 nfs_fattr_init(opendata->o_res.f_attr);
1696 status = nfs4_do_setattr(state->inode, cred,
1697 opendata->o_res.f_attr, sattr,
1700 nfs_setattr_update_inode(state->inode, sattr);
1701 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1703 nfs4_opendata_put(opendata);
1704 nfs4_put_state_owner(sp);
1708 nfs4_opendata_put(opendata);
1709 err_put_state_owner:
1710 nfs4_put_state_owner(sp);
1717 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)
1719 struct nfs4_exception exception = { };
1720 struct nfs4_state *res;
1724 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1727 /* NOTE: BAD_SEQID means the server and client disagree about the
1728 * book-keeping w.r.t. state-changing operations
1729 * (OPEN/CLOSE/LOCK/LOCKU...)
1730 * It is actually a sign of a bug on the client or on the server.
1732 * If we receive a BAD_SEQID error in the particular case of
1733 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1734 * have unhashed the old state_owner for us, and that we can
1735 * therefore safely retry using a new one. We should still warn
1736 * the user though...
1738 if (status == -NFS4ERR_BAD_SEQID) {
1739 printk(KERN_WARNING "NFS: v4 server %s "
1740 " returned a bad sequence-id error!\n",
1741 NFS_SERVER(dir)->nfs_client->cl_hostname);
1742 exception.retry = 1;
1746 * BAD_STATEID on OPEN means that the server cancelled our
1747 * state before it received the OPEN_CONFIRM.
1748 * Recover by retrying the request as per the discussion
1749 * on Page 181 of RFC3530.
1751 if (status == -NFS4ERR_BAD_STATEID) {
1752 exception.retry = 1;
1755 if (status == -EAGAIN) {
1756 /* We must have found a delegation */
1757 exception.retry = 1;
1760 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1761 status, &exception));
1762 } while (exception.retry);
1766 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1767 struct nfs_fattr *fattr, struct iattr *sattr,
1768 struct nfs4_state *state)
1770 struct nfs_server *server = NFS_SERVER(inode);
1771 struct nfs_setattrargs arg = {
1772 .fh = NFS_FH(inode),
1775 .bitmask = server->attr_bitmask,
1777 struct nfs_setattrres res = {
1781 struct rpc_message msg = {
1782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1787 unsigned long timestamp = jiffies;
1790 nfs_fattr_init(fattr);
1792 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1793 /* Use that stateid */
1794 } else if (state != NULL) {
1795 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1797 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1799 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1800 if (status == 0 && state != NULL)
1801 renew_lease(server, timestamp);
1805 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1806 struct nfs_fattr *fattr, struct iattr *sattr,
1807 struct nfs4_state *state)
1809 struct nfs_server *server = NFS_SERVER(inode);
1810 struct nfs4_exception exception = { };
1813 err = nfs4_handle_exception(server,
1814 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1816 } while (exception.retry);
1820 struct nfs4_closedata {
1822 struct inode *inode;
1823 struct nfs4_state *state;
1824 struct nfs_closeargs arg;
1825 struct nfs_closeres res;
1826 struct nfs_fattr fattr;
1827 unsigned long timestamp;
1830 static void nfs4_free_closedata(void *data)
1832 struct nfs4_closedata *calldata = data;
1833 struct nfs4_state_owner *sp = calldata->state->owner;
1835 nfs4_put_open_state(calldata->state);
1836 nfs_free_seqid(calldata->arg.seqid);
1837 nfs4_put_state_owner(sp);
1838 path_put(&calldata->path);
1842 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1845 spin_lock(&state->owner->so_lock);
1846 if (!(fmode & FMODE_READ))
1847 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1848 if (!(fmode & FMODE_WRITE))
1849 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1850 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1851 spin_unlock(&state->owner->so_lock);
1854 static void nfs4_close_done(struct rpc_task *task, void *data)
1856 struct nfs4_closedata *calldata = data;
1857 struct nfs4_state *state = calldata->state;
1858 struct nfs_server *server = NFS_SERVER(calldata->inode);
1860 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1862 if (RPC_ASSASSINATED(task))
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;
1985 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1987 calldata->path = *path;
1989 msg.rpc_argp = &calldata->arg,
1990 msg.rpc_resp = &calldata->res,
1991 task_setup_data.callback_data = calldata;
1992 task = rpc_run_task(&task_setup_data);
1994 return PTR_ERR(task);
1997 status = rpc_wait_for_completion_task(task);
2003 nfs4_put_open_state(state);
2004 nfs4_put_state_owner(sp);
2008 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
2013 /* If the open_intent is for execute, we have an extra check to make */
2014 if (fmode & FMODE_EXEC) {
2015 ret = nfs_may_open(state->inode,
2016 state->owner->so_cred,
2017 nd->intent.open.flags);
2021 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
2022 if (!IS_ERR(filp)) {
2023 struct nfs_open_context *ctx;
2024 ctx = nfs_file_open_context(filp);
2028 ret = PTR_ERR(filp);
2030 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
2035 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2037 struct path path = {
2038 .mnt = nd->path.mnt,
2041 struct dentry *parent;
2043 struct rpc_cred *cred;
2044 struct nfs4_state *state;
2046 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
2048 if (nd->flags & LOOKUP_CREATE) {
2049 attr.ia_mode = nd->intent.open.create_mode;
2050 attr.ia_valid = ATTR_MODE;
2051 if (!IS_POSIXACL(dir))
2052 attr.ia_mode &= ~current_umask();
2055 BUG_ON(nd->intent.open.flags & O_CREAT);
2058 cred = rpc_lookup_cred();
2060 return (struct dentry *)cred;
2061 parent = dentry->d_parent;
2062 /* Protect against concurrent sillydeletes */
2063 nfs_block_sillyrename(parent);
2064 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
2066 if (IS_ERR(state)) {
2067 if (PTR_ERR(state) == -ENOENT) {
2068 d_add(dentry, NULL);
2069 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2071 nfs_unblock_sillyrename(parent);
2072 return (struct dentry *)state;
2074 res = d_add_unique(dentry, igrab(state->inode));
2077 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
2078 nfs_unblock_sillyrename(parent);
2079 nfs4_intent_set_file(nd, &path, state, fmode);
2084 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
2086 struct path path = {
2087 .mnt = nd->path.mnt,
2090 struct rpc_cred *cred;
2091 struct nfs4_state *state;
2092 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
2094 cred = rpc_lookup_cred();
2096 return PTR_ERR(cred);
2097 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
2099 if (IS_ERR(state)) {
2100 switch (PTR_ERR(state)) {
2106 return PTR_ERR(state);
2111 if (state->inode == dentry->d_inode) {
2112 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2113 nfs4_intent_set_file(nd, &path, state, fmode);
2116 nfs4_close_sync(&path, state, fmode);
2122 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2124 if (ctx->state == NULL)
2127 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2129 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2132 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2134 struct nfs4_server_caps_arg args = {
2137 struct nfs4_server_caps_res res = {};
2138 struct rpc_message msg = {
2139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2145 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2147 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2148 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2149 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2150 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2151 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2152 NFS_CAP_CTIME|NFS_CAP_MTIME);
2153 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2154 server->caps |= NFS_CAP_ACLS;
2155 if (res.has_links != 0)
2156 server->caps |= NFS_CAP_HARDLINKS;
2157 if (res.has_symlinks != 0)
2158 server->caps |= NFS_CAP_SYMLINKS;
2159 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2160 server->caps |= NFS_CAP_FILEID;
2161 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2162 server->caps |= NFS_CAP_MODE;
2163 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2164 server->caps |= NFS_CAP_NLINK;
2165 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2166 server->caps |= NFS_CAP_OWNER;
2167 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2168 server->caps |= NFS_CAP_OWNER_GROUP;
2169 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2170 server->caps |= NFS_CAP_ATIME;
2171 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2172 server->caps |= NFS_CAP_CTIME;
2173 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2174 server->caps |= NFS_CAP_MTIME;
2176 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2177 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2178 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2179 server->acl_bitmask = res.acl_bitmask;
2185 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2187 struct nfs4_exception exception = { };
2190 err = nfs4_handle_exception(server,
2191 _nfs4_server_capabilities(server, fhandle),
2193 } while (exception.retry);
2197 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2198 struct nfs_fsinfo *info)
2200 struct nfs4_lookup_root_arg args = {
2201 .bitmask = nfs4_fattr_bitmap,
2203 struct nfs4_lookup_res res = {
2205 .fattr = info->fattr,
2208 struct rpc_message msg = {
2209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2214 nfs_fattr_init(info->fattr);
2215 return nfs4_call_sync(server, &msg, &args, &res, 0);
2218 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2219 struct nfs_fsinfo *info)
2221 struct nfs4_exception exception = { };
2224 err = nfs4_handle_exception(server,
2225 _nfs4_lookup_root(server, fhandle, info),
2227 } while (exception.retry);
2232 * get the file handle for the "/" directory on the server
2234 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2235 struct nfs_fsinfo *info)
2239 status = nfs4_lookup_root(server, fhandle, info);
2241 status = nfs4_server_capabilities(server, fhandle);
2243 status = nfs4_do_fsinfo(server, fhandle, info);
2244 return nfs4_map_errors(status);
2248 * Get locations and (maybe) other attributes of a referral.
2249 * Note that we'll actually follow the referral later when
2250 * we detect fsid mismatch in inode revalidation
2252 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2254 int status = -ENOMEM;
2255 struct page *page = NULL;
2256 struct nfs4_fs_locations *locations = NULL;
2258 page = alloc_page(GFP_KERNEL);
2261 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2262 if (locations == NULL)
2265 status = nfs4_proc_fs_locations(dir, name, locations, page);
2268 /* Make sure server returned a different fsid for the referral */
2269 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2270 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2275 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2276 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2278 fattr->mode = S_IFDIR;
2279 memset(fhandle, 0, sizeof(struct nfs_fh));
2288 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2290 struct nfs4_getattr_arg args = {
2292 .bitmask = server->attr_bitmask,
2294 struct nfs4_getattr_res res = {
2298 struct rpc_message msg = {
2299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2304 nfs_fattr_init(fattr);
2305 return nfs4_call_sync(server, &msg, &args, &res, 0);
2308 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2310 struct nfs4_exception exception = { };
2313 err = nfs4_handle_exception(server,
2314 _nfs4_proc_getattr(server, fhandle, fattr),
2316 } while (exception.retry);
2321 * The file is not closed if it is opened due to the a request to change
2322 * the size of the file. The open call will not be needed once the
2323 * VFS layer lookup-intents are implemented.
2325 * Close is called when the inode is destroyed.
2326 * If we haven't opened the file for O_WRONLY, we
2327 * need to in the size_change case to obtain a stateid.
2330 * Because OPEN is always done by name in nfsv4, it is
2331 * possible that we opened a different file by the same
2332 * name. We can recognize this race condition, but we
2333 * can't do anything about it besides returning an error.
2335 * This will be fixed with VFS changes (lookup-intent).
2338 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2339 struct iattr *sattr)
2341 struct inode *inode = dentry->d_inode;
2342 struct rpc_cred *cred = NULL;
2343 struct nfs4_state *state = NULL;
2346 nfs_fattr_init(fattr);
2348 /* Search for an existing open(O_WRITE) file */
2349 if (sattr->ia_valid & ATTR_FILE) {
2350 struct nfs_open_context *ctx;
2352 ctx = nfs_file_open_context(sattr->ia_file);
2359 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2361 nfs_setattr_update_inode(inode, sattr);
2365 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2366 const struct qstr *name, struct nfs_fh *fhandle,
2367 struct nfs_fattr *fattr)
2370 struct nfs4_lookup_arg args = {
2371 .bitmask = server->attr_bitmask,
2375 struct nfs4_lookup_res res = {
2380 struct rpc_message msg = {
2381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2386 nfs_fattr_init(fattr);
2388 dprintk("NFS call lookupfh %s\n", name->name);
2389 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2390 dprintk("NFS reply lookupfh: %d\n", status);
2394 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2395 struct qstr *name, struct nfs_fh *fhandle,
2396 struct nfs_fattr *fattr)
2398 struct nfs4_exception exception = { };
2401 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2403 if (err == -NFS4ERR_MOVED) {
2407 err = nfs4_handle_exception(server, err, &exception);
2408 } while (exception.retry);
2412 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2413 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2417 dprintk("NFS call lookup %s\n", name->name);
2418 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2419 if (status == -NFS4ERR_MOVED)
2420 status = nfs4_get_referral(dir, name, fattr, fhandle);
2421 dprintk("NFS reply lookup: %d\n", status);
2425 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2427 struct nfs4_exception exception = { };
2430 err = nfs4_handle_exception(NFS_SERVER(dir),
2431 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2433 } while (exception.retry);
2437 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2439 struct nfs_server *server = NFS_SERVER(inode);
2440 struct nfs4_accessargs args = {
2441 .fh = NFS_FH(inode),
2442 .bitmask = server->attr_bitmask,
2444 struct nfs4_accessres res = {
2447 struct rpc_message msg = {
2448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2451 .rpc_cred = entry->cred,
2453 int mode = entry->mask;
2457 * Determine which access bits we want to ask for...
2459 if (mode & MAY_READ)
2460 args.access |= NFS4_ACCESS_READ;
2461 if (S_ISDIR(inode->i_mode)) {
2462 if (mode & MAY_WRITE)
2463 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2464 if (mode & MAY_EXEC)
2465 args.access |= NFS4_ACCESS_LOOKUP;
2467 if (mode & MAY_WRITE)
2468 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2469 if (mode & MAY_EXEC)
2470 args.access |= NFS4_ACCESS_EXECUTE;
2473 res.fattr = nfs_alloc_fattr();
2474 if (res.fattr == NULL)
2477 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2480 if (res.access & NFS4_ACCESS_READ)
2481 entry->mask |= MAY_READ;
2482 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2483 entry->mask |= MAY_WRITE;
2484 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2485 entry->mask |= MAY_EXEC;
2486 nfs_refresh_inode(inode, res.fattr);
2488 nfs_free_fattr(res.fattr);
2492 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2494 struct nfs4_exception exception = { };
2497 err = nfs4_handle_exception(NFS_SERVER(inode),
2498 _nfs4_proc_access(inode, entry),
2500 } while (exception.retry);
2505 * TODO: For the time being, we don't try to get any attributes
2506 * along with any of the zero-copy operations READ, READDIR,
2509 * In the case of the first three, we want to put the GETATTR
2510 * after the read-type operation -- this is because it is hard
2511 * to predict the length of a GETATTR response in v4, and thus
2512 * align the READ data correctly. This means that the GETATTR
2513 * may end up partially falling into the page cache, and we should
2514 * shift it into the 'tail' of the xdr_buf before processing.
2515 * To do this efficiently, we need to know the total length
2516 * of data received, which doesn't seem to be available outside
2519 * In the case of WRITE, we also want to put the GETATTR after
2520 * the operation -- in this case because we want to make sure
2521 * we get the post-operation mtime and size. This means that
2522 * we can't use xdr_encode_pages() as written: we need a variant
2523 * of it which would leave room in the 'tail' iovec.
2525 * Both of these changes to the XDR layer would in fact be quite
2526 * minor, but I decided to leave them for a subsequent patch.
2528 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2529 unsigned int pgbase, unsigned int pglen)
2531 struct nfs4_readlink args = {
2532 .fh = NFS_FH(inode),
2537 struct nfs4_readlink_res res;
2538 struct rpc_message msg = {
2539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2544 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2547 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2548 unsigned int pgbase, unsigned int pglen)
2550 struct nfs4_exception exception = { };
2553 err = nfs4_handle_exception(NFS_SERVER(inode),
2554 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2556 } while (exception.retry);
2562 * We will need to arrange for the VFS layer to provide an atomic open.
2563 * Until then, this create/open method is prone to inefficiency and race
2564 * conditions due to the lookup, create, and open VFS calls from sys_open()
2565 * placed on the wire.
2567 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2568 * The file will be opened again in the subsequent VFS open call
2569 * (nfs4_proc_file_open).
2571 * The open for read will just hang around to be used by any process that
2572 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2576 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2577 int flags, struct nameidata *nd)
2579 struct path path = {
2580 .mnt = nd->path.mnt,
2583 struct nfs4_state *state;
2584 struct rpc_cred *cred;
2585 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2588 cred = rpc_lookup_cred();
2590 status = PTR_ERR(cred);
2593 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2595 if (IS_ERR(state)) {
2596 status = PTR_ERR(state);
2599 d_add(dentry, igrab(state->inode));
2600 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2601 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2602 status = nfs4_intent_set_file(nd, &path, state, fmode);
2604 nfs4_close_sync(&path, state, fmode);
2611 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2613 struct nfs_server *server = NFS_SERVER(dir);
2614 struct nfs_removeargs args = {
2616 .name.len = name->len,
2617 .name.name = name->name,
2618 .bitmask = server->attr_bitmask,
2620 struct nfs_removeres res = {
2623 struct rpc_message msg = {
2624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2628 int status = -ENOMEM;
2630 res.dir_attr = nfs_alloc_fattr();
2631 if (res.dir_attr == NULL)
2634 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2636 update_changeattr(dir, &res.cinfo);
2637 nfs_post_op_update_inode(dir, res.dir_attr);
2639 nfs_free_fattr(res.dir_attr);
2644 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2646 struct nfs4_exception exception = { };
2649 err = nfs4_handle_exception(NFS_SERVER(dir),
2650 _nfs4_proc_remove(dir, name),
2652 } while (exception.retry);
2656 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2658 struct nfs_server *server = NFS_SERVER(dir);
2659 struct nfs_removeargs *args = msg->rpc_argp;
2660 struct nfs_removeres *res = msg->rpc_resp;
2662 args->bitmask = server->cache_consistency_bitmask;
2663 res->server = server;
2664 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2667 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2669 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2671 if (!nfs4_sequence_done(task, &res->seq_res))
2673 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2675 update_changeattr(dir, &res->cinfo);
2676 nfs_post_op_update_inode(dir, res->dir_attr);
2680 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2681 struct inode *new_dir, struct qstr *new_name)
2683 struct nfs_server *server = NFS_SERVER(old_dir);
2684 struct nfs4_rename_arg arg = {
2685 .old_dir = NFS_FH(old_dir),
2686 .new_dir = NFS_FH(new_dir),
2687 .old_name = old_name,
2688 .new_name = new_name,
2689 .bitmask = server->attr_bitmask,
2691 struct nfs4_rename_res res = {
2694 struct rpc_message msg = {
2695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2699 int status = -ENOMEM;
2701 res.old_fattr = nfs_alloc_fattr();
2702 res.new_fattr = nfs_alloc_fattr();
2703 if (res.old_fattr == NULL || res.new_fattr == NULL)
2706 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2708 update_changeattr(old_dir, &res.old_cinfo);
2709 nfs_post_op_update_inode(old_dir, res.old_fattr);
2710 update_changeattr(new_dir, &res.new_cinfo);
2711 nfs_post_op_update_inode(new_dir, res.new_fattr);
2714 nfs_free_fattr(res.new_fattr);
2715 nfs_free_fattr(res.old_fattr);
2719 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2720 struct inode *new_dir, struct qstr *new_name)
2722 struct nfs4_exception exception = { };
2725 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2726 _nfs4_proc_rename(old_dir, old_name,
2729 } while (exception.retry);
2733 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2735 struct nfs_server *server = NFS_SERVER(inode);
2736 struct nfs4_link_arg arg = {
2737 .fh = NFS_FH(inode),
2738 .dir_fh = NFS_FH(dir),
2740 .bitmask = server->attr_bitmask,
2742 struct nfs4_link_res res = {
2745 struct rpc_message msg = {
2746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2750 int status = -ENOMEM;
2752 res.fattr = nfs_alloc_fattr();
2753 res.dir_attr = nfs_alloc_fattr();
2754 if (res.fattr == NULL || res.dir_attr == NULL)
2757 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2759 update_changeattr(dir, &res.cinfo);
2760 nfs_post_op_update_inode(dir, res.dir_attr);
2761 nfs_post_op_update_inode(inode, res.fattr);
2764 nfs_free_fattr(res.dir_attr);
2765 nfs_free_fattr(res.fattr);
2769 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2771 struct nfs4_exception exception = { };
2774 err = nfs4_handle_exception(NFS_SERVER(inode),
2775 _nfs4_proc_link(inode, dir, name),
2777 } while (exception.retry);
2781 struct nfs4_createdata {
2782 struct rpc_message msg;
2783 struct nfs4_create_arg arg;
2784 struct nfs4_create_res res;
2786 struct nfs_fattr fattr;
2787 struct nfs_fattr dir_fattr;
2790 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2791 struct qstr *name, struct iattr *sattr, u32 ftype)
2793 struct nfs4_createdata *data;
2795 data = kzalloc(sizeof(*data), GFP_KERNEL);
2797 struct nfs_server *server = NFS_SERVER(dir);
2799 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2800 data->msg.rpc_argp = &data->arg;
2801 data->msg.rpc_resp = &data->res;
2802 data->arg.dir_fh = NFS_FH(dir);
2803 data->arg.server = server;
2804 data->arg.name = name;
2805 data->arg.attrs = sattr;
2806 data->arg.ftype = ftype;
2807 data->arg.bitmask = server->attr_bitmask;
2808 data->res.server = server;
2809 data->res.fh = &data->fh;
2810 data->res.fattr = &data->fattr;
2811 data->res.dir_fattr = &data->dir_fattr;
2812 nfs_fattr_init(data->res.fattr);
2813 nfs_fattr_init(data->res.dir_fattr);
2818 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2820 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2821 &data->arg, &data->res, 1);
2823 update_changeattr(dir, &data->res.dir_cinfo);
2824 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2825 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2830 static void nfs4_free_createdata(struct nfs4_createdata *data)
2835 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2836 struct page *page, unsigned int len, struct iattr *sattr)
2838 struct nfs4_createdata *data;
2839 int status = -ENAMETOOLONG;
2841 if (len > NFS4_MAXPATHLEN)
2845 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2849 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2850 data->arg.u.symlink.pages = &page;
2851 data->arg.u.symlink.len = len;
2853 status = nfs4_do_create(dir, dentry, data);
2855 nfs4_free_createdata(data);
2860 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2861 struct page *page, unsigned int len, struct iattr *sattr)
2863 struct nfs4_exception exception = { };
2866 err = nfs4_handle_exception(NFS_SERVER(dir),
2867 _nfs4_proc_symlink(dir, dentry, page,
2870 } while (exception.retry);
2874 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2875 struct iattr *sattr)
2877 struct nfs4_createdata *data;
2878 int status = -ENOMEM;
2880 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2884 status = nfs4_do_create(dir, dentry, data);
2886 nfs4_free_createdata(data);
2891 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2892 struct iattr *sattr)
2894 struct nfs4_exception exception = { };
2897 err = nfs4_handle_exception(NFS_SERVER(dir),
2898 _nfs4_proc_mkdir(dir, dentry, sattr),
2900 } while (exception.retry);
2904 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2905 u64 cookie, struct page *page, unsigned int count, int plus)
2907 struct inode *dir = dentry->d_inode;
2908 struct nfs4_readdir_arg args = {
2913 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2915 struct nfs4_readdir_res res;
2916 struct rpc_message msg = {
2917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2924 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2925 dentry->d_parent->d_name.name,
2926 dentry->d_name.name,
2927 (unsigned long long)cookie);
2928 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2929 res.pgbase = args.pgbase;
2930 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2932 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2934 nfs_invalidate_atime(dir);
2936 dprintk("%s: returns %d\n", __func__, status);
2940 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2941 u64 cookie, struct page *page, unsigned int count, int plus)
2943 struct nfs4_exception exception = { };
2946 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2947 _nfs4_proc_readdir(dentry, cred, cookie,
2950 } while (exception.retry);
2954 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2955 struct iattr *sattr, dev_t rdev)
2957 struct nfs4_createdata *data;
2958 int mode = sattr->ia_mode;
2959 int status = -ENOMEM;
2961 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2962 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2964 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2969 data->arg.ftype = NF4FIFO;
2970 else if (S_ISBLK(mode)) {
2971 data->arg.ftype = NF4BLK;
2972 data->arg.u.device.specdata1 = MAJOR(rdev);
2973 data->arg.u.device.specdata2 = MINOR(rdev);
2975 else if (S_ISCHR(mode)) {
2976 data->arg.ftype = NF4CHR;
2977 data->arg.u.device.specdata1 = MAJOR(rdev);
2978 data->arg.u.device.specdata2 = MINOR(rdev);
2981 status = nfs4_do_create(dir, dentry, data);
2983 nfs4_free_createdata(data);
2988 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2989 struct iattr *sattr, dev_t rdev)
2991 struct nfs4_exception exception = { };
2994 err = nfs4_handle_exception(NFS_SERVER(dir),
2995 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2997 } while (exception.retry);
3001 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3002 struct nfs_fsstat *fsstat)
3004 struct nfs4_statfs_arg args = {
3006 .bitmask = server->attr_bitmask,
3008 struct nfs4_statfs_res res = {
3011 struct rpc_message msg = {
3012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3017 nfs_fattr_init(fsstat->fattr);
3018 return nfs4_call_sync(server, &msg, &args, &res, 0);
3021 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3023 struct nfs4_exception exception = { };
3026 err = nfs4_handle_exception(server,
3027 _nfs4_proc_statfs(server, fhandle, fsstat),
3029 } while (exception.retry);
3033 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3034 struct nfs_fsinfo *fsinfo)
3036 struct nfs4_fsinfo_arg args = {
3038 .bitmask = server->attr_bitmask,
3040 struct nfs4_fsinfo_res res = {
3043 struct rpc_message msg = {
3044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3049 return nfs4_call_sync(server, &msg, &args, &res, 0);
3052 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3054 struct nfs4_exception exception = { };
3058 err = nfs4_handle_exception(server,
3059 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3061 } while (exception.retry);
3065 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3067 nfs_fattr_init(fsinfo->fattr);
3068 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3071 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3072 struct nfs_pathconf *pathconf)
3074 struct nfs4_pathconf_arg args = {
3076 .bitmask = server->attr_bitmask,
3078 struct nfs4_pathconf_res res = {
3079 .pathconf = pathconf,
3081 struct rpc_message msg = {
3082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3087 /* None of the pathconf attributes are mandatory to implement */
3088 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3089 memset(pathconf, 0, sizeof(*pathconf));
3093 nfs_fattr_init(pathconf->fattr);
3094 return nfs4_call_sync(server, &msg, &args, &res, 0);
3097 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3098 struct nfs_pathconf *pathconf)
3100 struct nfs4_exception exception = { };
3104 err = nfs4_handle_exception(server,
3105 _nfs4_proc_pathconf(server, fhandle, pathconf),
3107 } while (exception.retry);
3111 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3113 struct nfs_server *server = NFS_SERVER(data->inode);
3115 dprintk("--> %s\n", __func__);
3117 if (!nfs4_sequence_done(task, &data->res.seq_res))
3120 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3121 nfs_restart_rpc(task, server->nfs_client);
3125 nfs_invalidate_atime(data->inode);
3126 if (task->tk_status > 0)
3127 renew_lease(server, data->timestamp);
3131 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3133 data->timestamp = jiffies;
3134 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3137 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3139 struct inode *inode = data->inode;
3141 if (!nfs4_sequence_done(task, &data->res.seq_res))
3144 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3145 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3148 if (task->tk_status >= 0) {
3149 renew_lease(NFS_SERVER(inode), data->timestamp);
3150 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3155 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3157 struct nfs_server *server = NFS_SERVER(data->inode);
3159 data->args.bitmask = server->cache_consistency_bitmask;
3160 data->res.server = server;
3161 data->timestamp = jiffies;
3163 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3166 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3168 struct inode *inode = data->inode;
3170 if (!nfs4_sequence_done(task, &data->res.seq_res))
3173 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3174 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3177 nfs_refresh_inode(inode, data->res.fattr);
3181 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3183 struct nfs_server *server = NFS_SERVER(data->inode);
3185 data->args.bitmask = server->cache_consistency_bitmask;
3186 data->res.server = server;
3187 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3190 struct nfs4_renewdata {
3191 struct nfs_client *client;
3192 unsigned long timestamp;
3196 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3197 * standalone procedure for queueing an asynchronous RENEW.
3199 static void nfs4_renew_release(void *calldata)
3201 struct nfs4_renewdata *data = calldata;
3202 struct nfs_client *clp = data->client;
3204 if (atomic_read(&clp->cl_count) > 1)
3205 nfs4_schedule_state_renewal(clp);
3206 nfs_put_client(clp);
3210 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3212 struct nfs4_renewdata *data = calldata;
3213 struct nfs_client *clp = data->client;
3214 unsigned long timestamp = data->timestamp;
3216 if (task->tk_status < 0) {
3217 /* Unless we're shutting down, schedule state recovery! */
3218 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3219 nfs4_schedule_state_recovery(clp);
3222 spin_lock(&clp->cl_lock);
3223 if (time_before(clp->cl_last_renewal,timestamp))
3224 clp->cl_last_renewal = timestamp;
3225 spin_unlock(&clp->cl_lock);
3228 static const struct rpc_call_ops nfs4_renew_ops = {
3229 .rpc_call_done = nfs4_renew_done,
3230 .rpc_release = nfs4_renew_release,
3233 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3235 struct rpc_message msg = {
3236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3240 struct nfs4_renewdata *data;
3242 if (!atomic_inc_not_zero(&clp->cl_count))
3244 data = kmalloc(sizeof(*data), GFP_KERNEL);
3248 data->timestamp = jiffies;
3249 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3250 &nfs4_renew_ops, data);
3253 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3255 struct rpc_message msg = {
3256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3260 unsigned long now = jiffies;
3263 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3266 spin_lock(&clp->cl_lock);
3267 if (time_before(clp->cl_last_renewal,now))
3268 clp->cl_last_renewal = now;
3269 spin_unlock(&clp->cl_lock);
3273 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3275 return (server->caps & NFS_CAP_ACLS)
3276 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3277 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3280 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3281 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3284 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3286 static void buf_to_pages(const void *buf, size_t buflen,
3287 struct page **pages, unsigned int *pgbase)
3289 const void *p = buf;
3291 *pgbase = offset_in_page(buf);
3293 while (p < buf + buflen) {
3294 *(pages++) = virt_to_page(p);
3295 p += PAGE_CACHE_SIZE;
3299 struct nfs4_cached_acl {
3305 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3307 struct nfs_inode *nfsi = NFS_I(inode);
3309 spin_lock(&inode->i_lock);
3310 kfree(nfsi->nfs4_acl);
3311 nfsi->nfs4_acl = acl;
3312 spin_unlock(&inode->i_lock);
3315 static void nfs4_zap_acl_attr(struct inode *inode)
3317 nfs4_set_cached_acl(inode, NULL);
3320 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3322 struct nfs_inode *nfsi = NFS_I(inode);
3323 struct nfs4_cached_acl *acl;
3326 spin_lock(&inode->i_lock);
3327 acl = nfsi->nfs4_acl;
3330 if (buf == NULL) /* user is just asking for length */
3332 if (acl->cached == 0)
3334 ret = -ERANGE; /* see getxattr(2) man page */
3335 if (acl->len > buflen)
3337 memcpy(buf, acl->data, acl->len);
3341 spin_unlock(&inode->i_lock);
3345 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3347 struct nfs4_cached_acl *acl;
3349 if (buf && acl_len <= PAGE_SIZE) {
3350 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3354 memcpy(acl->data, buf, acl_len);
3356 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3363 nfs4_set_cached_acl(inode, acl);
3366 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3368 struct page *pages[NFS4ACL_MAXPAGES];
3369 struct nfs_getaclargs args = {
3370 .fh = NFS_FH(inode),
3374 struct nfs_getaclres res = {
3378 struct rpc_message msg = {
3379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3383 struct page *localpage = NULL;
3386 if (buflen < PAGE_SIZE) {
3387 /* As long as we're doing a round trip to the server anyway,
3388 * let's be prepared for a page of acl data. */
3389 localpage = alloc_page(GFP_KERNEL);
3390 resp_buf = page_address(localpage);
3391 if (localpage == NULL)
3393 args.acl_pages[0] = localpage;
3394 args.acl_pgbase = 0;
3395 args.acl_len = PAGE_SIZE;
3398 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3400 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3403 if (res.acl_len > args.acl_len)
3404 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3406 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3409 if (res.acl_len > buflen)
3412 memcpy(buf, resp_buf, res.acl_len);
3417 __free_page(localpage);
3421 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3423 struct nfs4_exception exception = { };
3426 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3429 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3430 } while (exception.retry);
3434 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3436 struct nfs_server *server = NFS_SERVER(inode);
3439 if (!nfs4_server_supports_acls(server))
3441 ret = nfs_revalidate_inode(server, inode);
3444 ret = nfs4_read_cached_acl(inode, buf, buflen);
3447 return nfs4_get_acl_uncached(inode, buf, buflen);
3450 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3452 struct nfs_server *server = NFS_SERVER(inode);
3453 struct page *pages[NFS4ACL_MAXPAGES];
3454 struct nfs_setaclargs arg = {
3455 .fh = NFS_FH(inode),
3459 struct nfs_setaclres res;
3460 struct rpc_message msg = {
3461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3467 if (!nfs4_server_supports_acls(server))
3469 nfs_inode_return_delegation(inode);
3470 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3471 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3472 nfs_access_zap_cache(inode);
3473 nfs_zap_acl_cache(inode);
3477 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3479 struct nfs4_exception exception = { };
3482 err = nfs4_handle_exception(NFS_SERVER(inode),
3483 __nfs4_proc_set_acl(inode, buf, buflen),
3485 } while (exception.retry);
3490 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3492 struct nfs_client *clp = server->nfs_client;
3494 if (task->tk_status >= 0)
3496 switch(task->tk_status) {
3497 case -NFS4ERR_ADMIN_REVOKED:
3498 case -NFS4ERR_BAD_STATEID:
3499 case -NFS4ERR_OPENMODE:
3502 nfs4_state_mark_reclaim_nograce(clp, state);
3503 goto do_state_recovery;
3504 case -NFS4ERR_STALE_STATEID:
3507 nfs4_state_mark_reclaim_reboot(clp, state);
3508 case -NFS4ERR_STALE_CLIENTID:
3509 case -NFS4ERR_EXPIRED:
3510 goto do_state_recovery;
3511 #if defined(CONFIG_NFS_V4_1)
3512 case -NFS4ERR_BADSESSION:
3513 case -NFS4ERR_BADSLOT:
3514 case -NFS4ERR_BAD_HIGH_SLOT:
3515 case -NFS4ERR_DEADSESSION:
3516 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3517 case -NFS4ERR_SEQ_FALSE_RETRY:
3518 case -NFS4ERR_SEQ_MISORDERED:
3519 dprintk("%s ERROR %d, Reset session\n", __func__,
3521 nfs4_schedule_state_recovery(clp);
3522 task->tk_status = 0;
3524 #endif /* CONFIG_NFS_V4_1 */
3525 case -NFS4ERR_DELAY:
3526 nfs_inc_server_stats(server, NFSIOS_DELAY);
3527 case -NFS4ERR_GRACE:
3529 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3530 task->tk_status = 0;
3532 case -NFS4ERR_OLD_STATEID:
3533 task->tk_status = 0;
3536 task->tk_status = nfs4_map_errors(task->tk_status);
3539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3540 nfs4_schedule_state_recovery(clp);
3541 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3542 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3543 task->tk_status = 0;
3547 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3548 unsigned short port, struct rpc_cred *cred,
3549 struct nfs4_setclientid_res *res)
3551 nfs4_verifier sc_verifier;
3552 struct nfs4_setclientid setclientid = {
3553 .sc_verifier = &sc_verifier,
3556 struct rpc_message msg = {
3557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3558 .rpc_argp = &setclientid,
3566 p = (__be32*)sc_verifier.data;
3567 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3568 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3571 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3572 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3574 rpc_peeraddr2str(clp->cl_rpcclient,
3576 rpc_peeraddr2str(clp->cl_rpcclient,
3578 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3579 clp->cl_id_uniquifier);
3580 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3581 sizeof(setclientid.sc_netid),
3582 rpc_peeraddr2str(clp->cl_rpcclient,
3583 RPC_DISPLAY_NETID));
3584 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3585 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3586 clp->cl_ipaddr, port >> 8, port & 255);
3588 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3589 if (status != -NFS4ERR_CLID_INUSE)
3594 ssleep(clp->cl_lease_time + 1);
3596 if (++clp->cl_id_uniquifier == 0)
3602 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3603 struct nfs4_setclientid_res *arg,
3604 struct rpc_cred *cred)
3606 struct nfs_fsinfo fsinfo;
3607 struct rpc_message msg = {
3608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3610 .rpc_resp = &fsinfo,
3617 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3619 spin_lock(&clp->cl_lock);
3620 clp->cl_lease_time = fsinfo.lease_time * HZ;
3621 clp->cl_last_renewal = now;
3622 spin_unlock(&clp->cl_lock);
3627 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3628 struct nfs4_setclientid_res *arg,
3629 struct rpc_cred *cred)
3634 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3638 case -NFS4ERR_RESOURCE:
3639 /* The IBM lawyers misread another document! */
3640 case -NFS4ERR_DELAY:
3642 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3648 struct nfs4_delegreturndata {
3649 struct nfs4_delegreturnargs args;
3650 struct nfs4_delegreturnres res;
3652 nfs4_stateid stateid;
3653 unsigned long timestamp;
3654 struct nfs_fattr fattr;
3658 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3660 struct nfs4_delegreturndata *data = calldata;
3662 if (!nfs4_sequence_done(task, &data->res.seq_res))
3665 switch (task->tk_status) {
3666 case -NFS4ERR_STALE_STATEID:
3667 case -NFS4ERR_EXPIRED:
3669 renew_lease(data->res.server, data->timestamp);
3672 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3674 nfs_restart_rpc(task, data->res.server->nfs_client);
3678 data->rpc_status = task->tk_status;
3681 static void nfs4_delegreturn_release(void *calldata)
3686 #if defined(CONFIG_NFS_V4_1)
3687 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3689 struct nfs4_delegreturndata *d_data;
3691 d_data = (struct nfs4_delegreturndata *)data;
3693 if (nfs4_setup_sequence(d_data->res.server,
3694 &d_data->args.seq_args,
3695 &d_data->res.seq_res, 1, task))
3697 rpc_call_start(task);
3699 #endif /* CONFIG_NFS_V4_1 */
3701 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3702 #if defined(CONFIG_NFS_V4_1)
3703 .rpc_call_prepare = nfs4_delegreturn_prepare,
3704 #endif /* CONFIG_NFS_V4_1 */
3705 .rpc_call_done = nfs4_delegreturn_done,
3706 .rpc_release = nfs4_delegreturn_release,
3709 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3711 struct nfs4_delegreturndata *data;
3712 struct nfs_server *server = NFS_SERVER(inode);
3713 struct rpc_task *task;
3714 struct rpc_message msg = {
3715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3718 struct rpc_task_setup task_setup_data = {
3719 .rpc_client = server->client,
3720 .rpc_message = &msg,
3721 .callback_ops = &nfs4_delegreturn_ops,
3722 .flags = RPC_TASK_ASYNC,
3726 data = kzalloc(sizeof(*data), GFP_NOFS);
3729 data->args.fhandle = &data->fh;
3730 data->args.stateid = &data->stateid;
3731 data->args.bitmask = server->attr_bitmask;
3732 nfs_copy_fh(&data->fh, NFS_FH(inode));
3733 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3734 data->res.fattr = &data->fattr;
3735 data->res.server = server;
3736 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3737 nfs_fattr_init(data->res.fattr);
3738 data->timestamp = jiffies;
3739 data->rpc_status = 0;
3741 task_setup_data.callback_data = data;
3742 msg.rpc_argp = &data->args,
3743 msg.rpc_resp = &data->res,
3744 task = rpc_run_task(&task_setup_data);
3746 return PTR_ERR(task);
3749 status = nfs4_wait_for_completion_rpc_task(task);
3752 status = data->rpc_status;
3755 nfs_refresh_inode(inode, &data->fattr);
3761 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3763 struct nfs_server *server = NFS_SERVER(inode);
3764 struct nfs4_exception exception = { };
3767 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3769 case -NFS4ERR_STALE_STATEID:
3770 case -NFS4ERR_EXPIRED:
3774 err = nfs4_handle_exception(server, err, &exception);
3775 } while (exception.retry);
3779 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3780 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3783 * sleep, with exponential backoff, and retry the LOCK operation.
3785 static unsigned long
3786 nfs4_set_lock_task_retry(unsigned long timeout)
3788 schedule_timeout_killable(timeout);
3790 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3791 return NFS4_LOCK_MAXTIMEOUT;
3795 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3797 struct inode *inode = state->inode;
3798 struct nfs_server *server = NFS_SERVER(inode);
3799 struct nfs_client *clp = server->nfs_client;
3800 struct nfs_lockt_args arg = {
3801 .fh = NFS_FH(inode),
3804 struct nfs_lockt_res res = {
3807 struct rpc_message msg = {
3808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3811 .rpc_cred = state->owner->so_cred,
3813 struct nfs4_lock_state *lsp;
3816 arg.lock_owner.clientid = clp->cl_clientid;
3817 status = nfs4_set_lock_state(state, request);
3820 lsp = request->fl_u.nfs4_fl.owner;
3821 arg.lock_owner.id = lsp->ls_id.id;
3822 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3825 request->fl_type = F_UNLCK;
3827 case -NFS4ERR_DENIED:
3830 request->fl_ops->fl_release_private(request);
3835 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3837 struct nfs4_exception exception = { };
3841 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3842 _nfs4_proc_getlk(state, cmd, request),
3844 } while (exception.retry);
3848 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3851 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3853 res = posix_lock_file_wait(file, fl);
3856 res = flock_lock_file_wait(file, fl);
3864 struct nfs4_unlockdata {
3865 struct nfs_locku_args arg;
3866 struct nfs_locku_res res;
3867 struct nfs4_lock_state *lsp;
3868 struct nfs_open_context *ctx;
3869 struct file_lock fl;
3870 const struct nfs_server *server;
3871 unsigned long timestamp;
3874 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3875 struct nfs_open_context *ctx,
3876 struct nfs4_lock_state *lsp,
3877 struct nfs_seqid *seqid)
3879 struct nfs4_unlockdata *p;
3880 struct inode *inode = lsp->ls_state->inode;
3882 p = kzalloc(sizeof(*p), GFP_NOFS);
3885 p->arg.fh = NFS_FH(inode);
3887 p->arg.seqid = seqid;
3888 p->res.seqid = seqid;
3889 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3890 p->arg.stateid = &lsp->ls_stateid;
3892 atomic_inc(&lsp->ls_count);
3893 /* Ensure we don't close file until we're done freeing locks! */
3894 p->ctx = get_nfs_open_context(ctx);
3895 memcpy(&p->fl, fl, sizeof(p->fl));
3896 p->server = NFS_SERVER(inode);
3900 static void nfs4_locku_release_calldata(void *data)
3902 struct nfs4_unlockdata *calldata = data;
3903 nfs_free_seqid(calldata->arg.seqid);
3904 nfs4_put_lock_state(calldata->lsp);
3905 put_nfs_open_context(calldata->ctx);
3909 static void nfs4_locku_done(struct rpc_task *task, void *data)
3911 struct nfs4_unlockdata *calldata = data;
3913 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3915 if (RPC_ASSASSINATED(task))
3917 switch (task->tk_status) {
3919 memcpy(calldata->lsp->ls_stateid.data,
3920 calldata->res.stateid.data,
3921 sizeof(calldata->lsp->ls_stateid.data));
3922 renew_lease(calldata->server, calldata->timestamp);
3924 case -NFS4ERR_BAD_STATEID:
3925 case -NFS4ERR_OLD_STATEID:
3926 case -NFS4ERR_STALE_STATEID:
3927 case -NFS4ERR_EXPIRED:
3930 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3931 nfs_restart_rpc(task,
3932 calldata->server->nfs_client);
3936 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3938 struct nfs4_unlockdata *calldata = data;
3940 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3942 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3943 /* Note: exit _without_ running nfs4_locku_done */
3944 task->tk_action = NULL;
3947 calldata->timestamp = jiffies;
3948 if (nfs4_setup_sequence(calldata->server,
3949 &calldata->arg.seq_args,
3950 &calldata->res.seq_res, 1, task))
3952 rpc_call_start(task);
3955 static const struct rpc_call_ops nfs4_locku_ops = {
3956 .rpc_call_prepare = nfs4_locku_prepare,
3957 .rpc_call_done = nfs4_locku_done,
3958 .rpc_release = nfs4_locku_release_calldata,
3961 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3962 struct nfs_open_context *ctx,
3963 struct nfs4_lock_state *lsp,
3964 struct nfs_seqid *seqid)
3966 struct nfs4_unlockdata *data;
3967 struct rpc_message msg = {
3968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3969 .rpc_cred = ctx->cred,
3971 struct rpc_task_setup task_setup_data = {
3972 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3973 .rpc_message = &msg,
3974 .callback_ops = &nfs4_locku_ops,
3975 .workqueue = nfsiod_workqueue,
3976 .flags = RPC_TASK_ASYNC,
3979 /* Ensure this is an unlock - when canceling a lock, the
3980 * canceled lock is passed in, and it won't be an unlock.
3982 fl->fl_type = F_UNLCK;
3984 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3986 nfs_free_seqid(seqid);
3987 return ERR_PTR(-ENOMEM);
3990 msg.rpc_argp = &data->arg,
3991 msg.rpc_resp = &data->res,
3992 task_setup_data.callback_data = data;
3993 return rpc_run_task(&task_setup_data);
3996 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3998 struct nfs_inode *nfsi = NFS_I(state->inode);
3999 struct nfs_seqid *seqid;
4000 struct nfs4_lock_state *lsp;
4001 struct rpc_task *task;
4003 unsigned char fl_flags = request->fl_flags;
4005 status = nfs4_set_lock_state(state, request);
4006 /* Unlock _before_ we do the RPC call */
4007 request->fl_flags |= FL_EXISTS;
4008 down_read(&nfsi->rwsem);
4009 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4010 up_read(&nfsi->rwsem);
4013 up_read(&nfsi->rwsem);
4016 /* Is this a delegated lock? */
4017 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4019 lsp = request->fl_u.nfs4_fl.owner;
4020 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4024 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4025 status = PTR_ERR(task);
4028 status = nfs4_wait_for_completion_rpc_task(task);
4031 request->fl_flags = fl_flags;
4035 struct nfs4_lockdata {
4036 struct nfs_lock_args arg;
4037 struct nfs_lock_res res;
4038 struct nfs4_lock_state *lsp;
4039 struct nfs_open_context *ctx;
4040 struct file_lock fl;
4041 unsigned long timestamp;
4044 struct nfs_server *server;
4047 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4048 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4051 struct nfs4_lockdata *p;
4052 struct inode *inode = lsp->ls_state->inode;
4053 struct nfs_server *server = NFS_SERVER(inode);
4055 p = kzalloc(sizeof(*p), gfp_mask);
4059 p->arg.fh = NFS_FH(inode);
4061 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4062 if (p->arg.open_seqid == NULL)
4064 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4065 if (p->arg.lock_seqid == NULL)
4066 goto out_free_seqid;
4067 p->arg.lock_stateid = &lsp->ls_stateid;
4068 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4069 p->arg.lock_owner.id = lsp->ls_id.id;
4070 p->res.lock_seqid = p->arg.lock_seqid;
4071 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4074 atomic_inc(&lsp->ls_count);
4075 p->ctx = get_nfs_open_context(ctx);
4076 memcpy(&p->fl, fl, sizeof(p->fl));
4079 nfs_free_seqid(p->arg.open_seqid);
4085 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4087 struct nfs4_lockdata *data = calldata;
4088 struct nfs4_state *state = data->lsp->ls_state;
4090 dprintk("%s: begin!\n", __func__);
4091 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4093 /* Do we need to do an open_to_lock_owner? */
4094 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4095 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4097 data->arg.open_stateid = &state->stateid;
4098 data->arg.new_lock_owner = 1;
4099 data->res.open_seqid = data->arg.open_seqid;
4101 data->arg.new_lock_owner = 0;
4102 data->timestamp = jiffies;
4103 if (nfs4_setup_sequence(data->server,
4104 &data->arg.seq_args,
4105 &data->res.seq_res, 1, task))
4107 rpc_call_start(task);
4108 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4111 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4113 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4114 nfs4_lock_prepare(task, calldata);
4117 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4119 struct nfs4_lockdata *data = calldata;
4121 dprintk("%s: begin!\n", __func__);
4123 if (!nfs4_sequence_done(task, &data->res.seq_res))
4126 data->rpc_status = task->tk_status;
4127 if (RPC_ASSASSINATED(task))
4129 if (data->arg.new_lock_owner != 0) {
4130 if (data->rpc_status == 0)
4131 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4135 if (data->rpc_status == 0) {
4136 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4137 sizeof(data->lsp->ls_stateid.data));
4138 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4139 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4142 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4145 static void nfs4_lock_release(void *calldata)
4147 struct nfs4_lockdata *data = calldata;
4149 dprintk("%s: begin!\n", __func__);
4150 nfs_free_seqid(data->arg.open_seqid);
4151 if (data->cancelled != 0) {
4152 struct rpc_task *task;
4153 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4154 data->arg.lock_seqid);
4157 dprintk("%s: cancelling lock!\n", __func__);
4159 nfs_free_seqid(data->arg.lock_seqid);
4160 nfs4_put_lock_state(data->lsp);
4161 put_nfs_open_context(data->ctx);
4163 dprintk("%s: done!\n", __func__);
4166 static const struct rpc_call_ops nfs4_lock_ops = {
4167 .rpc_call_prepare = nfs4_lock_prepare,
4168 .rpc_call_done = nfs4_lock_done,
4169 .rpc_release = nfs4_lock_release,
4172 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4173 .rpc_call_prepare = nfs4_recover_lock_prepare,
4174 .rpc_call_done = nfs4_lock_done,
4175 .rpc_release = nfs4_lock_release,
4178 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4180 struct nfs_client *clp = server->nfs_client;
4181 struct nfs4_state *state = lsp->ls_state;
4184 case -NFS4ERR_ADMIN_REVOKED:
4185 case -NFS4ERR_BAD_STATEID:
4186 case -NFS4ERR_EXPIRED:
4187 if (new_lock_owner != 0 ||
4188 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4189 nfs4_state_mark_reclaim_nograce(clp, state);
4190 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4192 case -NFS4ERR_STALE_STATEID:
4193 if (new_lock_owner != 0 ||
4194 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4195 nfs4_state_mark_reclaim_reboot(clp, state);
4196 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4200 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4202 struct nfs4_lockdata *data;
4203 struct rpc_task *task;
4204 struct rpc_message msg = {
4205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4206 .rpc_cred = state->owner->so_cred,
4208 struct rpc_task_setup task_setup_data = {
4209 .rpc_client = NFS_CLIENT(state->inode),
4210 .rpc_message = &msg,
4211 .callback_ops = &nfs4_lock_ops,
4212 .workqueue = nfsiod_workqueue,
4213 .flags = RPC_TASK_ASYNC,
4217 dprintk("%s: begin!\n", __func__);
4218 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4219 fl->fl_u.nfs4_fl.owner,
4220 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4224 data->arg.block = 1;
4225 if (recovery_type > NFS_LOCK_NEW) {
4226 if (recovery_type == NFS_LOCK_RECLAIM)
4227 data->arg.reclaim = NFS_LOCK_RECLAIM;
4228 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4230 msg.rpc_argp = &data->arg,
4231 msg.rpc_resp = &data->res,
4232 task_setup_data.callback_data = data;
4233 task = rpc_run_task(&task_setup_data);
4235 return PTR_ERR(task);
4236 ret = nfs4_wait_for_completion_rpc_task(task);
4238 ret = data->rpc_status;
4240 nfs4_handle_setlk_error(data->server, data->lsp,
4241 data->arg.new_lock_owner, ret);
4243 data->cancelled = 1;
4245 dprintk("%s: done, ret = %d!\n", __func__, ret);
4249 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4251 struct nfs_server *server = NFS_SERVER(state->inode);
4252 struct nfs4_exception exception = { };
4256 /* Cache the lock if possible... */
4257 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4259 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4260 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
4262 nfs4_handle_exception(server, err, &exception);
4263 } while (exception.retry);
4267 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4269 struct nfs_server *server = NFS_SERVER(state->inode);
4270 struct nfs4_exception exception = { };
4273 err = nfs4_set_lock_state(state, request);
4277 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4279 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4283 case -NFS4ERR_GRACE:
4284 case -NFS4ERR_DELAY:
4286 nfs4_handle_exception(server, err, &exception);
4289 } while (exception.retry);
4294 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4296 struct nfs_inode *nfsi = NFS_I(state->inode);
4297 unsigned char fl_flags = request->fl_flags;
4298 int status = -ENOLCK;
4300 if ((fl_flags & FL_POSIX) &&
4301 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4303 /* Is this a delegated open? */
4304 status = nfs4_set_lock_state(state, request);
4307 request->fl_flags |= FL_ACCESS;
4308 status = do_vfs_lock(request->fl_file, request);
4311 down_read(&nfsi->rwsem);
4312 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4313 /* Yes: cache locks! */
4314 /* ...but avoid races with delegation recall... */
4315 request->fl_flags = fl_flags & ~FL_SLEEP;
4316 status = do_vfs_lock(request->fl_file, request);
4319 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4322 /* Note: we always want to sleep here! */
4323 request->fl_flags = fl_flags | FL_SLEEP;
4324 if (do_vfs_lock(request->fl_file, request) < 0)
4325 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4327 up_read(&nfsi->rwsem);
4329 request->fl_flags = fl_flags;
4333 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4335 struct nfs4_exception exception = { };
4339 err = _nfs4_proc_setlk(state, cmd, request);
4340 if (err == -NFS4ERR_DENIED)
4342 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4344 } while (exception.retry);
4349 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4351 struct nfs_open_context *ctx;
4352 struct nfs4_state *state;
4353 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4356 /* verify open state */
4357 ctx = nfs_file_open_context(filp);
4360 if (request->fl_start < 0 || request->fl_end < 0)
4363 if (IS_GETLK(cmd)) {
4365 return nfs4_proc_getlk(state, F_GETLK, request);
4369 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4372 if (request->fl_type == F_UNLCK) {
4374 return nfs4_proc_unlck(state, cmd, request);
4381 status = nfs4_proc_setlk(state, cmd, request);
4382 if ((status != -EAGAIN) || IS_SETLK(cmd))
4384 timeout = nfs4_set_lock_task_retry(timeout);
4385 status = -ERESTARTSYS;
4388 } while(status < 0);
4392 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4394 struct nfs_server *server = NFS_SERVER(state->inode);
4395 struct nfs4_exception exception = { };
4398 err = nfs4_set_lock_state(state, fl);
4402 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4405 printk(KERN_ERR "%s: unhandled error %d.\n",
4410 case -NFS4ERR_EXPIRED:
4411 case -NFS4ERR_STALE_CLIENTID:
4412 case -NFS4ERR_STALE_STATEID:
4413 case -NFS4ERR_BADSESSION:
4414 case -NFS4ERR_BADSLOT:
4415 case -NFS4ERR_BAD_HIGH_SLOT:
4416 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4417 case -NFS4ERR_DEADSESSION:
4418 nfs4_schedule_state_recovery(server->nfs_client);
4422 * The show must go on: exit, but mark the
4423 * stateid as needing recovery.
4425 case -NFS4ERR_ADMIN_REVOKED:
4426 case -NFS4ERR_BAD_STATEID:
4427 case -NFS4ERR_OPENMODE:
4428 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4432 case -NFS4ERR_DENIED:
4433 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4436 case -NFS4ERR_DELAY:
4440 err = nfs4_handle_exception(server, err, &exception);
4441 } while (exception.retry);
4446 static void nfs4_release_lockowner_release(void *calldata)
4451 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4452 .rpc_release = nfs4_release_lockowner_release,
4455 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4457 struct nfs_server *server = lsp->ls_state->owner->so_server;
4458 struct nfs_release_lockowner_args *args;
4459 struct rpc_message msg = {
4460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4463 if (server->nfs_client->cl_mvops->minor_version != 0)
4465 args = kmalloc(sizeof(*args), GFP_NOFS);
4468 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4469 args->lock_owner.id = lsp->ls_id.id;
4470 msg.rpc_argp = args;
4471 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4474 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4476 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4477 size_t buflen, int flags)
4479 struct inode *inode = dentry->d_inode;
4481 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4484 return nfs4_proc_set_acl(inode, buf, buflen);
4487 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4488 * and that's what we'll do for e.g. user attributes that haven't been set.
4489 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4490 * attributes in kernel-managed attribute namespaces. */
4491 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4494 struct inode *inode = dentry->d_inode;
4496 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4499 return nfs4_proc_get_acl(inode, buf, buflen);
4502 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4504 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4506 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4508 if (buf && buflen < len)
4511 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4515 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4517 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4518 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4519 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4522 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4523 NFS_ATTR_FATTR_NLINK;
4524 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4528 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4529 struct nfs4_fs_locations *fs_locations, struct page *page)
4531 struct nfs_server *server = NFS_SERVER(dir);
4533 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4534 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4536 struct nfs4_fs_locations_arg args = {
4537 .dir_fh = NFS_FH(dir),
4542 struct nfs4_fs_locations_res res = {
4543 .fs_locations = fs_locations,
4545 struct rpc_message msg = {
4546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4552 dprintk("%s: start\n", __func__);
4553 nfs_fattr_init(&fs_locations->fattr);
4554 fs_locations->server = server;
4555 fs_locations->nlocations = 0;
4556 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4557 nfs_fixup_referral_attributes(&fs_locations->fattr);
4558 dprintk("%s: returned status = %d\n", __func__, status);
4562 #ifdef CONFIG_NFS_V4_1
4564 * nfs4_proc_exchange_id()
4566 * Since the clientid has expired, all compounds using sessions
4567 * associated with the stale clientid will be returning
4568 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4569 * be in some phase of session reset.
4571 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4573 nfs4_verifier verifier;
4574 struct nfs41_exchange_id_args args = {
4576 .flags = clp->cl_exchange_flags,
4578 struct nfs41_exchange_id_res res = {
4582 struct rpc_message msg = {
4583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4590 dprintk("--> %s\n", __func__);
4591 BUG_ON(clp == NULL);
4593 /* Remove server-only flags */
4594 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4596 p = (u32 *)verifier.data;
4597 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4598 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4599 args.verifier = &verifier;
4602 args.id_len = scnprintf(args.id, sizeof(args.id),
4605 rpc_peeraddr2str(clp->cl_rpcclient,
4607 clp->cl_id_uniquifier);
4609 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4611 if (status != -NFS4ERR_CLID_INUSE)
4617 if (++clp->cl_id_uniquifier == 0)
4621 dprintk("<-- %s status= %d\n", __func__, status);
4625 struct nfs4_get_lease_time_data {
4626 struct nfs4_get_lease_time_args *args;
4627 struct nfs4_get_lease_time_res *res;
4628 struct nfs_client *clp;
4631 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4635 struct nfs4_get_lease_time_data *data =
4636 (struct nfs4_get_lease_time_data *)calldata;
4638 dprintk("--> %s\n", __func__);
4639 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4640 /* just setup sequence, do not trigger session recovery
4641 since we're invoked within one */
4642 ret = nfs41_setup_sequence(data->clp->cl_session,
4643 &data->args->la_seq_args,
4644 &data->res->lr_seq_res, 0, task);
4646 BUG_ON(ret == -EAGAIN);
4647 rpc_call_start(task);
4648 dprintk("<-- %s\n", __func__);
4652 * Called from nfs4_state_manager thread for session setup, so don't recover
4653 * from sequence operation or clientid errors.
4655 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4657 struct nfs4_get_lease_time_data *data =
4658 (struct nfs4_get_lease_time_data *)calldata;
4660 dprintk("--> %s\n", __func__);
4661 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4663 switch (task->tk_status) {
4664 case -NFS4ERR_DELAY:
4665 case -NFS4ERR_GRACE:
4667 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4668 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4669 task->tk_status = 0;
4670 nfs_restart_rpc(task, data->clp);
4673 dprintk("<-- %s\n", __func__);
4676 struct rpc_call_ops nfs4_get_lease_time_ops = {
4677 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4678 .rpc_call_done = nfs4_get_lease_time_done,
4681 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4683 struct rpc_task *task;
4684 struct nfs4_get_lease_time_args args;
4685 struct nfs4_get_lease_time_res res = {
4686 .lr_fsinfo = fsinfo,
4688 struct nfs4_get_lease_time_data data = {
4693 struct rpc_message msg = {
4694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4698 struct rpc_task_setup task_setup = {
4699 .rpc_client = clp->cl_rpcclient,
4700 .rpc_message = &msg,
4701 .callback_ops = &nfs4_get_lease_time_ops,
4702 .callback_data = &data
4706 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4707 dprintk("--> %s\n", __func__);
4708 task = rpc_run_task(&task_setup);
4711 status = PTR_ERR(task);
4713 status = task->tk_status;
4716 dprintk("<-- %s return %d\n", __func__, status);
4722 * Reset a slot table
4724 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4727 struct nfs4_slot *new = NULL;
4731 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4732 max_reqs, tbl->max_slots);
4734 /* Does the newly negotiated max_reqs match the existing slot table? */
4735 if (max_reqs != tbl->max_slots) {
4737 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4744 spin_lock(&tbl->slot_tbl_lock);
4747 tbl->max_slots = max_reqs;
4749 for (i = 0; i < tbl->max_slots; ++i)
4750 tbl->slots[i].seq_nr = ivalue;
4751 spin_unlock(&tbl->slot_tbl_lock);
4752 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4753 tbl, tbl->slots, tbl->max_slots);
4755 dprintk("<-- %s: return %d\n", __func__, ret);
4760 * Reset the forechannel and backchannel slot tables
4762 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4766 status = nfs4_reset_slot_table(&session->fc_slot_table,
4767 session->fc_attrs.max_reqs, 1);
4771 status = nfs4_reset_slot_table(&session->bc_slot_table,
4772 session->bc_attrs.max_reqs, 0);
4776 /* Destroy the slot table */
4777 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4779 if (session->fc_slot_table.slots != NULL) {
4780 kfree(session->fc_slot_table.slots);
4781 session->fc_slot_table.slots = NULL;
4783 if (session->bc_slot_table.slots != NULL) {
4784 kfree(session->bc_slot_table.slots);
4785 session->bc_slot_table.slots = NULL;
4791 * Initialize slot table
4793 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4794 int max_slots, int ivalue)
4796 struct nfs4_slot *slot;
4799 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4801 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4803 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4808 spin_lock(&tbl->slot_tbl_lock);
4809 tbl->max_slots = max_slots;
4811 tbl->highest_used_slotid = -1; /* no slot is currently used */
4812 spin_unlock(&tbl->slot_tbl_lock);
4813 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4814 tbl, tbl->slots, tbl->max_slots);
4816 dprintk("<-- %s: return %d\n", __func__, ret);
4821 * Initialize the forechannel and backchannel tables
4823 static int nfs4_init_slot_tables(struct nfs4_session *session)
4825 struct nfs4_slot_table *tbl;
4828 tbl = &session->fc_slot_table;
4829 if (tbl->slots == NULL) {
4830 status = nfs4_init_slot_table(tbl,
4831 session->fc_attrs.max_reqs, 1);
4836 tbl = &session->bc_slot_table;
4837 if (tbl->slots == NULL) {
4838 status = nfs4_init_slot_table(tbl,
4839 session->bc_attrs.max_reqs, 0);
4841 nfs4_destroy_slot_tables(session);
4847 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4849 struct nfs4_session *session;
4850 struct nfs4_slot_table *tbl;
4852 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
4856 init_completion(&session->complete);
4858 tbl = &session->fc_slot_table;
4859 tbl->highest_used_slotid = -1;
4860 spin_lock_init(&tbl->slot_tbl_lock);
4861 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4863 tbl = &session->bc_slot_table;
4864 tbl->highest_used_slotid = -1;
4865 spin_lock_init(&tbl->slot_tbl_lock);
4866 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4868 session->session_state = 1<<NFS4_SESSION_INITING;
4874 void nfs4_destroy_session(struct nfs4_session *session)
4876 nfs4_proc_destroy_session(session);
4877 dprintk("%s Destroy backchannel for xprt %p\n",
4878 __func__, session->clp->cl_rpcclient->cl_xprt);
4879 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4880 NFS41_BC_MIN_CALLBACKS);
4881 nfs4_destroy_slot_tables(session);
4886 * Initialize the values to be used by the client in CREATE_SESSION
4887 * If nfs4_init_session set the fore channel request and response sizes,
4890 * Set the back channel max_resp_sz_cached to zero to force the client to
4891 * always set csa_cachethis to FALSE because the current implementation
4892 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4894 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4896 struct nfs4_session *session = args->client->cl_session;
4897 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4898 mxresp_sz = session->fc_attrs.max_resp_sz;
4901 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4903 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4904 /* Fore channel attributes */
4905 args->fc_attrs.headerpadsz = 0;
4906 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4907 args->fc_attrs.max_resp_sz = mxresp_sz;
4908 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4909 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4911 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4912 "max_ops=%u max_reqs=%u\n",
4914 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4915 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
4917 /* Back channel attributes */
4918 args->bc_attrs.headerpadsz = 0;
4919 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4920 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4921 args->bc_attrs.max_resp_sz_cached = 0;
4922 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4923 args->bc_attrs.max_reqs = 1;
4925 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4926 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4928 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4929 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4930 args->bc_attrs.max_reqs);
4933 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4937 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4938 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4942 #define _verify_fore_channel_attr(_name_) \
4943 _verify_channel_attr("fore", #_name_, \
4944 args->fc_attrs._name_, \
4945 session->fc_attrs._name_)
4947 #define _verify_back_channel_attr(_name_) \
4948 _verify_channel_attr("back", #_name_, \
4949 args->bc_attrs._name_, \
4950 session->bc_attrs._name_)
4953 * The server is not allowed to increase the fore channel header pad size,
4954 * maximum response size, or maximum number of operations.
4956 * The back channel attributes are only negotiatied down: We send what the
4957 * (back channel) server insists upon.
4959 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4960 struct nfs4_session *session)
4964 ret |= _verify_fore_channel_attr(headerpadsz);
4965 ret |= _verify_fore_channel_attr(max_resp_sz);
4966 ret |= _verify_fore_channel_attr(max_ops);
4968 ret |= _verify_back_channel_attr(headerpadsz);
4969 ret |= _verify_back_channel_attr(max_rqst_sz);
4970 ret |= _verify_back_channel_attr(max_resp_sz);
4971 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4972 ret |= _verify_back_channel_attr(max_ops);
4973 ret |= _verify_back_channel_attr(max_reqs);
4978 static int _nfs4_proc_create_session(struct nfs_client *clp)
4980 struct nfs4_session *session = clp->cl_session;
4981 struct nfs41_create_session_args args = {
4983 .cb_program = NFS4_CALLBACK,
4985 struct nfs41_create_session_res res = {
4988 struct rpc_message msg = {
4989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4995 nfs4_init_channel_attrs(&args);
4996 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4998 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5001 /* Verify the session's negotiated channel_attrs values */
5002 status = nfs4_verify_channel_attrs(&args, session);
5004 /* Increment the clientid slot sequence id */
5012 * Issues a CREATE_SESSION operation to the server.
5013 * It is the responsibility of the caller to verify the session is
5014 * expired before calling this routine.
5016 int nfs4_proc_create_session(struct nfs_client *clp)
5020 struct nfs4_session *session = clp->cl_session;
5022 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5024 status = _nfs4_proc_create_session(clp);
5028 /* Init and reset the fore channel */
5029 status = nfs4_init_slot_tables(session);
5030 dprintk("slot table initialization returned %d\n", status);
5033 status = nfs4_reset_slot_tables(session);
5034 dprintk("slot table reset returned %d\n", status);
5038 ptr = (unsigned *)&session->sess_id.data[0];
5039 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5040 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5042 dprintk("<-- %s\n", __func__);
5047 * Issue the over-the-wire RPC DESTROY_SESSION.
5048 * The caller must serialize access to this routine.
5050 int nfs4_proc_destroy_session(struct nfs4_session *session)
5053 struct rpc_message msg;
5055 dprintk("--> nfs4_proc_destroy_session\n");
5057 /* session is still being setup */
5058 if (session->clp->cl_cons_state != NFS_CS_READY)
5061 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5062 msg.rpc_argp = session;
5063 msg.rpc_resp = NULL;
5064 msg.rpc_cred = NULL;
5065 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5069 "Got error %d from the server on DESTROY_SESSION. "
5070 "Session has been destroyed regardless...\n", status);
5072 dprintk("<-- nfs4_proc_destroy_session\n");
5076 int nfs4_init_session(struct nfs_server *server)
5078 struct nfs_client *clp = server->nfs_client;
5079 struct nfs4_session *session;
5080 unsigned int rsize, wsize;
5083 if (!nfs4_has_session(clp))
5086 session = clp->cl_session;
5087 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5090 rsize = server->rsize;
5092 rsize = NFS_MAX_FILE_IO_SIZE;
5093 wsize = server->wsize;
5095 wsize = NFS_MAX_FILE_IO_SIZE;
5097 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5098 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5100 ret = nfs4_recover_expired_lease(server);
5102 ret = nfs4_check_client_ready(clp);
5107 * Renew the cl_session lease.
5109 struct nfs4_sequence_data {
5110 struct nfs_client *clp;
5111 struct nfs4_sequence_args args;
5112 struct nfs4_sequence_res res;
5115 static void nfs41_sequence_release(void *data)
5117 struct nfs4_sequence_data *calldata = data;
5118 struct nfs_client *clp = calldata->clp;
5120 if (atomic_read(&clp->cl_count) > 1)
5121 nfs4_schedule_state_renewal(clp);
5122 nfs_put_client(clp);
5126 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5128 switch(task->tk_status) {
5129 case -NFS4ERR_DELAY:
5131 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5134 nfs4_schedule_state_recovery(clp);
5139 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5141 struct nfs4_sequence_data *calldata = data;
5142 struct nfs_client *clp = calldata->clp;
5144 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5147 if (task->tk_status < 0) {
5148 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5149 if (atomic_read(&clp->cl_count) == 1)
5152 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5153 rpc_restart_call_prepare(task);
5157 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5159 dprintk("<-- %s\n", __func__);
5162 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5164 struct nfs4_sequence_data *calldata = data;
5165 struct nfs_client *clp = calldata->clp;
5166 struct nfs4_sequence_args *args;
5167 struct nfs4_sequence_res *res;
5169 args = task->tk_msg.rpc_argp;
5170 res = task->tk_msg.rpc_resp;
5172 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5174 rpc_call_start(task);
5177 static const struct rpc_call_ops nfs41_sequence_ops = {
5178 .rpc_call_done = nfs41_sequence_call_done,
5179 .rpc_call_prepare = nfs41_sequence_prepare,
5180 .rpc_release = nfs41_sequence_release,
5183 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5185 struct nfs4_sequence_data *calldata;
5186 struct rpc_message msg = {
5187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5190 struct rpc_task_setup task_setup_data = {
5191 .rpc_client = clp->cl_rpcclient,
5192 .rpc_message = &msg,
5193 .callback_ops = &nfs41_sequence_ops,
5194 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5197 if (!atomic_inc_not_zero(&clp->cl_count))
5198 return ERR_PTR(-EIO);
5199 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5200 if (calldata == NULL) {
5201 nfs_put_client(clp);
5202 return ERR_PTR(-ENOMEM);
5204 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5205 msg.rpc_argp = &calldata->args;
5206 msg.rpc_resp = &calldata->res;
5207 calldata->clp = clp;
5208 task_setup_data.callback_data = calldata;
5210 return rpc_run_task(&task_setup_data);
5213 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5215 struct rpc_task *task;
5218 task = _nfs41_proc_sequence(clp, cred);
5220 ret = PTR_ERR(task);
5223 dprintk("<-- %s status=%d\n", __func__, ret);
5227 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5229 struct rpc_task *task;
5232 task = _nfs41_proc_sequence(clp, cred);
5234 ret = PTR_ERR(task);
5237 ret = rpc_wait_for_completion_task(task);
5239 ret = task->tk_status;
5242 dprintk("<-- %s status=%d\n", __func__, ret);
5246 struct nfs4_reclaim_complete_data {
5247 struct nfs_client *clp;
5248 struct nfs41_reclaim_complete_args arg;
5249 struct nfs41_reclaim_complete_res res;
5252 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5254 struct nfs4_reclaim_complete_data *calldata = data;
5256 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5257 if (nfs41_setup_sequence(calldata->clp->cl_session,
5258 &calldata->arg.seq_args,
5259 &calldata->res.seq_res, 0, task))
5262 rpc_call_start(task);
5265 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5267 switch(task->tk_status) {
5269 case -NFS4ERR_COMPLETE_ALREADY:
5270 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5272 case -NFS4ERR_DELAY:
5274 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5277 nfs4_schedule_state_recovery(clp);
5282 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5284 struct nfs4_reclaim_complete_data *calldata = data;
5285 struct nfs_client *clp = calldata->clp;
5286 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5288 dprintk("--> %s\n", __func__);
5289 if (!nfs41_sequence_done(task, res))
5292 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5293 rpc_restart_call_prepare(task);
5296 dprintk("<-- %s\n", __func__);
5299 static void nfs4_free_reclaim_complete_data(void *data)
5301 struct nfs4_reclaim_complete_data *calldata = data;
5306 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5307 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5308 .rpc_call_done = nfs4_reclaim_complete_done,
5309 .rpc_release = nfs4_free_reclaim_complete_data,
5313 * Issue a global reclaim complete.
5315 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5317 struct nfs4_reclaim_complete_data *calldata;
5318 struct rpc_task *task;
5319 struct rpc_message msg = {
5320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5322 struct rpc_task_setup task_setup_data = {
5323 .rpc_client = clp->cl_rpcclient,
5324 .rpc_message = &msg,
5325 .callback_ops = &nfs4_reclaim_complete_call_ops,
5326 .flags = RPC_TASK_ASYNC,
5328 int status = -ENOMEM;
5330 dprintk("--> %s\n", __func__);
5331 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5332 if (calldata == NULL)
5334 calldata->clp = clp;
5335 calldata->arg.one_fs = 0;
5336 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5338 msg.rpc_argp = &calldata->arg;
5339 msg.rpc_resp = &calldata->res;
5340 task_setup_data.callback_data = calldata;
5341 task = rpc_run_task(&task_setup_data);
5343 status = PTR_ERR(task);
5349 dprintk("<-- %s status=%d\n", __func__, status);
5352 #endif /* CONFIG_NFS_V4_1 */
5354 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5355 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5356 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5357 .recover_open = nfs4_open_reclaim,
5358 .recover_lock = nfs4_lock_reclaim,
5359 .establish_clid = nfs4_init_clientid,
5360 .get_clid_cred = nfs4_get_setclientid_cred,
5363 #if defined(CONFIG_NFS_V4_1)
5364 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5365 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5366 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5367 .recover_open = nfs4_open_reclaim,
5368 .recover_lock = nfs4_lock_reclaim,
5369 .establish_clid = nfs41_init_clientid,
5370 .get_clid_cred = nfs4_get_exchange_id_cred,
5371 .reclaim_complete = nfs41_proc_reclaim_complete,
5373 #endif /* CONFIG_NFS_V4_1 */
5375 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5376 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5377 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5378 .recover_open = nfs4_open_expired,
5379 .recover_lock = nfs4_lock_expired,
5380 .establish_clid = nfs4_init_clientid,
5381 .get_clid_cred = nfs4_get_setclientid_cred,
5384 #if defined(CONFIG_NFS_V4_1)
5385 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5386 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5387 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5388 .recover_open = nfs4_open_expired,
5389 .recover_lock = nfs4_lock_expired,
5390 .establish_clid = nfs41_init_clientid,
5391 .get_clid_cred = nfs4_get_exchange_id_cred,
5393 #endif /* CONFIG_NFS_V4_1 */
5395 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5396 .sched_state_renewal = nfs4_proc_async_renew,
5397 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5398 .renew_lease = nfs4_proc_renew,
5401 #if defined(CONFIG_NFS_V4_1)
5402 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5403 .sched_state_renewal = nfs41_proc_async_sequence,
5404 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5405 .renew_lease = nfs4_proc_sequence,
5409 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5411 .call_sync = _nfs4_call_sync,
5412 .validate_stateid = nfs4_validate_delegation_stateid,
5413 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5414 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5415 .state_renewal_ops = &nfs40_state_renewal_ops,
5418 #if defined(CONFIG_NFS_V4_1)
5419 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5421 .call_sync = _nfs4_call_sync_session,
5422 .validate_stateid = nfs41_validate_delegation_stateid,
5423 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5424 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5425 .state_renewal_ops = &nfs41_state_renewal_ops,
5429 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5430 [0] = &nfs_v4_0_minor_ops,
5431 #if defined(CONFIG_NFS_V4_1)
5432 [1] = &nfs_v4_1_minor_ops,
5436 static const struct inode_operations nfs4_file_inode_operations = {
5437 .permission = nfs_permission,
5438 .getattr = nfs_getattr,
5439 .setattr = nfs_setattr,
5440 .getxattr = nfs4_getxattr,
5441 .setxattr = nfs4_setxattr,
5442 .listxattr = nfs4_listxattr,
5445 const struct nfs_rpc_ops nfs_v4_clientops = {
5446 .version = 4, /* protocol version */
5447 .dentry_ops = &nfs4_dentry_operations,
5448 .dir_inode_ops = &nfs4_dir_inode_operations,
5449 .file_inode_ops = &nfs4_file_inode_operations,
5450 .getroot = nfs4_proc_get_root,
5451 .getattr = nfs4_proc_getattr,
5452 .setattr = nfs4_proc_setattr,
5453 .lookupfh = nfs4_proc_lookupfh,
5454 .lookup = nfs4_proc_lookup,
5455 .access = nfs4_proc_access,
5456 .readlink = nfs4_proc_readlink,
5457 .create = nfs4_proc_create,
5458 .remove = nfs4_proc_remove,
5459 .unlink_setup = nfs4_proc_unlink_setup,
5460 .unlink_done = nfs4_proc_unlink_done,
5461 .rename = nfs4_proc_rename,
5462 .link = nfs4_proc_link,
5463 .symlink = nfs4_proc_symlink,
5464 .mkdir = nfs4_proc_mkdir,
5465 .rmdir = nfs4_proc_remove,
5466 .readdir = nfs4_proc_readdir,
5467 .mknod = nfs4_proc_mknod,
5468 .statfs = nfs4_proc_statfs,
5469 .fsinfo = nfs4_proc_fsinfo,
5470 .pathconf = nfs4_proc_pathconf,
5471 .set_capabilities = nfs4_server_capabilities,
5472 .decode_dirent = nfs4_decode_dirent,
5473 .read_setup = nfs4_proc_read_setup,
5474 .read_done = nfs4_read_done,
5475 .write_setup = nfs4_proc_write_setup,
5476 .write_done = nfs4_write_done,
5477 .commit_setup = nfs4_proc_commit_setup,
5478 .commit_done = nfs4_commit_done,
5479 .lock = nfs4_proc_lock,
5480 .clear_acl_cache = nfs4_zap_acl_attr,
5481 .close_context = nfs4_close_context,