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 nfs4_schedule_state_recovery(clp);
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.
322 * Must be called while holding tbl->slot_tbl_lock
325 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
327 int slotid = free_slotid;
329 /* clear used bit in bitmap */
330 __clear_bit(slotid, tbl->used_slots);
332 /* update highest_used_slotid when it is freed */
333 if (slotid == tbl->highest_used_slotid) {
334 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
335 if (slotid < tbl->max_slots)
336 tbl->highest_used_slotid = slotid;
338 tbl->highest_used_slotid = -1;
340 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
341 free_slotid, tbl->highest_used_slotid);
345 * Signal state manager thread if session is drained
347 static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
349 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
350 rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
354 if (ses->fc_slot_table.highest_used_slotid != -1)
357 dprintk("%s COMPLETE: Session Drained\n", __func__);
358 complete(&ses->complete);
361 static void nfs41_sequence_free_slot(const struct nfs_client *clp,
362 struct nfs4_sequence_res *res)
364 struct nfs4_slot_table *tbl;
366 tbl = &clp->cl_session->fc_slot_table;
367 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
368 /* just wake up the next guy waiting since
369 * we may have not consumed a slot after all */
370 dprintk("%s: No slot\n", __func__);
374 spin_lock(&tbl->slot_tbl_lock);
375 nfs4_free_slot(tbl, res->sr_slotid);
376 nfs41_check_drain_session_complete(clp->cl_session);
377 spin_unlock(&tbl->slot_tbl_lock);
378 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
381 static void nfs41_sequence_done(struct nfs_client *clp,
382 struct nfs4_sequence_res *res,
385 unsigned long timestamp;
386 struct nfs4_slot_table *tbl;
387 struct nfs4_slot *slot;
390 * sr_status remains 1 if an RPC level error occurred. The server
391 * may or may not have processed the sequence operation..
392 * Proceed as if the server received and processed the sequence
395 if (res->sr_status == 1)
396 res->sr_status = NFS_OK;
398 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
399 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
402 /* Check the SEQUENCE operation status */
403 if (res->sr_status == 0) {
404 tbl = &clp->cl_session->fc_slot_table;
405 slot = tbl->slots + res->sr_slotid;
406 /* Update the slot's sequence and clientid lease timer */
408 timestamp = res->sr_renewal_time;
409 spin_lock(&clp->cl_lock);
410 if (time_before(clp->cl_last_renewal, timestamp))
411 clp->cl_last_renewal = timestamp;
412 spin_unlock(&clp->cl_lock);
413 /* Check sequence flags */
414 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
417 /* The session may be reset by one of the error handlers. */
418 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
419 nfs41_sequence_free_slot(clp, res);
423 * nfs4_find_slot - efficiently look for a free slot
425 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
426 * If found, we mark the slot as used, update the highest_used_slotid,
427 * and respectively set up the sequence operation args.
428 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
430 * Note: must be called with under the slot_tbl_lock.
433 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
436 u8 ret_id = NFS4_MAX_SLOT_TABLE;
437 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
439 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
440 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
442 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
443 if (slotid >= tbl->max_slots)
445 __set_bit(slotid, tbl->used_slots);
446 if (slotid > tbl->highest_used_slotid)
447 tbl->highest_used_slotid = slotid;
450 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
451 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
455 static int nfs41_setup_sequence(struct nfs4_session *session,
456 struct nfs4_sequence_args *args,
457 struct nfs4_sequence_res *res,
459 struct rpc_task *task)
461 struct nfs4_slot *slot;
462 struct nfs4_slot_table *tbl;
465 dprintk("--> %s\n", __func__);
466 /* slot already allocated? */
467 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
470 memset(res, 0, sizeof(*res));
471 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
472 tbl = &session->fc_slot_table;
474 spin_lock(&tbl->slot_tbl_lock);
475 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state)) {
477 * The state manager will wait until the slot table is empty.
478 * Schedule the reset thread
480 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
481 spin_unlock(&tbl->slot_tbl_lock);
482 dprintk("%s Schedule Session Reset\n", __func__);
486 slotid = nfs4_find_slot(tbl, task);
487 if (slotid == NFS4_MAX_SLOT_TABLE) {
488 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
489 spin_unlock(&tbl->slot_tbl_lock);
490 dprintk("<-- %s: no free slots\n", __func__);
493 spin_unlock(&tbl->slot_tbl_lock);
495 slot = tbl->slots + slotid;
496 args->sa_session = session;
497 args->sa_slotid = slotid;
498 args->sa_cache_this = cache_reply;
500 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
502 res->sr_session = session;
503 res->sr_slotid = slotid;
504 res->sr_renewal_time = jiffies;
506 * sr_status is only set in decode_sequence, and so will remain
507 * set to 1 if an rpc level failure occurs.
513 int nfs4_setup_sequence(struct nfs_client *clp,
514 struct nfs4_sequence_args *args,
515 struct nfs4_sequence_res *res,
517 struct rpc_task *task)
521 dprintk("--> %s clp %p session %p sr_slotid %d\n",
522 __func__, clp, clp->cl_session, res->sr_slotid);
524 if (!nfs4_has_session(clp))
526 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
528 if (ret && ret != -EAGAIN) {
529 /* terminate rpc task */
530 task->tk_status = ret;
531 task->tk_action = NULL;
534 dprintk("<-- %s status=%d\n", __func__, ret);
538 struct nfs41_call_sync_data {
539 struct nfs_client *clp;
540 struct nfs4_sequence_args *seq_args;
541 struct nfs4_sequence_res *seq_res;
545 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
547 struct nfs41_call_sync_data *data = calldata;
549 dprintk("--> %s data->clp->cl_session %p\n", __func__,
550 data->clp->cl_session);
551 if (nfs4_setup_sequence(data->clp, data->seq_args,
552 data->seq_res, data->cache_reply, task))
554 rpc_call_start(task);
557 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
559 struct nfs41_call_sync_data *data = calldata;
561 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
564 struct rpc_call_ops nfs41_call_sync_ops = {
565 .rpc_call_prepare = nfs41_call_sync_prepare,
566 .rpc_call_done = nfs41_call_sync_done,
569 static int nfs4_call_sync_sequence(struct nfs_client *clp,
570 struct rpc_clnt *clnt,
571 struct rpc_message *msg,
572 struct nfs4_sequence_args *args,
573 struct nfs4_sequence_res *res,
577 struct rpc_task *task;
578 struct nfs41_call_sync_data data = {
582 .cache_reply = cache_reply,
584 struct rpc_task_setup task_setup = {
587 .callback_ops = &nfs41_call_sync_ops,
588 .callback_data = &data
591 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
592 task = rpc_run_task(&task_setup);
596 ret = task->tk_status;
602 int _nfs4_call_sync_session(struct nfs_server *server,
603 struct rpc_message *msg,
604 struct nfs4_sequence_args *args,
605 struct nfs4_sequence_res *res,
608 return nfs4_call_sync_sequence(server->nfs_client, server->client,
609 msg, args, res, cache_reply);
612 #endif /* CONFIG_NFS_V4_1 */
614 int _nfs4_call_sync(struct nfs_server *server,
615 struct rpc_message *msg,
616 struct nfs4_sequence_args *args,
617 struct nfs4_sequence_res *res,
620 args->sa_session = res->sr_session = NULL;
621 return rpc_call_sync(server->client, msg, 0);
624 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
625 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
626 &(res)->seq_res, (cache_reply))
628 static void nfs4_sequence_done(const struct nfs_server *server,
629 struct nfs4_sequence_res *res, int rpc_status)
631 #ifdef CONFIG_NFS_V4_1
632 if (nfs4_has_session(server->nfs_client))
633 nfs41_sequence_done(server->nfs_client, res, rpc_status);
634 #endif /* CONFIG_NFS_V4_1 */
637 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
639 struct nfs_inode *nfsi = NFS_I(dir);
641 spin_lock(&dir->i_lock);
642 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
643 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
644 nfs_force_lookup_revalidate(dir);
645 nfsi->change_attr = cinfo->after;
646 spin_unlock(&dir->i_lock);
649 struct nfs4_opendata {
651 struct nfs_openargs o_arg;
652 struct nfs_openres o_res;
653 struct nfs_open_confirmargs c_arg;
654 struct nfs_open_confirmres c_res;
655 struct nfs_fattr f_attr;
656 struct nfs_fattr dir_attr;
659 struct nfs4_state_owner *owner;
660 struct nfs4_state *state;
662 unsigned long timestamp;
663 unsigned int rpc_done : 1;
669 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
671 p->o_res.f_attr = &p->f_attr;
672 p->o_res.dir_attr = &p->dir_attr;
673 p->o_res.seqid = p->o_arg.seqid;
674 p->c_res.seqid = p->c_arg.seqid;
675 p->o_res.server = p->o_arg.server;
676 nfs_fattr_init(&p->f_attr);
677 nfs_fattr_init(&p->dir_attr);
678 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
681 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
682 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
683 const struct iattr *attrs)
685 struct dentry *parent = dget_parent(path->dentry);
686 struct inode *dir = parent->d_inode;
687 struct nfs_server *server = NFS_SERVER(dir);
688 struct nfs4_opendata *p;
690 p = kzalloc(sizeof(*p), GFP_KERNEL);
693 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
694 if (p->o_arg.seqid == NULL)
696 p->path.mnt = mntget(path->mnt);
697 p->path.dentry = dget(path->dentry);
700 atomic_inc(&sp->so_count);
701 p->o_arg.fh = NFS_FH(dir);
702 p->o_arg.open_flags = flags;
703 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
704 p->o_arg.clientid = server->nfs_client->cl_clientid;
705 p->o_arg.id = sp->so_owner_id.id;
706 p->o_arg.name = &p->path.dentry->d_name;
707 p->o_arg.server = server;
708 p->o_arg.bitmask = server->attr_bitmask;
709 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
710 if (flags & O_EXCL) {
711 if (nfs4_has_persistent_session(server->nfs_client)) {
713 p->o_arg.u.attrs = &p->attrs;
714 memcpy(&p->attrs, attrs, sizeof(p->attrs));
715 } else { /* EXCLUSIVE4_1 */
716 u32 *s = (u32 *) p->o_arg.u.verifier.data;
720 } else if (flags & O_CREAT) {
721 p->o_arg.u.attrs = &p->attrs;
722 memcpy(&p->attrs, attrs, sizeof(p->attrs));
724 p->c_arg.fh = &p->o_res.fh;
725 p->c_arg.stateid = &p->o_res.stateid;
726 p->c_arg.seqid = p->o_arg.seqid;
727 nfs4_init_opendata_res(p);
737 static void nfs4_opendata_free(struct kref *kref)
739 struct nfs4_opendata *p = container_of(kref,
740 struct nfs4_opendata, kref);
742 nfs_free_seqid(p->o_arg.seqid);
743 if (p->state != NULL)
744 nfs4_put_open_state(p->state);
745 nfs4_put_state_owner(p->owner);
751 static void nfs4_opendata_put(struct nfs4_opendata *p)
754 kref_put(&p->kref, nfs4_opendata_free);
757 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
761 ret = rpc_wait_for_completion_task(task);
765 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
769 if (open_mode & O_EXCL)
771 switch (mode & (FMODE_READ|FMODE_WRITE)) {
773 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
774 && state->n_rdonly != 0;
777 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
778 && state->n_wronly != 0;
780 case FMODE_READ|FMODE_WRITE:
781 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
782 && state->n_rdwr != 0;
788 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
790 if ((delegation->type & fmode) != fmode)
792 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
794 nfs_mark_delegation_referenced(delegation);
798 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
807 case FMODE_READ|FMODE_WRITE:
810 nfs4_state_set_mode_locked(state, state->state | fmode);
813 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
815 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
816 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
817 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
820 set_bit(NFS_O_RDONLY_STATE, &state->flags);
823 set_bit(NFS_O_WRONLY_STATE, &state->flags);
825 case FMODE_READ|FMODE_WRITE:
826 set_bit(NFS_O_RDWR_STATE, &state->flags);
830 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
832 write_seqlock(&state->seqlock);
833 nfs_set_open_stateid_locked(state, stateid, fmode);
834 write_sequnlock(&state->seqlock);
837 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
840 * Protect the call to nfs4_state_set_mode_locked and
841 * serialise the stateid update
843 write_seqlock(&state->seqlock);
844 if (deleg_stateid != NULL) {
845 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
846 set_bit(NFS_DELEGATED_STATE, &state->flags);
848 if (open_stateid != NULL)
849 nfs_set_open_stateid_locked(state, open_stateid, fmode);
850 write_sequnlock(&state->seqlock);
851 spin_lock(&state->owner->so_lock);
852 update_open_stateflags(state, fmode);
853 spin_unlock(&state->owner->so_lock);
856 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
858 struct nfs_inode *nfsi = NFS_I(state->inode);
859 struct nfs_delegation *deleg_cur;
862 fmode &= (FMODE_READ|FMODE_WRITE);
865 deleg_cur = rcu_dereference(nfsi->delegation);
866 if (deleg_cur == NULL)
869 spin_lock(&deleg_cur->lock);
870 if (nfsi->delegation != deleg_cur ||
871 (deleg_cur->type & fmode) != fmode)
872 goto no_delegation_unlock;
874 if (delegation == NULL)
875 delegation = &deleg_cur->stateid;
876 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
877 goto no_delegation_unlock;
879 nfs_mark_delegation_referenced(deleg_cur);
880 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
882 no_delegation_unlock:
883 spin_unlock(&deleg_cur->lock);
887 if (!ret && open_stateid != NULL) {
888 __update_open_stateid(state, open_stateid, NULL, fmode);
896 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
898 struct nfs_delegation *delegation;
901 delegation = rcu_dereference(NFS_I(inode)->delegation);
902 if (delegation == NULL || (delegation->type & fmode) == fmode) {
907 nfs_inode_return_delegation(inode);
910 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
912 struct nfs4_state *state = opendata->state;
913 struct nfs_inode *nfsi = NFS_I(state->inode);
914 struct nfs_delegation *delegation;
915 int open_mode = opendata->o_arg.open_flags & O_EXCL;
916 fmode_t fmode = opendata->o_arg.fmode;
917 nfs4_stateid stateid;
921 if (can_open_cached(state, fmode, open_mode)) {
922 spin_lock(&state->owner->so_lock);
923 if (can_open_cached(state, fmode, open_mode)) {
924 update_open_stateflags(state, fmode);
925 spin_unlock(&state->owner->so_lock);
926 goto out_return_state;
928 spin_unlock(&state->owner->so_lock);
931 delegation = rcu_dereference(nfsi->delegation);
932 if (delegation == NULL ||
933 !can_open_delegated(delegation, fmode)) {
937 /* Save the delegation */
938 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
940 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
945 /* Try to update the stateid using the delegation */
946 if (update_open_stateid(state, NULL, &stateid, fmode))
947 goto out_return_state;
952 atomic_inc(&state->count);
956 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
959 struct nfs4_state *state = NULL;
960 struct nfs_delegation *delegation;
963 if (!data->rpc_done) {
964 state = nfs4_try_open_cached(data);
969 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
971 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
972 ret = PTR_ERR(inode);
976 state = nfs4_get_open_state(inode, data->owner);
979 if (data->o_res.delegation_type != 0) {
980 int delegation_flags = 0;
983 delegation = rcu_dereference(NFS_I(inode)->delegation);
985 delegation_flags = delegation->flags;
987 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
988 nfs_inode_set_delegation(state->inode,
989 data->owner->so_cred,
992 nfs_inode_reclaim_delegation(state->inode,
993 data->owner->so_cred,
997 update_open_stateid(state, &data->o_res.stateid, NULL,
1005 return ERR_PTR(ret);
1008 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1010 struct nfs_inode *nfsi = NFS_I(state->inode);
1011 struct nfs_open_context *ctx;
1013 spin_lock(&state->inode->i_lock);
1014 list_for_each_entry(ctx, &nfsi->open_files, list) {
1015 if (ctx->state != state)
1017 get_nfs_open_context(ctx);
1018 spin_unlock(&state->inode->i_lock);
1021 spin_unlock(&state->inode->i_lock);
1022 return ERR_PTR(-ENOENT);
1025 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1027 struct nfs4_opendata *opendata;
1029 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
1030 if (opendata == NULL)
1031 return ERR_PTR(-ENOMEM);
1032 opendata->state = state;
1033 atomic_inc(&state->count);
1037 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1039 struct nfs4_state *newstate;
1042 opendata->o_arg.open_flags = 0;
1043 opendata->o_arg.fmode = fmode;
1044 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1045 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1046 nfs4_init_opendata_res(opendata);
1047 ret = _nfs4_proc_open(opendata);
1050 newstate = nfs4_opendata_to_nfs4_state(opendata);
1051 if (IS_ERR(newstate))
1052 return PTR_ERR(newstate);
1053 nfs4_close_state(&opendata->path, newstate, fmode);
1058 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1060 struct nfs4_state *newstate;
1063 /* memory barrier prior to reading state->n_* */
1064 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1066 if (state->n_rdwr != 0) {
1067 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1070 if (newstate != state)
1073 if (state->n_wronly != 0) {
1074 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1077 if (newstate != state)
1080 if (state->n_rdonly != 0) {
1081 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1084 if (newstate != state)
1088 * We may have performed cached opens for all three recoveries.
1089 * Check if we need to update the current stateid.
1091 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1092 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1093 write_seqlock(&state->seqlock);
1094 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1095 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1096 write_sequnlock(&state->seqlock);
1103 * reclaim state on the server after a reboot.
1105 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1107 struct nfs_delegation *delegation;
1108 struct nfs4_opendata *opendata;
1109 fmode_t delegation_type = 0;
1112 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1113 if (IS_ERR(opendata))
1114 return PTR_ERR(opendata);
1115 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1116 opendata->o_arg.fh = NFS_FH(state->inode);
1118 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1119 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1120 delegation_type = delegation->type;
1122 opendata->o_arg.u.delegation_type = delegation_type;
1123 status = nfs4_open_recover(opendata, state);
1124 nfs4_opendata_put(opendata);
1128 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1130 struct nfs_server *server = NFS_SERVER(state->inode);
1131 struct nfs4_exception exception = { };
1134 err = _nfs4_do_open_reclaim(ctx, state);
1135 if (err != -NFS4ERR_DELAY)
1137 nfs4_handle_exception(server, err, &exception);
1138 } while (exception.retry);
1142 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1144 struct nfs_open_context *ctx;
1147 ctx = nfs4_state_find_open_context(state);
1149 return PTR_ERR(ctx);
1150 ret = nfs4_do_open_reclaim(ctx, state);
1151 put_nfs_open_context(ctx);
1155 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1157 struct nfs4_opendata *opendata;
1160 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1161 if (IS_ERR(opendata))
1162 return PTR_ERR(opendata);
1163 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1164 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1165 sizeof(opendata->o_arg.u.delegation.data));
1166 ret = nfs4_open_recover(opendata, state);
1167 nfs4_opendata_put(opendata);
1171 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1173 struct nfs4_exception exception = { };
1174 struct nfs_server *server = NFS_SERVER(state->inode);
1177 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1183 case -NFS4ERR_BADSESSION:
1184 case -NFS4ERR_BADSLOT:
1185 case -NFS4ERR_BAD_HIGH_SLOT:
1186 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1187 case -NFS4ERR_DEADSESSION:
1188 nfs4_schedule_state_recovery(
1189 server->nfs_client);
1191 case -NFS4ERR_STALE_CLIENTID:
1192 case -NFS4ERR_STALE_STATEID:
1193 case -NFS4ERR_EXPIRED:
1194 /* Don't recall a delegation if it was lost */
1195 nfs4_schedule_state_recovery(server->nfs_client);
1199 * The show must go on: exit, but mark the
1200 * stateid as needing recovery.
1202 case -NFS4ERR_ADMIN_REVOKED:
1203 case -NFS4ERR_BAD_STATEID:
1204 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1209 err = nfs4_handle_exception(server, err, &exception);
1210 } while (exception.retry);
1215 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1217 struct nfs4_opendata *data = calldata;
1219 data->rpc_status = task->tk_status;
1220 if (RPC_ASSASSINATED(task))
1222 if (data->rpc_status == 0) {
1223 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1224 sizeof(data->o_res.stateid.data));
1225 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1226 renew_lease(data->o_res.server, data->timestamp);
1231 static void nfs4_open_confirm_release(void *calldata)
1233 struct nfs4_opendata *data = calldata;
1234 struct nfs4_state *state = NULL;
1236 /* If this request hasn't been cancelled, do nothing */
1237 if (data->cancelled == 0)
1239 /* In case of error, no cleanup! */
1240 if (!data->rpc_done)
1242 state = nfs4_opendata_to_nfs4_state(data);
1244 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1246 nfs4_opendata_put(data);
1249 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1250 .rpc_call_done = nfs4_open_confirm_done,
1251 .rpc_release = nfs4_open_confirm_release,
1255 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1257 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1259 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1260 struct rpc_task *task;
1261 struct rpc_message msg = {
1262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1263 .rpc_argp = &data->c_arg,
1264 .rpc_resp = &data->c_res,
1265 .rpc_cred = data->owner->so_cred,
1267 struct rpc_task_setup task_setup_data = {
1268 .rpc_client = server->client,
1269 .rpc_message = &msg,
1270 .callback_ops = &nfs4_open_confirm_ops,
1271 .callback_data = data,
1272 .workqueue = nfsiod_workqueue,
1273 .flags = RPC_TASK_ASYNC,
1277 kref_get(&data->kref);
1279 data->rpc_status = 0;
1280 data->timestamp = jiffies;
1281 task = rpc_run_task(&task_setup_data);
1283 return PTR_ERR(task);
1284 status = nfs4_wait_for_completion_rpc_task(task);
1286 data->cancelled = 1;
1289 status = data->rpc_status;
1294 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1296 struct nfs4_opendata *data = calldata;
1297 struct nfs4_state_owner *sp = data->owner;
1299 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1302 * Check if we still need to send an OPEN call, or if we can use
1303 * a delegation instead.
1305 if (data->state != NULL) {
1306 struct nfs_delegation *delegation;
1308 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1311 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1312 if (delegation != NULL &&
1313 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1319 /* Update sequence id. */
1320 data->o_arg.id = sp->so_owner_id.id;
1321 data->o_arg.clientid = sp->so_client->cl_clientid;
1322 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1323 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1324 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1326 data->timestamp = jiffies;
1327 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1328 &data->o_arg.seq_args,
1329 &data->o_res.seq_res, 1, task))
1331 rpc_call_start(task);
1334 task->tk_action = NULL;
1338 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1340 struct nfs4_opendata *data = calldata;
1342 data->rpc_status = task->tk_status;
1344 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1347 if (RPC_ASSASSINATED(task))
1349 if (task->tk_status == 0) {
1350 switch (data->o_res.f_attr->mode & S_IFMT) {
1354 data->rpc_status = -ELOOP;
1357 data->rpc_status = -EISDIR;
1360 data->rpc_status = -ENOTDIR;
1362 renew_lease(data->o_res.server, data->timestamp);
1363 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1364 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1369 static void nfs4_open_release(void *calldata)
1371 struct nfs4_opendata *data = calldata;
1372 struct nfs4_state *state = NULL;
1374 /* If this request hasn't been cancelled, do nothing */
1375 if (data->cancelled == 0)
1377 /* In case of error, no cleanup! */
1378 if (data->rpc_status != 0 || !data->rpc_done)
1380 /* In case we need an open_confirm, no cleanup! */
1381 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1383 state = nfs4_opendata_to_nfs4_state(data);
1385 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1387 nfs4_opendata_put(data);
1390 static const struct rpc_call_ops nfs4_open_ops = {
1391 .rpc_call_prepare = nfs4_open_prepare,
1392 .rpc_call_done = nfs4_open_done,
1393 .rpc_release = nfs4_open_release,
1397 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1399 static int _nfs4_proc_open(struct nfs4_opendata *data)
1401 struct inode *dir = data->dir->d_inode;
1402 struct nfs_server *server = NFS_SERVER(dir);
1403 struct nfs_openargs *o_arg = &data->o_arg;
1404 struct nfs_openres *o_res = &data->o_res;
1405 struct rpc_task *task;
1406 struct rpc_message msg = {
1407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1410 .rpc_cred = data->owner->so_cred,
1412 struct rpc_task_setup task_setup_data = {
1413 .rpc_client = server->client,
1414 .rpc_message = &msg,
1415 .callback_ops = &nfs4_open_ops,
1416 .callback_data = data,
1417 .workqueue = nfsiod_workqueue,
1418 .flags = RPC_TASK_ASYNC,
1422 kref_get(&data->kref);
1424 data->rpc_status = 0;
1425 data->cancelled = 0;
1426 task = rpc_run_task(&task_setup_data);
1428 return PTR_ERR(task);
1429 status = nfs4_wait_for_completion_rpc_task(task);
1431 data->cancelled = 1;
1434 status = data->rpc_status;
1436 if (status != 0 || !data->rpc_done)
1439 if (o_res->fh.size == 0)
1440 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1442 if (o_arg->open_flags & O_CREAT) {
1443 update_changeattr(dir, &o_res->cinfo);
1444 nfs_post_op_update_inode(dir, o_res->dir_attr);
1446 nfs_refresh_inode(dir, o_res->dir_attr);
1447 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1448 status = _nfs4_proc_open_confirm(data);
1452 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1453 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1457 static int nfs4_recover_expired_lease(struct nfs_server *server)
1459 struct nfs_client *clp = server->nfs_client;
1463 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1464 ret = nfs4_wait_clnt_recover(clp);
1467 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1468 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1470 nfs4_schedule_state_recovery(clp);
1478 * reclaim state on the server after a network partition.
1479 * Assumes caller holds the appropriate lock
1481 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1483 struct nfs4_opendata *opendata;
1486 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1487 if (IS_ERR(opendata))
1488 return PTR_ERR(opendata);
1489 ret = nfs4_open_recover(opendata, state);
1491 d_drop(ctx->path.dentry);
1492 nfs4_opendata_put(opendata);
1496 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1498 struct nfs_server *server = NFS_SERVER(state->inode);
1499 struct nfs4_exception exception = { };
1503 err = _nfs4_open_expired(ctx, state);
1507 case -NFS4ERR_GRACE:
1508 case -NFS4ERR_DELAY:
1509 nfs4_handle_exception(server, err, &exception);
1512 } while (exception.retry);
1517 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1519 struct nfs_open_context *ctx;
1522 ctx = nfs4_state_find_open_context(state);
1524 return PTR_ERR(ctx);
1525 ret = nfs4_do_open_expired(ctx, state);
1526 put_nfs_open_context(ctx);
1531 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1532 * fields corresponding to attributes that were used to store the verifier.
1533 * Make sure we clobber those fields in the later setattr call
1535 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1537 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1538 !(sattr->ia_valid & ATTR_ATIME_SET))
1539 sattr->ia_valid |= ATTR_ATIME;
1541 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1542 !(sattr->ia_valid & ATTR_MTIME_SET))
1543 sattr->ia_valid |= ATTR_MTIME;
1547 * Returns a referenced nfs4_state
1549 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)
1551 struct nfs4_state_owner *sp;
1552 struct nfs4_state *state = NULL;
1553 struct nfs_server *server = NFS_SERVER(dir);
1554 struct nfs4_opendata *opendata;
1557 /* Protect against reboot recovery conflicts */
1559 if (!(sp = nfs4_get_state_owner(server, cred))) {
1560 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1563 status = nfs4_recover_expired_lease(server);
1565 goto err_put_state_owner;
1566 if (path->dentry->d_inode != NULL)
1567 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1569 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1570 if (opendata == NULL)
1571 goto err_put_state_owner;
1573 if (path->dentry->d_inode != NULL)
1574 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1576 status = _nfs4_proc_open(opendata);
1578 goto err_opendata_put;
1580 if (opendata->o_arg.open_flags & O_EXCL)
1581 nfs4_exclusive_attrset(opendata, sattr);
1583 state = nfs4_opendata_to_nfs4_state(opendata);
1584 status = PTR_ERR(state);
1586 goto err_opendata_put;
1587 nfs4_opendata_put(opendata);
1588 nfs4_put_state_owner(sp);
1592 nfs4_opendata_put(opendata);
1593 err_put_state_owner:
1594 nfs4_put_state_owner(sp);
1601 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)
1603 struct nfs4_exception exception = { };
1604 struct nfs4_state *res;
1608 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1611 /* NOTE: BAD_SEQID means the server and client disagree about the
1612 * book-keeping w.r.t. state-changing operations
1613 * (OPEN/CLOSE/LOCK/LOCKU...)
1614 * It is actually a sign of a bug on the client or on the server.
1616 * If we receive a BAD_SEQID error in the particular case of
1617 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1618 * have unhashed the old state_owner for us, and that we can
1619 * therefore safely retry using a new one. We should still warn
1620 * the user though...
1622 if (status == -NFS4ERR_BAD_SEQID) {
1623 printk(KERN_WARNING "NFS: v4 server %s "
1624 " returned a bad sequence-id error!\n",
1625 NFS_SERVER(dir)->nfs_client->cl_hostname);
1626 exception.retry = 1;
1630 * BAD_STATEID on OPEN means that the server cancelled our
1631 * state before it received the OPEN_CONFIRM.
1632 * Recover by retrying the request as per the discussion
1633 * on Page 181 of RFC3530.
1635 if (status == -NFS4ERR_BAD_STATEID) {
1636 exception.retry = 1;
1639 if (status == -EAGAIN) {
1640 /* We must have found a delegation */
1641 exception.retry = 1;
1644 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1645 status, &exception));
1646 } while (exception.retry);
1650 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1651 struct nfs_fattr *fattr, struct iattr *sattr,
1652 struct nfs4_state *state)
1654 struct nfs_server *server = NFS_SERVER(inode);
1655 struct nfs_setattrargs arg = {
1656 .fh = NFS_FH(inode),
1659 .bitmask = server->attr_bitmask,
1661 struct nfs_setattrres res = {
1665 struct rpc_message msg = {
1666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1671 unsigned long timestamp = jiffies;
1674 nfs_fattr_init(fattr);
1676 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1677 /* Use that stateid */
1678 } else if (state != NULL) {
1679 nfs4_copy_stateid(&arg.stateid, state, current->files);
1681 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1683 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1684 if (status == 0 && state != NULL)
1685 renew_lease(server, timestamp);
1689 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1690 struct nfs_fattr *fattr, struct iattr *sattr,
1691 struct nfs4_state *state)
1693 struct nfs_server *server = NFS_SERVER(inode);
1694 struct nfs4_exception exception = { };
1697 err = nfs4_handle_exception(server,
1698 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1700 } while (exception.retry);
1704 struct nfs4_closedata {
1706 struct inode *inode;
1707 struct nfs4_state *state;
1708 struct nfs_closeargs arg;
1709 struct nfs_closeres res;
1710 struct nfs_fattr fattr;
1711 unsigned long timestamp;
1714 static void nfs4_free_closedata(void *data)
1716 struct nfs4_closedata *calldata = data;
1717 struct nfs4_state_owner *sp = calldata->state->owner;
1719 nfs4_put_open_state(calldata->state);
1720 nfs_free_seqid(calldata->arg.seqid);
1721 nfs4_put_state_owner(sp);
1722 path_put(&calldata->path);
1726 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1729 spin_lock(&state->owner->so_lock);
1730 if (!(fmode & FMODE_READ))
1731 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1732 if (!(fmode & FMODE_WRITE))
1733 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1734 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1735 spin_unlock(&state->owner->so_lock);
1738 static void nfs4_close_done(struct rpc_task *task, void *data)
1740 struct nfs4_closedata *calldata = data;
1741 struct nfs4_state *state = calldata->state;
1742 struct nfs_server *server = NFS_SERVER(calldata->inode);
1744 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1745 if (RPC_ASSASSINATED(task))
1747 /* hmm. we are done with the inode, and in the process of freeing
1748 * the state_owner. we keep this around to process errors
1750 switch (task->tk_status) {
1752 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1753 renew_lease(server, calldata->timestamp);
1754 nfs4_close_clear_stateid_flags(state,
1755 calldata->arg.fmode);
1757 case -NFS4ERR_STALE_STATEID:
1758 case -NFS4ERR_OLD_STATEID:
1759 case -NFS4ERR_BAD_STATEID:
1760 case -NFS4ERR_EXPIRED:
1761 if (calldata->arg.fmode == 0)
1764 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1765 nfs_restart_rpc(task, server->nfs_client);
1769 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1772 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1774 struct nfs4_closedata *calldata = data;
1775 struct nfs4_state *state = calldata->state;
1778 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1781 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1782 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1783 spin_lock(&state->owner->so_lock);
1784 /* Calculate the change in open mode */
1785 if (state->n_rdwr == 0) {
1786 if (state->n_rdonly == 0) {
1787 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1788 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1789 calldata->arg.fmode &= ~FMODE_READ;
1791 if (state->n_wronly == 0) {
1792 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1793 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1794 calldata->arg.fmode &= ~FMODE_WRITE;
1797 spin_unlock(&state->owner->so_lock);
1800 /* Note: exit _without_ calling nfs4_close_done */
1801 task->tk_action = NULL;
1805 if (calldata->arg.fmode == 0)
1806 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1808 nfs_fattr_init(calldata->res.fattr);
1809 calldata->timestamp = jiffies;
1810 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1811 &calldata->arg.seq_args, &calldata->res.seq_res,
1814 rpc_call_start(task);
1817 static const struct rpc_call_ops nfs4_close_ops = {
1818 .rpc_call_prepare = nfs4_close_prepare,
1819 .rpc_call_done = nfs4_close_done,
1820 .rpc_release = nfs4_free_closedata,
1824 * It is possible for data to be read/written from a mem-mapped file
1825 * after the sys_close call (which hits the vfs layer as a flush).
1826 * This means that we can't safely call nfsv4 close on a file until
1827 * the inode is cleared. This in turn means that we are not good
1828 * NFSv4 citizens - we do not indicate to the server to update the file's
1829 * share state even when we are done with one of the three share
1830 * stateid's in the inode.
1832 * NOTE: Caller must be holding the sp->so_owner semaphore!
1834 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1836 struct nfs_server *server = NFS_SERVER(state->inode);
1837 struct nfs4_closedata *calldata;
1838 struct nfs4_state_owner *sp = state->owner;
1839 struct rpc_task *task;
1840 struct rpc_message msg = {
1841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1842 .rpc_cred = state->owner->so_cred,
1844 struct rpc_task_setup task_setup_data = {
1845 .rpc_client = server->client,
1846 .rpc_message = &msg,
1847 .callback_ops = &nfs4_close_ops,
1848 .workqueue = nfsiod_workqueue,
1849 .flags = RPC_TASK_ASYNC,
1851 int status = -ENOMEM;
1853 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1854 if (calldata == NULL)
1856 calldata->inode = state->inode;
1857 calldata->state = state;
1858 calldata->arg.fh = NFS_FH(state->inode);
1859 calldata->arg.stateid = &state->open_stateid;
1860 if (nfs4_has_session(server->nfs_client))
1861 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
1862 /* Serialization for the sequence id */
1863 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1864 if (calldata->arg.seqid == NULL)
1865 goto out_free_calldata;
1866 calldata->arg.fmode = 0;
1867 calldata->arg.bitmask = server->cache_consistency_bitmask;
1868 calldata->res.fattr = &calldata->fattr;
1869 calldata->res.seqid = calldata->arg.seqid;
1870 calldata->res.server = server;
1871 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1872 calldata->path.mnt = mntget(path->mnt);
1873 calldata->path.dentry = dget(path->dentry);
1875 msg.rpc_argp = &calldata->arg,
1876 msg.rpc_resp = &calldata->res,
1877 task_setup_data.callback_data = calldata;
1878 task = rpc_run_task(&task_setup_data);
1880 return PTR_ERR(task);
1883 status = rpc_wait_for_completion_task(task);
1889 nfs4_put_open_state(state);
1890 nfs4_put_state_owner(sp);
1894 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1899 /* If the open_intent is for execute, we have an extra check to make */
1900 if (fmode & FMODE_EXEC) {
1901 ret = nfs_may_open(state->inode,
1902 state->owner->so_cred,
1903 nd->intent.open.flags);
1907 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1908 if (!IS_ERR(filp)) {
1909 struct nfs_open_context *ctx;
1910 ctx = nfs_file_open_context(filp);
1914 ret = PTR_ERR(filp);
1916 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1921 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1923 struct path path = {
1924 .mnt = nd->path.mnt,
1927 struct dentry *parent;
1929 struct rpc_cred *cred;
1930 struct nfs4_state *state;
1932 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1934 if (nd->flags & LOOKUP_CREATE) {
1935 attr.ia_mode = nd->intent.open.create_mode;
1936 attr.ia_valid = ATTR_MODE;
1937 if (!IS_POSIXACL(dir))
1938 attr.ia_mode &= ~current_umask();
1941 BUG_ON(nd->intent.open.flags & O_CREAT);
1944 cred = rpc_lookup_cred();
1946 return (struct dentry *)cred;
1947 parent = dentry->d_parent;
1948 /* Protect against concurrent sillydeletes */
1949 nfs_block_sillyrename(parent);
1950 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1952 if (IS_ERR(state)) {
1953 if (PTR_ERR(state) == -ENOENT) {
1954 d_add(dentry, NULL);
1955 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1957 nfs_unblock_sillyrename(parent);
1958 return (struct dentry *)state;
1960 res = d_add_unique(dentry, igrab(state->inode));
1963 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1964 nfs_unblock_sillyrename(parent);
1965 nfs4_intent_set_file(nd, &path, state, fmode);
1970 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1972 struct path path = {
1973 .mnt = nd->path.mnt,
1976 struct rpc_cred *cred;
1977 struct nfs4_state *state;
1978 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1980 cred = rpc_lookup_cred();
1982 return PTR_ERR(cred);
1983 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1985 if (IS_ERR(state)) {
1986 switch (PTR_ERR(state)) {
1992 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1998 if (state->inode == dentry->d_inode) {
1999 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2000 nfs4_intent_set_file(nd, &path, state, fmode);
2003 nfs4_close_sync(&path, state, fmode);
2009 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2011 if (ctx->state == NULL)
2014 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2016 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2019 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2021 struct nfs4_server_caps_arg args = {
2024 struct nfs4_server_caps_res res = {};
2025 struct rpc_message msg = {
2026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2032 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2034 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2035 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2036 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2037 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2038 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2039 NFS_CAP_CTIME|NFS_CAP_MTIME);
2040 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2041 server->caps |= NFS_CAP_ACLS;
2042 if (res.has_links != 0)
2043 server->caps |= NFS_CAP_HARDLINKS;
2044 if (res.has_symlinks != 0)
2045 server->caps |= NFS_CAP_SYMLINKS;
2046 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2047 server->caps |= NFS_CAP_FILEID;
2048 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2049 server->caps |= NFS_CAP_MODE;
2050 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2051 server->caps |= NFS_CAP_NLINK;
2052 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2053 server->caps |= NFS_CAP_OWNER;
2054 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2055 server->caps |= NFS_CAP_OWNER_GROUP;
2056 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2057 server->caps |= NFS_CAP_ATIME;
2058 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2059 server->caps |= NFS_CAP_CTIME;
2060 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2061 server->caps |= NFS_CAP_MTIME;
2063 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2064 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2065 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2066 server->acl_bitmask = res.acl_bitmask;
2072 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2074 struct nfs4_exception exception = { };
2077 err = nfs4_handle_exception(server,
2078 _nfs4_server_capabilities(server, fhandle),
2080 } while (exception.retry);
2084 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2085 struct nfs_fsinfo *info)
2087 struct nfs4_lookup_root_arg args = {
2088 .bitmask = nfs4_fattr_bitmap,
2090 struct nfs4_lookup_res res = {
2092 .fattr = info->fattr,
2095 struct rpc_message msg = {
2096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2101 nfs_fattr_init(info->fattr);
2102 return nfs4_call_sync(server, &msg, &args, &res, 0);
2105 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2106 struct nfs_fsinfo *info)
2108 struct nfs4_exception exception = { };
2111 err = nfs4_handle_exception(server,
2112 _nfs4_lookup_root(server, fhandle, info),
2114 } while (exception.retry);
2119 * get the file handle for the "/" directory on the server
2121 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2122 struct nfs_fsinfo *info)
2126 status = nfs4_lookup_root(server, fhandle, info);
2128 status = nfs4_server_capabilities(server, fhandle);
2130 status = nfs4_do_fsinfo(server, fhandle, info);
2131 return nfs4_map_errors(status);
2135 * Get locations and (maybe) other attributes of a referral.
2136 * Note that we'll actually follow the referral later when
2137 * we detect fsid mismatch in inode revalidation
2139 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2141 int status = -ENOMEM;
2142 struct page *page = NULL;
2143 struct nfs4_fs_locations *locations = NULL;
2145 page = alloc_page(GFP_KERNEL);
2148 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2149 if (locations == NULL)
2152 status = nfs4_proc_fs_locations(dir, name, locations, page);
2155 /* Make sure server returned a different fsid for the referral */
2156 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2157 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2162 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2163 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2165 fattr->mode = S_IFDIR;
2166 memset(fhandle, 0, sizeof(struct nfs_fh));
2175 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2177 struct nfs4_getattr_arg args = {
2179 .bitmask = server->attr_bitmask,
2181 struct nfs4_getattr_res res = {
2185 struct rpc_message msg = {
2186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2191 nfs_fattr_init(fattr);
2192 return nfs4_call_sync(server, &msg, &args, &res, 0);
2195 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2197 struct nfs4_exception exception = { };
2200 err = nfs4_handle_exception(server,
2201 _nfs4_proc_getattr(server, fhandle, fattr),
2203 } while (exception.retry);
2208 * The file is not closed if it is opened due to the a request to change
2209 * the size of the file. The open call will not be needed once the
2210 * VFS layer lookup-intents are implemented.
2212 * Close is called when the inode is destroyed.
2213 * If we haven't opened the file for O_WRONLY, we
2214 * need to in the size_change case to obtain a stateid.
2217 * Because OPEN is always done by name in nfsv4, it is
2218 * possible that we opened a different file by the same
2219 * name. We can recognize this race condition, but we
2220 * can't do anything about it besides returning an error.
2222 * This will be fixed with VFS changes (lookup-intent).
2225 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2226 struct iattr *sattr)
2228 struct inode *inode = dentry->d_inode;
2229 struct rpc_cred *cred = NULL;
2230 struct nfs4_state *state = NULL;
2233 nfs_fattr_init(fattr);
2235 /* Search for an existing open(O_WRITE) file */
2236 if (sattr->ia_valid & ATTR_FILE) {
2237 struct nfs_open_context *ctx;
2239 ctx = nfs_file_open_context(sattr->ia_file);
2246 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2248 nfs_setattr_update_inode(inode, sattr);
2252 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2253 const struct qstr *name, struct nfs_fh *fhandle,
2254 struct nfs_fattr *fattr)
2257 struct nfs4_lookup_arg args = {
2258 .bitmask = server->attr_bitmask,
2262 struct nfs4_lookup_res res = {
2267 struct rpc_message msg = {
2268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2273 nfs_fattr_init(fattr);
2275 dprintk("NFS call lookupfh %s\n", name->name);
2276 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2277 dprintk("NFS reply lookupfh: %d\n", status);
2281 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2282 struct qstr *name, struct nfs_fh *fhandle,
2283 struct nfs_fattr *fattr)
2285 struct nfs4_exception exception = { };
2288 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2290 if (err == -NFS4ERR_MOVED) {
2294 err = nfs4_handle_exception(server, err, &exception);
2295 } while (exception.retry);
2299 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2300 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2304 dprintk("NFS call lookup %s\n", name->name);
2305 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2306 if (status == -NFS4ERR_MOVED)
2307 status = nfs4_get_referral(dir, name, fattr, fhandle);
2308 dprintk("NFS reply lookup: %d\n", status);
2312 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2314 struct nfs4_exception exception = { };
2317 err = nfs4_handle_exception(NFS_SERVER(dir),
2318 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2320 } while (exception.retry);
2324 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2326 struct nfs_server *server = NFS_SERVER(inode);
2327 struct nfs_fattr fattr;
2328 struct nfs4_accessargs args = {
2329 .fh = NFS_FH(inode),
2330 .bitmask = server->attr_bitmask,
2332 struct nfs4_accessres res = {
2336 struct rpc_message msg = {
2337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2340 .rpc_cred = entry->cred,
2342 int mode = entry->mask;
2346 * Determine which access bits we want to ask for...
2348 if (mode & MAY_READ)
2349 args.access |= NFS4_ACCESS_READ;
2350 if (S_ISDIR(inode->i_mode)) {
2351 if (mode & MAY_WRITE)
2352 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2353 if (mode & MAY_EXEC)
2354 args.access |= NFS4_ACCESS_LOOKUP;
2356 if (mode & MAY_WRITE)
2357 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2358 if (mode & MAY_EXEC)
2359 args.access |= NFS4_ACCESS_EXECUTE;
2361 nfs_fattr_init(&fattr);
2362 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2365 if (res.access & NFS4_ACCESS_READ)
2366 entry->mask |= MAY_READ;
2367 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2368 entry->mask |= MAY_WRITE;
2369 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2370 entry->mask |= MAY_EXEC;
2371 nfs_refresh_inode(inode, &fattr);
2376 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2378 struct nfs4_exception exception = { };
2381 err = nfs4_handle_exception(NFS_SERVER(inode),
2382 _nfs4_proc_access(inode, entry),
2384 } while (exception.retry);
2389 * TODO: For the time being, we don't try to get any attributes
2390 * along with any of the zero-copy operations READ, READDIR,
2393 * In the case of the first three, we want to put the GETATTR
2394 * after the read-type operation -- this is because it is hard
2395 * to predict the length of a GETATTR response in v4, and thus
2396 * align the READ data correctly. This means that the GETATTR
2397 * may end up partially falling into the page cache, and we should
2398 * shift it into the 'tail' of the xdr_buf before processing.
2399 * To do this efficiently, we need to know the total length
2400 * of data received, which doesn't seem to be available outside
2403 * In the case of WRITE, we also want to put the GETATTR after
2404 * the operation -- in this case because we want to make sure
2405 * we get the post-operation mtime and size. This means that
2406 * we can't use xdr_encode_pages() as written: we need a variant
2407 * of it which would leave room in the 'tail' iovec.
2409 * Both of these changes to the XDR layer would in fact be quite
2410 * minor, but I decided to leave them for a subsequent patch.
2412 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2413 unsigned int pgbase, unsigned int pglen)
2415 struct nfs4_readlink args = {
2416 .fh = NFS_FH(inode),
2421 struct nfs4_readlink_res res;
2422 struct rpc_message msg = {
2423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2428 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2431 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2432 unsigned int pgbase, unsigned int pglen)
2434 struct nfs4_exception exception = { };
2437 err = nfs4_handle_exception(NFS_SERVER(inode),
2438 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2440 } while (exception.retry);
2446 * We will need to arrange for the VFS layer to provide an atomic open.
2447 * Until then, this create/open method is prone to inefficiency and race
2448 * conditions due to the lookup, create, and open VFS calls from sys_open()
2449 * placed on the wire.
2451 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2452 * The file will be opened again in the subsequent VFS open call
2453 * (nfs4_proc_file_open).
2455 * The open for read will just hang around to be used by any process that
2456 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2460 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2461 int flags, struct nameidata *nd)
2463 struct path path = {
2464 .mnt = nd->path.mnt,
2467 struct nfs4_state *state;
2468 struct rpc_cred *cred;
2469 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2472 cred = rpc_lookup_cred();
2474 status = PTR_ERR(cred);
2477 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2479 if (IS_ERR(state)) {
2480 status = PTR_ERR(state);
2483 d_add(dentry, igrab(state->inode));
2484 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2485 if (flags & O_EXCL) {
2486 struct nfs_fattr fattr;
2487 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2489 nfs_setattr_update_inode(state->inode, sattr);
2490 nfs_post_op_update_inode(state->inode, &fattr);
2492 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2493 status = nfs4_intent_set_file(nd, &path, state, fmode);
2495 nfs4_close_sync(&path, state, fmode);
2502 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2504 struct nfs_server *server = NFS_SERVER(dir);
2505 struct nfs_removeargs args = {
2507 .name.len = name->len,
2508 .name.name = name->name,
2509 .bitmask = server->attr_bitmask,
2511 struct nfs_removeres res = {
2514 struct rpc_message msg = {
2515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2521 nfs_fattr_init(&res.dir_attr);
2522 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2524 update_changeattr(dir, &res.cinfo);
2525 nfs_post_op_update_inode(dir, &res.dir_attr);
2530 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2532 struct nfs4_exception exception = { };
2535 err = nfs4_handle_exception(NFS_SERVER(dir),
2536 _nfs4_proc_remove(dir, name),
2538 } while (exception.retry);
2542 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2544 struct nfs_server *server = NFS_SERVER(dir);
2545 struct nfs_removeargs *args = msg->rpc_argp;
2546 struct nfs_removeres *res = msg->rpc_resp;
2548 args->bitmask = server->cache_consistency_bitmask;
2549 res->server = server;
2550 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2553 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2555 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2557 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2558 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2560 update_changeattr(dir, &res->cinfo);
2561 nfs_post_op_update_inode(dir, &res->dir_attr);
2565 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2566 struct inode *new_dir, struct qstr *new_name)
2568 struct nfs_server *server = NFS_SERVER(old_dir);
2569 struct nfs4_rename_arg arg = {
2570 .old_dir = NFS_FH(old_dir),
2571 .new_dir = NFS_FH(new_dir),
2572 .old_name = old_name,
2573 .new_name = new_name,
2574 .bitmask = server->attr_bitmask,
2576 struct nfs_fattr old_fattr, new_fattr;
2577 struct nfs4_rename_res res = {
2579 .old_fattr = &old_fattr,
2580 .new_fattr = &new_fattr,
2582 struct rpc_message msg = {
2583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2589 nfs_fattr_init(res.old_fattr);
2590 nfs_fattr_init(res.new_fattr);
2591 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2594 update_changeattr(old_dir, &res.old_cinfo);
2595 nfs_post_op_update_inode(old_dir, res.old_fattr);
2596 update_changeattr(new_dir, &res.new_cinfo);
2597 nfs_post_op_update_inode(new_dir, res.new_fattr);
2602 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2603 struct inode *new_dir, struct qstr *new_name)
2605 struct nfs4_exception exception = { };
2608 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2609 _nfs4_proc_rename(old_dir, old_name,
2612 } while (exception.retry);
2616 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2618 struct nfs_server *server = NFS_SERVER(inode);
2619 struct nfs4_link_arg arg = {
2620 .fh = NFS_FH(inode),
2621 .dir_fh = NFS_FH(dir),
2623 .bitmask = server->attr_bitmask,
2625 struct nfs_fattr fattr, dir_attr;
2626 struct nfs4_link_res res = {
2629 .dir_attr = &dir_attr,
2631 struct rpc_message msg = {
2632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2638 nfs_fattr_init(res.fattr);
2639 nfs_fattr_init(res.dir_attr);
2640 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2642 update_changeattr(dir, &res.cinfo);
2643 nfs_post_op_update_inode(dir, res.dir_attr);
2644 nfs_post_op_update_inode(inode, res.fattr);
2650 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2652 struct nfs4_exception exception = { };
2655 err = nfs4_handle_exception(NFS_SERVER(inode),
2656 _nfs4_proc_link(inode, dir, name),
2658 } while (exception.retry);
2662 struct nfs4_createdata {
2663 struct rpc_message msg;
2664 struct nfs4_create_arg arg;
2665 struct nfs4_create_res res;
2667 struct nfs_fattr fattr;
2668 struct nfs_fattr dir_fattr;
2671 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2672 struct qstr *name, struct iattr *sattr, u32 ftype)
2674 struct nfs4_createdata *data;
2676 data = kzalloc(sizeof(*data), GFP_KERNEL);
2678 struct nfs_server *server = NFS_SERVER(dir);
2680 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2681 data->msg.rpc_argp = &data->arg;
2682 data->msg.rpc_resp = &data->res;
2683 data->arg.dir_fh = NFS_FH(dir);
2684 data->arg.server = server;
2685 data->arg.name = name;
2686 data->arg.attrs = sattr;
2687 data->arg.ftype = ftype;
2688 data->arg.bitmask = server->attr_bitmask;
2689 data->res.server = server;
2690 data->res.fh = &data->fh;
2691 data->res.fattr = &data->fattr;
2692 data->res.dir_fattr = &data->dir_fattr;
2693 nfs_fattr_init(data->res.fattr);
2694 nfs_fattr_init(data->res.dir_fattr);
2699 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2701 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2702 &data->arg, &data->res, 1);
2704 update_changeattr(dir, &data->res.dir_cinfo);
2705 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2706 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2711 static void nfs4_free_createdata(struct nfs4_createdata *data)
2716 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2717 struct page *page, unsigned int len, struct iattr *sattr)
2719 struct nfs4_createdata *data;
2720 int status = -ENAMETOOLONG;
2722 if (len > NFS4_MAXPATHLEN)
2726 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2730 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2731 data->arg.u.symlink.pages = &page;
2732 data->arg.u.symlink.len = len;
2734 status = nfs4_do_create(dir, dentry, data);
2736 nfs4_free_createdata(data);
2741 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2742 struct page *page, unsigned int len, struct iattr *sattr)
2744 struct nfs4_exception exception = { };
2747 err = nfs4_handle_exception(NFS_SERVER(dir),
2748 _nfs4_proc_symlink(dir, dentry, page,
2751 } while (exception.retry);
2755 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2756 struct iattr *sattr)
2758 struct nfs4_createdata *data;
2759 int status = -ENOMEM;
2761 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2765 status = nfs4_do_create(dir, dentry, data);
2767 nfs4_free_createdata(data);
2772 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2773 struct iattr *sattr)
2775 struct nfs4_exception exception = { };
2778 err = nfs4_handle_exception(NFS_SERVER(dir),
2779 _nfs4_proc_mkdir(dir, dentry, sattr),
2781 } while (exception.retry);
2785 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2786 u64 cookie, struct page *page, unsigned int count, int plus)
2788 struct inode *dir = dentry->d_inode;
2789 struct nfs4_readdir_arg args = {
2794 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2796 struct nfs4_readdir_res res;
2797 struct rpc_message msg = {
2798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2805 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2806 dentry->d_parent->d_name.name,
2807 dentry->d_name.name,
2808 (unsigned long long)cookie);
2809 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2810 res.pgbase = args.pgbase;
2811 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2813 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2815 nfs_invalidate_atime(dir);
2817 dprintk("%s: returns %d\n", __func__, status);
2821 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2822 u64 cookie, struct page *page, unsigned int count, int plus)
2824 struct nfs4_exception exception = { };
2827 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2828 _nfs4_proc_readdir(dentry, cred, cookie,
2831 } while (exception.retry);
2835 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2836 struct iattr *sattr, dev_t rdev)
2838 struct nfs4_createdata *data;
2839 int mode = sattr->ia_mode;
2840 int status = -ENOMEM;
2842 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2843 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2845 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2850 data->arg.ftype = NF4FIFO;
2851 else if (S_ISBLK(mode)) {
2852 data->arg.ftype = NF4BLK;
2853 data->arg.u.device.specdata1 = MAJOR(rdev);
2854 data->arg.u.device.specdata2 = MINOR(rdev);
2856 else if (S_ISCHR(mode)) {
2857 data->arg.ftype = NF4CHR;
2858 data->arg.u.device.specdata1 = MAJOR(rdev);
2859 data->arg.u.device.specdata2 = MINOR(rdev);
2862 status = nfs4_do_create(dir, dentry, data);
2864 nfs4_free_createdata(data);
2869 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2870 struct iattr *sattr, dev_t rdev)
2872 struct nfs4_exception exception = { };
2875 err = nfs4_handle_exception(NFS_SERVER(dir),
2876 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2878 } while (exception.retry);
2882 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2883 struct nfs_fsstat *fsstat)
2885 struct nfs4_statfs_arg args = {
2887 .bitmask = server->attr_bitmask,
2889 struct nfs4_statfs_res res = {
2892 struct rpc_message msg = {
2893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2898 nfs_fattr_init(fsstat->fattr);
2899 return nfs4_call_sync(server, &msg, &args, &res, 0);
2902 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2904 struct nfs4_exception exception = { };
2907 err = nfs4_handle_exception(server,
2908 _nfs4_proc_statfs(server, fhandle, fsstat),
2910 } while (exception.retry);
2914 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2915 struct nfs_fsinfo *fsinfo)
2917 struct nfs4_fsinfo_arg args = {
2919 .bitmask = server->attr_bitmask,
2921 struct nfs4_fsinfo_res res = {
2924 struct rpc_message msg = {
2925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2930 return nfs4_call_sync(server, &msg, &args, &res, 0);
2933 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2935 struct nfs4_exception exception = { };
2939 err = nfs4_handle_exception(server,
2940 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2942 } while (exception.retry);
2946 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2948 nfs_fattr_init(fsinfo->fattr);
2949 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2952 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2953 struct nfs_pathconf *pathconf)
2955 struct nfs4_pathconf_arg args = {
2957 .bitmask = server->attr_bitmask,
2959 struct nfs4_pathconf_res res = {
2960 .pathconf = pathconf,
2962 struct rpc_message msg = {
2963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2968 /* None of the pathconf attributes are mandatory to implement */
2969 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2970 memset(pathconf, 0, sizeof(*pathconf));
2974 nfs_fattr_init(pathconf->fattr);
2975 return nfs4_call_sync(server, &msg, &args, &res, 0);
2978 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2979 struct nfs_pathconf *pathconf)
2981 struct nfs4_exception exception = { };
2985 err = nfs4_handle_exception(server,
2986 _nfs4_proc_pathconf(server, fhandle, pathconf),
2988 } while (exception.retry);
2992 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2994 struct nfs_server *server = NFS_SERVER(data->inode);
2996 dprintk("--> %s\n", __func__);
2998 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3000 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3001 nfs_restart_rpc(task, server->nfs_client);
3005 nfs_invalidate_atime(data->inode);
3006 if (task->tk_status > 0)
3007 renew_lease(server, data->timestamp);
3011 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3013 data->timestamp = jiffies;
3014 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3017 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3019 struct inode *inode = data->inode;
3021 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3024 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3025 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3028 if (task->tk_status >= 0) {
3029 renew_lease(NFS_SERVER(inode), data->timestamp);
3030 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3035 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3037 struct nfs_server *server = NFS_SERVER(data->inode);
3039 data->args.bitmask = server->cache_consistency_bitmask;
3040 data->res.server = server;
3041 data->timestamp = jiffies;
3043 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3046 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3048 struct inode *inode = data->inode;
3050 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3052 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3053 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3056 nfs_refresh_inode(inode, data->res.fattr);
3060 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3062 struct nfs_server *server = NFS_SERVER(data->inode);
3064 data->args.bitmask = server->cache_consistency_bitmask;
3065 data->res.server = server;
3066 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3070 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3071 * standalone procedure for queueing an asynchronous RENEW.
3073 static void nfs4_renew_done(struct rpc_task *task, void *data)
3075 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
3076 unsigned long timestamp = (unsigned long)data;
3078 if (task->tk_status < 0) {
3079 /* Unless we're shutting down, schedule state recovery! */
3080 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3081 nfs4_schedule_state_recovery(clp);
3084 spin_lock(&clp->cl_lock);
3085 if (time_before(clp->cl_last_renewal,timestamp))
3086 clp->cl_last_renewal = timestamp;
3087 spin_unlock(&clp->cl_lock);
3090 static const struct rpc_call_ops nfs4_renew_ops = {
3091 .rpc_call_done = nfs4_renew_done,
3094 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3096 struct rpc_message msg = {
3097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3102 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3103 &nfs4_renew_ops, (void *)jiffies);
3106 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3108 struct rpc_message msg = {
3109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3113 unsigned long now = jiffies;
3116 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3119 spin_lock(&clp->cl_lock);
3120 if (time_before(clp->cl_last_renewal,now))
3121 clp->cl_last_renewal = now;
3122 spin_unlock(&clp->cl_lock);
3126 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3128 return (server->caps & NFS_CAP_ACLS)
3129 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3130 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3133 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3134 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3137 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3139 static void buf_to_pages(const void *buf, size_t buflen,
3140 struct page **pages, unsigned int *pgbase)
3142 const void *p = buf;
3144 *pgbase = offset_in_page(buf);
3146 while (p < buf + buflen) {
3147 *(pages++) = virt_to_page(p);
3148 p += PAGE_CACHE_SIZE;
3152 struct nfs4_cached_acl {
3158 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3160 struct nfs_inode *nfsi = NFS_I(inode);
3162 spin_lock(&inode->i_lock);
3163 kfree(nfsi->nfs4_acl);
3164 nfsi->nfs4_acl = acl;
3165 spin_unlock(&inode->i_lock);
3168 static void nfs4_zap_acl_attr(struct inode *inode)
3170 nfs4_set_cached_acl(inode, NULL);
3173 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3175 struct nfs_inode *nfsi = NFS_I(inode);
3176 struct nfs4_cached_acl *acl;
3179 spin_lock(&inode->i_lock);
3180 acl = nfsi->nfs4_acl;
3183 if (buf == NULL) /* user is just asking for length */
3185 if (acl->cached == 0)
3187 ret = -ERANGE; /* see getxattr(2) man page */
3188 if (acl->len > buflen)
3190 memcpy(buf, acl->data, acl->len);
3194 spin_unlock(&inode->i_lock);
3198 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3200 struct nfs4_cached_acl *acl;
3202 if (buf && acl_len <= PAGE_SIZE) {
3203 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3207 memcpy(acl->data, buf, acl_len);
3209 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3216 nfs4_set_cached_acl(inode, acl);
3219 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3221 struct page *pages[NFS4ACL_MAXPAGES];
3222 struct nfs_getaclargs args = {
3223 .fh = NFS_FH(inode),
3227 struct nfs_getaclres res = {
3231 struct rpc_message msg = {
3232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3236 struct page *localpage = NULL;
3239 if (buflen < PAGE_SIZE) {
3240 /* As long as we're doing a round trip to the server anyway,
3241 * let's be prepared for a page of acl data. */
3242 localpage = alloc_page(GFP_KERNEL);
3243 resp_buf = page_address(localpage);
3244 if (localpage == NULL)
3246 args.acl_pages[0] = localpage;
3247 args.acl_pgbase = 0;
3248 args.acl_len = PAGE_SIZE;
3251 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3253 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3256 if (res.acl_len > args.acl_len)
3257 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3259 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3262 if (res.acl_len > buflen)
3265 memcpy(buf, resp_buf, res.acl_len);
3270 __free_page(localpage);
3274 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3276 struct nfs4_exception exception = { };
3279 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3282 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3283 } while (exception.retry);
3287 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3289 struct nfs_server *server = NFS_SERVER(inode);
3292 if (!nfs4_server_supports_acls(server))
3294 ret = nfs_revalidate_inode(server, inode);
3297 ret = nfs4_read_cached_acl(inode, buf, buflen);
3300 return nfs4_get_acl_uncached(inode, buf, buflen);
3303 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3305 struct nfs_server *server = NFS_SERVER(inode);
3306 struct page *pages[NFS4ACL_MAXPAGES];
3307 struct nfs_setaclargs arg = {
3308 .fh = NFS_FH(inode),
3312 struct nfs_setaclres res;
3313 struct rpc_message msg = {
3314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3320 if (!nfs4_server_supports_acls(server))
3322 nfs_inode_return_delegation(inode);
3323 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3324 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3325 nfs_access_zap_cache(inode);
3326 nfs_zap_acl_cache(inode);
3330 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3332 struct nfs4_exception exception = { };
3335 err = nfs4_handle_exception(NFS_SERVER(inode),
3336 __nfs4_proc_set_acl(inode, buf, buflen),
3338 } while (exception.retry);
3343 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3345 if (!clp || task->tk_status >= 0)
3347 switch(task->tk_status) {
3348 case -NFS4ERR_ADMIN_REVOKED:
3349 case -NFS4ERR_BAD_STATEID:
3350 case -NFS4ERR_OPENMODE:
3353 nfs4_state_mark_reclaim_nograce(clp, state);
3354 case -NFS4ERR_STALE_CLIENTID:
3355 case -NFS4ERR_STALE_STATEID:
3356 case -NFS4ERR_EXPIRED:
3357 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3358 nfs4_schedule_state_recovery(clp);
3359 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3360 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3361 task->tk_status = 0;
3363 #if defined(CONFIG_NFS_V4_1)
3364 case -NFS4ERR_BADSESSION:
3365 case -NFS4ERR_BADSLOT:
3366 case -NFS4ERR_BAD_HIGH_SLOT:
3367 case -NFS4ERR_DEADSESSION:
3368 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3369 case -NFS4ERR_SEQ_FALSE_RETRY:
3370 case -NFS4ERR_SEQ_MISORDERED:
3371 dprintk("%s ERROR %d, Reset session\n", __func__,
3373 nfs4_schedule_state_recovery(clp);
3374 task->tk_status = 0;
3376 #endif /* CONFIG_NFS_V4_1 */
3377 case -NFS4ERR_DELAY:
3379 nfs_inc_server_stats(server, NFSIOS_DELAY);
3380 case -NFS4ERR_GRACE:
3381 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3382 task->tk_status = 0;
3384 case -NFS4ERR_OLD_STATEID:
3385 task->tk_status = 0;
3388 task->tk_status = nfs4_map_errors(task->tk_status);
3393 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3395 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3398 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3400 nfs4_verifier sc_verifier;
3401 struct nfs4_setclientid setclientid = {
3402 .sc_verifier = &sc_verifier,
3405 struct rpc_message msg = {
3406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3407 .rpc_argp = &setclientid,
3415 p = (__be32*)sc_verifier.data;
3416 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3417 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3420 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3421 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3423 rpc_peeraddr2str(clp->cl_rpcclient,
3425 rpc_peeraddr2str(clp->cl_rpcclient,
3427 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3428 clp->cl_id_uniquifier);
3429 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3430 sizeof(setclientid.sc_netid),
3431 rpc_peeraddr2str(clp->cl_rpcclient,
3432 RPC_DISPLAY_NETID));
3433 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3434 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3435 clp->cl_ipaddr, port >> 8, port & 255);
3437 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3438 if (status != -NFS4ERR_CLID_INUSE)
3443 ssleep(clp->cl_lease_time + 1);
3445 if (++clp->cl_id_uniquifier == 0)
3451 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3453 struct nfs_fsinfo fsinfo;
3454 struct rpc_message msg = {
3455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3457 .rpc_resp = &fsinfo,
3464 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3466 spin_lock(&clp->cl_lock);
3467 clp->cl_lease_time = fsinfo.lease_time * HZ;
3468 clp->cl_last_renewal = now;
3469 spin_unlock(&clp->cl_lock);
3474 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3479 err = _nfs4_proc_setclientid_confirm(clp, cred);
3483 case -NFS4ERR_RESOURCE:
3484 /* The IBM lawyers misread another document! */
3485 case -NFS4ERR_DELAY:
3486 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3492 struct nfs4_delegreturndata {
3493 struct nfs4_delegreturnargs args;
3494 struct nfs4_delegreturnres res;
3496 nfs4_stateid stateid;
3497 unsigned long timestamp;
3498 struct nfs_fattr fattr;
3502 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3504 struct nfs4_delegreturndata *data = calldata;
3506 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3509 switch (task->tk_status) {
3510 case -NFS4ERR_STALE_STATEID:
3511 case -NFS4ERR_EXPIRED:
3513 renew_lease(data->res.server, data->timestamp);
3516 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3518 nfs_restart_rpc(task, data->res.server->nfs_client);
3522 data->rpc_status = task->tk_status;
3525 static void nfs4_delegreturn_release(void *calldata)
3530 #if defined(CONFIG_NFS_V4_1)
3531 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3533 struct nfs4_delegreturndata *d_data;
3535 d_data = (struct nfs4_delegreturndata *)data;
3537 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3538 &d_data->args.seq_args,
3539 &d_data->res.seq_res, 1, task))
3541 rpc_call_start(task);
3543 #endif /* CONFIG_NFS_V4_1 */
3545 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3546 #if defined(CONFIG_NFS_V4_1)
3547 .rpc_call_prepare = nfs4_delegreturn_prepare,
3548 #endif /* CONFIG_NFS_V4_1 */
3549 .rpc_call_done = nfs4_delegreturn_done,
3550 .rpc_release = nfs4_delegreturn_release,
3553 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3555 struct nfs4_delegreturndata *data;
3556 struct nfs_server *server = NFS_SERVER(inode);
3557 struct rpc_task *task;
3558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3562 struct rpc_task_setup task_setup_data = {
3563 .rpc_client = server->client,
3564 .rpc_message = &msg,
3565 .callback_ops = &nfs4_delegreturn_ops,
3566 .flags = RPC_TASK_ASYNC,
3570 data = kzalloc(sizeof(*data), GFP_KERNEL);
3573 data->args.fhandle = &data->fh;
3574 data->args.stateid = &data->stateid;
3575 data->args.bitmask = server->attr_bitmask;
3576 nfs_copy_fh(&data->fh, NFS_FH(inode));
3577 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3578 data->res.fattr = &data->fattr;
3579 data->res.server = server;
3580 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3581 nfs_fattr_init(data->res.fattr);
3582 data->timestamp = jiffies;
3583 data->rpc_status = 0;
3585 task_setup_data.callback_data = data;
3586 msg.rpc_argp = &data->args,
3587 msg.rpc_resp = &data->res,
3588 task = rpc_run_task(&task_setup_data);
3590 return PTR_ERR(task);
3593 status = nfs4_wait_for_completion_rpc_task(task);
3596 status = data->rpc_status;
3599 nfs_refresh_inode(inode, &data->fattr);
3605 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3607 struct nfs_server *server = NFS_SERVER(inode);
3608 struct nfs4_exception exception = { };
3611 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3613 case -NFS4ERR_STALE_STATEID:
3614 case -NFS4ERR_EXPIRED:
3618 err = nfs4_handle_exception(server, err, &exception);
3619 } while (exception.retry);
3623 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3624 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3627 * sleep, with exponential backoff, and retry the LOCK operation.
3629 static unsigned long
3630 nfs4_set_lock_task_retry(unsigned long timeout)
3632 schedule_timeout_killable(timeout);
3634 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3635 return NFS4_LOCK_MAXTIMEOUT;
3639 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3641 struct inode *inode = state->inode;
3642 struct nfs_server *server = NFS_SERVER(inode);
3643 struct nfs_client *clp = server->nfs_client;
3644 struct nfs_lockt_args arg = {
3645 .fh = NFS_FH(inode),
3648 struct nfs_lockt_res res = {
3651 struct rpc_message msg = {
3652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3655 .rpc_cred = state->owner->so_cred,
3657 struct nfs4_lock_state *lsp;
3660 arg.lock_owner.clientid = clp->cl_clientid;
3661 status = nfs4_set_lock_state(state, request);
3664 lsp = request->fl_u.nfs4_fl.owner;
3665 arg.lock_owner.id = lsp->ls_id.id;
3666 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3669 request->fl_type = F_UNLCK;
3671 case -NFS4ERR_DENIED:
3674 request->fl_ops->fl_release_private(request);
3679 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3681 struct nfs4_exception exception = { };
3685 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3686 _nfs4_proc_getlk(state, cmd, request),
3688 } while (exception.retry);
3692 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3695 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3697 res = posix_lock_file_wait(file, fl);
3700 res = flock_lock_file_wait(file, fl);
3708 struct nfs4_unlockdata {
3709 struct nfs_locku_args arg;
3710 struct nfs_locku_res res;
3711 struct nfs4_lock_state *lsp;
3712 struct nfs_open_context *ctx;
3713 struct file_lock fl;
3714 const struct nfs_server *server;
3715 unsigned long timestamp;
3718 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3719 struct nfs_open_context *ctx,
3720 struct nfs4_lock_state *lsp,
3721 struct nfs_seqid *seqid)
3723 struct nfs4_unlockdata *p;
3724 struct inode *inode = lsp->ls_state->inode;
3726 p = kzalloc(sizeof(*p), GFP_KERNEL);
3729 p->arg.fh = NFS_FH(inode);
3731 p->arg.seqid = seqid;
3732 p->res.seqid = seqid;
3733 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3734 p->arg.stateid = &lsp->ls_stateid;
3736 atomic_inc(&lsp->ls_count);
3737 /* Ensure we don't close file until we're done freeing locks! */
3738 p->ctx = get_nfs_open_context(ctx);
3739 memcpy(&p->fl, fl, sizeof(p->fl));
3740 p->server = NFS_SERVER(inode);
3744 static void nfs4_locku_release_calldata(void *data)
3746 struct nfs4_unlockdata *calldata = data;
3747 nfs_free_seqid(calldata->arg.seqid);
3748 nfs4_put_lock_state(calldata->lsp);
3749 put_nfs_open_context(calldata->ctx);
3753 static void nfs4_locku_done(struct rpc_task *task, void *data)
3755 struct nfs4_unlockdata *calldata = data;
3757 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3759 if (RPC_ASSASSINATED(task))
3761 switch (task->tk_status) {
3763 memcpy(calldata->lsp->ls_stateid.data,
3764 calldata->res.stateid.data,
3765 sizeof(calldata->lsp->ls_stateid.data));
3766 renew_lease(calldata->server, calldata->timestamp);
3768 case -NFS4ERR_BAD_STATEID:
3769 case -NFS4ERR_OLD_STATEID:
3770 case -NFS4ERR_STALE_STATEID:
3771 case -NFS4ERR_EXPIRED:
3774 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3775 nfs_restart_rpc(task,
3776 calldata->server->nfs_client);
3780 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3782 struct nfs4_unlockdata *calldata = data;
3784 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3786 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3787 /* Note: exit _without_ running nfs4_locku_done */
3788 task->tk_action = NULL;
3791 calldata->timestamp = jiffies;
3792 if (nfs4_setup_sequence(calldata->server->nfs_client,
3793 &calldata->arg.seq_args,
3794 &calldata->res.seq_res, 1, task))
3796 rpc_call_start(task);
3799 static const struct rpc_call_ops nfs4_locku_ops = {
3800 .rpc_call_prepare = nfs4_locku_prepare,
3801 .rpc_call_done = nfs4_locku_done,
3802 .rpc_release = nfs4_locku_release_calldata,
3805 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3806 struct nfs_open_context *ctx,
3807 struct nfs4_lock_state *lsp,
3808 struct nfs_seqid *seqid)
3810 struct nfs4_unlockdata *data;
3811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3813 .rpc_cred = ctx->cred,
3815 struct rpc_task_setup task_setup_data = {
3816 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3817 .rpc_message = &msg,
3818 .callback_ops = &nfs4_locku_ops,
3819 .workqueue = nfsiod_workqueue,
3820 .flags = RPC_TASK_ASYNC,
3823 /* Ensure this is an unlock - when canceling a lock, the
3824 * canceled lock is passed in, and it won't be an unlock.
3826 fl->fl_type = F_UNLCK;
3828 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3830 nfs_free_seqid(seqid);
3831 return ERR_PTR(-ENOMEM);
3834 msg.rpc_argp = &data->arg,
3835 msg.rpc_resp = &data->res,
3836 task_setup_data.callback_data = data;
3837 return rpc_run_task(&task_setup_data);
3840 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3842 struct nfs_inode *nfsi = NFS_I(state->inode);
3843 struct nfs_seqid *seqid;
3844 struct nfs4_lock_state *lsp;
3845 struct rpc_task *task;
3847 unsigned char fl_flags = request->fl_flags;
3849 status = nfs4_set_lock_state(state, request);
3850 /* Unlock _before_ we do the RPC call */
3851 request->fl_flags |= FL_EXISTS;
3852 down_read(&nfsi->rwsem);
3853 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3854 up_read(&nfsi->rwsem);
3857 up_read(&nfsi->rwsem);
3860 /* Is this a delegated lock? */
3861 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3863 lsp = request->fl_u.nfs4_fl.owner;
3864 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3868 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3869 status = PTR_ERR(task);
3872 status = nfs4_wait_for_completion_rpc_task(task);
3875 request->fl_flags = fl_flags;
3879 struct nfs4_lockdata {
3880 struct nfs_lock_args arg;
3881 struct nfs_lock_res res;
3882 struct nfs4_lock_state *lsp;
3883 struct nfs_open_context *ctx;
3884 struct file_lock fl;
3885 unsigned long timestamp;
3888 struct nfs_server *server;
3891 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3892 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3894 struct nfs4_lockdata *p;
3895 struct inode *inode = lsp->ls_state->inode;
3896 struct nfs_server *server = NFS_SERVER(inode);
3898 p = kzalloc(sizeof(*p), GFP_KERNEL);
3902 p->arg.fh = NFS_FH(inode);
3904 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3905 if (p->arg.open_seqid == NULL)
3907 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3908 if (p->arg.lock_seqid == NULL)
3909 goto out_free_seqid;
3910 p->arg.lock_stateid = &lsp->ls_stateid;
3911 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3912 p->arg.lock_owner.id = lsp->ls_id.id;
3913 p->res.lock_seqid = p->arg.lock_seqid;
3914 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3917 atomic_inc(&lsp->ls_count);
3918 p->ctx = get_nfs_open_context(ctx);
3919 memcpy(&p->fl, fl, sizeof(p->fl));
3922 nfs_free_seqid(p->arg.open_seqid);
3928 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3930 struct nfs4_lockdata *data = calldata;
3931 struct nfs4_state *state = data->lsp->ls_state;
3933 dprintk("%s: begin!\n", __func__);
3934 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3936 /* Do we need to do an open_to_lock_owner? */
3937 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3938 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3940 data->arg.open_stateid = &state->stateid;
3941 data->arg.new_lock_owner = 1;
3942 data->res.open_seqid = data->arg.open_seqid;
3944 data->arg.new_lock_owner = 0;
3945 data->timestamp = jiffies;
3946 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3947 &data->res.seq_res, 1, task))
3949 rpc_call_start(task);
3950 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3953 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3955 struct nfs4_lockdata *data = calldata;
3957 dprintk("%s: begin!\n", __func__);
3959 nfs4_sequence_done(data->server, &data->res.seq_res,
3962 data->rpc_status = task->tk_status;
3963 if (RPC_ASSASSINATED(task))
3965 if (data->arg.new_lock_owner != 0) {
3966 if (data->rpc_status == 0)
3967 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3971 if (data->rpc_status == 0) {
3972 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3973 sizeof(data->lsp->ls_stateid.data));
3974 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3975 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3978 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3981 static void nfs4_lock_release(void *calldata)
3983 struct nfs4_lockdata *data = calldata;
3985 dprintk("%s: begin!\n", __func__);
3986 nfs_free_seqid(data->arg.open_seqid);
3987 if (data->cancelled != 0) {
3988 struct rpc_task *task;
3989 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3990 data->arg.lock_seqid);
3993 dprintk("%s: cancelling lock!\n", __func__);
3995 nfs_free_seqid(data->arg.lock_seqid);
3996 nfs4_put_lock_state(data->lsp);
3997 put_nfs_open_context(data->ctx);
3999 dprintk("%s: done!\n", __func__);
4002 static const struct rpc_call_ops nfs4_lock_ops = {
4003 .rpc_call_prepare = nfs4_lock_prepare,
4004 .rpc_call_done = nfs4_lock_done,
4005 .rpc_release = nfs4_lock_release,
4008 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
4010 struct nfs4_lockdata *data;
4011 struct rpc_task *task;
4012 struct rpc_message msg = {
4013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4014 .rpc_cred = state->owner->so_cred,
4016 struct rpc_task_setup task_setup_data = {
4017 .rpc_client = NFS_CLIENT(state->inode),
4018 .rpc_message = &msg,
4019 .callback_ops = &nfs4_lock_ops,
4020 .workqueue = nfsiod_workqueue,
4021 .flags = RPC_TASK_ASYNC,
4025 dprintk("%s: begin!\n", __func__);
4026 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4027 fl->fl_u.nfs4_fl.owner);
4031 data->arg.block = 1;
4033 data->arg.reclaim = 1;
4034 msg.rpc_argp = &data->arg,
4035 msg.rpc_resp = &data->res,
4036 task_setup_data.callback_data = data;
4037 task = rpc_run_task(&task_setup_data);
4039 return PTR_ERR(task);
4040 ret = nfs4_wait_for_completion_rpc_task(task);
4042 ret = data->rpc_status;
4044 data->cancelled = 1;
4046 dprintk("%s: done, ret = %d!\n", __func__, ret);
4050 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4052 struct nfs_server *server = NFS_SERVER(state->inode);
4053 struct nfs4_exception exception = { };
4057 /* Cache the lock if possible... */
4058 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4060 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4061 if (err != -NFS4ERR_DELAY)
4063 nfs4_handle_exception(server, err, &exception);
4064 } while (exception.retry);
4068 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4070 struct nfs_server *server = NFS_SERVER(state->inode);
4071 struct nfs4_exception exception = { };
4074 err = nfs4_set_lock_state(state, request);
4078 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4080 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4084 case -NFS4ERR_GRACE:
4085 case -NFS4ERR_DELAY:
4086 nfs4_handle_exception(server, err, &exception);
4089 } while (exception.retry);
4094 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4096 struct nfs_inode *nfsi = NFS_I(state->inode);
4097 unsigned char fl_flags = request->fl_flags;
4100 /* Is this a delegated open? */
4101 status = nfs4_set_lock_state(state, request);
4104 request->fl_flags |= FL_ACCESS;
4105 status = do_vfs_lock(request->fl_file, request);
4108 down_read(&nfsi->rwsem);
4109 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4110 /* Yes: cache locks! */
4111 /* ...but avoid races with delegation recall... */
4112 request->fl_flags = fl_flags & ~FL_SLEEP;
4113 status = do_vfs_lock(request->fl_file, request);
4116 status = _nfs4_do_setlk(state, cmd, request, 0);
4119 /* Note: we always want to sleep here! */
4120 request->fl_flags = fl_flags | FL_SLEEP;
4121 if (do_vfs_lock(request->fl_file, request) < 0)
4122 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4124 up_read(&nfsi->rwsem);
4126 request->fl_flags = fl_flags;
4130 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4132 struct nfs4_exception exception = { };
4136 err = _nfs4_proc_setlk(state, cmd, request);
4137 if (err == -NFS4ERR_DENIED)
4139 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4141 } while (exception.retry);
4146 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4148 struct nfs_open_context *ctx;
4149 struct nfs4_state *state;
4150 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4153 /* verify open state */
4154 ctx = nfs_file_open_context(filp);
4157 if (request->fl_start < 0 || request->fl_end < 0)
4160 if (IS_GETLK(cmd)) {
4162 return nfs4_proc_getlk(state, F_GETLK, request);
4166 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4169 if (request->fl_type == F_UNLCK) {
4171 return nfs4_proc_unlck(state, cmd, request);
4178 status = nfs4_proc_setlk(state, cmd, request);
4179 if ((status != -EAGAIN) || IS_SETLK(cmd))
4181 timeout = nfs4_set_lock_task_retry(timeout);
4182 status = -ERESTARTSYS;
4185 } while(status < 0);
4189 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4191 struct nfs_server *server = NFS_SERVER(state->inode);
4192 struct nfs4_exception exception = { };
4195 err = nfs4_set_lock_state(state, fl);
4199 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4202 printk(KERN_ERR "%s: unhandled error %d.\n",
4207 case -NFS4ERR_EXPIRED:
4208 case -NFS4ERR_STALE_CLIENTID:
4209 case -NFS4ERR_STALE_STATEID:
4210 case -NFS4ERR_BADSESSION:
4211 case -NFS4ERR_BADSLOT:
4212 case -NFS4ERR_BAD_HIGH_SLOT:
4213 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4214 case -NFS4ERR_DEADSESSION:
4215 nfs4_schedule_state_recovery(server->nfs_client);
4219 * The show must go on: exit, but mark the
4220 * stateid as needing recovery.
4222 case -NFS4ERR_ADMIN_REVOKED:
4223 case -NFS4ERR_BAD_STATEID:
4224 case -NFS4ERR_OPENMODE:
4225 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4229 case -NFS4ERR_DENIED:
4230 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4233 case -NFS4ERR_DELAY:
4236 err = nfs4_handle_exception(server, err, &exception);
4237 } while (exception.retry);
4242 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4244 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4245 size_t buflen, int flags)
4247 struct inode *inode = dentry->d_inode;
4249 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4252 return nfs4_proc_set_acl(inode, buf, buflen);
4255 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4256 * and that's what we'll do for e.g. user attributes that haven't been set.
4257 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4258 * attributes in kernel-managed attribute namespaces. */
4259 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4262 struct inode *inode = dentry->d_inode;
4264 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4267 return nfs4_proc_get_acl(inode, buf, buflen);
4270 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4272 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4274 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4276 if (buf && buflen < len)
4279 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4283 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4285 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4286 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4287 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4290 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4291 NFS_ATTR_FATTR_NLINK;
4292 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4296 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4297 struct nfs4_fs_locations *fs_locations, struct page *page)
4299 struct nfs_server *server = NFS_SERVER(dir);
4301 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4302 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4304 struct nfs4_fs_locations_arg args = {
4305 .dir_fh = NFS_FH(dir),
4310 struct nfs4_fs_locations_res res = {
4311 .fs_locations = fs_locations,
4313 struct rpc_message msg = {
4314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4320 dprintk("%s: start\n", __func__);
4321 nfs_fattr_init(&fs_locations->fattr);
4322 fs_locations->server = server;
4323 fs_locations->nlocations = 0;
4324 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4325 nfs_fixup_referral_attributes(&fs_locations->fattr);
4326 dprintk("%s: returned status = %d\n", __func__, status);
4330 #ifdef CONFIG_NFS_V4_1
4332 * nfs4_proc_exchange_id()
4334 * Since the clientid has expired, all compounds using sessions
4335 * associated with the stale clientid will be returning
4336 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4337 * be in some phase of session reset.
4339 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4341 nfs4_verifier verifier;
4342 struct nfs41_exchange_id_args args = {
4344 .flags = clp->cl_exchange_flags,
4346 struct nfs41_exchange_id_res res = {
4350 struct rpc_message msg = {
4351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4358 dprintk("--> %s\n", __func__);
4359 BUG_ON(clp == NULL);
4361 /* Remove server-only flags */
4362 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4364 p = (u32 *)verifier.data;
4365 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4366 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4367 args.verifier = &verifier;
4370 args.id_len = scnprintf(args.id, sizeof(args.id),
4373 rpc_peeraddr2str(clp->cl_rpcclient,
4375 clp->cl_id_uniquifier);
4377 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4379 if (status != NFS4ERR_CLID_INUSE)
4385 if (++clp->cl_id_uniquifier == 0)
4389 dprintk("<-- %s status= %d\n", __func__, status);
4393 struct nfs4_get_lease_time_data {
4394 struct nfs4_get_lease_time_args *args;
4395 struct nfs4_get_lease_time_res *res;
4396 struct nfs_client *clp;
4399 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4403 struct nfs4_get_lease_time_data *data =
4404 (struct nfs4_get_lease_time_data *)calldata;
4406 dprintk("--> %s\n", __func__);
4407 /* just setup sequence, do not trigger session recovery
4408 since we're invoked within one */
4409 ret = nfs41_setup_sequence(data->clp->cl_session,
4410 &data->args->la_seq_args,
4411 &data->res->lr_seq_res, 0, task);
4413 BUG_ON(ret == -EAGAIN);
4414 rpc_call_start(task);
4415 dprintk("<-- %s\n", __func__);
4419 * Called from nfs4_state_manager thread for session setup, so don't recover
4420 * from sequence operation or clientid errors.
4422 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4424 struct nfs4_get_lease_time_data *data =
4425 (struct nfs4_get_lease_time_data *)calldata;
4427 dprintk("--> %s\n", __func__);
4428 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4429 switch (task->tk_status) {
4430 case -NFS4ERR_DELAY:
4431 case -NFS4ERR_GRACE:
4432 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4433 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4434 task->tk_status = 0;
4435 nfs_restart_rpc(task, data->clp);
4438 dprintk("<-- %s\n", __func__);
4441 struct rpc_call_ops nfs4_get_lease_time_ops = {
4442 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4443 .rpc_call_done = nfs4_get_lease_time_done,
4446 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4448 struct rpc_task *task;
4449 struct nfs4_get_lease_time_args args;
4450 struct nfs4_get_lease_time_res res = {
4451 .lr_fsinfo = fsinfo,
4453 struct nfs4_get_lease_time_data data = {
4458 struct rpc_message msg = {
4459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4463 struct rpc_task_setup task_setup = {
4464 .rpc_client = clp->cl_rpcclient,
4465 .rpc_message = &msg,
4466 .callback_ops = &nfs4_get_lease_time_ops,
4467 .callback_data = &data
4471 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4472 dprintk("--> %s\n", __func__);
4473 task = rpc_run_task(&task_setup);
4476 status = PTR_ERR(task);
4478 status = task->tk_status;
4481 dprintk("<-- %s return %d\n", __func__, status);
4487 * Reset a slot table
4489 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4490 int old_max_slots, int ivalue)
4495 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
4498 * Until we have dynamic slot table adjustment, insist
4499 * upon the same slot table size
4501 if (max_slots != old_max_slots) {
4502 dprintk("%s reset slot table does't match old\n",
4504 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4507 spin_lock(&tbl->slot_tbl_lock);
4508 for (i = 0; i < max_slots; ++i)
4509 tbl->slots[i].seq_nr = ivalue;
4510 spin_unlock(&tbl->slot_tbl_lock);
4511 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4512 tbl, tbl->slots, tbl->max_slots);
4514 dprintk("<-- %s: return %d\n", __func__, ret);
4519 * Reset the forechannel and backchannel slot tables
4521 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4525 status = nfs4_reset_slot_table(&session->fc_slot_table,
4526 session->fc_attrs.max_reqs,
4527 session->fc_slot_table.max_slots,
4532 status = nfs4_reset_slot_table(&session->bc_slot_table,
4533 session->bc_attrs.max_reqs,
4534 session->bc_slot_table.max_slots,
4539 /* Destroy the slot table */
4540 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4542 if (session->fc_slot_table.slots != NULL) {
4543 kfree(session->fc_slot_table.slots);
4544 session->fc_slot_table.slots = NULL;
4546 if (session->bc_slot_table.slots != NULL) {
4547 kfree(session->bc_slot_table.slots);
4548 session->bc_slot_table.slots = NULL;
4554 * Initialize slot table
4556 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4557 int max_slots, int ivalue)
4559 struct nfs4_slot *slot;
4562 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4564 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4566 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4571 spin_lock(&tbl->slot_tbl_lock);
4572 tbl->max_slots = max_slots;
4574 tbl->highest_used_slotid = -1; /* no slot is currently used */
4575 spin_unlock(&tbl->slot_tbl_lock);
4576 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4577 tbl, tbl->slots, tbl->max_slots);
4579 dprintk("<-- %s: return %d\n", __func__, ret);
4584 * Initialize the forechannel and backchannel tables
4586 static int nfs4_init_slot_tables(struct nfs4_session *session)
4588 struct nfs4_slot_table *tbl;
4591 tbl = &session->fc_slot_table;
4592 if (tbl->slots == NULL) {
4593 status = nfs4_init_slot_table(tbl,
4594 session->fc_attrs.max_reqs, 1);
4599 tbl = &session->bc_slot_table;
4600 if (tbl->slots == NULL) {
4601 status = nfs4_init_slot_table(tbl,
4602 session->bc_attrs.max_reqs, 0);
4604 nfs4_destroy_slot_tables(session);
4610 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4612 struct nfs4_session *session;
4613 struct nfs4_slot_table *tbl;
4615 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4620 * The create session reply races with the server back
4621 * channel probe. Mark the client NFS_CS_SESSION_INITING
4622 * so that the client back channel can find the
4625 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4626 init_completion(&session->complete);
4628 tbl = &session->fc_slot_table;
4629 tbl->highest_used_slotid = -1;
4630 spin_lock_init(&tbl->slot_tbl_lock);
4631 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4633 tbl = &session->bc_slot_table;
4634 tbl->highest_used_slotid = -1;
4635 spin_lock_init(&tbl->slot_tbl_lock);
4636 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4642 void nfs4_destroy_session(struct nfs4_session *session)
4644 nfs4_proc_destroy_session(session);
4645 dprintk("%s Destroy backchannel for xprt %p\n",
4646 __func__, session->clp->cl_rpcclient->cl_xprt);
4647 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4648 NFS41_BC_MIN_CALLBACKS);
4649 nfs4_destroy_slot_tables(session);
4654 * Initialize the values to be used by the client in CREATE_SESSION
4655 * If nfs4_init_session set the fore channel request and response sizes,
4658 * Set the back channel max_resp_sz_cached to zero to force the client to
4659 * always set csa_cachethis to FALSE because the current implementation
4660 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4662 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4664 struct nfs4_session *session = args->client->cl_session;
4665 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4666 mxresp_sz = session->fc_attrs.max_resp_sz;
4669 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4671 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4672 /* Fore channel attributes */
4673 args->fc_attrs.headerpadsz = 0;
4674 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4675 args->fc_attrs.max_resp_sz = mxresp_sz;
4676 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4677 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4678 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4680 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4681 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4683 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4684 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4685 args->fc_attrs.max_reqs);
4687 /* Back channel attributes */
4688 args->bc_attrs.headerpadsz = 0;
4689 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4690 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4691 args->bc_attrs.max_resp_sz_cached = 0;
4692 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4693 args->bc_attrs.max_reqs = 1;
4695 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4696 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4698 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4699 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4700 args->bc_attrs.max_reqs);
4703 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4707 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4708 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4712 #define _verify_fore_channel_attr(_name_) \
4713 _verify_channel_attr("fore", #_name_, \
4714 args->fc_attrs._name_, \
4715 session->fc_attrs._name_)
4717 #define _verify_back_channel_attr(_name_) \
4718 _verify_channel_attr("back", #_name_, \
4719 args->bc_attrs._name_, \
4720 session->bc_attrs._name_)
4723 * The server is not allowed to increase the fore channel header pad size,
4724 * maximum response size, or maximum number of operations.
4726 * The back channel attributes are only negotiatied down: We send what the
4727 * (back channel) server insists upon.
4729 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4730 struct nfs4_session *session)
4734 ret |= _verify_fore_channel_attr(headerpadsz);
4735 ret |= _verify_fore_channel_attr(max_resp_sz);
4736 ret |= _verify_fore_channel_attr(max_ops);
4738 ret |= _verify_back_channel_attr(headerpadsz);
4739 ret |= _verify_back_channel_attr(max_rqst_sz);
4740 ret |= _verify_back_channel_attr(max_resp_sz);
4741 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4742 ret |= _verify_back_channel_attr(max_ops);
4743 ret |= _verify_back_channel_attr(max_reqs);
4748 static int _nfs4_proc_create_session(struct nfs_client *clp)
4750 struct nfs4_session *session = clp->cl_session;
4751 struct nfs41_create_session_args args = {
4753 .cb_program = NFS4_CALLBACK,
4755 struct nfs41_create_session_res res = {
4758 struct rpc_message msg = {
4759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4765 nfs4_init_channel_attrs(&args);
4766 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4768 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4771 /* Verify the session's negotiated channel_attrs values */
4772 status = nfs4_verify_channel_attrs(&args, session);
4774 /* Increment the clientid slot sequence id */
4782 * Issues a CREATE_SESSION operation to the server.
4783 * It is the responsibility of the caller to verify the session is
4784 * expired before calling this routine.
4786 int nfs4_proc_create_session(struct nfs_client *clp)
4790 struct nfs4_session *session = clp->cl_session;
4792 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4794 status = _nfs4_proc_create_session(clp);
4798 /* Init and reset the fore channel */
4799 status = nfs4_init_slot_tables(session);
4800 dprintk("slot table initialization returned %d\n", status);
4803 status = nfs4_reset_slot_tables(session);
4804 dprintk("slot table reset returned %d\n", status);
4808 ptr = (unsigned *)&session->sess_id.data[0];
4809 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4810 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4812 dprintk("<-- %s\n", __func__);
4817 * Issue the over-the-wire RPC DESTROY_SESSION.
4818 * The caller must serialize access to this routine.
4820 int nfs4_proc_destroy_session(struct nfs4_session *session)
4823 struct rpc_message msg;
4825 dprintk("--> nfs4_proc_destroy_session\n");
4827 /* session is still being setup */
4828 if (session->clp->cl_cons_state != NFS_CS_READY)
4831 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4832 msg.rpc_argp = session;
4833 msg.rpc_resp = NULL;
4834 msg.rpc_cred = NULL;
4835 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4839 "Got error %d from the server on DESTROY_SESSION. "
4840 "Session has been destroyed regardless...\n", status);
4842 dprintk("<-- nfs4_proc_destroy_session\n");
4846 int nfs4_init_session(struct nfs_server *server)
4848 struct nfs_client *clp = server->nfs_client;
4849 struct nfs4_session *session;
4852 if (!nfs4_has_session(clp))
4855 session = clp->cl_session;
4856 session->fc_attrs.max_rqst_sz = server->wsize + nfs41_maxwrite_overhead;
4857 session->fc_attrs.max_resp_sz = server->rsize + nfs41_maxread_overhead;
4859 ret = nfs4_recover_expired_lease(server);
4861 ret = nfs4_check_client_ready(clp);
4866 * Renew the cl_session lease.
4868 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4870 struct nfs4_sequence_args args;
4871 struct nfs4_sequence_res res;
4873 struct rpc_message msg = {
4874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4880 args.sa_cache_this = 0;
4882 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4886 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4888 struct nfs_client *clp = (struct nfs_client *)data;
4890 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4892 if (task->tk_status < 0) {
4893 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4895 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4897 nfs_restart_rpc(task, clp);
4901 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4903 kfree(task->tk_msg.rpc_argp);
4904 kfree(task->tk_msg.rpc_resp);
4906 dprintk("<-- %s\n", __func__);
4909 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4911 struct nfs_client *clp;
4912 struct nfs4_sequence_args *args;
4913 struct nfs4_sequence_res *res;
4915 clp = (struct nfs_client *)data;
4916 args = task->tk_msg.rpc_argp;
4917 res = task->tk_msg.rpc_resp;
4919 if (nfs4_setup_sequence(clp, args, res, 0, task))
4921 rpc_call_start(task);
4924 static const struct rpc_call_ops nfs41_sequence_ops = {
4925 .rpc_call_done = nfs41_sequence_call_done,
4926 .rpc_call_prepare = nfs41_sequence_prepare,
4929 static int nfs41_proc_async_sequence(struct nfs_client *clp,
4930 struct rpc_cred *cred)
4932 struct nfs4_sequence_args *args;
4933 struct nfs4_sequence_res *res;
4934 struct rpc_message msg = {
4935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4939 args = kzalloc(sizeof(*args), GFP_KERNEL);
4942 res = kzalloc(sizeof(*res), GFP_KERNEL);
4947 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4948 msg.rpc_argp = args;
4951 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4952 &nfs41_sequence_ops, (void *)clp);
4955 struct nfs4_reclaim_complete_data {
4956 struct nfs_client *clp;
4957 struct nfs41_reclaim_complete_args arg;
4958 struct nfs41_reclaim_complete_res res;
4961 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
4963 struct nfs4_reclaim_complete_data *calldata = data;
4965 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
4966 &calldata->res.seq_res, 0, task))
4969 rpc_call_start(task);
4972 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
4974 struct nfs4_reclaim_complete_data *calldata = data;
4975 struct nfs_client *clp = calldata->clp;
4976 struct nfs4_sequence_res *res = &calldata->res.seq_res;
4978 dprintk("--> %s\n", __func__);
4979 nfs41_sequence_done(clp, res, task->tk_status);
4980 switch (task->tk_status) {
4982 case -NFS4ERR_COMPLETE_ALREADY:
4984 case -NFS4ERR_BADSESSION:
4985 case -NFS4ERR_DEADSESSION:
4987 * Handle the session error, but do not retry the operation, as
4988 * we have no way of telling whether the clientid had to be
4989 * reset before we got our reply. If reset, a new wave of
4990 * reclaim operations will follow, containing their own reclaim
4991 * complete. We don't want our retry to get on the way of
4992 * recovery by incorrectly indicating to the server that we're
4993 * done reclaiming state since the process had to be restarted.
4995 _nfs4_async_handle_error(task, NULL, clp, NULL);
4998 if (_nfs4_async_handle_error(
4999 task, NULL, clp, NULL) == -EAGAIN) {
5000 rpc_restart_call_prepare(task);
5005 dprintk("<-- %s\n", __func__);
5008 static void nfs4_free_reclaim_complete_data(void *data)
5010 struct nfs4_reclaim_complete_data *calldata = data;
5015 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5016 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5017 .rpc_call_done = nfs4_reclaim_complete_done,
5018 .rpc_release = nfs4_free_reclaim_complete_data,
5022 * Issue a global reclaim complete.
5024 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5026 struct nfs4_reclaim_complete_data *calldata;
5027 struct rpc_task *task;
5028 struct rpc_message msg = {
5029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5031 struct rpc_task_setup task_setup_data = {
5032 .rpc_client = clp->cl_rpcclient,
5033 .rpc_message = &msg,
5034 .callback_ops = &nfs4_reclaim_complete_call_ops,
5035 .flags = RPC_TASK_ASYNC,
5037 int status = -ENOMEM;
5039 dprintk("--> %s\n", __func__);
5040 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5041 if (calldata == NULL)
5043 calldata->clp = clp;
5044 calldata->arg.one_fs = 0;
5045 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5047 msg.rpc_argp = &calldata->arg;
5048 msg.rpc_resp = &calldata->res;
5049 task_setup_data.callback_data = calldata;
5050 task = rpc_run_task(&task_setup_data);
5052 status = PTR_ERR(task);
5055 dprintk("<-- %s status=%d\n", __func__, status);
5058 #endif /* CONFIG_NFS_V4_1 */
5060 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5061 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5062 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5063 .recover_open = nfs4_open_reclaim,
5064 .recover_lock = nfs4_lock_reclaim,
5065 .establish_clid = nfs4_init_clientid,
5066 .get_clid_cred = nfs4_get_setclientid_cred,
5069 #if defined(CONFIG_NFS_V4_1)
5070 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5071 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5072 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5073 .recover_open = nfs4_open_reclaim,
5074 .recover_lock = nfs4_lock_reclaim,
5075 .establish_clid = nfs41_init_clientid,
5076 .get_clid_cred = nfs4_get_exchange_id_cred,
5077 .reclaim_complete = nfs41_proc_reclaim_complete,
5079 #endif /* CONFIG_NFS_V4_1 */
5081 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5082 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5083 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5084 .recover_open = nfs4_open_expired,
5085 .recover_lock = nfs4_lock_expired,
5086 .establish_clid = nfs4_init_clientid,
5087 .get_clid_cred = nfs4_get_setclientid_cred,
5090 #if defined(CONFIG_NFS_V4_1)
5091 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5092 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5093 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5094 .recover_open = nfs4_open_expired,
5095 .recover_lock = nfs4_lock_expired,
5096 .establish_clid = nfs41_init_clientid,
5097 .get_clid_cred = nfs4_get_exchange_id_cred,
5099 #endif /* CONFIG_NFS_V4_1 */
5101 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5102 .sched_state_renewal = nfs4_proc_async_renew,
5103 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5104 .renew_lease = nfs4_proc_renew,
5107 #if defined(CONFIG_NFS_V4_1)
5108 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5109 .sched_state_renewal = nfs41_proc_async_sequence,
5110 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5111 .renew_lease = nfs4_proc_sequence,
5116 * Per minor version reboot and network partition recovery ops
5119 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5120 &nfs40_reboot_recovery_ops,
5121 #if defined(CONFIG_NFS_V4_1)
5122 &nfs41_reboot_recovery_ops,
5126 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5127 &nfs40_nograce_recovery_ops,
5128 #if defined(CONFIG_NFS_V4_1)
5129 &nfs41_nograce_recovery_ops,
5133 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5134 &nfs40_state_renewal_ops,
5135 #if defined(CONFIG_NFS_V4_1)
5136 &nfs41_state_renewal_ops,
5140 static const struct inode_operations nfs4_file_inode_operations = {
5141 .permission = nfs_permission,
5142 .getattr = nfs_getattr,
5143 .setattr = nfs_setattr,
5144 .getxattr = nfs4_getxattr,
5145 .setxattr = nfs4_setxattr,
5146 .listxattr = nfs4_listxattr,
5149 const struct nfs_rpc_ops nfs_v4_clientops = {
5150 .version = 4, /* protocol version */
5151 .dentry_ops = &nfs4_dentry_operations,
5152 .dir_inode_ops = &nfs4_dir_inode_operations,
5153 .file_inode_ops = &nfs4_file_inode_operations,
5154 .getroot = nfs4_proc_get_root,
5155 .getattr = nfs4_proc_getattr,
5156 .setattr = nfs4_proc_setattr,
5157 .lookupfh = nfs4_proc_lookupfh,
5158 .lookup = nfs4_proc_lookup,
5159 .access = nfs4_proc_access,
5160 .readlink = nfs4_proc_readlink,
5161 .create = nfs4_proc_create,
5162 .remove = nfs4_proc_remove,
5163 .unlink_setup = nfs4_proc_unlink_setup,
5164 .unlink_done = nfs4_proc_unlink_done,
5165 .rename = nfs4_proc_rename,
5166 .link = nfs4_proc_link,
5167 .symlink = nfs4_proc_symlink,
5168 .mkdir = nfs4_proc_mkdir,
5169 .rmdir = nfs4_proc_remove,
5170 .readdir = nfs4_proc_readdir,
5171 .mknod = nfs4_proc_mknod,
5172 .statfs = nfs4_proc_statfs,
5173 .fsinfo = nfs4_proc_fsinfo,
5174 .pathconf = nfs4_proc_pathconf,
5175 .set_capabilities = nfs4_server_capabilities,
5176 .decode_dirent = nfs4_decode_dirent,
5177 .read_setup = nfs4_proc_read_setup,
5178 .read_done = nfs4_read_done,
5179 .write_setup = nfs4_proc_write_setup,
5180 .write_done = nfs4_write_done,
5181 .commit_setup = nfs4_proc_commit_setup,
5182 .commit_done = nfs4_commit_done,
5183 .lock = nfs4_proc_lock,
5184 .clear_acl_cache = nfs4_zap_acl_attr,
5185 .close_context = nfs4_close_context,