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_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
68 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
69 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
70 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72 /* Prevent leaks of NFSv4 errors into userland */
73 static int nfs4_map_errors(int err)
78 case -NFS4ERR_RESOURCE:
81 dprintk("%s could not handle NFSv4 error %d\n",
89 * This is our standard bitmap for GETATTR requests.
91 const u32 nfs4_fattr_bitmap[2] = {
96 | FATTR4_WORD0_FILEID,
98 | FATTR4_WORD1_NUMLINKS
100 | FATTR4_WORD1_OWNER_GROUP
101 | FATTR4_WORD1_RAWDEV
102 | FATTR4_WORD1_SPACE_USED
103 | FATTR4_WORD1_TIME_ACCESS
104 | FATTR4_WORD1_TIME_METADATA
105 | FATTR4_WORD1_TIME_MODIFY
108 const u32 nfs4_statfs_bitmap[2] = {
109 FATTR4_WORD0_FILES_AVAIL
110 | FATTR4_WORD0_FILES_FREE
111 | FATTR4_WORD0_FILES_TOTAL,
112 FATTR4_WORD1_SPACE_AVAIL
113 | FATTR4_WORD1_SPACE_FREE
114 | FATTR4_WORD1_SPACE_TOTAL
117 const u32 nfs4_pathconf_bitmap[2] = {
119 | FATTR4_WORD0_MAXNAME,
123 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
124 | FATTR4_WORD0_MAXREAD
125 | FATTR4_WORD0_MAXWRITE
126 | FATTR4_WORD0_LEASE_TIME,
130 const u32 nfs4_fs_locations_bitmap[2] = {
132 | FATTR4_WORD0_CHANGE
135 | FATTR4_WORD0_FILEID
136 | FATTR4_WORD0_FS_LOCATIONS,
138 | FATTR4_WORD1_NUMLINKS
140 | FATTR4_WORD1_OWNER_GROUP
141 | FATTR4_WORD1_RAWDEV
142 | FATTR4_WORD1_SPACE_USED
143 | FATTR4_WORD1_TIME_ACCESS
144 | FATTR4_WORD1_TIME_METADATA
145 | FATTR4_WORD1_TIME_MODIFY
146 | FATTR4_WORD1_MOUNTED_ON_FILEID
149 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
150 struct nfs4_readdir_arg *readdir)
154 BUG_ON(readdir->count < 80);
156 readdir->cookie = cookie;
157 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
162 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
167 * NFSv4 servers do not return entries for '.' and '..'
168 * Therefore, we fake these entries here. We let '.'
169 * have cookie 0 and '..' have cookie 1. Note that
170 * when talking to the server, we always send cookie 0
173 start = p = kmap_atomic(*readdir->pages, KM_USER0);
176 *p++ = xdr_one; /* next */
177 *p++ = xdr_zero; /* cookie, first word */
178 *p++ = xdr_one; /* cookie, second word */
179 *p++ = xdr_one; /* entry len */
180 memcpy(p, ".\0\0\0", 4); /* entry */
182 *p++ = xdr_one; /* bitmap length */
183 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
184 *p++ = htonl(8); /* attribute buffer length */
185 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
188 *p++ = xdr_one; /* next */
189 *p++ = xdr_zero; /* cookie, first word */
190 *p++ = xdr_two; /* cookie, second word */
191 *p++ = xdr_two; /* entry len */
192 memcpy(p, "..\0\0", 4); /* entry */
194 *p++ = xdr_one; /* bitmap length */
195 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
196 *p++ = htonl(8); /* attribute buffer length */
197 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
199 readdir->pgbase = (char *)p - (char *)start;
200 readdir->count -= readdir->pgbase;
201 kunmap_atomic(start, KM_USER0);
204 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
211 nfs_wait_bit_killable, TASK_KILLABLE);
215 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
222 *timeout = NFS4_POLL_RETRY_MIN;
223 if (*timeout > NFS4_POLL_RETRY_MAX)
224 *timeout = NFS4_POLL_RETRY_MAX;
225 schedule_timeout_killable(*timeout);
226 if (fatal_signal_pending(current))
232 /* This is the error handling routine for processes that are allowed
235 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
237 struct nfs_client *clp = server->nfs_client;
238 struct nfs4_state *state = exception->state;
241 exception->retry = 0;
245 case -NFS4ERR_ADMIN_REVOKED:
246 case -NFS4ERR_BAD_STATEID:
247 case -NFS4ERR_OPENMODE:
250 nfs4_state_mark_reclaim_nograce(clp, state);
251 case -NFS4ERR_STALE_CLIENTID:
252 case -NFS4ERR_STALE_STATEID:
253 case -NFS4ERR_EXPIRED:
254 nfs4_schedule_state_recovery(clp);
255 ret = nfs4_wait_clnt_recover(clp);
257 exception->retry = 1;
258 #if !defined(CONFIG_NFS_V4_1)
260 #else /* !defined(CONFIG_NFS_V4_1) */
261 if (!nfs4_has_session(server->nfs_client))
264 case -NFS4ERR_BADSESSION:
265 case -NFS4ERR_BADSLOT:
266 case -NFS4ERR_BAD_HIGH_SLOT:
267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
268 case -NFS4ERR_DEADSESSION:
269 case -NFS4ERR_SEQ_FALSE_RETRY:
270 case -NFS4ERR_SEQ_MISORDERED:
271 dprintk("%s ERROR: %d Reset session\n", __func__,
273 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
274 exception->retry = 1;
276 #endif /* !defined(CONFIG_NFS_V4_1) */
277 case -NFS4ERR_FILE_OPEN:
278 if (exception->timeout > HZ) {
279 /* We have retried a decent amount, time to
287 ret = nfs4_delay(server->client, &exception->timeout);
290 case -NFS4ERR_OLD_STATEID:
291 exception->retry = 1;
293 /* We failed to handle the error */
294 return nfs4_map_errors(ret);
298 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
300 struct nfs_client *clp = server->nfs_client;
301 spin_lock(&clp->cl_lock);
302 if (time_before(clp->cl_last_renewal,timestamp))
303 clp->cl_last_renewal = timestamp;
304 spin_unlock(&clp->cl_lock);
307 #if defined(CONFIG_NFS_V4_1)
310 * nfs4_free_slot - free a slot and efficiently update slot table.
312 * freeing a slot is trivially done by clearing its respective bit
314 * If the freed slotid equals highest_used_slotid we want to update it
315 * so that the server would be able to size down the slot table if needed,
316 * otherwise we know that the highest_used_slotid is still in use.
317 * When updating highest_used_slotid there may be "holes" in the bitmap
318 * so we need to scan down from highest_used_slotid to 0 looking for the now
319 * highest slotid in use.
320 * If none found, highest_used_slotid is set to -1.
323 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
325 int slotid = free_slotid;
327 spin_lock(&tbl->slot_tbl_lock);
328 /* clear used bit in bitmap */
329 __clear_bit(slotid, tbl->used_slots);
331 /* update highest_used_slotid when it is freed */
332 if (slotid == tbl->highest_used_slotid) {
333 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
334 if (slotid >= 0 && slotid < tbl->max_slots)
335 tbl->highest_used_slotid = slotid;
337 tbl->highest_used_slotid = -1;
339 rpc_wake_up_next(&tbl->slot_tbl_waitq);
340 spin_unlock(&tbl->slot_tbl_lock);
341 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
342 free_slotid, tbl->highest_used_slotid);
345 void nfs41_sequence_free_slot(const struct nfs_client *clp,
346 struct nfs4_sequence_res *res)
348 struct nfs4_slot_table *tbl;
350 if (!nfs4_has_session(clp)) {
351 dprintk("%s: No session\n", __func__);
354 tbl = &clp->cl_session->fc_slot_table;
355 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
356 dprintk("%s: No slot\n", __func__);
357 /* just wake up the next guy waiting since
358 * we may have not consumed a slot after all */
359 rpc_wake_up_next(&tbl->slot_tbl_waitq);
362 nfs4_free_slot(tbl, res->sr_slotid);
363 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
365 /* Signal state manager thread if session is drained */
366 if (test_bit(NFS4CLNT_SESSION_DRAINING, &clp->cl_state)) {
367 spin_lock(&tbl->slot_tbl_lock);
368 if (tbl->highest_used_slotid == -1) {
369 dprintk("%s COMPLETE: Session Drained\n", __func__);
370 complete(&clp->cl_session->complete);
372 spin_unlock(&tbl->slot_tbl_lock);
376 static void nfs41_sequence_done(struct nfs_client *clp,
377 struct nfs4_sequence_res *res,
380 unsigned long timestamp;
381 struct nfs4_slot_table *tbl;
382 struct nfs4_slot *slot;
385 * sr_status remains 1 if an RPC level error occurred. The server
386 * may or may not have processed the sequence operation..
387 * Proceed as if the server received and processed the sequence
390 if (res->sr_status == 1)
391 res->sr_status = NFS_OK;
393 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
394 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
397 tbl = &clp->cl_session->fc_slot_table;
398 slot = tbl->slots + res->sr_slotid;
400 if (res->sr_status == 0) {
401 /* Update the slot's sequence and clientid lease timer */
403 timestamp = res->sr_renewal_time;
404 spin_lock(&clp->cl_lock);
405 if (time_before(clp->cl_last_renewal, timestamp))
406 clp->cl_last_renewal = timestamp;
407 spin_unlock(&clp->cl_lock);
411 /* The session may be reset by one of the error handlers. */
412 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
413 nfs41_sequence_free_slot(clp, res);
417 * nfs4_find_slot - efficiently look for a free slot
419 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
420 * If found, we mark the slot as used, update the highest_used_slotid,
421 * and respectively set up the sequence operation args.
422 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
424 * Note: must be called with under the slot_tbl_lock.
427 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
430 u8 ret_id = NFS4_MAX_SLOT_TABLE;
431 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
433 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
434 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
436 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
437 if (slotid >= tbl->max_slots)
439 __set_bit(slotid, tbl->used_slots);
440 if (slotid > tbl->highest_used_slotid)
441 tbl->highest_used_slotid = slotid;
444 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
445 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
449 static int nfs41_setup_sequence(struct nfs4_session *session,
450 struct nfs4_sequence_args *args,
451 struct nfs4_sequence_res *res,
453 struct rpc_task *task)
455 struct nfs4_slot *slot;
456 struct nfs4_slot_table *tbl;
459 dprintk("--> %s\n", __func__);
460 /* slot already allocated? */
461 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
464 memset(res, 0, sizeof(*res));
465 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
466 tbl = &session->fc_slot_table;
468 spin_lock(&tbl->slot_tbl_lock);
469 if (test_bit(NFS4CLNT_SESSION_RESET, &session->clp->cl_state)) {
471 * The state manager will wait until the slot table is empty.
472 * Schedule the reset thread
474 dprintk("%s Schedule Session Reset\n", __func__);
475 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
476 nfs4_schedule_state_manager(session->clp);
477 spin_unlock(&tbl->slot_tbl_lock);
481 slotid = nfs4_find_slot(tbl, task);
482 if (slotid == NFS4_MAX_SLOT_TABLE) {
483 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
484 spin_unlock(&tbl->slot_tbl_lock);
485 dprintk("<-- %s: no free slots\n", __func__);
488 spin_unlock(&tbl->slot_tbl_lock);
490 slot = tbl->slots + slotid;
491 args->sa_session = session;
492 args->sa_slotid = slotid;
493 args->sa_cache_this = cache_reply;
495 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
497 res->sr_session = session;
498 res->sr_slotid = slotid;
499 res->sr_renewal_time = jiffies;
501 * sr_status is only set in decode_sequence, and so will remain
502 * set to 1 if an rpc level failure occurs.
508 int nfs4_setup_sequence(struct nfs_client *clp,
509 struct nfs4_sequence_args *args,
510 struct nfs4_sequence_res *res,
512 struct rpc_task *task)
516 dprintk("--> %s clp %p session %p sr_slotid %d\n",
517 __func__, clp, clp->cl_session, res->sr_slotid);
519 if (!nfs4_has_session(clp))
521 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
523 if (ret && ret != -EAGAIN) {
524 /* terminate rpc task */
525 task->tk_status = ret;
526 task->tk_action = NULL;
529 dprintk("<-- %s status=%d\n", __func__, ret);
533 struct nfs41_call_sync_data {
534 struct nfs_client *clp;
535 struct nfs4_sequence_args *seq_args;
536 struct nfs4_sequence_res *seq_res;
540 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
542 struct nfs41_call_sync_data *data = calldata;
544 dprintk("--> %s data->clp->cl_session %p\n", __func__,
545 data->clp->cl_session);
546 if (nfs4_setup_sequence(data->clp, data->seq_args,
547 data->seq_res, data->cache_reply, task))
549 rpc_call_start(task);
552 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
554 struct nfs41_call_sync_data *data = calldata;
556 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
557 nfs41_sequence_free_slot(data->clp, data->seq_res);
560 struct rpc_call_ops nfs41_call_sync_ops = {
561 .rpc_call_prepare = nfs41_call_sync_prepare,
562 .rpc_call_done = nfs41_call_sync_done,
565 static int nfs4_call_sync_sequence(struct nfs_client *clp,
566 struct rpc_clnt *clnt,
567 struct rpc_message *msg,
568 struct nfs4_sequence_args *args,
569 struct nfs4_sequence_res *res,
573 struct rpc_task *task;
574 struct nfs41_call_sync_data data = {
578 .cache_reply = cache_reply,
580 struct rpc_task_setup task_setup = {
583 .callback_ops = &nfs41_call_sync_ops,
584 .callback_data = &data
587 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
588 task = rpc_run_task(&task_setup);
592 ret = task->tk_status;
598 int _nfs4_call_sync_session(struct nfs_server *server,
599 struct rpc_message *msg,
600 struct nfs4_sequence_args *args,
601 struct nfs4_sequence_res *res,
604 return nfs4_call_sync_sequence(server->nfs_client, server->client,
605 msg, args, res, cache_reply);
608 #endif /* CONFIG_NFS_V4_1 */
610 int _nfs4_call_sync(struct nfs_server *server,
611 struct rpc_message *msg,
612 struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res,
616 args->sa_session = res->sr_session = NULL;
617 return rpc_call_sync(server->client, msg, 0);
620 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
621 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
622 &(res)->seq_res, (cache_reply))
624 static void nfs4_sequence_done(const struct nfs_server *server,
625 struct nfs4_sequence_res *res, int rpc_status)
627 #ifdef CONFIG_NFS_V4_1
628 if (nfs4_has_session(server->nfs_client))
629 nfs41_sequence_done(server->nfs_client, res, rpc_status);
630 #endif /* CONFIG_NFS_V4_1 */
633 void nfs4_restart_rpc(struct rpc_task *task, const struct nfs_client *clp,
634 struct nfs4_sequence_res *res)
636 #ifdef CONFIG_NFS_V4_1
637 if (nfs4_has_session(clp)) {
638 nfs41_sequence_free_slot(clp, res);
639 rpc_restart_call_prepare(task);
642 #endif /* CONFIG_NFS_V4_1 */
643 rpc_restart_call(task);
646 /* no restart, therefore free slot here */
647 static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
648 struct nfs4_sequence_res *res,
651 nfs4_sequence_done(server, res, rpc_status);
652 nfs4_sequence_free_slot(server->nfs_client, res);
655 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
657 struct nfs_inode *nfsi = NFS_I(dir);
659 spin_lock(&dir->i_lock);
660 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
661 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
662 nfs_force_lookup_revalidate(dir);
663 nfsi->change_attr = cinfo->after;
664 spin_unlock(&dir->i_lock);
667 struct nfs4_opendata {
669 struct nfs_openargs o_arg;
670 struct nfs_openres o_res;
671 struct nfs_open_confirmargs c_arg;
672 struct nfs_open_confirmres c_res;
673 struct nfs_fattr f_attr;
674 struct nfs_fattr dir_attr;
677 struct nfs4_state_owner *owner;
678 struct nfs4_state *state;
680 unsigned long timestamp;
681 unsigned int rpc_done : 1;
687 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
689 p->o_res.f_attr = &p->f_attr;
690 p->o_res.dir_attr = &p->dir_attr;
691 p->o_res.seqid = p->o_arg.seqid;
692 p->c_res.seqid = p->c_arg.seqid;
693 p->o_res.server = p->o_arg.server;
694 nfs_fattr_init(&p->f_attr);
695 nfs_fattr_init(&p->dir_attr);
696 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
699 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
700 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
701 const struct iattr *attrs)
703 struct dentry *parent = dget_parent(path->dentry);
704 struct inode *dir = parent->d_inode;
705 struct nfs_server *server = NFS_SERVER(dir);
706 struct nfs4_opendata *p;
708 p = kzalloc(sizeof(*p), GFP_KERNEL);
711 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
712 if (p->o_arg.seqid == NULL)
714 p->path.mnt = mntget(path->mnt);
715 p->path.dentry = dget(path->dentry);
718 atomic_inc(&sp->so_count);
719 p->o_arg.fh = NFS_FH(dir);
720 p->o_arg.open_flags = flags;
721 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
722 p->o_arg.clientid = server->nfs_client->cl_clientid;
723 p->o_arg.id = sp->so_owner_id.id;
724 p->o_arg.name = &p->path.dentry->d_name;
725 p->o_arg.server = server;
726 p->o_arg.bitmask = server->attr_bitmask;
727 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
728 if (flags & O_EXCL) {
729 u32 *s = (u32 *) p->o_arg.u.verifier.data;
732 } else if (flags & O_CREAT) {
733 p->o_arg.u.attrs = &p->attrs;
734 memcpy(&p->attrs, attrs, sizeof(p->attrs));
736 p->c_arg.fh = &p->o_res.fh;
737 p->c_arg.stateid = &p->o_res.stateid;
738 p->c_arg.seqid = p->o_arg.seqid;
739 nfs4_init_opendata_res(p);
749 static void nfs4_opendata_free(struct kref *kref)
751 struct nfs4_opendata *p = container_of(kref,
752 struct nfs4_opendata, kref);
754 nfs_free_seqid(p->o_arg.seqid);
755 if (p->state != NULL)
756 nfs4_put_open_state(p->state);
757 nfs4_put_state_owner(p->owner);
763 static void nfs4_opendata_put(struct nfs4_opendata *p)
766 kref_put(&p->kref, nfs4_opendata_free);
769 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
773 ret = rpc_wait_for_completion_task(task);
777 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
781 if (open_mode & O_EXCL)
783 switch (mode & (FMODE_READ|FMODE_WRITE)) {
785 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
788 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
790 case FMODE_READ|FMODE_WRITE:
791 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
797 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
799 if ((delegation->type & fmode) != fmode)
801 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
803 nfs_mark_delegation_referenced(delegation);
807 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
816 case FMODE_READ|FMODE_WRITE:
819 nfs4_state_set_mode_locked(state, state->state | fmode);
822 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
824 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
825 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
826 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
829 set_bit(NFS_O_RDONLY_STATE, &state->flags);
832 set_bit(NFS_O_WRONLY_STATE, &state->flags);
834 case FMODE_READ|FMODE_WRITE:
835 set_bit(NFS_O_RDWR_STATE, &state->flags);
839 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
841 write_seqlock(&state->seqlock);
842 nfs_set_open_stateid_locked(state, stateid, fmode);
843 write_sequnlock(&state->seqlock);
846 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
849 * Protect the call to nfs4_state_set_mode_locked and
850 * serialise the stateid update
852 write_seqlock(&state->seqlock);
853 if (deleg_stateid != NULL) {
854 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
855 set_bit(NFS_DELEGATED_STATE, &state->flags);
857 if (open_stateid != NULL)
858 nfs_set_open_stateid_locked(state, open_stateid, fmode);
859 write_sequnlock(&state->seqlock);
860 spin_lock(&state->owner->so_lock);
861 update_open_stateflags(state, fmode);
862 spin_unlock(&state->owner->so_lock);
865 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
867 struct nfs_inode *nfsi = NFS_I(state->inode);
868 struct nfs_delegation *deleg_cur;
871 fmode &= (FMODE_READ|FMODE_WRITE);
874 deleg_cur = rcu_dereference(nfsi->delegation);
875 if (deleg_cur == NULL)
878 spin_lock(&deleg_cur->lock);
879 if (nfsi->delegation != deleg_cur ||
880 (deleg_cur->type & fmode) != fmode)
881 goto no_delegation_unlock;
883 if (delegation == NULL)
884 delegation = &deleg_cur->stateid;
885 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
886 goto no_delegation_unlock;
888 nfs_mark_delegation_referenced(deleg_cur);
889 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
891 no_delegation_unlock:
892 spin_unlock(&deleg_cur->lock);
896 if (!ret && open_stateid != NULL) {
897 __update_open_stateid(state, open_stateid, NULL, fmode);
905 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
907 struct nfs_delegation *delegation;
910 delegation = rcu_dereference(NFS_I(inode)->delegation);
911 if (delegation == NULL || (delegation->type & fmode) == fmode) {
916 nfs_inode_return_delegation(inode);
919 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
921 struct nfs4_state *state = opendata->state;
922 struct nfs_inode *nfsi = NFS_I(state->inode);
923 struct nfs_delegation *delegation;
924 int open_mode = opendata->o_arg.open_flags & O_EXCL;
925 fmode_t fmode = opendata->o_arg.fmode;
926 nfs4_stateid stateid;
930 if (can_open_cached(state, fmode, open_mode)) {
931 spin_lock(&state->owner->so_lock);
932 if (can_open_cached(state, fmode, open_mode)) {
933 update_open_stateflags(state, fmode);
934 spin_unlock(&state->owner->so_lock);
935 goto out_return_state;
937 spin_unlock(&state->owner->so_lock);
940 delegation = rcu_dereference(nfsi->delegation);
941 if (delegation == NULL ||
942 !can_open_delegated(delegation, fmode)) {
946 /* Save the delegation */
947 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
949 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
954 /* Try to update the stateid using the delegation */
955 if (update_open_stateid(state, NULL, &stateid, fmode))
956 goto out_return_state;
961 atomic_inc(&state->count);
965 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
968 struct nfs4_state *state = NULL;
969 struct nfs_delegation *delegation;
972 if (!data->rpc_done) {
973 state = nfs4_try_open_cached(data);
978 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
980 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
981 ret = PTR_ERR(inode);
985 state = nfs4_get_open_state(inode, data->owner);
988 if (data->o_res.delegation_type != 0) {
989 int delegation_flags = 0;
992 delegation = rcu_dereference(NFS_I(inode)->delegation);
994 delegation_flags = delegation->flags;
996 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
997 nfs_inode_set_delegation(state->inode,
998 data->owner->so_cred,
1001 nfs_inode_reclaim_delegation(state->inode,
1002 data->owner->so_cred,
1006 update_open_stateid(state, &data->o_res.stateid, NULL,
1014 return ERR_PTR(ret);
1017 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1019 struct nfs_inode *nfsi = NFS_I(state->inode);
1020 struct nfs_open_context *ctx;
1022 spin_lock(&state->inode->i_lock);
1023 list_for_each_entry(ctx, &nfsi->open_files, list) {
1024 if (ctx->state != state)
1026 get_nfs_open_context(ctx);
1027 spin_unlock(&state->inode->i_lock);
1030 spin_unlock(&state->inode->i_lock);
1031 return ERR_PTR(-ENOENT);
1034 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1036 struct nfs4_opendata *opendata;
1038 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
1039 if (opendata == NULL)
1040 return ERR_PTR(-ENOMEM);
1041 opendata->state = state;
1042 atomic_inc(&state->count);
1046 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1048 struct nfs4_state *newstate;
1051 opendata->o_arg.open_flags = 0;
1052 opendata->o_arg.fmode = fmode;
1053 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1054 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1055 nfs4_init_opendata_res(opendata);
1056 ret = _nfs4_proc_open(opendata);
1059 newstate = nfs4_opendata_to_nfs4_state(opendata);
1060 if (IS_ERR(newstate))
1061 return PTR_ERR(newstate);
1062 nfs4_close_state(&opendata->path, newstate, fmode);
1067 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1069 struct nfs4_state *newstate;
1072 /* memory barrier prior to reading state->n_* */
1073 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1075 if (state->n_rdwr != 0) {
1076 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1079 if (newstate != state)
1082 if (state->n_wronly != 0) {
1083 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1086 if (newstate != state)
1089 if (state->n_rdonly != 0) {
1090 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1093 if (newstate != state)
1097 * We may have performed cached opens for all three recoveries.
1098 * Check if we need to update the current stateid.
1100 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1101 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1102 write_seqlock(&state->seqlock);
1103 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1104 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1105 write_sequnlock(&state->seqlock);
1112 * reclaim state on the server after a reboot.
1114 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1116 struct nfs_delegation *delegation;
1117 struct nfs4_opendata *opendata;
1118 fmode_t delegation_type = 0;
1121 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1122 if (IS_ERR(opendata))
1123 return PTR_ERR(opendata);
1124 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1125 opendata->o_arg.fh = NFS_FH(state->inode);
1127 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1128 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1129 delegation_type = delegation->type;
1131 opendata->o_arg.u.delegation_type = delegation_type;
1132 status = nfs4_open_recover(opendata, state);
1133 nfs4_opendata_put(opendata);
1137 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1139 struct nfs_server *server = NFS_SERVER(state->inode);
1140 struct nfs4_exception exception = { };
1143 err = _nfs4_do_open_reclaim(ctx, state);
1144 if (err != -NFS4ERR_DELAY)
1146 nfs4_handle_exception(server, err, &exception);
1147 } while (exception.retry);
1151 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1153 struct nfs_open_context *ctx;
1156 ctx = nfs4_state_find_open_context(state);
1158 return PTR_ERR(ctx);
1159 ret = nfs4_do_open_reclaim(ctx, state);
1160 put_nfs_open_context(ctx);
1164 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1166 struct nfs4_opendata *opendata;
1169 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1170 if (IS_ERR(opendata))
1171 return PTR_ERR(opendata);
1172 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1173 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1174 sizeof(opendata->o_arg.u.delegation.data));
1175 ret = nfs4_open_recover(opendata, state);
1176 nfs4_opendata_put(opendata);
1180 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1182 struct nfs4_exception exception = { };
1183 struct nfs_server *server = NFS_SERVER(state->inode);
1186 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1192 case -NFS4ERR_STALE_CLIENTID:
1193 case -NFS4ERR_STALE_STATEID:
1194 case -NFS4ERR_EXPIRED:
1195 /* Don't recall a delegation if it was lost */
1196 nfs4_schedule_state_recovery(server->nfs_client);
1200 * The show must go on: exit, but mark the
1201 * stateid as needing recovery.
1203 case -NFS4ERR_ADMIN_REVOKED:
1204 case -NFS4ERR_BAD_STATEID:
1205 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1210 err = nfs4_handle_exception(server, err, &exception);
1211 } while (exception.retry);
1216 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1218 struct nfs4_opendata *data = calldata;
1220 data->rpc_status = task->tk_status;
1221 if (RPC_ASSASSINATED(task))
1223 if (data->rpc_status == 0) {
1224 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1225 sizeof(data->o_res.stateid.data));
1226 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1227 renew_lease(data->o_res.server, data->timestamp);
1232 static void nfs4_open_confirm_release(void *calldata)
1234 struct nfs4_opendata *data = calldata;
1235 struct nfs4_state *state = NULL;
1237 /* If this request hasn't been cancelled, do nothing */
1238 if (data->cancelled == 0)
1240 /* In case of error, no cleanup! */
1241 if (!data->rpc_done)
1243 state = nfs4_opendata_to_nfs4_state(data);
1245 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1247 nfs4_opendata_put(data);
1250 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1251 .rpc_call_done = nfs4_open_confirm_done,
1252 .rpc_release = nfs4_open_confirm_release,
1256 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1258 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1260 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1261 struct rpc_task *task;
1262 struct rpc_message msg = {
1263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1264 .rpc_argp = &data->c_arg,
1265 .rpc_resp = &data->c_res,
1266 .rpc_cred = data->owner->so_cred,
1268 struct rpc_task_setup task_setup_data = {
1269 .rpc_client = server->client,
1270 .rpc_message = &msg,
1271 .callback_ops = &nfs4_open_confirm_ops,
1272 .callback_data = data,
1273 .workqueue = nfsiod_workqueue,
1274 .flags = RPC_TASK_ASYNC,
1278 kref_get(&data->kref);
1280 data->rpc_status = 0;
1281 data->timestamp = jiffies;
1282 task = rpc_run_task(&task_setup_data);
1284 return PTR_ERR(task);
1285 status = nfs4_wait_for_completion_rpc_task(task);
1287 data->cancelled = 1;
1290 status = data->rpc_status;
1295 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1297 struct nfs4_opendata *data = calldata;
1298 struct nfs4_state_owner *sp = data->owner;
1300 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1303 * Check if we still need to send an OPEN call, or if we can use
1304 * a delegation instead.
1306 if (data->state != NULL) {
1307 struct nfs_delegation *delegation;
1309 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1312 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1313 if (delegation != NULL &&
1314 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1320 /* Update sequence id. */
1321 data->o_arg.id = sp->so_owner_id.id;
1322 data->o_arg.clientid = sp->so_client->cl_clientid;
1323 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1324 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1325 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1327 data->timestamp = jiffies;
1328 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1329 &data->o_arg.seq_args,
1330 &data->o_res.seq_res, 1, task))
1332 rpc_call_start(task);
1335 task->tk_action = NULL;
1339 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1341 struct nfs4_opendata *data = calldata;
1343 data->rpc_status = task->tk_status;
1345 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1348 if (RPC_ASSASSINATED(task))
1350 if (task->tk_status == 0) {
1351 switch (data->o_res.f_attr->mode & S_IFMT) {
1355 data->rpc_status = -ELOOP;
1358 data->rpc_status = -EISDIR;
1361 data->rpc_status = -ENOTDIR;
1363 renew_lease(data->o_res.server, data->timestamp);
1364 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1365 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1370 static void nfs4_open_release(void *calldata)
1372 struct nfs4_opendata *data = calldata;
1373 struct nfs4_state *state = NULL;
1375 /* If this request hasn't been cancelled, do nothing */
1376 if (data->cancelled == 0)
1378 /* In case of error, no cleanup! */
1379 if (data->rpc_status != 0 || !data->rpc_done)
1381 /* In case we need an open_confirm, no cleanup! */
1382 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1384 state = nfs4_opendata_to_nfs4_state(data);
1386 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1388 nfs4_opendata_put(data);
1391 static const struct rpc_call_ops nfs4_open_ops = {
1392 .rpc_call_prepare = nfs4_open_prepare,
1393 .rpc_call_done = nfs4_open_done,
1394 .rpc_release = nfs4_open_release,
1398 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1400 static int _nfs4_proc_open(struct nfs4_opendata *data)
1402 struct inode *dir = data->dir->d_inode;
1403 struct nfs_server *server = NFS_SERVER(dir);
1404 struct nfs_openargs *o_arg = &data->o_arg;
1405 struct nfs_openres *o_res = &data->o_res;
1406 struct rpc_task *task;
1407 struct rpc_message msg = {
1408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1411 .rpc_cred = data->owner->so_cred,
1413 struct rpc_task_setup task_setup_data = {
1414 .rpc_client = server->client,
1415 .rpc_message = &msg,
1416 .callback_ops = &nfs4_open_ops,
1417 .callback_data = data,
1418 .workqueue = nfsiod_workqueue,
1419 .flags = RPC_TASK_ASYNC,
1423 kref_get(&data->kref);
1425 data->rpc_status = 0;
1426 data->cancelled = 0;
1427 task = rpc_run_task(&task_setup_data);
1429 return PTR_ERR(task);
1430 status = nfs4_wait_for_completion_rpc_task(task);
1432 data->cancelled = 1;
1435 status = data->rpc_status;
1437 if (status != 0 || !data->rpc_done)
1440 if (o_res->fh.size == 0)
1441 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1443 if (o_arg->open_flags & O_CREAT) {
1444 update_changeattr(dir, &o_res->cinfo);
1445 nfs_post_op_update_inode(dir, o_res->dir_attr);
1447 nfs_refresh_inode(dir, o_res->dir_attr);
1448 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1449 status = _nfs4_proc_open_confirm(data);
1453 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1454 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1458 static int nfs4_recover_expired_lease(struct nfs_server *server)
1460 struct nfs_client *clp = server->nfs_client;
1464 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1465 ret = nfs4_wait_clnt_recover(clp);
1468 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1469 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1471 nfs4_schedule_state_recovery(clp);
1479 * reclaim state on the server after a network partition.
1480 * Assumes caller holds the appropriate lock
1482 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1484 struct nfs4_opendata *opendata;
1487 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1488 if (IS_ERR(opendata))
1489 return PTR_ERR(opendata);
1490 ret = nfs4_open_recover(opendata, state);
1492 d_drop(ctx->path.dentry);
1493 nfs4_opendata_put(opendata);
1497 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1499 struct nfs_server *server = NFS_SERVER(state->inode);
1500 struct nfs4_exception exception = { };
1504 err = _nfs4_open_expired(ctx, state);
1508 case -NFS4ERR_GRACE:
1509 case -NFS4ERR_DELAY:
1510 nfs4_handle_exception(server, err, &exception);
1513 } while (exception.retry);
1518 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1520 struct nfs_open_context *ctx;
1523 ctx = nfs4_state_find_open_context(state);
1525 return PTR_ERR(ctx);
1526 ret = nfs4_do_open_expired(ctx, state);
1527 put_nfs_open_context(ctx);
1532 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1533 * fields corresponding to attributes that were used to store the verifier.
1534 * Make sure we clobber those fields in the later setattr call
1536 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1538 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1539 !(sattr->ia_valid & ATTR_ATIME_SET))
1540 sattr->ia_valid |= ATTR_ATIME;
1542 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1543 !(sattr->ia_valid & ATTR_MTIME_SET))
1544 sattr->ia_valid |= ATTR_MTIME;
1548 * Returns a referenced nfs4_state
1550 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)
1552 struct nfs4_state_owner *sp;
1553 struct nfs4_state *state = NULL;
1554 struct nfs_server *server = NFS_SERVER(dir);
1555 struct nfs4_opendata *opendata;
1558 /* Protect against reboot recovery conflicts */
1560 if (!(sp = nfs4_get_state_owner(server, cred))) {
1561 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1564 status = nfs4_recover_expired_lease(server);
1566 goto err_put_state_owner;
1567 if (path->dentry->d_inode != NULL)
1568 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1570 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1571 if (opendata == NULL)
1572 goto err_put_state_owner;
1574 if (path->dentry->d_inode != NULL)
1575 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1577 status = _nfs4_proc_open(opendata);
1579 goto err_opendata_put;
1581 if (opendata->o_arg.open_flags & O_EXCL)
1582 nfs4_exclusive_attrset(opendata, sattr);
1584 state = nfs4_opendata_to_nfs4_state(opendata);
1585 status = PTR_ERR(state);
1587 goto err_opendata_put;
1588 nfs4_opendata_put(opendata);
1589 nfs4_put_state_owner(sp);
1593 nfs4_opendata_put(opendata);
1594 err_put_state_owner:
1595 nfs4_put_state_owner(sp);
1602 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)
1604 struct nfs4_exception exception = { };
1605 struct nfs4_state *res;
1609 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1612 /* NOTE: BAD_SEQID means the server and client disagree about the
1613 * book-keeping w.r.t. state-changing operations
1614 * (OPEN/CLOSE/LOCK/LOCKU...)
1615 * It is actually a sign of a bug on the client or on the server.
1617 * If we receive a BAD_SEQID error in the particular case of
1618 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1619 * have unhashed the old state_owner for us, and that we can
1620 * therefore safely retry using a new one. We should still warn
1621 * the user though...
1623 if (status == -NFS4ERR_BAD_SEQID) {
1624 printk(KERN_WARNING "NFS: v4 server %s "
1625 " returned a bad sequence-id error!\n",
1626 NFS_SERVER(dir)->nfs_client->cl_hostname);
1627 exception.retry = 1;
1631 * BAD_STATEID on OPEN means that the server cancelled our
1632 * state before it received the OPEN_CONFIRM.
1633 * Recover by retrying the request as per the discussion
1634 * on Page 181 of RFC3530.
1636 if (status == -NFS4ERR_BAD_STATEID) {
1637 exception.retry = 1;
1640 if (status == -EAGAIN) {
1641 /* We must have found a delegation */
1642 exception.retry = 1;
1645 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1646 status, &exception));
1647 } while (exception.retry);
1651 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1652 struct nfs_fattr *fattr, struct iattr *sattr,
1653 struct nfs4_state *state)
1655 struct nfs_server *server = NFS_SERVER(inode);
1656 struct nfs_setattrargs arg = {
1657 .fh = NFS_FH(inode),
1660 .bitmask = server->attr_bitmask,
1662 struct nfs_setattrres res = {
1666 struct rpc_message msg = {
1667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1672 unsigned long timestamp = jiffies;
1675 nfs_fattr_init(fattr);
1677 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1678 /* Use that stateid */
1679 } else if (state != NULL) {
1680 nfs4_copy_stateid(&arg.stateid, state, current->files);
1682 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1684 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1685 if (status == 0 && state != NULL)
1686 renew_lease(server, timestamp);
1690 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1691 struct nfs_fattr *fattr, struct iattr *sattr,
1692 struct nfs4_state *state)
1694 struct nfs_server *server = NFS_SERVER(inode);
1695 struct nfs4_exception exception = { };
1698 err = nfs4_handle_exception(server,
1699 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1701 } while (exception.retry);
1705 struct nfs4_closedata {
1707 struct inode *inode;
1708 struct nfs4_state *state;
1709 struct nfs_closeargs arg;
1710 struct nfs_closeres res;
1711 struct nfs_fattr fattr;
1712 unsigned long timestamp;
1715 static void nfs4_free_closedata(void *data)
1717 struct nfs4_closedata *calldata = data;
1718 struct nfs4_state_owner *sp = calldata->state->owner;
1720 nfs4_put_open_state(calldata->state);
1721 nfs_free_seqid(calldata->arg.seqid);
1722 nfs4_put_state_owner(sp);
1723 path_put(&calldata->path);
1727 static void nfs4_close_done(struct rpc_task *task, void *data)
1729 struct nfs4_closedata *calldata = data;
1730 struct nfs4_state *state = calldata->state;
1731 struct nfs_server *server = NFS_SERVER(calldata->inode);
1733 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1734 if (RPC_ASSASSINATED(task))
1736 /* hmm. we are done with the inode, and in the process of freeing
1737 * the state_owner. we keep this around to process errors
1739 switch (task->tk_status) {
1741 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1742 renew_lease(server, calldata->timestamp);
1744 case -NFS4ERR_STALE_STATEID:
1745 case -NFS4ERR_OLD_STATEID:
1746 case -NFS4ERR_BAD_STATEID:
1747 case -NFS4ERR_EXPIRED:
1748 if (calldata->arg.fmode == 0)
1751 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1752 nfs4_restart_rpc(task, server->nfs_client,
1753 &calldata->res.seq_res);
1757 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
1758 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1761 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1763 struct nfs4_closedata *calldata = data;
1764 struct nfs4_state *state = calldata->state;
1765 int clear_rd, clear_wr, clear_rdwr;
1767 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1770 clear_rd = clear_wr = clear_rdwr = 0;
1771 spin_lock(&state->owner->so_lock);
1772 /* Calculate the change in open mode */
1773 if (state->n_rdwr == 0) {
1774 if (state->n_rdonly == 0) {
1775 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1776 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1778 if (state->n_wronly == 0) {
1779 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1780 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1783 spin_unlock(&state->owner->so_lock);
1784 if (!clear_rd && !clear_wr && !clear_rdwr) {
1785 /* Note: exit _without_ calling nfs4_close_done */
1786 task->tk_action = NULL;
1789 nfs_fattr_init(calldata->res.fattr);
1790 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1791 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1792 calldata->arg.fmode = FMODE_READ;
1793 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1794 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1795 calldata->arg.fmode = FMODE_WRITE;
1797 calldata->timestamp = jiffies;
1798 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1799 &calldata->arg.seq_args, &calldata->res.seq_res,
1802 rpc_call_start(task);
1805 static const struct rpc_call_ops nfs4_close_ops = {
1806 .rpc_call_prepare = nfs4_close_prepare,
1807 .rpc_call_done = nfs4_close_done,
1808 .rpc_release = nfs4_free_closedata,
1812 * It is possible for data to be read/written from a mem-mapped file
1813 * after the sys_close call (which hits the vfs layer as a flush).
1814 * This means that we can't safely call nfsv4 close on a file until
1815 * the inode is cleared. This in turn means that we are not good
1816 * NFSv4 citizens - we do not indicate to the server to update the file's
1817 * share state even when we are done with one of the three share
1818 * stateid's in the inode.
1820 * NOTE: Caller must be holding the sp->so_owner semaphore!
1822 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1824 struct nfs_server *server = NFS_SERVER(state->inode);
1825 struct nfs4_closedata *calldata;
1826 struct nfs4_state_owner *sp = state->owner;
1827 struct rpc_task *task;
1828 struct rpc_message msg = {
1829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1830 .rpc_cred = state->owner->so_cred,
1832 struct rpc_task_setup task_setup_data = {
1833 .rpc_client = server->client,
1834 .rpc_message = &msg,
1835 .callback_ops = &nfs4_close_ops,
1836 .workqueue = nfsiod_workqueue,
1837 .flags = RPC_TASK_ASYNC,
1839 int status = -ENOMEM;
1841 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1842 if (calldata == NULL)
1844 calldata->inode = state->inode;
1845 calldata->state = state;
1846 calldata->arg.fh = NFS_FH(state->inode);
1847 calldata->arg.stateid = &state->open_stateid;
1848 if (nfs4_has_session(server->nfs_client))
1849 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
1850 /* Serialization for the sequence id */
1851 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1852 if (calldata->arg.seqid == NULL)
1853 goto out_free_calldata;
1854 calldata->arg.fmode = 0;
1855 calldata->arg.bitmask = server->cache_consistency_bitmask;
1856 calldata->res.fattr = &calldata->fattr;
1857 calldata->res.seqid = calldata->arg.seqid;
1858 calldata->res.server = server;
1859 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1860 calldata->path.mnt = mntget(path->mnt);
1861 calldata->path.dentry = dget(path->dentry);
1863 msg.rpc_argp = &calldata->arg,
1864 msg.rpc_resp = &calldata->res,
1865 task_setup_data.callback_data = calldata;
1866 task = rpc_run_task(&task_setup_data);
1868 return PTR_ERR(task);
1871 status = rpc_wait_for_completion_task(task);
1877 nfs4_put_open_state(state);
1878 nfs4_put_state_owner(sp);
1882 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1887 /* If the open_intent is for execute, we have an extra check to make */
1888 if (fmode & FMODE_EXEC) {
1889 ret = nfs_may_open(state->inode,
1890 state->owner->so_cred,
1891 nd->intent.open.flags);
1895 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1896 if (!IS_ERR(filp)) {
1897 struct nfs_open_context *ctx;
1898 ctx = nfs_file_open_context(filp);
1902 ret = PTR_ERR(filp);
1904 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1909 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1911 struct path path = {
1912 .mnt = nd->path.mnt,
1915 struct dentry *parent;
1917 struct rpc_cred *cred;
1918 struct nfs4_state *state;
1920 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1922 if (nd->flags & LOOKUP_CREATE) {
1923 attr.ia_mode = nd->intent.open.create_mode;
1924 attr.ia_valid = ATTR_MODE;
1925 if (!IS_POSIXACL(dir))
1926 attr.ia_mode &= ~current_umask();
1929 BUG_ON(nd->intent.open.flags & O_CREAT);
1932 cred = rpc_lookup_cred();
1934 return (struct dentry *)cred;
1935 parent = dentry->d_parent;
1936 /* Protect against concurrent sillydeletes */
1937 nfs_block_sillyrename(parent);
1938 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1940 if (IS_ERR(state)) {
1941 if (PTR_ERR(state) == -ENOENT) {
1942 d_add(dentry, NULL);
1943 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1945 nfs_unblock_sillyrename(parent);
1946 return (struct dentry *)state;
1948 res = d_add_unique(dentry, igrab(state->inode));
1951 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1952 nfs_unblock_sillyrename(parent);
1953 nfs4_intent_set_file(nd, &path, state, fmode);
1958 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1960 struct path path = {
1961 .mnt = nd->path.mnt,
1964 struct rpc_cred *cred;
1965 struct nfs4_state *state;
1966 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1968 cred = rpc_lookup_cred();
1970 return PTR_ERR(cred);
1971 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1973 if (IS_ERR(state)) {
1974 switch (PTR_ERR(state)) {
1980 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1986 if (state->inode == dentry->d_inode) {
1987 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1988 nfs4_intent_set_file(nd, &path, state, fmode);
1991 nfs4_close_sync(&path, state, fmode);
1997 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1999 if (ctx->state == NULL)
2002 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2004 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2007 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2009 struct nfs4_server_caps_arg args = {
2012 struct nfs4_server_caps_res res = {};
2013 struct rpc_message msg = {
2014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2020 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2022 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2023 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2024 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2025 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2026 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2027 NFS_CAP_CTIME|NFS_CAP_MTIME);
2028 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2029 server->caps |= NFS_CAP_ACLS;
2030 if (res.has_links != 0)
2031 server->caps |= NFS_CAP_HARDLINKS;
2032 if (res.has_symlinks != 0)
2033 server->caps |= NFS_CAP_SYMLINKS;
2034 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2035 server->caps |= NFS_CAP_FILEID;
2036 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2037 server->caps |= NFS_CAP_MODE;
2038 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2039 server->caps |= NFS_CAP_NLINK;
2040 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2041 server->caps |= NFS_CAP_OWNER;
2042 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2043 server->caps |= NFS_CAP_OWNER_GROUP;
2044 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2045 server->caps |= NFS_CAP_ATIME;
2046 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2047 server->caps |= NFS_CAP_CTIME;
2048 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2049 server->caps |= NFS_CAP_MTIME;
2051 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2052 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2053 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2054 server->acl_bitmask = res.acl_bitmask;
2060 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2062 struct nfs4_exception exception = { };
2065 err = nfs4_handle_exception(server,
2066 _nfs4_server_capabilities(server, fhandle),
2068 } while (exception.retry);
2072 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2073 struct nfs_fsinfo *info)
2075 struct nfs4_lookup_root_arg args = {
2076 .bitmask = nfs4_fattr_bitmap,
2078 struct nfs4_lookup_res res = {
2080 .fattr = info->fattr,
2083 struct rpc_message msg = {
2084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2089 nfs_fattr_init(info->fattr);
2090 return nfs4_call_sync(server, &msg, &args, &res, 0);
2093 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2094 struct nfs_fsinfo *info)
2096 struct nfs4_exception exception = { };
2099 err = nfs4_handle_exception(server,
2100 _nfs4_lookup_root(server, fhandle, info),
2102 } while (exception.retry);
2107 * get the file handle for the "/" directory on the server
2109 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2110 struct nfs_fsinfo *info)
2114 status = nfs4_lookup_root(server, fhandle, info);
2116 status = nfs4_server_capabilities(server, fhandle);
2118 status = nfs4_do_fsinfo(server, fhandle, info);
2119 return nfs4_map_errors(status);
2123 * Get locations and (maybe) other attributes of a referral.
2124 * Note that we'll actually follow the referral later when
2125 * we detect fsid mismatch in inode revalidation
2127 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2129 int status = -ENOMEM;
2130 struct page *page = NULL;
2131 struct nfs4_fs_locations *locations = NULL;
2133 page = alloc_page(GFP_KERNEL);
2136 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2137 if (locations == NULL)
2140 status = nfs4_proc_fs_locations(dir, name, locations, page);
2143 /* Make sure server returned a different fsid for the referral */
2144 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2145 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2150 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2151 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2153 fattr->mode = S_IFDIR;
2154 memset(fhandle, 0, sizeof(struct nfs_fh));
2163 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2165 struct nfs4_getattr_arg args = {
2167 .bitmask = server->attr_bitmask,
2169 struct nfs4_getattr_res res = {
2173 struct rpc_message msg = {
2174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2179 nfs_fattr_init(fattr);
2180 return nfs4_call_sync(server, &msg, &args, &res, 0);
2183 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2185 struct nfs4_exception exception = { };
2188 err = nfs4_handle_exception(server,
2189 _nfs4_proc_getattr(server, fhandle, fattr),
2191 } while (exception.retry);
2196 * The file is not closed if it is opened due to the a request to change
2197 * the size of the file. The open call will not be needed once the
2198 * VFS layer lookup-intents are implemented.
2200 * Close is called when the inode is destroyed.
2201 * If we haven't opened the file for O_WRONLY, we
2202 * need to in the size_change case to obtain a stateid.
2205 * Because OPEN is always done by name in nfsv4, it is
2206 * possible that we opened a different file by the same
2207 * name. We can recognize this race condition, but we
2208 * can't do anything about it besides returning an error.
2210 * This will be fixed with VFS changes (lookup-intent).
2213 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2214 struct iattr *sattr)
2216 struct inode *inode = dentry->d_inode;
2217 struct rpc_cred *cred = NULL;
2218 struct nfs4_state *state = NULL;
2221 nfs_fattr_init(fattr);
2223 /* Search for an existing open(O_WRITE) file */
2224 if (sattr->ia_valid & ATTR_FILE) {
2225 struct nfs_open_context *ctx;
2227 ctx = nfs_file_open_context(sattr->ia_file);
2234 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2236 nfs_setattr_update_inode(inode, sattr);
2240 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2241 const struct qstr *name, struct nfs_fh *fhandle,
2242 struct nfs_fattr *fattr)
2245 struct nfs4_lookup_arg args = {
2246 .bitmask = server->attr_bitmask,
2250 struct nfs4_lookup_res res = {
2255 struct rpc_message msg = {
2256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2261 nfs_fattr_init(fattr);
2263 dprintk("NFS call lookupfh %s\n", name->name);
2264 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2265 dprintk("NFS reply lookupfh: %d\n", status);
2269 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2270 struct qstr *name, struct nfs_fh *fhandle,
2271 struct nfs_fattr *fattr)
2273 struct nfs4_exception exception = { };
2276 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2278 if (err == -NFS4ERR_MOVED) {
2282 err = nfs4_handle_exception(server, err, &exception);
2283 } while (exception.retry);
2287 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2288 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2292 dprintk("NFS call lookup %s\n", name->name);
2293 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2294 if (status == -NFS4ERR_MOVED)
2295 status = nfs4_get_referral(dir, name, fattr, fhandle);
2296 dprintk("NFS reply lookup: %d\n", status);
2300 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2302 struct nfs4_exception exception = { };
2305 err = nfs4_handle_exception(NFS_SERVER(dir),
2306 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2308 } while (exception.retry);
2312 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2314 struct nfs_server *server = NFS_SERVER(inode);
2315 struct nfs_fattr fattr;
2316 struct nfs4_accessargs args = {
2317 .fh = NFS_FH(inode),
2318 .bitmask = server->attr_bitmask,
2320 struct nfs4_accessres res = {
2324 struct rpc_message msg = {
2325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2328 .rpc_cred = entry->cred,
2330 int mode = entry->mask;
2334 * Determine which access bits we want to ask for...
2336 if (mode & MAY_READ)
2337 args.access |= NFS4_ACCESS_READ;
2338 if (S_ISDIR(inode->i_mode)) {
2339 if (mode & MAY_WRITE)
2340 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2341 if (mode & MAY_EXEC)
2342 args.access |= NFS4_ACCESS_LOOKUP;
2344 if (mode & MAY_WRITE)
2345 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2346 if (mode & MAY_EXEC)
2347 args.access |= NFS4_ACCESS_EXECUTE;
2349 nfs_fattr_init(&fattr);
2350 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2353 if (res.access & NFS4_ACCESS_READ)
2354 entry->mask |= MAY_READ;
2355 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2356 entry->mask |= MAY_WRITE;
2357 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2358 entry->mask |= MAY_EXEC;
2359 nfs_refresh_inode(inode, &fattr);
2364 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2366 struct nfs4_exception exception = { };
2369 err = nfs4_handle_exception(NFS_SERVER(inode),
2370 _nfs4_proc_access(inode, entry),
2372 } while (exception.retry);
2377 * TODO: For the time being, we don't try to get any attributes
2378 * along with any of the zero-copy operations READ, READDIR,
2381 * In the case of the first three, we want to put the GETATTR
2382 * after the read-type operation -- this is because it is hard
2383 * to predict the length of a GETATTR response in v4, and thus
2384 * align the READ data correctly. This means that the GETATTR
2385 * may end up partially falling into the page cache, and we should
2386 * shift it into the 'tail' of the xdr_buf before processing.
2387 * To do this efficiently, we need to know the total length
2388 * of data received, which doesn't seem to be available outside
2391 * In the case of WRITE, we also want to put the GETATTR after
2392 * the operation -- in this case because we want to make sure
2393 * we get the post-operation mtime and size. This means that
2394 * we can't use xdr_encode_pages() as written: we need a variant
2395 * of it which would leave room in the 'tail' iovec.
2397 * Both of these changes to the XDR layer would in fact be quite
2398 * minor, but I decided to leave them for a subsequent patch.
2400 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2401 unsigned int pgbase, unsigned int pglen)
2403 struct nfs4_readlink args = {
2404 .fh = NFS_FH(inode),
2409 struct nfs4_readlink_res res;
2410 struct rpc_message msg = {
2411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2416 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2419 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2420 unsigned int pgbase, unsigned int pglen)
2422 struct nfs4_exception exception = { };
2425 err = nfs4_handle_exception(NFS_SERVER(inode),
2426 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2428 } while (exception.retry);
2434 * We will need to arrange for the VFS layer to provide an atomic open.
2435 * Until then, this create/open method is prone to inefficiency and race
2436 * conditions due to the lookup, create, and open VFS calls from sys_open()
2437 * placed on the wire.
2439 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2440 * The file will be opened again in the subsequent VFS open call
2441 * (nfs4_proc_file_open).
2443 * The open for read will just hang around to be used by any process that
2444 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2448 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2449 int flags, struct nameidata *nd)
2451 struct path path = {
2452 .mnt = nd->path.mnt,
2455 struct nfs4_state *state;
2456 struct rpc_cred *cred;
2457 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2460 cred = rpc_lookup_cred();
2462 status = PTR_ERR(cred);
2465 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2467 if (IS_ERR(state)) {
2468 status = PTR_ERR(state);
2471 d_add(dentry, igrab(state->inode));
2472 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2473 if (flags & O_EXCL) {
2474 struct nfs_fattr fattr;
2475 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2477 nfs_setattr_update_inode(state->inode, sattr);
2478 nfs_post_op_update_inode(state->inode, &fattr);
2480 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2481 status = nfs4_intent_set_file(nd, &path, state, fmode);
2483 nfs4_close_sync(&path, state, fmode);
2490 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2492 struct nfs_server *server = NFS_SERVER(dir);
2493 struct nfs_removeargs args = {
2495 .name.len = name->len,
2496 .name.name = name->name,
2497 .bitmask = server->attr_bitmask,
2499 struct nfs_removeres res = {
2502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2509 nfs_fattr_init(&res.dir_attr);
2510 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2512 update_changeattr(dir, &res.cinfo);
2513 nfs_post_op_update_inode(dir, &res.dir_attr);
2518 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2520 struct nfs4_exception exception = { };
2523 err = nfs4_handle_exception(NFS_SERVER(dir),
2524 _nfs4_proc_remove(dir, name),
2526 } while (exception.retry);
2530 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2532 struct nfs_server *server = NFS_SERVER(dir);
2533 struct nfs_removeargs *args = msg->rpc_argp;
2534 struct nfs_removeres *res = msg->rpc_resp;
2536 args->bitmask = server->cache_consistency_bitmask;
2537 res->server = server;
2538 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2541 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2543 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2545 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2546 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2548 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
2549 update_changeattr(dir, &res->cinfo);
2550 nfs_post_op_update_inode(dir, &res->dir_attr);
2554 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2555 struct inode *new_dir, struct qstr *new_name)
2557 struct nfs_server *server = NFS_SERVER(old_dir);
2558 struct nfs4_rename_arg arg = {
2559 .old_dir = NFS_FH(old_dir),
2560 .new_dir = NFS_FH(new_dir),
2561 .old_name = old_name,
2562 .new_name = new_name,
2563 .bitmask = server->attr_bitmask,
2565 struct nfs_fattr old_fattr, new_fattr;
2566 struct nfs4_rename_res res = {
2568 .old_fattr = &old_fattr,
2569 .new_fattr = &new_fattr,
2571 struct rpc_message msg = {
2572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2578 nfs_fattr_init(res.old_fattr);
2579 nfs_fattr_init(res.new_fattr);
2580 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2583 update_changeattr(old_dir, &res.old_cinfo);
2584 nfs_post_op_update_inode(old_dir, res.old_fattr);
2585 update_changeattr(new_dir, &res.new_cinfo);
2586 nfs_post_op_update_inode(new_dir, res.new_fattr);
2591 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2592 struct inode *new_dir, struct qstr *new_name)
2594 struct nfs4_exception exception = { };
2597 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2598 _nfs4_proc_rename(old_dir, old_name,
2601 } while (exception.retry);
2605 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2607 struct nfs_server *server = NFS_SERVER(inode);
2608 struct nfs4_link_arg arg = {
2609 .fh = NFS_FH(inode),
2610 .dir_fh = NFS_FH(dir),
2612 .bitmask = server->attr_bitmask,
2614 struct nfs_fattr fattr, dir_attr;
2615 struct nfs4_link_res res = {
2618 .dir_attr = &dir_attr,
2620 struct rpc_message msg = {
2621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2627 nfs_fattr_init(res.fattr);
2628 nfs_fattr_init(res.dir_attr);
2629 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2631 update_changeattr(dir, &res.cinfo);
2632 nfs_post_op_update_inode(dir, res.dir_attr);
2633 nfs_post_op_update_inode(inode, res.fattr);
2639 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2641 struct nfs4_exception exception = { };
2644 err = nfs4_handle_exception(NFS_SERVER(inode),
2645 _nfs4_proc_link(inode, dir, name),
2647 } while (exception.retry);
2651 struct nfs4_createdata {
2652 struct rpc_message msg;
2653 struct nfs4_create_arg arg;
2654 struct nfs4_create_res res;
2656 struct nfs_fattr fattr;
2657 struct nfs_fattr dir_fattr;
2660 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2661 struct qstr *name, struct iattr *sattr, u32 ftype)
2663 struct nfs4_createdata *data;
2665 data = kzalloc(sizeof(*data), GFP_KERNEL);
2667 struct nfs_server *server = NFS_SERVER(dir);
2669 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2670 data->msg.rpc_argp = &data->arg;
2671 data->msg.rpc_resp = &data->res;
2672 data->arg.dir_fh = NFS_FH(dir);
2673 data->arg.server = server;
2674 data->arg.name = name;
2675 data->arg.attrs = sattr;
2676 data->arg.ftype = ftype;
2677 data->arg.bitmask = server->attr_bitmask;
2678 data->res.server = server;
2679 data->res.fh = &data->fh;
2680 data->res.fattr = &data->fattr;
2681 data->res.dir_fattr = &data->dir_fattr;
2682 nfs_fattr_init(data->res.fattr);
2683 nfs_fattr_init(data->res.dir_fattr);
2688 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2690 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2691 &data->arg, &data->res, 1);
2693 update_changeattr(dir, &data->res.dir_cinfo);
2694 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2695 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2700 static void nfs4_free_createdata(struct nfs4_createdata *data)
2705 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2706 struct page *page, unsigned int len, struct iattr *sattr)
2708 struct nfs4_createdata *data;
2709 int status = -ENAMETOOLONG;
2711 if (len > NFS4_MAXPATHLEN)
2715 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2719 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2720 data->arg.u.symlink.pages = &page;
2721 data->arg.u.symlink.len = len;
2723 status = nfs4_do_create(dir, dentry, data);
2725 nfs4_free_createdata(data);
2730 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2731 struct page *page, unsigned int len, struct iattr *sattr)
2733 struct nfs4_exception exception = { };
2736 err = nfs4_handle_exception(NFS_SERVER(dir),
2737 _nfs4_proc_symlink(dir, dentry, page,
2740 } while (exception.retry);
2744 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2745 struct iattr *sattr)
2747 struct nfs4_createdata *data;
2748 int status = -ENOMEM;
2750 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2754 status = nfs4_do_create(dir, dentry, data);
2756 nfs4_free_createdata(data);
2761 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2762 struct iattr *sattr)
2764 struct nfs4_exception exception = { };
2767 err = nfs4_handle_exception(NFS_SERVER(dir),
2768 _nfs4_proc_mkdir(dir, dentry, sattr),
2770 } while (exception.retry);
2774 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2775 u64 cookie, struct page *page, unsigned int count, int plus)
2777 struct inode *dir = dentry->d_inode;
2778 struct nfs4_readdir_arg args = {
2783 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2785 struct nfs4_readdir_res res;
2786 struct rpc_message msg = {
2787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2794 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2795 dentry->d_parent->d_name.name,
2796 dentry->d_name.name,
2797 (unsigned long long)cookie);
2798 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2799 res.pgbase = args.pgbase;
2800 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2802 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2804 nfs_invalidate_atime(dir);
2806 dprintk("%s: returns %d\n", __func__, status);
2810 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2811 u64 cookie, struct page *page, unsigned int count, int plus)
2813 struct nfs4_exception exception = { };
2816 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2817 _nfs4_proc_readdir(dentry, cred, cookie,
2820 } while (exception.retry);
2824 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2825 struct iattr *sattr, dev_t rdev)
2827 struct nfs4_createdata *data;
2828 int mode = sattr->ia_mode;
2829 int status = -ENOMEM;
2831 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2832 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2834 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2839 data->arg.ftype = NF4FIFO;
2840 else if (S_ISBLK(mode)) {
2841 data->arg.ftype = NF4BLK;
2842 data->arg.u.device.specdata1 = MAJOR(rdev);
2843 data->arg.u.device.specdata2 = MINOR(rdev);
2845 else if (S_ISCHR(mode)) {
2846 data->arg.ftype = NF4CHR;
2847 data->arg.u.device.specdata1 = MAJOR(rdev);
2848 data->arg.u.device.specdata2 = MINOR(rdev);
2851 status = nfs4_do_create(dir, dentry, data);
2853 nfs4_free_createdata(data);
2858 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2859 struct iattr *sattr, dev_t rdev)
2861 struct nfs4_exception exception = { };
2864 err = nfs4_handle_exception(NFS_SERVER(dir),
2865 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2867 } while (exception.retry);
2871 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2872 struct nfs_fsstat *fsstat)
2874 struct nfs4_statfs_arg args = {
2876 .bitmask = server->attr_bitmask,
2878 struct nfs4_statfs_res res = {
2881 struct rpc_message msg = {
2882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2887 nfs_fattr_init(fsstat->fattr);
2888 return nfs4_call_sync(server, &msg, &args, &res, 0);
2891 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2893 struct nfs4_exception exception = { };
2896 err = nfs4_handle_exception(server,
2897 _nfs4_proc_statfs(server, fhandle, fsstat),
2899 } while (exception.retry);
2903 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2904 struct nfs_fsinfo *fsinfo)
2906 struct nfs4_fsinfo_arg args = {
2908 .bitmask = server->attr_bitmask,
2910 struct nfs4_fsinfo_res res = {
2913 struct rpc_message msg = {
2914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2919 return nfs4_call_sync(server, &msg, &args, &res, 0);
2922 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2924 struct nfs4_exception exception = { };
2928 err = nfs4_handle_exception(server,
2929 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2931 } while (exception.retry);
2935 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2937 nfs_fattr_init(fsinfo->fattr);
2938 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2941 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2942 struct nfs_pathconf *pathconf)
2944 struct nfs4_pathconf_arg args = {
2946 .bitmask = server->attr_bitmask,
2948 struct nfs4_pathconf_res res = {
2949 .pathconf = pathconf,
2951 struct rpc_message msg = {
2952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2957 /* None of the pathconf attributes are mandatory to implement */
2958 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2959 memset(pathconf, 0, sizeof(*pathconf));
2963 nfs_fattr_init(pathconf->fattr);
2964 return nfs4_call_sync(server, &msg, &args, &res, 0);
2967 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2968 struct nfs_pathconf *pathconf)
2970 struct nfs4_exception exception = { };
2974 err = nfs4_handle_exception(server,
2975 _nfs4_proc_pathconf(server, fhandle, pathconf),
2977 } while (exception.retry);
2981 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2983 struct nfs_server *server = NFS_SERVER(data->inode);
2985 dprintk("--> %s\n", __func__);
2987 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2988 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2990 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2991 nfs4_restart_rpc(task, server->nfs_client, &data->res.seq_res);
2995 nfs_invalidate_atime(data->inode);
2996 if (task->tk_status > 0)
2997 renew_lease(server, data->timestamp);
2998 else if (task->tk_status < 0)
2999 nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res);
3004 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3006 data->timestamp = jiffies;
3007 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3010 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3012 struct inode *inode = data->inode;
3014 /* slot is freed in nfs_writeback_done */
3015 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3018 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3019 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3020 &data->res.seq_res);
3023 if (task->tk_status >= 0) {
3024 renew_lease(NFS_SERVER(inode), data->timestamp);
3025 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3030 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3032 struct nfs_server *server = NFS_SERVER(data->inode);
3034 data->args.bitmask = server->cache_consistency_bitmask;
3035 data->res.server = server;
3036 data->timestamp = jiffies;
3038 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3041 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3043 struct inode *inode = data->inode;
3045 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3047 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3048 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3049 &data->res.seq_res);
3052 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3053 &data->res.seq_res);
3054 nfs_refresh_inode(inode, data->res.fattr);
3058 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3060 struct nfs_server *server = NFS_SERVER(data->inode);
3062 data->args.bitmask = server->cache_consistency_bitmask;
3063 data->res.server = server;
3064 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3068 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3069 * standalone procedure for queueing an asynchronous RENEW.
3071 static void nfs4_renew_done(struct rpc_task *task, void *data)
3073 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
3074 unsigned long timestamp = (unsigned long)data;
3076 if (task->tk_status < 0) {
3077 /* Unless we're shutting down, schedule state recovery! */
3078 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3079 nfs4_schedule_state_recovery(clp);
3082 spin_lock(&clp->cl_lock);
3083 if (time_before(clp->cl_last_renewal,timestamp))
3084 clp->cl_last_renewal = timestamp;
3085 spin_unlock(&clp->cl_lock);
3088 static const struct rpc_call_ops nfs4_renew_ops = {
3089 .rpc_call_done = nfs4_renew_done,
3092 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3094 struct rpc_message msg = {
3095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3100 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3101 &nfs4_renew_ops, (void *)jiffies);
3104 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3106 struct rpc_message msg = {
3107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3111 unsigned long now = jiffies;
3114 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3117 spin_lock(&clp->cl_lock);
3118 if (time_before(clp->cl_last_renewal,now))
3119 clp->cl_last_renewal = now;
3120 spin_unlock(&clp->cl_lock);
3124 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3126 return (server->caps & NFS_CAP_ACLS)
3127 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3128 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3131 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3132 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3135 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3137 static void buf_to_pages(const void *buf, size_t buflen,
3138 struct page **pages, unsigned int *pgbase)
3140 const void *p = buf;
3142 *pgbase = offset_in_page(buf);
3144 while (p < buf + buflen) {
3145 *(pages++) = virt_to_page(p);
3146 p += PAGE_CACHE_SIZE;
3150 struct nfs4_cached_acl {
3156 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3158 struct nfs_inode *nfsi = NFS_I(inode);
3160 spin_lock(&inode->i_lock);
3161 kfree(nfsi->nfs4_acl);
3162 nfsi->nfs4_acl = acl;
3163 spin_unlock(&inode->i_lock);
3166 static void nfs4_zap_acl_attr(struct inode *inode)
3168 nfs4_set_cached_acl(inode, NULL);
3171 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3173 struct nfs_inode *nfsi = NFS_I(inode);
3174 struct nfs4_cached_acl *acl;
3177 spin_lock(&inode->i_lock);
3178 acl = nfsi->nfs4_acl;
3181 if (buf == NULL) /* user is just asking for length */
3183 if (acl->cached == 0)
3185 ret = -ERANGE; /* see getxattr(2) man page */
3186 if (acl->len > buflen)
3188 memcpy(buf, acl->data, acl->len);
3192 spin_unlock(&inode->i_lock);
3196 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3198 struct nfs4_cached_acl *acl;
3200 if (buf && acl_len <= PAGE_SIZE) {
3201 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3205 memcpy(acl->data, buf, acl_len);
3207 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3214 nfs4_set_cached_acl(inode, acl);
3217 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3219 struct page *pages[NFS4ACL_MAXPAGES];
3220 struct nfs_getaclargs args = {
3221 .fh = NFS_FH(inode),
3225 struct nfs_getaclres res = {
3229 struct rpc_message msg = {
3230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3234 struct page *localpage = NULL;
3237 if (buflen < PAGE_SIZE) {
3238 /* As long as we're doing a round trip to the server anyway,
3239 * let's be prepared for a page of acl data. */
3240 localpage = alloc_page(GFP_KERNEL);
3241 resp_buf = page_address(localpage);
3242 if (localpage == NULL)
3244 args.acl_pages[0] = localpage;
3245 args.acl_pgbase = 0;
3246 args.acl_len = PAGE_SIZE;
3249 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3251 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3254 if (res.acl_len > args.acl_len)
3255 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3257 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3260 if (res.acl_len > buflen)
3263 memcpy(buf, resp_buf, res.acl_len);
3268 __free_page(localpage);
3272 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3274 struct nfs4_exception exception = { };
3277 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3280 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3281 } while (exception.retry);
3285 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3287 struct nfs_server *server = NFS_SERVER(inode);
3290 if (!nfs4_server_supports_acls(server))
3292 ret = nfs_revalidate_inode(server, inode);
3295 ret = nfs4_read_cached_acl(inode, buf, buflen);
3298 return nfs4_get_acl_uncached(inode, buf, buflen);
3301 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3303 struct nfs_server *server = NFS_SERVER(inode);
3304 struct page *pages[NFS4ACL_MAXPAGES];
3305 struct nfs_setaclargs arg = {
3306 .fh = NFS_FH(inode),
3310 struct nfs_setaclres res;
3311 struct rpc_message msg = {
3312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3318 if (!nfs4_server_supports_acls(server))
3320 nfs_inode_return_delegation(inode);
3321 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3322 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3323 nfs_access_zap_cache(inode);
3324 nfs_zap_acl_cache(inode);
3328 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3330 struct nfs4_exception exception = { };
3333 err = nfs4_handle_exception(NFS_SERVER(inode),
3334 __nfs4_proc_set_acl(inode, buf, buflen),
3336 } while (exception.retry);
3341 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3343 if (!clp || task->tk_status >= 0)
3345 switch(task->tk_status) {
3346 case -NFS4ERR_ADMIN_REVOKED:
3347 case -NFS4ERR_BAD_STATEID:
3348 case -NFS4ERR_OPENMODE:
3351 nfs4_state_mark_reclaim_nograce(clp, state);
3352 case -NFS4ERR_STALE_CLIENTID:
3353 case -NFS4ERR_STALE_STATEID:
3354 case -NFS4ERR_EXPIRED:
3355 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3356 nfs4_schedule_state_recovery(clp);
3357 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3358 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3359 task->tk_status = 0;
3361 #if defined(CONFIG_NFS_V4_1)
3362 case -NFS4ERR_BADSESSION:
3363 case -NFS4ERR_BADSLOT:
3364 case -NFS4ERR_BAD_HIGH_SLOT:
3365 case -NFS4ERR_DEADSESSION:
3366 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3367 case -NFS4ERR_SEQ_FALSE_RETRY:
3368 case -NFS4ERR_SEQ_MISORDERED:
3369 dprintk("%s ERROR %d, Reset session\n", __func__,
3371 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
3372 task->tk_status = 0;
3374 #endif /* CONFIG_NFS_V4_1 */
3375 case -NFS4ERR_DELAY:
3377 nfs_inc_server_stats(server, NFSIOS_DELAY);
3378 case -NFS4ERR_GRACE:
3379 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3380 task->tk_status = 0;
3382 case -NFS4ERR_OLD_STATEID:
3383 task->tk_status = 0;
3386 task->tk_status = nfs4_map_errors(task->tk_status);
3391 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3393 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3396 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3398 nfs4_verifier sc_verifier;
3399 struct nfs4_setclientid setclientid = {
3400 .sc_verifier = &sc_verifier,
3403 struct rpc_message msg = {
3404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3405 .rpc_argp = &setclientid,
3413 p = (__be32*)sc_verifier.data;
3414 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3415 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3418 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3419 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3421 rpc_peeraddr2str(clp->cl_rpcclient,
3423 rpc_peeraddr2str(clp->cl_rpcclient,
3425 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3426 clp->cl_id_uniquifier);
3427 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3428 sizeof(setclientid.sc_netid),
3429 rpc_peeraddr2str(clp->cl_rpcclient,
3430 RPC_DISPLAY_NETID));
3431 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3432 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3433 clp->cl_ipaddr, port >> 8, port & 255);
3435 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3436 if (status != -NFS4ERR_CLID_INUSE)
3441 ssleep(clp->cl_lease_time + 1);
3443 if (++clp->cl_id_uniquifier == 0)
3449 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3451 struct nfs_fsinfo fsinfo;
3452 struct rpc_message msg = {
3453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3455 .rpc_resp = &fsinfo,
3462 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3464 spin_lock(&clp->cl_lock);
3465 clp->cl_lease_time = fsinfo.lease_time * HZ;
3466 clp->cl_last_renewal = now;
3467 spin_unlock(&clp->cl_lock);
3472 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3477 err = _nfs4_proc_setclientid_confirm(clp, cred);
3481 case -NFS4ERR_RESOURCE:
3482 /* The IBM lawyers misread another document! */
3483 case -NFS4ERR_DELAY:
3484 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3490 struct nfs4_delegreturndata {
3491 struct nfs4_delegreturnargs args;
3492 struct nfs4_delegreturnres res;
3494 nfs4_stateid stateid;
3495 unsigned long timestamp;
3496 struct nfs_fattr fattr;
3500 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3502 struct nfs4_delegreturndata *data = calldata;
3504 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3507 data->rpc_status = task->tk_status;
3508 if (data->rpc_status == 0)
3509 renew_lease(data->res.server, data->timestamp);
3512 static void nfs4_delegreturn_release(void *calldata)
3517 #if defined(CONFIG_NFS_V4_1)
3518 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3520 struct nfs4_delegreturndata *d_data;
3522 d_data = (struct nfs4_delegreturndata *)data;
3524 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3525 &d_data->args.seq_args,
3526 &d_data->res.seq_res, 1, task))
3528 rpc_call_start(task);
3530 #endif /* CONFIG_NFS_V4_1 */
3532 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3533 #if defined(CONFIG_NFS_V4_1)
3534 .rpc_call_prepare = nfs4_delegreturn_prepare,
3535 #endif /* CONFIG_NFS_V4_1 */
3536 .rpc_call_done = nfs4_delegreturn_done,
3537 .rpc_release = nfs4_delegreturn_release,
3540 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3542 struct nfs4_delegreturndata *data;
3543 struct nfs_server *server = NFS_SERVER(inode);
3544 struct rpc_task *task;
3545 struct rpc_message msg = {
3546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3549 struct rpc_task_setup task_setup_data = {
3550 .rpc_client = server->client,
3551 .rpc_message = &msg,
3552 .callback_ops = &nfs4_delegreturn_ops,
3553 .flags = RPC_TASK_ASYNC,
3557 data = kzalloc(sizeof(*data), GFP_KERNEL);
3560 data->args.fhandle = &data->fh;
3561 data->args.stateid = &data->stateid;
3562 data->args.bitmask = server->attr_bitmask;
3563 nfs_copy_fh(&data->fh, NFS_FH(inode));
3564 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3565 data->res.fattr = &data->fattr;
3566 data->res.server = server;
3567 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3568 nfs_fattr_init(data->res.fattr);
3569 data->timestamp = jiffies;
3570 data->rpc_status = 0;
3572 task_setup_data.callback_data = data;
3573 msg.rpc_argp = &data->args,
3574 msg.rpc_resp = &data->res,
3575 task = rpc_run_task(&task_setup_data);
3577 return PTR_ERR(task);
3580 status = nfs4_wait_for_completion_rpc_task(task);
3583 status = data->rpc_status;
3586 nfs_refresh_inode(inode, &data->fattr);
3592 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3594 struct nfs_server *server = NFS_SERVER(inode);
3595 struct nfs4_exception exception = { };
3598 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3600 case -NFS4ERR_STALE_STATEID:
3601 case -NFS4ERR_EXPIRED:
3605 err = nfs4_handle_exception(server, err, &exception);
3606 } while (exception.retry);
3610 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3611 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3614 * sleep, with exponential backoff, and retry the LOCK operation.
3616 static unsigned long
3617 nfs4_set_lock_task_retry(unsigned long timeout)
3619 schedule_timeout_killable(timeout);
3621 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3622 return NFS4_LOCK_MAXTIMEOUT;
3626 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3628 struct inode *inode = state->inode;
3629 struct nfs_server *server = NFS_SERVER(inode);
3630 struct nfs_client *clp = server->nfs_client;
3631 struct nfs_lockt_args arg = {
3632 .fh = NFS_FH(inode),
3635 struct nfs_lockt_res res = {
3638 struct rpc_message msg = {
3639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3642 .rpc_cred = state->owner->so_cred,
3644 struct nfs4_lock_state *lsp;
3647 arg.lock_owner.clientid = clp->cl_clientid;
3648 status = nfs4_set_lock_state(state, request);
3651 lsp = request->fl_u.nfs4_fl.owner;
3652 arg.lock_owner.id = lsp->ls_id.id;
3653 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3656 request->fl_type = F_UNLCK;
3658 case -NFS4ERR_DENIED:
3661 request->fl_ops->fl_release_private(request);
3666 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3668 struct nfs4_exception exception = { };
3672 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3673 _nfs4_proc_getlk(state, cmd, request),
3675 } while (exception.retry);
3679 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3682 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3684 res = posix_lock_file_wait(file, fl);
3687 res = flock_lock_file_wait(file, fl);
3695 struct nfs4_unlockdata {
3696 struct nfs_locku_args arg;
3697 struct nfs_locku_res res;
3698 struct nfs4_lock_state *lsp;
3699 struct nfs_open_context *ctx;
3700 struct file_lock fl;
3701 const struct nfs_server *server;
3702 unsigned long timestamp;
3705 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3706 struct nfs_open_context *ctx,
3707 struct nfs4_lock_state *lsp,
3708 struct nfs_seqid *seqid)
3710 struct nfs4_unlockdata *p;
3711 struct inode *inode = lsp->ls_state->inode;
3713 p = kzalloc(sizeof(*p), GFP_KERNEL);
3716 p->arg.fh = NFS_FH(inode);
3718 p->arg.seqid = seqid;
3719 p->res.seqid = seqid;
3720 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3721 p->arg.stateid = &lsp->ls_stateid;
3723 atomic_inc(&lsp->ls_count);
3724 /* Ensure we don't close file until we're done freeing locks! */
3725 p->ctx = get_nfs_open_context(ctx);
3726 memcpy(&p->fl, fl, sizeof(p->fl));
3727 p->server = NFS_SERVER(inode);
3731 static void nfs4_locku_release_calldata(void *data)
3733 struct nfs4_unlockdata *calldata = data;
3734 nfs_free_seqid(calldata->arg.seqid);
3735 nfs4_put_lock_state(calldata->lsp);
3736 put_nfs_open_context(calldata->ctx);
3740 static void nfs4_locku_done(struct rpc_task *task, void *data)
3742 struct nfs4_unlockdata *calldata = data;
3744 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3746 if (RPC_ASSASSINATED(task))
3748 switch (task->tk_status) {
3750 memcpy(calldata->lsp->ls_stateid.data,
3751 calldata->res.stateid.data,
3752 sizeof(calldata->lsp->ls_stateid.data));
3753 renew_lease(calldata->server, calldata->timestamp);
3755 case -NFS4ERR_BAD_STATEID:
3756 case -NFS4ERR_OLD_STATEID:
3757 case -NFS4ERR_STALE_STATEID:
3758 case -NFS4ERR_EXPIRED:
3761 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3762 nfs4_restart_rpc(task,
3763 calldata->server->nfs_client,
3764 &calldata->res.seq_res);
3766 nfs4_sequence_free_slot(calldata->server->nfs_client,
3767 &calldata->res.seq_res);
3770 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3772 struct nfs4_unlockdata *calldata = data;
3774 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3776 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3777 /* Note: exit _without_ running nfs4_locku_done */
3778 task->tk_action = NULL;
3781 calldata->timestamp = jiffies;
3782 if (nfs4_setup_sequence(calldata->server->nfs_client,
3783 &calldata->arg.seq_args,
3784 &calldata->res.seq_res, 1, task))
3786 rpc_call_start(task);
3789 static const struct rpc_call_ops nfs4_locku_ops = {
3790 .rpc_call_prepare = nfs4_locku_prepare,
3791 .rpc_call_done = nfs4_locku_done,
3792 .rpc_release = nfs4_locku_release_calldata,
3795 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3796 struct nfs_open_context *ctx,
3797 struct nfs4_lock_state *lsp,
3798 struct nfs_seqid *seqid)
3800 struct nfs4_unlockdata *data;
3801 struct rpc_message msg = {
3802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3803 .rpc_cred = ctx->cred,
3805 struct rpc_task_setup task_setup_data = {
3806 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3807 .rpc_message = &msg,
3808 .callback_ops = &nfs4_locku_ops,
3809 .workqueue = nfsiod_workqueue,
3810 .flags = RPC_TASK_ASYNC,
3813 /* Ensure this is an unlock - when canceling a lock, the
3814 * canceled lock is passed in, and it won't be an unlock.
3816 fl->fl_type = F_UNLCK;
3818 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3820 nfs_free_seqid(seqid);
3821 return ERR_PTR(-ENOMEM);
3824 msg.rpc_argp = &data->arg,
3825 msg.rpc_resp = &data->res,
3826 task_setup_data.callback_data = data;
3827 return rpc_run_task(&task_setup_data);
3830 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3832 struct nfs_inode *nfsi = NFS_I(state->inode);
3833 struct nfs_seqid *seqid;
3834 struct nfs4_lock_state *lsp;
3835 struct rpc_task *task;
3837 unsigned char fl_flags = request->fl_flags;
3839 status = nfs4_set_lock_state(state, request);
3840 /* Unlock _before_ we do the RPC call */
3841 request->fl_flags |= FL_EXISTS;
3842 down_read(&nfsi->rwsem);
3843 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3844 up_read(&nfsi->rwsem);
3847 up_read(&nfsi->rwsem);
3850 /* Is this a delegated lock? */
3851 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3853 lsp = request->fl_u.nfs4_fl.owner;
3854 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3858 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3859 status = PTR_ERR(task);
3862 status = nfs4_wait_for_completion_rpc_task(task);
3865 request->fl_flags = fl_flags;
3869 struct nfs4_lockdata {
3870 struct nfs_lock_args arg;
3871 struct nfs_lock_res res;
3872 struct nfs4_lock_state *lsp;
3873 struct nfs_open_context *ctx;
3874 struct file_lock fl;
3875 unsigned long timestamp;
3878 struct nfs_server *server;
3881 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3882 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3884 struct nfs4_lockdata *p;
3885 struct inode *inode = lsp->ls_state->inode;
3886 struct nfs_server *server = NFS_SERVER(inode);
3888 p = kzalloc(sizeof(*p), GFP_KERNEL);
3892 p->arg.fh = NFS_FH(inode);
3894 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3895 if (p->arg.open_seqid == NULL)
3897 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3898 if (p->arg.lock_seqid == NULL)
3899 goto out_free_seqid;
3900 p->arg.lock_stateid = &lsp->ls_stateid;
3901 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3902 p->arg.lock_owner.id = lsp->ls_id.id;
3903 p->res.lock_seqid = p->arg.lock_seqid;
3904 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3907 atomic_inc(&lsp->ls_count);
3908 p->ctx = get_nfs_open_context(ctx);
3909 memcpy(&p->fl, fl, sizeof(p->fl));
3912 nfs_free_seqid(p->arg.open_seqid);
3918 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3920 struct nfs4_lockdata *data = calldata;
3921 struct nfs4_state *state = data->lsp->ls_state;
3923 dprintk("%s: begin!\n", __func__);
3924 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3926 /* Do we need to do an open_to_lock_owner? */
3927 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3928 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3930 data->arg.open_stateid = &state->stateid;
3931 data->arg.new_lock_owner = 1;
3932 data->res.open_seqid = data->arg.open_seqid;
3934 data->arg.new_lock_owner = 0;
3935 data->timestamp = jiffies;
3936 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3937 &data->res.seq_res, 1, task))
3939 rpc_call_start(task);
3940 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3943 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3945 struct nfs4_lockdata *data = calldata;
3947 dprintk("%s: begin!\n", __func__);
3949 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3952 data->rpc_status = task->tk_status;
3953 if (RPC_ASSASSINATED(task))
3955 if (data->arg.new_lock_owner != 0) {
3956 if (data->rpc_status == 0)
3957 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3961 if (data->rpc_status == 0) {
3962 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3963 sizeof(data->lsp->ls_stateid.data));
3964 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3965 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3968 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3971 static void nfs4_lock_release(void *calldata)
3973 struct nfs4_lockdata *data = calldata;
3975 dprintk("%s: begin!\n", __func__);
3976 nfs_free_seqid(data->arg.open_seqid);
3977 if (data->cancelled != 0) {
3978 struct rpc_task *task;
3979 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3980 data->arg.lock_seqid);
3983 dprintk("%s: cancelling lock!\n", __func__);
3985 nfs_free_seqid(data->arg.lock_seqid);
3986 nfs4_put_lock_state(data->lsp);
3987 put_nfs_open_context(data->ctx);
3989 dprintk("%s: done!\n", __func__);
3992 static const struct rpc_call_ops nfs4_lock_ops = {
3993 .rpc_call_prepare = nfs4_lock_prepare,
3994 .rpc_call_done = nfs4_lock_done,
3995 .rpc_release = nfs4_lock_release,
3998 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
4000 struct nfs4_lockdata *data;
4001 struct rpc_task *task;
4002 struct rpc_message msg = {
4003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4004 .rpc_cred = state->owner->so_cred,
4006 struct rpc_task_setup task_setup_data = {
4007 .rpc_client = NFS_CLIENT(state->inode),
4008 .rpc_message = &msg,
4009 .callback_ops = &nfs4_lock_ops,
4010 .workqueue = nfsiod_workqueue,
4011 .flags = RPC_TASK_ASYNC,
4015 dprintk("%s: begin!\n", __func__);
4016 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4017 fl->fl_u.nfs4_fl.owner);
4021 data->arg.block = 1;
4023 data->arg.reclaim = 1;
4024 msg.rpc_argp = &data->arg,
4025 msg.rpc_resp = &data->res,
4026 task_setup_data.callback_data = data;
4027 task = rpc_run_task(&task_setup_data);
4029 return PTR_ERR(task);
4030 ret = nfs4_wait_for_completion_rpc_task(task);
4032 ret = data->rpc_status;
4034 data->cancelled = 1;
4036 dprintk("%s: done, ret = %d!\n", __func__, ret);
4040 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4042 struct nfs_server *server = NFS_SERVER(state->inode);
4043 struct nfs4_exception exception = { };
4047 /* Cache the lock if possible... */
4048 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4050 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4051 if (err != -NFS4ERR_DELAY)
4053 nfs4_handle_exception(server, err, &exception);
4054 } while (exception.retry);
4058 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4060 struct nfs_server *server = NFS_SERVER(state->inode);
4061 struct nfs4_exception exception = { };
4064 err = nfs4_set_lock_state(state, request);
4068 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4070 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4074 case -NFS4ERR_GRACE:
4075 case -NFS4ERR_DELAY:
4076 nfs4_handle_exception(server, err, &exception);
4079 } while (exception.retry);
4084 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4086 struct nfs_inode *nfsi = NFS_I(state->inode);
4087 unsigned char fl_flags = request->fl_flags;
4090 /* Is this a delegated open? */
4091 status = nfs4_set_lock_state(state, request);
4094 request->fl_flags |= FL_ACCESS;
4095 status = do_vfs_lock(request->fl_file, request);
4098 down_read(&nfsi->rwsem);
4099 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4100 /* Yes: cache locks! */
4101 /* ...but avoid races with delegation recall... */
4102 request->fl_flags = fl_flags & ~FL_SLEEP;
4103 status = do_vfs_lock(request->fl_file, request);
4106 status = _nfs4_do_setlk(state, cmd, request, 0);
4109 /* Note: we always want to sleep here! */
4110 request->fl_flags = fl_flags | FL_SLEEP;
4111 if (do_vfs_lock(request->fl_file, request) < 0)
4112 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4114 up_read(&nfsi->rwsem);
4116 request->fl_flags = fl_flags;
4120 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4122 struct nfs4_exception exception = { };
4126 err = _nfs4_proc_setlk(state, cmd, request);
4127 if (err == -NFS4ERR_DENIED)
4129 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4131 } while (exception.retry);
4136 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4138 struct nfs_open_context *ctx;
4139 struct nfs4_state *state;
4140 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4143 /* verify open state */
4144 ctx = nfs_file_open_context(filp);
4147 if (request->fl_start < 0 || request->fl_end < 0)
4150 if (IS_GETLK(cmd)) {
4152 return nfs4_proc_getlk(state, F_GETLK, request);
4156 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4159 if (request->fl_type == F_UNLCK) {
4161 return nfs4_proc_unlck(state, cmd, request);
4168 status = nfs4_proc_setlk(state, cmd, request);
4169 if ((status != -EAGAIN) || IS_SETLK(cmd))
4171 timeout = nfs4_set_lock_task_retry(timeout);
4172 status = -ERESTARTSYS;
4175 } while(status < 0);
4179 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4181 struct nfs_server *server = NFS_SERVER(state->inode);
4182 struct nfs4_exception exception = { };
4185 err = nfs4_set_lock_state(state, fl);
4189 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4192 printk(KERN_ERR "%s: unhandled error %d.\n",
4197 case -NFS4ERR_EXPIRED:
4198 case -NFS4ERR_STALE_CLIENTID:
4199 case -NFS4ERR_STALE_STATEID:
4200 nfs4_schedule_state_recovery(server->nfs_client);
4204 * The show must go on: exit, but mark the
4205 * stateid as needing recovery.
4207 case -NFS4ERR_ADMIN_REVOKED:
4208 case -NFS4ERR_BAD_STATEID:
4209 case -NFS4ERR_OPENMODE:
4210 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4214 case -NFS4ERR_DENIED:
4215 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4218 case -NFS4ERR_DELAY:
4221 err = nfs4_handle_exception(server, err, &exception);
4222 } while (exception.retry);
4227 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4229 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4230 size_t buflen, int flags)
4232 struct inode *inode = dentry->d_inode;
4234 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4237 return nfs4_proc_set_acl(inode, buf, buflen);
4240 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4241 * and that's what we'll do for e.g. user attributes that haven't been set.
4242 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4243 * attributes in kernel-managed attribute namespaces. */
4244 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4247 struct inode *inode = dentry->d_inode;
4249 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4252 return nfs4_proc_get_acl(inode, buf, buflen);
4255 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4257 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4259 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4261 if (buf && buflen < len)
4264 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4268 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4270 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4271 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4272 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4275 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4276 NFS_ATTR_FATTR_NLINK;
4277 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4281 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4282 struct nfs4_fs_locations *fs_locations, struct page *page)
4284 struct nfs_server *server = NFS_SERVER(dir);
4286 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4287 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4289 struct nfs4_fs_locations_arg args = {
4290 .dir_fh = NFS_FH(dir),
4295 struct nfs4_fs_locations_res res = {
4296 .fs_locations = fs_locations,
4298 struct rpc_message msg = {
4299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4305 dprintk("%s: start\n", __func__);
4306 nfs_fattr_init(&fs_locations->fattr);
4307 fs_locations->server = server;
4308 fs_locations->nlocations = 0;
4309 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4310 nfs_fixup_referral_attributes(&fs_locations->fattr);
4311 dprintk("%s: returned status = %d\n", __func__, status);
4315 #ifdef CONFIG_NFS_V4_1
4317 * nfs4_proc_exchange_id()
4319 * Since the clientid has expired, all compounds using sessions
4320 * associated with the stale clientid will be returning
4321 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4322 * be in some phase of session reset.
4324 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4326 nfs4_verifier verifier;
4327 struct nfs41_exchange_id_args args = {
4329 .flags = clp->cl_exchange_flags,
4331 struct nfs41_exchange_id_res res = {
4335 struct rpc_message msg = {
4336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4343 dprintk("--> %s\n", __func__);
4344 BUG_ON(clp == NULL);
4346 p = (u32 *)verifier.data;
4347 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4348 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4349 args.verifier = &verifier;
4352 args.id_len = scnprintf(args.id, sizeof(args.id),
4355 rpc_peeraddr2str(clp->cl_rpcclient,
4357 clp->cl_id_uniquifier);
4359 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4361 if (status != NFS4ERR_CLID_INUSE)
4367 if (++clp->cl_id_uniquifier == 0)
4371 dprintk("<-- %s status= %d\n", __func__, status);
4375 struct nfs4_get_lease_time_data {
4376 struct nfs4_get_lease_time_args *args;
4377 struct nfs4_get_lease_time_res *res;
4378 struct nfs_client *clp;
4381 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4385 struct nfs4_get_lease_time_data *data =
4386 (struct nfs4_get_lease_time_data *)calldata;
4388 dprintk("--> %s\n", __func__);
4389 /* just setup sequence, do not trigger session recovery
4390 since we're invoked within one */
4391 ret = nfs41_setup_sequence(data->clp->cl_session,
4392 &data->args->la_seq_args,
4393 &data->res->lr_seq_res, 0, task);
4395 BUG_ON(ret == -EAGAIN);
4396 rpc_call_start(task);
4397 dprintk("<-- %s\n", __func__);
4401 * Called from nfs4_state_manager thread for session setup, so don't recover
4402 * from sequence operation or clientid errors.
4404 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4406 struct nfs4_get_lease_time_data *data =
4407 (struct nfs4_get_lease_time_data *)calldata;
4409 dprintk("--> %s\n", __func__);
4410 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4411 switch (task->tk_status) {
4412 case -NFS4ERR_DELAY:
4413 case -NFS4ERR_GRACE:
4414 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4415 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4416 task->tk_status = 0;
4417 rpc_restart_call(task);
4420 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4421 dprintk("<-- %s\n", __func__);
4424 struct rpc_call_ops nfs4_get_lease_time_ops = {
4425 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4426 .rpc_call_done = nfs4_get_lease_time_done,
4429 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4431 struct rpc_task *task;
4432 struct nfs4_get_lease_time_args args;
4433 struct nfs4_get_lease_time_res res = {
4434 .lr_fsinfo = fsinfo,
4436 struct nfs4_get_lease_time_data data = {
4441 struct rpc_message msg = {
4442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4446 struct rpc_task_setup task_setup = {
4447 .rpc_client = clp->cl_rpcclient,
4448 .rpc_message = &msg,
4449 .callback_ops = &nfs4_get_lease_time_ops,
4450 .callback_data = &data
4454 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4455 dprintk("--> %s\n", __func__);
4456 task = rpc_run_task(&task_setup);
4459 status = PTR_ERR(task);
4461 status = task->tk_status;
4464 dprintk("<-- %s return %d\n", __func__, status);
4470 * Reset a slot table
4472 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4473 int old_max_slots, int ivalue)
4478 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
4481 * Until we have dynamic slot table adjustment, insist
4482 * upon the same slot table size
4484 if (max_slots != old_max_slots) {
4485 dprintk("%s reset slot table does't match old\n",
4487 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4490 spin_lock(&tbl->slot_tbl_lock);
4491 for (i = 0; i < max_slots; ++i)
4492 tbl->slots[i].seq_nr = ivalue;
4493 tbl->highest_used_slotid = -1;
4494 spin_unlock(&tbl->slot_tbl_lock);
4495 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4496 tbl, tbl->slots, tbl->max_slots);
4498 dprintk("<-- %s: return %d\n", __func__, ret);
4503 * Reset the forechannel and backchannel slot tables
4505 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4509 status = nfs4_reset_slot_table(&session->fc_slot_table,
4510 session->fc_attrs.max_reqs,
4511 session->fc_slot_table.max_slots,
4515 init_completion(&session->complete);
4517 status = nfs4_reset_slot_table(&session->bc_slot_table,
4518 session->bc_attrs.max_reqs,
4519 session->bc_slot_table.max_slots,
4524 /* Destroy the slot table */
4525 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4527 if (session->fc_slot_table.slots != NULL) {
4528 kfree(session->fc_slot_table.slots);
4529 session->fc_slot_table.slots = NULL;
4531 if (session->bc_slot_table.slots != NULL) {
4532 kfree(session->bc_slot_table.slots);
4533 session->bc_slot_table.slots = NULL;
4539 * Initialize slot table
4541 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4542 int max_slots, int ivalue)
4545 struct nfs4_slot *slot;
4548 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4550 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4552 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4555 for (i = 0; i < max_slots; ++i)
4556 slot[i].seq_nr = ivalue;
4559 spin_lock(&tbl->slot_tbl_lock);
4560 if (tbl->slots != NULL) {
4561 spin_unlock(&tbl->slot_tbl_lock);
4562 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4563 __func__, tbl, tbl->slots);
4567 tbl->max_slots = max_slots;
4569 tbl->highest_used_slotid = -1; /* no slot is currently used */
4570 spin_unlock(&tbl->slot_tbl_lock);
4571 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4572 tbl, tbl->slots, tbl->max_slots);
4574 dprintk("<-- %s: return %d\n", __func__, ret);
4583 * Initialize the forechannel and backchannel tables
4585 static int nfs4_init_slot_tables(struct nfs4_session *session)
4589 status = nfs4_init_slot_table(&session->fc_slot_table,
4590 session->fc_attrs.max_reqs, 1);
4594 status = nfs4_init_slot_table(&session->bc_slot_table,
4595 session->bc_attrs.max_reqs, 0);
4597 nfs4_destroy_slot_tables(session);
4602 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4604 struct nfs4_session *session;
4605 struct nfs4_slot_table *tbl;
4607 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4612 * The create session reply races with the server back
4613 * channel probe. Mark the client NFS_CS_SESSION_INITING
4614 * so that the client back channel can find the
4617 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4618 init_completion(&session->complete);
4620 tbl = &session->fc_slot_table;
4621 spin_lock_init(&tbl->slot_tbl_lock);
4622 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4624 tbl = &session->bc_slot_table;
4625 spin_lock_init(&tbl->slot_tbl_lock);
4626 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4632 void nfs4_destroy_session(struct nfs4_session *session)
4634 nfs4_proc_destroy_session(session);
4635 dprintk("%s Destroy backchannel for xprt %p\n",
4636 __func__, session->clp->cl_rpcclient->cl_xprt);
4637 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4638 NFS41_BC_MIN_CALLBACKS);
4639 nfs4_destroy_slot_tables(session);
4644 * Initialize the values to be used by the client in CREATE_SESSION
4645 * If nfs4_init_session set the fore channel request and response sizes,
4648 * Set the back channel max_resp_sz_cached to zero to force the client to
4649 * always set csa_cachethis to FALSE because the current implementation
4650 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4652 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4654 struct nfs4_session *session = args->client->cl_session;
4655 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4656 mxresp_sz = session->fc_attrs.max_resp_sz;
4659 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4661 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4662 /* Fore channel attributes */
4663 args->fc_attrs.headerpadsz = 0;
4664 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4665 args->fc_attrs.max_resp_sz = mxresp_sz;
4666 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4667 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4668 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4670 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4671 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4673 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4674 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4675 args->fc_attrs.max_reqs);
4677 /* Back channel attributes */
4678 args->bc_attrs.headerpadsz = 0;
4679 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4680 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4681 args->bc_attrs.max_resp_sz_cached = 0;
4682 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4683 args->bc_attrs.max_reqs = 1;
4685 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4686 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4688 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4689 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4690 args->bc_attrs.max_reqs);
4693 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4697 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4698 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4702 #define _verify_fore_channel_attr(_name_) \
4703 _verify_channel_attr("fore", #_name_, \
4704 args->fc_attrs._name_, \
4705 session->fc_attrs._name_)
4707 #define _verify_back_channel_attr(_name_) \
4708 _verify_channel_attr("back", #_name_, \
4709 args->bc_attrs._name_, \
4710 session->bc_attrs._name_)
4713 * The server is not allowed to increase the fore channel header pad size,
4714 * maximum response size, or maximum number of operations.
4716 * The back channel attributes are only negotiatied down: We send what the
4717 * (back channel) server insists upon.
4719 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4720 struct nfs4_session *session)
4724 ret |= _verify_fore_channel_attr(headerpadsz);
4725 ret |= _verify_fore_channel_attr(max_resp_sz);
4726 ret |= _verify_fore_channel_attr(max_ops);
4728 ret |= _verify_back_channel_attr(headerpadsz);
4729 ret |= _verify_back_channel_attr(max_rqst_sz);
4730 ret |= _verify_back_channel_attr(max_resp_sz);
4731 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4732 ret |= _verify_back_channel_attr(max_ops);
4733 ret |= _verify_back_channel_attr(max_reqs);
4738 static int _nfs4_proc_create_session(struct nfs_client *clp)
4740 struct nfs4_session *session = clp->cl_session;
4741 struct nfs41_create_session_args args = {
4743 .cb_program = NFS4_CALLBACK,
4745 struct nfs41_create_session_res res = {
4748 struct rpc_message msg = {
4749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4755 nfs4_init_channel_attrs(&args);
4756 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4758 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4761 /* Verify the session's negotiated channel_attrs values */
4762 status = nfs4_verify_channel_attrs(&args, session);
4764 /* Increment the clientid slot sequence id */
4772 * Issues a CREATE_SESSION operation to the server.
4773 * It is the responsibility of the caller to verify the session is
4774 * expired before calling this routine.
4776 int nfs4_proc_create_session(struct nfs_client *clp, int reset)
4780 struct nfs_fsinfo fsinfo;
4781 struct nfs4_session *session = clp->cl_session;
4783 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4785 status = _nfs4_proc_create_session(clp);
4789 /* Init or reset the fore channel */
4791 status = nfs4_reset_slot_tables(session);
4793 status = nfs4_init_slot_tables(session);
4794 dprintk("fore channel slot table initialization returned %d\n", status);
4798 ptr = (unsigned *)&session->sess_id.data[0];
4799 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4800 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4803 /* Lease time is aleady set */
4806 /* Get the lease time */
4807 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4809 /* Update lease time and schedule renewal */
4810 spin_lock(&clp->cl_lock);
4811 clp->cl_lease_time = fsinfo.lease_time * HZ;
4812 clp->cl_last_renewal = jiffies;
4813 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4814 spin_unlock(&clp->cl_lock);
4816 nfs4_schedule_state_renewal(clp);
4819 dprintk("<-- %s\n", __func__);
4824 * Issue the over-the-wire RPC DESTROY_SESSION.
4825 * The caller must serialize access to this routine.
4827 int nfs4_proc_destroy_session(struct nfs4_session *session)
4830 struct rpc_message msg;
4832 dprintk("--> nfs4_proc_destroy_session\n");
4834 /* session is still being setup */
4835 if (session->clp->cl_cons_state != NFS_CS_READY)
4838 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4839 msg.rpc_argp = session;
4840 msg.rpc_resp = NULL;
4841 msg.rpc_cred = NULL;
4842 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4846 "Got error %d from the server on DESTROY_SESSION. "
4847 "Session has been destroyed regardless...\n", status);
4849 dprintk("<-- nfs4_proc_destroy_session\n");
4853 int nfs4_init_session(struct nfs_server *server)
4855 struct nfs_client *clp = server->nfs_client;
4858 if (!nfs4_has_session(clp))
4861 clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4862 clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4863 ret = nfs4_recover_expired_lease(server);
4865 ret = nfs4_check_client_ready(clp);
4870 * Renew the cl_session lease.
4872 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4874 struct nfs4_sequence_args args;
4875 struct nfs4_sequence_res res;
4877 struct rpc_message msg = {
4878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4884 args.sa_cache_this = 0;
4886 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4890 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4892 struct nfs_client *clp = (struct nfs_client *)data;
4894 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4896 if (task->tk_status < 0) {
4897 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4899 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4901 nfs4_restart_rpc(task, clp, task->tk_msg.rpc_resp);
4905 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4906 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4908 kfree(task->tk_msg.rpc_argp);
4909 kfree(task->tk_msg.rpc_resp);
4911 dprintk("<-- %s\n", __func__);
4914 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4916 struct nfs_client *clp;
4917 struct nfs4_sequence_args *args;
4918 struct nfs4_sequence_res *res;
4920 clp = (struct nfs_client *)data;
4921 args = task->tk_msg.rpc_argp;
4922 res = task->tk_msg.rpc_resp;
4924 if (nfs4_setup_sequence(clp, args, res, 0, task))
4926 rpc_call_start(task);
4929 static const struct rpc_call_ops nfs41_sequence_ops = {
4930 .rpc_call_done = nfs41_sequence_call_done,
4931 .rpc_call_prepare = nfs41_sequence_prepare,
4934 static int nfs41_proc_async_sequence(struct nfs_client *clp,
4935 struct rpc_cred *cred)
4937 struct nfs4_sequence_args *args;
4938 struct nfs4_sequence_res *res;
4939 struct rpc_message msg = {
4940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4944 args = kzalloc(sizeof(*args), GFP_KERNEL);
4947 res = kzalloc(sizeof(*res), GFP_KERNEL);
4952 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4953 msg.rpc_argp = args;
4956 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4957 &nfs41_sequence_ops, (void *)clp);
4960 #endif /* CONFIG_NFS_V4_1 */
4962 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
4963 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4964 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4965 .recover_open = nfs4_open_reclaim,
4966 .recover_lock = nfs4_lock_reclaim,
4967 .establish_clid = nfs4_init_clientid,
4968 .get_clid_cred = nfs4_get_setclientid_cred,
4971 #if defined(CONFIG_NFS_V4_1)
4972 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4973 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4974 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4975 .recover_open = nfs4_open_reclaim,
4976 .recover_lock = nfs4_lock_reclaim,
4977 .establish_clid = nfs41_init_clientid,
4978 .get_clid_cred = nfs4_get_exchange_id_cred,
4980 #endif /* CONFIG_NFS_V4_1 */
4982 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
4983 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4984 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4985 .recover_open = nfs4_open_expired,
4986 .recover_lock = nfs4_lock_expired,
4987 .establish_clid = nfs4_init_clientid,
4988 .get_clid_cred = nfs4_get_setclientid_cred,
4991 #if defined(CONFIG_NFS_V4_1)
4992 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
4993 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4994 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4995 .recover_open = nfs4_open_expired,
4996 .recover_lock = nfs4_lock_expired,
4997 .establish_clid = nfs41_init_clientid,
4998 .get_clid_cred = nfs4_get_exchange_id_cred,
5000 #endif /* CONFIG_NFS_V4_1 */
5002 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5003 .sched_state_renewal = nfs4_proc_async_renew,
5004 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5005 .renew_lease = nfs4_proc_renew,
5008 #if defined(CONFIG_NFS_V4_1)
5009 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5010 .sched_state_renewal = nfs41_proc_async_sequence,
5011 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5012 .renew_lease = nfs4_proc_sequence,
5017 * Per minor version reboot and network partition recovery ops
5020 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5021 &nfs40_reboot_recovery_ops,
5022 #if defined(CONFIG_NFS_V4_1)
5023 &nfs41_reboot_recovery_ops,
5027 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5028 &nfs40_nograce_recovery_ops,
5029 #if defined(CONFIG_NFS_V4_1)
5030 &nfs41_nograce_recovery_ops,
5034 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5035 &nfs40_state_renewal_ops,
5036 #if defined(CONFIG_NFS_V4_1)
5037 &nfs41_state_renewal_ops,
5041 static const struct inode_operations nfs4_file_inode_operations = {
5042 .permission = nfs_permission,
5043 .getattr = nfs_getattr,
5044 .setattr = nfs_setattr,
5045 .getxattr = nfs4_getxattr,
5046 .setxattr = nfs4_setxattr,
5047 .listxattr = nfs4_listxattr,
5050 const struct nfs_rpc_ops nfs_v4_clientops = {
5051 .version = 4, /* protocol version */
5052 .dentry_ops = &nfs4_dentry_operations,
5053 .dir_inode_ops = &nfs4_dir_inode_operations,
5054 .file_inode_ops = &nfs4_file_inode_operations,
5055 .getroot = nfs4_proc_get_root,
5056 .getattr = nfs4_proc_getattr,
5057 .setattr = nfs4_proc_setattr,
5058 .lookupfh = nfs4_proc_lookupfh,
5059 .lookup = nfs4_proc_lookup,
5060 .access = nfs4_proc_access,
5061 .readlink = nfs4_proc_readlink,
5062 .create = nfs4_proc_create,
5063 .remove = nfs4_proc_remove,
5064 .unlink_setup = nfs4_proc_unlink_setup,
5065 .unlink_done = nfs4_proc_unlink_done,
5066 .rename = nfs4_proc_rename,
5067 .link = nfs4_proc_link,
5068 .symlink = nfs4_proc_symlink,
5069 .mkdir = nfs4_proc_mkdir,
5070 .rmdir = nfs4_proc_remove,
5071 .readdir = nfs4_proc_readdir,
5072 .mknod = nfs4_proc_mknod,
5073 .statfs = nfs4_proc_statfs,
5074 .fsinfo = nfs4_proc_fsinfo,
5075 .pathconf = nfs4_proc_pathconf,
5076 .set_capabilities = nfs4_server_capabilities,
5077 .decode_dirent = nfs4_decode_dirent,
5078 .read_setup = nfs4_proc_read_setup,
5079 .read_done = nfs4_read_done,
5080 .write_setup = nfs4_proc_write_setup,
5081 .write_done = nfs4_write_done,
5082 .commit_setup = nfs4_proc_commit_setup,
5083 .commit_done = nfs4_commit_done,
5084 .lock = nfs4_proc_lock,
5085 .clear_acl_cache = nfs4_zap_acl_attr,
5086 .close_context = nfs4_close_context,