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.
323 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
325 int slotid = free_slotid;
327 spin_lock(&tbl->slot_tbl_lock);
328 /* clear used bit in bitmap */
329 __clear_bit(slotid, tbl->used_slots);
331 /* update highest_used_slotid when it is freed */
332 if (slotid == tbl->highest_used_slotid) {
333 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
334 if (slotid >= 0 && slotid < tbl->max_slots)
335 tbl->highest_used_slotid = slotid;
337 tbl->highest_used_slotid = -1;
339 spin_unlock(&tbl->slot_tbl_lock);
340 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
341 free_slotid, tbl->highest_used_slotid);
344 void nfs41_sequence_free_slot(const struct nfs_client *clp,
345 struct nfs4_sequence_res *res)
347 struct nfs4_slot_table *tbl;
349 if (!nfs4_has_session(clp)) {
350 dprintk("%s: No session\n", __func__);
353 tbl = &clp->cl_session->fc_slot_table;
354 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
355 /* just wake up the next guy waiting since
356 * we may have not consumed a slot after all */
357 dprintk("%s: No slot\n", __func__);
359 nfs4_free_slot(tbl, res->sr_slotid);
360 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
363 /* Signal state manager thread if session is drained */
364 if (test_bit(NFS4CLNT_SESSION_DRAINING, &clp->cl_state)) {
365 spin_lock(&tbl->slot_tbl_lock);
366 if (tbl->highest_used_slotid == -1) {
367 dprintk("%s COMPLETE: Session Drained\n", __func__);
368 complete(&clp->cl_session->complete);
370 spin_unlock(&tbl->slot_tbl_lock);
372 rpc_wake_up_next(&tbl->slot_tbl_waitq);
376 static void nfs41_sequence_done(struct nfs_client *clp,
377 struct nfs4_sequence_res *res,
380 unsigned long timestamp;
381 struct nfs4_slot_table *tbl;
382 struct nfs4_slot *slot;
385 * sr_status remains 1 if an RPC level error occurred. The server
386 * may or may not have processed the sequence operation..
387 * Proceed as if the server received and processed the sequence
390 if (res->sr_status == 1)
391 res->sr_status = NFS_OK;
393 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
394 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
397 /* Check the SEQUENCE operation status */
398 if (res->sr_status == 0) {
399 tbl = &clp->cl_session->fc_slot_table;
400 slot = tbl->slots + res->sr_slotid;
401 /* Update the slot's sequence and clientid lease timer */
403 timestamp = res->sr_renewal_time;
404 spin_lock(&clp->cl_lock);
405 if (time_before(clp->cl_last_renewal, timestamp))
406 clp->cl_last_renewal = timestamp;
407 spin_unlock(&clp->cl_lock);
408 /* Check sequence flags */
409 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
413 /* The session may be reset by one of the error handlers. */
414 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
415 nfs41_sequence_free_slot(clp, res);
419 * nfs4_find_slot - efficiently look for a free slot
421 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
422 * If found, we mark the slot as used, update the highest_used_slotid,
423 * and respectively set up the sequence operation args.
424 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
426 * Note: must be called with under the slot_tbl_lock.
429 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
432 u8 ret_id = NFS4_MAX_SLOT_TABLE;
433 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
435 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
436 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
438 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
439 if (slotid >= tbl->max_slots)
441 __set_bit(slotid, tbl->used_slots);
442 if (slotid > tbl->highest_used_slotid)
443 tbl->highest_used_slotid = slotid;
446 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
447 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
451 static int nfs41_setup_sequence(struct nfs4_session *session,
452 struct nfs4_sequence_args *args,
453 struct nfs4_sequence_res *res,
455 struct rpc_task *task)
457 struct nfs4_slot *slot;
458 struct nfs4_slot_table *tbl;
461 dprintk("--> %s\n", __func__);
462 /* slot already allocated? */
463 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
466 memset(res, 0, sizeof(*res));
467 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
468 tbl = &session->fc_slot_table;
470 spin_lock(&tbl->slot_tbl_lock);
471 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state)) {
473 * The state manager will wait until the slot table is empty.
474 * Schedule the reset thread
476 dprintk("%s Schedule Session Reset\n", __func__);
477 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
478 nfs4_schedule_state_manager(session->clp);
479 spin_unlock(&tbl->slot_tbl_lock);
483 slotid = nfs4_find_slot(tbl, task);
484 if (slotid == NFS4_MAX_SLOT_TABLE) {
485 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
486 spin_unlock(&tbl->slot_tbl_lock);
487 dprintk("<-- %s: no free slots\n", __func__);
490 spin_unlock(&tbl->slot_tbl_lock);
492 slot = tbl->slots + slotid;
493 args->sa_session = session;
494 args->sa_slotid = slotid;
495 args->sa_cache_this = cache_reply;
497 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
499 res->sr_session = session;
500 res->sr_slotid = slotid;
501 res->sr_renewal_time = jiffies;
503 * sr_status is only set in decode_sequence, and so will remain
504 * set to 1 if an rpc level failure occurs.
510 int nfs4_setup_sequence(struct nfs_client *clp,
511 struct nfs4_sequence_args *args,
512 struct nfs4_sequence_res *res,
514 struct rpc_task *task)
518 dprintk("--> %s clp %p session %p sr_slotid %d\n",
519 __func__, clp, clp->cl_session, res->sr_slotid);
521 if (!nfs4_has_session(clp))
523 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
525 if (ret && ret != -EAGAIN) {
526 /* terminate rpc task */
527 task->tk_status = ret;
528 task->tk_action = NULL;
531 dprintk("<-- %s status=%d\n", __func__, ret);
535 struct nfs41_call_sync_data {
536 struct nfs_client *clp;
537 struct nfs4_sequence_args *seq_args;
538 struct nfs4_sequence_res *seq_res;
542 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
544 struct nfs41_call_sync_data *data = calldata;
546 dprintk("--> %s data->clp->cl_session %p\n", __func__,
547 data->clp->cl_session);
548 if (nfs4_setup_sequence(data->clp, data->seq_args,
549 data->seq_res, data->cache_reply, task))
551 rpc_call_start(task);
554 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
556 struct nfs41_call_sync_data *data = calldata;
558 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
559 nfs41_sequence_free_slot(data->clp, data->seq_res);
562 struct rpc_call_ops nfs41_call_sync_ops = {
563 .rpc_call_prepare = nfs41_call_sync_prepare,
564 .rpc_call_done = nfs41_call_sync_done,
567 static int nfs4_call_sync_sequence(struct nfs_client *clp,
568 struct rpc_clnt *clnt,
569 struct rpc_message *msg,
570 struct nfs4_sequence_args *args,
571 struct nfs4_sequence_res *res,
575 struct rpc_task *task;
576 struct nfs41_call_sync_data data = {
580 .cache_reply = cache_reply,
582 struct rpc_task_setup task_setup = {
585 .callback_ops = &nfs41_call_sync_ops,
586 .callback_data = &data
589 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
590 task = rpc_run_task(&task_setup);
594 ret = task->tk_status;
600 int _nfs4_call_sync_session(struct nfs_server *server,
601 struct rpc_message *msg,
602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
606 return nfs4_call_sync_sequence(server->nfs_client, server->client,
607 msg, args, res, cache_reply);
610 #endif /* CONFIG_NFS_V4_1 */
612 int _nfs4_call_sync(struct nfs_server *server,
613 struct rpc_message *msg,
614 struct nfs4_sequence_args *args,
615 struct nfs4_sequence_res *res,
618 args->sa_session = res->sr_session = NULL;
619 return rpc_call_sync(server->client, msg, 0);
622 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
623 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
624 &(res)->seq_res, (cache_reply))
626 static void nfs4_sequence_done(const struct nfs_server *server,
627 struct nfs4_sequence_res *res, int rpc_status)
629 #ifdef CONFIG_NFS_V4_1
630 if (nfs4_has_session(server->nfs_client))
631 nfs41_sequence_done(server->nfs_client, res, rpc_status);
632 #endif /* CONFIG_NFS_V4_1 */
635 void nfs4_restart_rpc(struct rpc_task *task, const struct nfs_client *clp,
636 struct nfs4_sequence_res *res)
638 #ifdef CONFIG_NFS_V4_1
639 if (nfs4_has_session(clp)) {
640 nfs41_sequence_free_slot(clp, res);
641 rpc_restart_call_prepare(task);
644 #endif /* CONFIG_NFS_V4_1 */
645 rpc_restart_call(task);
648 /* no restart, therefore free slot here */
649 static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
650 struct nfs4_sequence_res *res,
653 nfs4_sequence_done(server, res, rpc_status);
654 nfs4_sequence_free_slot(server->nfs_client, res);
657 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
659 struct nfs_inode *nfsi = NFS_I(dir);
661 spin_lock(&dir->i_lock);
662 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
663 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
664 nfs_force_lookup_revalidate(dir);
665 nfsi->change_attr = cinfo->after;
666 spin_unlock(&dir->i_lock);
669 struct nfs4_opendata {
671 struct nfs_openargs o_arg;
672 struct nfs_openres o_res;
673 struct nfs_open_confirmargs c_arg;
674 struct nfs_open_confirmres c_res;
675 struct nfs_fattr f_attr;
676 struct nfs_fattr dir_attr;
679 struct nfs4_state_owner *owner;
680 struct nfs4_state *state;
682 unsigned long timestamp;
683 unsigned int rpc_done : 1;
689 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
691 p->o_res.f_attr = &p->f_attr;
692 p->o_res.dir_attr = &p->dir_attr;
693 p->o_res.seqid = p->o_arg.seqid;
694 p->c_res.seqid = p->c_arg.seqid;
695 p->o_res.server = p->o_arg.server;
696 nfs_fattr_init(&p->f_attr);
697 nfs_fattr_init(&p->dir_attr);
698 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
701 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
702 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
703 const struct iattr *attrs)
705 struct dentry *parent = dget_parent(path->dentry);
706 struct inode *dir = parent->d_inode;
707 struct nfs_server *server = NFS_SERVER(dir);
708 struct nfs4_opendata *p;
710 p = kzalloc(sizeof(*p), GFP_KERNEL);
713 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
714 if (p->o_arg.seqid == NULL)
716 p->path.mnt = mntget(path->mnt);
717 p->path.dentry = dget(path->dentry);
720 atomic_inc(&sp->so_count);
721 p->o_arg.fh = NFS_FH(dir);
722 p->o_arg.open_flags = flags;
723 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
724 p->o_arg.clientid = server->nfs_client->cl_clientid;
725 p->o_arg.id = sp->so_owner_id.id;
726 p->o_arg.name = &p->path.dentry->d_name;
727 p->o_arg.server = server;
728 p->o_arg.bitmask = server->attr_bitmask;
729 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
730 if (flags & O_EXCL) {
731 if (nfs4_has_persistent_session(server->nfs_client)) {
733 p->o_arg.u.attrs = &p->attrs;
734 memcpy(&p->attrs, attrs, sizeof(p->attrs));
735 } else { /* EXCLUSIVE4_1 */
736 u32 *s = (u32 *) p->o_arg.u.verifier.data;
740 } else if (flags & O_CREAT) {
741 p->o_arg.u.attrs = &p->attrs;
742 memcpy(&p->attrs, attrs, sizeof(p->attrs));
744 p->c_arg.fh = &p->o_res.fh;
745 p->c_arg.stateid = &p->o_res.stateid;
746 p->c_arg.seqid = p->o_arg.seqid;
747 nfs4_init_opendata_res(p);
757 static void nfs4_opendata_free(struct kref *kref)
759 struct nfs4_opendata *p = container_of(kref,
760 struct nfs4_opendata, kref);
762 nfs_free_seqid(p->o_arg.seqid);
763 if (p->state != NULL)
764 nfs4_put_open_state(p->state);
765 nfs4_put_state_owner(p->owner);
771 static void nfs4_opendata_put(struct nfs4_opendata *p)
774 kref_put(&p->kref, nfs4_opendata_free);
777 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
781 ret = rpc_wait_for_completion_task(task);
785 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
789 if (open_mode & O_EXCL)
791 switch (mode & (FMODE_READ|FMODE_WRITE)) {
793 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
796 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
798 case FMODE_READ|FMODE_WRITE:
799 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
805 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
807 if ((delegation->type & fmode) != fmode)
809 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
811 nfs_mark_delegation_referenced(delegation);
815 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
824 case FMODE_READ|FMODE_WRITE:
827 nfs4_state_set_mode_locked(state, state->state | fmode);
830 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
832 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
833 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
834 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
837 set_bit(NFS_O_RDONLY_STATE, &state->flags);
840 set_bit(NFS_O_WRONLY_STATE, &state->flags);
842 case FMODE_READ|FMODE_WRITE:
843 set_bit(NFS_O_RDWR_STATE, &state->flags);
847 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
849 write_seqlock(&state->seqlock);
850 nfs_set_open_stateid_locked(state, stateid, fmode);
851 write_sequnlock(&state->seqlock);
854 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
857 * Protect the call to nfs4_state_set_mode_locked and
858 * serialise the stateid update
860 write_seqlock(&state->seqlock);
861 if (deleg_stateid != NULL) {
862 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
863 set_bit(NFS_DELEGATED_STATE, &state->flags);
865 if (open_stateid != NULL)
866 nfs_set_open_stateid_locked(state, open_stateid, fmode);
867 write_sequnlock(&state->seqlock);
868 spin_lock(&state->owner->so_lock);
869 update_open_stateflags(state, fmode);
870 spin_unlock(&state->owner->so_lock);
873 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
875 struct nfs_inode *nfsi = NFS_I(state->inode);
876 struct nfs_delegation *deleg_cur;
879 fmode &= (FMODE_READ|FMODE_WRITE);
882 deleg_cur = rcu_dereference(nfsi->delegation);
883 if (deleg_cur == NULL)
886 spin_lock(&deleg_cur->lock);
887 if (nfsi->delegation != deleg_cur ||
888 (deleg_cur->type & fmode) != fmode)
889 goto no_delegation_unlock;
891 if (delegation == NULL)
892 delegation = &deleg_cur->stateid;
893 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
894 goto no_delegation_unlock;
896 nfs_mark_delegation_referenced(deleg_cur);
897 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
899 no_delegation_unlock:
900 spin_unlock(&deleg_cur->lock);
904 if (!ret && open_stateid != NULL) {
905 __update_open_stateid(state, open_stateid, NULL, fmode);
913 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
915 struct nfs_delegation *delegation;
918 delegation = rcu_dereference(NFS_I(inode)->delegation);
919 if (delegation == NULL || (delegation->type & fmode) == fmode) {
924 nfs_inode_return_delegation(inode);
927 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
929 struct nfs4_state *state = opendata->state;
930 struct nfs_inode *nfsi = NFS_I(state->inode);
931 struct nfs_delegation *delegation;
932 int open_mode = opendata->o_arg.open_flags & O_EXCL;
933 fmode_t fmode = opendata->o_arg.fmode;
934 nfs4_stateid stateid;
938 if (can_open_cached(state, fmode, open_mode)) {
939 spin_lock(&state->owner->so_lock);
940 if (can_open_cached(state, fmode, open_mode)) {
941 update_open_stateflags(state, fmode);
942 spin_unlock(&state->owner->so_lock);
943 goto out_return_state;
945 spin_unlock(&state->owner->so_lock);
948 delegation = rcu_dereference(nfsi->delegation);
949 if (delegation == NULL ||
950 !can_open_delegated(delegation, fmode)) {
954 /* Save the delegation */
955 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
957 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
962 /* Try to update the stateid using the delegation */
963 if (update_open_stateid(state, NULL, &stateid, fmode))
964 goto out_return_state;
969 atomic_inc(&state->count);
973 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
976 struct nfs4_state *state = NULL;
977 struct nfs_delegation *delegation;
980 if (!data->rpc_done) {
981 state = nfs4_try_open_cached(data);
986 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
988 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
989 ret = PTR_ERR(inode);
993 state = nfs4_get_open_state(inode, data->owner);
996 if (data->o_res.delegation_type != 0) {
997 int delegation_flags = 0;
1000 delegation = rcu_dereference(NFS_I(inode)->delegation);
1002 delegation_flags = delegation->flags;
1004 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1005 nfs_inode_set_delegation(state->inode,
1006 data->owner->so_cred,
1009 nfs_inode_reclaim_delegation(state->inode,
1010 data->owner->so_cred,
1014 update_open_stateid(state, &data->o_res.stateid, NULL,
1022 return ERR_PTR(ret);
1025 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1027 struct nfs_inode *nfsi = NFS_I(state->inode);
1028 struct nfs_open_context *ctx;
1030 spin_lock(&state->inode->i_lock);
1031 list_for_each_entry(ctx, &nfsi->open_files, list) {
1032 if (ctx->state != state)
1034 get_nfs_open_context(ctx);
1035 spin_unlock(&state->inode->i_lock);
1038 spin_unlock(&state->inode->i_lock);
1039 return ERR_PTR(-ENOENT);
1042 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1044 struct nfs4_opendata *opendata;
1046 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
1047 if (opendata == NULL)
1048 return ERR_PTR(-ENOMEM);
1049 opendata->state = state;
1050 atomic_inc(&state->count);
1054 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1056 struct nfs4_state *newstate;
1059 opendata->o_arg.open_flags = 0;
1060 opendata->o_arg.fmode = fmode;
1061 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1062 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1063 nfs4_init_opendata_res(opendata);
1064 ret = _nfs4_proc_open(opendata);
1067 newstate = nfs4_opendata_to_nfs4_state(opendata);
1068 if (IS_ERR(newstate))
1069 return PTR_ERR(newstate);
1070 nfs4_close_state(&opendata->path, newstate, fmode);
1075 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1077 struct nfs4_state *newstate;
1080 /* memory barrier prior to reading state->n_* */
1081 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1083 if (state->n_rdwr != 0) {
1084 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1087 if (newstate != state)
1090 if (state->n_wronly != 0) {
1091 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1094 if (newstate != state)
1097 if (state->n_rdonly != 0) {
1098 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1101 if (newstate != state)
1105 * We may have performed cached opens for all three recoveries.
1106 * Check if we need to update the current stateid.
1108 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1109 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1110 write_seqlock(&state->seqlock);
1111 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1112 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1113 write_sequnlock(&state->seqlock);
1120 * reclaim state on the server after a reboot.
1122 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1124 struct nfs_delegation *delegation;
1125 struct nfs4_opendata *opendata;
1126 fmode_t delegation_type = 0;
1129 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1130 if (IS_ERR(opendata))
1131 return PTR_ERR(opendata);
1132 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1133 opendata->o_arg.fh = NFS_FH(state->inode);
1135 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1136 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1137 delegation_type = delegation->type;
1139 opendata->o_arg.u.delegation_type = delegation_type;
1140 status = nfs4_open_recover(opendata, state);
1141 nfs4_opendata_put(opendata);
1145 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1147 struct nfs_server *server = NFS_SERVER(state->inode);
1148 struct nfs4_exception exception = { };
1151 err = _nfs4_do_open_reclaim(ctx, state);
1152 if (err != -NFS4ERR_DELAY)
1154 nfs4_handle_exception(server, err, &exception);
1155 } while (exception.retry);
1159 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1161 struct nfs_open_context *ctx;
1164 ctx = nfs4_state_find_open_context(state);
1166 return PTR_ERR(ctx);
1167 ret = nfs4_do_open_reclaim(ctx, state);
1168 put_nfs_open_context(ctx);
1172 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1174 struct nfs4_opendata *opendata;
1177 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1178 if (IS_ERR(opendata))
1179 return PTR_ERR(opendata);
1180 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1181 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1182 sizeof(opendata->o_arg.u.delegation.data));
1183 ret = nfs4_open_recover(opendata, state);
1184 nfs4_opendata_put(opendata);
1188 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1190 struct nfs4_exception exception = { };
1191 struct nfs_server *server = NFS_SERVER(state->inode);
1194 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1200 case -NFS4ERR_STALE_CLIENTID:
1201 case -NFS4ERR_STALE_STATEID:
1202 case -NFS4ERR_EXPIRED:
1203 /* Don't recall a delegation if it was lost */
1204 nfs4_schedule_state_recovery(server->nfs_client);
1208 * The show must go on: exit, but mark the
1209 * stateid as needing recovery.
1211 case -NFS4ERR_ADMIN_REVOKED:
1212 case -NFS4ERR_BAD_STATEID:
1213 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1218 err = nfs4_handle_exception(server, err, &exception);
1219 } while (exception.retry);
1224 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1226 struct nfs4_opendata *data = calldata;
1228 data->rpc_status = task->tk_status;
1229 if (RPC_ASSASSINATED(task))
1231 if (data->rpc_status == 0) {
1232 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1233 sizeof(data->o_res.stateid.data));
1234 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1235 renew_lease(data->o_res.server, data->timestamp);
1240 static void nfs4_open_confirm_release(void *calldata)
1242 struct nfs4_opendata *data = calldata;
1243 struct nfs4_state *state = NULL;
1245 /* If this request hasn't been cancelled, do nothing */
1246 if (data->cancelled == 0)
1248 /* In case of error, no cleanup! */
1249 if (!data->rpc_done)
1251 state = nfs4_opendata_to_nfs4_state(data);
1253 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1255 nfs4_opendata_put(data);
1258 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1259 .rpc_call_done = nfs4_open_confirm_done,
1260 .rpc_release = nfs4_open_confirm_release,
1264 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1266 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1268 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1269 struct rpc_task *task;
1270 struct rpc_message msg = {
1271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1272 .rpc_argp = &data->c_arg,
1273 .rpc_resp = &data->c_res,
1274 .rpc_cred = data->owner->so_cred,
1276 struct rpc_task_setup task_setup_data = {
1277 .rpc_client = server->client,
1278 .rpc_message = &msg,
1279 .callback_ops = &nfs4_open_confirm_ops,
1280 .callback_data = data,
1281 .workqueue = nfsiod_workqueue,
1282 .flags = RPC_TASK_ASYNC,
1286 kref_get(&data->kref);
1288 data->rpc_status = 0;
1289 data->timestamp = jiffies;
1290 task = rpc_run_task(&task_setup_data);
1292 return PTR_ERR(task);
1293 status = nfs4_wait_for_completion_rpc_task(task);
1295 data->cancelled = 1;
1298 status = data->rpc_status;
1303 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1305 struct nfs4_opendata *data = calldata;
1306 struct nfs4_state_owner *sp = data->owner;
1308 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1311 * Check if we still need to send an OPEN call, or if we can use
1312 * a delegation instead.
1314 if (data->state != NULL) {
1315 struct nfs_delegation *delegation;
1317 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1320 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1321 if (delegation != NULL &&
1322 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1328 /* Update sequence id. */
1329 data->o_arg.id = sp->so_owner_id.id;
1330 data->o_arg.clientid = sp->so_client->cl_clientid;
1331 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1332 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1333 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1335 data->timestamp = jiffies;
1336 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1337 &data->o_arg.seq_args,
1338 &data->o_res.seq_res, 1, task))
1340 rpc_call_start(task);
1343 task->tk_action = NULL;
1347 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1349 struct nfs4_opendata *data = calldata;
1351 data->rpc_status = task->tk_status;
1353 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1356 if (RPC_ASSASSINATED(task))
1358 if (task->tk_status == 0) {
1359 switch (data->o_res.f_attr->mode & S_IFMT) {
1363 data->rpc_status = -ELOOP;
1366 data->rpc_status = -EISDIR;
1369 data->rpc_status = -ENOTDIR;
1371 renew_lease(data->o_res.server, data->timestamp);
1372 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1373 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1378 static void nfs4_open_release(void *calldata)
1380 struct nfs4_opendata *data = calldata;
1381 struct nfs4_state *state = NULL;
1383 /* If this request hasn't been cancelled, do nothing */
1384 if (data->cancelled == 0)
1386 /* In case of error, no cleanup! */
1387 if (data->rpc_status != 0 || !data->rpc_done)
1389 /* In case we need an open_confirm, no cleanup! */
1390 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1392 state = nfs4_opendata_to_nfs4_state(data);
1394 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1396 nfs4_opendata_put(data);
1399 static const struct rpc_call_ops nfs4_open_ops = {
1400 .rpc_call_prepare = nfs4_open_prepare,
1401 .rpc_call_done = nfs4_open_done,
1402 .rpc_release = nfs4_open_release,
1406 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1408 static int _nfs4_proc_open(struct nfs4_opendata *data)
1410 struct inode *dir = data->dir->d_inode;
1411 struct nfs_server *server = NFS_SERVER(dir);
1412 struct nfs_openargs *o_arg = &data->o_arg;
1413 struct nfs_openres *o_res = &data->o_res;
1414 struct rpc_task *task;
1415 struct rpc_message msg = {
1416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1419 .rpc_cred = data->owner->so_cred,
1421 struct rpc_task_setup task_setup_data = {
1422 .rpc_client = server->client,
1423 .rpc_message = &msg,
1424 .callback_ops = &nfs4_open_ops,
1425 .callback_data = data,
1426 .workqueue = nfsiod_workqueue,
1427 .flags = RPC_TASK_ASYNC,
1431 kref_get(&data->kref);
1433 data->rpc_status = 0;
1434 data->cancelled = 0;
1435 task = rpc_run_task(&task_setup_data);
1437 return PTR_ERR(task);
1438 status = nfs4_wait_for_completion_rpc_task(task);
1440 data->cancelled = 1;
1443 status = data->rpc_status;
1445 if (status != 0 || !data->rpc_done)
1448 if (o_res->fh.size == 0)
1449 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1451 if (o_arg->open_flags & O_CREAT) {
1452 update_changeattr(dir, &o_res->cinfo);
1453 nfs_post_op_update_inode(dir, o_res->dir_attr);
1455 nfs_refresh_inode(dir, o_res->dir_attr);
1456 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1457 status = _nfs4_proc_open_confirm(data);
1461 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1462 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1466 static int nfs4_recover_expired_lease(struct nfs_server *server)
1468 struct nfs_client *clp = server->nfs_client;
1472 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1473 ret = nfs4_wait_clnt_recover(clp);
1476 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1477 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1479 nfs4_schedule_state_recovery(clp);
1487 * reclaim state on the server after a network partition.
1488 * Assumes caller holds the appropriate lock
1490 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1492 struct nfs4_opendata *opendata;
1495 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1496 if (IS_ERR(opendata))
1497 return PTR_ERR(opendata);
1498 ret = nfs4_open_recover(opendata, state);
1500 d_drop(ctx->path.dentry);
1501 nfs4_opendata_put(opendata);
1505 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1507 struct nfs_server *server = NFS_SERVER(state->inode);
1508 struct nfs4_exception exception = { };
1512 err = _nfs4_open_expired(ctx, state);
1516 case -NFS4ERR_GRACE:
1517 case -NFS4ERR_DELAY:
1518 nfs4_handle_exception(server, err, &exception);
1521 } while (exception.retry);
1526 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1528 struct nfs_open_context *ctx;
1531 ctx = nfs4_state_find_open_context(state);
1533 return PTR_ERR(ctx);
1534 ret = nfs4_do_open_expired(ctx, state);
1535 put_nfs_open_context(ctx);
1540 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1541 * fields corresponding to attributes that were used to store the verifier.
1542 * Make sure we clobber those fields in the later setattr call
1544 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1546 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1547 !(sattr->ia_valid & ATTR_ATIME_SET))
1548 sattr->ia_valid |= ATTR_ATIME;
1550 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1551 !(sattr->ia_valid & ATTR_MTIME_SET))
1552 sattr->ia_valid |= ATTR_MTIME;
1556 * Returns a referenced nfs4_state
1558 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)
1560 struct nfs4_state_owner *sp;
1561 struct nfs4_state *state = NULL;
1562 struct nfs_server *server = NFS_SERVER(dir);
1563 struct nfs4_opendata *opendata;
1566 /* Protect against reboot recovery conflicts */
1568 if (!(sp = nfs4_get_state_owner(server, cred))) {
1569 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1572 status = nfs4_recover_expired_lease(server);
1574 goto err_put_state_owner;
1575 if (path->dentry->d_inode != NULL)
1576 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1578 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1579 if (opendata == NULL)
1580 goto err_put_state_owner;
1582 if (path->dentry->d_inode != NULL)
1583 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1585 status = _nfs4_proc_open(opendata);
1587 goto err_opendata_put;
1589 if (opendata->o_arg.open_flags & O_EXCL)
1590 nfs4_exclusive_attrset(opendata, sattr);
1592 state = nfs4_opendata_to_nfs4_state(opendata);
1593 status = PTR_ERR(state);
1595 goto err_opendata_put;
1596 nfs4_opendata_put(opendata);
1597 nfs4_put_state_owner(sp);
1601 nfs4_opendata_put(opendata);
1602 err_put_state_owner:
1603 nfs4_put_state_owner(sp);
1610 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)
1612 struct nfs4_exception exception = { };
1613 struct nfs4_state *res;
1617 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1620 /* NOTE: BAD_SEQID means the server and client disagree about the
1621 * book-keeping w.r.t. state-changing operations
1622 * (OPEN/CLOSE/LOCK/LOCKU...)
1623 * It is actually a sign of a bug on the client or on the server.
1625 * If we receive a BAD_SEQID error in the particular case of
1626 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1627 * have unhashed the old state_owner for us, and that we can
1628 * therefore safely retry using a new one. We should still warn
1629 * the user though...
1631 if (status == -NFS4ERR_BAD_SEQID) {
1632 printk(KERN_WARNING "NFS: v4 server %s "
1633 " returned a bad sequence-id error!\n",
1634 NFS_SERVER(dir)->nfs_client->cl_hostname);
1635 exception.retry = 1;
1639 * BAD_STATEID on OPEN means that the server cancelled our
1640 * state before it received the OPEN_CONFIRM.
1641 * Recover by retrying the request as per the discussion
1642 * on Page 181 of RFC3530.
1644 if (status == -NFS4ERR_BAD_STATEID) {
1645 exception.retry = 1;
1648 if (status == -EAGAIN) {
1649 /* We must have found a delegation */
1650 exception.retry = 1;
1653 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1654 status, &exception));
1655 } while (exception.retry);
1659 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1660 struct nfs_fattr *fattr, struct iattr *sattr,
1661 struct nfs4_state *state)
1663 struct nfs_server *server = NFS_SERVER(inode);
1664 struct nfs_setattrargs arg = {
1665 .fh = NFS_FH(inode),
1668 .bitmask = server->attr_bitmask,
1670 struct nfs_setattrres res = {
1674 struct rpc_message msg = {
1675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1680 unsigned long timestamp = jiffies;
1683 nfs_fattr_init(fattr);
1685 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1686 /* Use that stateid */
1687 } else if (state != NULL) {
1688 nfs4_copy_stateid(&arg.stateid, state, current->files);
1690 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1692 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1693 if (status == 0 && state != NULL)
1694 renew_lease(server, timestamp);
1698 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1699 struct nfs_fattr *fattr, struct iattr *sattr,
1700 struct nfs4_state *state)
1702 struct nfs_server *server = NFS_SERVER(inode);
1703 struct nfs4_exception exception = { };
1706 err = nfs4_handle_exception(server,
1707 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1709 } while (exception.retry);
1713 struct nfs4_closedata {
1715 struct inode *inode;
1716 struct nfs4_state *state;
1717 struct nfs_closeargs arg;
1718 struct nfs_closeres res;
1719 struct nfs_fattr fattr;
1720 unsigned long timestamp;
1723 static void nfs4_free_closedata(void *data)
1725 struct nfs4_closedata *calldata = data;
1726 struct nfs4_state_owner *sp = calldata->state->owner;
1728 nfs4_put_open_state(calldata->state);
1729 nfs_free_seqid(calldata->arg.seqid);
1730 nfs4_put_state_owner(sp);
1731 path_put(&calldata->path);
1735 static void nfs4_close_done(struct rpc_task *task, void *data)
1737 struct nfs4_closedata *calldata = data;
1738 struct nfs4_state *state = calldata->state;
1739 struct nfs_server *server = NFS_SERVER(calldata->inode);
1741 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1742 if (RPC_ASSASSINATED(task))
1744 /* hmm. we are done with the inode, and in the process of freeing
1745 * the state_owner. we keep this around to process errors
1747 switch (task->tk_status) {
1749 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1750 renew_lease(server, calldata->timestamp);
1752 case -NFS4ERR_STALE_STATEID:
1753 case -NFS4ERR_OLD_STATEID:
1754 case -NFS4ERR_BAD_STATEID:
1755 case -NFS4ERR_EXPIRED:
1756 if (calldata->arg.fmode == 0)
1759 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1760 nfs4_restart_rpc(task, server->nfs_client,
1761 &calldata->res.seq_res);
1765 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
1766 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1769 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1771 struct nfs4_closedata *calldata = data;
1772 struct nfs4_state *state = calldata->state;
1773 int clear_rd, clear_wr, clear_rdwr;
1775 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1778 clear_rd = clear_wr = clear_rdwr = 0;
1779 spin_lock(&state->owner->so_lock);
1780 /* Calculate the change in open mode */
1781 if (state->n_rdwr == 0) {
1782 if (state->n_rdonly == 0) {
1783 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1784 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1786 if (state->n_wronly == 0) {
1787 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1788 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1791 spin_unlock(&state->owner->so_lock);
1792 if (!clear_rd && !clear_wr && !clear_rdwr) {
1793 /* Note: exit _without_ calling nfs4_close_done */
1794 task->tk_action = NULL;
1797 nfs_fattr_init(calldata->res.fattr);
1798 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1799 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1800 calldata->arg.fmode = FMODE_READ;
1801 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1802 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1803 calldata->arg.fmode = FMODE_WRITE;
1805 calldata->timestamp = jiffies;
1806 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1807 &calldata->arg.seq_args, &calldata->res.seq_res,
1810 rpc_call_start(task);
1813 static const struct rpc_call_ops nfs4_close_ops = {
1814 .rpc_call_prepare = nfs4_close_prepare,
1815 .rpc_call_done = nfs4_close_done,
1816 .rpc_release = nfs4_free_closedata,
1820 * It is possible for data to be read/written from a mem-mapped file
1821 * after the sys_close call (which hits the vfs layer as a flush).
1822 * This means that we can't safely call nfsv4 close on a file until
1823 * the inode is cleared. This in turn means that we are not good
1824 * NFSv4 citizens - we do not indicate to the server to update the file's
1825 * share state even when we are done with one of the three share
1826 * stateid's in the inode.
1828 * NOTE: Caller must be holding the sp->so_owner semaphore!
1830 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1832 struct nfs_server *server = NFS_SERVER(state->inode);
1833 struct nfs4_closedata *calldata;
1834 struct nfs4_state_owner *sp = state->owner;
1835 struct rpc_task *task;
1836 struct rpc_message msg = {
1837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1838 .rpc_cred = state->owner->so_cred,
1840 struct rpc_task_setup task_setup_data = {
1841 .rpc_client = server->client,
1842 .rpc_message = &msg,
1843 .callback_ops = &nfs4_close_ops,
1844 .workqueue = nfsiod_workqueue,
1845 .flags = RPC_TASK_ASYNC,
1847 int status = -ENOMEM;
1849 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1850 if (calldata == NULL)
1852 calldata->inode = state->inode;
1853 calldata->state = state;
1854 calldata->arg.fh = NFS_FH(state->inode);
1855 calldata->arg.stateid = &state->open_stateid;
1856 if (nfs4_has_session(server->nfs_client))
1857 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
1858 /* Serialization for the sequence id */
1859 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1860 if (calldata->arg.seqid == NULL)
1861 goto out_free_calldata;
1862 calldata->arg.fmode = 0;
1863 calldata->arg.bitmask = server->cache_consistency_bitmask;
1864 calldata->res.fattr = &calldata->fattr;
1865 calldata->res.seqid = calldata->arg.seqid;
1866 calldata->res.server = server;
1867 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1868 calldata->path.mnt = mntget(path->mnt);
1869 calldata->path.dentry = dget(path->dentry);
1871 msg.rpc_argp = &calldata->arg,
1872 msg.rpc_resp = &calldata->res,
1873 task_setup_data.callback_data = calldata;
1874 task = rpc_run_task(&task_setup_data);
1876 return PTR_ERR(task);
1879 status = rpc_wait_for_completion_task(task);
1885 nfs4_put_open_state(state);
1886 nfs4_put_state_owner(sp);
1890 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1895 /* If the open_intent is for execute, we have an extra check to make */
1896 if (fmode & FMODE_EXEC) {
1897 ret = nfs_may_open(state->inode,
1898 state->owner->so_cred,
1899 nd->intent.open.flags);
1903 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1904 if (!IS_ERR(filp)) {
1905 struct nfs_open_context *ctx;
1906 ctx = nfs_file_open_context(filp);
1910 ret = PTR_ERR(filp);
1912 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1917 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1919 struct path path = {
1920 .mnt = nd->path.mnt,
1923 struct dentry *parent;
1925 struct rpc_cred *cred;
1926 struct nfs4_state *state;
1928 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1930 if (nd->flags & LOOKUP_CREATE) {
1931 attr.ia_mode = nd->intent.open.create_mode;
1932 attr.ia_valid = ATTR_MODE;
1933 if (!IS_POSIXACL(dir))
1934 attr.ia_mode &= ~current_umask();
1937 BUG_ON(nd->intent.open.flags & O_CREAT);
1940 cred = rpc_lookup_cred();
1942 return (struct dentry *)cred;
1943 parent = dentry->d_parent;
1944 /* Protect against concurrent sillydeletes */
1945 nfs_block_sillyrename(parent);
1946 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1948 if (IS_ERR(state)) {
1949 if (PTR_ERR(state) == -ENOENT) {
1950 d_add(dentry, NULL);
1951 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1953 nfs_unblock_sillyrename(parent);
1954 return (struct dentry *)state;
1956 res = d_add_unique(dentry, igrab(state->inode));
1959 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1960 nfs_unblock_sillyrename(parent);
1961 nfs4_intent_set_file(nd, &path, state, fmode);
1966 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1968 struct path path = {
1969 .mnt = nd->path.mnt,
1972 struct rpc_cred *cred;
1973 struct nfs4_state *state;
1974 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1976 cred = rpc_lookup_cred();
1978 return PTR_ERR(cred);
1979 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1981 if (IS_ERR(state)) {
1982 switch (PTR_ERR(state)) {
1988 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1994 if (state->inode == dentry->d_inode) {
1995 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1996 nfs4_intent_set_file(nd, &path, state, fmode);
1999 nfs4_close_sync(&path, state, fmode);
2005 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2007 if (ctx->state == NULL)
2010 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2012 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2015 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2017 struct nfs4_server_caps_arg args = {
2020 struct nfs4_server_caps_res res = {};
2021 struct rpc_message msg = {
2022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2028 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2030 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2031 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2032 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2033 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2034 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2035 NFS_CAP_CTIME|NFS_CAP_MTIME);
2036 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2037 server->caps |= NFS_CAP_ACLS;
2038 if (res.has_links != 0)
2039 server->caps |= NFS_CAP_HARDLINKS;
2040 if (res.has_symlinks != 0)
2041 server->caps |= NFS_CAP_SYMLINKS;
2042 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2043 server->caps |= NFS_CAP_FILEID;
2044 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2045 server->caps |= NFS_CAP_MODE;
2046 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2047 server->caps |= NFS_CAP_NLINK;
2048 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2049 server->caps |= NFS_CAP_OWNER;
2050 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2051 server->caps |= NFS_CAP_OWNER_GROUP;
2052 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2053 server->caps |= NFS_CAP_ATIME;
2054 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2055 server->caps |= NFS_CAP_CTIME;
2056 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2057 server->caps |= NFS_CAP_MTIME;
2059 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2060 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2061 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2062 server->acl_bitmask = res.acl_bitmask;
2068 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2070 struct nfs4_exception exception = { };
2073 err = nfs4_handle_exception(server,
2074 _nfs4_server_capabilities(server, fhandle),
2076 } while (exception.retry);
2080 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2081 struct nfs_fsinfo *info)
2083 struct nfs4_lookup_root_arg args = {
2084 .bitmask = nfs4_fattr_bitmap,
2086 struct nfs4_lookup_res res = {
2088 .fattr = info->fattr,
2091 struct rpc_message msg = {
2092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2097 nfs_fattr_init(info->fattr);
2098 return nfs4_call_sync(server, &msg, &args, &res, 0);
2101 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2102 struct nfs_fsinfo *info)
2104 struct nfs4_exception exception = { };
2107 err = nfs4_handle_exception(server,
2108 _nfs4_lookup_root(server, fhandle, info),
2110 } while (exception.retry);
2115 * get the file handle for the "/" directory on the server
2117 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2118 struct nfs_fsinfo *info)
2122 status = nfs4_lookup_root(server, fhandle, info);
2124 status = nfs4_server_capabilities(server, fhandle);
2126 status = nfs4_do_fsinfo(server, fhandle, info);
2127 return nfs4_map_errors(status);
2131 * Get locations and (maybe) other attributes of a referral.
2132 * Note that we'll actually follow the referral later when
2133 * we detect fsid mismatch in inode revalidation
2135 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2137 int status = -ENOMEM;
2138 struct page *page = NULL;
2139 struct nfs4_fs_locations *locations = NULL;
2141 page = alloc_page(GFP_KERNEL);
2144 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2145 if (locations == NULL)
2148 status = nfs4_proc_fs_locations(dir, name, locations, page);
2151 /* Make sure server returned a different fsid for the referral */
2152 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2153 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2158 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2159 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2161 fattr->mode = S_IFDIR;
2162 memset(fhandle, 0, sizeof(struct nfs_fh));
2171 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2173 struct nfs4_getattr_arg args = {
2175 .bitmask = server->attr_bitmask,
2177 struct nfs4_getattr_res res = {
2181 struct rpc_message msg = {
2182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2187 nfs_fattr_init(fattr);
2188 return nfs4_call_sync(server, &msg, &args, &res, 0);
2191 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2193 struct nfs4_exception exception = { };
2196 err = nfs4_handle_exception(server,
2197 _nfs4_proc_getattr(server, fhandle, fattr),
2199 } while (exception.retry);
2204 * The file is not closed if it is opened due to the a request to change
2205 * the size of the file. The open call will not be needed once the
2206 * VFS layer lookup-intents are implemented.
2208 * Close is called when the inode is destroyed.
2209 * If we haven't opened the file for O_WRONLY, we
2210 * need to in the size_change case to obtain a stateid.
2213 * Because OPEN is always done by name in nfsv4, it is
2214 * possible that we opened a different file by the same
2215 * name. We can recognize this race condition, but we
2216 * can't do anything about it besides returning an error.
2218 * This will be fixed with VFS changes (lookup-intent).
2221 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2222 struct iattr *sattr)
2224 struct inode *inode = dentry->d_inode;
2225 struct rpc_cred *cred = NULL;
2226 struct nfs4_state *state = NULL;
2229 nfs_fattr_init(fattr);
2231 /* Search for an existing open(O_WRITE) file */
2232 if (sattr->ia_valid & ATTR_FILE) {
2233 struct nfs_open_context *ctx;
2235 ctx = nfs_file_open_context(sattr->ia_file);
2242 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2244 nfs_setattr_update_inode(inode, sattr);
2248 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2249 const struct qstr *name, struct nfs_fh *fhandle,
2250 struct nfs_fattr *fattr)
2253 struct nfs4_lookup_arg args = {
2254 .bitmask = server->attr_bitmask,
2258 struct nfs4_lookup_res res = {
2263 struct rpc_message msg = {
2264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2269 nfs_fattr_init(fattr);
2271 dprintk("NFS call lookupfh %s\n", name->name);
2272 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2273 dprintk("NFS reply lookupfh: %d\n", status);
2277 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2278 struct qstr *name, struct nfs_fh *fhandle,
2279 struct nfs_fattr *fattr)
2281 struct nfs4_exception exception = { };
2284 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2286 if (err == -NFS4ERR_MOVED) {
2290 err = nfs4_handle_exception(server, err, &exception);
2291 } while (exception.retry);
2295 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2296 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2300 dprintk("NFS call lookup %s\n", name->name);
2301 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2302 if (status == -NFS4ERR_MOVED)
2303 status = nfs4_get_referral(dir, name, fattr, fhandle);
2304 dprintk("NFS reply lookup: %d\n", status);
2308 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2310 struct nfs4_exception exception = { };
2313 err = nfs4_handle_exception(NFS_SERVER(dir),
2314 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2316 } while (exception.retry);
2320 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2322 struct nfs_server *server = NFS_SERVER(inode);
2323 struct nfs_fattr fattr;
2324 struct nfs4_accessargs args = {
2325 .fh = NFS_FH(inode),
2326 .bitmask = server->attr_bitmask,
2328 struct nfs4_accessres res = {
2332 struct rpc_message msg = {
2333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2336 .rpc_cred = entry->cred,
2338 int mode = entry->mask;
2342 * Determine which access bits we want to ask for...
2344 if (mode & MAY_READ)
2345 args.access |= NFS4_ACCESS_READ;
2346 if (S_ISDIR(inode->i_mode)) {
2347 if (mode & MAY_WRITE)
2348 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2349 if (mode & MAY_EXEC)
2350 args.access |= NFS4_ACCESS_LOOKUP;
2352 if (mode & MAY_WRITE)
2353 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2354 if (mode & MAY_EXEC)
2355 args.access |= NFS4_ACCESS_EXECUTE;
2357 nfs_fattr_init(&fattr);
2358 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2361 if (res.access & NFS4_ACCESS_READ)
2362 entry->mask |= MAY_READ;
2363 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2364 entry->mask |= MAY_WRITE;
2365 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2366 entry->mask |= MAY_EXEC;
2367 nfs_refresh_inode(inode, &fattr);
2372 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2374 struct nfs4_exception exception = { };
2377 err = nfs4_handle_exception(NFS_SERVER(inode),
2378 _nfs4_proc_access(inode, entry),
2380 } while (exception.retry);
2385 * TODO: For the time being, we don't try to get any attributes
2386 * along with any of the zero-copy operations READ, READDIR,
2389 * In the case of the first three, we want to put the GETATTR
2390 * after the read-type operation -- this is because it is hard
2391 * to predict the length of a GETATTR response in v4, and thus
2392 * align the READ data correctly. This means that the GETATTR
2393 * may end up partially falling into the page cache, and we should
2394 * shift it into the 'tail' of the xdr_buf before processing.
2395 * To do this efficiently, we need to know the total length
2396 * of data received, which doesn't seem to be available outside
2399 * In the case of WRITE, we also want to put the GETATTR after
2400 * the operation -- in this case because we want to make sure
2401 * we get the post-operation mtime and size. This means that
2402 * we can't use xdr_encode_pages() as written: we need a variant
2403 * of it which would leave room in the 'tail' iovec.
2405 * Both of these changes to the XDR layer would in fact be quite
2406 * minor, but I decided to leave them for a subsequent patch.
2408 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2409 unsigned int pgbase, unsigned int pglen)
2411 struct nfs4_readlink args = {
2412 .fh = NFS_FH(inode),
2417 struct nfs4_readlink_res res;
2418 struct rpc_message msg = {
2419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2424 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2427 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2428 unsigned int pgbase, unsigned int pglen)
2430 struct nfs4_exception exception = { };
2433 err = nfs4_handle_exception(NFS_SERVER(inode),
2434 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2436 } while (exception.retry);
2442 * We will need to arrange for the VFS layer to provide an atomic open.
2443 * Until then, this create/open method is prone to inefficiency and race
2444 * conditions due to the lookup, create, and open VFS calls from sys_open()
2445 * placed on the wire.
2447 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2448 * The file will be opened again in the subsequent VFS open call
2449 * (nfs4_proc_file_open).
2451 * The open for read will just hang around to be used by any process that
2452 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2456 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2457 int flags, struct nameidata *nd)
2459 struct path path = {
2460 .mnt = nd->path.mnt,
2463 struct nfs4_state *state;
2464 struct rpc_cred *cred;
2465 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2468 cred = rpc_lookup_cred();
2470 status = PTR_ERR(cred);
2473 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2475 if (IS_ERR(state)) {
2476 status = PTR_ERR(state);
2479 d_add(dentry, igrab(state->inode));
2480 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2481 if (flags & O_EXCL) {
2482 struct nfs_fattr fattr;
2483 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2485 nfs_setattr_update_inode(state->inode, sattr);
2486 nfs_post_op_update_inode(state->inode, &fattr);
2488 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2489 status = nfs4_intent_set_file(nd, &path, state, fmode);
2491 nfs4_close_sync(&path, state, fmode);
2498 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2500 struct nfs_server *server = NFS_SERVER(dir);
2501 struct nfs_removeargs args = {
2503 .name.len = name->len,
2504 .name.name = name->name,
2505 .bitmask = server->attr_bitmask,
2507 struct nfs_removeres res = {
2510 struct rpc_message msg = {
2511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2517 nfs_fattr_init(&res.dir_attr);
2518 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2520 update_changeattr(dir, &res.cinfo);
2521 nfs_post_op_update_inode(dir, &res.dir_attr);
2526 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2528 struct nfs4_exception exception = { };
2531 err = nfs4_handle_exception(NFS_SERVER(dir),
2532 _nfs4_proc_remove(dir, name),
2534 } while (exception.retry);
2538 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2540 struct nfs_server *server = NFS_SERVER(dir);
2541 struct nfs_removeargs *args = msg->rpc_argp;
2542 struct nfs_removeres *res = msg->rpc_resp;
2544 args->bitmask = server->cache_consistency_bitmask;
2545 res->server = server;
2546 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2549 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2551 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2553 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2554 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2556 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
2557 update_changeattr(dir, &res->cinfo);
2558 nfs_post_op_update_inode(dir, &res->dir_attr);
2562 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2563 struct inode *new_dir, struct qstr *new_name)
2565 struct nfs_server *server = NFS_SERVER(old_dir);
2566 struct nfs4_rename_arg arg = {
2567 .old_dir = NFS_FH(old_dir),
2568 .new_dir = NFS_FH(new_dir),
2569 .old_name = old_name,
2570 .new_name = new_name,
2571 .bitmask = server->attr_bitmask,
2573 struct nfs_fattr old_fattr, new_fattr;
2574 struct nfs4_rename_res res = {
2576 .old_fattr = &old_fattr,
2577 .new_fattr = &new_fattr,
2579 struct rpc_message msg = {
2580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2586 nfs_fattr_init(res.old_fattr);
2587 nfs_fattr_init(res.new_fattr);
2588 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2591 update_changeattr(old_dir, &res.old_cinfo);
2592 nfs_post_op_update_inode(old_dir, res.old_fattr);
2593 update_changeattr(new_dir, &res.new_cinfo);
2594 nfs_post_op_update_inode(new_dir, res.new_fattr);
2599 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2600 struct inode *new_dir, struct qstr *new_name)
2602 struct nfs4_exception exception = { };
2605 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2606 _nfs4_proc_rename(old_dir, old_name,
2609 } while (exception.retry);
2613 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2615 struct nfs_server *server = NFS_SERVER(inode);
2616 struct nfs4_link_arg arg = {
2617 .fh = NFS_FH(inode),
2618 .dir_fh = NFS_FH(dir),
2620 .bitmask = server->attr_bitmask,
2622 struct nfs_fattr fattr, dir_attr;
2623 struct nfs4_link_res res = {
2626 .dir_attr = &dir_attr,
2628 struct rpc_message msg = {
2629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2635 nfs_fattr_init(res.fattr);
2636 nfs_fattr_init(res.dir_attr);
2637 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2639 update_changeattr(dir, &res.cinfo);
2640 nfs_post_op_update_inode(dir, res.dir_attr);
2641 nfs_post_op_update_inode(inode, res.fattr);
2647 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2649 struct nfs4_exception exception = { };
2652 err = nfs4_handle_exception(NFS_SERVER(inode),
2653 _nfs4_proc_link(inode, dir, name),
2655 } while (exception.retry);
2659 struct nfs4_createdata {
2660 struct rpc_message msg;
2661 struct nfs4_create_arg arg;
2662 struct nfs4_create_res res;
2664 struct nfs_fattr fattr;
2665 struct nfs_fattr dir_fattr;
2668 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2669 struct qstr *name, struct iattr *sattr, u32 ftype)
2671 struct nfs4_createdata *data;
2673 data = kzalloc(sizeof(*data), GFP_KERNEL);
2675 struct nfs_server *server = NFS_SERVER(dir);
2677 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2678 data->msg.rpc_argp = &data->arg;
2679 data->msg.rpc_resp = &data->res;
2680 data->arg.dir_fh = NFS_FH(dir);
2681 data->arg.server = server;
2682 data->arg.name = name;
2683 data->arg.attrs = sattr;
2684 data->arg.ftype = ftype;
2685 data->arg.bitmask = server->attr_bitmask;
2686 data->res.server = server;
2687 data->res.fh = &data->fh;
2688 data->res.fattr = &data->fattr;
2689 data->res.dir_fattr = &data->dir_fattr;
2690 nfs_fattr_init(data->res.fattr);
2691 nfs_fattr_init(data->res.dir_fattr);
2696 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2698 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2699 &data->arg, &data->res, 1);
2701 update_changeattr(dir, &data->res.dir_cinfo);
2702 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2703 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2708 static void nfs4_free_createdata(struct nfs4_createdata *data)
2713 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2714 struct page *page, unsigned int len, struct iattr *sattr)
2716 struct nfs4_createdata *data;
2717 int status = -ENAMETOOLONG;
2719 if (len > NFS4_MAXPATHLEN)
2723 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2727 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2728 data->arg.u.symlink.pages = &page;
2729 data->arg.u.symlink.len = len;
2731 status = nfs4_do_create(dir, dentry, data);
2733 nfs4_free_createdata(data);
2738 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2739 struct page *page, unsigned int len, struct iattr *sattr)
2741 struct nfs4_exception exception = { };
2744 err = nfs4_handle_exception(NFS_SERVER(dir),
2745 _nfs4_proc_symlink(dir, dentry, page,
2748 } while (exception.retry);
2752 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2753 struct iattr *sattr)
2755 struct nfs4_createdata *data;
2756 int status = -ENOMEM;
2758 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2762 status = nfs4_do_create(dir, dentry, data);
2764 nfs4_free_createdata(data);
2769 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2770 struct iattr *sattr)
2772 struct nfs4_exception exception = { };
2775 err = nfs4_handle_exception(NFS_SERVER(dir),
2776 _nfs4_proc_mkdir(dir, dentry, sattr),
2778 } while (exception.retry);
2782 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2783 u64 cookie, struct page *page, unsigned int count, int plus)
2785 struct inode *dir = dentry->d_inode;
2786 struct nfs4_readdir_arg args = {
2791 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2793 struct nfs4_readdir_res res;
2794 struct rpc_message msg = {
2795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2802 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2803 dentry->d_parent->d_name.name,
2804 dentry->d_name.name,
2805 (unsigned long long)cookie);
2806 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2807 res.pgbase = args.pgbase;
2808 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2810 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2812 nfs_invalidate_atime(dir);
2814 dprintk("%s: returns %d\n", __func__, status);
2818 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2819 u64 cookie, struct page *page, unsigned int count, int plus)
2821 struct nfs4_exception exception = { };
2824 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2825 _nfs4_proc_readdir(dentry, cred, cookie,
2828 } while (exception.retry);
2832 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2833 struct iattr *sattr, dev_t rdev)
2835 struct nfs4_createdata *data;
2836 int mode = sattr->ia_mode;
2837 int status = -ENOMEM;
2839 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2840 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2842 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2847 data->arg.ftype = NF4FIFO;
2848 else if (S_ISBLK(mode)) {
2849 data->arg.ftype = NF4BLK;
2850 data->arg.u.device.specdata1 = MAJOR(rdev);
2851 data->arg.u.device.specdata2 = MINOR(rdev);
2853 else if (S_ISCHR(mode)) {
2854 data->arg.ftype = NF4CHR;
2855 data->arg.u.device.specdata1 = MAJOR(rdev);
2856 data->arg.u.device.specdata2 = MINOR(rdev);
2859 status = nfs4_do_create(dir, dentry, data);
2861 nfs4_free_createdata(data);
2866 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2867 struct iattr *sattr, dev_t rdev)
2869 struct nfs4_exception exception = { };
2872 err = nfs4_handle_exception(NFS_SERVER(dir),
2873 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2875 } while (exception.retry);
2879 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2880 struct nfs_fsstat *fsstat)
2882 struct nfs4_statfs_arg args = {
2884 .bitmask = server->attr_bitmask,
2886 struct nfs4_statfs_res res = {
2889 struct rpc_message msg = {
2890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2895 nfs_fattr_init(fsstat->fattr);
2896 return nfs4_call_sync(server, &msg, &args, &res, 0);
2899 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2901 struct nfs4_exception exception = { };
2904 err = nfs4_handle_exception(server,
2905 _nfs4_proc_statfs(server, fhandle, fsstat),
2907 } while (exception.retry);
2911 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2912 struct nfs_fsinfo *fsinfo)
2914 struct nfs4_fsinfo_arg args = {
2916 .bitmask = server->attr_bitmask,
2918 struct nfs4_fsinfo_res res = {
2921 struct rpc_message msg = {
2922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2927 return nfs4_call_sync(server, &msg, &args, &res, 0);
2930 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2932 struct nfs4_exception exception = { };
2936 err = nfs4_handle_exception(server,
2937 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2939 } while (exception.retry);
2943 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2945 nfs_fattr_init(fsinfo->fattr);
2946 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2949 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2950 struct nfs_pathconf *pathconf)
2952 struct nfs4_pathconf_arg args = {
2954 .bitmask = server->attr_bitmask,
2956 struct nfs4_pathconf_res res = {
2957 .pathconf = pathconf,
2959 struct rpc_message msg = {
2960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2965 /* None of the pathconf attributes are mandatory to implement */
2966 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2967 memset(pathconf, 0, sizeof(*pathconf));
2971 nfs_fattr_init(pathconf->fattr);
2972 return nfs4_call_sync(server, &msg, &args, &res, 0);
2975 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2976 struct nfs_pathconf *pathconf)
2978 struct nfs4_exception exception = { };
2982 err = nfs4_handle_exception(server,
2983 _nfs4_proc_pathconf(server, fhandle, pathconf),
2985 } while (exception.retry);
2989 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2991 struct nfs_server *server = NFS_SERVER(data->inode);
2993 dprintk("--> %s\n", __func__);
2995 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2996 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2998 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2999 nfs4_restart_rpc(task, server->nfs_client, &data->res.seq_res);
3003 nfs_invalidate_atime(data->inode);
3004 if (task->tk_status > 0)
3005 renew_lease(server, data->timestamp);
3006 else if (task->tk_status < 0)
3007 nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res);
3012 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3014 data->timestamp = jiffies;
3015 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3018 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3020 struct inode *inode = data->inode;
3022 /* slot is freed in nfs_writeback_done */
3023 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3026 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3027 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3028 &data->res.seq_res);
3031 if (task->tk_status >= 0) {
3032 renew_lease(NFS_SERVER(inode), data->timestamp);
3033 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3038 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3040 struct nfs_server *server = NFS_SERVER(data->inode);
3042 data->args.bitmask = server->cache_consistency_bitmask;
3043 data->res.server = server;
3044 data->timestamp = jiffies;
3046 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3049 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3051 struct inode *inode = data->inode;
3053 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3055 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3056 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3057 &data->res.seq_res);
3060 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3061 &data->res.seq_res);
3062 nfs_refresh_inode(inode, data->res.fattr);
3066 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3068 struct nfs_server *server = NFS_SERVER(data->inode);
3070 data->args.bitmask = server->cache_consistency_bitmask;
3071 data->res.server = server;
3072 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3076 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3077 * standalone procedure for queueing an asynchronous RENEW.
3079 static void nfs4_renew_done(struct rpc_task *task, void *data)
3081 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
3082 unsigned long timestamp = (unsigned long)data;
3084 if (task->tk_status < 0) {
3085 /* Unless we're shutting down, schedule state recovery! */
3086 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3087 nfs4_schedule_state_recovery(clp);
3090 spin_lock(&clp->cl_lock);
3091 if (time_before(clp->cl_last_renewal,timestamp))
3092 clp->cl_last_renewal = timestamp;
3093 spin_unlock(&clp->cl_lock);
3096 static const struct rpc_call_ops nfs4_renew_ops = {
3097 .rpc_call_done = nfs4_renew_done,
3100 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3102 struct rpc_message msg = {
3103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3108 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3109 &nfs4_renew_ops, (void *)jiffies);
3112 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3114 struct rpc_message msg = {
3115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3119 unsigned long now = jiffies;
3122 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3125 spin_lock(&clp->cl_lock);
3126 if (time_before(clp->cl_last_renewal,now))
3127 clp->cl_last_renewal = now;
3128 spin_unlock(&clp->cl_lock);
3132 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3134 return (server->caps & NFS_CAP_ACLS)
3135 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3136 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3139 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3140 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3143 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3145 static void buf_to_pages(const void *buf, size_t buflen,
3146 struct page **pages, unsigned int *pgbase)
3148 const void *p = buf;
3150 *pgbase = offset_in_page(buf);
3152 while (p < buf + buflen) {
3153 *(pages++) = virt_to_page(p);
3154 p += PAGE_CACHE_SIZE;
3158 struct nfs4_cached_acl {
3164 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3166 struct nfs_inode *nfsi = NFS_I(inode);
3168 spin_lock(&inode->i_lock);
3169 kfree(nfsi->nfs4_acl);
3170 nfsi->nfs4_acl = acl;
3171 spin_unlock(&inode->i_lock);
3174 static void nfs4_zap_acl_attr(struct inode *inode)
3176 nfs4_set_cached_acl(inode, NULL);
3179 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3181 struct nfs_inode *nfsi = NFS_I(inode);
3182 struct nfs4_cached_acl *acl;
3185 spin_lock(&inode->i_lock);
3186 acl = nfsi->nfs4_acl;
3189 if (buf == NULL) /* user is just asking for length */
3191 if (acl->cached == 0)
3193 ret = -ERANGE; /* see getxattr(2) man page */
3194 if (acl->len > buflen)
3196 memcpy(buf, acl->data, acl->len);
3200 spin_unlock(&inode->i_lock);
3204 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3206 struct nfs4_cached_acl *acl;
3208 if (buf && acl_len <= PAGE_SIZE) {
3209 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3213 memcpy(acl->data, buf, acl_len);
3215 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3222 nfs4_set_cached_acl(inode, acl);
3225 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3227 struct page *pages[NFS4ACL_MAXPAGES];
3228 struct nfs_getaclargs args = {
3229 .fh = NFS_FH(inode),
3233 struct nfs_getaclres res = {
3237 struct rpc_message msg = {
3238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3242 struct page *localpage = NULL;
3245 if (buflen < PAGE_SIZE) {
3246 /* As long as we're doing a round trip to the server anyway,
3247 * let's be prepared for a page of acl data. */
3248 localpage = alloc_page(GFP_KERNEL);
3249 resp_buf = page_address(localpage);
3250 if (localpage == NULL)
3252 args.acl_pages[0] = localpage;
3253 args.acl_pgbase = 0;
3254 args.acl_len = PAGE_SIZE;
3257 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3259 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3262 if (res.acl_len > args.acl_len)
3263 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3265 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3268 if (res.acl_len > buflen)
3271 memcpy(buf, resp_buf, res.acl_len);
3276 __free_page(localpage);
3280 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3282 struct nfs4_exception exception = { };
3285 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3288 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3289 } while (exception.retry);
3293 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3295 struct nfs_server *server = NFS_SERVER(inode);
3298 if (!nfs4_server_supports_acls(server))
3300 ret = nfs_revalidate_inode(server, inode);
3303 ret = nfs4_read_cached_acl(inode, buf, buflen);
3306 return nfs4_get_acl_uncached(inode, buf, buflen);
3309 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3311 struct nfs_server *server = NFS_SERVER(inode);
3312 struct page *pages[NFS4ACL_MAXPAGES];
3313 struct nfs_setaclargs arg = {
3314 .fh = NFS_FH(inode),
3318 struct nfs_setaclres res;
3319 struct rpc_message msg = {
3320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3326 if (!nfs4_server_supports_acls(server))
3328 nfs_inode_return_delegation(inode);
3329 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3330 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3331 nfs_access_zap_cache(inode);
3332 nfs_zap_acl_cache(inode);
3336 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3338 struct nfs4_exception exception = { };
3341 err = nfs4_handle_exception(NFS_SERVER(inode),
3342 __nfs4_proc_set_acl(inode, buf, buflen),
3344 } while (exception.retry);
3349 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3351 if (!clp || task->tk_status >= 0)
3353 switch(task->tk_status) {
3354 case -NFS4ERR_ADMIN_REVOKED:
3355 case -NFS4ERR_BAD_STATEID:
3356 case -NFS4ERR_OPENMODE:
3359 nfs4_state_mark_reclaim_nograce(clp, state);
3360 case -NFS4ERR_STALE_CLIENTID:
3361 case -NFS4ERR_STALE_STATEID:
3362 case -NFS4ERR_EXPIRED:
3363 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3364 nfs4_schedule_state_recovery(clp);
3365 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3366 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3367 task->tk_status = 0;
3369 #if defined(CONFIG_NFS_V4_1)
3370 case -NFS4ERR_BADSESSION:
3371 case -NFS4ERR_BADSLOT:
3372 case -NFS4ERR_BAD_HIGH_SLOT:
3373 case -NFS4ERR_DEADSESSION:
3374 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3375 case -NFS4ERR_SEQ_FALSE_RETRY:
3376 case -NFS4ERR_SEQ_MISORDERED:
3377 dprintk("%s ERROR %d, Reset session\n", __func__,
3379 nfs4_schedule_state_recovery(clp);
3380 task->tk_status = 0;
3382 #endif /* CONFIG_NFS_V4_1 */
3383 case -NFS4ERR_DELAY:
3385 nfs_inc_server_stats(server, NFSIOS_DELAY);
3386 case -NFS4ERR_GRACE:
3387 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3388 task->tk_status = 0;
3390 case -NFS4ERR_OLD_STATEID:
3391 task->tk_status = 0;
3394 task->tk_status = nfs4_map_errors(task->tk_status);
3399 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3401 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3404 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3406 nfs4_verifier sc_verifier;
3407 struct nfs4_setclientid setclientid = {
3408 .sc_verifier = &sc_verifier,
3411 struct rpc_message msg = {
3412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3413 .rpc_argp = &setclientid,
3421 p = (__be32*)sc_verifier.data;
3422 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3423 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3426 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3427 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3429 rpc_peeraddr2str(clp->cl_rpcclient,
3431 rpc_peeraddr2str(clp->cl_rpcclient,
3433 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3434 clp->cl_id_uniquifier);
3435 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3436 sizeof(setclientid.sc_netid),
3437 rpc_peeraddr2str(clp->cl_rpcclient,
3438 RPC_DISPLAY_NETID));
3439 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3440 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3441 clp->cl_ipaddr, port >> 8, port & 255);
3443 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3444 if (status != -NFS4ERR_CLID_INUSE)
3449 ssleep(clp->cl_lease_time + 1);
3451 if (++clp->cl_id_uniquifier == 0)
3457 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3459 struct nfs_fsinfo fsinfo;
3460 struct rpc_message msg = {
3461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3463 .rpc_resp = &fsinfo,
3470 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3472 spin_lock(&clp->cl_lock);
3473 clp->cl_lease_time = fsinfo.lease_time * HZ;
3474 clp->cl_last_renewal = now;
3475 spin_unlock(&clp->cl_lock);
3480 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3485 err = _nfs4_proc_setclientid_confirm(clp, cred);
3489 case -NFS4ERR_RESOURCE:
3490 /* The IBM lawyers misread another document! */
3491 case -NFS4ERR_DELAY:
3492 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3498 struct nfs4_delegreturndata {
3499 struct nfs4_delegreturnargs args;
3500 struct nfs4_delegreturnres res;
3502 nfs4_stateid stateid;
3503 unsigned long timestamp;
3504 struct nfs_fattr fattr;
3508 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3510 struct nfs4_delegreturndata *data = calldata;
3512 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3515 data->rpc_status = task->tk_status;
3516 if (data->rpc_status == 0)
3517 renew_lease(data->res.server, data->timestamp);
3520 static void nfs4_delegreturn_release(void *calldata)
3525 #if defined(CONFIG_NFS_V4_1)
3526 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3528 struct nfs4_delegreturndata *d_data;
3530 d_data = (struct nfs4_delegreturndata *)data;
3532 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3533 &d_data->args.seq_args,
3534 &d_data->res.seq_res, 1, task))
3536 rpc_call_start(task);
3538 #endif /* CONFIG_NFS_V4_1 */
3540 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3541 #if defined(CONFIG_NFS_V4_1)
3542 .rpc_call_prepare = nfs4_delegreturn_prepare,
3543 #endif /* CONFIG_NFS_V4_1 */
3544 .rpc_call_done = nfs4_delegreturn_done,
3545 .rpc_release = nfs4_delegreturn_release,
3548 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3550 struct nfs4_delegreturndata *data;
3551 struct nfs_server *server = NFS_SERVER(inode);
3552 struct rpc_task *task;
3553 struct rpc_message msg = {
3554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3557 struct rpc_task_setup task_setup_data = {
3558 .rpc_client = server->client,
3559 .rpc_message = &msg,
3560 .callback_ops = &nfs4_delegreturn_ops,
3561 .flags = RPC_TASK_ASYNC,
3565 data = kzalloc(sizeof(*data), GFP_KERNEL);
3568 data->args.fhandle = &data->fh;
3569 data->args.stateid = &data->stateid;
3570 data->args.bitmask = server->attr_bitmask;
3571 nfs_copy_fh(&data->fh, NFS_FH(inode));
3572 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3573 data->res.fattr = &data->fattr;
3574 data->res.server = server;
3575 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3576 nfs_fattr_init(data->res.fattr);
3577 data->timestamp = jiffies;
3578 data->rpc_status = 0;
3580 task_setup_data.callback_data = data;
3581 msg.rpc_argp = &data->args,
3582 msg.rpc_resp = &data->res,
3583 task = rpc_run_task(&task_setup_data);
3585 return PTR_ERR(task);
3588 status = nfs4_wait_for_completion_rpc_task(task);
3591 status = data->rpc_status;
3594 nfs_refresh_inode(inode, &data->fattr);
3600 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3602 struct nfs_server *server = NFS_SERVER(inode);
3603 struct nfs4_exception exception = { };
3606 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3608 case -NFS4ERR_STALE_STATEID:
3609 case -NFS4ERR_EXPIRED:
3613 err = nfs4_handle_exception(server, err, &exception);
3614 } while (exception.retry);
3618 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3619 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3622 * sleep, with exponential backoff, and retry the LOCK operation.
3624 static unsigned long
3625 nfs4_set_lock_task_retry(unsigned long timeout)
3627 schedule_timeout_killable(timeout);
3629 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3630 return NFS4_LOCK_MAXTIMEOUT;
3634 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3636 struct inode *inode = state->inode;
3637 struct nfs_server *server = NFS_SERVER(inode);
3638 struct nfs_client *clp = server->nfs_client;
3639 struct nfs_lockt_args arg = {
3640 .fh = NFS_FH(inode),
3643 struct nfs_lockt_res res = {
3646 struct rpc_message msg = {
3647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3650 .rpc_cred = state->owner->so_cred,
3652 struct nfs4_lock_state *lsp;
3655 arg.lock_owner.clientid = clp->cl_clientid;
3656 status = nfs4_set_lock_state(state, request);
3659 lsp = request->fl_u.nfs4_fl.owner;
3660 arg.lock_owner.id = lsp->ls_id.id;
3661 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3664 request->fl_type = F_UNLCK;
3666 case -NFS4ERR_DENIED:
3669 request->fl_ops->fl_release_private(request);
3674 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3676 struct nfs4_exception exception = { };
3680 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3681 _nfs4_proc_getlk(state, cmd, request),
3683 } while (exception.retry);
3687 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3690 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3692 res = posix_lock_file_wait(file, fl);
3695 res = flock_lock_file_wait(file, fl);
3703 struct nfs4_unlockdata {
3704 struct nfs_locku_args arg;
3705 struct nfs_locku_res res;
3706 struct nfs4_lock_state *lsp;
3707 struct nfs_open_context *ctx;
3708 struct file_lock fl;
3709 const struct nfs_server *server;
3710 unsigned long timestamp;
3713 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3714 struct nfs_open_context *ctx,
3715 struct nfs4_lock_state *lsp,
3716 struct nfs_seqid *seqid)
3718 struct nfs4_unlockdata *p;
3719 struct inode *inode = lsp->ls_state->inode;
3721 p = kzalloc(sizeof(*p), GFP_KERNEL);
3724 p->arg.fh = NFS_FH(inode);
3726 p->arg.seqid = seqid;
3727 p->res.seqid = seqid;
3728 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3729 p->arg.stateid = &lsp->ls_stateid;
3731 atomic_inc(&lsp->ls_count);
3732 /* Ensure we don't close file until we're done freeing locks! */
3733 p->ctx = get_nfs_open_context(ctx);
3734 memcpy(&p->fl, fl, sizeof(p->fl));
3735 p->server = NFS_SERVER(inode);
3739 static void nfs4_locku_release_calldata(void *data)
3741 struct nfs4_unlockdata *calldata = data;
3742 nfs_free_seqid(calldata->arg.seqid);
3743 nfs4_put_lock_state(calldata->lsp);
3744 put_nfs_open_context(calldata->ctx);
3748 static void nfs4_locku_done(struct rpc_task *task, void *data)
3750 struct nfs4_unlockdata *calldata = data;
3752 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3754 if (RPC_ASSASSINATED(task))
3756 switch (task->tk_status) {
3758 memcpy(calldata->lsp->ls_stateid.data,
3759 calldata->res.stateid.data,
3760 sizeof(calldata->lsp->ls_stateid.data));
3761 renew_lease(calldata->server, calldata->timestamp);
3763 case -NFS4ERR_BAD_STATEID:
3764 case -NFS4ERR_OLD_STATEID:
3765 case -NFS4ERR_STALE_STATEID:
3766 case -NFS4ERR_EXPIRED:
3769 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3770 nfs4_restart_rpc(task,
3771 calldata->server->nfs_client,
3772 &calldata->res.seq_res);
3774 nfs4_sequence_free_slot(calldata->server->nfs_client,
3775 &calldata->res.seq_res);
3778 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3780 struct nfs4_unlockdata *calldata = data;
3782 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3784 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3785 /* Note: exit _without_ running nfs4_locku_done */
3786 task->tk_action = NULL;
3789 calldata->timestamp = jiffies;
3790 if (nfs4_setup_sequence(calldata->server->nfs_client,
3791 &calldata->arg.seq_args,
3792 &calldata->res.seq_res, 1, task))
3794 rpc_call_start(task);
3797 static const struct rpc_call_ops nfs4_locku_ops = {
3798 .rpc_call_prepare = nfs4_locku_prepare,
3799 .rpc_call_done = nfs4_locku_done,
3800 .rpc_release = nfs4_locku_release_calldata,
3803 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3804 struct nfs_open_context *ctx,
3805 struct nfs4_lock_state *lsp,
3806 struct nfs_seqid *seqid)
3808 struct nfs4_unlockdata *data;
3809 struct rpc_message msg = {
3810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3811 .rpc_cred = ctx->cred,
3813 struct rpc_task_setup task_setup_data = {
3814 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3815 .rpc_message = &msg,
3816 .callback_ops = &nfs4_locku_ops,
3817 .workqueue = nfsiod_workqueue,
3818 .flags = RPC_TASK_ASYNC,
3821 /* Ensure this is an unlock - when canceling a lock, the
3822 * canceled lock is passed in, and it won't be an unlock.
3824 fl->fl_type = F_UNLCK;
3826 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3828 nfs_free_seqid(seqid);
3829 return ERR_PTR(-ENOMEM);
3832 msg.rpc_argp = &data->arg,
3833 msg.rpc_resp = &data->res,
3834 task_setup_data.callback_data = data;
3835 return rpc_run_task(&task_setup_data);
3838 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3840 struct nfs_inode *nfsi = NFS_I(state->inode);
3841 struct nfs_seqid *seqid;
3842 struct nfs4_lock_state *lsp;
3843 struct rpc_task *task;
3845 unsigned char fl_flags = request->fl_flags;
3847 status = nfs4_set_lock_state(state, request);
3848 /* Unlock _before_ we do the RPC call */
3849 request->fl_flags |= FL_EXISTS;
3850 down_read(&nfsi->rwsem);
3851 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3852 up_read(&nfsi->rwsem);
3855 up_read(&nfsi->rwsem);
3858 /* Is this a delegated lock? */
3859 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3861 lsp = request->fl_u.nfs4_fl.owner;
3862 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3866 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3867 status = PTR_ERR(task);
3870 status = nfs4_wait_for_completion_rpc_task(task);
3873 request->fl_flags = fl_flags;
3877 struct nfs4_lockdata {
3878 struct nfs_lock_args arg;
3879 struct nfs_lock_res res;
3880 struct nfs4_lock_state *lsp;
3881 struct nfs_open_context *ctx;
3882 struct file_lock fl;
3883 unsigned long timestamp;
3886 struct nfs_server *server;
3889 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3890 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3892 struct nfs4_lockdata *p;
3893 struct inode *inode = lsp->ls_state->inode;
3894 struct nfs_server *server = NFS_SERVER(inode);
3896 p = kzalloc(sizeof(*p), GFP_KERNEL);
3900 p->arg.fh = NFS_FH(inode);
3902 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3903 if (p->arg.open_seqid == NULL)
3905 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3906 if (p->arg.lock_seqid == NULL)
3907 goto out_free_seqid;
3908 p->arg.lock_stateid = &lsp->ls_stateid;
3909 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3910 p->arg.lock_owner.id = lsp->ls_id.id;
3911 p->res.lock_seqid = p->arg.lock_seqid;
3912 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3915 atomic_inc(&lsp->ls_count);
3916 p->ctx = get_nfs_open_context(ctx);
3917 memcpy(&p->fl, fl, sizeof(p->fl));
3920 nfs_free_seqid(p->arg.open_seqid);
3926 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3928 struct nfs4_lockdata *data = calldata;
3929 struct nfs4_state *state = data->lsp->ls_state;
3931 dprintk("%s: begin!\n", __func__);
3932 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3934 /* Do we need to do an open_to_lock_owner? */
3935 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3936 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3938 data->arg.open_stateid = &state->stateid;
3939 data->arg.new_lock_owner = 1;
3940 data->res.open_seqid = data->arg.open_seqid;
3942 data->arg.new_lock_owner = 0;
3943 data->timestamp = jiffies;
3944 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3945 &data->res.seq_res, 1, task))
3947 rpc_call_start(task);
3948 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3951 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3953 struct nfs4_lockdata *data = calldata;
3955 dprintk("%s: begin!\n", __func__);
3957 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3960 data->rpc_status = task->tk_status;
3961 if (RPC_ASSASSINATED(task))
3963 if (data->arg.new_lock_owner != 0) {
3964 if (data->rpc_status == 0)
3965 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3969 if (data->rpc_status == 0) {
3970 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3971 sizeof(data->lsp->ls_stateid.data));
3972 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3973 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3976 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3979 static void nfs4_lock_release(void *calldata)
3981 struct nfs4_lockdata *data = calldata;
3983 dprintk("%s: begin!\n", __func__);
3984 nfs_free_seqid(data->arg.open_seqid);
3985 if (data->cancelled != 0) {
3986 struct rpc_task *task;
3987 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3988 data->arg.lock_seqid);
3991 dprintk("%s: cancelling lock!\n", __func__);
3993 nfs_free_seqid(data->arg.lock_seqid);
3994 nfs4_put_lock_state(data->lsp);
3995 put_nfs_open_context(data->ctx);
3997 dprintk("%s: done!\n", __func__);
4000 static const struct rpc_call_ops nfs4_lock_ops = {
4001 .rpc_call_prepare = nfs4_lock_prepare,
4002 .rpc_call_done = nfs4_lock_done,
4003 .rpc_release = nfs4_lock_release,
4006 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
4008 struct nfs4_lockdata *data;
4009 struct rpc_task *task;
4010 struct rpc_message msg = {
4011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4012 .rpc_cred = state->owner->so_cred,
4014 struct rpc_task_setup task_setup_data = {
4015 .rpc_client = NFS_CLIENT(state->inode),
4016 .rpc_message = &msg,
4017 .callback_ops = &nfs4_lock_ops,
4018 .workqueue = nfsiod_workqueue,
4019 .flags = RPC_TASK_ASYNC,
4023 dprintk("%s: begin!\n", __func__);
4024 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4025 fl->fl_u.nfs4_fl.owner);
4029 data->arg.block = 1;
4031 data->arg.reclaim = 1;
4032 msg.rpc_argp = &data->arg,
4033 msg.rpc_resp = &data->res,
4034 task_setup_data.callback_data = data;
4035 task = rpc_run_task(&task_setup_data);
4037 return PTR_ERR(task);
4038 ret = nfs4_wait_for_completion_rpc_task(task);
4040 ret = data->rpc_status;
4042 data->cancelled = 1;
4044 dprintk("%s: done, ret = %d!\n", __func__, ret);
4048 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4050 struct nfs_server *server = NFS_SERVER(state->inode);
4051 struct nfs4_exception exception = { };
4055 /* Cache the lock if possible... */
4056 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4058 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4059 if (err != -NFS4ERR_DELAY)
4061 nfs4_handle_exception(server, err, &exception);
4062 } while (exception.retry);
4066 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4068 struct nfs_server *server = NFS_SERVER(state->inode);
4069 struct nfs4_exception exception = { };
4072 err = nfs4_set_lock_state(state, request);
4076 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4078 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4082 case -NFS4ERR_GRACE:
4083 case -NFS4ERR_DELAY:
4084 nfs4_handle_exception(server, err, &exception);
4087 } while (exception.retry);
4092 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4094 struct nfs_inode *nfsi = NFS_I(state->inode);
4095 unsigned char fl_flags = request->fl_flags;
4098 /* Is this a delegated open? */
4099 status = nfs4_set_lock_state(state, request);
4102 request->fl_flags |= FL_ACCESS;
4103 status = do_vfs_lock(request->fl_file, request);
4106 down_read(&nfsi->rwsem);
4107 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4108 /* Yes: cache locks! */
4109 /* ...but avoid races with delegation recall... */
4110 request->fl_flags = fl_flags & ~FL_SLEEP;
4111 status = do_vfs_lock(request->fl_file, request);
4114 status = _nfs4_do_setlk(state, cmd, request, 0);
4117 /* Note: we always want to sleep here! */
4118 request->fl_flags = fl_flags | FL_SLEEP;
4119 if (do_vfs_lock(request->fl_file, request) < 0)
4120 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4122 up_read(&nfsi->rwsem);
4124 request->fl_flags = fl_flags;
4128 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4130 struct nfs4_exception exception = { };
4134 err = _nfs4_proc_setlk(state, cmd, request);
4135 if (err == -NFS4ERR_DENIED)
4137 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4139 } while (exception.retry);
4144 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4146 struct nfs_open_context *ctx;
4147 struct nfs4_state *state;
4148 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4151 /* verify open state */
4152 ctx = nfs_file_open_context(filp);
4155 if (request->fl_start < 0 || request->fl_end < 0)
4158 if (IS_GETLK(cmd)) {
4160 return nfs4_proc_getlk(state, F_GETLK, request);
4164 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4167 if (request->fl_type == F_UNLCK) {
4169 return nfs4_proc_unlck(state, cmd, request);
4176 status = nfs4_proc_setlk(state, cmd, request);
4177 if ((status != -EAGAIN) || IS_SETLK(cmd))
4179 timeout = nfs4_set_lock_task_retry(timeout);
4180 status = -ERESTARTSYS;
4183 } while(status < 0);
4187 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4189 struct nfs_server *server = NFS_SERVER(state->inode);
4190 struct nfs4_exception exception = { };
4193 err = nfs4_set_lock_state(state, fl);
4197 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4200 printk(KERN_ERR "%s: unhandled error %d.\n",
4205 case -NFS4ERR_EXPIRED:
4206 case -NFS4ERR_STALE_CLIENTID:
4207 case -NFS4ERR_STALE_STATEID:
4208 nfs4_schedule_state_recovery(server->nfs_client);
4212 * The show must go on: exit, but mark the
4213 * stateid as needing recovery.
4215 case -NFS4ERR_ADMIN_REVOKED:
4216 case -NFS4ERR_BAD_STATEID:
4217 case -NFS4ERR_OPENMODE:
4218 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4222 case -NFS4ERR_DENIED:
4223 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4226 case -NFS4ERR_DELAY:
4229 err = nfs4_handle_exception(server, err, &exception);
4230 } while (exception.retry);
4235 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4237 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4238 size_t buflen, int flags)
4240 struct inode *inode = dentry->d_inode;
4242 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4245 return nfs4_proc_set_acl(inode, buf, buflen);
4248 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4249 * and that's what we'll do for e.g. user attributes that haven't been set.
4250 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4251 * attributes in kernel-managed attribute namespaces. */
4252 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4255 struct inode *inode = dentry->d_inode;
4257 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4260 return nfs4_proc_get_acl(inode, buf, buflen);
4263 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4265 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4267 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4269 if (buf && buflen < len)
4272 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4276 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4278 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4279 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4280 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4283 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4284 NFS_ATTR_FATTR_NLINK;
4285 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4289 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4290 struct nfs4_fs_locations *fs_locations, struct page *page)
4292 struct nfs_server *server = NFS_SERVER(dir);
4294 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4295 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4297 struct nfs4_fs_locations_arg args = {
4298 .dir_fh = NFS_FH(dir),
4303 struct nfs4_fs_locations_res res = {
4304 .fs_locations = fs_locations,
4306 struct rpc_message msg = {
4307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4313 dprintk("%s: start\n", __func__);
4314 nfs_fattr_init(&fs_locations->fattr);
4315 fs_locations->server = server;
4316 fs_locations->nlocations = 0;
4317 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4318 nfs_fixup_referral_attributes(&fs_locations->fattr);
4319 dprintk("%s: returned status = %d\n", __func__, status);
4323 #ifdef CONFIG_NFS_V4_1
4325 * nfs4_proc_exchange_id()
4327 * Since the clientid has expired, all compounds using sessions
4328 * associated with the stale clientid will be returning
4329 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4330 * be in some phase of session reset.
4332 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4334 nfs4_verifier verifier;
4335 struct nfs41_exchange_id_args args = {
4337 .flags = clp->cl_exchange_flags,
4339 struct nfs41_exchange_id_res res = {
4343 struct rpc_message msg = {
4344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4351 dprintk("--> %s\n", __func__);
4352 BUG_ON(clp == NULL);
4354 /* Remove server-only flags */
4355 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4357 p = (u32 *)verifier.data;
4358 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4359 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4360 args.verifier = &verifier;
4363 args.id_len = scnprintf(args.id, sizeof(args.id),
4366 rpc_peeraddr2str(clp->cl_rpcclient,
4368 clp->cl_id_uniquifier);
4370 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4372 if (status != NFS4ERR_CLID_INUSE)
4378 if (++clp->cl_id_uniquifier == 0)
4382 dprintk("<-- %s status= %d\n", __func__, status);
4386 struct nfs4_get_lease_time_data {
4387 struct nfs4_get_lease_time_args *args;
4388 struct nfs4_get_lease_time_res *res;
4389 struct nfs_client *clp;
4392 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4396 struct nfs4_get_lease_time_data *data =
4397 (struct nfs4_get_lease_time_data *)calldata;
4399 dprintk("--> %s\n", __func__);
4400 /* just setup sequence, do not trigger session recovery
4401 since we're invoked within one */
4402 ret = nfs41_setup_sequence(data->clp->cl_session,
4403 &data->args->la_seq_args,
4404 &data->res->lr_seq_res, 0, task);
4406 BUG_ON(ret == -EAGAIN);
4407 rpc_call_start(task);
4408 dprintk("<-- %s\n", __func__);
4412 * Called from nfs4_state_manager thread for session setup, so don't recover
4413 * from sequence operation or clientid errors.
4415 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4417 struct nfs4_get_lease_time_data *data =
4418 (struct nfs4_get_lease_time_data *)calldata;
4420 dprintk("--> %s\n", __func__);
4421 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4422 switch (task->tk_status) {
4423 case -NFS4ERR_DELAY:
4424 case -NFS4ERR_GRACE:
4425 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4426 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4427 task->tk_status = 0;
4428 rpc_restart_call(task);
4431 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4432 dprintk("<-- %s\n", __func__);
4435 struct rpc_call_ops nfs4_get_lease_time_ops = {
4436 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4437 .rpc_call_done = nfs4_get_lease_time_done,
4440 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4442 struct rpc_task *task;
4443 struct nfs4_get_lease_time_args args;
4444 struct nfs4_get_lease_time_res res = {
4445 .lr_fsinfo = fsinfo,
4447 struct nfs4_get_lease_time_data data = {
4452 struct rpc_message msg = {
4453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4457 struct rpc_task_setup task_setup = {
4458 .rpc_client = clp->cl_rpcclient,
4459 .rpc_message = &msg,
4460 .callback_ops = &nfs4_get_lease_time_ops,
4461 .callback_data = &data
4465 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4466 dprintk("--> %s\n", __func__);
4467 task = rpc_run_task(&task_setup);
4470 status = PTR_ERR(task);
4472 status = task->tk_status;
4475 dprintk("<-- %s return %d\n", __func__, status);
4481 * Reset a slot table
4483 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4484 int old_max_slots, int ivalue)
4489 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
4492 * Until we have dynamic slot table adjustment, insist
4493 * upon the same slot table size
4495 if (max_slots != old_max_slots) {
4496 dprintk("%s reset slot table does't match old\n",
4498 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4501 spin_lock(&tbl->slot_tbl_lock);
4502 for (i = 0; i < max_slots; ++i)
4503 tbl->slots[i].seq_nr = ivalue;
4504 tbl->highest_used_slotid = -1;
4505 spin_unlock(&tbl->slot_tbl_lock);
4506 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4507 tbl, tbl->slots, tbl->max_slots);
4509 dprintk("<-- %s: return %d\n", __func__, ret);
4514 * Reset the forechannel and backchannel slot tables
4516 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4520 status = nfs4_reset_slot_table(&session->fc_slot_table,
4521 session->fc_attrs.max_reqs,
4522 session->fc_slot_table.max_slots,
4526 init_completion(&session->complete);
4528 status = nfs4_reset_slot_table(&session->bc_slot_table,
4529 session->bc_attrs.max_reqs,
4530 session->bc_slot_table.max_slots,
4535 /* Destroy the slot table */
4536 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4538 if (session->fc_slot_table.slots != NULL) {
4539 kfree(session->fc_slot_table.slots);
4540 session->fc_slot_table.slots = NULL;
4542 if (session->bc_slot_table.slots != NULL) {
4543 kfree(session->bc_slot_table.slots);
4544 session->bc_slot_table.slots = NULL;
4550 * Initialize slot table
4552 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4553 int max_slots, int ivalue)
4556 struct nfs4_slot *slot;
4559 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4561 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4563 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4566 for (i = 0; i < max_slots; ++i)
4567 slot[i].seq_nr = ivalue;
4570 spin_lock(&tbl->slot_tbl_lock);
4571 if (tbl->slots != NULL) {
4572 spin_unlock(&tbl->slot_tbl_lock);
4573 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4574 __func__, tbl, tbl->slots);
4578 tbl->max_slots = max_slots;
4580 tbl->highest_used_slotid = -1; /* no slot is currently used */
4581 spin_unlock(&tbl->slot_tbl_lock);
4582 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4583 tbl, tbl->slots, tbl->max_slots);
4585 dprintk("<-- %s: return %d\n", __func__, ret);
4594 * Initialize the forechannel and backchannel tables
4596 static int nfs4_init_slot_tables(struct nfs4_session *session)
4600 status = nfs4_init_slot_table(&session->fc_slot_table,
4601 session->fc_attrs.max_reqs, 1);
4605 status = nfs4_init_slot_table(&session->bc_slot_table,
4606 session->bc_attrs.max_reqs, 0);
4608 nfs4_destroy_slot_tables(session);
4613 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4615 struct nfs4_session *session;
4616 struct nfs4_slot_table *tbl;
4618 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4623 * The create session reply races with the server back
4624 * channel probe. Mark the client NFS_CS_SESSION_INITING
4625 * so that the client back channel can find the
4628 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4629 init_completion(&session->complete);
4631 tbl = &session->fc_slot_table;
4632 spin_lock_init(&tbl->slot_tbl_lock);
4633 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4635 tbl = &session->bc_slot_table;
4636 spin_lock_init(&tbl->slot_tbl_lock);
4637 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4643 void nfs4_destroy_session(struct nfs4_session *session)
4645 nfs4_proc_destroy_session(session);
4646 dprintk("%s Destroy backchannel for xprt %p\n",
4647 __func__, session->clp->cl_rpcclient->cl_xprt);
4648 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4649 NFS41_BC_MIN_CALLBACKS);
4650 nfs4_destroy_slot_tables(session);
4655 * Initialize the values to be used by the client in CREATE_SESSION
4656 * If nfs4_init_session set the fore channel request and response sizes,
4659 * Set the back channel max_resp_sz_cached to zero to force the client to
4660 * always set csa_cachethis to FALSE because the current implementation
4661 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4663 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4665 struct nfs4_session *session = args->client->cl_session;
4666 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4667 mxresp_sz = session->fc_attrs.max_resp_sz;
4670 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4672 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4673 /* Fore channel attributes */
4674 args->fc_attrs.headerpadsz = 0;
4675 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4676 args->fc_attrs.max_resp_sz = mxresp_sz;
4677 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4678 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4679 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4681 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4682 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4684 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4685 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4686 args->fc_attrs.max_reqs);
4688 /* Back channel attributes */
4689 args->bc_attrs.headerpadsz = 0;
4690 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4691 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4692 args->bc_attrs.max_resp_sz_cached = 0;
4693 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4694 args->bc_attrs.max_reqs = 1;
4696 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4697 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4699 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4700 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4701 args->bc_attrs.max_reqs);
4704 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4708 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4709 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4713 #define _verify_fore_channel_attr(_name_) \
4714 _verify_channel_attr("fore", #_name_, \
4715 args->fc_attrs._name_, \
4716 session->fc_attrs._name_)
4718 #define _verify_back_channel_attr(_name_) \
4719 _verify_channel_attr("back", #_name_, \
4720 args->bc_attrs._name_, \
4721 session->bc_attrs._name_)
4724 * The server is not allowed to increase the fore channel header pad size,
4725 * maximum response size, or maximum number of operations.
4727 * The back channel attributes are only negotiatied down: We send what the
4728 * (back channel) server insists upon.
4730 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4731 struct nfs4_session *session)
4735 ret |= _verify_fore_channel_attr(headerpadsz);
4736 ret |= _verify_fore_channel_attr(max_resp_sz);
4737 ret |= _verify_fore_channel_attr(max_ops);
4739 ret |= _verify_back_channel_attr(headerpadsz);
4740 ret |= _verify_back_channel_attr(max_rqst_sz);
4741 ret |= _verify_back_channel_attr(max_resp_sz);
4742 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4743 ret |= _verify_back_channel_attr(max_ops);
4744 ret |= _verify_back_channel_attr(max_reqs);
4749 static int _nfs4_proc_create_session(struct nfs_client *clp)
4751 struct nfs4_session *session = clp->cl_session;
4752 struct nfs41_create_session_args args = {
4754 .cb_program = NFS4_CALLBACK,
4756 struct nfs41_create_session_res res = {
4759 struct rpc_message msg = {
4760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4766 nfs4_init_channel_attrs(&args);
4767 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4769 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4772 /* Verify the session's negotiated channel_attrs values */
4773 status = nfs4_verify_channel_attrs(&args, session);
4775 /* Increment the clientid slot sequence id */
4783 * Issues a CREATE_SESSION operation to the server.
4784 * It is the responsibility of the caller to verify the session is
4785 * expired before calling this routine.
4787 int nfs4_proc_create_session(struct nfs_client *clp, int reset)
4791 struct nfs_fsinfo fsinfo;
4792 struct nfs4_session *session = clp->cl_session;
4794 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4796 status = _nfs4_proc_create_session(clp);
4800 /* Init or reset the fore channel */
4802 status = nfs4_reset_slot_tables(session);
4804 status = nfs4_init_slot_tables(session);
4805 dprintk("fore channel slot table initialization returned %d\n", status);
4809 ptr = (unsigned *)&session->sess_id.data[0];
4810 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4811 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4814 /* Lease time is aleady set */
4817 /* Get the lease time */
4818 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4820 /* Update lease time and schedule renewal */
4821 spin_lock(&clp->cl_lock);
4822 clp->cl_lease_time = fsinfo.lease_time * HZ;
4823 clp->cl_last_renewal = jiffies;
4824 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4825 spin_unlock(&clp->cl_lock);
4827 nfs4_schedule_state_renewal(clp);
4830 dprintk("<-- %s\n", __func__);
4835 * Issue the over-the-wire RPC DESTROY_SESSION.
4836 * The caller must serialize access to this routine.
4838 int nfs4_proc_destroy_session(struct nfs4_session *session)
4841 struct rpc_message msg;
4843 dprintk("--> nfs4_proc_destroy_session\n");
4845 /* session is still being setup */
4846 if (session->clp->cl_cons_state != NFS_CS_READY)
4849 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4850 msg.rpc_argp = session;
4851 msg.rpc_resp = NULL;
4852 msg.rpc_cred = NULL;
4853 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4857 "Got error %d from the server on DESTROY_SESSION. "
4858 "Session has been destroyed regardless...\n", status);
4860 dprintk("<-- nfs4_proc_destroy_session\n");
4864 int nfs4_init_session(struct nfs_server *server)
4866 struct nfs_client *clp = server->nfs_client;
4867 struct nfs4_session *session;
4870 if (!nfs4_has_session(clp))
4873 session = clp->cl_session;
4874 session->fc_attrs.max_rqst_sz = server->wsize + nfs41_maxwrite_overhead;
4875 session->fc_attrs.max_resp_sz = server->rsize + nfs41_maxread_overhead;
4877 ret = nfs4_recover_expired_lease(server);
4879 ret = nfs4_check_client_ready(clp);
4884 * Renew the cl_session lease.
4886 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4888 struct nfs4_sequence_args args;
4889 struct nfs4_sequence_res res;
4891 struct rpc_message msg = {
4892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4898 args.sa_cache_this = 0;
4900 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4904 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4906 struct nfs_client *clp = (struct nfs_client *)data;
4908 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4910 if (task->tk_status < 0) {
4911 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4913 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4915 nfs4_restart_rpc(task, clp, task->tk_msg.rpc_resp);
4919 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4920 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4922 kfree(task->tk_msg.rpc_argp);
4923 kfree(task->tk_msg.rpc_resp);
4925 dprintk("<-- %s\n", __func__);
4928 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4930 struct nfs_client *clp;
4931 struct nfs4_sequence_args *args;
4932 struct nfs4_sequence_res *res;
4934 clp = (struct nfs_client *)data;
4935 args = task->tk_msg.rpc_argp;
4936 res = task->tk_msg.rpc_resp;
4938 if (nfs4_setup_sequence(clp, args, res, 0, task))
4940 rpc_call_start(task);
4943 static const struct rpc_call_ops nfs41_sequence_ops = {
4944 .rpc_call_done = nfs41_sequence_call_done,
4945 .rpc_call_prepare = nfs41_sequence_prepare,
4948 static int nfs41_proc_async_sequence(struct nfs_client *clp,
4949 struct rpc_cred *cred)
4951 struct nfs4_sequence_args *args;
4952 struct nfs4_sequence_res *res;
4953 struct rpc_message msg = {
4954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4958 args = kzalloc(sizeof(*args), GFP_KERNEL);
4961 res = kzalloc(sizeof(*res), GFP_KERNEL);
4966 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4967 msg.rpc_argp = args;
4970 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4971 &nfs41_sequence_ops, (void *)clp);
4974 #endif /* CONFIG_NFS_V4_1 */
4976 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
4977 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4978 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4979 .recover_open = nfs4_open_reclaim,
4980 .recover_lock = nfs4_lock_reclaim,
4981 .establish_clid = nfs4_init_clientid,
4982 .get_clid_cred = nfs4_get_setclientid_cred,
4985 #if defined(CONFIG_NFS_V4_1)
4986 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4987 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4988 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4989 .recover_open = nfs4_open_reclaim,
4990 .recover_lock = nfs4_lock_reclaim,
4991 .establish_clid = nfs41_init_clientid,
4992 .get_clid_cred = nfs4_get_exchange_id_cred,
4994 #endif /* CONFIG_NFS_V4_1 */
4996 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
4997 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4998 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4999 .recover_open = nfs4_open_expired,
5000 .recover_lock = nfs4_lock_expired,
5001 .establish_clid = nfs4_init_clientid,
5002 .get_clid_cred = nfs4_get_setclientid_cred,
5005 #if defined(CONFIG_NFS_V4_1)
5006 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5007 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5008 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5009 .recover_open = nfs4_open_expired,
5010 .recover_lock = nfs4_lock_expired,
5011 .establish_clid = nfs41_init_clientid,
5012 .get_clid_cred = nfs4_get_exchange_id_cred,
5014 #endif /* CONFIG_NFS_V4_1 */
5016 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5017 .sched_state_renewal = nfs4_proc_async_renew,
5018 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5019 .renew_lease = nfs4_proc_renew,
5022 #if defined(CONFIG_NFS_V4_1)
5023 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5024 .sched_state_renewal = nfs41_proc_async_sequence,
5025 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5026 .renew_lease = nfs4_proc_sequence,
5031 * Per minor version reboot and network partition recovery ops
5034 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5035 &nfs40_reboot_recovery_ops,
5036 #if defined(CONFIG_NFS_V4_1)
5037 &nfs41_reboot_recovery_ops,
5041 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5042 &nfs40_nograce_recovery_ops,
5043 #if defined(CONFIG_NFS_V4_1)
5044 &nfs41_nograce_recovery_ops,
5048 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5049 &nfs40_state_renewal_ops,
5050 #if defined(CONFIG_NFS_V4_1)
5051 &nfs41_state_renewal_ops,
5055 static const struct inode_operations nfs4_file_inode_operations = {
5056 .permission = nfs_permission,
5057 .getattr = nfs_getattr,
5058 .setattr = nfs_setattr,
5059 .getxattr = nfs4_getxattr,
5060 .setxattr = nfs4_setxattr,
5061 .listxattr = nfs4_listxattr,
5064 const struct nfs_rpc_ops nfs_v4_clientops = {
5065 .version = 4, /* protocol version */
5066 .dentry_ops = &nfs4_dentry_operations,
5067 .dir_inode_ops = &nfs4_dir_inode_operations,
5068 .file_inode_ops = &nfs4_file_inode_operations,
5069 .getroot = nfs4_proc_get_root,
5070 .getattr = nfs4_proc_getattr,
5071 .setattr = nfs4_proc_setattr,
5072 .lookupfh = nfs4_proc_lookupfh,
5073 .lookup = nfs4_proc_lookup,
5074 .access = nfs4_proc_access,
5075 .readlink = nfs4_proc_readlink,
5076 .create = nfs4_proc_create,
5077 .remove = nfs4_proc_remove,
5078 .unlink_setup = nfs4_proc_unlink_setup,
5079 .unlink_done = nfs4_proc_unlink_done,
5080 .rename = nfs4_proc_rename,
5081 .link = nfs4_proc_link,
5082 .symlink = nfs4_proc_symlink,
5083 .mkdir = nfs4_proc_mkdir,
5084 .rmdir = nfs4_proc_remove,
5085 .readdir = nfs4_proc_readdir,
5086 .mknod = nfs4_proc_mknod,
5087 .statfs = nfs4_proc_statfs,
5088 .fsinfo = nfs4_proc_fsinfo,
5089 .pathconf = nfs4_proc_pathconf,
5090 .set_capabilities = nfs4_server_capabilities,
5091 .decode_dirent = nfs4_decode_dirent,
5092 .read_setup = nfs4_proc_read_setup,
5093 .read_done = nfs4_read_done,
5094 .write_setup = nfs4_proc_write_setup,
5095 .write_done = nfs4_write_done,
5096 .commit_setup = nfs4_proc_commit_setup,
5097 .commit_done = nfs4_commit_done,
5098 .lock = nfs4_proc_lock,
5099 .clear_acl_cache = nfs4_zap_acl_attr,
5100 .close_context = nfs4_close_context,