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/utsname.h>
40 #include <linux/delay.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/smp_lock.h>
49 #include <linux/namei.h>
50 #include <linux/mount.h>
51 #include <linux/module.h>
54 #include "delegation.h"
59 #define NFSDBG_FACILITY NFSDBG_PROC
61 #define NFS4_POLL_RETRY_MIN (HZ/10)
62 #define NFS4_POLL_RETRY_MAX (15*HZ)
65 static int _nfs4_proc_open(struct nfs4_opendata *data);
66 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
67 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
68 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
69 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
71 /* Prevent leaks of NFSv4 errors into userland */
72 static int nfs4_map_errors(int err)
75 dprintk("%s could not handle NFSv4 error %d\n",
83 * This is our standard bitmap for GETATTR requests.
85 const u32 nfs4_fattr_bitmap[2] = {
90 | FATTR4_WORD0_FILEID,
92 | FATTR4_WORD1_NUMLINKS
94 | FATTR4_WORD1_OWNER_GROUP
96 | FATTR4_WORD1_SPACE_USED
97 | FATTR4_WORD1_TIME_ACCESS
98 | FATTR4_WORD1_TIME_METADATA
99 | FATTR4_WORD1_TIME_MODIFY
102 const u32 nfs4_statfs_bitmap[2] = {
103 FATTR4_WORD0_FILES_AVAIL
104 | FATTR4_WORD0_FILES_FREE
105 | FATTR4_WORD0_FILES_TOTAL,
106 FATTR4_WORD1_SPACE_AVAIL
107 | FATTR4_WORD1_SPACE_FREE
108 | FATTR4_WORD1_SPACE_TOTAL
111 const u32 nfs4_pathconf_bitmap[2] = {
113 | FATTR4_WORD0_MAXNAME,
117 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
118 | FATTR4_WORD0_MAXREAD
119 | FATTR4_WORD0_MAXWRITE
120 | FATTR4_WORD0_LEASE_TIME,
124 const u32 nfs4_fs_locations_bitmap[2] = {
126 | FATTR4_WORD0_CHANGE
129 | FATTR4_WORD0_FILEID
130 | FATTR4_WORD0_FS_LOCATIONS,
132 | FATTR4_WORD1_NUMLINKS
134 | FATTR4_WORD1_OWNER_GROUP
135 | FATTR4_WORD1_RAWDEV
136 | FATTR4_WORD1_SPACE_USED
137 | FATTR4_WORD1_TIME_ACCESS
138 | FATTR4_WORD1_TIME_METADATA
139 | FATTR4_WORD1_TIME_MODIFY
140 | FATTR4_WORD1_MOUNTED_ON_FILEID
143 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
144 struct nfs4_readdir_arg *readdir)
148 BUG_ON(readdir->count < 80);
150 readdir->cookie = cookie;
151 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
156 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
161 * NFSv4 servers do not return entries for '.' and '..'
162 * Therefore, we fake these entries here. We let '.'
163 * have cookie 0 and '..' have cookie 1. Note that
164 * when talking to the server, we always send cookie 0
167 start = p = kmap_atomic(*readdir->pages, KM_USER0);
170 *p++ = xdr_one; /* next */
171 *p++ = xdr_zero; /* cookie, first word */
172 *p++ = xdr_one; /* cookie, second word */
173 *p++ = xdr_one; /* entry len */
174 memcpy(p, ".\0\0\0", 4); /* entry */
176 *p++ = xdr_one; /* bitmap length */
177 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
178 *p++ = htonl(8); /* attribute buffer length */
179 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
182 *p++ = xdr_one; /* next */
183 *p++ = xdr_zero; /* cookie, first word */
184 *p++ = xdr_two; /* cookie, second word */
185 *p++ = xdr_two; /* entry len */
186 memcpy(p, "..\0\0", 4); /* entry */
188 *p++ = xdr_one; /* bitmap length */
189 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
190 *p++ = htonl(8); /* attribute buffer length */
191 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
193 readdir->pgbase = (char *)p - (char *)start;
194 readdir->count -= readdir->pgbase;
195 kunmap_atomic(start, KM_USER0);
198 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
204 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
205 nfs_wait_bit_killable, TASK_KILLABLE);
209 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
216 *timeout = NFS4_POLL_RETRY_MIN;
217 if (*timeout > NFS4_POLL_RETRY_MAX)
218 *timeout = NFS4_POLL_RETRY_MAX;
219 schedule_timeout_killable(*timeout);
220 if (fatal_signal_pending(current))
226 /* This is the error handling routine for processes that are allowed
229 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
231 struct nfs_client *clp = server->nfs_client;
232 struct nfs4_state *state = exception->state;
235 exception->retry = 0;
239 case -NFS4ERR_ADMIN_REVOKED:
240 case -NFS4ERR_BAD_STATEID:
241 case -NFS4ERR_OPENMODE:
244 nfs4_state_mark_reclaim_nograce(clp, state);
245 case -NFS4ERR_STALE_CLIENTID:
246 case -NFS4ERR_STALE_STATEID:
247 case -NFS4ERR_EXPIRED:
248 nfs4_schedule_state_recovery(clp);
249 ret = nfs4_wait_clnt_recover(clp);
251 exception->retry = 1;
252 #if !defined(CONFIG_NFS_V4_1)
254 #else /* !defined(CONFIG_NFS_V4_1) */
255 if (!nfs4_has_session(server->nfs_client))
258 case -NFS4ERR_BADSESSION:
259 case -NFS4ERR_BADSLOT:
260 case -NFS4ERR_BAD_HIGH_SLOT:
261 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
262 case -NFS4ERR_DEADSESSION:
263 case -NFS4ERR_SEQ_FALSE_RETRY:
264 case -NFS4ERR_SEQ_MISORDERED:
265 dprintk("%s ERROR: %d Reset session\n", __func__,
267 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
268 exception->retry = 1;
270 #endif /* !defined(CONFIG_NFS_V4_1) */
271 case -NFS4ERR_FILE_OPEN:
274 ret = nfs4_delay(server->client, &exception->timeout);
277 case -NFS4ERR_OLD_STATEID:
278 exception->retry = 1;
280 /* We failed to handle the error */
281 return nfs4_map_errors(ret);
285 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
287 struct nfs_client *clp = server->nfs_client;
288 spin_lock(&clp->cl_lock);
289 if (time_before(clp->cl_last_renewal,timestamp))
290 clp->cl_last_renewal = timestamp;
291 spin_unlock(&clp->cl_lock);
294 #if defined(CONFIG_NFS_V4_1)
297 * nfs4_free_slot - free a slot and efficiently update slot table.
299 * freeing a slot is trivially done by clearing its respective bit
301 * If the freed slotid equals highest_used_slotid we want to update it
302 * so that the server would be able to size down the slot table if needed,
303 * otherwise we know that the highest_used_slotid is still in use.
304 * When updating highest_used_slotid there may be "holes" in the bitmap
305 * so we need to scan down from highest_used_slotid to 0 looking for the now
306 * highest slotid in use.
307 * If none found, highest_used_slotid is set to -1.
310 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
312 int slotid = free_slotid;
314 spin_lock(&tbl->slot_tbl_lock);
315 /* clear used bit in bitmap */
316 __clear_bit(slotid, tbl->used_slots);
318 /* update highest_used_slotid when it is freed */
319 if (slotid == tbl->highest_used_slotid) {
320 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
321 if (slotid >= 0 && slotid < tbl->max_slots)
322 tbl->highest_used_slotid = slotid;
324 tbl->highest_used_slotid = -1;
326 rpc_wake_up_next(&tbl->slot_tbl_waitq);
327 spin_unlock(&tbl->slot_tbl_lock);
328 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
329 free_slotid, tbl->highest_used_slotid);
332 void nfs41_sequence_free_slot(const struct nfs_client *clp,
333 struct nfs4_sequence_res *res)
335 struct nfs4_slot_table *tbl;
337 if (!nfs4_has_session(clp)) {
338 dprintk("%s: No session\n", __func__);
341 tbl = &clp->cl_session->fc_slot_table;
342 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
343 dprintk("%s: No slot\n", __func__);
344 /* just wake up the next guy waiting since
345 * we may have not consumed a slot after all */
346 rpc_wake_up_next(&tbl->slot_tbl_waitq);
349 nfs4_free_slot(tbl, res->sr_slotid);
350 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
353 static void nfs41_sequence_done(struct nfs_client *clp,
354 struct nfs4_sequence_res *res,
357 unsigned long timestamp;
358 struct nfs4_slot_table *tbl;
359 struct nfs4_slot *slot;
362 * sr_status remains 1 if an RPC level error occurred. The server
363 * may or may not have processed the sequence operation..
364 * Proceed as if the server received and processed the sequence
367 if (res->sr_status == 1)
368 res->sr_status = NFS_OK;
370 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
371 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
374 tbl = &clp->cl_session->fc_slot_table;
375 slot = tbl->slots + res->sr_slotid;
377 if (res->sr_status == 0) {
378 /* Update the slot's sequence and clientid lease timer */
380 timestamp = res->sr_renewal_time;
381 spin_lock(&clp->cl_lock);
382 if (time_before(clp->cl_last_renewal, timestamp))
383 clp->cl_last_renewal = timestamp;
384 spin_unlock(&clp->cl_lock);
388 /* The session may be reset by one of the error handlers. */
389 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
390 nfs41_sequence_free_slot(clp, res);
394 * nfs4_find_slot - efficiently look for a free slot
396 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
397 * If found, we mark the slot as used, update the highest_used_slotid,
398 * and respectively set up the sequence operation args.
399 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
401 * Note: must be called with under the slot_tbl_lock.
404 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
407 u8 ret_id = NFS4_MAX_SLOT_TABLE;
408 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
410 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
411 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
413 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
414 if (slotid >= tbl->max_slots)
416 __set_bit(slotid, tbl->used_slots);
417 if (slotid > tbl->highest_used_slotid)
418 tbl->highest_used_slotid = slotid;
421 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
422 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
426 static int nfs41_setup_sequence(struct nfs4_session *session,
427 struct nfs4_sequence_args *args,
428 struct nfs4_sequence_res *res,
430 struct rpc_task *task)
432 struct nfs4_slot *slot;
433 struct nfs4_slot_table *tbl;
436 dprintk("--> %s\n", __func__);
437 /* slot already allocated? */
438 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
441 memset(res, 0, sizeof(*res));
442 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
443 tbl = &session->fc_slot_table;
445 spin_lock(&tbl->slot_tbl_lock);
446 slotid = nfs4_find_slot(tbl, task);
447 if (slotid == NFS4_MAX_SLOT_TABLE) {
448 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
449 spin_unlock(&tbl->slot_tbl_lock);
450 dprintk("<-- %s: no free slots\n", __func__);
453 spin_unlock(&tbl->slot_tbl_lock);
455 slot = tbl->slots + slotid;
456 args->sa_session = session;
457 args->sa_slotid = slotid;
458 args->sa_cache_this = cache_reply;
460 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
462 res->sr_session = session;
463 res->sr_slotid = slotid;
464 res->sr_renewal_time = jiffies;
466 * sr_status is only set in decode_sequence, and so will remain
467 * set to 1 if an rpc level failure occurs.
473 int nfs4_setup_sequence(struct nfs_client *clp,
474 struct nfs4_sequence_args *args,
475 struct nfs4_sequence_res *res,
477 struct rpc_task *task)
481 dprintk("--> %s clp %p session %p sr_slotid %d\n",
482 __func__, clp, clp->cl_session, res->sr_slotid);
484 if (!nfs4_has_session(clp))
486 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
488 if (ret != -EAGAIN) {
489 /* terminate rpc task */
490 task->tk_status = ret;
491 task->tk_action = NULL;
494 dprintk("<-- %s status=%d\n", __func__, ret);
498 struct nfs41_call_sync_data {
499 struct nfs_client *clp;
500 struct nfs4_sequence_args *seq_args;
501 struct nfs4_sequence_res *seq_res;
505 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
507 struct nfs41_call_sync_data *data = calldata;
509 dprintk("--> %s data->clp->cl_session %p\n", __func__,
510 data->clp->cl_session);
511 if (nfs4_setup_sequence(data->clp, data->seq_args,
512 data->seq_res, data->cache_reply, task))
514 rpc_call_start(task);
517 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
519 struct nfs41_call_sync_data *data = calldata;
521 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
522 nfs41_sequence_free_slot(data->clp, data->seq_res);
525 struct rpc_call_ops nfs41_call_sync_ops = {
526 .rpc_call_prepare = nfs41_call_sync_prepare,
527 .rpc_call_done = nfs41_call_sync_done,
530 static int nfs4_call_sync_sequence(struct nfs_client *clp,
531 struct rpc_clnt *clnt,
532 struct rpc_message *msg,
533 struct nfs4_sequence_args *args,
534 struct nfs4_sequence_res *res,
538 struct rpc_task *task;
539 struct nfs41_call_sync_data data = {
543 .cache_reply = cache_reply,
545 struct rpc_task_setup task_setup = {
548 .callback_ops = &nfs41_call_sync_ops,
549 .callback_data = &data
552 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
553 task = rpc_run_task(&task_setup);
557 ret = task->tk_status;
563 int _nfs4_call_sync_session(struct nfs_server *server,
564 struct rpc_message *msg,
565 struct nfs4_sequence_args *args,
566 struct nfs4_sequence_res *res,
569 return nfs4_call_sync_sequence(server->nfs_client, server->client,
570 msg, args, res, cache_reply);
573 #endif /* CONFIG_NFS_V4_1 */
575 int _nfs4_call_sync(struct nfs_server *server,
576 struct rpc_message *msg,
577 struct nfs4_sequence_args *args,
578 struct nfs4_sequence_res *res,
581 args->sa_session = res->sr_session = NULL;
582 return rpc_call_sync(server->client, msg, 0);
585 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
586 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
587 &(res)->seq_res, (cache_reply))
589 static void nfs4_sequence_done(const struct nfs_server *server,
590 struct nfs4_sequence_res *res, int rpc_status)
592 #ifdef CONFIG_NFS_V4_1
593 if (nfs4_has_session(server->nfs_client))
594 nfs41_sequence_done(server->nfs_client, res, rpc_status);
595 #endif /* CONFIG_NFS_V4_1 */
598 /* no restart, therefore free slot here */
599 static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
600 struct nfs4_sequence_res *res,
603 nfs4_sequence_done(server, res, rpc_status);
604 nfs4_sequence_free_slot(server->nfs_client, res);
607 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
609 struct nfs_inode *nfsi = NFS_I(dir);
611 spin_lock(&dir->i_lock);
612 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
613 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
614 nfs_force_lookup_revalidate(dir);
615 nfsi->change_attr = cinfo->after;
616 spin_unlock(&dir->i_lock);
619 struct nfs4_opendata {
621 struct nfs_openargs o_arg;
622 struct nfs_openres o_res;
623 struct nfs_open_confirmargs c_arg;
624 struct nfs_open_confirmres c_res;
625 struct nfs_fattr f_attr;
626 struct nfs_fattr dir_attr;
629 struct nfs4_state_owner *owner;
630 struct nfs4_state *state;
632 unsigned long timestamp;
633 unsigned int rpc_done : 1;
639 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
641 p->o_res.f_attr = &p->f_attr;
642 p->o_res.dir_attr = &p->dir_attr;
643 p->o_res.seqid = p->o_arg.seqid;
644 p->c_res.seqid = p->c_arg.seqid;
645 p->o_res.server = p->o_arg.server;
646 nfs_fattr_init(&p->f_attr);
647 nfs_fattr_init(&p->dir_attr);
650 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
651 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
652 const struct iattr *attrs)
654 struct dentry *parent = dget_parent(path->dentry);
655 struct inode *dir = parent->d_inode;
656 struct nfs_server *server = NFS_SERVER(dir);
657 struct nfs4_opendata *p;
659 p = kzalloc(sizeof(*p), GFP_KERNEL);
662 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
663 if (p->o_arg.seqid == NULL)
665 p->path.mnt = mntget(path->mnt);
666 p->path.dentry = dget(path->dentry);
669 atomic_inc(&sp->so_count);
670 p->o_arg.fh = NFS_FH(dir);
671 p->o_arg.open_flags = flags;
672 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
673 p->o_arg.clientid = server->nfs_client->cl_clientid;
674 p->o_arg.id = sp->so_owner_id.id;
675 p->o_arg.name = &p->path.dentry->d_name;
676 p->o_arg.server = server;
677 p->o_arg.bitmask = server->attr_bitmask;
678 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
679 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
680 if (flags & O_EXCL) {
681 u32 *s = (u32 *) p->o_arg.u.verifier.data;
684 } else if (flags & O_CREAT) {
685 p->o_arg.u.attrs = &p->attrs;
686 memcpy(&p->attrs, attrs, sizeof(p->attrs));
688 p->c_arg.fh = &p->o_res.fh;
689 p->c_arg.stateid = &p->o_res.stateid;
690 p->c_arg.seqid = p->o_arg.seqid;
691 nfs4_init_opendata_res(p);
701 static void nfs4_opendata_free(struct kref *kref)
703 struct nfs4_opendata *p = container_of(kref,
704 struct nfs4_opendata, kref);
706 nfs_free_seqid(p->o_arg.seqid);
707 if (p->state != NULL)
708 nfs4_put_open_state(p->state);
709 nfs4_put_state_owner(p->owner);
715 static void nfs4_opendata_put(struct nfs4_opendata *p)
718 kref_put(&p->kref, nfs4_opendata_free);
721 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
725 ret = rpc_wait_for_completion_task(task);
729 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
733 if (open_mode & O_EXCL)
735 switch (mode & (FMODE_READ|FMODE_WRITE)) {
737 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
740 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
742 case FMODE_READ|FMODE_WRITE:
743 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
749 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
751 if ((delegation->type & fmode) != fmode)
753 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
755 nfs_mark_delegation_referenced(delegation);
759 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
768 case FMODE_READ|FMODE_WRITE:
771 nfs4_state_set_mode_locked(state, state->state | fmode);
774 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
776 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
777 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
778 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
781 set_bit(NFS_O_RDONLY_STATE, &state->flags);
784 set_bit(NFS_O_WRONLY_STATE, &state->flags);
786 case FMODE_READ|FMODE_WRITE:
787 set_bit(NFS_O_RDWR_STATE, &state->flags);
791 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
793 write_seqlock(&state->seqlock);
794 nfs_set_open_stateid_locked(state, stateid, fmode);
795 write_sequnlock(&state->seqlock);
798 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
801 * Protect the call to nfs4_state_set_mode_locked and
802 * serialise the stateid update
804 write_seqlock(&state->seqlock);
805 if (deleg_stateid != NULL) {
806 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
807 set_bit(NFS_DELEGATED_STATE, &state->flags);
809 if (open_stateid != NULL)
810 nfs_set_open_stateid_locked(state, open_stateid, fmode);
811 write_sequnlock(&state->seqlock);
812 spin_lock(&state->owner->so_lock);
813 update_open_stateflags(state, fmode);
814 spin_unlock(&state->owner->so_lock);
817 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
819 struct nfs_inode *nfsi = NFS_I(state->inode);
820 struct nfs_delegation *deleg_cur;
823 fmode &= (FMODE_READ|FMODE_WRITE);
826 deleg_cur = rcu_dereference(nfsi->delegation);
827 if (deleg_cur == NULL)
830 spin_lock(&deleg_cur->lock);
831 if (nfsi->delegation != deleg_cur ||
832 (deleg_cur->type & fmode) != fmode)
833 goto no_delegation_unlock;
835 if (delegation == NULL)
836 delegation = &deleg_cur->stateid;
837 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
838 goto no_delegation_unlock;
840 nfs_mark_delegation_referenced(deleg_cur);
841 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
843 no_delegation_unlock:
844 spin_unlock(&deleg_cur->lock);
848 if (!ret && open_stateid != NULL) {
849 __update_open_stateid(state, open_stateid, NULL, fmode);
857 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
859 struct nfs_delegation *delegation;
862 delegation = rcu_dereference(NFS_I(inode)->delegation);
863 if (delegation == NULL || (delegation->type & fmode) == fmode) {
868 nfs_inode_return_delegation(inode);
871 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
873 struct nfs4_state *state = opendata->state;
874 struct nfs_inode *nfsi = NFS_I(state->inode);
875 struct nfs_delegation *delegation;
876 int open_mode = opendata->o_arg.open_flags & O_EXCL;
877 fmode_t fmode = opendata->o_arg.fmode;
878 nfs4_stateid stateid;
882 if (can_open_cached(state, fmode, open_mode)) {
883 spin_lock(&state->owner->so_lock);
884 if (can_open_cached(state, fmode, open_mode)) {
885 update_open_stateflags(state, fmode);
886 spin_unlock(&state->owner->so_lock);
887 goto out_return_state;
889 spin_unlock(&state->owner->so_lock);
892 delegation = rcu_dereference(nfsi->delegation);
893 if (delegation == NULL ||
894 !can_open_delegated(delegation, fmode)) {
898 /* Save the delegation */
899 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
901 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
906 /* Try to update the stateid using the delegation */
907 if (update_open_stateid(state, NULL, &stateid, fmode))
908 goto out_return_state;
913 atomic_inc(&state->count);
917 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
920 struct nfs4_state *state = NULL;
921 struct nfs_delegation *delegation;
924 if (!data->rpc_done) {
925 state = nfs4_try_open_cached(data);
930 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
932 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
933 ret = PTR_ERR(inode);
937 state = nfs4_get_open_state(inode, data->owner);
940 if (data->o_res.delegation_type != 0) {
941 int delegation_flags = 0;
944 delegation = rcu_dereference(NFS_I(inode)->delegation);
946 delegation_flags = delegation->flags;
948 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
949 nfs_inode_set_delegation(state->inode,
950 data->owner->so_cred,
953 nfs_inode_reclaim_delegation(state->inode,
954 data->owner->so_cred,
958 update_open_stateid(state, &data->o_res.stateid, NULL,
969 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
971 struct nfs_inode *nfsi = NFS_I(state->inode);
972 struct nfs_open_context *ctx;
974 spin_lock(&state->inode->i_lock);
975 list_for_each_entry(ctx, &nfsi->open_files, list) {
976 if (ctx->state != state)
978 get_nfs_open_context(ctx);
979 spin_unlock(&state->inode->i_lock);
982 spin_unlock(&state->inode->i_lock);
983 return ERR_PTR(-ENOENT);
986 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
988 struct nfs4_opendata *opendata;
990 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
991 if (opendata == NULL)
992 return ERR_PTR(-ENOMEM);
993 opendata->state = state;
994 atomic_inc(&state->count);
998 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1000 struct nfs4_state *newstate;
1003 opendata->o_arg.open_flags = 0;
1004 opendata->o_arg.fmode = fmode;
1005 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1006 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1007 nfs4_init_opendata_res(opendata);
1008 ret = _nfs4_proc_open(opendata);
1011 newstate = nfs4_opendata_to_nfs4_state(opendata);
1012 if (IS_ERR(newstate))
1013 return PTR_ERR(newstate);
1014 nfs4_close_state(&opendata->path, newstate, fmode);
1019 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1021 struct nfs4_state *newstate;
1024 /* memory barrier prior to reading state->n_* */
1025 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1027 if (state->n_rdwr != 0) {
1028 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1031 if (newstate != state)
1034 if (state->n_wronly != 0) {
1035 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1038 if (newstate != state)
1041 if (state->n_rdonly != 0) {
1042 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1045 if (newstate != state)
1049 * We may have performed cached opens for all three recoveries.
1050 * Check if we need to update the current stateid.
1052 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1053 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1054 write_seqlock(&state->seqlock);
1055 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1056 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1057 write_sequnlock(&state->seqlock);
1064 * reclaim state on the server after a reboot.
1066 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1068 struct nfs_delegation *delegation;
1069 struct nfs4_opendata *opendata;
1070 fmode_t delegation_type = 0;
1073 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1074 if (IS_ERR(opendata))
1075 return PTR_ERR(opendata);
1076 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1077 opendata->o_arg.fh = NFS_FH(state->inode);
1079 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1080 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1081 delegation_type = delegation->type;
1083 opendata->o_arg.u.delegation_type = delegation_type;
1084 status = nfs4_open_recover(opendata, state);
1085 nfs4_opendata_put(opendata);
1089 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1091 struct nfs_server *server = NFS_SERVER(state->inode);
1092 struct nfs4_exception exception = { };
1095 err = _nfs4_do_open_reclaim(ctx, state);
1096 if (err != -NFS4ERR_DELAY)
1098 nfs4_handle_exception(server, err, &exception);
1099 } while (exception.retry);
1103 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1105 struct nfs_open_context *ctx;
1108 ctx = nfs4_state_find_open_context(state);
1110 return PTR_ERR(ctx);
1111 ret = nfs4_do_open_reclaim(ctx, state);
1112 put_nfs_open_context(ctx);
1116 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1118 struct nfs4_opendata *opendata;
1121 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1122 if (IS_ERR(opendata))
1123 return PTR_ERR(opendata);
1124 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1125 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1126 sizeof(opendata->o_arg.u.delegation.data));
1127 ret = nfs4_open_recover(opendata, state);
1128 nfs4_opendata_put(opendata);
1132 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1134 struct nfs4_exception exception = { };
1135 struct nfs_server *server = NFS_SERVER(state->inode);
1138 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1142 case -NFS4ERR_STALE_CLIENTID:
1143 case -NFS4ERR_STALE_STATEID:
1144 case -NFS4ERR_EXPIRED:
1145 /* Don't recall a delegation if it was lost */
1146 nfs4_schedule_state_recovery(server->nfs_client);
1149 err = nfs4_handle_exception(server, err, &exception);
1150 } while (exception.retry);
1154 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1156 struct nfs4_opendata *data = calldata;
1158 data->rpc_status = task->tk_status;
1159 if (RPC_ASSASSINATED(task))
1161 if (data->rpc_status == 0) {
1162 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1163 sizeof(data->o_res.stateid.data));
1164 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1165 renew_lease(data->o_res.server, data->timestamp);
1170 static void nfs4_open_confirm_release(void *calldata)
1172 struct nfs4_opendata *data = calldata;
1173 struct nfs4_state *state = NULL;
1175 /* If this request hasn't been cancelled, do nothing */
1176 if (data->cancelled == 0)
1178 /* In case of error, no cleanup! */
1179 if (!data->rpc_done)
1181 state = nfs4_opendata_to_nfs4_state(data);
1183 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1185 nfs4_opendata_put(data);
1188 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1189 .rpc_call_done = nfs4_open_confirm_done,
1190 .rpc_release = nfs4_open_confirm_release,
1194 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1196 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1198 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1199 struct rpc_task *task;
1200 struct rpc_message msg = {
1201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1202 .rpc_argp = &data->c_arg,
1203 .rpc_resp = &data->c_res,
1204 .rpc_cred = data->owner->so_cred,
1206 struct rpc_task_setup task_setup_data = {
1207 .rpc_client = server->client,
1208 .rpc_message = &msg,
1209 .callback_ops = &nfs4_open_confirm_ops,
1210 .callback_data = data,
1211 .workqueue = nfsiod_workqueue,
1212 .flags = RPC_TASK_ASYNC,
1216 kref_get(&data->kref);
1218 data->rpc_status = 0;
1219 data->timestamp = jiffies;
1220 task = rpc_run_task(&task_setup_data);
1222 return PTR_ERR(task);
1223 status = nfs4_wait_for_completion_rpc_task(task);
1225 data->cancelled = 1;
1228 status = data->rpc_status;
1233 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1235 struct nfs4_opendata *data = calldata;
1236 struct nfs4_state_owner *sp = data->owner;
1238 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1241 * Check if we still need to send an OPEN call, or if we can use
1242 * a delegation instead.
1244 if (data->state != NULL) {
1245 struct nfs_delegation *delegation;
1247 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1250 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1251 if (delegation != NULL &&
1252 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1258 /* Update sequence id. */
1259 data->o_arg.id = sp->so_owner_id.id;
1260 data->o_arg.clientid = sp->so_client->cl_clientid;
1261 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1262 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1263 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1265 data->timestamp = jiffies;
1266 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1267 &data->o_arg.seq_args,
1268 &data->o_res.seq_res, 1, task))
1270 rpc_call_start(task);
1273 task->tk_action = NULL;
1277 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1279 struct nfs4_opendata *data = calldata;
1281 data->rpc_status = task->tk_status;
1283 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1286 if (RPC_ASSASSINATED(task))
1288 if (task->tk_status == 0) {
1289 switch (data->o_res.f_attr->mode & S_IFMT) {
1293 data->rpc_status = -ELOOP;
1296 data->rpc_status = -EISDIR;
1299 data->rpc_status = -ENOTDIR;
1301 renew_lease(data->o_res.server, data->timestamp);
1302 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1303 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1308 static void nfs4_open_release(void *calldata)
1310 struct nfs4_opendata *data = calldata;
1311 struct nfs4_state *state = NULL;
1313 /* If this request hasn't been cancelled, do nothing */
1314 if (data->cancelled == 0)
1316 /* In case of error, no cleanup! */
1317 if (data->rpc_status != 0 || !data->rpc_done)
1319 /* In case we need an open_confirm, no cleanup! */
1320 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1322 state = nfs4_opendata_to_nfs4_state(data);
1324 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1326 nfs4_opendata_put(data);
1329 static const struct rpc_call_ops nfs4_open_ops = {
1330 .rpc_call_prepare = nfs4_open_prepare,
1331 .rpc_call_done = nfs4_open_done,
1332 .rpc_release = nfs4_open_release,
1336 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1338 static int _nfs4_proc_open(struct nfs4_opendata *data)
1340 struct inode *dir = data->dir->d_inode;
1341 struct nfs_server *server = NFS_SERVER(dir);
1342 struct nfs_openargs *o_arg = &data->o_arg;
1343 struct nfs_openres *o_res = &data->o_res;
1344 struct rpc_task *task;
1345 struct rpc_message msg = {
1346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1349 .rpc_cred = data->owner->so_cred,
1351 struct rpc_task_setup task_setup_data = {
1352 .rpc_client = server->client,
1353 .rpc_message = &msg,
1354 .callback_ops = &nfs4_open_ops,
1355 .callback_data = data,
1356 .workqueue = nfsiod_workqueue,
1357 .flags = RPC_TASK_ASYNC,
1361 kref_get(&data->kref);
1363 data->rpc_status = 0;
1364 data->cancelled = 0;
1365 task = rpc_run_task(&task_setup_data);
1367 return PTR_ERR(task);
1368 status = nfs4_wait_for_completion_rpc_task(task);
1370 data->cancelled = 1;
1373 status = data->rpc_status;
1375 if (status != 0 || !data->rpc_done)
1378 if (o_res->fh.size == 0)
1379 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1381 if (o_arg->open_flags & O_CREAT) {
1382 update_changeattr(dir, &o_res->cinfo);
1383 nfs_post_op_update_inode(dir, o_res->dir_attr);
1385 nfs_refresh_inode(dir, o_res->dir_attr);
1386 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1387 status = _nfs4_proc_open_confirm(data);
1391 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1392 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1396 static int nfs4_recover_expired_lease(struct nfs_server *server)
1398 struct nfs_client *clp = server->nfs_client;
1402 ret = nfs4_wait_clnt_recover(clp);
1405 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1406 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1408 nfs4_schedule_state_recovery(clp);
1415 * reclaim state on the server after a network partition.
1416 * Assumes caller holds the appropriate lock
1418 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1420 struct nfs4_opendata *opendata;
1423 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1424 if (IS_ERR(opendata))
1425 return PTR_ERR(opendata);
1426 ret = nfs4_open_recover(opendata, state);
1428 d_drop(ctx->path.dentry);
1429 nfs4_opendata_put(opendata);
1433 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1435 struct nfs_server *server = NFS_SERVER(state->inode);
1436 struct nfs4_exception exception = { };
1440 err = _nfs4_open_expired(ctx, state);
1441 if (err != -NFS4ERR_DELAY)
1443 nfs4_handle_exception(server, err, &exception);
1444 } while (exception.retry);
1448 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1450 struct nfs_open_context *ctx;
1453 ctx = nfs4_state_find_open_context(state);
1455 return PTR_ERR(ctx);
1456 ret = nfs4_do_open_expired(ctx, state);
1457 put_nfs_open_context(ctx);
1462 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1463 * fields corresponding to attributes that were used to store the verifier.
1464 * Make sure we clobber those fields in the later setattr call
1466 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1468 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1469 !(sattr->ia_valid & ATTR_ATIME_SET))
1470 sattr->ia_valid |= ATTR_ATIME;
1472 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1473 !(sattr->ia_valid & ATTR_MTIME_SET))
1474 sattr->ia_valid |= ATTR_MTIME;
1478 * Returns a referenced nfs4_state
1480 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)
1482 struct nfs4_state_owner *sp;
1483 struct nfs4_state *state = NULL;
1484 struct nfs_server *server = NFS_SERVER(dir);
1485 struct nfs4_opendata *opendata;
1488 /* Protect against reboot recovery conflicts */
1490 if (!(sp = nfs4_get_state_owner(server, cred))) {
1491 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1494 status = nfs4_recover_expired_lease(server);
1496 goto err_put_state_owner;
1497 if (path->dentry->d_inode != NULL)
1498 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1500 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1501 if (opendata == NULL)
1502 goto err_put_state_owner;
1504 if (path->dentry->d_inode != NULL)
1505 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1507 status = _nfs4_proc_open(opendata);
1509 goto err_opendata_put;
1511 if (opendata->o_arg.open_flags & O_EXCL)
1512 nfs4_exclusive_attrset(opendata, sattr);
1514 state = nfs4_opendata_to_nfs4_state(opendata);
1515 status = PTR_ERR(state);
1517 goto err_opendata_put;
1518 nfs4_opendata_put(opendata);
1519 nfs4_put_state_owner(sp);
1523 nfs4_opendata_put(opendata);
1524 err_put_state_owner:
1525 nfs4_put_state_owner(sp);
1532 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)
1534 struct nfs4_exception exception = { };
1535 struct nfs4_state *res;
1539 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1542 /* NOTE: BAD_SEQID means the server and client disagree about the
1543 * book-keeping w.r.t. state-changing operations
1544 * (OPEN/CLOSE/LOCK/LOCKU...)
1545 * It is actually a sign of a bug on the client or on the server.
1547 * If we receive a BAD_SEQID error in the particular case of
1548 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1549 * have unhashed the old state_owner for us, and that we can
1550 * therefore safely retry using a new one. We should still warn
1551 * the user though...
1553 if (status == -NFS4ERR_BAD_SEQID) {
1554 printk(KERN_WARNING "NFS: v4 server %s "
1555 " returned a bad sequence-id error!\n",
1556 NFS_SERVER(dir)->nfs_client->cl_hostname);
1557 exception.retry = 1;
1561 * BAD_STATEID on OPEN means that the server cancelled our
1562 * state before it received the OPEN_CONFIRM.
1563 * Recover by retrying the request as per the discussion
1564 * on Page 181 of RFC3530.
1566 if (status == -NFS4ERR_BAD_STATEID) {
1567 exception.retry = 1;
1570 if (status == -EAGAIN) {
1571 /* We must have found a delegation */
1572 exception.retry = 1;
1575 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1576 status, &exception));
1577 } while (exception.retry);
1581 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1582 struct nfs_fattr *fattr, struct iattr *sattr,
1583 struct nfs4_state *state)
1585 struct nfs_server *server = NFS_SERVER(inode);
1586 struct nfs_setattrargs arg = {
1587 .fh = NFS_FH(inode),
1590 .bitmask = server->attr_bitmask,
1592 struct nfs_setattrres res = {
1596 struct rpc_message msg = {
1597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1602 unsigned long timestamp = jiffies;
1605 nfs_fattr_init(fattr);
1607 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1608 /* Use that stateid */
1609 } else if (state != NULL) {
1610 nfs4_copy_stateid(&arg.stateid, state, current->files);
1612 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1614 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1615 if (status == 0 && state != NULL)
1616 renew_lease(server, timestamp);
1620 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1621 struct nfs_fattr *fattr, struct iattr *sattr,
1622 struct nfs4_state *state)
1624 struct nfs_server *server = NFS_SERVER(inode);
1625 struct nfs4_exception exception = { };
1628 err = nfs4_handle_exception(server,
1629 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1631 } while (exception.retry);
1635 struct nfs4_closedata {
1637 struct inode *inode;
1638 struct nfs4_state *state;
1639 struct nfs_closeargs arg;
1640 struct nfs_closeres res;
1641 struct nfs_fattr fattr;
1642 unsigned long timestamp;
1645 static void nfs4_free_closedata(void *data)
1647 struct nfs4_closedata *calldata = data;
1648 struct nfs4_state_owner *sp = calldata->state->owner;
1650 nfs4_put_open_state(calldata->state);
1651 nfs_free_seqid(calldata->arg.seqid);
1652 nfs4_put_state_owner(sp);
1653 path_put(&calldata->path);
1657 static void nfs4_close_done(struct rpc_task *task, void *data)
1659 struct nfs4_closedata *calldata = data;
1660 struct nfs4_state *state = calldata->state;
1661 struct nfs_server *server = NFS_SERVER(calldata->inode);
1663 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1664 if (RPC_ASSASSINATED(task))
1666 /* hmm. we are done with the inode, and in the process of freeing
1667 * the state_owner. we keep this around to process errors
1669 switch (task->tk_status) {
1671 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1672 renew_lease(server, calldata->timestamp);
1674 case -NFS4ERR_STALE_STATEID:
1675 case -NFS4ERR_OLD_STATEID:
1676 case -NFS4ERR_BAD_STATEID:
1677 case -NFS4ERR_EXPIRED:
1678 if (calldata->arg.fmode == 0)
1681 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1682 nfs4_restart_rpc(task, server->nfs_client);
1686 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
1687 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1690 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1692 struct nfs4_closedata *calldata = data;
1693 struct nfs4_state *state = calldata->state;
1694 int clear_rd, clear_wr, clear_rdwr;
1696 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1699 clear_rd = clear_wr = clear_rdwr = 0;
1700 spin_lock(&state->owner->so_lock);
1701 /* Calculate the change in open mode */
1702 if (state->n_rdwr == 0) {
1703 if (state->n_rdonly == 0) {
1704 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1705 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1707 if (state->n_wronly == 0) {
1708 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1709 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1712 spin_unlock(&state->owner->so_lock);
1713 if (!clear_rd && !clear_wr && !clear_rdwr) {
1714 /* Note: exit _without_ calling nfs4_close_done */
1715 task->tk_action = NULL;
1718 nfs_fattr_init(calldata->res.fattr);
1719 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1720 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1721 calldata->arg.fmode = FMODE_READ;
1722 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1723 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1724 calldata->arg.fmode = FMODE_WRITE;
1726 calldata->timestamp = jiffies;
1727 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1728 &calldata->arg.seq_args, &calldata->res.seq_res,
1731 rpc_call_start(task);
1734 static const struct rpc_call_ops nfs4_close_ops = {
1735 .rpc_call_prepare = nfs4_close_prepare,
1736 .rpc_call_done = nfs4_close_done,
1737 .rpc_release = nfs4_free_closedata,
1741 * It is possible for data to be read/written from a mem-mapped file
1742 * after the sys_close call (which hits the vfs layer as a flush).
1743 * This means that we can't safely call nfsv4 close on a file until
1744 * the inode is cleared. This in turn means that we are not good
1745 * NFSv4 citizens - we do not indicate to the server to update the file's
1746 * share state even when we are done with one of the three share
1747 * stateid's in the inode.
1749 * NOTE: Caller must be holding the sp->so_owner semaphore!
1751 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1753 struct nfs_server *server = NFS_SERVER(state->inode);
1754 struct nfs4_closedata *calldata;
1755 struct nfs4_state_owner *sp = state->owner;
1756 struct rpc_task *task;
1757 struct rpc_message msg = {
1758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1759 .rpc_cred = state->owner->so_cred,
1761 struct rpc_task_setup task_setup_data = {
1762 .rpc_client = server->client,
1763 .rpc_message = &msg,
1764 .callback_ops = &nfs4_close_ops,
1765 .workqueue = nfsiod_workqueue,
1766 .flags = RPC_TASK_ASYNC,
1768 int status = -ENOMEM;
1770 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1771 if (calldata == NULL)
1773 calldata->inode = state->inode;
1774 calldata->state = state;
1775 calldata->arg.fh = NFS_FH(state->inode);
1776 calldata->arg.stateid = &state->open_stateid;
1777 /* Serialization for the sequence id */
1778 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1779 if (calldata->arg.seqid == NULL)
1780 goto out_free_calldata;
1781 calldata->arg.fmode = 0;
1782 calldata->arg.bitmask = server->cache_consistency_bitmask;
1783 calldata->res.fattr = &calldata->fattr;
1784 calldata->res.seqid = calldata->arg.seqid;
1785 calldata->res.server = server;
1786 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1787 calldata->path.mnt = mntget(path->mnt);
1788 calldata->path.dentry = dget(path->dentry);
1790 msg.rpc_argp = &calldata->arg,
1791 msg.rpc_resp = &calldata->res,
1792 task_setup_data.callback_data = calldata;
1793 task = rpc_run_task(&task_setup_data);
1795 return PTR_ERR(task);
1798 status = rpc_wait_for_completion_task(task);
1804 nfs4_put_open_state(state);
1805 nfs4_put_state_owner(sp);
1809 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1814 /* If the open_intent is for execute, we have an extra check to make */
1815 if (fmode & FMODE_EXEC) {
1816 ret = nfs_may_open(state->inode,
1817 state->owner->so_cred,
1818 nd->intent.open.flags);
1822 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1823 if (!IS_ERR(filp)) {
1824 struct nfs_open_context *ctx;
1825 ctx = nfs_file_open_context(filp);
1829 ret = PTR_ERR(filp);
1831 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1836 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1838 struct path path = {
1839 .mnt = nd->path.mnt,
1842 struct dentry *parent;
1844 struct rpc_cred *cred;
1845 struct nfs4_state *state;
1847 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1849 if (nd->flags & LOOKUP_CREATE) {
1850 attr.ia_mode = nd->intent.open.create_mode;
1851 attr.ia_valid = ATTR_MODE;
1852 if (!IS_POSIXACL(dir))
1853 attr.ia_mode &= ~current_umask();
1856 BUG_ON(nd->intent.open.flags & O_CREAT);
1859 cred = rpc_lookup_cred();
1861 return (struct dentry *)cred;
1862 parent = dentry->d_parent;
1863 /* Protect against concurrent sillydeletes */
1864 nfs_block_sillyrename(parent);
1865 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1867 if (IS_ERR(state)) {
1868 if (PTR_ERR(state) == -ENOENT) {
1869 d_add(dentry, NULL);
1870 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1872 nfs_unblock_sillyrename(parent);
1873 return (struct dentry *)state;
1875 res = d_add_unique(dentry, igrab(state->inode));
1878 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1879 nfs_unblock_sillyrename(parent);
1880 nfs4_intent_set_file(nd, &path, state, fmode);
1885 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1887 struct path path = {
1888 .mnt = nd->path.mnt,
1891 struct rpc_cred *cred;
1892 struct nfs4_state *state;
1893 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1895 cred = rpc_lookup_cred();
1897 return PTR_ERR(cred);
1898 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1900 if (IS_ERR(state)) {
1901 switch (PTR_ERR(state)) {
1907 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1913 if (state->inode == dentry->d_inode) {
1914 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1915 nfs4_intent_set_file(nd, &path, state, fmode);
1918 nfs4_close_sync(&path, state, fmode);
1924 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1926 if (ctx->state == NULL)
1929 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1931 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1934 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1936 struct nfs4_server_caps_arg args = {
1939 struct nfs4_server_caps_res res = {};
1940 struct rpc_message msg = {
1941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1947 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1949 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1950 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1951 server->caps |= NFS_CAP_ACLS;
1952 if (res.has_links != 0)
1953 server->caps |= NFS_CAP_HARDLINKS;
1954 if (res.has_symlinks != 0)
1955 server->caps |= NFS_CAP_SYMLINKS;
1956 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1957 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1958 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1959 server->acl_bitmask = res.acl_bitmask;
1965 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1967 struct nfs4_exception exception = { };
1970 err = nfs4_handle_exception(server,
1971 _nfs4_server_capabilities(server, fhandle),
1973 } while (exception.retry);
1977 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1978 struct nfs_fsinfo *info)
1980 struct nfs4_lookup_root_arg args = {
1981 .bitmask = nfs4_fattr_bitmap,
1983 struct nfs4_lookup_res res = {
1985 .fattr = info->fattr,
1988 struct rpc_message msg = {
1989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1993 nfs_fattr_init(info->fattr);
1994 return nfs4_call_sync(server, &msg, &args, &res, 0);
1997 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1998 struct nfs_fsinfo *info)
2000 struct nfs4_exception exception = { };
2003 err = nfs4_handle_exception(server,
2004 _nfs4_lookup_root(server, fhandle, info),
2006 } while (exception.retry);
2011 * get the file handle for the "/" directory on the server
2013 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2014 struct nfs_fsinfo *info)
2018 status = nfs4_lookup_root(server, fhandle, info);
2020 status = nfs4_server_capabilities(server, fhandle);
2022 status = nfs4_do_fsinfo(server, fhandle, info);
2023 return nfs4_map_errors(status);
2027 * Get locations and (maybe) other attributes of a referral.
2028 * Note that we'll actually follow the referral later when
2029 * we detect fsid mismatch in inode revalidation
2031 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2033 int status = -ENOMEM;
2034 struct page *page = NULL;
2035 struct nfs4_fs_locations *locations = NULL;
2037 page = alloc_page(GFP_KERNEL);
2040 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2041 if (locations == NULL)
2044 status = nfs4_proc_fs_locations(dir, name, locations, page);
2047 /* Make sure server returned a different fsid for the referral */
2048 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2049 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2054 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2055 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2057 fattr->mode = S_IFDIR;
2058 memset(fhandle, 0, sizeof(struct nfs_fh));
2067 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2069 struct nfs4_getattr_arg args = {
2071 .bitmask = server->attr_bitmask,
2073 struct nfs4_getattr_res res = {
2077 struct rpc_message msg = {
2078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2083 nfs_fattr_init(fattr);
2084 return nfs4_call_sync(server, &msg, &args, &res, 0);
2087 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2089 struct nfs4_exception exception = { };
2092 err = nfs4_handle_exception(server,
2093 _nfs4_proc_getattr(server, fhandle, fattr),
2095 } while (exception.retry);
2100 * The file is not closed if it is opened due to the a request to change
2101 * the size of the file. The open call will not be needed once the
2102 * VFS layer lookup-intents are implemented.
2104 * Close is called when the inode is destroyed.
2105 * If we haven't opened the file for O_WRONLY, we
2106 * need to in the size_change case to obtain a stateid.
2109 * Because OPEN is always done by name in nfsv4, it is
2110 * possible that we opened a different file by the same
2111 * name. We can recognize this race condition, but we
2112 * can't do anything about it besides returning an error.
2114 * This will be fixed with VFS changes (lookup-intent).
2117 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2118 struct iattr *sattr)
2120 struct inode *inode = dentry->d_inode;
2121 struct rpc_cred *cred = NULL;
2122 struct nfs4_state *state = NULL;
2125 nfs_fattr_init(fattr);
2127 /* Search for an existing open(O_WRITE) file */
2128 if (sattr->ia_valid & ATTR_FILE) {
2129 struct nfs_open_context *ctx;
2131 ctx = nfs_file_open_context(sattr->ia_file);
2138 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2140 nfs_setattr_update_inode(inode, sattr);
2144 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2145 const struct qstr *name, struct nfs_fh *fhandle,
2146 struct nfs_fattr *fattr)
2149 struct nfs4_lookup_arg args = {
2150 .bitmask = server->attr_bitmask,
2154 struct nfs4_lookup_res res = {
2159 struct rpc_message msg = {
2160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2165 nfs_fattr_init(fattr);
2167 dprintk("NFS call lookupfh %s\n", name->name);
2168 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2169 dprintk("NFS reply lookupfh: %d\n", status);
2173 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2174 struct qstr *name, struct nfs_fh *fhandle,
2175 struct nfs_fattr *fattr)
2177 struct nfs4_exception exception = { };
2180 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2182 if (err == -NFS4ERR_MOVED) {
2186 err = nfs4_handle_exception(server, err, &exception);
2187 } while (exception.retry);
2191 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2192 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2196 dprintk("NFS call lookup %s\n", name->name);
2197 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2198 if (status == -NFS4ERR_MOVED)
2199 status = nfs4_get_referral(dir, name, fattr, fhandle);
2200 dprintk("NFS reply lookup: %d\n", status);
2204 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2206 struct nfs4_exception exception = { };
2209 err = nfs4_handle_exception(NFS_SERVER(dir),
2210 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2212 } while (exception.retry);
2216 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2218 struct nfs_server *server = NFS_SERVER(inode);
2219 struct nfs_fattr fattr;
2220 struct nfs4_accessargs args = {
2221 .fh = NFS_FH(inode),
2222 .bitmask = server->attr_bitmask,
2224 struct nfs4_accessres res = {
2228 struct rpc_message msg = {
2229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2232 .rpc_cred = entry->cred,
2234 int mode = entry->mask;
2238 * Determine which access bits we want to ask for...
2240 if (mode & MAY_READ)
2241 args.access |= NFS4_ACCESS_READ;
2242 if (S_ISDIR(inode->i_mode)) {
2243 if (mode & MAY_WRITE)
2244 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2245 if (mode & MAY_EXEC)
2246 args.access |= NFS4_ACCESS_LOOKUP;
2248 if (mode & MAY_WRITE)
2249 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2250 if (mode & MAY_EXEC)
2251 args.access |= NFS4_ACCESS_EXECUTE;
2253 nfs_fattr_init(&fattr);
2254 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2257 if (res.access & NFS4_ACCESS_READ)
2258 entry->mask |= MAY_READ;
2259 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2260 entry->mask |= MAY_WRITE;
2261 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2262 entry->mask |= MAY_EXEC;
2263 nfs_refresh_inode(inode, &fattr);
2268 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2270 struct nfs4_exception exception = { };
2273 err = nfs4_handle_exception(NFS_SERVER(inode),
2274 _nfs4_proc_access(inode, entry),
2276 } while (exception.retry);
2281 * TODO: For the time being, we don't try to get any attributes
2282 * along with any of the zero-copy operations READ, READDIR,
2285 * In the case of the first three, we want to put the GETATTR
2286 * after the read-type operation -- this is because it is hard
2287 * to predict the length of a GETATTR response in v4, and thus
2288 * align the READ data correctly. This means that the GETATTR
2289 * may end up partially falling into the page cache, and we should
2290 * shift it into the 'tail' of the xdr_buf before processing.
2291 * To do this efficiently, we need to know the total length
2292 * of data received, which doesn't seem to be available outside
2295 * In the case of WRITE, we also want to put the GETATTR after
2296 * the operation -- in this case because we want to make sure
2297 * we get the post-operation mtime and size. This means that
2298 * we can't use xdr_encode_pages() as written: we need a variant
2299 * of it which would leave room in the 'tail' iovec.
2301 * Both of these changes to the XDR layer would in fact be quite
2302 * minor, but I decided to leave them for a subsequent patch.
2304 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2305 unsigned int pgbase, unsigned int pglen)
2307 struct nfs4_readlink args = {
2308 .fh = NFS_FH(inode),
2313 struct nfs4_readlink_res res;
2314 struct rpc_message msg = {
2315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2320 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2323 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2324 unsigned int pgbase, unsigned int pglen)
2326 struct nfs4_exception exception = { };
2329 err = nfs4_handle_exception(NFS_SERVER(inode),
2330 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2332 } while (exception.retry);
2338 * We will need to arrange for the VFS layer to provide an atomic open.
2339 * Until then, this create/open method is prone to inefficiency and race
2340 * conditions due to the lookup, create, and open VFS calls from sys_open()
2341 * placed on the wire.
2343 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2344 * The file will be opened again in the subsequent VFS open call
2345 * (nfs4_proc_file_open).
2347 * The open for read will just hang around to be used by any process that
2348 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2352 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2353 int flags, struct nameidata *nd)
2355 struct path path = {
2356 .mnt = nd->path.mnt,
2359 struct nfs4_state *state;
2360 struct rpc_cred *cred;
2361 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2364 cred = rpc_lookup_cred();
2366 status = PTR_ERR(cred);
2369 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2371 if (IS_ERR(state)) {
2372 status = PTR_ERR(state);
2375 d_add(dentry, igrab(state->inode));
2376 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2377 if (flags & O_EXCL) {
2378 struct nfs_fattr fattr;
2379 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2381 nfs_setattr_update_inode(state->inode, sattr);
2382 nfs_post_op_update_inode(state->inode, &fattr);
2384 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2385 status = nfs4_intent_set_file(nd, &path, state, fmode);
2387 nfs4_close_sync(&path, state, fmode);
2394 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2396 struct nfs_server *server = NFS_SERVER(dir);
2397 struct nfs_removeargs args = {
2399 .name.len = name->len,
2400 .name.name = name->name,
2401 .bitmask = server->attr_bitmask,
2403 struct nfs_removeres res = {
2406 struct rpc_message msg = {
2407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2413 nfs_fattr_init(&res.dir_attr);
2414 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2416 update_changeattr(dir, &res.cinfo);
2417 nfs_post_op_update_inode(dir, &res.dir_attr);
2422 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2424 struct nfs4_exception exception = { };
2427 err = nfs4_handle_exception(NFS_SERVER(dir),
2428 _nfs4_proc_remove(dir, name),
2430 } while (exception.retry);
2434 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2436 struct nfs_server *server = NFS_SERVER(dir);
2437 struct nfs_removeargs *args = msg->rpc_argp;
2438 struct nfs_removeres *res = msg->rpc_resp;
2440 args->bitmask = server->cache_consistency_bitmask;
2441 res->server = server;
2442 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2445 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2447 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2449 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2450 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2452 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
2453 update_changeattr(dir, &res->cinfo);
2454 nfs_post_op_update_inode(dir, &res->dir_attr);
2458 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2459 struct inode *new_dir, struct qstr *new_name)
2461 struct nfs_server *server = NFS_SERVER(old_dir);
2462 struct nfs4_rename_arg arg = {
2463 .old_dir = NFS_FH(old_dir),
2464 .new_dir = NFS_FH(new_dir),
2465 .old_name = old_name,
2466 .new_name = new_name,
2467 .bitmask = server->attr_bitmask,
2469 struct nfs_fattr old_fattr, new_fattr;
2470 struct nfs4_rename_res res = {
2472 .old_fattr = &old_fattr,
2473 .new_fattr = &new_fattr,
2475 struct rpc_message msg = {
2476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2482 nfs_fattr_init(res.old_fattr);
2483 nfs_fattr_init(res.new_fattr);
2484 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2487 update_changeattr(old_dir, &res.old_cinfo);
2488 nfs_post_op_update_inode(old_dir, res.old_fattr);
2489 update_changeattr(new_dir, &res.new_cinfo);
2490 nfs_post_op_update_inode(new_dir, res.new_fattr);
2495 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2496 struct inode *new_dir, struct qstr *new_name)
2498 struct nfs4_exception exception = { };
2501 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2502 _nfs4_proc_rename(old_dir, old_name,
2505 } while (exception.retry);
2509 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2511 struct nfs_server *server = NFS_SERVER(inode);
2512 struct nfs4_link_arg arg = {
2513 .fh = NFS_FH(inode),
2514 .dir_fh = NFS_FH(dir),
2516 .bitmask = server->attr_bitmask,
2518 struct nfs_fattr fattr, dir_attr;
2519 struct nfs4_link_res res = {
2522 .dir_attr = &dir_attr,
2524 struct rpc_message msg = {
2525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2531 nfs_fattr_init(res.fattr);
2532 nfs_fattr_init(res.dir_attr);
2533 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2535 update_changeattr(dir, &res.cinfo);
2536 nfs_post_op_update_inode(dir, res.dir_attr);
2537 nfs_post_op_update_inode(inode, res.fattr);
2543 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2545 struct nfs4_exception exception = { };
2548 err = nfs4_handle_exception(NFS_SERVER(inode),
2549 _nfs4_proc_link(inode, dir, name),
2551 } while (exception.retry);
2555 struct nfs4_createdata {
2556 struct rpc_message msg;
2557 struct nfs4_create_arg arg;
2558 struct nfs4_create_res res;
2560 struct nfs_fattr fattr;
2561 struct nfs_fattr dir_fattr;
2564 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2565 struct qstr *name, struct iattr *sattr, u32 ftype)
2567 struct nfs4_createdata *data;
2569 data = kzalloc(sizeof(*data), GFP_KERNEL);
2571 struct nfs_server *server = NFS_SERVER(dir);
2573 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2574 data->msg.rpc_argp = &data->arg;
2575 data->msg.rpc_resp = &data->res;
2576 data->arg.dir_fh = NFS_FH(dir);
2577 data->arg.server = server;
2578 data->arg.name = name;
2579 data->arg.attrs = sattr;
2580 data->arg.ftype = ftype;
2581 data->arg.bitmask = server->attr_bitmask;
2582 data->res.server = server;
2583 data->res.fh = &data->fh;
2584 data->res.fattr = &data->fattr;
2585 data->res.dir_fattr = &data->dir_fattr;
2586 nfs_fattr_init(data->res.fattr);
2587 nfs_fattr_init(data->res.dir_fattr);
2592 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2594 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2595 &data->arg, &data->res, 1);
2597 update_changeattr(dir, &data->res.dir_cinfo);
2598 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2599 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2604 static void nfs4_free_createdata(struct nfs4_createdata *data)
2609 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2610 struct page *page, unsigned int len, struct iattr *sattr)
2612 struct nfs4_createdata *data;
2613 int status = -ENAMETOOLONG;
2615 if (len > NFS4_MAXPATHLEN)
2619 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2623 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2624 data->arg.u.symlink.pages = &page;
2625 data->arg.u.symlink.len = len;
2627 status = nfs4_do_create(dir, dentry, data);
2629 nfs4_free_createdata(data);
2634 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2635 struct page *page, unsigned int len, struct iattr *sattr)
2637 struct nfs4_exception exception = { };
2640 err = nfs4_handle_exception(NFS_SERVER(dir),
2641 _nfs4_proc_symlink(dir, dentry, page,
2644 } while (exception.retry);
2648 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2649 struct iattr *sattr)
2651 struct nfs4_createdata *data;
2652 int status = -ENOMEM;
2654 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2658 status = nfs4_do_create(dir, dentry, data);
2660 nfs4_free_createdata(data);
2665 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2666 struct iattr *sattr)
2668 struct nfs4_exception exception = { };
2671 err = nfs4_handle_exception(NFS_SERVER(dir),
2672 _nfs4_proc_mkdir(dir, dentry, sattr),
2674 } while (exception.retry);
2678 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2679 u64 cookie, struct page *page, unsigned int count, int plus)
2681 struct inode *dir = dentry->d_inode;
2682 struct nfs4_readdir_arg args = {
2687 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
2689 struct nfs4_readdir_res res;
2690 struct rpc_message msg = {
2691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2698 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2699 dentry->d_parent->d_name.name,
2700 dentry->d_name.name,
2701 (unsigned long long)cookie);
2702 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2703 res.pgbase = args.pgbase;
2704 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2706 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2708 nfs_invalidate_atime(dir);
2710 dprintk("%s: returns %d\n", __func__, status);
2714 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2715 u64 cookie, struct page *page, unsigned int count, int plus)
2717 struct nfs4_exception exception = { };
2720 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2721 _nfs4_proc_readdir(dentry, cred, cookie,
2724 } while (exception.retry);
2728 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2729 struct iattr *sattr, dev_t rdev)
2731 struct nfs4_createdata *data;
2732 int mode = sattr->ia_mode;
2733 int status = -ENOMEM;
2735 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2736 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2738 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2743 data->arg.ftype = NF4FIFO;
2744 else if (S_ISBLK(mode)) {
2745 data->arg.ftype = NF4BLK;
2746 data->arg.u.device.specdata1 = MAJOR(rdev);
2747 data->arg.u.device.specdata2 = MINOR(rdev);
2749 else if (S_ISCHR(mode)) {
2750 data->arg.ftype = NF4CHR;
2751 data->arg.u.device.specdata1 = MAJOR(rdev);
2752 data->arg.u.device.specdata2 = MINOR(rdev);
2755 status = nfs4_do_create(dir, dentry, data);
2757 nfs4_free_createdata(data);
2762 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2763 struct iattr *sattr, dev_t rdev)
2765 struct nfs4_exception exception = { };
2768 err = nfs4_handle_exception(NFS_SERVER(dir),
2769 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2771 } while (exception.retry);
2775 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2776 struct nfs_fsstat *fsstat)
2778 struct nfs4_statfs_arg args = {
2780 .bitmask = server->attr_bitmask,
2782 struct nfs4_statfs_res res = {
2785 struct rpc_message msg = {
2786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2791 nfs_fattr_init(fsstat->fattr);
2792 return nfs4_call_sync(server, &msg, &args, &res, 0);
2795 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2797 struct nfs4_exception exception = { };
2800 err = nfs4_handle_exception(server,
2801 _nfs4_proc_statfs(server, fhandle, fsstat),
2803 } while (exception.retry);
2807 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2808 struct nfs_fsinfo *fsinfo)
2810 struct nfs4_fsinfo_arg args = {
2812 .bitmask = server->attr_bitmask,
2814 struct nfs4_fsinfo_res res = {
2817 struct rpc_message msg = {
2818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2823 return nfs4_call_sync(server, &msg, &args, &res, 0);
2826 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2828 struct nfs4_exception exception = { };
2832 err = nfs4_handle_exception(server,
2833 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2835 } while (exception.retry);
2839 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2841 nfs_fattr_init(fsinfo->fattr);
2842 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2845 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2846 struct nfs_pathconf *pathconf)
2848 struct nfs4_pathconf_arg args = {
2850 .bitmask = server->attr_bitmask,
2852 struct nfs4_pathconf_res res = {
2853 .pathconf = pathconf,
2855 struct rpc_message msg = {
2856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2861 /* None of the pathconf attributes are mandatory to implement */
2862 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2863 memset(pathconf, 0, sizeof(*pathconf));
2867 nfs_fattr_init(pathconf->fattr);
2868 return nfs4_call_sync(server, &msg, &args, &res, 0);
2871 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2872 struct nfs_pathconf *pathconf)
2874 struct nfs4_exception exception = { };
2878 err = nfs4_handle_exception(server,
2879 _nfs4_proc_pathconf(server, fhandle, pathconf),
2881 } while (exception.retry);
2885 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2887 struct nfs_server *server = NFS_SERVER(data->inode);
2889 dprintk("--> %s\n", __func__);
2891 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2892 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2894 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2895 nfs4_restart_rpc(task, server->nfs_client);
2899 nfs_invalidate_atime(data->inode);
2900 if (task->tk_status > 0)
2901 renew_lease(server, data->timestamp);
2905 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2907 data->timestamp = jiffies;
2908 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2911 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2913 struct inode *inode = data->inode;
2915 /* slot is freed in nfs_writeback_done */
2916 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2919 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2920 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
2923 if (task->tk_status >= 0) {
2924 renew_lease(NFS_SERVER(inode), data->timestamp);
2925 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2930 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2932 struct nfs_server *server = NFS_SERVER(data->inode);
2934 data->args.bitmask = server->cache_consistency_bitmask;
2935 data->res.server = server;
2936 data->timestamp = jiffies;
2938 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2941 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2943 struct inode *inode = data->inode;
2945 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2947 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
2948 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
2951 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
2952 &data->res.seq_res);
2953 nfs_refresh_inode(inode, data->res.fattr);
2957 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2959 struct nfs_server *server = NFS_SERVER(data->inode);
2961 data->args.bitmask = server->cache_consistency_bitmask;
2962 data->res.server = server;
2963 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2967 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2968 * standalone procedure for queueing an asynchronous RENEW.
2970 static void nfs4_renew_done(struct rpc_task *task, void *data)
2972 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2973 unsigned long timestamp = (unsigned long)data;
2975 if (task->tk_status < 0) {
2976 /* Unless we're shutting down, schedule state recovery! */
2977 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2978 nfs4_schedule_state_recovery(clp);
2981 spin_lock(&clp->cl_lock);
2982 if (time_before(clp->cl_last_renewal,timestamp))
2983 clp->cl_last_renewal = timestamp;
2984 spin_unlock(&clp->cl_lock);
2987 static const struct rpc_call_ops nfs4_renew_ops = {
2988 .rpc_call_done = nfs4_renew_done,
2991 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2993 struct rpc_message msg = {
2994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2999 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3000 &nfs4_renew_ops, (void *)jiffies);
3003 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3005 struct rpc_message msg = {
3006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3010 unsigned long now = jiffies;
3013 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3016 spin_lock(&clp->cl_lock);
3017 if (time_before(clp->cl_last_renewal,now))
3018 clp->cl_last_renewal = now;
3019 spin_unlock(&clp->cl_lock);
3023 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3025 return (server->caps & NFS_CAP_ACLS)
3026 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3027 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3030 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3031 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3034 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3036 static void buf_to_pages(const void *buf, size_t buflen,
3037 struct page **pages, unsigned int *pgbase)
3039 const void *p = buf;
3041 *pgbase = offset_in_page(buf);
3043 while (p < buf + buflen) {
3044 *(pages++) = virt_to_page(p);
3045 p += PAGE_CACHE_SIZE;
3049 struct nfs4_cached_acl {
3055 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3057 struct nfs_inode *nfsi = NFS_I(inode);
3059 spin_lock(&inode->i_lock);
3060 kfree(nfsi->nfs4_acl);
3061 nfsi->nfs4_acl = acl;
3062 spin_unlock(&inode->i_lock);
3065 static void nfs4_zap_acl_attr(struct inode *inode)
3067 nfs4_set_cached_acl(inode, NULL);
3070 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3072 struct nfs_inode *nfsi = NFS_I(inode);
3073 struct nfs4_cached_acl *acl;
3076 spin_lock(&inode->i_lock);
3077 acl = nfsi->nfs4_acl;
3080 if (buf == NULL) /* user is just asking for length */
3082 if (acl->cached == 0)
3084 ret = -ERANGE; /* see getxattr(2) man page */
3085 if (acl->len > buflen)
3087 memcpy(buf, acl->data, acl->len);
3091 spin_unlock(&inode->i_lock);
3095 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3097 struct nfs4_cached_acl *acl;
3099 if (buf && acl_len <= PAGE_SIZE) {
3100 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3104 memcpy(acl->data, buf, acl_len);
3106 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3113 nfs4_set_cached_acl(inode, acl);
3116 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3118 struct page *pages[NFS4ACL_MAXPAGES];
3119 struct nfs_getaclargs args = {
3120 .fh = NFS_FH(inode),
3124 struct nfs_getaclres res = {
3128 struct rpc_message msg = {
3129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3133 struct page *localpage = NULL;
3136 if (buflen < PAGE_SIZE) {
3137 /* As long as we're doing a round trip to the server anyway,
3138 * let's be prepared for a page of acl data. */
3139 localpage = alloc_page(GFP_KERNEL);
3140 resp_buf = page_address(localpage);
3141 if (localpage == NULL)
3143 args.acl_pages[0] = localpage;
3144 args.acl_pgbase = 0;
3145 args.acl_len = PAGE_SIZE;
3148 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3150 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3153 if (res.acl_len > args.acl_len)
3154 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3156 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3159 if (res.acl_len > buflen)
3162 memcpy(buf, resp_buf, res.acl_len);
3167 __free_page(localpage);
3171 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3173 struct nfs4_exception exception = { };
3176 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3179 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3180 } while (exception.retry);
3184 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3186 struct nfs_server *server = NFS_SERVER(inode);
3189 if (!nfs4_server_supports_acls(server))
3191 ret = nfs_revalidate_inode(server, inode);
3194 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3195 nfs_zap_acl_cache(inode);
3196 ret = nfs4_read_cached_acl(inode, buf, buflen);
3199 return nfs4_get_acl_uncached(inode, buf, buflen);
3202 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3204 struct nfs_server *server = NFS_SERVER(inode);
3205 struct page *pages[NFS4ACL_MAXPAGES];
3206 struct nfs_setaclargs arg = {
3207 .fh = NFS_FH(inode),
3211 struct nfs_setaclres res;
3212 struct rpc_message msg = {
3213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3219 if (!nfs4_server_supports_acls(server))
3221 nfs_inode_return_delegation(inode);
3222 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3223 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3224 nfs_access_zap_cache(inode);
3225 nfs_zap_acl_cache(inode);
3229 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3231 struct nfs4_exception exception = { };
3234 err = nfs4_handle_exception(NFS_SERVER(inode),
3235 __nfs4_proc_set_acl(inode, buf, buflen),
3237 } while (exception.retry);
3242 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3244 if (!clp || task->tk_status >= 0)
3246 switch(task->tk_status) {
3247 case -NFS4ERR_ADMIN_REVOKED:
3248 case -NFS4ERR_BAD_STATEID:
3249 case -NFS4ERR_OPENMODE:
3252 nfs4_state_mark_reclaim_nograce(clp, state);
3253 case -NFS4ERR_STALE_CLIENTID:
3254 case -NFS4ERR_STALE_STATEID:
3255 case -NFS4ERR_EXPIRED:
3256 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3257 nfs4_schedule_state_recovery(clp);
3258 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3259 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3260 task->tk_status = 0;
3262 #if defined(CONFIG_NFS_V4_1)
3263 case -NFS4ERR_BADSESSION:
3264 case -NFS4ERR_BADSLOT:
3265 case -NFS4ERR_BAD_HIGH_SLOT:
3266 case -NFS4ERR_DEADSESSION:
3267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3268 case -NFS4ERR_SEQ_FALSE_RETRY:
3269 case -NFS4ERR_SEQ_MISORDERED:
3270 dprintk("%s ERROR %d, Reset session\n", __func__,
3272 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
3273 task->tk_status = 0;
3275 #endif /* CONFIG_NFS_V4_1 */
3276 case -NFS4ERR_DELAY:
3278 nfs_inc_server_stats(server, NFSIOS_DELAY);
3279 case -NFS4ERR_GRACE:
3280 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3281 task->tk_status = 0;
3283 case -NFS4ERR_OLD_STATEID:
3284 task->tk_status = 0;
3287 task->tk_status = nfs4_map_errors(task->tk_status);
3292 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3294 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3297 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3299 nfs4_verifier sc_verifier;
3300 struct nfs4_setclientid setclientid = {
3301 .sc_verifier = &sc_verifier,
3304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3306 .rpc_argp = &setclientid,
3314 p = (__be32*)sc_verifier.data;
3315 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3316 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3319 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3320 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3322 rpc_peeraddr2str(clp->cl_rpcclient,
3324 rpc_peeraddr2str(clp->cl_rpcclient,
3326 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3327 clp->cl_id_uniquifier);
3328 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3329 sizeof(setclientid.sc_netid),
3330 rpc_peeraddr2str(clp->cl_rpcclient,
3331 RPC_DISPLAY_NETID));
3332 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3333 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3334 clp->cl_ipaddr, port >> 8, port & 255);
3336 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3337 if (status != -NFS4ERR_CLID_INUSE)
3342 ssleep(clp->cl_lease_time + 1);
3344 if (++clp->cl_id_uniquifier == 0)
3350 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3352 struct nfs_fsinfo fsinfo;
3353 struct rpc_message msg = {
3354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3356 .rpc_resp = &fsinfo,
3363 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3365 spin_lock(&clp->cl_lock);
3366 clp->cl_lease_time = fsinfo.lease_time * HZ;
3367 clp->cl_last_renewal = now;
3368 spin_unlock(&clp->cl_lock);
3373 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3378 err = _nfs4_proc_setclientid_confirm(clp, cred);
3382 case -NFS4ERR_RESOURCE:
3383 /* The IBM lawyers misread another document! */
3384 case -NFS4ERR_DELAY:
3385 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3391 struct nfs4_delegreturndata {
3392 struct nfs4_delegreturnargs args;
3393 struct nfs4_delegreturnres res;
3395 nfs4_stateid stateid;
3396 unsigned long timestamp;
3397 struct nfs_fattr fattr;
3401 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3403 struct nfs4_delegreturndata *data = calldata;
3405 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3408 data->rpc_status = task->tk_status;
3409 if (data->rpc_status == 0)
3410 renew_lease(data->res.server, data->timestamp);
3413 static void nfs4_delegreturn_release(void *calldata)
3418 #if defined(CONFIG_NFS_V4_1)
3419 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3421 struct nfs4_delegreturndata *d_data;
3423 d_data = (struct nfs4_delegreturndata *)data;
3425 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3426 &d_data->args.seq_args,
3427 &d_data->res.seq_res, 1, task))
3429 rpc_call_start(task);
3431 #endif /* CONFIG_NFS_V4_1 */
3433 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3434 #if defined(CONFIG_NFS_V4_1)
3435 .rpc_call_prepare = nfs4_delegreturn_prepare,
3436 #endif /* CONFIG_NFS_V4_1 */
3437 .rpc_call_done = nfs4_delegreturn_done,
3438 .rpc_release = nfs4_delegreturn_release,
3441 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3443 struct nfs4_delegreturndata *data;
3444 struct nfs_server *server = NFS_SERVER(inode);
3445 struct rpc_task *task;
3446 struct rpc_message msg = {
3447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3450 struct rpc_task_setup task_setup_data = {
3451 .rpc_client = server->client,
3452 .rpc_message = &msg,
3453 .callback_ops = &nfs4_delegreturn_ops,
3454 .flags = RPC_TASK_ASYNC,
3458 data = kzalloc(sizeof(*data), GFP_KERNEL);
3461 data->args.fhandle = &data->fh;
3462 data->args.stateid = &data->stateid;
3463 data->args.bitmask = server->attr_bitmask;
3464 nfs_copy_fh(&data->fh, NFS_FH(inode));
3465 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3466 data->res.fattr = &data->fattr;
3467 data->res.server = server;
3468 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3469 nfs_fattr_init(data->res.fattr);
3470 data->timestamp = jiffies;
3471 data->rpc_status = 0;
3473 task_setup_data.callback_data = data;
3474 msg.rpc_argp = &data->args,
3475 msg.rpc_resp = &data->res,
3476 task = rpc_run_task(&task_setup_data);
3478 return PTR_ERR(task);
3481 status = nfs4_wait_for_completion_rpc_task(task);
3484 status = data->rpc_status;
3487 nfs_refresh_inode(inode, &data->fattr);
3493 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3495 struct nfs_server *server = NFS_SERVER(inode);
3496 struct nfs4_exception exception = { };
3499 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3501 case -NFS4ERR_STALE_STATEID:
3502 case -NFS4ERR_EXPIRED:
3506 err = nfs4_handle_exception(server, err, &exception);
3507 } while (exception.retry);
3511 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3512 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3515 * sleep, with exponential backoff, and retry the LOCK operation.
3517 static unsigned long
3518 nfs4_set_lock_task_retry(unsigned long timeout)
3520 schedule_timeout_killable(timeout);
3522 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3523 return NFS4_LOCK_MAXTIMEOUT;
3527 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3529 struct inode *inode = state->inode;
3530 struct nfs_server *server = NFS_SERVER(inode);
3531 struct nfs_client *clp = server->nfs_client;
3532 struct nfs_lockt_args arg = {
3533 .fh = NFS_FH(inode),
3536 struct nfs_lockt_res res = {
3539 struct rpc_message msg = {
3540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3543 .rpc_cred = state->owner->so_cred,
3545 struct nfs4_lock_state *lsp;
3548 arg.lock_owner.clientid = clp->cl_clientid;
3549 status = nfs4_set_lock_state(state, request);
3552 lsp = request->fl_u.nfs4_fl.owner;
3553 arg.lock_owner.id = lsp->ls_id.id;
3554 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3557 request->fl_type = F_UNLCK;
3559 case -NFS4ERR_DENIED:
3562 request->fl_ops->fl_release_private(request);
3567 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3569 struct nfs4_exception exception = { };
3573 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3574 _nfs4_proc_getlk(state, cmd, request),
3576 } while (exception.retry);
3580 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3583 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3585 res = posix_lock_file_wait(file, fl);
3588 res = flock_lock_file_wait(file, fl);
3596 struct nfs4_unlockdata {
3597 struct nfs_locku_args arg;
3598 struct nfs_locku_res res;
3599 struct nfs4_lock_state *lsp;
3600 struct nfs_open_context *ctx;
3601 struct file_lock fl;
3602 const struct nfs_server *server;
3603 unsigned long timestamp;
3606 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3607 struct nfs_open_context *ctx,
3608 struct nfs4_lock_state *lsp,
3609 struct nfs_seqid *seqid)
3611 struct nfs4_unlockdata *p;
3612 struct inode *inode = lsp->ls_state->inode;
3614 p = kzalloc(sizeof(*p), GFP_KERNEL);
3617 p->arg.fh = NFS_FH(inode);
3619 p->arg.seqid = seqid;
3620 p->res.seqid = seqid;
3621 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3622 p->arg.stateid = &lsp->ls_stateid;
3624 atomic_inc(&lsp->ls_count);
3625 /* Ensure we don't close file until we're done freeing locks! */
3626 p->ctx = get_nfs_open_context(ctx);
3627 memcpy(&p->fl, fl, sizeof(p->fl));
3628 p->server = NFS_SERVER(inode);
3632 static void nfs4_locku_release_calldata(void *data)
3634 struct nfs4_unlockdata *calldata = data;
3635 nfs_free_seqid(calldata->arg.seqid);
3636 nfs4_put_lock_state(calldata->lsp);
3637 put_nfs_open_context(calldata->ctx);
3641 static void nfs4_locku_done(struct rpc_task *task, void *data)
3643 struct nfs4_unlockdata *calldata = data;
3645 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3647 if (RPC_ASSASSINATED(task))
3649 switch (task->tk_status) {
3651 memcpy(calldata->lsp->ls_stateid.data,
3652 calldata->res.stateid.data,
3653 sizeof(calldata->lsp->ls_stateid.data));
3654 renew_lease(calldata->server, calldata->timestamp);
3656 case -NFS4ERR_BAD_STATEID:
3657 case -NFS4ERR_OLD_STATEID:
3658 case -NFS4ERR_STALE_STATEID:
3659 case -NFS4ERR_EXPIRED:
3662 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3663 nfs4_restart_rpc(task,
3664 calldata->server->nfs_client);
3666 nfs4_sequence_free_slot(calldata->server->nfs_client,
3667 &calldata->res.seq_res);
3670 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3672 struct nfs4_unlockdata *calldata = data;
3674 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3676 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3677 /* Note: exit _without_ running nfs4_locku_done */
3678 task->tk_action = NULL;
3681 calldata->timestamp = jiffies;
3682 if (nfs4_setup_sequence(calldata->server->nfs_client,
3683 &calldata->arg.seq_args,
3684 &calldata->res.seq_res, 1, task))
3686 rpc_call_start(task);
3689 static const struct rpc_call_ops nfs4_locku_ops = {
3690 .rpc_call_prepare = nfs4_locku_prepare,
3691 .rpc_call_done = nfs4_locku_done,
3692 .rpc_release = nfs4_locku_release_calldata,
3695 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3696 struct nfs_open_context *ctx,
3697 struct nfs4_lock_state *lsp,
3698 struct nfs_seqid *seqid)
3700 struct nfs4_unlockdata *data;
3701 struct rpc_message msg = {
3702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3703 .rpc_cred = ctx->cred,
3705 struct rpc_task_setup task_setup_data = {
3706 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3707 .rpc_message = &msg,
3708 .callback_ops = &nfs4_locku_ops,
3709 .workqueue = nfsiod_workqueue,
3710 .flags = RPC_TASK_ASYNC,
3713 /* Ensure this is an unlock - when canceling a lock, the
3714 * canceled lock is passed in, and it won't be an unlock.
3716 fl->fl_type = F_UNLCK;
3718 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3720 nfs_free_seqid(seqid);
3721 return ERR_PTR(-ENOMEM);
3724 msg.rpc_argp = &data->arg,
3725 msg.rpc_resp = &data->res,
3726 task_setup_data.callback_data = data;
3727 return rpc_run_task(&task_setup_data);
3730 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3732 struct nfs_inode *nfsi = NFS_I(state->inode);
3733 struct nfs_seqid *seqid;
3734 struct nfs4_lock_state *lsp;
3735 struct rpc_task *task;
3737 unsigned char fl_flags = request->fl_flags;
3739 status = nfs4_set_lock_state(state, request);
3740 /* Unlock _before_ we do the RPC call */
3741 request->fl_flags |= FL_EXISTS;
3742 down_read(&nfsi->rwsem);
3743 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3744 up_read(&nfsi->rwsem);
3747 up_read(&nfsi->rwsem);
3750 /* Is this a delegated lock? */
3751 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3753 lsp = request->fl_u.nfs4_fl.owner;
3754 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3758 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3759 status = PTR_ERR(task);
3762 status = nfs4_wait_for_completion_rpc_task(task);
3765 request->fl_flags = fl_flags;
3769 struct nfs4_lockdata {
3770 struct nfs_lock_args arg;
3771 struct nfs_lock_res res;
3772 struct nfs4_lock_state *lsp;
3773 struct nfs_open_context *ctx;
3774 struct file_lock fl;
3775 unsigned long timestamp;
3778 struct nfs_server *server;
3781 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3782 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3784 struct nfs4_lockdata *p;
3785 struct inode *inode = lsp->ls_state->inode;
3786 struct nfs_server *server = NFS_SERVER(inode);
3788 p = kzalloc(sizeof(*p), GFP_KERNEL);
3792 p->arg.fh = NFS_FH(inode);
3794 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3795 if (p->arg.open_seqid == NULL)
3797 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3798 if (p->arg.lock_seqid == NULL)
3799 goto out_free_seqid;
3800 p->arg.lock_stateid = &lsp->ls_stateid;
3801 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3802 p->arg.lock_owner.id = lsp->ls_id.id;
3803 p->res.lock_seqid = p->arg.lock_seqid;
3804 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3807 atomic_inc(&lsp->ls_count);
3808 p->ctx = get_nfs_open_context(ctx);
3809 memcpy(&p->fl, fl, sizeof(p->fl));
3812 nfs_free_seqid(p->arg.open_seqid);
3818 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3820 struct nfs4_lockdata *data = calldata;
3821 struct nfs4_state *state = data->lsp->ls_state;
3823 dprintk("%s: begin!\n", __func__);
3824 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3826 /* Do we need to do an open_to_lock_owner? */
3827 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3828 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3830 data->arg.open_stateid = &state->stateid;
3831 data->arg.new_lock_owner = 1;
3832 data->res.open_seqid = data->arg.open_seqid;
3834 data->arg.new_lock_owner = 0;
3835 data->timestamp = jiffies;
3836 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3837 &data->res.seq_res, 1, task))
3839 rpc_call_start(task);
3840 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3843 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3845 struct nfs4_lockdata *data = calldata;
3847 dprintk("%s: begin!\n", __func__);
3849 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3852 data->rpc_status = task->tk_status;
3853 if (RPC_ASSASSINATED(task))
3855 if (data->arg.new_lock_owner != 0) {
3856 if (data->rpc_status == 0)
3857 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3861 if (data->rpc_status == 0) {
3862 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3863 sizeof(data->lsp->ls_stateid.data));
3864 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3865 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3868 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3871 static void nfs4_lock_release(void *calldata)
3873 struct nfs4_lockdata *data = calldata;
3875 dprintk("%s: begin!\n", __func__);
3876 nfs_free_seqid(data->arg.open_seqid);
3877 if (data->cancelled != 0) {
3878 struct rpc_task *task;
3879 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3880 data->arg.lock_seqid);
3883 dprintk("%s: cancelling lock!\n", __func__);
3885 nfs_free_seqid(data->arg.lock_seqid);
3886 nfs4_put_lock_state(data->lsp);
3887 put_nfs_open_context(data->ctx);
3889 dprintk("%s: done!\n", __func__);
3892 static const struct rpc_call_ops nfs4_lock_ops = {
3893 .rpc_call_prepare = nfs4_lock_prepare,
3894 .rpc_call_done = nfs4_lock_done,
3895 .rpc_release = nfs4_lock_release,
3898 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3900 struct nfs4_lockdata *data;
3901 struct rpc_task *task;
3902 struct rpc_message msg = {
3903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3904 .rpc_cred = state->owner->so_cred,
3906 struct rpc_task_setup task_setup_data = {
3907 .rpc_client = NFS_CLIENT(state->inode),
3908 .rpc_message = &msg,
3909 .callback_ops = &nfs4_lock_ops,
3910 .workqueue = nfsiod_workqueue,
3911 .flags = RPC_TASK_ASYNC,
3915 dprintk("%s: begin!\n", __func__);
3916 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3917 fl->fl_u.nfs4_fl.owner);
3921 data->arg.block = 1;
3923 data->arg.reclaim = 1;
3924 msg.rpc_argp = &data->arg,
3925 msg.rpc_resp = &data->res,
3926 task_setup_data.callback_data = data;
3927 task = rpc_run_task(&task_setup_data);
3929 return PTR_ERR(task);
3930 ret = nfs4_wait_for_completion_rpc_task(task);
3932 ret = data->rpc_status;
3933 if (ret == -NFS4ERR_DENIED)
3936 data->cancelled = 1;
3938 dprintk("%s: done, ret = %d!\n", __func__, ret);
3942 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3944 struct nfs_server *server = NFS_SERVER(state->inode);
3945 struct nfs4_exception exception = { };
3949 /* Cache the lock if possible... */
3950 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3952 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3953 if (err != -NFS4ERR_DELAY)
3955 nfs4_handle_exception(server, err, &exception);
3956 } while (exception.retry);
3960 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3962 struct nfs_server *server = NFS_SERVER(state->inode);
3963 struct nfs4_exception exception = { };
3966 err = nfs4_set_lock_state(state, request);
3970 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3972 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3973 if (err != -NFS4ERR_DELAY)
3975 nfs4_handle_exception(server, err, &exception);
3976 } while (exception.retry);
3980 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3982 struct nfs_inode *nfsi = NFS_I(state->inode);
3983 unsigned char fl_flags = request->fl_flags;
3986 /* Is this a delegated open? */
3987 status = nfs4_set_lock_state(state, request);
3990 request->fl_flags |= FL_ACCESS;
3991 status = do_vfs_lock(request->fl_file, request);
3994 down_read(&nfsi->rwsem);
3995 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3996 /* Yes: cache locks! */
3997 /* ...but avoid races with delegation recall... */
3998 request->fl_flags = fl_flags & ~FL_SLEEP;
3999 status = do_vfs_lock(request->fl_file, request);
4002 status = _nfs4_do_setlk(state, cmd, request, 0);
4005 /* Note: we always want to sleep here! */
4006 request->fl_flags = fl_flags | FL_SLEEP;
4007 if (do_vfs_lock(request->fl_file, request) < 0)
4008 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4010 up_read(&nfsi->rwsem);
4012 request->fl_flags = fl_flags;
4016 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4018 struct nfs4_exception exception = { };
4022 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4023 _nfs4_proc_setlk(state, cmd, request),
4025 } while (exception.retry);
4030 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4032 struct nfs_open_context *ctx;
4033 struct nfs4_state *state;
4034 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4037 /* verify open state */
4038 ctx = nfs_file_open_context(filp);
4041 if (request->fl_start < 0 || request->fl_end < 0)
4045 return nfs4_proc_getlk(state, F_GETLK, request);
4047 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4050 if (request->fl_type == F_UNLCK)
4051 return nfs4_proc_unlck(state, cmd, request);
4054 status = nfs4_proc_setlk(state, cmd, request);
4055 if ((status != -EAGAIN) || IS_SETLK(cmd))
4057 timeout = nfs4_set_lock_task_retry(timeout);
4058 status = -ERESTARTSYS;
4061 } while(status < 0);
4065 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4067 struct nfs_server *server = NFS_SERVER(state->inode);
4068 struct nfs4_exception exception = { };
4071 err = nfs4_set_lock_state(state, fl);
4075 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4076 if (err != -NFS4ERR_DELAY)
4078 err = nfs4_handle_exception(server, err, &exception);
4079 } while (exception.retry);
4084 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4086 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4087 size_t buflen, int flags)
4089 struct inode *inode = dentry->d_inode;
4091 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4094 return nfs4_proc_set_acl(inode, buf, buflen);
4097 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4098 * and that's what we'll do for e.g. user attributes that haven't been set.
4099 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4100 * attributes in kernel-managed attribute namespaces. */
4101 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4104 struct inode *inode = dentry->d_inode;
4106 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4109 return nfs4_proc_get_acl(inode, buf, buflen);
4112 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4114 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4116 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4118 if (buf && buflen < len)
4121 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4125 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4127 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4128 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4129 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4132 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4133 NFS_ATTR_FATTR_NLINK;
4134 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4138 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4139 struct nfs4_fs_locations *fs_locations, struct page *page)
4141 struct nfs_server *server = NFS_SERVER(dir);
4143 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4144 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4146 struct nfs4_fs_locations_arg args = {
4147 .dir_fh = NFS_FH(dir),
4152 struct nfs4_fs_locations_res res = {
4153 .fs_locations = fs_locations,
4155 struct rpc_message msg = {
4156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4162 dprintk("%s: start\n", __func__);
4163 nfs_fattr_init(&fs_locations->fattr);
4164 fs_locations->server = server;
4165 fs_locations->nlocations = 0;
4166 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4167 nfs_fixup_referral_attributes(&fs_locations->fattr);
4168 dprintk("%s: returned status = %d\n", __func__, status);
4172 #ifdef CONFIG_NFS_V4_1
4174 * nfs4_proc_exchange_id()
4176 * Since the clientid has expired, all compounds using sessions
4177 * associated with the stale clientid will be returning
4178 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4179 * be in some phase of session reset.
4181 static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4183 nfs4_verifier verifier;
4184 struct nfs41_exchange_id_args args = {
4186 .flags = clp->cl_exchange_flags,
4188 struct nfs41_exchange_id_res res = {
4192 struct rpc_message msg = {
4193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4200 dprintk("--> %s\n", __func__);
4201 BUG_ON(clp == NULL);
4202 p = (u32 *)verifier.data;
4203 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4204 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4205 args.verifier = &verifier;
4208 args.id_len = scnprintf(args.id, sizeof(args.id),
4211 rpc_peeraddr2str(clp->cl_rpcclient,
4213 clp->cl_id_uniquifier);
4215 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4217 if (status != NFS4ERR_CLID_INUSE)
4223 if (++clp->cl_id_uniquifier == 0)
4227 dprintk("<-- %s status= %d\n", __func__, status);
4231 struct nfs4_get_lease_time_data {
4232 struct nfs4_get_lease_time_args *args;
4233 struct nfs4_get_lease_time_res *res;
4234 struct nfs_client *clp;
4237 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4241 struct nfs4_get_lease_time_data *data =
4242 (struct nfs4_get_lease_time_data *)calldata;
4244 dprintk("--> %s\n", __func__);
4245 /* just setup sequence, do not trigger session recovery
4246 since we're invoked within one */
4247 ret = nfs41_setup_sequence(data->clp->cl_session,
4248 &data->args->la_seq_args,
4249 &data->res->lr_seq_res, 0, task);
4251 BUG_ON(ret == -EAGAIN);
4252 rpc_call_start(task);
4253 dprintk("<-- %s\n", __func__);
4257 * Called from nfs4_state_manager thread for session setup, so don't recover
4258 * from sequence operation or clientid errors.
4260 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4262 struct nfs4_get_lease_time_data *data =
4263 (struct nfs4_get_lease_time_data *)calldata;
4265 dprintk("--> %s\n", __func__);
4266 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4267 switch (task->tk_status) {
4268 case -NFS4ERR_DELAY:
4269 case -NFS4ERR_GRACE:
4270 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4271 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4272 task->tk_status = 0;
4273 nfs4_restart_rpc(task, data->clp);
4276 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4277 dprintk("<-- %s\n", __func__);
4280 struct rpc_call_ops nfs4_get_lease_time_ops = {
4281 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4282 .rpc_call_done = nfs4_get_lease_time_done,
4285 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4287 struct rpc_task *task;
4288 struct nfs4_get_lease_time_args args;
4289 struct nfs4_get_lease_time_res res = {
4290 .lr_fsinfo = fsinfo,
4292 struct nfs4_get_lease_time_data data = {
4297 struct rpc_message msg = {
4298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4302 struct rpc_task_setup task_setup = {
4303 .rpc_client = clp->cl_rpcclient,
4304 .rpc_message = &msg,
4305 .callback_ops = &nfs4_get_lease_time_ops,
4306 .callback_data = &data
4310 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4311 dprintk("--> %s\n", __func__);
4312 task = rpc_run_task(&task_setup);
4315 status = PTR_ERR(task);
4317 status = task->tk_status;
4320 dprintk("<-- %s return %d\n", __func__, status);
4325 /* Reset a slot table */
4326 static int nfs4_reset_slot_table(struct nfs4_session *session)
4328 struct nfs4_slot_table *tbl = &session->fc_slot_table;
4329 int i, max_slots = session->fc_attrs.max_reqs;
4330 int old_max_slots = session->fc_slot_table.max_slots;
4333 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__,
4334 session->fc_attrs.max_reqs, tbl);
4336 /* Until we have dynamic slot table adjustment, insist
4337 * upon the same slot table size */
4338 if (max_slots != old_max_slots) {
4339 dprintk("%s reset slot table does't match old\n",
4341 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4344 spin_lock(&tbl->slot_tbl_lock);
4345 for (i = 0; i < max_slots; ++i)
4346 tbl->slots[i].seq_nr = 1;
4347 tbl->highest_used_slotid = -1;
4348 spin_unlock(&tbl->slot_tbl_lock);
4349 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4350 tbl, tbl->slots, tbl->max_slots);
4352 dprintk("<-- %s: return %d\n", __func__, ret);
4357 * Initialize slot table
4359 static int nfs4_init_slot_table(struct nfs4_session *session)
4361 struct nfs4_slot_table *tbl = &session->fc_slot_table;
4362 int i, max_slots = session->fc_attrs.max_reqs;
4363 struct nfs4_slot *slot;
4366 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4368 dprintk("--> %s: max_reqs=%u\n", __func__,
4369 session->fc_attrs.max_reqs);
4371 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4374 for (i = 0; i < max_slots; ++i)
4378 spin_lock(&tbl->slot_tbl_lock);
4379 if (tbl->slots != NULL) {
4380 spin_unlock(&tbl->slot_tbl_lock);
4381 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4382 __func__, tbl, tbl->slots);
4386 tbl->max_slots = max_slots;
4388 tbl->highest_used_slotid = -1; /* no slot is currently used */
4389 spin_unlock(&tbl->slot_tbl_lock);
4390 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4391 tbl, tbl->slots, tbl->max_slots);
4393 dprintk("<-- %s: return %d\n", __func__, ret);
4400 /* Destroy the slot table */
4401 static void nfs4_destroy_slot_table(struct nfs4_session *session)
4403 if (session->fc_slot_table.slots == NULL)
4405 kfree(session->fc_slot_table.slots);
4406 session->fc_slot_table.slots = NULL;
4410 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4412 struct nfs4_session *session;
4413 struct nfs4_slot_table *tbl;
4415 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4419 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
4421 * The create session reply races with the server back
4422 * channel probe. Mark the client NFS_CS_SESSION_INITING
4423 * so that the client back channel can find the
4426 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4428 tbl = &session->fc_slot_table;
4429 spin_lock_init(&tbl->slot_tbl_lock);
4430 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4435 void nfs4_destroy_session(struct nfs4_session *session)
4437 nfs4_destroy_slot_table(session);
4442 * Initialize the values to be used by the client in CREATE_SESSION
4443 * If nfs4_init_session set the fore channel request and response sizes,
4446 * Set the back channel max_resp_sz_cached to zero to force the client to
4447 * always set csa_cachethis to FALSE because the current implementation
4448 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4450 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4452 struct nfs4_session *session = args->client->cl_session;
4453 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4454 mxresp_sz = session->fc_attrs.max_resp_sz;
4457 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4459 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4460 /* Fore channel attributes */
4461 args->fc_attrs.headerpadsz = 0;
4462 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4463 args->fc_attrs.max_resp_sz = mxresp_sz;
4464 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4465 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4466 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4468 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4469 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4471 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4472 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4473 args->fc_attrs.max_reqs);
4475 /* Back channel attributes */
4476 args->bc_attrs.headerpadsz = 0;
4477 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4478 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4479 args->bc_attrs.max_resp_sz_cached = 0;
4480 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4481 args->bc_attrs.max_reqs = 1;
4483 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4484 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4486 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4487 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4488 args->bc_attrs.max_reqs);
4491 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4495 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4496 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4500 #define _verify_fore_channel_attr(_name_) \
4501 _verify_channel_attr("fore", #_name_, \
4502 args->fc_attrs._name_, \
4503 session->fc_attrs._name_)
4505 #define _verify_back_channel_attr(_name_) \
4506 _verify_channel_attr("back", #_name_, \
4507 args->bc_attrs._name_, \
4508 session->bc_attrs._name_)
4511 * The server is not allowed to increase the fore channel header pad size,
4512 * maximum response size, or maximum number of operations.
4514 * The back channel attributes are only negotiatied down: We send what the
4515 * (back channel) server insists upon.
4517 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4518 struct nfs4_session *session)
4522 ret |= _verify_fore_channel_attr(headerpadsz);
4523 ret |= _verify_fore_channel_attr(max_resp_sz);
4524 ret |= _verify_fore_channel_attr(max_ops);
4526 ret |= _verify_back_channel_attr(headerpadsz);
4527 ret |= _verify_back_channel_attr(max_rqst_sz);
4528 ret |= _verify_back_channel_attr(max_resp_sz);
4529 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4530 ret |= _verify_back_channel_attr(max_ops);
4531 ret |= _verify_back_channel_attr(max_reqs);
4536 static int _nfs4_proc_create_session(struct nfs_client *clp)
4538 struct nfs4_session *session = clp->cl_session;
4539 struct nfs41_create_session_args args = {
4541 .cb_program = NFS4_CALLBACK,
4543 struct nfs41_create_session_res res = {
4546 struct rpc_message msg = {
4547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4553 nfs4_init_channel_attrs(&args);
4554 args.flags = (SESSION4_PERSIST);
4556 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4559 /* Verify the session's negotiated channel_attrs values */
4560 status = nfs4_verify_channel_attrs(&args, session);
4562 /* Increment the clientid slot sequence id */
4570 * Issues a CREATE_SESSION operation to the server.
4571 * It is the responsibility of the caller to verify the session is
4572 * expired before calling this routine.
4574 int nfs4_proc_create_session(struct nfs_client *clp, int reset)
4578 struct nfs_fsinfo fsinfo;
4579 struct nfs4_session *session = clp->cl_session;
4581 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4583 status = _nfs4_proc_create_session(clp);
4587 /* Init or reset the fore channel */
4589 status = nfs4_reset_slot_table(session);
4591 status = nfs4_init_slot_table(session);
4592 dprintk("fore channel slot table initialization returned %d\n", status);
4596 ptr = (unsigned *)&session->sess_id.data[0];
4597 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4598 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4601 /* Lease time is aleady set */
4604 /* Get the lease time */
4605 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4607 /* Update lease time and schedule renewal */
4608 spin_lock(&clp->cl_lock);
4609 clp->cl_lease_time = fsinfo.lease_time * HZ;
4610 clp->cl_last_renewal = jiffies;
4611 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4612 spin_unlock(&clp->cl_lock);
4614 nfs4_schedule_state_renewal(clp);
4617 dprintk("<-- %s\n", __func__);
4622 * Issue the over-the-wire RPC DESTROY_SESSION.
4623 * The caller must serialize access to this routine.
4625 int nfs4_proc_destroy_session(struct nfs4_session *session)
4628 struct rpc_message msg;
4630 dprintk("--> nfs4_proc_destroy_session\n");
4632 /* session is still being setup */
4633 if (session->clp->cl_cons_state != NFS_CS_READY)
4636 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4637 msg.rpc_argp = session;
4638 msg.rpc_resp = NULL;
4639 msg.rpc_cred = NULL;
4640 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4644 "Got error %d from the server on DESTROY_SESSION. "
4645 "Session has been destroyed regardless...\n", status);
4647 dprintk("<-- nfs4_proc_destroy_session\n");
4652 * Renew the cl_session lease.
4654 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4656 struct nfs4_sequence_args args;
4657 struct nfs4_sequence_res res;
4659 struct rpc_message msg = {
4660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4666 args.sa_cache_this = 0;
4668 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4672 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4674 struct nfs_client *clp = (struct nfs_client *)data;
4676 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4678 if (task->tk_status < 0) {
4679 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4681 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4683 nfs4_restart_rpc(task, clp);
4687 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4688 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4690 put_rpccred(task->tk_msg.rpc_cred);
4691 kfree(task->tk_msg.rpc_argp);
4692 kfree(task->tk_msg.rpc_resp);
4694 dprintk("<-- %s\n", __func__);
4697 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4699 struct nfs_client *clp;
4700 struct nfs4_sequence_args *args;
4701 struct nfs4_sequence_res *res;
4703 clp = (struct nfs_client *)data;
4704 args = task->tk_msg.rpc_argp;
4705 res = task->tk_msg.rpc_resp;
4707 if (nfs4_setup_sequence(clp, args, res, 0, task))
4709 rpc_call_start(task);
4712 static const struct rpc_call_ops nfs41_sequence_ops = {
4713 .rpc_call_done = nfs41_sequence_call_done,
4714 .rpc_call_prepare = nfs41_sequence_prepare,
4717 static int nfs41_proc_async_sequence(struct nfs_client *clp,
4718 struct rpc_cred *cred)
4720 struct nfs4_sequence_args *args;
4721 struct nfs4_sequence_res *res;
4722 struct rpc_message msg = {
4723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4727 args = kzalloc(sizeof(*args), GFP_KERNEL);
4730 res = kzalloc(sizeof(*res), GFP_KERNEL);
4735 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4736 msg.rpc_argp = args;
4739 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4740 &nfs41_sequence_ops, (void *)clp);
4743 #endif /* CONFIG_NFS_V4_1 */
4745 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
4746 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4747 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4748 .recover_open = nfs4_open_reclaim,
4749 .recover_lock = nfs4_lock_reclaim,
4752 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
4753 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4754 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4755 .recover_open = nfs4_open_expired,
4756 .recover_lock = nfs4_lock_expired,
4759 static const struct inode_operations nfs4_file_inode_operations = {
4760 .permission = nfs_permission,
4761 .getattr = nfs_getattr,
4762 .setattr = nfs_setattr,
4763 .getxattr = nfs4_getxattr,
4764 .setxattr = nfs4_setxattr,
4765 .listxattr = nfs4_listxattr,
4768 const struct nfs_rpc_ops nfs_v4_clientops = {
4769 .version = 4, /* protocol version */
4770 .dentry_ops = &nfs4_dentry_operations,
4771 .dir_inode_ops = &nfs4_dir_inode_operations,
4772 .file_inode_ops = &nfs4_file_inode_operations,
4773 .getroot = nfs4_proc_get_root,
4774 .getattr = nfs4_proc_getattr,
4775 .setattr = nfs4_proc_setattr,
4776 .lookupfh = nfs4_proc_lookupfh,
4777 .lookup = nfs4_proc_lookup,
4778 .access = nfs4_proc_access,
4779 .readlink = nfs4_proc_readlink,
4780 .create = nfs4_proc_create,
4781 .remove = nfs4_proc_remove,
4782 .unlink_setup = nfs4_proc_unlink_setup,
4783 .unlink_done = nfs4_proc_unlink_done,
4784 .rename = nfs4_proc_rename,
4785 .link = nfs4_proc_link,
4786 .symlink = nfs4_proc_symlink,
4787 .mkdir = nfs4_proc_mkdir,
4788 .rmdir = nfs4_proc_remove,
4789 .readdir = nfs4_proc_readdir,
4790 .mknod = nfs4_proc_mknod,
4791 .statfs = nfs4_proc_statfs,
4792 .fsinfo = nfs4_proc_fsinfo,
4793 .pathconf = nfs4_proc_pathconf,
4794 .set_capabilities = nfs4_server_capabilities,
4795 .decode_dirent = nfs4_decode_dirent,
4796 .read_setup = nfs4_proc_read_setup,
4797 .read_done = nfs4_read_done,
4798 .write_setup = nfs4_proc_write_setup,
4799 .write_done = nfs4_write_done,
4800 .commit_setup = nfs4_proc_commit_setup,
4801 .commit_done = nfs4_commit_done,
4802 .lock = nfs4_proc_lock,
4803 .clear_acl_cache = nfs4_zap_acl_attr,
4804 .close_context = nfs4_close_context,