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/sunrpc/clnt.h>
43 #include <linux/nfs.h>
44 #include <linux/nfs4.h>
45 #include <linux/nfs_fs.h>
46 #include <linux/nfs_page.h>
47 #include <linux/namei.h>
48 #include <linux/mount.h>
49 #include <linux/module.h>
50 #include <linux/sunrpc/bc_xprt.h>
53 #include "delegation.h"
58 #define NFSDBG_FACILITY NFSDBG_PROC
60 #define NFS4_POLL_RETRY_MIN (HZ/10)
61 #define NFS4_POLL_RETRY_MAX (15*HZ)
63 #define NFS4_MAX_LOOP_ON_RECOVER (10)
66 static int _nfs4_proc_open(struct nfs4_opendata *data);
67 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
68 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
69 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
70 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
71 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
73 /* Prevent leaks of NFSv4 errors into userland */
74 static int nfs4_map_errors(int err)
79 case -NFS4ERR_RESOURCE:
82 dprintk("%s could not handle NFSv4 error %d\n",
90 * This is our standard bitmap for GETATTR requests.
92 const u32 nfs4_fattr_bitmap[2] = {
97 | FATTR4_WORD0_FILEID,
99 | FATTR4_WORD1_NUMLINKS
101 | FATTR4_WORD1_OWNER_GROUP
102 | FATTR4_WORD1_RAWDEV
103 | FATTR4_WORD1_SPACE_USED
104 | FATTR4_WORD1_TIME_ACCESS
105 | FATTR4_WORD1_TIME_METADATA
106 | FATTR4_WORD1_TIME_MODIFY
109 const u32 nfs4_statfs_bitmap[2] = {
110 FATTR4_WORD0_FILES_AVAIL
111 | FATTR4_WORD0_FILES_FREE
112 | FATTR4_WORD0_FILES_TOTAL,
113 FATTR4_WORD1_SPACE_AVAIL
114 | FATTR4_WORD1_SPACE_FREE
115 | FATTR4_WORD1_SPACE_TOTAL
118 const u32 nfs4_pathconf_bitmap[2] = {
120 | FATTR4_WORD0_MAXNAME,
124 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
125 | FATTR4_WORD0_MAXREAD
126 | FATTR4_WORD0_MAXWRITE
127 | FATTR4_WORD0_LEASE_TIME,
131 const u32 nfs4_fs_locations_bitmap[2] = {
133 | FATTR4_WORD0_CHANGE
136 | FATTR4_WORD0_FILEID
137 | FATTR4_WORD0_FS_LOCATIONS,
139 | FATTR4_WORD1_NUMLINKS
141 | FATTR4_WORD1_OWNER_GROUP
142 | FATTR4_WORD1_RAWDEV
143 | FATTR4_WORD1_SPACE_USED
144 | FATTR4_WORD1_TIME_ACCESS
145 | FATTR4_WORD1_TIME_METADATA
146 | FATTR4_WORD1_TIME_MODIFY
147 | FATTR4_WORD1_MOUNTED_ON_FILEID
150 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
151 struct nfs4_readdir_arg *readdir)
155 BUG_ON(readdir->count < 80);
157 readdir->cookie = cookie;
158 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 * NFSv4 servers do not return entries for '.' and '..'
169 * Therefore, we fake these entries here. We let '.'
170 * have cookie 0 and '..' have cookie 1. Note that
171 * when talking to the server, we always send cookie 0
174 start = p = kmap_atomic(*readdir->pages, KM_USER0);
177 *p++ = xdr_one; /* next */
178 *p++ = xdr_zero; /* cookie, first word */
179 *p++ = xdr_one; /* cookie, second word */
180 *p++ = xdr_one; /* entry len */
181 memcpy(p, ".\0\0\0", 4); /* entry */
183 *p++ = xdr_one; /* bitmap length */
184 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
185 *p++ = htonl(8); /* attribute buffer length */
186 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
189 *p++ = xdr_one; /* next */
190 *p++ = xdr_zero; /* cookie, first word */
191 *p++ = xdr_two; /* cookie, second word */
192 *p++ = xdr_two; /* entry len */
193 memcpy(p, "..\0\0", 4); /* entry */
195 *p++ = xdr_one; /* bitmap length */
196 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
197 *p++ = htonl(8); /* attribute buffer length */
198 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
200 readdir->pgbase = (char *)p - (char *)start;
201 readdir->count -= readdir->pgbase;
202 kunmap_atomic(start, KM_USER0);
205 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
211 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
212 nfs_wait_bit_killable, TASK_KILLABLE);
216 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
223 *timeout = NFS4_POLL_RETRY_MIN;
224 if (*timeout > NFS4_POLL_RETRY_MAX)
225 *timeout = NFS4_POLL_RETRY_MAX;
226 schedule_timeout_killable(*timeout);
227 if (fatal_signal_pending(current))
233 /* This is the error handling routine for processes that are allowed
236 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
238 struct nfs_client *clp = server->nfs_client;
239 struct nfs4_state *state = exception->state;
242 exception->retry = 0;
246 case -NFS4ERR_ADMIN_REVOKED:
247 case -NFS4ERR_BAD_STATEID:
248 case -NFS4ERR_OPENMODE:
251 nfs4_state_mark_reclaim_nograce(clp, state);
252 case -NFS4ERR_STALE_CLIENTID:
253 case -NFS4ERR_STALE_STATEID:
254 case -NFS4ERR_EXPIRED:
255 nfs4_schedule_state_recovery(clp);
256 ret = nfs4_wait_clnt_recover(clp);
258 exception->retry = 1;
259 #if !defined(CONFIG_NFS_V4_1)
261 #else /* !defined(CONFIG_NFS_V4_1) */
262 if (!nfs4_has_session(server->nfs_client))
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
288 ret = nfs4_delay(server->client, &exception->timeout);
291 case -NFS4ERR_OLD_STATEID:
292 exception->retry = 1;
294 /* We failed to handle the error */
295 return nfs4_map_errors(ret);
299 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
301 struct nfs_client *clp = server->nfs_client;
302 spin_lock(&clp->cl_lock);
303 if (time_before(clp->cl_last_renewal,timestamp))
304 clp->cl_last_renewal = timestamp;
305 spin_unlock(&clp->cl_lock);
308 #if defined(CONFIG_NFS_V4_1)
311 * nfs4_free_slot - free a slot and efficiently update slot table.
313 * freeing a slot is trivially done by clearing its respective bit
315 * If the freed slotid equals highest_used_slotid we want to update it
316 * so that the server would be able to size down the slot table if needed,
317 * otherwise we know that the highest_used_slotid is still in use.
318 * When updating highest_used_slotid there may be "holes" in the bitmap
319 * so we need to scan down from highest_used_slotid to 0 looking for the now
320 * highest slotid in use.
321 * If none found, highest_used_slotid is set to -1.
323 * Must be called while holding tbl->slot_tbl_lock
326 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
328 int slotid = free_slotid;
330 /* clear used bit in bitmap */
331 __clear_bit(slotid, tbl->used_slots);
333 /* update highest_used_slotid when it is freed */
334 if (slotid == tbl->highest_used_slotid) {
335 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
336 if (slotid < tbl->max_slots)
337 tbl->highest_used_slotid = slotid;
339 tbl->highest_used_slotid = -1;
341 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
342 free_slotid, tbl->highest_used_slotid);
346 * Signal state manager thread if session is drained
348 static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
350 struct rpc_task *task;
352 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
353 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
355 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
359 if (ses->fc_slot_table.highest_used_slotid != -1)
362 dprintk("%s COMPLETE: Session Drained\n", __func__);
363 complete(&ses->complete);
366 static void nfs41_sequence_free_slot(const struct nfs_client *clp,
367 struct nfs4_sequence_res *res)
369 struct nfs4_slot_table *tbl;
371 tbl = &clp->cl_session->fc_slot_table;
372 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
373 /* just wake up the next guy waiting since
374 * we may have not consumed a slot after all */
375 dprintk("%s: No slot\n", __func__);
379 spin_lock(&tbl->slot_tbl_lock);
380 nfs4_free_slot(tbl, res->sr_slotid);
381 nfs41_check_drain_session_complete(clp->cl_session);
382 spin_unlock(&tbl->slot_tbl_lock);
383 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
386 static void nfs41_sequence_done(struct nfs_client *clp,
387 struct nfs4_sequence_res *res,
390 unsigned long timestamp;
391 struct nfs4_slot_table *tbl;
392 struct nfs4_slot *slot;
395 * sr_status remains 1 if an RPC level error occurred. The server
396 * may or may not have processed the sequence operation..
397 * Proceed as if the server received and processed the sequence
400 if (res->sr_status == 1)
401 res->sr_status = NFS_OK;
403 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
404 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
407 /* Check the SEQUENCE operation status */
408 if (res->sr_status == 0) {
409 tbl = &clp->cl_session->fc_slot_table;
410 slot = tbl->slots + res->sr_slotid;
411 /* Update the slot's sequence and clientid lease timer */
413 timestamp = res->sr_renewal_time;
414 spin_lock(&clp->cl_lock);
415 if (time_before(clp->cl_last_renewal, timestamp))
416 clp->cl_last_renewal = timestamp;
417 spin_unlock(&clp->cl_lock);
418 /* Check sequence flags */
419 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
422 /* The session may be reset by one of the error handlers. */
423 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
424 nfs41_sequence_free_slot(clp, res);
428 * nfs4_find_slot - efficiently look for a free slot
430 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
431 * If found, we mark the slot as used, update the highest_used_slotid,
432 * and respectively set up the sequence operation args.
433 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
435 * Note: must be called with under the slot_tbl_lock.
438 nfs4_find_slot(struct nfs4_slot_table *tbl)
441 u8 ret_id = NFS4_MAX_SLOT_TABLE;
442 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
444 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
445 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
447 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
448 if (slotid >= tbl->max_slots)
450 __set_bit(slotid, tbl->used_slots);
451 if (slotid > tbl->highest_used_slotid)
452 tbl->highest_used_slotid = slotid;
455 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
456 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
460 static int nfs41_setup_sequence(struct nfs4_session *session,
461 struct nfs4_sequence_args *args,
462 struct nfs4_sequence_res *res,
464 struct rpc_task *task)
466 struct nfs4_slot *slot;
467 struct nfs4_slot_table *tbl;
470 dprintk("--> %s\n", __func__);
471 /* slot already allocated? */
472 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
475 memset(res, 0, sizeof(*res));
476 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
477 tbl = &session->fc_slot_table;
479 spin_lock(&tbl->slot_tbl_lock);
480 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
481 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
483 * The state manager will wait until the slot table is empty.
484 * Schedule the reset thread
486 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
487 spin_unlock(&tbl->slot_tbl_lock);
488 dprintk("%s Schedule Session Reset\n", __func__);
492 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
493 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
494 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
495 spin_unlock(&tbl->slot_tbl_lock);
496 dprintk("%s enforce FIFO order\n", __func__);
500 slotid = nfs4_find_slot(tbl);
501 if (slotid == NFS4_MAX_SLOT_TABLE) {
502 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
503 spin_unlock(&tbl->slot_tbl_lock);
504 dprintk("<-- %s: no free slots\n", __func__);
507 spin_unlock(&tbl->slot_tbl_lock);
509 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
510 slot = tbl->slots + slotid;
511 args->sa_session = session;
512 args->sa_slotid = slotid;
513 args->sa_cache_this = cache_reply;
515 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
517 res->sr_session = session;
518 res->sr_slotid = slotid;
519 res->sr_renewal_time = jiffies;
521 * sr_status is only set in decode_sequence, and so will remain
522 * set to 1 if an rpc level failure occurs.
528 int nfs4_setup_sequence(struct nfs_client *clp,
529 struct nfs4_sequence_args *args,
530 struct nfs4_sequence_res *res,
532 struct rpc_task *task)
536 dprintk("--> %s clp %p session %p sr_slotid %d\n",
537 __func__, clp, clp->cl_session, res->sr_slotid);
539 if (!nfs4_has_session(clp))
541 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
543 if (ret && ret != -EAGAIN) {
544 /* terminate rpc task */
545 task->tk_status = ret;
546 task->tk_action = NULL;
549 dprintk("<-- %s status=%d\n", __func__, ret);
553 struct nfs41_call_sync_data {
554 struct nfs_client *clp;
555 struct nfs4_sequence_args *seq_args;
556 struct nfs4_sequence_res *seq_res;
560 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
562 struct nfs41_call_sync_data *data = calldata;
564 dprintk("--> %s data->clp->cl_session %p\n", __func__,
565 data->clp->cl_session);
566 if (nfs4_setup_sequence(data->clp, data->seq_args,
567 data->seq_res, data->cache_reply, task))
569 rpc_call_start(task);
572 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
574 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
575 nfs41_call_sync_prepare(task, calldata);
578 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
580 struct nfs41_call_sync_data *data = calldata;
582 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
585 struct rpc_call_ops nfs41_call_sync_ops = {
586 .rpc_call_prepare = nfs41_call_sync_prepare,
587 .rpc_call_done = nfs41_call_sync_done,
590 struct rpc_call_ops nfs41_call_priv_sync_ops = {
591 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
592 .rpc_call_done = nfs41_call_sync_done,
595 static int nfs4_call_sync_sequence(struct nfs_client *clp,
596 struct rpc_clnt *clnt,
597 struct rpc_message *msg,
598 struct nfs4_sequence_args *args,
599 struct nfs4_sequence_res *res,
604 struct rpc_task *task;
605 struct nfs41_call_sync_data data = {
609 .cache_reply = cache_reply,
611 struct rpc_task_setup task_setup = {
614 .callback_ops = &nfs41_call_sync_ops,
615 .callback_data = &data
618 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
620 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
621 task = rpc_run_task(&task_setup);
625 ret = task->tk_status;
631 int _nfs4_call_sync_session(struct nfs_server *server,
632 struct rpc_message *msg,
633 struct nfs4_sequence_args *args,
634 struct nfs4_sequence_res *res,
637 return nfs4_call_sync_sequence(server->nfs_client, server->client,
638 msg, args, res, cache_reply, 0);
641 #endif /* CONFIG_NFS_V4_1 */
643 int _nfs4_call_sync(struct nfs_server *server,
644 struct rpc_message *msg,
645 struct nfs4_sequence_args *args,
646 struct nfs4_sequence_res *res,
649 args->sa_session = res->sr_session = NULL;
650 return rpc_call_sync(server->client, msg, 0);
653 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
654 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
655 &(res)->seq_res, (cache_reply))
657 static void nfs4_sequence_done(const struct nfs_server *server,
658 struct nfs4_sequence_res *res, int rpc_status)
660 #ifdef CONFIG_NFS_V4_1
661 if (nfs4_has_session(server->nfs_client))
662 nfs41_sequence_done(server->nfs_client, res, rpc_status);
663 #endif /* CONFIG_NFS_V4_1 */
666 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
668 struct nfs_inode *nfsi = NFS_I(dir);
670 spin_lock(&dir->i_lock);
671 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
672 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
673 nfs_force_lookup_revalidate(dir);
674 nfsi->change_attr = cinfo->after;
675 spin_unlock(&dir->i_lock);
678 struct nfs4_opendata {
680 struct nfs_openargs o_arg;
681 struct nfs_openres o_res;
682 struct nfs_open_confirmargs c_arg;
683 struct nfs_open_confirmres c_res;
684 struct nfs_fattr f_attr;
685 struct nfs_fattr dir_attr;
688 struct nfs4_state_owner *owner;
689 struct nfs4_state *state;
691 unsigned long timestamp;
692 unsigned int rpc_done : 1;
698 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
700 p->o_res.f_attr = &p->f_attr;
701 p->o_res.dir_attr = &p->dir_attr;
702 p->o_res.seqid = p->o_arg.seqid;
703 p->c_res.seqid = p->c_arg.seqid;
704 p->o_res.server = p->o_arg.server;
705 nfs_fattr_init(&p->f_attr);
706 nfs_fattr_init(&p->dir_attr);
707 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
710 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
711 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
712 const struct iattr *attrs)
714 struct dentry *parent = dget_parent(path->dentry);
715 struct inode *dir = parent->d_inode;
716 struct nfs_server *server = NFS_SERVER(dir);
717 struct nfs4_opendata *p;
719 p = kzalloc(sizeof(*p), GFP_KERNEL);
722 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
723 if (p->o_arg.seqid == NULL)
725 p->path.mnt = mntget(path->mnt);
726 p->path.dentry = dget(path->dentry);
729 atomic_inc(&sp->so_count);
730 p->o_arg.fh = NFS_FH(dir);
731 p->o_arg.open_flags = flags;
732 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
733 p->o_arg.clientid = server->nfs_client->cl_clientid;
734 p->o_arg.id = sp->so_owner_id.id;
735 p->o_arg.name = &p->path.dentry->d_name;
736 p->o_arg.server = server;
737 p->o_arg.bitmask = server->attr_bitmask;
738 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
739 if (flags & O_EXCL) {
740 if (nfs4_has_persistent_session(server->nfs_client)) {
742 p->o_arg.u.attrs = &p->attrs;
743 memcpy(&p->attrs, attrs, sizeof(p->attrs));
744 } else { /* EXCLUSIVE4_1 */
745 u32 *s = (u32 *) p->o_arg.u.verifier.data;
749 } else if (flags & O_CREAT) {
750 p->o_arg.u.attrs = &p->attrs;
751 memcpy(&p->attrs, attrs, sizeof(p->attrs));
753 p->c_arg.fh = &p->o_res.fh;
754 p->c_arg.stateid = &p->o_res.stateid;
755 p->c_arg.seqid = p->o_arg.seqid;
756 nfs4_init_opendata_res(p);
766 static void nfs4_opendata_free(struct kref *kref)
768 struct nfs4_opendata *p = container_of(kref,
769 struct nfs4_opendata, kref);
771 nfs_free_seqid(p->o_arg.seqid);
772 if (p->state != NULL)
773 nfs4_put_open_state(p->state);
774 nfs4_put_state_owner(p->owner);
780 static void nfs4_opendata_put(struct nfs4_opendata *p)
783 kref_put(&p->kref, nfs4_opendata_free);
786 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
790 ret = rpc_wait_for_completion_task(task);
794 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
798 if (open_mode & O_EXCL)
800 switch (mode & (FMODE_READ|FMODE_WRITE)) {
802 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
803 && state->n_rdonly != 0;
806 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
807 && state->n_wronly != 0;
809 case FMODE_READ|FMODE_WRITE:
810 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
811 && state->n_rdwr != 0;
817 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
819 if ((delegation->type & fmode) != fmode)
821 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
823 nfs_mark_delegation_referenced(delegation);
827 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
836 case FMODE_READ|FMODE_WRITE:
839 nfs4_state_set_mode_locked(state, state->state | fmode);
842 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
844 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
845 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
846 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
849 set_bit(NFS_O_RDONLY_STATE, &state->flags);
852 set_bit(NFS_O_WRONLY_STATE, &state->flags);
854 case FMODE_READ|FMODE_WRITE:
855 set_bit(NFS_O_RDWR_STATE, &state->flags);
859 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
861 write_seqlock(&state->seqlock);
862 nfs_set_open_stateid_locked(state, stateid, fmode);
863 write_sequnlock(&state->seqlock);
866 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
869 * Protect the call to nfs4_state_set_mode_locked and
870 * serialise the stateid update
872 write_seqlock(&state->seqlock);
873 if (deleg_stateid != NULL) {
874 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
875 set_bit(NFS_DELEGATED_STATE, &state->flags);
877 if (open_stateid != NULL)
878 nfs_set_open_stateid_locked(state, open_stateid, fmode);
879 write_sequnlock(&state->seqlock);
880 spin_lock(&state->owner->so_lock);
881 update_open_stateflags(state, fmode);
882 spin_unlock(&state->owner->so_lock);
885 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
887 struct nfs_inode *nfsi = NFS_I(state->inode);
888 struct nfs_delegation *deleg_cur;
891 fmode &= (FMODE_READ|FMODE_WRITE);
894 deleg_cur = rcu_dereference(nfsi->delegation);
895 if (deleg_cur == NULL)
898 spin_lock(&deleg_cur->lock);
899 if (nfsi->delegation != deleg_cur ||
900 (deleg_cur->type & fmode) != fmode)
901 goto no_delegation_unlock;
903 if (delegation == NULL)
904 delegation = &deleg_cur->stateid;
905 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
906 goto no_delegation_unlock;
908 nfs_mark_delegation_referenced(deleg_cur);
909 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
911 no_delegation_unlock:
912 spin_unlock(&deleg_cur->lock);
916 if (!ret && open_stateid != NULL) {
917 __update_open_stateid(state, open_stateid, NULL, fmode);
925 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
927 struct nfs_delegation *delegation;
930 delegation = rcu_dereference(NFS_I(inode)->delegation);
931 if (delegation == NULL || (delegation->type & fmode) == fmode) {
936 nfs_inode_return_delegation(inode);
939 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
941 struct nfs4_state *state = opendata->state;
942 struct nfs_inode *nfsi = NFS_I(state->inode);
943 struct nfs_delegation *delegation;
944 int open_mode = opendata->o_arg.open_flags & O_EXCL;
945 fmode_t fmode = opendata->o_arg.fmode;
946 nfs4_stateid stateid;
950 if (can_open_cached(state, fmode, open_mode)) {
951 spin_lock(&state->owner->so_lock);
952 if (can_open_cached(state, fmode, open_mode)) {
953 update_open_stateflags(state, fmode);
954 spin_unlock(&state->owner->so_lock);
955 goto out_return_state;
957 spin_unlock(&state->owner->so_lock);
960 delegation = rcu_dereference(nfsi->delegation);
961 if (delegation == NULL ||
962 !can_open_delegated(delegation, fmode)) {
966 /* Save the delegation */
967 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
969 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
974 /* Try to update the stateid using the delegation */
975 if (update_open_stateid(state, NULL, &stateid, fmode))
976 goto out_return_state;
981 atomic_inc(&state->count);
985 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
988 struct nfs4_state *state = NULL;
989 struct nfs_delegation *delegation;
992 if (!data->rpc_done) {
993 state = nfs4_try_open_cached(data);
998 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1000 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1001 ret = PTR_ERR(inode);
1005 state = nfs4_get_open_state(inode, data->owner);
1008 if (data->o_res.delegation_type != 0) {
1009 int delegation_flags = 0;
1012 delegation = rcu_dereference(NFS_I(inode)->delegation);
1014 delegation_flags = delegation->flags;
1016 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1017 nfs_inode_set_delegation(state->inode,
1018 data->owner->so_cred,
1021 nfs_inode_reclaim_delegation(state->inode,
1022 data->owner->so_cred,
1026 update_open_stateid(state, &data->o_res.stateid, NULL,
1034 return ERR_PTR(ret);
1037 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1039 struct nfs_inode *nfsi = NFS_I(state->inode);
1040 struct nfs_open_context *ctx;
1042 spin_lock(&state->inode->i_lock);
1043 list_for_each_entry(ctx, &nfsi->open_files, list) {
1044 if (ctx->state != state)
1046 get_nfs_open_context(ctx);
1047 spin_unlock(&state->inode->i_lock);
1050 spin_unlock(&state->inode->i_lock);
1051 return ERR_PTR(-ENOENT);
1054 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1056 struct nfs4_opendata *opendata;
1058 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
1059 if (opendata == NULL)
1060 return ERR_PTR(-ENOMEM);
1061 opendata->state = state;
1062 atomic_inc(&state->count);
1066 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1068 struct nfs4_state *newstate;
1071 opendata->o_arg.open_flags = 0;
1072 opendata->o_arg.fmode = fmode;
1073 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1074 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1075 nfs4_init_opendata_res(opendata);
1076 ret = _nfs4_recover_proc_open(opendata);
1079 newstate = nfs4_opendata_to_nfs4_state(opendata);
1080 if (IS_ERR(newstate))
1081 return PTR_ERR(newstate);
1082 nfs4_close_state(&opendata->path, newstate, fmode);
1087 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1089 struct nfs4_state *newstate;
1092 /* memory barrier prior to reading state->n_* */
1093 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1095 if (state->n_rdwr != 0) {
1096 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1099 if (newstate != state)
1102 if (state->n_wronly != 0) {
1103 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1106 if (newstate != state)
1109 if (state->n_rdonly != 0) {
1110 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1113 if (newstate != state)
1117 * We may have performed cached opens for all three recoveries.
1118 * Check if we need to update the current stateid.
1120 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1121 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1122 write_seqlock(&state->seqlock);
1123 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1124 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1125 write_sequnlock(&state->seqlock);
1132 * reclaim state on the server after a reboot.
1134 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1136 struct nfs_delegation *delegation;
1137 struct nfs4_opendata *opendata;
1138 fmode_t delegation_type = 0;
1141 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1142 if (IS_ERR(opendata))
1143 return PTR_ERR(opendata);
1144 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1145 opendata->o_arg.fh = NFS_FH(state->inode);
1147 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1148 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1149 delegation_type = delegation->type;
1151 opendata->o_arg.u.delegation_type = delegation_type;
1152 status = nfs4_open_recover(opendata, state);
1153 nfs4_opendata_put(opendata);
1157 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1159 struct nfs_server *server = NFS_SERVER(state->inode);
1160 struct nfs4_exception exception = { };
1163 err = _nfs4_do_open_reclaim(ctx, state);
1164 if (err != -NFS4ERR_DELAY)
1166 nfs4_handle_exception(server, err, &exception);
1167 } while (exception.retry);
1171 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1173 struct nfs_open_context *ctx;
1176 ctx = nfs4_state_find_open_context(state);
1178 return PTR_ERR(ctx);
1179 ret = nfs4_do_open_reclaim(ctx, state);
1180 put_nfs_open_context(ctx);
1184 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1186 struct nfs4_opendata *opendata;
1189 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1190 if (IS_ERR(opendata))
1191 return PTR_ERR(opendata);
1192 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1193 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1194 sizeof(opendata->o_arg.u.delegation.data));
1195 ret = nfs4_open_recover(opendata, state);
1196 nfs4_opendata_put(opendata);
1200 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1202 struct nfs4_exception exception = { };
1203 struct nfs_server *server = NFS_SERVER(state->inode);
1206 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1212 case -NFS4ERR_BADSESSION:
1213 case -NFS4ERR_BADSLOT:
1214 case -NFS4ERR_BAD_HIGH_SLOT:
1215 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1216 case -NFS4ERR_DEADSESSION:
1217 nfs4_schedule_state_recovery(
1218 server->nfs_client);
1220 case -NFS4ERR_STALE_CLIENTID:
1221 case -NFS4ERR_STALE_STATEID:
1222 case -NFS4ERR_EXPIRED:
1223 /* Don't recall a delegation if it was lost */
1224 nfs4_schedule_state_recovery(server->nfs_client);
1228 * The show must go on: exit, but mark the
1229 * stateid as needing recovery.
1231 case -NFS4ERR_ADMIN_REVOKED:
1232 case -NFS4ERR_BAD_STATEID:
1233 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1238 err = nfs4_handle_exception(server, err, &exception);
1239 } while (exception.retry);
1244 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1246 struct nfs4_opendata *data = calldata;
1248 data->rpc_status = task->tk_status;
1249 if (RPC_ASSASSINATED(task))
1251 if (data->rpc_status == 0) {
1252 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1253 sizeof(data->o_res.stateid.data));
1254 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1255 renew_lease(data->o_res.server, data->timestamp);
1260 static void nfs4_open_confirm_release(void *calldata)
1262 struct nfs4_opendata *data = calldata;
1263 struct nfs4_state *state = NULL;
1265 /* If this request hasn't been cancelled, do nothing */
1266 if (data->cancelled == 0)
1268 /* In case of error, no cleanup! */
1269 if (!data->rpc_done)
1271 state = nfs4_opendata_to_nfs4_state(data);
1273 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1275 nfs4_opendata_put(data);
1278 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1279 .rpc_call_done = nfs4_open_confirm_done,
1280 .rpc_release = nfs4_open_confirm_release,
1284 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1286 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1288 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1289 struct rpc_task *task;
1290 struct rpc_message msg = {
1291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1292 .rpc_argp = &data->c_arg,
1293 .rpc_resp = &data->c_res,
1294 .rpc_cred = data->owner->so_cred,
1296 struct rpc_task_setup task_setup_data = {
1297 .rpc_client = server->client,
1298 .rpc_message = &msg,
1299 .callback_ops = &nfs4_open_confirm_ops,
1300 .callback_data = data,
1301 .workqueue = nfsiod_workqueue,
1302 .flags = RPC_TASK_ASYNC,
1306 kref_get(&data->kref);
1308 data->rpc_status = 0;
1309 data->timestamp = jiffies;
1310 task = rpc_run_task(&task_setup_data);
1312 return PTR_ERR(task);
1313 status = nfs4_wait_for_completion_rpc_task(task);
1315 data->cancelled = 1;
1318 status = data->rpc_status;
1323 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1325 struct nfs4_opendata *data = calldata;
1326 struct nfs4_state_owner *sp = data->owner;
1328 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1331 * Check if we still need to send an OPEN call, or if we can use
1332 * a delegation instead.
1334 if (data->state != NULL) {
1335 struct nfs_delegation *delegation;
1337 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1340 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1341 if (delegation != NULL &&
1342 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1348 /* Update sequence id. */
1349 data->o_arg.id = sp->so_owner_id.id;
1350 data->o_arg.clientid = sp->so_client->cl_clientid;
1351 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1352 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1353 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1355 data->timestamp = jiffies;
1356 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1357 &data->o_arg.seq_args,
1358 &data->o_res.seq_res, 1, task))
1360 rpc_call_start(task);
1363 task->tk_action = NULL;
1367 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1369 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1370 nfs4_open_prepare(task, calldata);
1373 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1375 struct nfs4_opendata *data = calldata;
1377 data->rpc_status = task->tk_status;
1379 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1382 if (RPC_ASSASSINATED(task))
1384 if (task->tk_status == 0) {
1385 switch (data->o_res.f_attr->mode & S_IFMT) {
1389 data->rpc_status = -ELOOP;
1392 data->rpc_status = -EISDIR;
1395 data->rpc_status = -ENOTDIR;
1397 renew_lease(data->o_res.server, data->timestamp);
1398 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1399 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1404 static void nfs4_open_release(void *calldata)
1406 struct nfs4_opendata *data = calldata;
1407 struct nfs4_state *state = NULL;
1409 /* If this request hasn't been cancelled, do nothing */
1410 if (data->cancelled == 0)
1412 /* In case of error, no cleanup! */
1413 if (data->rpc_status != 0 || !data->rpc_done)
1415 /* In case we need an open_confirm, no cleanup! */
1416 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1418 state = nfs4_opendata_to_nfs4_state(data);
1420 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1422 nfs4_opendata_put(data);
1425 static const struct rpc_call_ops nfs4_open_ops = {
1426 .rpc_call_prepare = nfs4_open_prepare,
1427 .rpc_call_done = nfs4_open_done,
1428 .rpc_release = nfs4_open_release,
1431 static const struct rpc_call_ops nfs4_recover_open_ops = {
1432 .rpc_call_prepare = nfs4_recover_open_prepare,
1433 .rpc_call_done = nfs4_open_done,
1434 .rpc_release = nfs4_open_release,
1437 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1439 struct inode *dir = data->dir->d_inode;
1440 struct nfs_server *server = NFS_SERVER(dir);
1441 struct nfs_openargs *o_arg = &data->o_arg;
1442 struct nfs_openres *o_res = &data->o_res;
1443 struct rpc_task *task;
1444 struct rpc_message msg = {
1445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1448 .rpc_cred = data->owner->so_cred,
1450 struct rpc_task_setup task_setup_data = {
1451 .rpc_client = server->client,
1452 .rpc_message = &msg,
1453 .callback_ops = &nfs4_open_ops,
1454 .callback_data = data,
1455 .workqueue = nfsiod_workqueue,
1456 .flags = RPC_TASK_ASYNC,
1460 kref_get(&data->kref);
1462 data->rpc_status = 0;
1463 data->cancelled = 0;
1465 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1466 task = rpc_run_task(&task_setup_data);
1468 return PTR_ERR(task);
1469 status = nfs4_wait_for_completion_rpc_task(task);
1471 data->cancelled = 1;
1474 status = data->rpc_status;
1480 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1482 struct inode *dir = data->dir->d_inode;
1483 struct nfs_openres *o_res = &data->o_res;
1486 status = nfs4_run_open_task(data, 1);
1487 if (status != 0 || !data->rpc_done)
1490 nfs_refresh_inode(dir, o_res->dir_attr);
1492 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1493 status = _nfs4_proc_open_confirm(data);
1502 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1504 static int _nfs4_proc_open(struct nfs4_opendata *data)
1506 struct inode *dir = data->dir->d_inode;
1507 struct nfs_server *server = NFS_SERVER(dir);
1508 struct nfs_openargs *o_arg = &data->o_arg;
1509 struct nfs_openres *o_res = &data->o_res;
1512 status = nfs4_run_open_task(data, 0);
1513 if (status != 0 || !data->rpc_done)
1516 if (o_arg->open_flags & O_CREAT) {
1517 update_changeattr(dir, &o_res->cinfo);
1518 nfs_post_op_update_inode(dir, o_res->dir_attr);
1520 nfs_refresh_inode(dir, o_res->dir_attr);
1521 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1522 status = _nfs4_proc_open_confirm(data);
1526 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1527 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1531 static int nfs4_recover_expired_lease(struct nfs_server *server)
1533 struct nfs_client *clp = server->nfs_client;
1537 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1538 ret = nfs4_wait_clnt_recover(clp);
1541 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1542 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1544 nfs4_schedule_state_recovery(clp);
1552 * reclaim state on the server after a network partition.
1553 * Assumes caller holds the appropriate lock
1555 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1557 struct nfs4_opendata *opendata;
1560 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1561 if (IS_ERR(opendata))
1562 return PTR_ERR(opendata);
1563 ret = nfs4_open_recover(opendata, state);
1565 d_drop(ctx->path.dentry);
1566 nfs4_opendata_put(opendata);
1570 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1572 struct nfs_server *server = NFS_SERVER(state->inode);
1573 struct nfs4_exception exception = { };
1577 err = _nfs4_open_expired(ctx, state);
1581 case -NFS4ERR_GRACE:
1582 case -NFS4ERR_DELAY:
1583 nfs4_handle_exception(server, err, &exception);
1586 } while (exception.retry);
1591 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1593 struct nfs_open_context *ctx;
1596 ctx = nfs4_state_find_open_context(state);
1598 return PTR_ERR(ctx);
1599 ret = nfs4_do_open_expired(ctx, state);
1600 put_nfs_open_context(ctx);
1605 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1606 * fields corresponding to attributes that were used to store the verifier.
1607 * Make sure we clobber those fields in the later setattr call
1609 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1611 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1612 !(sattr->ia_valid & ATTR_ATIME_SET))
1613 sattr->ia_valid |= ATTR_ATIME;
1615 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1616 !(sattr->ia_valid & ATTR_MTIME_SET))
1617 sattr->ia_valid |= ATTR_MTIME;
1621 * Returns a referenced nfs4_state
1623 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)
1625 struct nfs4_state_owner *sp;
1626 struct nfs4_state *state = NULL;
1627 struct nfs_server *server = NFS_SERVER(dir);
1628 struct nfs4_opendata *opendata;
1631 /* Protect against reboot recovery conflicts */
1633 if (!(sp = nfs4_get_state_owner(server, cred))) {
1634 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1637 status = nfs4_recover_expired_lease(server);
1639 goto err_put_state_owner;
1640 if (path->dentry->d_inode != NULL)
1641 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1643 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1644 if (opendata == NULL)
1645 goto err_put_state_owner;
1647 if (path->dentry->d_inode != NULL)
1648 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1650 status = _nfs4_proc_open(opendata);
1652 goto err_opendata_put;
1654 if (opendata->o_arg.open_flags & O_EXCL)
1655 nfs4_exclusive_attrset(opendata, sattr);
1657 state = nfs4_opendata_to_nfs4_state(opendata);
1658 status = PTR_ERR(state);
1660 goto err_opendata_put;
1661 nfs4_opendata_put(opendata);
1662 nfs4_put_state_owner(sp);
1666 nfs4_opendata_put(opendata);
1667 err_put_state_owner:
1668 nfs4_put_state_owner(sp);
1675 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)
1677 struct nfs4_exception exception = { };
1678 struct nfs4_state *res;
1682 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1685 /* NOTE: BAD_SEQID means the server and client disagree about the
1686 * book-keeping w.r.t. state-changing operations
1687 * (OPEN/CLOSE/LOCK/LOCKU...)
1688 * It is actually a sign of a bug on the client or on the server.
1690 * If we receive a BAD_SEQID error in the particular case of
1691 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1692 * have unhashed the old state_owner for us, and that we can
1693 * therefore safely retry using a new one. We should still warn
1694 * the user though...
1696 if (status == -NFS4ERR_BAD_SEQID) {
1697 printk(KERN_WARNING "NFS: v4 server %s "
1698 " returned a bad sequence-id error!\n",
1699 NFS_SERVER(dir)->nfs_client->cl_hostname);
1700 exception.retry = 1;
1704 * BAD_STATEID on OPEN means that the server cancelled our
1705 * state before it received the OPEN_CONFIRM.
1706 * Recover by retrying the request as per the discussion
1707 * on Page 181 of RFC3530.
1709 if (status == -NFS4ERR_BAD_STATEID) {
1710 exception.retry = 1;
1713 if (status == -EAGAIN) {
1714 /* We must have found a delegation */
1715 exception.retry = 1;
1718 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1719 status, &exception));
1720 } while (exception.retry);
1724 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1725 struct nfs_fattr *fattr, struct iattr *sattr,
1726 struct nfs4_state *state)
1728 struct nfs_server *server = NFS_SERVER(inode);
1729 struct nfs_setattrargs arg = {
1730 .fh = NFS_FH(inode),
1733 .bitmask = server->attr_bitmask,
1735 struct nfs_setattrres res = {
1739 struct rpc_message msg = {
1740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1745 unsigned long timestamp = jiffies;
1748 nfs_fattr_init(fattr);
1750 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1751 /* Use that stateid */
1752 } else if (state != NULL) {
1753 nfs4_copy_stateid(&arg.stateid, state, current->files);
1755 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1757 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1758 if (status == 0 && state != NULL)
1759 renew_lease(server, timestamp);
1763 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1764 struct nfs_fattr *fattr, struct iattr *sattr,
1765 struct nfs4_state *state)
1767 struct nfs_server *server = NFS_SERVER(inode);
1768 struct nfs4_exception exception = { };
1771 err = nfs4_handle_exception(server,
1772 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1774 } while (exception.retry);
1778 struct nfs4_closedata {
1780 struct inode *inode;
1781 struct nfs4_state *state;
1782 struct nfs_closeargs arg;
1783 struct nfs_closeres res;
1784 struct nfs_fattr fattr;
1785 unsigned long timestamp;
1788 static void nfs4_free_closedata(void *data)
1790 struct nfs4_closedata *calldata = data;
1791 struct nfs4_state_owner *sp = calldata->state->owner;
1793 nfs4_put_open_state(calldata->state);
1794 nfs_free_seqid(calldata->arg.seqid);
1795 nfs4_put_state_owner(sp);
1796 path_put(&calldata->path);
1800 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1803 spin_lock(&state->owner->so_lock);
1804 if (!(fmode & FMODE_READ))
1805 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1806 if (!(fmode & FMODE_WRITE))
1807 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1808 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1809 spin_unlock(&state->owner->so_lock);
1812 static void nfs4_close_done(struct rpc_task *task, void *data)
1814 struct nfs4_closedata *calldata = data;
1815 struct nfs4_state *state = calldata->state;
1816 struct nfs_server *server = NFS_SERVER(calldata->inode);
1818 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1819 if (RPC_ASSASSINATED(task))
1821 /* hmm. we are done with the inode, and in the process of freeing
1822 * the state_owner. we keep this around to process errors
1824 switch (task->tk_status) {
1826 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1827 renew_lease(server, calldata->timestamp);
1828 nfs4_close_clear_stateid_flags(state,
1829 calldata->arg.fmode);
1831 case -NFS4ERR_STALE_STATEID:
1832 case -NFS4ERR_OLD_STATEID:
1833 case -NFS4ERR_BAD_STATEID:
1834 case -NFS4ERR_EXPIRED:
1835 if (calldata->arg.fmode == 0)
1838 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1839 nfs_restart_rpc(task, server->nfs_client);
1843 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1846 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1848 struct nfs4_closedata *calldata = data;
1849 struct nfs4_state *state = calldata->state;
1852 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1855 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1856 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1857 spin_lock(&state->owner->so_lock);
1858 /* Calculate the change in open mode */
1859 if (state->n_rdwr == 0) {
1860 if (state->n_rdonly == 0) {
1861 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1862 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1863 calldata->arg.fmode &= ~FMODE_READ;
1865 if (state->n_wronly == 0) {
1866 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1867 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1868 calldata->arg.fmode &= ~FMODE_WRITE;
1871 spin_unlock(&state->owner->so_lock);
1874 /* Note: exit _without_ calling nfs4_close_done */
1875 task->tk_action = NULL;
1879 if (calldata->arg.fmode == 0)
1880 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1882 nfs_fattr_init(calldata->res.fattr);
1883 calldata->timestamp = jiffies;
1884 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1885 &calldata->arg.seq_args, &calldata->res.seq_res,
1888 rpc_call_start(task);
1891 static const struct rpc_call_ops nfs4_close_ops = {
1892 .rpc_call_prepare = nfs4_close_prepare,
1893 .rpc_call_done = nfs4_close_done,
1894 .rpc_release = nfs4_free_closedata,
1898 * It is possible for data to be read/written from a mem-mapped file
1899 * after the sys_close call (which hits the vfs layer as a flush).
1900 * This means that we can't safely call nfsv4 close on a file until
1901 * the inode is cleared. This in turn means that we are not good
1902 * NFSv4 citizens - we do not indicate to the server to update the file's
1903 * share state even when we are done with one of the three share
1904 * stateid's in the inode.
1906 * NOTE: Caller must be holding the sp->so_owner semaphore!
1908 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1910 struct nfs_server *server = NFS_SERVER(state->inode);
1911 struct nfs4_closedata *calldata;
1912 struct nfs4_state_owner *sp = state->owner;
1913 struct rpc_task *task;
1914 struct rpc_message msg = {
1915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1916 .rpc_cred = state->owner->so_cred,
1918 struct rpc_task_setup task_setup_data = {
1919 .rpc_client = server->client,
1920 .rpc_message = &msg,
1921 .callback_ops = &nfs4_close_ops,
1922 .workqueue = nfsiod_workqueue,
1923 .flags = RPC_TASK_ASYNC,
1925 int status = -ENOMEM;
1927 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1928 if (calldata == NULL)
1930 calldata->inode = state->inode;
1931 calldata->state = state;
1932 calldata->arg.fh = NFS_FH(state->inode);
1933 calldata->arg.stateid = &state->open_stateid;
1934 if (nfs4_has_session(server->nfs_client))
1935 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
1936 /* Serialization for the sequence id */
1937 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1938 if (calldata->arg.seqid == NULL)
1939 goto out_free_calldata;
1940 calldata->arg.fmode = 0;
1941 calldata->arg.bitmask = server->cache_consistency_bitmask;
1942 calldata->res.fattr = &calldata->fattr;
1943 calldata->res.seqid = calldata->arg.seqid;
1944 calldata->res.server = server;
1945 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1946 calldata->path.mnt = mntget(path->mnt);
1947 calldata->path.dentry = dget(path->dentry);
1949 msg.rpc_argp = &calldata->arg,
1950 msg.rpc_resp = &calldata->res,
1951 task_setup_data.callback_data = calldata;
1952 task = rpc_run_task(&task_setup_data);
1954 return PTR_ERR(task);
1957 status = rpc_wait_for_completion_task(task);
1963 nfs4_put_open_state(state);
1964 nfs4_put_state_owner(sp);
1968 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1973 /* If the open_intent is for execute, we have an extra check to make */
1974 if (fmode & FMODE_EXEC) {
1975 ret = nfs_may_open(state->inode,
1976 state->owner->so_cred,
1977 nd->intent.open.flags);
1981 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1982 if (!IS_ERR(filp)) {
1983 struct nfs_open_context *ctx;
1984 ctx = nfs_file_open_context(filp);
1988 ret = PTR_ERR(filp);
1990 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1995 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1997 struct path path = {
1998 .mnt = nd->path.mnt,
2001 struct dentry *parent;
2003 struct rpc_cred *cred;
2004 struct nfs4_state *state;
2006 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
2008 if (nd->flags & LOOKUP_CREATE) {
2009 attr.ia_mode = nd->intent.open.create_mode;
2010 attr.ia_valid = ATTR_MODE;
2011 if (!IS_POSIXACL(dir))
2012 attr.ia_mode &= ~current_umask();
2015 BUG_ON(nd->intent.open.flags & O_CREAT);
2018 cred = rpc_lookup_cred();
2020 return (struct dentry *)cred;
2021 parent = dentry->d_parent;
2022 /* Protect against concurrent sillydeletes */
2023 nfs_block_sillyrename(parent);
2024 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
2026 if (IS_ERR(state)) {
2027 if (PTR_ERR(state) == -ENOENT) {
2028 d_add(dentry, NULL);
2029 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2031 nfs_unblock_sillyrename(parent);
2032 return (struct dentry *)state;
2034 res = d_add_unique(dentry, igrab(state->inode));
2037 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
2038 nfs_unblock_sillyrename(parent);
2039 nfs4_intent_set_file(nd, &path, state, fmode);
2044 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
2046 struct path path = {
2047 .mnt = nd->path.mnt,
2050 struct rpc_cred *cred;
2051 struct nfs4_state *state;
2052 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
2054 cred = rpc_lookup_cred();
2056 return PTR_ERR(cred);
2057 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
2059 if (IS_ERR(state)) {
2060 switch (PTR_ERR(state)) {
2066 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
2072 if (state->inode == dentry->d_inode) {
2073 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2074 nfs4_intent_set_file(nd, &path, state, fmode);
2077 nfs4_close_sync(&path, state, fmode);
2083 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2085 if (ctx->state == NULL)
2088 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2090 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2093 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2095 struct nfs4_server_caps_arg args = {
2098 struct nfs4_server_caps_res res = {};
2099 struct rpc_message msg = {
2100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2106 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2108 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2109 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2110 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2111 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2112 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2113 NFS_CAP_CTIME|NFS_CAP_MTIME);
2114 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2115 server->caps |= NFS_CAP_ACLS;
2116 if (res.has_links != 0)
2117 server->caps |= NFS_CAP_HARDLINKS;
2118 if (res.has_symlinks != 0)
2119 server->caps |= NFS_CAP_SYMLINKS;
2120 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2121 server->caps |= NFS_CAP_FILEID;
2122 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2123 server->caps |= NFS_CAP_MODE;
2124 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2125 server->caps |= NFS_CAP_NLINK;
2126 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2127 server->caps |= NFS_CAP_OWNER;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2129 server->caps |= NFS_CAP_OWNER_GROUP;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2131 server->caps |= NFS_CAP_ATIME;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2133 server->caps |= NFS_CAP_CTIME;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2135 server->caps |= NFS_CAP_MTIME;
2137 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2138 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2139 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2140 server->acl_bitmask = res.acl_bitmask;
2146 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2148 struct nfs4_exception exception = { };
2151 err = nfs4_handle_exception(server,
2152 _nfs4_server_capabilities(server, fhandle),
2154 } while (exception.retry);
2158 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2159 struct nfs_fsinfo *info)
2161 struct nfs4_lookup_root_arg args = {
2162 .bitmask = nfs4_fattr_bitmap,
2164 struct nfs4_lookup_res res = {
2166 .fattr = info->fattr,
2169 struct rpc_message msg = {
2170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2175 nfs_fattr_init(info->fattr);
2176 return nfs4_call_sync(server, &msg, &args, &res, 0);
2179 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2180 struct nfs_fsinfo *info)
2182 struct nfs4_exception exception = { };
2185 err = nfs4_handle_exception(server,
2186 _nfs4_lookup_root(server, fhandle, info),
2188 } while (exception.retry);
2193 * get the file handle for the "/" directory on the server
2195 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2196 struct nfs_fsinfo *info)
2200 status = nfs4_lookup_root(server, fhandle, info);
2202 status = nfs4_server_capabilities(server, fhandle);
2204 status = nfs4_do_fsinfo(server, fhandle, info);
2205 return nfs4_map_errors(status);
2209 * Get locations and (maybe) other attributes of a referral.
2210 * Note that we'll actually follow the referral later when
2211 * we detect fsid mismatch in inode revalidation
2213 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2215 int status = -ENOMEM;
2216 struct page *page = NULL;
2217 struct nfs4_fs_locations *locations = NULL;
2219 page = alloc_page(GFP_KERNEL);
2222 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2223 if (locations == NULL)
2226 status = nfs4_proc_fs_locations(dir, name, locations, page);
2229 /* Make sure server returned a different fsid for the referral */
2230 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2231 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2236 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2237 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2239 fattr->mode = S_IFDIR;
2240 memset(fhandle, 0, sizeof(struct nfs_fh));
2249 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2251 struct nfs4_getattr_arg args = {
2253 .bitmask = server->attr_bitmask,
2255 struct nfs4_getattr_res res = {
2259 struct rpc_message msg = {
2260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2265 nfs_fattr_init(fattr);
2266 return nfs4_call_sync(server, &msg, &args, &res, 0);
2269 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2271 struct nfs4_exception exception = { };
2274 err = nfs4_handle_exception(server,
2275 _nfs4_proc_getattr(server, fhandle, fattr),
2277 } while (exception.retry);
2282 * The file is not closed if it is opened due to the a request to change
2283 * the size of the file. The open call will not be needed once the
2284 * VFS layer lookup-intents are implemented.
2286 * Close is called when the inode is destroyed.
2287 * If we haven't opened the file for O_WRONLY, we
2288 * need to in the size_change case to obtain a stateid.
2291 * Because OPEN is always done by name in nfsv4, it is
2292 * possible that we opened a different file by the same
2293 * name. We can recognize this race condition, but we
2294 * can't do anything about it besides returning an error.
2296 * This will be fixed with VFS changes (lookup-intent).
2299 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2300 struct iattr *sattr)
2302 struct inode *inode = dentry->d_inode;
2303 struct rpc_cred *cred = NULL;
2304 struct nfs4_state *state = NULL;
2307 nfs_fattr_init(fattr);
2309 /* Search for an existing open(O_WRITE) file */
2310 if (sattr->ia_valid & ATTR_FILE) {
2311 struct nfs_open_context *ctx;
2313 ctx = nfs_file_open_context(sattr->ia_file);
2320 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2322 nfs_setattr_update_inode(inode, sattr);
2326 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2327 const struct qstr *name, struct nfs_fh *fhandle,
2328 struct nfs_fattr *fattr)
2331 struct nfs4_lookup_arg args = {
2332 .bitmask = server->attr_bitmask,
2336 struct nfs4_lookup_res res = {
2341 struct rpc_message msg = {
2342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2347 nfs_fattr_init(fattr);
2349 dprintk("NFS call lookupfh %s\n", name->name);
2350 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2351 dprintk("NFS reply lookupfh: %d\n", status);
2355 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2356 struct qstr *name, struct nfs_fh *fhandle,
2357 struct nfs_fattr *fattr)
2359 struct nfs4_exception exception = { };
2362 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2364 if (err == -NFS4ERR_MOVED) {
2368 err = nfs4_handle_exception(server, err, &exception);
2369 } while (exception.retry);
2373 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2374 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2378 dprintk("NFS call lookup %s\n", name->name);
2379 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2380 if (status == -NFS4ERR_MOVED)
2381 status = nfs4_get_referral(dir, name, fattr, fhandle);
2382 dprintk("NFS reply lookup: %d\n", status);
2386 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2388 struct nfs4_exception exception = { };
2391 err = nfs4_handle_exception(NFS_SERVER(dir),
2392 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2394 } while (exception.retry);
2398 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2400 struct nfs_server *server = NFS_SERVER(inode);
2401 struct nfs_fattr fattr;
2402 struct nfs4_accessargs args = {
2403 .fh = NFS_FH(inode),
2404 .bitmask = server->attr_bitmask,
2406 struct nfs4_accessres res = {
2410 struct rpc_message msg = {
2411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2414 .rpc_cred = entry->cred,
2416 int mode = entry->mask;
2420 * Determine which access bits we want to ask for...
2422 if (mode & MAY_READ)
2423 args.access |= NFS4_ACCESS_READ;
2424 if (S_ISDIR(inode->i_mode)) {
2425 if (mode & MAY_WRITE)
2426 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2427 if (mode & MAY_EXEC)
2428 args.access |= NFS4_ACCESS_LOOKUP;
2430 if (mode & MAY_WRITE)
2431 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2432 if (mode & MAY_EXEC)
2433 args.access |= NFS4_ACCESS_EXECUTE;
2435 nfs_fattr_init(&fattr);
2436 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2439 if (res.access & NFS4_ACCESS_READ)
2440 entry->mask |= MAY_READ;
2441 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2442 entry->mask |= MAY_WRITE;
2443 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2444 entry->mask |= MAY_EXEC;
2445 nfs_refresh_inode(inode, &fattr);
2450 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2452 struct nfs4_exception exception = { };
2455 err = nfs4_handle_exception(NFS_SERVER(inode),
2456 _nfs4_proc_access(inode, entry),
2458 } while (exception.retry);
2463 * TODO: For the time being, we don't try to get any attributes
2464 * along with any of the zero-copy operations READ, READDIR,
2467 * In the case of the first three, we want to put the GETATTR
2468 * after the read-type operation -- this is because it is hard
2469 * to predict the length of a GETATTR response in v4, and thus
2470 * align the READ data correctly. This means that the GETATTR
2471 * may end up partially falling into the page cache, and we should
2472 * shift it into the 'tail' of the xdr_buf before processing.
2473 * To do this efficiently, we need to know the total length
2474 * of data received, which doesn't seem to be available outside
2477 * In the case of WRITE, we also want to put the GETATTR after
2478 * the operation -- in this case because we want to make sure
2479 * we get the post-operation mtime and size. This means that
2480 * we can't use xdr_encode_pages() as written: we need a variant
2481 * of it which would leave room in the 'tail' iovec.
2483 * Both of these changes to the XDR layer would in fact be quite
2484 * minor, but I decided to leave them for a subsequent patch.
2486 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2487 unsigned int pgbase, unsigned int pglen)
2489 struct nfs4_readlink args = {
2490 .fh = NFS_FH(inode),
2495 struct nfs4_readlink_res res;
2496 struct rpc_message msg = {
2497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2502 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2505 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2506 unsigned int pgbase, unsigned int pglen)
2508 struct nfs4_exception exception = { };
2511 err = nfs4_handle_exception(NFS_SERVER(inode),
2512 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2514 } while (exception.retry);
2520 * We will need to arrange for the VFS layer to provide an atomic open.
2521 * Until then, this create/open method is prone to inefficiency and race
2522 * conditions due to the lookup, create, and open VFS calls from sys_open()
2523 * placed on the wire.
2525 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2526 * The file will be opened again in the subsequent VFS open call
2527 * (nfs4_proc_file_open).
2529 * The open for read will just hang around to be used by any process that
2530 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2534 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2535 int flags, struct nameidata *nd)
2537 struct path path = {
2538 .mnt = nd->path.mnt,
2541 struct nfs4_state *state;
2542 struct rpc_cred *cred;
2543 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2546 cred = rpc_lookup_cred();
2548 status = PTR_ERR(cred);
2551 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2553 if (IS_ERR(state)) {
2554 status = PTR_ERR(state);
2557 d_add(dentry, igrab(state->inode));
2558 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2559 if (flags & O_EXCL) {
2560 struct nfs_fattr fattr;
2561 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2563 nfs_setattr_update_inode(state->inode, sattr);
2564 nfs_post_op_update_inode(state->inode, &fattr);
2566 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2567 status = nfs4_intent_set_file(nd, &path, state, fmode);
2569 nfs4_close_sync(&path, state, fmode);
2576 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2578 struct nfs_server *server = NFS_SERVER(dir);
2579 struct nfs_removeargs args = {
2581 .name.len = name->len,
2582 .name.name = name->name,
2583 .bitmask = server->attr_bitmask,
2585 struct nfs_removeres res = {
2588 struct rpc_message msg = {
2589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2595 nfs_fattr_init(&res.dir_attr);
2596 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2598 update_changeattr(dir, &res.cinfo);
2599 nfs_post_op_update_inode(dir, &res.dir_attr);
2604 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2606 struct nfs4_exception exception = { };
2609 err = nfs4_handle_exception(NFS_SERVER(dir),
2610 _nfs4_proc_remove(dir, name),
2612 } while (exception.retry);
2616 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2618 struct nfs_server *server = NFS_SERVER(dir);
2619 struct nfs_removeargs *args = msg->rpc_argp;
2620 struct nfs_removeres *res = msg->rpc_resp;
2622 args->bitmask = server->cache_consistency_bitmask;
2623 res->server = server;
2624 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2627 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2629 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2631 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2632 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2634 update_changeattr(dir, &res->cinfo);
2635 nfs_post_op_update_inode(dir, &res->dir_attr);
2639 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2640 struct inode *new_dir, struct qstr *new_name)
2642 struct nfs_server *server = NFS_SERVER(old_dir);
2643 struct nfs4_rename_arg arg = {
2644 .old_dir = NFS_FH(old_dir),
2645 .new_dir = NFS_FH(new_dir),
2646 .old_name = old_name,
2647 .new_name = new_name,
2648 .bitmask = server->attr_bitmask,
2650 struct nfs_fattr old_fattr, new_fattr;
2651 struct nfs4_rename_res res = {
2653 .old_fattr = &old_fattr,
2654 .new_fattr = &new_fattr,
2656 struct rpc_message msg = {
2657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2663 nfs_fattr_init(res.old_fattr);
2664 nfs_fattr_init(res.new_fattr);
2665 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2668 update_changeattr(old_dir, &res.old_cinfo);
2669 nfs_post_op_update_inode(old_dir, res.old_fattr);
2670 update_changeattr(new_dir, &res.new_cinfo);
2671 nfs_post_op_update_inode(new_dir, res.new_fattr);
2676 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2677 struct inode *new_dir, struct qstr *new_name)
2679 struct nfs4_exception exception = { };
2682 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2683 _nfs4_proc_rename(old_dir, old_name,
2686 } while (exception.retry);
2690 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2692 struct nfs_server *server = NFS_SERVER(inode);
2693 struct nfs4_link_arg arg = {
2694 .fh = NFS_FH(inode),
2695 .dir_fh = NFS_FH(dir),
2697 .bitmask = server->attr_bitmask,
2699 struct nfs_fattr fattr, dir_attr;
2700 struct nfs4_link_res res = {
2703 .dir_attr = &dir_attr,
2705 struct rpc_message msg = {
2706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2712 nfs_fattr_init(res.fattr);
2713 nfs_fattr_init(res.dir_attr);
2714 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2716 update_changeattr(dir, &res.cinfo);
2717 nfs_post_op_update_inode(dir, res.dir_attr);
2718 nfs_post_op_update_inode(inode, res.fattr);
2724 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2726 struct nfs4_exception exception = { };
2729 err = nfs4_handle_exception(NFS_SERVER(inode),
2730 _nfs4_proc_link(inode, dir, name),
2732 } while (exception.retry);
2736 struct nfs4_createdata {
2737 struct rpc_message msg;
2738 struct nfs4_create_arg arg;
2739 struct nfs4_create_res res;
2741 struct nfs_fattr fattr;
2742 struct nfs_fattr dir_fattr;
2745 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2746 struct qstr *name, struct iattr *sattr, u32 ftype)
2748 struct nfs4_createdata *data;
2750 data = kzalloc(sizeof(*data), GFP_KERNEL);
2752 struct nfs_server *server = NFS_SERVER(dir);
2754 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2755 data->msg.rpc_argp = &data->arg;
2756 data->msg.rpc_resp = &data->res;
2757 data->arg.dir_fh = NFS_FH(dir);
2758 data->arg.server = server;
2759 data->arg.name = name;
2760 data->arg.attrs = sattr;
2761 data->arg.ftype = ftype;
2762 data->arg.bitmask = server->attr_bitmask;
2763 data->res.server = server;
2764 data->res.fh = &data->fh;
2765 data->res.fattr = &data->fattr;
2766 data->res.dir_fattr = &data->dir_fattr;
2767 nfs_fattr_init(data->res.fattr);
2768 nfs_fattr_init(data->res.dir_fattr);
2773 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2775 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2776 &data->arg, &data->res, 1);
2778 update_changeattr(dir, &data->res.dir_cinfo);
2779 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2780 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2785 static void nfs4_free_createdata(struct nfs4_createdata *data)
2790 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2791 struct page *page, unsigned int len, struct iattr *sattr)
2793 struct nfs4_createdata *data;
2794 int status = -ENAMETOOLONG;
2796 if (len > NFS4_MAXPATHLEN)
2800 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2804 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2805 data->arg.u.symlink.pages = &page;
2806 data->arg.u.symlink.len = len;
2808 status = nfs4_do_create(dir, dentry, data);
2810 nfs4_free_createdata(data);
2815 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2816 struct page *page, unsigned int len, struct iattr *sattr)
2818 struct nfs4_exception exception = { };
2821 err = nfs4_handle_exception(NFS_SERVER(dir),
2822 _nfs4_proc_symlink(dir, dentry, page,
2825 } while (exception.retry);
2829 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2830 struct iattr *sattr)
2832 struct nfs4_createdata *data;
2833 int status = -ENOMEM;
2835 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2839 status = nfs4_do_create(dir, dentry, data);
2841 nfs4_free_createdata(data);
2846 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2847 struct iattr *sattr)
2849 struct nfs4_exception exception = { };
2852 err = nfs4_handle_exception(NFS_SERVER(dir),
2853 _nfs4_proc_mkdir(dir, dentry, sattr),
2855 } while (exception.retry);
2859 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2860 u64 cookie, struct page *page, unsigned int count, int plus)
2862 struct inode *dir = dentry->d_inode;
2863 struct nfs4_readdir_arg args = {
2868 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2870 struct nfs4_readdir_res res;
2871 struct rpc_message msg = {
2872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2879 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2880 dentry->d_parent->d_name.name,
2881 dentry->d_name.name,
2882 (unsigned long long)cookie);
2883 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2884 res.pgbase = args.pgbase;
2885 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2887 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2889 nfs_invalidate_atime(dir);
2891 dprintk("%s: returns %d\n", __func__, status);
2895 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2896 u64 cookie, struct page *page, unsigned int count, int plus)
2898 struct nfs4_exception exception = { };
2901 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2902 _nfs4_proc_readdir(dentry, cred, cookie,
2905 } while (exception.retry);
2909 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2910 struct iattr *sattr, dev_t rdev)
2912 struct nfs4_createdata *data;
2913 int mode = sattr->ia_mode;
2914 int status = -ENOMEM;
2916 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2917 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2919 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2924 data->arg.ftype = NF4FIFO;
2925 else if (S_ISBLK(mode)) {
2926 data->arg.ftype = NF4BLK;
2927 data->arg.u.device.specdata1 = MAJOR(rdev);
2928 data->arg.u.device.specdata2 = MINOR(rdev);
2930 else if (S_ISCHR(mode)) {
2931 data->arg.ftype = NF4CHR;
2932 data->arg.u.device.specdata1 = MAJOR(rdev);
2933 data->arg.u.device.specdata2 = MINOR(rdev);
2936 status = nfs4_do_create(dir, dentry, data);
2938 nfs4_free_createdata(data);
2943 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2944 struct iattr *sattr, dev_t rdev)
2946 struct nfs4_exception exception = { };
2949 err = nfs4_handle_exception(NFS_SERVER(dir),
2950 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2952 } while (exception.retry);
2956 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2957 struct nfs_fsstat *fsstat)
2959 struct nfs4_statfs_arg args = {
2961 .bitmask = server->attr_bitmask,
2963 struct nfs4_statfs_res res = {
2966 struct rpc_message msg = {
2967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2972 nfs_fattr_init(fsstat->fattr);
2973 return nfs4_call_sync(server, &msg, &args, &res, 0);
2976 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2978 struct nfs4_exception exception = { };
2981 err = nfs4_handle_exception(server,
2982 _nfs4_proc_statfs(server, fhandle, fsstat),
2984 } while (exception.retry);
2988 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2989 struct nfs_fsinfo *fsinfo)
2991 struct nfs4_fsinfo_arg args = {
2993 .bitmask = server->attr_bitmask,
2995 struct nfs4_fsinfo_res res = {
2998 struct rpc_message msg = {
2999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3004 return nfs4_call_sync(server, &msg, &args, &res, 0);
3007 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3009 struct nfs4_exception exception = { };
3013 err = nfs4_handle_exception(server,
3014 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3016 } while (exception.retry);
3020 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3022 nfs_fattr_init(fsinfo->fattr);
3023 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3026 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3027 struct nfs_pathconf *pathconf)
3029 struct nfs4_pathconf_arg args = {
3031 .bitmask = server->attr_bitmask,
3033 struct nfs4_pathconf_res res = {
3034 .pathconf = pathconf,
3036 struct rpc_message msg = {
3037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3042 /* None of the pathconf attributes are mandatory to implement */
3043 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3044 memset(pathconf, 0, sizeof(*pathconf));
3048 nfs_fattr_init(pathconf->fattr);
3049 return nfs4_call_sync(server, &msg, &args, &res, 0);
3052 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3053 struct nfs_pathconf *pathconf)
3055 struct nfs4_exception exception = { };
3059 err = nfs4_handle_exception(server,
3060 _nfs4_proc_pathconf(server, fhandle, pathconf),
3062 } while (exception.retry);
3066 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3068 struct nfs_server *server = NFS_SERVER(data->inode);
3070 dprintk("--> %s\n", __func__);
3072 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3074 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3075 nfs_restart_rpc(task, server->nfs_client);
3079 nfs_invalidate_atime(data->inode);
3080 if (task->tk_status > 0)
3081 renew_lease(server, data->timestamp);
3085 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3087 data->timestamp = jiffies;
3088 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3091 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3093 struct inode *inode = data->inode;
3095 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3098 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3099 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3102 if (task->tk_status >= 0) {
3103 renew_lease(NFS_SERVER(inode), data->timestamp);
3104 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3109 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3111 struct nfs_server *server = NFS_SERVER(data->inode);
3113 data->args.bitmask = server->cache_consistency_bitmask;
3114 data->res.server = server;
3115 data->timestamp = jiffies;
3117 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3120 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3122 struct inode *inode = data->inode;
3124 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3126 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3127 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3130 nfs_refresh_inode(inode, data->res.fattr);
3134 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3136 struct nfs_server *server = NFS_SERVER(data->inode);
3138 data->args.bitmask = server->cache_consistency_bitmask;
3139 data->res.server = server;
3140 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3144 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3145 * standalone procedure for queueing an asynchronous RENEW.
3147 static void nfs4_renew_done(struct rpc_task *task, void *data)
3149 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
3150 unsigned long timestamp = (unsigned long)data;
3152 if (task->tk_status < 0) {
3153 /* Unless we're shutting down, schedule state recovery! */
3154 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3155 nfs4_schedule_state_recovery(clp);
3158 spin_lock(&clp->cl_lock);
3159 if (time_before(clp->cl_last_renewal,timestamp))
3160 clp->cl_last_renewal = timestamp;
3161 spin_unlock(&clp->cl_lock);
3164 static const struct rpc_call_ops nfs4_renew_ops = {
3165 .rpc_call_done = nfs4_renew_done,
3168 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3170 struct rpc_message msg = {
3171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3176 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3177 &nfs4_renew_ops, (void *)jiffies);
3180 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3182 struct rpc_message msg = {
3183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3187 unsigned long now = jiffies;
3190 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3193 spin_lock(&clp->cl_lock);
3194 if (time_before(clp->cl_last_renewal,now))
3195 clp->cl_last_renewal = now;
3196 spin_unlock(&clp->cl_lock);
3200 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3202 return (server->caps & NFS_CAP_ACLS)
3203 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3204 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3207 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3208 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3211 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3213 static void buf_to_pages(const void *buf, size_t buflen,
3214 struct page **pages, unsigned int *pgbase)
3216 const void *p = buf;
3218 *pgbase = offset_in_page(buf);
3220 while (p < buf + buflen) {
3221 *(pages++) = virt_to_page(p);
3222 p += PAGE_CACHE_SIZE;
3226 struct nfs4_cached_acl {
3232 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3234 struct nfs_inode *nfsi = NFS_I(inode);
3236 spin_lock(&inode->i_lock);
3237 kfree(nfsi->nfs4_acl);
3238 nfsi->nfs4_acl = acl;
3239 spin_unlock(&inode->i_lock);
3242 static void nfs4_zap_acl_attr(struct inode *inode)
3244 nfs4_set_cached_acl(inode, NULL);
3247 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3249 struct nfs_inode *nfsi = NFS_I(inode);
3250 struct nfs4_cached_acl *acl;
3253 spin_lock(&inode->i_lock);
3254 acl = nfsi->nfs4_acl;
3257 if (buf == NULL) /* user is just asking for length */
3259 if (acl->cached == 0)
3261 ret = -ERANGE; /* see getxattr(2) man page */
3262 if (acl->len > buflen)
3264 memcpy(buf, acl->data, acl->len);
3268 spin_unlock(&inode->i_lock);
3272 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3274 struct nfs4_cached_acl *acl;
3276 if (buf && acl_len <= PAGE_SIZE) {
3277 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3281 memcpy(acl->data, buf, acl_len);
3283 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3290 nfs4_set_cached_acl(inode, acl);
3293 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3295 struct page *pages[NFS4ACL_MAXPAGES];
3296 struct nfs_getaclargs args = {
3297 .fh = NFS_FH(inode),
3301 struct nfs_getaclres res = {
3305 struct rpc_message msg = {
3306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3310 struct page *localpage = NULL;
3313 if (buflen < PAGE_SIZE) {
3314 /* As long as we're doing a round trip to the server anyway,
3315 * let's be prepared for a page of acl data. */
3316 localpage = alloc_page(GFP_KERNEL);
3317 resp_buf = page_address(localpage);
3318 if (localpage == NULL)
3320 args.acl_pages[0] = localpage;
3321 args.acl_pgbase = 0;
3322 args.acl_len = PAGE_SIZE;
3325 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3327 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3330 if (res.acl_len > args.acl_len)
3331 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3333 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3336 if (res.acl_len > buflen)
3339 memcpy(buf, resp_buf, res.acl_len);
3344 __free_page(localpage);
3348 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3350 struct nfs4_exception exception = { };
3353 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3356 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3357 } while (exception.retry);
3361 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3363 struct nfs_server *server = NFS_SERVER(inode);
3366 if (!nfs4_server_supports_acls(server))
3368 ret = nfs_revalidate_inode(server, inode);
3371 ret = nfs4_read_cached_acl(inode, buf, buflen);
3374 return nfs4_get_acl_uncached(inode, buf, buflen);
3377 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3379 struct nfs_server *server = NFS_SERVER(inode);
3380 struct page *pages[NFS4ACL_MAXPAGES];
3381 struct nfs_setaclargs arg = {
3382 .fh = NFS_FH(inode),
3386 struct nfs_setaclres res;
3387 struct rpc_message msg = {
3388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3394 if (!nfs4_server_supports_acls(server))
3396 nfs_inode_return_delegation(inode);
3397 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3398 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3399 nfs_access_zap_cache(inode);
3400 nfs_zap_acl_cache(inode);
3404 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3406 struct nfs4_exception exception = { };
3409 err = nfs4_handle_exception(NFS_SERVER(inode),
3410 __nfs4_proc_set_acl(inode, buf, buflen),
3412 } while (exception.retry);
3417 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3419 if (!clp || task->tk_status >= 0)
3421 switch(task->tk_status) {
3422 case -NFS4ERR_ADMIN_REVOKED:
3423 case -NFS4ERR_BAD_STATEID:
3424 case -NFS4ERR_OPENMODE:
3427 nfs4_state_mark_reclaim_nograce(clp, state);
3428 case -NFS4ERR_STALE_CLIENTID:
3429 case -NFS4ERR_STALE_STATEID:
3430 case -NFS4ERR_EXPIRED:
3431 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3432 nfs4_schedule_state_recovery(clp);
3433 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3434 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3435 task->tk_status = 0;
3437 #if defined(CONFIG_NFS_V4_1)
3438 case -NFS4ERR_BADSESSION:
3439 case -NFS4ERR_BADSLOT:
3440 case -NFS4ERR_BAD_HIGH_SLOT:
3441 case -NFS4ERR_DEADSESSION:
3442 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3443 case -NFS4ERR_SEQ_FALSE_RETRY:
3444 case -NFS4ERR_SEQ_MISORDERED:
3445 dprintk("%s ERROR %d, Reset session\n", __func__,
3447 nfs4_schedule_state_recovery(clp);
3448 task->tk_status = 0;
3450 #endif /* CONFIG_NFS_V4_1 */
3451 case -NFS4ERR_DELAY:
3453 nfs_inc_server_stats(server, NFSIOS_DELAY);
3454 case -NFS4ERR_GRACE:
3455 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3456 task->tk_status = 0;
3458 case -NFS4ERR_OLD_STATEID:
3459 task->tk_status = 0;
3462 task->tk_status = nfs4_map_errors(task->tk_status);
3467 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3469 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3472 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3474 nfs4_verifier sc_verifier;
3475 struct nfs4_setclientid setclientid = {
3476 .sc_verifier = &sc_verifier,
3479 struct rpc_message msg = {
3480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3481 .rpc_argp = &setclientid,
3489 p = (__be32*)sc_verifier.data;
3490 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3491 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3494 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3495 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3497 rpc_peeraddr2str(clp->cl_rpcclient,
3499 rpc_peeraddr2str(clp->cl_rpcclient,
3501 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3502 clp->cl_id_uniquifier);
3503 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3504 sizeof(setclientid.sc_netid),
3505 rpc_peeraddr2str(clp->cl_rpcclient,
3506 RPC_DISPLAY_NETID));
3507 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3508 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3509 clp->cl_ipaddr, port >> 8, port & 255);
3511 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3512 if (status != -NFS4ERR_CLID_INUSE)
3517 ssleep(clp->cl_lease_time + 1);
3519 if (++clp->cl_id_uniquifier == 0)
3525 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3527 struct nfs_fsinfo fsinfo;
3528 struct rpc_message msg = {
3529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3531 .rpc_resp = &fsinfo,
3538 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3540 spin_lock(&clp->cl_lock);
3541 clp->cl_lease_time = fsinfo.lease_time * HZ;
3542 clp->cl_last_renewal = now;
3543 spin_unlock(&clp->cl_lock);
3548 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3553 err = _nfs4_proc_setclientid_confirm(clp, cred);
3557 case -NFS4ERR_RESOURCE:
3558 /* The IBM lawyers misread another document! */
3559 case -NFS4ERR_DELAY:
3560 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3566 struct nfs4_delegreturndata {
3567 struct nfs4_delegreturnargs args;
3568 struct nfs4_delegreturnres res;
3570 nfs4_stateid stateid;
3571 unsigned long timestamp;
3572 struct nfs_fattr fattr;
3576 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3578 struct nfs4_delegreturndata *data = calldata;
3580 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3583 switch (task->tk_status) {
3584 case -NFS4ERR_STALE_STATEID:
3585 case -NFS4ERR_EXPIRED:
3587 renew_lease(data->res.server, data->timestamp);
3590 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3592 nfs_restart_rpc(task, data->res.server->nfs_client);
3596 data->rpc_status = task->tk_status;
3599 static void nfs4_delegreturn_release(void *calldata)
3604 #if defined(CONFIG_NFS_V4_1)
3605 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3607 struct nfs4_delegreturndata *d_data;
3609 d_data = (struct nfs4_delegreturndata *)data;
3611 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3612 &d_data->args.seq_args,
3613 &d_data->res.seq_res, 1, task))
3615 rpc_call_start(task);
3617 #endif /* CONFIG_NFS_V4_1 */
3619 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3620 #if defined(CONFIG_NFS_V4_1)
3621 .rpc_call_prepare = nfs4_delegreturn_prepare,
3622 #endif /* CONFIG_NFS_V4_1 */
3623 .rpc_call_done = nfs4_delegreturn_done,
3624 .rpc_release = nfs4_delegreturn_release,
3627 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3629 struct nfs4_delegreturndata *data;
3630 struct nfs_server *server = NFS_SERVER(inode);
3631 struct rpc_task *task;
3632 struct rpc_message msg = {
3633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3636 struct rpc_task_setup task_setup_data = {
3637 .rpc_client = server->client,
3638 .rpc_message = &msg,
3639 .callback_ops = &nfs4_delegreturn_ops,
3640 .flags = RPC_TASK_ASYNC,
3644 data = kzalloc(sizeof(*data), GFP_KERNEL);
3647 data->args.fhandle = &data->fh;
3648 data->args.stateid = &data->stateid;
3649 data->args.bitmask = server->attr_bitmask;
3650 nfs_copy_fh(&data->fh, NFS_FH(inode));
3651 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3652 data->res.fattr = &data->fattr;
3653 data->res.server = server;
3654 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3655 nfs_fattr_init(data->res.fattr);
3656 data->timestamp = jiffies;
3657 data->rpc_status = 0;
3659 task_setup_data.callback_data = data;
3660 msg.rpc_argp = &data->args,
3661 msg.rpc_resp = &data->res,
3662 task = rpc_run_task(&task_setup_data);
3664 return PTR_ERR(task);
3667 status = nfs4_wait_for_completion_rpc_task(task);
3670 status = data->rpc_status;
3673 nfs_refresh_inode(inode, &data->fattr);
3679 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3681 struct nfs_server *server = NFS_SERVER(inode);
3682 struct nfs4_exception exception = { };
3685 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3687 case -NFS4ERR_STALE_STATEID:
3688 case -NFS4ERR_EXPIRED:
3692 err = nfs4_handle_exception(server, err, &exception);
3693 } while (exception.retry);
3697 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3698 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3701 * sleep, with exponential backoff, and retry the LOCK operation.
3703 static unsigned long
3704 nfs4_set_lock_task_retry(unsigned long timeout)
3706 schedule_timeout_killable(timeout);
3708 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3709 return NFS4_LOCK_MAXTIMEOUT;
3713 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3715 struct inode *inode = state->inode;
3716 struct nfs_server *server = NFS_SERVER(inode);
3717 struct nfs_client *clp = server->nfs_client;
3718 struct nfs_lockt_args arg = {
3719 .fh = NFS_FH(inode),
3722 struct nfs_lockt_res res = {
3725 struct rpc_message msg = {
3726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3729 .rpc_cred = state->owner->so_cred,
3731 struct nfs4_lock_state *lsp;
3734 arg.lock_owner.clientid = clp->cl_clientid;
3735 status = nfs4_set_lock_state(state, request);
3738 lsp = request->fl_u.nfs4_fl.owner;
3739 arg.lock_owner.id = lsp->ls_id.id;
3740 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3743 request->fl_type = F_UNLCK;
3745 case -NFS4ERR_DENIED:
3748 request->fl_ops->fl_release_private(request);
3753 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3755 struct nfs4_exception exception = { };
3759 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3760 _nfs4_proc_getlk(state, cmd, request),
3762 } while (exception.retry);
3766 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3769 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3771 res = posix_lock_file_wait(file, fl);
3774 res = flock_lock_file_wait(file, fl);
3782 struct nfs4_unlockdata {
3783 struct nfs_locku_args arg;
3784 struct nfs_locku_res res;
3785 struct nfs4_lock_state *lsp;
3786 struct nfs_open_context *ctx;
3787 struct file_lock fl;
3788 const struct nfs_server *server;
3789 unsigned long timestamp;
3792 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3793 struct nfs_open_context *ctx,
3794 struct nfs4_lock_state *lsp,
3795 struct nfs_seqid *seqid)
3797 struct nfs4_unlockdata *p;
3798 struct inode *inode = lsp->ls_state->inode;
3800 p = kzalloc(sizeof(*p), GFP_KERNEL);
3803 p->arg.fh = NFS_FH(inode);
3805 p->arg.seqid = seqid;
3806 p->res.seqid = seqid;
3807 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3808 p->arg.stateid = &lsp->ls_stateid;
3810 atomic_inc(&lsp->ls_count);
3811 /* Ensure we don't close file until we're done freeing locks! */
3812 p->ctx = get_nfs_open_context(ctx);
3813 memcpy(&p->fl, fl, sizeof(p->fl));
3814 p->server = NFS_SERVER(inode);
3818 static void nfs4_locku_release_calldata(void *data)
3820 struct nfs4_unlockdata *calldata = data;
3821 nfs_free_seqid(calldata->arg.seqid);
3822 nfs4_put_lock_state(calldata->lsp);
3823 put_nfs_open_context(calldata->ctx);
3827 static void nfs4_locku_done(struct rpc_task *task, void *data)
3829 struct nfs4_unlockdata *calldata = data;
3831 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3833 if (RPC_ASSASSINATED(task))
3835 switch (task->tk_status) {
3837 memcpy(calldata->lsp->ls_stateid.data,
3838 calldata->res.stateid.data,
3839 sizeof(calldata->lsp->ls_stateid.data));
3840 renew_lease(calldata->server, calldata->timestamp);
3842 case -NFS4ERR_BAD_STATEID:
3843 case -NFS4ERR_OLD_STATEID:
3844 case -NFS4ERR_STALE_STATEID:
3845 case -NFS4ERR_EXPIRED:
3848 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3849 nfs_restart_rpc(task,
3850 calldata->server->nfs_client);
3854 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3856 struct nfs4_unlockdata *calldata = data;
3858 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3860 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3861 /* Note: exit _without_ running nfs4_locku_done */
3862 task->tk_action = NULL;
3865 calldata->timestamp = jiffies;
3866 if (nfs4_setup_sequence(calldata->server->nfs_client,
3867 &calldata->arg.seq_args,
3868 &calldata->res.seq_res, 1, task))
3870 rpc_call_start(task);
3873 static const struct rpc_call_ops nfs4_locku_ops = {
3874 .rpc_call_prepare = nfs4_locku_prepare,
3875 .rpc_call_done = nfs4_locku_done,
3876 .rpc_release = nfs4_locku_release_calldata,
3879 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3880 struct nfs_open_context *ctx,
3881 struct nfs4_lock_state *lsp,
3882 struct nfs_seqid *seqid)
3884 struct nfs4_unlockdata *data;
3885 struct rpc_message msg = {
3886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3887 .rpc_cred = ctx->cred,
3889 struct rpc_task_setup task_setup_data = {
3890 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3891 .rpc_message = &msg,
3892 .callback_ops = &nfs4_locku_ops,
3893 .workqueue = nfsiod_workqueue,
3894 .flags = RPC_TASK_ASYNC,
3897 /* Ensure this is an unlock - when canceling a lock, the
3898 * canceled lock is passed in, and it won't be an unlock.
3900 fl->fl_type = F_UNLCK;
3902 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3904 nfs_free_seqid(seqid);
3905 return ERR_PTR(-ENOMEM);
3908 msg.rpc_argp = &data->arg,
3909 msg.rpc_resp = &data->res,
3910 task_setup_data.callback_data = data;
3911 return rpc_run_task(&task_setup_data);
3914 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3916 struct nfs_inode *nfsi = NFS_I(state->inode);
3917 struct nfs_seqid *seqid;
3918 struct nfs4_lock_state *lsp;
3919 struct rpc_task *task;
3921 unsigned char fl_flags = request->fl_flags;
3923 status = nfs4_set_lock_state(state, request);
3924 /* Unlock _before_ we do the RPC call */
3925 request->fl_flags |= FL_EXISTS;
3926 down_read(&nfsi->rwsem);
3927 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3928 up_read(&nfsi->rwsem);
3931 up_read(&nfsi->rwsem);
3934 /* Is this a delegated lock? */
3935 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3937 lsp = request->fl_u.nfs4_fl.owner;
3938 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3942 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3943 status = PTR_ERR(task);
3946 status = nfs4_wait_for_completion_rpc_task(task);
3949 request->fl_flags = fl_flags;
3953 struct nfs4_lockdata {
3954 struct nfs_lock_args arg;
3955 struct nfs_lock_res res;
3956 struct nfs4_lock_state *lsp;
3957 struct nfs_open_context *ctx;
3958 struct file_lock fl;
3959 unsigned long timestamp;
3962 struct nfs_server *server;
3965 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3966 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3968 struct nfs4_lockdata *p;
3969 struct inode *inode = lsp->ls_state->inode;
3970 struct nfs_server *server = NFS_SERVER(inode);
3972 p = kzalloc(sizeof(*p), GFP_KERNEL);
3976 p->arg.fh = NFS_FH(inode);
3978 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3979 if (p->arg.open_seqid == NULL)
3981 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3982 if (p->arg.lock_seqid == NULL)
3983 goto out_free_seqid;
3984 p->arg.lock_stateid = &lsp->ls_stateid;
3985 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3986 p->arg.lock_owner.id = lsp->ls_id.id;
3987 p->res.lock_seqid = p->arg.lock_seqid;
3988 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3991 atomic_inc(&lsp->ls_count);
3992 p->ctx = get_nfs_open_context(ctx);
3993 memcpy(&p->fl, fl, sizeof(p->fl));
3996 nfs_free_seqid(p->arg.open_seqid);
4002 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4004 struct nfs4_lockdata *data = calldata;
4005 struct nfs4_state *state = data->lsp->ls_state;
4007 dprintk("%s: begin!\n", __func__);
4008 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4010 /* Do we need to do an open_to_lock_owner? */
4011 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4012 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4014 data->arg.open_stateid = &state->stateid;
4015 data->arg.new_lock_owner = 1;
4016 data->res.open_seqid = data->arg.open_seqid;
4018 data->arg.new_lock_owner = 0;
4019 data->timestamp = jiffies;
4020 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4021 &data->res.seq_res, 1, task))
4023 rpc_call_start(task);
4024 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4027 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4029 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4030 nfs4_lock_prepare(task, calldata);
4033 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4035 struct nfs4_lockdata *data = calldata;
4037 dprintk("%s: begin!\n", __func__);
4039 nfs4_sequence_done(data->server, &data->res.seq_res,
4042 data->rpc_status = task->tk_status;
4043 if (RPC_ASSASSINATED(task))
4045 if (data->arg.new_lock_owner != 0) {
4046 if (data->rpc_status == 0)
4047 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4051 if (data->rpc_status == 0) {
4052 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4053 sizeof(data->lsp->ls_stateid.data));
4054 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4055 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4058 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4061 static void nfs4_lock_release(void *calldata)
4063 struct nfs4_lockdata *data = calldata;
4065 dprintk("%s: begin!\n", __func__);
4066 nfs_free_seqid(data->arg.open_seqid);
4067 if (data->cancelled != 0) {
4068 struct rpc_task *task;
4069 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4070 data->arg.lock_seqid);
4073 dprintk("%s: cancelling lock!\n", __func__);
4075 nfs_free_seqid(data->arg.lock_seqid);
4076 nfs4_put_lock_state(data->lsp);
4077 put_nfs_open_context(data->ctx);
4079 dprintk("%s: done!\n", __func__);
4082 static const struct rpc_call_ops nfs4_lock_ops = {
4083 .rpc_call_prepare = nfs4_lock_prepare,
4084 .rpc_call_done = nfs4_lock_done,
4085 .rpc_release = nfs4_lock_release,
4088 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4089 .rpc_call_prepare = nfs4_recover_lock_prepare,
4090 .rpc_call_done = nfs4_lock_done,
4091 .rpc_release = nfs4_lock_release,
4094 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4096 struct nfs4_lockdata *data;
4097 struct rpc_task *task;
4098 struct rpc_message msg = {
4099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4100 .rpc_cred = state->owner->so_cred,
4102 struct rpc_task_setup task_setup_data = {
4103 .rpc_client = NFS_CLIENT(state->inode),
4104 .rpc_message = &msg,
4105 .callback_ops = &nfs4_lock_ops,
4106 .workqueue = nfsiod_workqueue,
4107 .flags = RPC_TASK_ASYNC,
4111 dprintk("%s: begin!\n", __func__);
4112 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4113 fl->fl_u.nfs4_fl.owner);
4117 data->arg.block = 1;
4118 if (recovery_type > NFS_LOCK_NEW) {
4119 if (recovery_type == NFS_LOCK_RECLAIM)
4120 data->arg.reclaim = NFS_LOCK_RECLAIM;
4121 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4123 msg.rpc_argp = &data->arg,
4124 msg.rpc_resp = &data->res,
4125 task_setup_data.callback_data = data;
4126 task = rpc_run_task(&task_setup_data);
4128 return PTR_ERR(task);
4129 ret = nfs4_wait_for_completion_rpc_task(task);
4131 ret = data->rpc_status;
4133 data->cancelled = 1;
4135 dprintk("%s: done, ret = %d!\n", __func__, ret);
4139 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4141 struct nfs_server *server = NFS_SERVER(state->inode);
4142 struct nfs4_exception exception = { };
4146 /* Cache the lock if possible... */
4147 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4149 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4150 if (err != -NFS4ERR_DELAY)
4152 nfs4_handle_exception(server, err, &exception);
4153 } while (exception.retry);
4157 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4159 struct nfs_server *server = NFS_SERVER(state->inode);
4160 struct nfs4_exception exception = { };
4163 err = nfs4_set_lock_state(state, request);
4167 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4169 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4173 case -NFS4ERR_GRACE:
4174 case -NFS4ERR_DELAY:
4175 nfs4_handle_exception(server, err, &exception);
4178 } while (exception.retry);
4183 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4185 struct nfs_inode *nfsi = NFS_I(state->inode);
4186 unsigned char fl_flags = request->fl_flags;
4189 /* Is this a delegated open? */
4190 status = nfs4_set_lock_state(state, request);
4193 request->fl_flags |= FL_ACCESS;
4194 status = do_vfs_lock(request->fl_file, request);
4197 down_read(&nfsi->rwsem);
4198 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4199 /* Yes: cache locks! */
4200 /* ...but avoid races with delegation recall... */
4201 request->fl_flags = fl_flags & ~FL_SLEEP;
4202 status = do_vfs_lock(request->fl_file, request);
4205 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4208 /* Note: we always want to sleep here! */
4209 request->fl_flags = fl_flags | FL_SLEEP;
4210 if (do_vfs_lock(request->fl_file, request) < 0)
4211 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4213 up_read(&nfsi->rwsem);
4215 request->fl_flags = fl_flags;
4219 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4221 struct nfs4_exception exception = { };
4225 err = _nfs4_proc_setlk(state, cmd, request);
4226 if (err == -NFS4ERR_DENIED)
4228 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4230 } while (exception.retry);
4235 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4237 struct nfs_open_context *ctx;
4238 struct nfs4_state *state;
4239 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4242 /* verify open state */
4243 ctx = nfs_file_open_context(filp);
4246 if (request->fl_start < 0 || request->fl_end < 0)
4249 if (IS_GETLK(cmd)) {
4251 return nfs4_proc_getlk(state, F_GETLK, request);
4255 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4258 if (request->fl_type == F_UNLCK) {
4260 return nfs4_proc_unlck(state, cmd, request);
4267 status = nfs4_proc_setlk(state, cmd, request);
4268 if ((status != -EAGAIN) || IS_SETLK(cmd))
4270 timeout = nfs4_set_lock_task_retry(timeout);
4271 status = -ERESTARTSYS;
4274 } while(status < 0);
4278 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4280 struct nfs_server *server = NFS_SERVER(state->inode);
4281 struct nfs4_exception exception = { };
4284 err = nfs4_set_lock_state(state, fl);
4288 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4291 printk(KERN_ERR "%s: unhandled error %d.\n",
4296 case -NFS4ERR_EXPIRED:
4297 case -NFS4ERR_STALE_CLIENTID:
4298 case -NFS4ERR_STALE_STATEID:
4299 case -NFS4ERR_BADSESSION:
4300 case -NFS4ERR_BADSLOT:
4301 case -NFS4ERR_BAD_HIGH_SLOT:
4302 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4303 case -NFS4ERR_DEADSESSION:
4304 nfs4_schedule_state_recovery(server->nfs_client);
4308 * The show must go on: exit, but mark the
4309 * stateid as needing recovery.
4311 case -NFS4ERR_ADMIN_REVOKED:
4312 case -NFS4ERR_BAD_STATEID:
4313 case -NFS4ERR_OPENMODE:
4314 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4318 case -NFS4ERR_DENIED:
4319 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4322 case -NFS4ERR_DELAY:
4325 err = nfs4_handle_exception(server, err, &exception);
4326 } while (exception.retry);
4331 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4333 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4334 size_t buflen, int flags)
4336 struct inode *inode = dentry->d_inode;
4338 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4341 return nfs4_proc_set_acl(inode, buf, buflen);
4344 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4345 * and that's what we'll do for e.g. user attributes that haven't been set.
4346 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4347 * attributes in kernel-managed attribute namespaces. */
4348 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4351 struct inode *inode = dentry->d_inode;
4353 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4356 return nfs4_proc_get_acl(inode, buf, buflen);
4359 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4361 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4363 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4365 if (buf && buflen < len)
4368 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4372 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4374 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4375 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4376 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4379 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4380 NFS_ATTR_FATTR_NLINK;
4381 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4385 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4386 struct nfs4_fs_locations *fs_locations, struct page *page)
4388 struct nfs_server *server = NFS_SERVER(dir);
4390 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4391 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4393 struct nfs4_fs_locations_arg args = {
4394 .dir_fh = NFS_FH(dir),
4399 struct nfs4_fs_locations_res res = {
4400 .fs_locations = fs_locations,
4402 struct rpc_message msg = {
4403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4409 dprintk("%s: start\n", __func__);
4410 nfs_fattr_init(&fs_locations->fattr);
4411 fs_locations->server = server;
4412 fs_locations->nlocations = 0;
4413 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4414 nfs_fixup_referral_attributes(&fs_locations->fattr);
4415 dprintk("%s: returned status = %d\n", __func__, status);
4419 #ifdef CONFIG_NFS_V4_1
4421 * nfs4_proc_exchange_id()
4423 * Since the clientid has expired, all compounds using sessions
4424 * associated with the stale clientid will be returning
4425 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4426 * be in some phase of session reset.
4428 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4430 nfs4_verifier verifier;
4431 struct nfs41_exchange_id_args args = {
4433 .flags = clp->cl_exchange_flags,
4435 struct nfs41_exchange_id_res res = {
4439 struct rpc_message msg = {
4440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4447 dprintk("--> %s\n", __func__);
4448 BUG_ON(clp == NULL);
4450 /* Remove server-only flags */
4451 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4453 p = (u32 *)verifier.data;
4454 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4455 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4456 args.verifier = &verifier;
4459 args.id_len = scnprintf(args.id, sizeof(args.id),
4462 rpc_peeraddr2str(clp->cl_rpcclient,
4464 clp->cl_id_uniquifier);
4466 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4468 if (status != NFS4ERR_CLID_INUSE)
4474 if (++clp->cl_id_uniquifier == 0)
4478 dprintk("<-- %s status= %d\n", __func__, status);
4482 struct nfs4_get_lease_time_data {
4483 struct nfs4_get_lease_time_args *args;
4484 struct nfs4_get_lease_time_res *res;
4485 struct nfs_client *clp;
4488 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4492 struct nfs4_get_lease_time_data *data =
4493 (struct nfs4_get_lease_time_data *)calldata;
4495 dprintk("--> %s\n", __func__);
4496 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4497 /* just setup sequence, do not trigger session recovery
4498 since we're invoked within one */
4499 ret = nfs41_setup_sequence(data->clp->cl_session,
4500 &data->args->la_seq_args,
4501 &data->res->lr_seq_res, 0, task);
4503 BUG_ON(ret == -EAGAIN);
4504 rpc_call_start(task);
4505 dprintk("<-- %s\n", __func__);
4509 * Called from nfs4_state_manager thread for session setup, so don't recover
4510 * from sequence operation or clientid errors.
4512 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4514 struct nfs4_get_lease_time_data *data =
4515 (struct nfs4_get_lease_time_data *)calldata;
4517 dprintk("--> %s\n", __func__);
4518 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4519 switch (task->tk_status) {
4520 case -NFS4ERR_DELAY:
4521 case -NFS4ERR_GRACE:
4522 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4523 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4524 task->tk_status = 0;
4525 nfs_restart_rpc(task, data->clp);
4528 dprintk("<-- %s\n", __func__);
4531 struct rpc_call_ops nfs4_get_lease_time_ops = {
4532 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4533 .rpc_call_done = nfs4_get_lease_time_done,
4536 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4538 struct rpc_task *task;
4539 struct nfs4_get_lease_time_args args;
4540 struct nfs4_get_lease_time_res res = {
4541 .lr_fsinfo = fsinfo,
4543 struct nfs4_get_lease_time_data data = {
4548 struct rpc_message msg = {
4549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4553 struct rpc_task_setup task_setup = {
4554 .rpc_client = clp->cl_rpcclient,
4555 .rpc_message = &msg,
4556 .callback_ops = &nfs4_get_lease_time_ops,
4557 .callback_data = &data
4561 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4562 dprintk("--> %s\n", __func__);
4563 task = rpc_run_task(&task_setup);
4566 status = PTR_ERR(task);
4568 status = task->tk_status;
4571 dprintk("<-- %s return %d\n", __func__, status);
4577 * Reset a slot table
4579 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4580 int old_max_slots, int ivalue)
4585 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
4588 * Until we have dynamic slot table adjustment, insist
4589 * upon the same slot table size
4591 if (max_slots != old_max_slots) {
4592 dprintk("%s reset slot table does't match old\n",
4594 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4597 spin_lock(&tbl->slot_tbl_lock);
4598 for (i = 0; i < max_slots; ++i)
4599 tbl->slots[i].seq_nr = ivalue;
4600 spin_unlock(&tbl->slot_tbl_lock);
4601 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4602 tbl, tbl->slots, tbl->max_slots);
4604 dprintk("<-- %s: return %d\n", __func__, ret);
4609 * Reset the forechannel and backchannel slot tables
4611 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4615 status = nfs4_reset_slot_table(&session->fc_slot_table,
4616 session->fc_attrs.max_reqs,
4617 session->fc_slot_table.max_slots,
4622 status = nfs4_reset_slot_table(&session->bc_slot_table,
4623 session->bc_attrs.max_reqs,
4624 session->bc_slot_table.max_slots,
4629 /* Destroy the slot table */
4630 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4632 if (session->fc_slot_table.slots != NULL) {
4633 kfree(session->fc_slot_table.slots);
4634 session->fc_slot_table.slots = NULL;
4636 if (session->bc_slot_table.slots != NULL) {
4637 kfree(session->bc_slot_table.slots);
4638 session->bc_slot_table.slots = NULL;
4644 * Initialize slot table
4646 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4647 int max_slots, int ivalue)
4649 struct nfs4_slot *slot;
4652 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4654 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4656 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4661 spin_lock(&tbl->slot_tbl_lock);
4662 tbl->max_slots = max_slots;
4664 tbl->highest_used_slotid = -1; /* no slot is currently used */
4665 spin_unlock(&tbl->slot_tbl_lock);
4666 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4667 tbl, tbl->slots, tbl->max_slots);
4669 dprintk("<-- %s: return %d\n", __func__, ret);
4674 * Initialize the forechannel and backchannel tables
4676 static int nfs4_init_slot_tables(struct nfs4_session *session)
4678 struct nfs4_slot_table *tbl;
4681 tbl = &session->fc_slot_table;
4682 if (tbl->slots == NULL) {
4683 status = nfs4_init_slot_table(tbl,
4684 session->fc_attrs.max_reqs, 1);
4689 tbl = &session->bc_slot_table;
4690 if (tbl->slots == NULL) {
4691 status = nfs4_init_slot_table(tbl,
4692 session->bc_attrs.max_reqs, 0);
4694 nfs4_destroy_slot_tables(session);
4700 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4702 struct nfs4_session *session;
4703 struct nfs4_slot_table *tbl;
4705 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4710 * The create session reply races with the server back
4711 * channel probe. Mark the client NFS_CS_SESSION_INITING
4712 * so that the client back channel can find the
4715 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4716 init_completion(&session->complete);
4718 tbl = &session->fc_slot_table;
4719 tbl->highest_used_slotid = -1;
4720 spin_lock_init(&tbl->slot_tbl_lock);
4721 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4723 tbl = &session->bc_slot_table;
4724 tbl->highest_used_slotid = -1;
4725 spin_lock_init(&tbl->slot_tbl_lock);
4726 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4732 void nfs4_destroy_session(struct nfs4_session *session)
4734 nfs4_proc_destroy_session(session);
4735 dprintk("%s Destroy backchannel for xprt %p\n",
4736 __func__, session->clp->cl_rpcclient->cl_xprt);
4737 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4738 NFS41_BC_MIN_CALLBACKS);
4739 nfs4_destroy_slot_tables(session);
4744 * Initialize the values to be used by the client in CREATE_SESSION
4745 * If nfs4_init_session set the fore channel request and response sizes,
4748 * Set the back channel max_resp_sz_cached to zero to force the client to
4749 * always set csa_cachethis to FALSE because the current implementation
4750 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4752 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4754 struct nfs4_session *session = args->client->cl_session;
4755 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4756 mxresp_sz = session->fc_attrs.max_resp_sz;
4759 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4761 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4762 /* Fore channel attributes */
4763 args->fc_attrs.headerpadsz = 0;
4764 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4765 args->fc_attrs.max_resp_sz = mxresp_sz;
4766 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4767 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4768 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4770 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4771 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4773 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4774 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4775 args->fc_attrs.max_reqs);
4777 /* Back channel attributes */
4778 args->bc_attrs.headerpadsz = 0;
4779 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4780 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4781 args->bc_attrs.max_resp_sz_cached = 0;
4782 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4783 args->bc_attrs.max_reqs = 1;
4785 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4786 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4788 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4789 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4790 args->bc_attrs.max_reqs);
4793 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4797 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4798 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4802 #define _verify_fore_channel_attr(_name_) \
4803 _verify_channel_attr("fore", #_name_, \
4804 args->fc_attrs._name_, \
4805 session->fc_attrs._name_)
4807 #define _verify_back_channel_attr(_name_) \
4808 _verify_channel_attr("back", #_name_, \
4809 args->bc_attrs._name_, \
4810 session->bc_attrs._name_)
4813 * The server is not allowed to increase the fore channel header pad size,
4814 * maximum response size, or maximum number of operations.
4816 * The back channel attributes are only negotiatied down: We send what the
4817 * (back channel) server insists upon.
4819 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4820 struct nfs4_session *session)
4824 ret |= _verify_fore_channel_attr(headerpadsz);
4825 ret |= _verify_fore_channel_attr(max_resp_sz);
4826 ret |= _verify_fore_channel_attr(max_ops);
4828 ret |= _verify_back_channel_attr(headerpadsz);
4829 ret |= _verify_back_channel_attr(max_rqst_sz);
4830 ret |= _verify_back_channel_attr(max_resp_sz);
4831 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4832 ret |= _verify_back_channel_attr(max_ops);
4833 ret |= _verify_back_channel_attr(max_reqs);
4838 static int _nfs4_proc_create_session(struct nfs_client *clp)
4840 struct nfs4_session *session = clp->cl_session;
4841 struct nfs41_create_session_args args = {
4843 .cb_program = NFS4_CALLBACK,
4845 struct nfs41_create_session_res res = {
4848 struct rpc_message msg = {
4849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4855 nfs4_init_channel_attrs(&args);
4856 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4858 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4861 /* Verify the session's negotiated channel_attrs values */
4862 status = nfs4_verify_channel_attrs(&args, session);
4864 /* Increment the clientid slot sequence id */
4872 * Issues a CREATE_SESSION operation to the server.
4873 * It is the responsibility of the caller to verify the session is
4874 * expired before calling this routine.
4876 int nfs4_proc_create_session(struct nfs_client *clp)
4880 struct nfs4_session *session = clp->cl_session;
4882 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4884 status = _nfs4_proc_create_session(clp);
4888 /* Init and reset the fore channel */
4889 status = nfs4_init_slot_tables(session);
4890 dprintk("slot table initialization returned %d\n", status);
4893 status = nfs4_reset_slot_tables(session);
4894 dprintk("slot table reset returned %d\n", status);
4898 ptr = (unsigned *)&session->sess_id.data[0];
4899 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4900 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4902 dprintk("<-- %s\n", __func__);
4907 * Issue the over-the-wire RPC DESTROY_SESSION.
4908 * The caller must serialize access to this routine.
4910 int nfs4_proc_destroy_session(struct nfs4_session *session)
4913 struct rpc_message msg;
4915 dprintk("--> nfs4_proc_destroy_session\n");
4917 /* session is still being setup */
4918 if (session->clp->cl_cons_state != NFS_CS_READY)
4921 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4922 msg.rpc_argp = session;
4923 msg.rpc_resp = NULL;
4924 msg.rpc_cred = NULL;
4925 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4929 "Got error %d from the server on DESTROY_SESSION. "
4930 "Session has been destroyed regardless...\n", status);
4932 dprintk("<-- nfs4_proc_destroy_session\n");
4936 int nfs4_init_session(struct nfs_server *server)
4938 struct nfs_client *clp = server->nfs_client;
4939 struct nfs4_session *session;
4942 if (!nfs4_has_session(clp))
4945 session = clp->cl_session;
4946 session->fc_attrs.max_rqst_sz = server->wsize + nfs41_maxwrite_overhead;
4947 session->fc_attrs.max_resp_sz = server->rsize + nfs41_maxread_overhead;
4949 ret = nfs4_recover_expired_lease(server);
4951 ret = nfs4_check_client_ready(clp);
4956 * Renew the cl_session lease.
4958 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4960 struct nfs4_sequence_args args;
4961 struct nfs4_sequence_res res;
4963 struct rpc_message msg = {
4964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4970 args.sa_cache_this = 0;
4972 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4973 &res, args.sa_cache_this, 1);
4976 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4978 struct nfs_client *clp = (struct nfs_client *)data;
4980 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4982 if (task->tk_status < 0) {
4983 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4985 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4987 nfs_restart_rpc(task, clp);
4991 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4993 kfree(task->tk_msg.rpc_argp);
4994 kfree(task->tk_msg.rpc_resp);
4996 dprintk("<-- %s\n", __func__);
4999 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5001 struct nfs_client *clp;
5002 struct nfs4_sequence_args *args;
5003 struct nfs4_sequence_res *res;
5005 clp = (struct nfs_client *)data;
5006 args = task->tk_msg.rpc_argp;
5007 res = task->tk_msg.rpc_resp;
5009 if (nfs4_setup_sequence(clp, args, res, 0, task))
5011 rpc_call_start(task);
5014 static const struct rpc_call_ops nfs41_sequence_ops = {
5015 .rpc_call_done = nfs41_sequence_call_done,
5016 .rpc_call_prepare = nfs41_sequence_prepare,
5019 static int nfs41_proc_async_sequence(struct nfs_client *clp,
5020 struct rpc_cred *cred)
5022 struct nfs4_sequence_args *args;
5023 struct nfs4_sequence_res *res;
5024 struct rpc_message msg = {
5025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5029 args = kzalloc(sizeof(*args), GFP_KERNEL);
5032 res = kzalloc(sizeof(*res), GFP_KERNEL);
5037 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5038 msg.rpc_argp = args;
5041 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5042 &nfs41_sequence_ops, (void *)clp);
5045 struct nfs4_reclaim_complete_data {
5046 struct nfs_client *clp;
5047 struct nfs41_reclaim_complete_args arg;
5048 struct nfs41_reclaim_complete_res res;
5051 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5053 struct nfs4_reclaim_complete_data *calldata = data;
5055 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5056 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5057 &calldata->res.seq_res, 0, task))
5060 rpc_call_start(task);
5063 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5065 struct nfs4_reclaim_complete_data *calldata = data;
5066 struct nfs_client *clp = calldata->clp;
5067 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5069 dprintk("--> %s\n", __func__);
5070 nfs41_sequence_done(clp, res, task->tk_status);
5071 switch (task->tk_status) {
5073 case -NFS4ERR_COMPLETE_ALREADY:
5075 case -NFS4ERR_BADSESSION:
5076 case -NFS4ERR_DEADSESSION:
5078 * Handle the session error, but do not retry the operation, as
5079 * we have no way of telling whether the clientid had to be
5080 * reset before we got our reply. If reset, a new wave of
5081 * reclaim operations will follow, containing their own reclaim
5082 * complete. We don't want our retry to get on the way of
5083 * recovery by incorrectly indicating to the server that we're
5084 * done reclaiming state since the process had to be restarted.
5086 _nfs4_async_handle_error(task, NULL, clp, NULL);
5089 if (_nfs4_async_handle_error(
5090 task, NULL, clp, NULL) == -EAGAIN) {
5091 rpc_restart_call_prepare(task);
5096 dprintk("<-- %s\n", __func__);
5099 static void nfs4_free_reclaim_complete_data(void *data)
5101 struct nfs4_reclaim_complete_data *calldata = data;
5106 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5107 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5108 .rpc_call_done = nfs4_reclaim_complete_done,
5109 .rpc_release = nfs4_free_reclaim_complete_data,
5113 * Issue a global reclaim complete.
5115 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5117 struct nfs4_reclaim_complete_data *calldata;
5118 struct rpc_task *task;
5119 struct rpc_message msg = {
5120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5122 struct rpc_task_setup task_setup_data = {
5123 .rpc_client = clp->cl_rpcclient,
5124 .rpc_message = &msg,
5125 .callback_ops = &nfs4_reclaim_complete_call_ops,
5126 .flags = RPC_TASK_ASYNC,
5128 int status = -ENOMEM;
5130 dprintk("--> %s\n", __func__);
5131 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5132 if (calldata == NULL)
5134 calldata->clp = clp;
5135 calldata->arg.one_fs = 0;
5136 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5138 msg.rpc_argp = &calldata->arg;
5139 msg.rpc_resp = &calldata->res;
5140 task_setup_data.callback_data = calldata;
5141 task = rpc_run_task(&task_setup_data);
5143 status = PTR_ERR(task);
5146 dprintk("<-- %s status=%d\n", __func__, status);
5149 #endif /* CONFIG_NFS_V4_1 */
5151 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5152 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5153 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5154 .recover_open = nfs4_open_reclaim,
5155 .recover_lock = nfs4_lock_reclaim,
5156 .establish_clid = nfs4_init_clientid,
5157 .get_clid_cred = nfs4_get_setclientid_cred,
5160 #if defined(CONFIG_NFS_V4_1)
5161 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5162 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5163 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5164 .recover_open = nfs4_open_reclaim,
5165 .recover_lock = nfs4_lock_reclaim,
5166 .establish_clid = nfs41_init_clientid,
5167 .get_clid_cred = nfs4_get_exchange_id_cred,
5168 .reclaim_complete = nfs41_proc_reclaim_complete,
5170 #endif /* CONFIG_NFS_V4_1 */
5172 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5173 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5174 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5175 .recover_open = nfs4_open_expired,
5176 .recover_lock = nfs4_lock_expired,
5177 .establish_clid = nfs4_init_clientid,
5178 .get_clid_cred = nfs4_get_setclientid_cred,
5181 #if defined(CONFIG_NFS_V4_1)
5182 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5183 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5184 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5185 .recover_open = nfs4_open_expired,
5186 .recover_lock = nfs4_lock_expired,
5187 .establish_clid = nfs41_init_clientid,
5188 .get_clid_cred = nfs4_get_exchange_id_cred,
5190 #endif /* CONFIG_NFS_V4_1 */
5192 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5193 .sched_state_renewal = nfs4_proc_async_renew,
5194 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5195 .renew_lease = nfs4_proc_renew,
5198 #if defined(CONFIG_NFS_V4_1)
5199 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5200 .sched_state_renewal = nfs41_proc_async_sequence,
5201 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5202 .renew_lease = nfs4_proc_sequence,
5207 * Per minor version reboot and network partition recovery ops
5210 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5211 &nfs40_reboot_recovery_ops,
5212 #if defined(CONFIG_NFS_V4_1)
5213 &nfs41_reboot_recovery_ops,
5217 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5218 &nfs40_nograce_recovery_ops,
5219 #if defined(CONFIG_NFS_V4_1)
5220 &nfs41_nograce_recovery_ops,
5224 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5225 &nfs40_state_renewal_ops,
5226 #if defined(CONFIG_NFS_V4_1)
5227 &nfs41_state_renewal_ops,
5231 static const struct inode_operations nfs4_file_inode_operations = {
5232 .permission = nfs_permission,
5233 .getattr = nfs_getattr,
5234 .setattr = nfs_setattr,
5235 .getxattr = nfs4_getxattr,
5236 .setxattr = nfs4_setxattr,
5237 .listxattr = nfs4_listxattr,
5240 const struct nfs_rpc_ops nfs_v4_clientops = {
5241 .version = 4, /* protocol version */
5242 .dentry_ops = &nfs4_dentry_operations,
5243 .dir_inode_ops = &nfs4_dir_inode_operations,
5244 .file_inode_ops = &nfs4_file_inode_operations,
5245 .getroot = nfs4_proc_get_root,
5246 .getattr = nfs4_proc_getattr,
5247 .setattr = nfs4_proc_setattr,
5248 .lookupfh = nfs4_proc_lookupfh,
5249 .lookup = nfs4_proc_lookup,
5250 .access = nfs4_proc_access,
5251 .readlink = nfs4_proc_readlink,
5252 .create = nfs4_proc_create,
5253 .remove = nfs4_proc_remove,
5254 .unlink_setup = nfs4_proc_unlink_setup,
5255 .unlink_done = nfs4_proc_unlink_done,
5256 .rename = nfs4_proc_rename,
5257 .link = nfs4_proc_link,
5258 .symlink = nfs4_proc_symlink,
5259 .mkdir = nfs4_proc_mkdir,
5260 .rmdir = nfs4_proc_remove,
5261 .readdir = nfs4_proc_readdir,
5262 .mknod = nfs4_proc_mknod,
5263 .statfs = nfs4_proc_statfs,
5264 .fsinfo = nfs4_proc_fsinfo,
5265 .pathconf = nfs4_proc_pathconf,
5266 .set_capabilities = nfs4_server_capabilities,
5267 .decode_dirent = nfs4_decode_dirent,
5268 .read_setup = nfs4_proc_read_setup,
5269 .read_done = nfs4_read_done,
5270 .write_setup = nfs4_proc_write_setup,
5271 .write_done = nfs4_write_done,
5272 .commit_setup = nfs4_proc_commit_setup,
5273 .commit_done = nfs4_commit_done,
5274 .lock = nfs4_proc_lock,
5275 .clear_acl_cache = nfs4_zap_acl_attr,
5276 .close_context = nfs4_close_context,