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>
53 #include "delegation.h"
57 #define NFSDBG_FACILITY NFSDBG_PROC
59 #define NFS4_POLL_RETRY_MIN (HZ/10)
60 #define NFS4_POLL_RETRY_MAX (15*HZ)
63 static int _nfs4_proc_open(struct nfs4_opendata *data);
64 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
65 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
66 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
67 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
69 /* Prevent leaks of NFSv4 errors into userland */
70 static int nfs4_map_errors(int err)
73 dprintk("%s could not handle NFSv4 error %d\n",
81 * This is our standard bitmap for GETATTR requests.
83 const u32 nfs4_fattr_bitmap[2] = {
88 | FATTR4_WORD0_FILEID,
90 | FATTR4_WORD1_NUMLINKS
92 | FATTR4_WORD1_OWNER_GROUP
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
100 const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
109 const u32 nfs4_pathconf_bitmap[2] = {
111 | FATTR4_WORD0_MAXNAME,
115 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
122 const u32 nfs4_fs_locations_bitmap[2] = {
124 | FATTR4_WORD0_CHANGE
127 | FATTR4_WORD0_FILEID
128 | FATTR4_WORD0_FS_LOCATIONS,
130 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138 | FATTR4_WORD1_MOUNTED_ON_FILEID
141 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
142 struct nfs4_readdir_arg *readdir)
146 BUG_ON(readdir->count < 80);
148 readdir->cookie = cookie;
149 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
154 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
159 * NFSv4 servers do not return entries for '.' and '..'
160 * Therefore, we fake these entries here. We let '.'
161 * have cookie 0 and '..' have cookie 1. Note that
162 * when talking to the server, we always send cookie 0
165 start = p = kmap_atomic(*readdir->pages, KM_USER0);
168 *p++ = xdr_one; /* next */
169 *p++ = xdr_zero; /* cookie, first word */
170 *p++ = xdr_one; /* cookie, second word */
171 *p++ = xdr_one; /* entry len */
172 memcpy(p, ".\0\0\0", 4); /* entry */
174 *p++ = xdr_one; /* bitmap length */
175 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
176 *p++ = htonl(8); /* attribute buffer length */
177 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
180 *p++ = xdr_one; /* next */
181 *p++ = xdr_zero; /* cookie, first word */
182 *p++ = xdr_two; /* cookie, second word */
183 *p++ = xdr_two; /* entry len */
184 memcpy(p, "..\0\0", 4); /* entry */
186 *p++ = xdr_one; /* bitmap length */
187 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
188 *p++ = htonl(8); /* attribute buffer length */
189 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
191 readdir->pgbase = (char *)p - (char *)start;
192 readdir->count -= readdir->pgbase;
193 kunmap_atomic(start, KM_USER0);
196 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
202 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
203 nfs_wait_bit_killable, TASK_KILLABLE);
207 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
214 *timeout = NFS4_POLL_RETRY_MIN;
215 if (*timeout > NFS4_POLL_RETRY_MAX)
216 *timeout = NFS4_POLL_RETRY_MAX;
217 schedule_timeout_killable(*timeout);
218 if (fatal_signal_pending(current))
224 /* This is the error handling routine for processes that are allowed
227 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
229 struct nfs_client *clp = server->nfs_client;
230 struct nfs4_state *state = exception->state;
233 exception->retry = 0;
237 case -NFS4ERR_ADMIN_REVOKED:
238 case -NFS4ERR_BAD_STATEID:
239 case -NFS4ERR_OPENMODE:
242 nfs4_state_mark_reclaim_nograce(clp, state);
243 case -NFS4ERR_STALE_CLIENTID:
244 case -NFS4ERR_STALE_STATEID:
245 case -NFS4ERR_EXPIRED:
246 nfs4_schedule_state_recovery(clp);
247 ret = nfs4_wait_clnt_recover(clp);
249 exception->retry = 1;
251 case -NFS4ERR_FILE_OPEN:
254 ret = nfs4_delay(server->client, &exception->timeout);
257 case -NFS4ERR_OLD_STATEID:
258 exception->retry = 1;
260 /* We failed to handle the error */
261 return nfs4_map_errors(ret);
265 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
267 struct nfs_client *clp = server->nfs_client;
268 spin_lock(&clp->cl_lock);
269 if (time_before(clp->cl_last_renewal,timestamp))
270 clp->cl_last_renewal = timestamp;
271 spin_unlock(&clp->cl_lock);
274 #if defined(CONFIG_NFS_V4_1)
277 * nfs4_find_slot - efficiently look for a free slot
279 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
280 * If found, we mark the slot as used, update the highest_used_slotid,
281 * and respectively set up the sequence operation args.
282 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
285 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
288 u8 ret_id = NFS4_MAX_SLOT_TABLE;
289 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
291 spin_lock(&tbl->slot_tbl_lock);
292 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
293 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
295 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
296 if (slotid >= tbl->max_slots)
298 __set_bit(slotid, tbl->used_slots);
299 if (slotid > tbl->highest_used_slotid)
300 tbl->highest_used_slotid = slotid;
303 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
304 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
305 spin_unlock(&tbl->slot_tbl_lock);
309 static int nfs41_setup_sequence(struct nfs4_session *session,
310 struct nfs4_sequence_args *args,
311 struct nfs4_sequence_res *res,
313 struct rpc_task *task)
315 /* slot already allocated? */
316 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
319 memset(res, 0, sizeof(*res));
320 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
324 int nfs4_setup_sequence(struct nfs_client *clp,
325 struct nfs4_sequence_args *args,
326 struct nfs4_sequence_res *res,
328 struct rpc_task *task)
332 dprintk("--> %s clp %p session %p sr_slotid %d\n",
333 __func__, clp, clp->cl_session, res->sr_slotid);
335 if (!nfs4_has_session(clp))
337 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
339 if (ret != -EAGAIN) {
340 /* terminate rpc task */
341 task->tk_status = ret;
342 task->tk_action = NULL;
345 dprintk("<-- %s status=%d\n", __func__, ret);
349 struct nfs41_call_sync_data {
350 struct nfs_client *clp;
351 struct nfs4_sequence_args *seq_args;
352 struct nfs4_sequence_res *seq_res;
356 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
358 struct nfs41_call_sync_data *data = calldata;
360 dprintk("--> %s data->clp->cl_session %p\n", __func__,
361 data->clp->cl_session);
362 if (nfs4_setup_sequence(data->clp, data->seq_args,
363 data->seq_res, data->cache_reply, task))
365 rpc_call_start(task);
368 struct rpc_call_ops nfs41_call_sync_ops = {
369 .rpc_call_prepare = nfs41_call_sync_prepare,
372 static int nfs4_call_sync_sequence(struct nfs_client *clp,
373 struct rpc_clnt *clnt,
374 struct rpc_message *msg,
375 struct nfs4_sequence_args *args,
376 struct nfs4_sequence_res *res,
380 struct rpc_task *task;
381 struct nfs41_call_sync_data data = {
385 .cache_reply = cache_reply,
387 struct rpc_task_setup task_setup = {
390 .callback_ops = &nfs41_call_sync_ops,
391 .callback_data = &data
394 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
395 task = rpc_run_task(&task_setup);
399 ret = task->tk_status;
405 int _nfs4_call_sync_session(struct nfs_server *server,
406 struct rpc_message *msg,
407 struct nfs4_sequence_args *args,
408 struct nfs4_sequence_res *res,
411 return nfs4_call_sync_sequence(server->nfs_client, server->client,
412 msg, args, res, cache_reply);
415 #endif /* CONFIG_NFS_V4_1 */
417 int _nfs4_call_sync(struct nfs_server *server,
418 struct rpc_message *msg,
419 struct nfs4_sequence_args *args,
420 struct nfs4_sequence_res *res,
423 args->sa_session = res->sr_session = NULL;
424 return rpc_call_sync(server->client, msg, 0);
427 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
428 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
429 &(res)->seq_res, (cache_reply))
431 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
433 struct nfs_inode *nfsi = NFS_I(dir);
435 spin_lock(&dir->i_lock);
436 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
437 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
438 nfs_force_lookup_revalidate(dir);
439 nfsi->change_attr = cinfo->after;
440 spin_unlock(&dir->i_lock);
443 struct nfs4_opendata {
445 struct nfs_openargs o_arg;
446 struct nfs_openres o_res;
447 struct nfs_open_confirmargs c_arg;
448 struct nfs_open_confirmres c_res;
449 struct nfs_fattr f_attr;
450 struct nfs_fattr dir_attr;
453 struct nfs4_state_owner *owner;
454 struct nfs4_state *state;
456 unsigned long timestamp;
457 unsigned int rpc_done : 1;
463 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
465 p->o_res.f_attr = &p->f_attr;
466 p->o_res.dir_attr = &p->dir_attr;
467 p->o_res.seqid = p->o_arg.seqid;
468 p->c_res.seqid = p->c_arg.seqid;
469 p->o_res.server = p->o_arg.server;
470 nfs_fattr_init(&p->f_attr);
471 nfs_fattr_init(&p->dir_attr);
474 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
475 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
476 const struct iattr *attrs)
478 struct dentry *parent = dget_parent(path->dentry);
479 struct inode *dir = parent->d_inode;
480 struct nfs_server *server = NFS_SERVER(dir);
481 struct nfs4_opendata *p;
483 p = kzalloc(sizeof(*p), GFP_KERNEL);
486 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
487 if (p->o_arg.seqid == NULL)
489 p->path.mnt = mntget(path->mnt);
490 p->path.dentry = dget(path->dentry);
493 atomic_inc(&sp->so_count);
494 p->o_arg.fh = NFS_FH(dir);
495 p->o_arg.open_flags = flags;
496 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
497 p->o_arg.clientid = server->nfs_client->cl_clientid;
498 p->o_arg.id = sp->so_owner_id.id;
499 p->o_arg.name = &p->path.dentry->d_name;
500 p->o_arg.server = server;
501 p->o_arg.bitmask = server->attr_bitmask;
502 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
503 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
504 if (flags & O_EXCL) {
505 u32 *s = (u32 *) p->o_arg.u.verifier.data;
508 } else if (flags & O_CREAT) {
509 p->o_arg.u.attrs = &p->attrs;
510 memcpy(&p->attrs, attrs, sizeof(p->attrs));
512 p->c_arg.fh = &p->o_res.fh;
513 p->c_arg.stateid = &p->o_res.stateid;
514 p->c_arg.seqid = p->o_arg.seqid;
515 nfs4_init_opendata_res(p);
525 static void nfs4_opendata_free(struct kref *kref)
527 struct nfs4_opendata *p = container_of(kref,
528 struct nfs4_opendata, kref);
530 nfs_free_seqid(p->o_arg.seqid);
531 if (p->state != NULL)
532 nfs4_put_open_state(p->state);
533 nfs4_put_state_owner(p->owner);
539 static void nfs4_opendata_put(struct nfs4_opendata *p)
542 kref_put(&p->kref, nfs4_opendata_free);
545 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
549 ret = rpc_wait_for_completion_task(task);
553 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
557 if (open_mode & O_EXCL)
559 switch (mode & (FMODE_READ|FMODE_WRITE)) {
561 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
564 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
566 case FMODE_READ|FMODE_WRITE:
567 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
573 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
575 if ((delegation->type & fmode) != fmode)
577 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
579 nfs_mark_delegation_referenced(delegation);
583 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
592 case FMODE_READ|FMODE_WRITE:
595 nfs4_state_set_mode_locked(state, state->state | fmode);
598 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
600 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
601 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
602 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
605 set_bit(NFS_O_RDONLY_STATE, &state->flags);
608 set_bit(NFS_O_WRONLY_STATE, &state->flags);
610 case FMODE_READ|FMODE_WRITE:
611 set_bit(NFS_O_RDWR_STATE, &state->flags);
615 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
617 write_seqlock(&state->seqlock);
618 nfs_set_open_stateid_locked(state, stateid, fmode);
619 write_sequnlock(&state->seqlock);
622 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
625 * Protect the call to nfs4_state_set_mode_locked and
626 * serialise the stateid update
628 write_seqlock(&state->seqlock);
629 if (deleg_stateid != NULL) {
630 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
631 set_bit(NFS_DELEGATED_STATE, &state->flags);
633 if (open_stateid != NULL)
634 nfs_set_open_stateid_locked(state, open_stateid, fmode);
635 write_sequnlock(&state->seqlock);
636 spin_lock(&state->owner->so_lock);
637 update_open_stateflags(state, fmode);
638 spin_unlock(&state->owner->so_lock);
641 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
643 struct nfs_inode *nfsi = NFS_I(state->inode);
644 struct nfs_delegation *deleg_cur;
647 fmode &= (FMODE_READ|FMODE_WRITE);
650 deleg_cur = rcu_dereference(nfsi->delegation);
651 if (deleg_cur == NULL)
654 spin_lock(&deleg_cur->lock);
655 if (nfsi->delegation != deleg_cur ||
656 (deleg_cur->type & fmode) != fmode)
657 goto no_delegation_unlock;
659 if (delegation == NULL)
660 delegation = &deleg_cur->stateid;
661 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
662 goto no_delegation_unlock;
664 nfs_mark_delegation_referenced(deleg_cur);
665 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
667 no_delegation_unlock:
668 spin_unlock(&deleg_cur->lock);
672 if (!ret && open_stateid != NULL) {
673 __update_open_stateid(state, open_stateid, NULL, fmode);
681 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
683 struct nfs_delegation *delegation;
686 delegation = rcu_dereference(NFS_I(inode)->delegation);
687 if (delegation == NULL || (delegation->type & fmode) == fmode) {
692 nfs_inode_return_delegation(inode);
695 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
697 struct nfs4_state *state = opendata->state;
698 struct nfs_inode *nfsi = NFS_I(state->inode);
699 struct nfs_delegation *delegation;
700 int open_mode = opendata->o_arg.open_flags & O_EXCL;
701 fmode_t fmode = opendata->o_arg.fmode;
702 nfs4_stateid stateid;
706 if (can_open_cached(state, fmode, open_mode)) {
707 spin_lock(&state->owner->so_lock);
708 if (can_open_cached(state, fmode, open_mode)) {
709 update_open_stateflags(state, fmode);
710 spin_unlock(&state->owner->so_lock);
711 goto out_return_state;
713 spin_unlock(&state->owner->so_lock);
716 delegation = rcu_dereference(nfsi->delegation);
717 if (delegation == NULL ||
718 !can_open_delegated(delegation, fmode)) {
722 /* Save the delegation */
723 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
725 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
730 /* Try to update the stateid using the delegation */
731 if (update_open_stateid(state, NULL, &stateid, fmode))
732 goto out_return_state;
737 atomic_inc(&state->count);
741 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
744 struct nfs4_state *state = NULL;
745 struct nfs_delegation *delegation;
748 if (!data->rpc_done) {
749 state = nfs4_try_open_cached(data);
754 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
756 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
757 ret = PTR_ERR(inode);
761 state = nfs4_get_open_state(inode, data->owner);
764 if (data->o_res.delegation_type != 0) {
765 int delegation_flags = 0;
768 delegation = rcu_dereference(NFS_I(inode)->delegation);
770 delegation_flags = delegation->flags;
772 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
773 nfs_inode_set_delegation(state->inode,
774 data->owner->so_cred,
777 nfs_inode_reclaim_delegation(state->inode,
778 data->owner->so_cred,
782 update_open_stateid(state, &data->o_res.stateid, NULL,
793 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
795 struct nfs_inode *nfsi = NFS_I(state->inode);
796 struct nfs_open_context *ctx;
798 spin_lock(&state->inode->i_lock);
799 list_for_each_entry(ctx, &nfsi->open_files, list) {
800 if (ctx->state != state)
802 get_nfs_open_context(ctx);
803 spin_unlock(&state->inode->i_lock);
806 spin_unlock(&state->inode->i_lock);
807 return ERR_PTR(-ENOENT);
810 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
812 struct nfs4_opendata *opendata;
814 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
815 if (opendata == NULL)
816 return ERR_PTR(-ENOMEM);
817 opendata->state = state;
818 atomic_inc(&state->count);
822 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
824 struct nfs4_state *newstate;
827 opendata->o_arg.open_flags = 0;
828 opendata->o_arg.fmode = fmode;
829 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
830 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
831 nfs4_init_opendata_res(opendata);
832 ret = _nfs4_proc_open(opendata);
835 newstate = nfs4_opendata_to_nfs4_state(opendata);
836 if (IS_ERR(newstate))
837 return PTR_ERR(newstate);
838 nfs4_close_state(&opendata->path, newstate, fmode);
843 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
845 struct nfs4_state *newstate;
848 /* memory barrier prior to reading state->n_* */
849 clear_bit(NFS_DELEGATED_STATE, &state->flags);
851 if (state->n_rdwr != 0) {
852 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
855 if (newstate != state)
858 if (state->n_wronly != 0) {
859 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
862 if (newstate != state)
865 if (state->n_rdonly != 0) {
866 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
869 if (newstate != state)
873 * We may have performed cached opens for all three recoveries.
874 * Check if we need to update the current stateid.
876 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
877 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
878 write_seqlock(&state->seqlock);
879 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
880 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
881 write_sequnlock(&state->seqlock);
888 * reclaim state on the server after a reboot.
890 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
892 struct nfs_delegation *delegation;
893 struct nfs4_opendata *opendata;
894 fmode_t delegation_type = 0;
897 opendata = nfs4_open_recoverdata_alloc(ctx, state);
898 if (IS_ERR(opendata))
899 return PTR_ERR(opendata);
900 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
901 opendata->o_arg.fh = NFS_FH(state->inode);
903 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
904 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
905 delegation_type = delegation->type;
907 opendata->o_arg.u.delegation_type = delegation_type;
908 status = nfs4_open_recover(opendata, state);
909 nfs4_opendata_put(opendata);
913 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
915 struct nfs_server *server = NFS_SERVER(state->inode);
916 struct nfs4_exception exception = { };
919 err = _nfs4_do_open_reclaim(ctx, state);
920 if (err != -NFS4ERR_DELAY)
922 nfs4_handle_exception(server, err, &exception);
923 } while (exception.retry);
927 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
929 struct nfs_open_context *ctx;
932 ctx = nfs4_state_find_open_context(state);
935 ret = nfs4_do_open_reclaim(ctx, state);
936 put_nfs_open_context(ctx);
940 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
942 struct nfs4_opendata *opendata;
945 opendata = nfs4_open_recoverdata_alloc(ctx, state);
946 if (IS_ERR(opendata))
947 return PTR_ERR(opendata);
948 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
949 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
950 sizeof(opendata->o_arg.u.delegation.data));
951 ret = nfs4_open_recover(opendata, state);
952 nfs4_opendata_put(opendata);
956 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
958 struct nfs4_exception exception = { };
959 struct nfs_server *server = NFS_SERVER(state->inode);
962 err = _nfs4_open_delegation_recall(ctx, state, stateid);
966 case -NFS4ERR_STALE_CLIENTID:
967 case -NFS4ERR_STALE_STATEID:
968 case -NFS4ERR_EXPIRED:
969 /* Don't recall a delegation if it was lost */
970 nfs4_schedule_state_recovery(server->nfs_client);
973 err = nfs4_handle_exception(server, err, &exception);
974 } while (exception.retry);
978 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
980 struct nfs4_opendata *data = calldata;
982 data->rpc_status = task->tk_status;
983 if (RPC_ASSASSINATED(task))
985 if (data->rpc_status == 0) {
986 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
987 sizeof(data->o_res.stateid.data));
988 nfs_confirm_seqid(&data->owner->so_seqid, 0);
989 renew_lease(data->o_res.server, data->timestamp);
994 static void nfs4_open_confirm_release(void *calldata)
996 struct nfs4_opendata *data = calldata;
997 struct nfs4_state *state = NULL;
999 /* If this request hasn't been cancelled, do nothing */
1000 if (data->cancelled == 0)
1002 /* In case of error, no cleanup! */
1003 if (!data->rpc_done)
1005 state = nfs4_opendata_to_nfs4_state(data);
1007 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1009 nfs4_opendata_put(data);
1012 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1013 .rpc_call_done = nfs4_open_confirm_done,
1014 .rpc_release = nfs4_open_confirm_release,
1018 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1020 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1022 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1023 struct rpc_task *task;
1024 struct rpc_message msg = {
1025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1026 .rpc_argp = &data->c_arg,
1027 .rpc_resp = &data->c_res,
1028 .rpc_cred = data->owner->so_cred,
1030 struct rpc_task_setup task_setup_data = {
1031 .rpc_client = server->client,
1032 .rpc_message = &msg,
1033 .callback_ops = &nfs4_open_confirm_ops,
1034 .callback_data = data,
1035 .workqueue = nfsiod_workqueue,
1036 .flags = RPC_TASK_ASYNC,
1040 kref_get(&data->kref);
1042 data->rpc_status = 0;
1043 data->timestamp = jiffies;
1044 task = rpc_run_task(&task_setup_data);
1046 return PTR_ERR(task);
1047 status = nfs4_wait_for_completion_rpc_task(task);
1049 data->cancelled = 1;
1052 status = data->rpc_status;
1057 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1059 struct nfs4_opendata *data = calldata;
1060 struct nfs4_state_owner *sp = data->owner;
1062 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1065 * Check if we still need to send an OPEN call, or if we can use
1066 * a delegation instead.
1068 if (data->state != NULL) {
1069 struct nfs_delegation *delegation;
1071 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1074 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1075 if (delegation != NULL &&
1076 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1082 /* Update sequence id. */
1083 data->o_arg.id = sp->so_owner_id.id;
1084 data->o_arg.clientid = sp->so_client->cl_clientid;
1085 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1086 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1087 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1089 data->timestamp = jiffies;
1090 rpc_call_start(task);
1093 task->tk_action = NULL;
1097 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1099 struct nfs4_opendata *data = calldata;
1101 data->rpc_status = task->tk_status;
1102 if (RPC_ASSASSINATED(task))
1104 if (task->tk_status == 0) {
1105 switch (data->o_res.f_attr->mode & S_IFMT) {
1109 data->rpc_status = -ELOOP;
1112 data->rpc_status = -EISDIR;
1115 data->rpc_status = -ENOTDIR;
1117 renew_lease(data->o_res.server, data->timestamp);
1118 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1119 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1124 static void nfs4_open_release(void *calldata)
1126 struct nfs4_opendata *data = calldata;
1127 struct nfs4_state *state = NULL;
1129 /* If this request hasn't been cancelled, do nothing */
1130 if (data->cancelled == 0)
1132 /* In case of error, no cleanup! */
1133 if (data->rpc_status != 0 || !data->rpc_done)
1135 /* In case we need an open_confirm, no cleanup! */
1136 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1138 state = nfs4_opendata_to_nfs4_state(data);
1140 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1142 nfs4_opendata_put(data);
1145 static const struct rpc_call_ops nfs4_open_ops = {
1146 .rpc_call_prepare = nfs4_open_prepare,
1147 .rpc_call_done = nfs4_open_done,
1148 .rpc_release = nfs4_open_release,
1152 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1154 static int _nfs4_proc_open(struct nfs4_opendata *data)
1156 struct inode *dir = data->dir->d_inode;
1157 struct nfs_server *server = NFS_SERVER(dir);
1158 struct nfs_openargs *o_arg = &data->o_arg;
1159 struct nfs_openres *o_res = &data->o_res;
1160 struct rpc_task *task;
1161 struct rpc_message msg = {
1162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1165 .rpc_cred = data->owner->so_cred,
1167 struct rpc_task_setup task_setup_data = {
1168 .rpc_client = server->client,
1169 .rpc_message = &msg,
1170 .callback_ops = &nfs4_open_ops,
1171 .callback_data = data,
1172 .workqueue = nfsiod_workqueue,
1173 .flags = RPC_TASK_ASYNC,
1177 kref_get(&data->kref);
1179 data->rpc_status = 0;
1180 data->cancelled = 0;
1181 task = rpc_run_task(&task_setup_data);
1183 return PTR_ERR(task);
1184 status = nfs4_wait_for_completion_rpc_task(task);
1186 data->cancelled = 1;
1189 status = data->rpc_status;
1191 if (status != 0 || !data->rpc_done)
1194 if (o_res->fh.size == 0)
1195 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1197 if (o_arg->open_flags & O_CREAT) {
1198 update_changeattr(dir, &o_res->cinfo);
1199 nfs_post_op_update_inode(dir, o_res->dir_attr);
1201 nfs_refresh_inode(dir, o_res->dir_attr);
1202 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1203 status = _nfs4_proc_open_confirm(data);
1207 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1208 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1212 static int nfs4_recover_expired_lease(struct nfs_server *server)
1214 struct nfs_client *clp = server->nfs_client;
1218 ret = nfs4_wait_clnt_recover(clp);
1221 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1222 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1224 nfs4_schedule_state_recovery(clp);
1231 * reclaim state on the server after a network partition.
1232 * Assumes caller holds the appropriate lock
1234 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1236 struct nfs4_opendata *opendata;
1239 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1240 if (IS_ERR(opendata))
1241 return PTR_ERR(opendata);
1242 ret = nfs4_open_recover(opendata, state);
1244 d_drop(ctx->path.dentry);
1245 nfs4_opendata_put(opendata);
1249 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1251 struct nfs_server *server = NFS_SERVER(state->inode);
1252 struct nfs4_exception exception = { };
1256 err = _nfs4_open_expired(ctx, state);
1257 if (err != -NFS4ERR_DELAY)
1259 nfs4_handle_exception(server, err, &exception);
1260 } while (exception.retry);
1264 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1266 struct nfs_open_context *ctx;
1269 ctx = nfs4_state_find_open_context(state);
1271 return PTR_ERR(ctx);
1272 ret = nfs4_do_open_expired(ctx, state);
1273 put_nfs_open_context(ctx);
1278 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1279 * fields corresponding to attributes that were used to store the verifier.
1280 * Make sure we clobber those fields in the later setattr call
1282 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1284 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1285 !(sattr->ia_valid & ATTR_ATIME_SET))
1286 sattr->ia_valid |= ATTR_ATIME;
1288 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1289 !(sattr->ia_valid & ATTR_MTIME_SET))
1290 sattr->ia_valid |= ATTR_MTIME;
1294 * Returns a referenced nfs4_state
1296 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)
1298 struct nfs4_state_owner *sp;
1299 struct nfs4_state *state = NULL;
1300 struct nfs_server *server = NFS_SERVER(dir);
1301 struct nfs4_opendata *opendata;
1304 /* Protect against reboot recovery conflicts */
1306 if (!(sp = nfs4_get_state_owner(server, cred))) {
1307 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1310 status = nfs4_recover_expired_lease(server);
1312 goto err_put_state_owner;
1313 if (path->dentry->d_inode != NULL)
1314 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1316 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1317 if (opendata == NULL)
1318 goto err_put_state_owner;
1320 if (path->dentry->d_inode != NULL)
1321 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1323 status = _nfs4_proc_open(opendata);
1325 goto err_opendata_put;
1327 if (opendata->o_arg.open_flags & O_EXCL)
1328 nfs4_exclusive_attrset(opendata, sattr);
1330 state = nfs4_opendata_to_nfs4_state(opendata);
1331 status = PTR_ERR(state);
1333 goto err_opendata_put;
1334 nfs4_opendata_put(opendata);
1335 nfs4_put_state_owner(sp);
1339 nfs4_opendata_put(opendata);
1340 err_put_state_owner:
1341 nfs4_put_state_owner(sp);
1348 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)
1350 struct nfs4_exception exception = { };
1351 struct nfs4_state *res;
1355 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1358 /* NOTE: BAD_SEQID means the server and client disagree about the
1359 * book-keeping w.r.t. state-changing operations
1360 * (OPEN/CLOSE/LOCK/LOCKU...)
1361 * It is actually a sign of a bug on the client or on the server.
1363 * If we receive a BAD_SEQID error in the particular case of
1364 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1365 * have unhashed the old state_owner for us, and that we can
1366 * therefore safely retry using a new one. We should still warn
1367 * the user though...
1369 if (status == -NFS4ERR_BAD_SEQID) {
1370 printk(KERN_WARNING "NFS: v4 server %s "
1371 " returned a bad sequence-id error!\n",
1372 NFS_SERVER(dir)->nfs_client->cl_hostname);
1373 exception.retry = 1;
1377 * BAD_STATEID on OPEN means that the server cancelled our
1378 * state before it received the OPEN_CONFIRM.
1379 * Recover by retrying the request as per the discussion
1380 * on Page 181 of RFC3530.
1382 if (status == -NFS4ERR_BAD_STATEID) {
1383 exception.retry = 1;
1386 if (status == -EAGAIN) {
1387 /* We must have found a delegation */
1388 exception.retry = 1;
1391 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1392 status, &exception));
1393 } while (exception.retry);
1397 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1398 struct nfs_fattr *fattr, struct iattr *sattr,
1399 struct nfs4_state *state)
1401 struct nfs_server *server = NFS_SERVER(inode);
1402 struct nfs_setattrargs arg = {
1403 .fh = NFS_FH(inode),
1406 .bitmask = server->attr_bitmask,
1408 struct nfs_setattrres res = {
1412 struct rpc_message msg = {
1413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1418 unsigned long timestamp = jiffies;
1421 nfs_fattr_init(fattr);
1423 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1424 /* Use that stateid */
1425 } else if (state != NULL) {
1426 nfs4_copy_stateid(&arg.stateid, state, current->files);
1428 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1430 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1431 if (status == 0 && state != NULL)
1432 renew_lease(server, timestamp);
1436 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1437 struct nfs_fattr *fattr, struct iattr *sattr,
1438 struct nfs4_state *state)
1440 struct nfs_server *server = NFS_SERVER(inode);
1441 struct nfs4_exception exception = { };
1444 err = nfs4_handle_exception(server,
1445 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1447 } while (exception.retry);
1451 struct nfs4_closedata {
1453 struct inode *inode;
1454 struct nfs4_state *state;
1455 struct nfs_closeargs arg;
1456 struct nfs_closeres res;
1457 struct nfs_fattr fattr;
1458 unsigned long timestamp;
1461 static void nfs4_free_closedata(void *data)
1463 struct nfs4_closedata *calldata = data;
1464 struct nfs4_state_owner *sp = calldata->state->owner;
1466 nfs4_put_open_state(calldata->state);
1467 nfs_free_seqid(calldata->arg.seqid);
1468 nfs4_put_state_owner(sp);
1469 path_put(&calldata->path);
1473 static void nfs4_close_done(struct rpc_task *task, void *data)
1475 struct nfs4_closedata *calldata = data;
1476 struct nfs4_state *state = calldata->state;
1477 struct nfs_server *server = NFS_SERVER(calldata->inode);
1479 if (RPC_ASSASSINATED(task))
1481 /* hmm. we are done with the inode, and in the process of freeing
1482 * the state_owner. we keep this around to process errors
1484 switch (task->tk_status) {
1486 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1487 renew_lease(server, calldata->timestamp);
1489 case -NFS4ERR_STALE_STATEID:
1490 case -NFS4ERR_OLD_STATEID:
1491 case -NFS4ERR_BAD_STATEID:
1492 case -NFS4ERR_EXPIRED:
1493 if (calldata->arg.fmode == 0)
1496 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1497 rpc_restart_call(task);
1501 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1504 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1506 struct nfs4_closedata *calldata = data;
1507 struct nfs4_state *state = calldata->state;
1508 int clear_rd, clear_wr, clear_rdwr;
1510 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1513 clear_rd = clear_wr = clear_rdwr = 0;
1514 spin_lock(&state->owner->so_lock);
1515 /* Calculate the change in open mode */
1516 if (state->n_rdwr == 0) {
1517 if (state->n_rdonly == 0) {
1518 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1519 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1521 if (state->n_wronly == 0) {
1522 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1523 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1526 spin_unlock(&state->owner->so_lock);
1527 if (!clear_rd && !clear_wr && !clear_rdwr) {
1528 /* Note: exit _without_ calling nfs4_close_done */
1529 task->tk_action = NULL;
1532 nfs_fattr_init(calldata->res.fattr);
1533 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1534 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1535 calldata->arg.fmode = FMODE_READ;
1536 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1537 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1538 calldata->arg.fmode = FMODE_WRITE;
1540 calldata->timestamp = jiffies;
1541 rpc_call_start(task);
1544 static const struct rpc_call_ops nfs4_close_ops = {
1545 .rpc_call_prepare = nfs4_close_prepare,
1546 .rpc_call_done = nfs4_close_done,
1547 .rpc_release = nfs4_free_closedata,
1551 * It is possible for data to be read/written from a mem-mapped file
1552 * after the sys_close call (which hits the vfs layer as a flush).
1553 * This means that we can't safely call nfsv4 close on a file until
1554 * the inode is cleared. This in turn means that we are not good
1555 * NFSv4 citizens - we do not indicate to the server to update the file's
1556 * share state even when we are done with one of the three share
1557 * stateid's in the inode.
1559 * NOTE: Caller must be holding the sp->so_owner semaphore!
1561 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1563 struct nfs_server *server = NFS_SERVER(state->inode);
1564 struct nfs4_closedata *calldata;
1565 struct nfs4_state_owner *sp = state->owner;
1566 struct rpc_task *task;
1567 struct rpc_message msg = {
1568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1569 .rpc_cred = state->owner->so_cred,
1571 struct rpc_task_setup task_setup_data = {
1572 .rpc_client = server->client,
1573 .rpc_message = &msg,
1574 .callback_ops = &nfs4_close_ops,
1575 .workqueue = nfsiod_workqueue,
1576 .flags = RPC_TASK_ASYNC,
1578 int status = -ENOMEM;
1580 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1581 if (calldata == NULL)
1583 calldata->inode = state->inode;
1584 calldata->state = state;
1585 calldata->arg.fh = NFS_FH(state->inode);
1586 calldata->arg.stateid = &state->open_stateid;
1587 /* Serialization for the sequence id */
1588 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1589 if (calldata->arg.seqid == NULL)
1590 goto out_free_calldata;
1591 calldata->arg.fmode = 0;
1592 calldata->arg.bitmask = server->cache_consistency_bitmask;
1593 calldata->res.fattr = &calldata->fattr;
1594 calldata->res.seqid = calldata->arg.seqid;
1595 calldata->res.server = server;
1596 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1597 calldata->path.mnt = mntget(path->mnt);
1598 calldata->path.dentry = dget(path->dentry);
1600 msg.rpc_argp = &calldata->arg,
1601 msg.rpc_resp = &calldata->res,
1602 task_setup_data.callback_data = calldata;
1603 task = rpc_run_task(&task_setup_data);
1605 return PTR_ERR(task);
1608 status = rpc_wait_for_completion_task(task);
1614 nfs4_put_open_state(state);
1615 nfs4_put_state_owner(sp);
1619 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1624 /* If the open_intent is for execute, we have an extra check to make */
1625 if (fmode & FMODE_EXEC) {
1626 ret = nfs_may_open(state->inode,
1627 state->owner->so_cred,
1628 nd->intent.open.flags);
1632 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1633 if (!IS_ERR(filp)) {
1634 struct nfs_open_context *ctx;
1635 ctx = nfs_file_open_context(filp);
1639 ret = PTR_ERR(filp);
1641 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1646 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1648 struct path path = {
1649 .mnt = nd->path.mnt,
1652 struct dentry *parent;
1654 struct rpc_cred *cred;
1655 struct nfs4_state *state;
1657 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1659 if (nd->flags & LOOKUP_CREATE) {
1660 attr.ia_mode = nd->intent.open.create_mode;
1661 attr.ia_valid = ATTR_MODE;
1662 if (!IS_POSIXACL(dir))
1663 attr.ia_mode &= ~current_umask();
1666 BUG_ON(nd->intent.open.flags & O_CREAT);
1669 cred = rpc_lookup_cred();
1671 return (struct dentry *)cred;
1672 parent = dentry->d_parent;
1673 /* Protect against concurrent sillydeletes */
1674 nfs_block_sillyrename(parent);
1675 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1677 if (IS_ERR(state)) {
1678 if (PTR_ERR(state) == -ENOENT) {
1679 d_add(dentry, NULL);
1680 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1682 nfs_unblock_sillyrename(parent);
1683 return (struct dentry *)state;
1685 res = d_add_unique(dentry, igrab(state->inode));
1688 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1689 nfs_unblock_sillyrename(parent);
1690 nfs4_intent_set_file(nd, &path, state, fmode);
1695 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1697 struct path path = {
1698 .mnt = nd->path.mnt,
1701 struct rpc_cred *cred;
1702 struct nfs4_state *state;
1703 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1705 cred = rpc_lookup_cred();
1707 return PTR_ERR(cred);
1708 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1710 if (IS_ERR(state)) {
1711 switch (PTR_ERR(state)) {
1717 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1723 if (state->inode == dentry->d_inode) {
1724 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1725 nfs4_intent_set_file(nd, &path, state, fmode);
1728 nfs4_close_sync(&path, state, fmode);
1734 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1736 if (ctx->state == NULL)
1739 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1741 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1744 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1746 struct nfs4_server_caps_arg args = {
1749 struct nfs4_server_caps_res res = {};
1750 struct rpc_message msg = {
1751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1757 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1759 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1760 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1761 server->caps |= NFS_CAP_ACLS;
1762 if (res.has_links != 0)
1763 server->caps |= NFS_CAP_HARDLINKS;
1764 if (res.has_symlinks != 0)
1765 server->caps |= NFS_CAP_SYMLINKS;
1766 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1767 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1768 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1769 server->acl_bitmask = res.acl_bitmask;
1775 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1777 struct nfs4_exception exception = { };
1780 err = nfs4_handle_exception(server,
1781 _nfs4_server_capabilities(server, fhandle),
1783 } while (exception.retry);
1787 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1788 struct nfs_fsinfo *info)
1790 struct nfs4_lookup_root_arg args = {
1791 .bitmask = nfs4_fattr_bitmap,
1793 struct nfs4_lookup_res res = {
1795 .fattr = info->fattr,
1798 struct rpc_message msg = {
1799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1803 nfs_fattr_init(info->fattr);
1804 return nfs4_call_sync(server, &msg, &args, &res, 0);
1807 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1808 struct nfs_fsinfo *info)
1810 struct nfs4_exception exception = { };
1813 err = nfs4_handle_exception(server,
1814 _nfs4_lookup_root(server, fhandle, info),
1816 } while (exception.retry);
1821 * get the file handle for the "/" directory on the server
1823 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1824 struct nfs_fsinfo *info)
1828 status = nfs4_lookup_root(server, fhandle, info);
1830 status = nfs4_server_capabilities(server, fhandle);
1832 status = nfs4_do_fsinfo(server, fhandle, info);
1833 return nfs4_map_errors(status);
1837 * Get locations and (maybe) other attributes of a referral.
1838 * Note that we'll actually follow the referral later when
1839 * we detect fsid mismatch in inode revalidation
1841 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1843 int status = -ENOMEM;
1844 struct page *page = NULL;
1845 struct nfs4_fs_locations *locations = NULL;
1847 page = alloc_page(GFP_KERNEL);
1850 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1851 if (locations == NULL)
1854 status = nfs4_proc_fs_locations(dir, name, locations, page);
1857 /* Make sure server returned a different fsid for the referral */
1858 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1859 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
1864 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1865 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1867 fattr->mode = S_IFDIR;
1868 memset(fhandle, 0, sizeof(struct nfs_fh));
1877 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1879 struct nfs4_getattr_arg args = {
1881 .bitmask = server->attr_bitmask,
1883 struct nfs4_getattr_res res = {
1887 struct rpc_message msg = {
1888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1893 nfs_fattr_init(fattr);
1894 return nfs4_call_sync(server, &msg, &args, &res, 0);
1897 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1899 struct nfs4_exception exception = { };
1902 err = nfs4_handle_exception(server,
1903 _nfs4_proc_getattr(server, fhandle, fattr),
1905 } while (exception.retry);
1910 * The file is not closed if it is opened due to the a request to change
1911 * the size of the file. The open call will not be needed once the
1912 * VFS layer lookup-intents are implemented.
1914 * Close is called when the inode is destroyed.
1915 * If we haven't opened the file for O_WRONLY, we
1916 * need to in the size_change case to obtain a stateid.
1919 * Because OPEN is always done by name in nfsv4, it is
1920 * possible that we opened a different file by the same
1921 * name. We can recognize this race condition, but we
1922 * can't do anything about it besides returning an error.
1924 * This will be fixed with VFS changes (lookup-intent).
1927 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1928 struct iattr *sattr)
1930 struct inode *inode = dentry->d_inode;
1931 struct rpc_cred *cred = NULL;
1932 struct nfs4_state *state = NULL;
1935 nfs_fattr_init(fattr);
1937 /* Search for an existing open(O_WRITE) file */
1938 if (sattr->ia_valid & ATTR_FILE) {
1939 struct nfs_open_context *ctx;
1941 ctx = nfs_file_open_context(sattr->ia_file);
1948 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
1950 nfs_setattr_update_inode(inode, sattr);
1954 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1955 const struct qstr *name, struct nfs_fh *fhandle,
1956 struct nfs_fattr *fattr)
1959 struct nfs4_lookup_arg args = {
1960 .bitmask = server->attr_bitmask,
1964 struct nfs4_lookup_res res = {
1969 struct rpc_message msg = {
1970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1975 nfs_fattr_init(fattr);
1977 dprintk("NFS call lookupfh %s\n", name->name);
1978 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1979 dprintk("NFS reply lookupfh: %d\n", status);
1983 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1984 struct qstr *name, struct nfs_fh *fhandle,
1985 struct nfs_fattr *fattr)
1987 struct nfs4_exception exception = { };
1990 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1992 if (err == -NFS4ERR_MOVED) {
1996 err = nfs4_handle_exception(server, err, &exception);
1997 } while (exception.retry);
2001 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2002 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2006 dprintk("NFS call lookup %s\n", name->name);
2007 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2008 if (status == -NFS4ERR_MOVED)
2009 status = nfs4_get_referral(dir, name, fattr, fhandle);
2010 dprintk("NFS reply lookup: %d\n", status);
2014 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2016 struct nfs4_exception exception = { };
2019 err = nfs4_handle_exception(NFS_SERVER(dir),
2020 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2022 } while (exception.retry);
2026 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2028 struct nfs_server *server = NFS_SERVER(inode);
2029 struct nfs_fattr fattr;
2030 struct nfs4_accessargs args = {
2031 .fh = NFS_FH(inode),
2032 .bitmask = server->attr_bitmask,
2034 struct nfs4_accessres res = {
2038 struct rpc_message msg = {
2039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2042 .rpc_cred = entry->cred,
2044 int mode = entry->mask;
2048 * Determine which access bits we want to ask for...
2050 if (mode & MAY_READ)
2051 args.access |= NFS4_ACCESS_READ;
2052 if (S_ISDIR(inode->i_mode)) {
2053 if (mode & MAY_WRITE)
2054 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2055 if (mode & MAY_EXEC)
2056 args.access |= NFS4_ACCESS_LOOKUP;
2058 if (mode & MAY_WRITE)
2059 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2060 if (mode & MAY_EXEC)
2061 args.access |= NFS4_ACCESS_EXECUTE;
2063 nfs_fattr_init(&fattr);
2064 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2067 if (res.access & NFS4_ACCESS_READ)
2068 entry->mask |= MAY_READ;
2069 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2070 entry->mask |= MAY_WRITE;
2071 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2072 entry->mask |= MAY_EXEC;
2073 nfs_refresh_inode(inode, &fattr);
2078 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2080 struct nfs4_exception exception = { };
2083 err = nfs4_handle_exception(NFS_SERVER(inode),
2084 _nfs4_proc_access(inode, entry),
2086 } while (exception.retry);
2091 * TODO: For the time being, we don't try to get any attributes
2092 * along with any of the zero-copy operations READ, READDIR,
2095 * In the case of the first three, we want to put the GETATTR
2096 * after the read-type operation -- this is because it is hard
2097 * to predict the length of a GETATTR response in v4, and thus
2098 * align the READ data correctly. This means that the GETATTR
2099 * may end up partially falling into the page cache, and we should
2100 * shift it into the 'tail' of the xdr_buf before processing.
2101 * To do this efficiently, we need to know the total length
2102 * of data received, which doesn't seem to be available outside
2105 * In the case of WRITE, we also want to put the GETATTR after
2106 * the operation -- in this case because we want to make sure
2107 * we get the post-operation mtime and size. This means that
2108 * we can't use xdr_encode_pages() as written: we need a variant
2109 * of it which would leave room in the 'tail' iovec.
2111 * Both of these changes to the XDR layer would in fact be quite
2112 * minor, but I decided to leave them for a subsequent patch.
2114 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2115 unsigned int pgbase, unsigned int pglen)
2117 struct nfs4_readlink args = {
2118 .fh = NFS_FH(inode),
2123 struct nfs4_readlink_res res;
2124 struct rpc_message msg = {
2125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2130 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2133 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2134 unsigned int pgbase, unsigned int pglen)
2136 struct nfs4_exception exception = { };
2139 err = nfs4_handle_exception(NFS_SERVER(inode),
2140 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2142 } while (exception.retry);
2148 * We will need to arrange for the VFS layer to provide an atomic open.
2149 * Until then, this create/open method is prone to inefficiency and race
2150 * conditions due to the lookup, create, and open VFS calls from sys_open()
2151 * placed on the wire.
2153 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2154 * The file will be opened again in the subsequent VFS open call
2155 * (nfs4_proc_file_open).
2157 * The open for read will just hang around to be used by any process that
2158 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2162 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2163 int flags, struct nameidata *nd)
2165 struct path path = {
2166 .mnt = nd->path.mnt,
2169 struct nfs4_state *state;
2170 struct rpc_cred *cred;
2171 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2174 cred = rpc_lookup_cred();
2176 status = PTR_ERR(cred);
2179 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2181 if (IS_ERR(state)) {
2182 status = PTR_ERR(state);
2185 d_add(dentry, igrab(state->inode));
2186 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2187 if (flags & O_EXCL) {
2188 struct nfs_fattr fattr;
2189 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2191 nfs_setattr_update_inode(state->inode, sattr);
2192 nfs_post_op_update_inode(state->inode, &fattr);
2194 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2195 status = nfs4_intent_set_file(nd, &path, state, fmode);
2197 nfs4_close_sync(&path, state, fmode);
2204 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2206 struct nfs_server *server = NFS_SERVER(dir);
2207 struct nfs_removeargs args = {
2209 .name.len = name->len,
2210 .name.name = name->name,
2211 .bitmask = server->attr_bitmask,
2213 struct nfs_removeres res = {
2216 struct rpc_message msg = {
2217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2223 nfs_fattr_init(&res.dir_attr);
2224 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2226 update_changeattr(dir, &res.cinfo);
2227 nfs_post_op_update_inode(dir, &res.dir_attr);
2232 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2234 struct nfs4_exception exception = { };
2237 err = nfs4_handle_exception(NFS_SERVER(dir),
2238 _nfs4_proc_remove(dir, name),
2240 } while (exception.retry);
2244 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2246 struct nfs_server *server = NFS_SERVER(dir);
2247 struct nfs_removeargs *args = msg->rpc_argp;
2248 struct nfs_removeres *res = msg->rpc_resp;
2250 args->bitmask = server->cache_consistency_bitmask;
2251 res->server = server;
2252 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2255 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2257 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2259 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2261 update_changeattr(dir, &res->cinfo);
2262 nfs_post_op_update_inode(dir, &res->dir_attr);
2266 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2267 struct inode *new_dir, struct qstr *new_name)
2269 struct nfs_server *server = NFS_SERVER(old_dir);
2270 struct nfs4_rename_arg arg = {
2271 .old_dir = NFS_FH(old_dir),
2272 .new_dir = NFS_FH(new_dir),
2273 .old_name = old_name,
2274 .new_name = new_name,
2275 .bitmask = server->attr_bitmask,
2277 struct nfs_fattr old_fattr, new_fattr;
2278 struct nfs4_rename_res res = {
2280 .old_fattr = &old_fattr,
2281 .new_fattr = &new_fattr,
2283 struct rpc_message msg = {
2284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2290 nfs_fattr_init(res.old_fattr);
2291 nfs_fattr_init(res.new_fattr);
2292 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2295 update_changeattr(old_dir, &res.old_cinfo);
2296 nfs_post_op_update_inode(old_dir, res.old_fattr);
2297 update_changeattr(new_dir, &res.new_cinfo);
2298 nfs_post_op_update_inode(new_dir, res.new_fattr);
2303 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2304 struct inode *new_dir, struct qstr *new_name)
2306 struct nfs4_exception exception = { };
2309 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2310 _nfs4_proc_rename(old_dir, old_name,
2313 } while (exception.retry);
2317 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2319 struct nfs_server *server = NFS_SERVER(inode);
2320 struct nfs4_link_arg arg = {
2321 .fh = NFS_FH(inode),
2322 .dir_fh = NFS_FH(dir),
2324 .bitmask = server->attr_bitmask,
2326 struct nfs_fattr fattr, dir_attr;
2327 struct nfs4_link_res res = {
2330 .dir_attr = &dir_attr,
2332 struct rpc_message msg = {
2333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2339 nfs_fattr_init(res.fattr);
2340 nfs_fattr_init(res.dir_attr);
2341 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2343 update_changeattr(dir, &res.cinfo);
2344 nfs_post_op_update_inode(dir, res.dir_attr);
2345 nfs_post_op_update_inode(inode, res.fattr);
2351 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2353 struct nfs4_exception exception = { };
2356 err = nfs4_handle_exception(NFS_SERVER(inode),
2357 _nfs4_proc_link(inode, dir, name),
2359 } while (exception.retry);
2363 struct nfs4_createdata {
2364 struct rpc_message msg;
2365 struct nfs4_create_arg arg;
2366 struct nfs4_create_res res;
2368 struct nfs_fattr fattr;
2369 struct nfs_fattr dir_fattr;
2372 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2373 struct qstr *name, struct iattr *sattr, u32 ftype)
2375 struct nfs4_createdata *data;
2377 data = kzalloc(sizeof(*data), GFP_KERNEL);
2379 struct nfs_server *server = NFS_SERVER(dir);
2381 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2382 data->msg.rpc_argp = &data->arg;
2383 data->msg.rpc_resp = &data->res;
2384 data->arg.dir_fh = NFS_FH(dir);
2385 data->arg.server = server;
2386 data->arg.name = name;
2387 data->arg.attrs = sattr;
2388 data->arg.ftype = ftype;
2389 data->arg.bitmask = server->attr_bitmask;
2390 data->res.server = server;
2391 data->res.fh = &data->fh;
2392 data->res.fattr = &data->fattr;
2393 data->res.dir_fattr = &data->dir_fattr;
2394 nfs_fattr_init(data->res.fattr);
2395 nfs_fattr_init(data->res.dir_fattr);
2400 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2402 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2403 &data->arg, &data->res, 1);
2405 update_changeattr(dir, &data->res.dir_cinfo);
2406 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2407 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2412 static void nfs4_free_createdata(struct nfs4_createdata *data)
2417 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2418 struct page *page, unsigned int len, struct iattr *sattr)
2420 struct nfs4_createdata *data;
2421 int status = -ENAMETOOLONG;
2423 if (len > NFS4_MAXPATHLEN)
2427 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2431 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2432 data->arg.u.symlink.pages = &page;
2433 data->arg.u.symlink.len = len;
2435 status = nfs4_do_create(dir, dentry, data);
2437 nfs4_free_createdata(data);
2442 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2443 struct page *page, unsigned int len, struct iattr *sattr)
2445 struct nfs4_exception exception = { };
2448 err = nfs4_handle_exception(NFS_SERVER(dir),
2449 _nfs4_proc_symlink(dir, dentry, page,
2452 } while (exception.retry);
2456 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2457 struct iattr *sattr)
2459 struct nfs4_createdata *data;
2460 int status = -ENOMEM;
2462 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2466 status = nfs4_do_create(dir, dentry, data);
2468 nfs4_free_createdata(data);
2473 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2474 struct iattr *sattr)
2476 struct nfs4_exception exception = { };
2479 err = nfs4_handle_exception(NFS_SERVER(dir),
2480 _nfs4_proc_mkdir(dir, dentry, sattr),
2482 } while (exception.retry);
2486 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2487 u64 cookie, struct page *page, unsigned int count, int plus)
2489 struct inode *dir = dentry->d_inode;
2490 struct nfs4_readdir_arg args = {
2495 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
2497 struct nfs4_readdir_res res;
2498 struct rpc_message msg = {
2499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2506 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2507 dentry->d_parent->d_name.name,
2508 dentry->d_name.name,
2509 (unsigned long long)cookie);
2510 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2511 res.pgbase = args.pgbase;
2512 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2514 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2516 nfs_invalidate_atime(dir);
2518 dprintk("%s: returns %d\n", __func__, status);
2522 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2523 u64 cookie, struct page *page, unsigned int count, int plus)
2525 struct nfs4_exception exception = { };
2528 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2529 _nfs4_proc_readdir(dentry, cred, cookie,
2532 } while (exception.retry);
2536 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2537 struct iattr *sattr, dev_t rdev)
2539 struct nfs4_createdata *data;
2540 int mode = sattr->ia_mode;
2541 int status = -ENOMEM;
2543 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2544 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2546 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2551 data->arg.ftype = NF4FIFO;
2552 else if (S_ISBLK(mode)) {
2553 data->arg.ftype = NF4BLK;
2554 data->arg.u.device.specdata1 = MAJOR(rdev);
2555 data->arg.u.device.specdata2 = MINOR(rdev);
2557 else if (S_ISCHR(mode)) {
2558 data->arg.ftype = NF4CHR;
2559 data->arg.u.device.specdata1 = MAJOR(rdev);
2560 data->arg.u.device.specdata2 = MINOR(rdev);
2563 status = nfs4_do_create(dir, dentry, data);
2565 nfs4_free_createdata(data);
2570 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2571 struct iattr *sattr, dev_t rdev)
2573 struct nfs4_exception exception = { };
2576 err = nfs4_handle_exception(NFS_SERVER(dir),
2577 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2579 } while (exception.retry);
2583 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2584 struct nfs_fsstat *fsstat)
2586 struct nfs4_statfs_arg args = {
2588 .bitmask = server->attr_bitmask,
2590 struct nfs4_statfs_res res = {
2593 struct rpc_message msg = {
2594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2599 nfs_fattr_init(fsstat->fattr);
2600 return nfs4_call_sync(server, &msg, &args, &res, 0);
2603 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2605 struct nfs4_exception exception = { };
2608 err = nfs4_handle_exception(server,
2609 _nfs4_proc_statfs(server, fhandle, fsstat),
2611 } while (exception.retry);
2615 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2616 struct nfs_fsinfo *fsinfo)
2618 struct nfs4_fsinfo_arg args = {
2620 .bitmask = server->attr_bitmask,
2622 struct nfs4_fsinfo_res res = {
2625 struct rpc_message msg = {
2626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2631 return nfs4_call_sync(server, &msg, &args, &res, 0);
2634 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2636 struct nfs4_exception exception = { };
2640 err = nfs4_handle_exception(server,
2641 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2643 } while (exception.retry);
2647 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2649 nfs_fattr_init(fsinfo->fattr);
2650 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2653 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2654 struct nfs_pathconf *pathconf)
2656 struct nfs4_pathconf_arg args = {
2658 .bitmask = server->attr_bitmask,
2660 struct nfs4_pathconf_res res = {
2661 .pathconf = pathconf,
2663 struct rpc_message msg = {
2664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2669 /* None of the pathconf attributes are mandatory to implement */
2670 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2671 memset(pathconf, 0, sizeof(*pathconf));
2675 nfs_fattr_init(pathconf->fattr);
2676 return nfs4_call_sync(server, &msg, &args, &res, 0);
2679 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2680 struct nfs_pathconf *pathconf)
2682 struct nfs4_exception exception = { };
2686 err = nfs4_handle_exception(server,
2687 _nfs4_proc_pathconf(server, fhandle, pathconf),
2689 } while (exception.retry);
2693 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2695 struct nfs_server *server = NFS_SERVER(data->inode);
2697 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2698 rpc_restart_call(task);
2702 nfs_invalidate_atime(data->inode);
2703 if (task->tk_status > 0)
2704 renew_lease(server, data->timestamp);
2708 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2710 data->timestamp = jiffies;
2711 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2714 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2716 struct inode *inode = data->inode;
2718 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2719 rpc_restart_call(task);
2722 if (task->tk_status >= 0) {
2723 renew_lease(NFS_SERVER(inode), data->timestamp);
2724 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2729 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2731 struct nfs_server *server = NFS_SERVER(data->inode);
2733 data->args.bitmask = server->cache_consistency_bitmask;
2734 data->res.server = server;
2735 data->timestamp = jiffies;
2737 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2740 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2742 struct inode *inode = data->inode;
2744 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
2745 rpc_restart_call(task);
2748 nfs_refresh_inode(inode, data->res.fattr);
2752 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2754 struct nfs_server *server = NFS_SERVER(data->inode);
2756 data->args.bitmask = server->cache_consistency_bitmask;
2757 data->res.server = server;
2758 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2762 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2763 * standalone procedure for queueing an asynchronous RENEW.
2765 static void nfs4_renew_done(struct rpc_task *task, void *data)
2767 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2768 unsigned long timestamp = (unsigned long)data;
2770 if (task->tk_status < 0) {
2771 /* Unless we're shutting down, schedule state recovery! */
2772 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2773 nfs4_schedule_state_recovery(clp);
2776 spin_lock(&clp->cl_lock);
2777 if (time_before(clp->cl_last_renewal,timestamp))
2778 clp->cl_last_renewal = timestamp;
2779 spin_unlock(&clp->cl_lock);
2782 static const struct rpc_call_ops nfs4_renew_ops = {
2783 .rpc_call_done = nfs4_renew_done,
2786 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2788 struct rpc_message msg = {
2789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2794 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2795 &nfs4_renew_ops, (void *)jiffies);
2798 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2800 struct rpc_message msg = {
2801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2805 unsigned long now = jiffies;
2808 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2811 spin_lock(&clp->cl_lock);
2812 if (time_before(clp->cl_last_renewal,now))
2813 clp->cl_last_renewal = now;
2814 spin_unlock(&clp->cl_lock);
2818 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2820 return (server->caps & NFS_CAP_ACLS)
2821 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2822 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2825 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2826 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2829 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2831 static void buf_to_pages(const void *buf, size_t buflen,
2832 struct page **pages, unsigned int *pgbase)
2834 const void *p = buf;
2836 *pgbase = offset_in_page(buf);
2838 while (p < buf + buflen) {
2839 *(pages++) = virt_to_page(p);
2840 p += PAGE_CACHE_SIZE;
2844 struct nfs4_cached_acl {
2850 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2852 struct nfs_inode *nfsi = NFS_I(inode);
2854 spin_lock(&inode->i_lock);
2855 kfree(nfsi->nfs4_acl);
2856 nfsi->nfs4_acl = acl;
2857 spin_unlock(&inode->i_lock);
2860 static void nfs4_zap_acl_attr(struct inode *inode)
2862 nfs4_set_cached_acl(inode, NULL);
2865 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2867 struct nfs_inode *nfsi = NFS_I(inode);
2868 struct nfs4_cached_acl *acl;
2871 spin_lock(&inode->i_lock);
2872 acl = nfsi->nfs4_acl;
2875 if (buf == NULL) /* user is just asking for length */
2877 if (acl->cached == 0)
2879 ret = -ERANGE; /* see getxattr(2) man page */
2880 if (acl->len > buflen)
2882 memcpy(buf, acl->data, acl->len);
2886 spin_unlock(&inode->i_lock);
2890 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2892 struct nfs4_cached_acl *acl;
2894 if (buf && acl_len <= PAGE_SIZE) {
2895 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2899 memcpy(acl->data, buf, acl_len);
2901 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2908 nfs4_set_cached_acl(inode, acl);
2911 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2913 struct page *pages[NFS4ACL_MAXPAGES];
2914 struct nfs_getaclargs args = {
2915 .fh = NFS_FH(inode),
2919 struct nfs_getaclres res = {
2923 struct rpc_message msg = {
2924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2928 struct page *localpage = NULL;
2931 if (buflen < PAGE_SIZE) {
2932 /* As long as we're doing a round trip to the server anyway,
2933 * let's be prepared for a page of acl data. */
2934 localpage = alloc_page(GFP_KERNEL);
2935 resp_buf = page_address(localpage);
2936 if (localpage == NULL)
2938 args.acl_pages[0] = localpage;
2939 args.acl_pgbase = 0;
2940 args.acl_len = PAGE_SIZE;
2943 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2945 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2948 if (res.acl_len > args.acl_len)
2949 nfs4_write_cached_acl(inode, NULL, res.acl_len);
2951 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
2954 if (res.acl_len > buflen)
2957 memcpy(buf, resp_buf, res.acl_len);
2962 __free_page(localpage);
2966 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2968 struct nfs4_exception exception = { };
2971 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2974 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2975 } while (exception.retry);
2979 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2981 struct nfs_server *server = NFS_SERVER(inode);
2984 if (!nfs4_server_supports_acls(server))
2986 ret = nfs_revalidate_inode(server, inode);
2989 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
2990 nfs_zap_acl_cache(inode);
2991 ret = nfs4_read_cached_acl(inode, buf, buflen);
2994 return nfs4_get_acl_uncached(inode, buf, buflen);
2997 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2999 struct nfs_server *server = NFS_SERVER(inode);
3000 struct page *pages[NFS4ACL_MAXPAGES];
3001 struct nfs_setaclargs arg = {
3002 .fh = NFS_FH(inode),
3006 struct nfs_setaclres res;
3007 struct rpc_message msg = {
3008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3014 if (!nfs4_server_supports_acls(server))
3016 nfs_inode_return_delegation(inode);
3017 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3018 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3019 nfs_access_zap_cache(inode);
3020 nfs_zap_acl_cache(inode);
3024 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3026 struct nfs4_exception exception = { };
3029 err = nfs4_handle_exception(NFS_SERVER(inode),
3030 __nfs4_proc_set_acl(inode, buf, buflen),
3032 } while (exception.retry);
3037 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3039 struct nfs_client *clp = server->nfs_client;
3041 if (!clp || task->tk_status >= 0)
3043 switch(task->tk_status) {
3044 case -NFS4ERR_ADMIN_REVOKED:
3045 case -NFS4ERR_BAD_STATEID:
3046 case -NFS4ERR_OPENMODE:
3049 nfs4_state_mark_reclaim_nograce(clp, state);
3050 case -NFS4ERR_STALE_CLIENTID:
3051 case -NFS4ERR_STALE_STATEID:
3052 case -NFS4ERR_EXPIRED:
3053 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3054 nfs4_schedule_state_recovery(clp);
3055 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3056 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3057 task->tk_status = 0;
3059 case -NFS4ERR_DELAY:
3060 nfs_inc_server_stats(server, NFSIOS_DELAY);
3061 case -NFS4ERR_GRACE:
3062 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3063 task->tk_status = 0;
3065 case -NFS4ERR_OLD_STATEID:
3066 task->tk_status = 0;
3069 task->tk_status = nfs4_map_errors(task->tk_status);
3073 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3075 nfs4_verifier sc_verifier;
3076 struct nfs4_setclientid setclientid = {
3077 .sc_verifier = &sc_verifier,
3080 struct rpc_message msg = {
3081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3082 .rpc_argp = &setclientid,
3090 p = (__be32*)sc_verifier.data;
3091 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3092 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3095 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3096 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3098 rpc_peeraddr2str(clp->cl_rpcclient,
3100 rpc_peeraddr2str(clp->cl_rpcclient,
3102 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3103 clp->cl_id_uniquifier);
3104 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3105 sizeof(setclientid.sc_netid),
3106 rpc_peeraddr2str(clp->cl_rpcclient,
3107 RPC_DISPLAY_NETID));
3108 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3109 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3110 clp->cl_ipaddr, port >> 8, port & 255);
3112 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3113 if (status != -NFS4ERR_CLID_INUSE)
3118 ssleep(clp->cl_lease_time + 1);
3120 if (++clp->cl_id_uniquifier == 0)
3126 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3128 struct nfs_fsinfo fsinfo;
3129 struct rpc_message msg = {
3130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3132 .rpc_resp = &fsinfo,
3139 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3141 spin_lock(&clp->cl_lock);
3142 clp->cl_lease_time = fsinfo.lease_time * HZ;
3143 clp->cl_last_renewal = now;
3144 spin_unlock(&clp->cl_lock);
3149 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3154 err = _nfs4_proc_setclientid_confirm(clp, cred);
3158 case -NFS4ERR_RESOURCE:
3159 /* The IBM lawyers misread another document! */
3160 case -NFS4ERR_DELAY:
3161 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3167 struct nfs4_delegreturndata {
3168 struct nfs4_delegreturnargs args;
3169 struct nfs4_delegreturnres res;
3171 nfs4_stateid stateid;
3172 unsigned long timestamp;
3173 struct nfs_fattr fattr;
3177 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3179 struct nfs4_delegreturndata *data = calldata;
3180 data->rpc_status = task->tk_status;
3181 if (data->rpc_status == 0)
3182 renew_lease(data->res.server, data->timestamp);
3185 static void nfs4_delegreturn_release(void *calldata)
3190 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3191 .rpc_call_done = nfs4_delegreturn_done,
3192 .rpc_release = nfs4_delegreturn_release,
3195 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3197 struct nfs4_delegreturndata *data;
3198 struct nfs_server *server = NFS_SERVER(inode);
3199 struct rpc_task *task;
3200 struct rpc_message msg = {
3201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3204 struct rpc_task_setup task_setup_data = {
3205 .rpc_client = server->client,
3206 .rpc_message = &msg,
3207 .callback_ops = &nfs4_delegreturn_ops,
3208 .flags = RPC_TASK_ASYNC,
3212 data = kmalloc(sizeof(*data), GFP_KERNEL);
3215 data->args.fhandle = &data->fh;
3216 data->args.stateid = &data->stateid;
3217 data->args.bitmask = server->attr_bitmask;
3218 nfs_copy_fh(&data->fh, NFS_FH(inode));
3219 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3220 data->res.fattr = &data->fattr;
3221 data->res.server = server;
3222 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3223 nfs_fattr_init(data->res.fattr);
3224 data->timestamp = jiffies;
3225 data->rpc_status = 0;
3227 task_setup_data.callback_data = data;
3228 msg.rpc_argp = &data->args,
3229 msg.rpc_resp = &data->res,
3230 task = rpc_run_task(&task_setup_data);
3232 return PTR_ERR(task);
3235 status = nfs4_wait_for_completion_rpc_task(task);
3238 status = data->rpc_status;
3241 nfs_refresh_inode(inode, &data->fattr);
3247 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3249 struct nfs_server *server = NFS_SERVER(inode);
3250 struct nfs4_exception exception = { };
3253 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3255 case -NFS4ERR_STALE_STATEID:
3256 case -NFS4ERR_EXPIRED:
3260 err = nfs4_handle_exception(server, err, &exception);
3261 } while (exception.retry);
3265 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3266 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3269 * sleep, with exponential backoff, and retry the LOCK operation.
3271 static unsigned long
3272 nfs4_set_lock_task_retry(unsigned long timeout)
3274 schedule_timeout_killable(timeout);
3276 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3277 return NFS4_LOCK_MAXTIMEOUT;
3281 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3283 struct inode *inode = state->inode;
3284 struct nfs_server *server = NFS_SERVER(inode);
3285 struct nfs_client *clp = server->nfs_client;
3286 struct nfs_lockt_args arg = {
3287 .fh = NFS_FH(inode),
3290 struct nfs_lockt_res res = {
3293 struct rpc_message msg = {
3294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3297 .rpc_cred = state->owner->so_cred,
3299 struct nfs4_lock_state *lsp;
3302 arg.lock_owner.clientid = clp->cl_clientid;
3303 status = nfs4_set_lock_state(state, request);
3306 lsp = request->fl_u.nfs4_fl.owner;
3307 arg.lock_owner.id = lsp->ls_id.id;
3308 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3311 request->fl_type = F_UNLCK;
3313 case -NFS4ERR_DENIED:
3316 request->fl_ops->fl_release_private(request);
3321 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3323 struct nfs4_exception exception = { };
3327 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3328 _nfs4_proc_getlk(state, cmd, request),
3330 } while (exception.retry);
3334 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3337 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3339 res = posix_lock_file_wait(file, fl);
3342 res = flock_lock_file_wait(file, fl);
3350 struct nfs4_unlockdata {
3351 struct nfs_locku_args arg;
3352 struct nfs_locku_res res;
3353 struct nfs4_lock_state *lsp;
3354 struct nfs_open_context *ctx;
3355 struct file_lock fl;
3356 const struct nfs_server *server;
3357 unsigned long timestamp;
3360 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3361 struct nfs_open_context *ctx,
3362 struct nfs4_lock_state *lsp,
3363 struct nfs_seqid *seqid)
3365 struct nfs4_unlockdata *p;
3366 struct inode *inode = lsp->ls_state->inode;
3368 p = kmalloc(sizeof(*p), GFP_KERNEL);
3371 p->arg.fh = NFS_FH(inode);
3373 p->arg.seqid = seqid;
3374 p->res.seqid = seqid;
3375 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3376 p->arg.stateid = &lsp->ls_stateid;
3378 atomic_inc(&lsp->ls_count);
3379 /* Ensure we don't close file until we're done freeing locks! */
3380 p->ctx = get_nfs_open_context(ctx);
3381 memcpy(&p->fl, fl, sizeof(p->fl));
3382 p->server = NFS_SERVER(inode);
3386 static void nfs4_locku_release_calldata(void *data)
3388 struct nfs4_unlockdata *calldata = data;
3389 nfs_free_seqid(calldata->arg.seqid);
3390 nfs4_put_lock_state(calldata->lsp);
3391 put_nfs_open_context(calldata->ctx);
3395 static void nfs4_locku_done(struct rpc_task *task, void *data)
3397 struct nfs4_unlockdata *calldata = data;
3399 if (RPC_ASSASSINATED(task))
3401 switch (task->tk_status) {
3403 memcpy(calldata->lsp->ls_stateid.data,
3404 calldata->res.stateid.data,
3405 sizeof(calldata->lsp->ls_stateid.data));
3406 renew_lease(calldata->server, calldata->timestamp);
3408 case -NFS4ERR_BAD_STATEID:
3409 case -NFS4ERR_OLD_STATEID:
3410 case -NFS4ERR_STALE_STATEID:
3411 case -NFS4ERR_EXPIRED:
3414 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3415 rpc_restart_call(task);
3419 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3421 struct nfs4_unlockdata *calldata = data;
3423 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3425 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3426 /* Note: exit _without_ running nfs4_locku_done */
3427 task->tk_action = NULL;
3430 calldata->timestamp = jiffies;
3431 rpc_call_start(task);
3434 static const struct rpc_call_ops nfs4_locku_ops = {
3435 .rpc_call_prepare = nfs4_locku_prepare,
3436 .rpc_call_done = nfs4_locku_done,
3437 .rpc_release = nfs4_locku_release_calldata,
3440 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3441 struct nfs_open_context *ctx,
3442 struct nfs4_lock_state *lsp,
3443 struct nfs_seqid *seqid)
3445 struct nfs4_unlockdata *data;
3446 struct rpc_message msg = {
3447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3448 .rpc_cred = ctx->cred,
3450 struct rpc_task_setup task_setup_data = {
3451 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3452 .rpc_message = &msg,
3453 .callback_ops = &nfs4_locku_ops,
3454 .workqueue = nfsiod_workqueue,
3455 .flags = RPC_TASK_ASYNC,
3458 /* Ensure this is an unlock - when canceling a lock, the
3459 * canceled lock is passed in, and it won't be an unlock.
3461 fl->fl_type = F_UNLCK;
3463 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3465 nfs_free_seqid(seqid);
3466 return ERR_PTR(-ENOMEM);
3469 msg.rpc_argp = &data->arg,
3470 msg.rpc_resp = &data->res,
3471 task_setup_data.callback_data = data;
3472 return rpc_run_task(&task_setup_data);
3475 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3477 struct nfs_inode *nfsi = NFS_I(state->inode);
3478 struct nfs_seqid *seqid;
3479 struct nfs4_lock_state *lsp;
3480 struct rpc_task *task;
3482 unsigned char fl_flags = request->fl_flags;
3484 status = nfs4_set_lock_state(state, request);
3485 /* Unlock _before_ we do the RPC call */
3486 request->fl_flags |= FL_EXISTS;
3487 down_read(&nfsi->rwsem);
3488 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3489 up_read(&nfsi->rwsem);
3492 up_read(&nfsi->rwsem);
3495 /* Is this a delegated lock? */
3496 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3498 lsp = request->fl_u.nfs4_fl.owner;
3499 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3503 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3504 status = PTR_ERR(task);
3507 status = nfs4_wait_for_completion_rpc_task(task);
3510 request->fl_flags = fl_flags;
3514 struct nfs4_lockdata {
3515 struct nfs_lock_args arg;
3516 struct nfs_lock_res res;
3517 struct nfs4_lock_state *lsp;
3518 struct nfs_open_context *ctx;
3519 struct file_lock fl;
3520 unsigned long timestamp;
3525 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3526 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3528 struct nfs4_lockdata *p;
3529 struct inode *inode = lsp->ls_state->inode;
3530 struct nfs_server *server = NFS_SERVER(inode);
3532 p = kzalloc(sizeof(*p), GFP_KERNEL);
3536 p->arg.fh = NFS_FH(inode);
3538 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3539 if (p->arg.open_seqid == NULL)
3541 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3542 if (p->arg.lock_seqid == NULL)
3543 goto out_free_seqid;
3544 p->arg.lock_stateid = &lsp->ls_stateid;
3545 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3546 p->arg.lock_owner.id = lsp->ls_id.id;
3547 p->res.lock_seqid = p->arg.lock_seqid;
3548 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3550 atomic_inc(&lsp->ls_count);
3551 p->ctx = get_nfs_open_context(ctx);
3552 memcpy(&p->fl, fl, sizeof(p->fl));
3555 nfs_free_seqid(p->arg.open_seqid);
3561 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3563 struct nfs4_lockdata *data = calldata;
3564 struct nfs4_state *state = data->lsp->ls_state;
3566 dprintk("%s: begin!\n", __func__);
3567 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3569 /* Do we need to do an open_to_lock_owner? */
3570 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3571 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3573 data->arg.open_stateid = &state->stateid;
3574 data->arg.new_lock_owner = 1;
3575 data->res.open_seqid = data->arg.open_seqid;
3577 data->arg.new_lock_owner = 0;
3578 data->timestamp = jiffies;
3579 rpc_call_start(task);
3580 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3583 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3585 struct nfs4_lockdata *data = calldata;
3587 dprintk("%s: begin!\n", __func__);
3589 data->rpc_status = task->tk_status;
3590 if (RPC_ASSASSINATED(task))
3592 if (data->arg.new_lock_owner != 0) {
3593 if (data->rpc_status == 0)
3594 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3598 if (data->rpc_status == 0) {
3599 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3600 sizeof(data->lsp->ls_stateid.data));
3601 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3602 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3605 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3608 static void nfs4_lock_release(void *calldata)
3610 struct nfs4_lockdata *data = calldata;
3612 dprintk("%s: begin!\n", __func__);
3613 nfs_free_seqid(data->arg.open_seqid);
3614 if (data->cancelled != 0) {
3615 struct rpc_task *task;
3616 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3617 data->arg.lock_seqid);
3620 dprintk("%s: cancelling lock!\n", __func__);
3622 nfs_free_seqid(data->arg.lock_seqid);
3623 nfs4_put_lock_state(data->lsp);
3624 put_nfs_open_context(data->ctx);
3626 dprintk("%s: done!\n", __func__);
3629 static const struct rpc_call_ops nfs4_lock_ops = {
3630 .rpc_call_prepare = nfs4_lock_prepare,
3631 .rpc_call_done = nfs4_lock_done,
3632 .rpc_release = nfs4_lock_release,
3635 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3637 struct nfs4_lockdata *data;
3638 struct rpc_task *task;
3639 struct rpc_message msg = {
3640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3641 .rpc_cred = state->owner->so_cred,
3643 struct rpc_task_setup task_setup_data = {
3644 .rpc_client = NFS_CLIENT(state->inode),
3645 .rpc_message = &msg,
3646 .callback_ops = &nfs4_lock_ops,
3647 .workqueue = nfsiod_workqueue,
3648 .flags = RPC_TASK_ASYNC,
3652 dprintk("%s: begin!\n", __func__);
3653 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3654 fl->fl_u.nfs4_fl.owner);
3658 data->arg.block = 1;
3660 data->arg.reclaim = 1;
3661 msg.rpc_argp = &data->arg,
3662 msg.rpc_resp = &data->res,
3663 task_setup_data.callback_data = data;
3664 task = rpc_run_task(&task_setup_data);
3666 return PTR_ERR(task);
3667 ret = nfs4_wait_for_completion_rpc_task(task);
3669 ret = data->rpc_status;
3670 if (ret == -NFS4ERR_DENIED)
3673 data->cancelled = 1;
3675 dprintk("%s: done, ret = %d!\n", __func__, ret);
3679 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3681 struct nfs_server *server = NFS_SERVER(state->inode);
3682 struct nfs4_exception exception = { };
3686 /* Cache the lock if possible... */
3687 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3689 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3690 if (err != -NFS4ERR_DELAY)
3692 nfs4_handle_exception(server, err, &exception);
3693 } while (exception.retry);
3697 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3699 struct nfs_server *server = NFS_SERVER(state->inode);
3700 struct nfs4_exception exception = { };
3703 err = nfs4_set_lock_state(state, request);
3707 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3709 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3710 if (err != -NFS4ERR_DELAY)
3712 nfs4_handle_exception(server, err, &exception);
3713 } while (exception.retry);
3717 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3719 struct nfs_inode *nfsi = NFS_I(state->inode);
3720 unsigned char fl_flags = request->fl_flags;
3723 /* Is this a delegated open? */
3724 status = nfs4_set_lock_state(state, request);
3727 request->fl_flags |= FL_ACCESS;
3728 status = do_vfs_lock(request->fl_file, request);
3731 down_read(&nfsi->rwsem);
3732 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3733 /* Yes: cache locks! */
3734 /* ...but avoid races with delegation recall... */
3735 request->fl_flags = fl_flags & ~FL_SLEEP;
3736 status = do_vfs_lock(request->fl_file, request);
3739 status = _nfs4_do_setlk(state, cmd, request, 0);
3742 /* Note: we always want to sleep here! */
3743 request->fl_flags = fl_flags | FL_SLEEP;
3744 if (do_vfs_lock(request->fl_file, request) < 0)
3745 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
3747 up_read(&nfsi->rwsem);
3749 request->fl_flags = fl_flags;
3753 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3755 struct nfs4_exception exception = { };
3759 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3760 _nfs4_proc_setlk(state, cmd, request),
3762 } while (exception.retry);
3767 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3769 struct nfs_open_context *ctx;
3770 struct nfs4_state *state;
3771 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3774 /* verify open state */
3775 ctx = nfs_file_open_context(filp);
3778 if (request->fl_start < 0 || request->fl_end < 0)
3782 return nfs4_proc_getlk(state, F_GETLK, request);
3784 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3787 if (request->fl_type == F_UNLCK)
3788 return nfs4_proc_unlck(state, cmd, request);
3791 status = nfs4_proc_setlk(state, cmd, request);
3792 if ((status != -EAGAIN) || IS_SETLK(cmd))
3794 timeout = nfs4_set_lock_task_retry(timeout);
3795 status = -ERESTARTSYS;
3798 } while(status < 0);
3802 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3804 struct nfs_server *server = NFS_SERVER(state->inode);
3805 struct nfs4_exception exception = { };
3808 err = nfs4_set_lock_state(state, fl);
3812 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3813 if (err != -NFS4ERR_DELAY)
3815 err = nfs4_handle_exception(server, err, &exception);
3816 } while (exception.retry);
3821 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3823 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3824 size_t buflen, int flags)
3826 struct inode *inode = dentry->d_inode;
3828 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3831 return nfs4_proc_set_acl(inode, buf, buflen);
3834 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3835 * and that's what we'll do for e.g. user attributes that haven't been set.
3836 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3837 * attributes in kernel-managed attribute namespaces. */
3838 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3841 struct inode *inode = dentry->d_inode;
3843 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3846 return nfs4_proc_get_acl(inode, buf, buflen);
3849 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3851 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3853 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3855 if (buf && buflen < len)
3858 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3862 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3864 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3865 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3866 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3869 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3870 NFS_ATTR_FATTR_NLINK;
3871 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3875 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
3876 struct nfs4_fs_locations *fs_locations, struct page *page)
3878 struct nfs_server *server = NFS_SERVER(dir);
3880 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3881 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3883 struct nfs4_fs_locations_arg args = {
3884 .dir_fh = NFS_FH(dir),
3889 struct nfs4_fs_locations_res res = {
3890 .fs_locations = fs_locations,
3892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3899 dprintk("%s: start\n", __func__);
3900 nfs_fattr_init(&fs_locations->fattr);
3901 fs_locations->server = server;
3902 fs_locations->nlocations = 0;
3903 status = nfs4_call_sync(server, &msg, &args, &res, 0);
3904 nfs_fixup_referral_attributes(&fs_locations->fattr);
3905 dprintk("%s: returned status = %d\n", __func__, status);
3909 #ifdef CONFIG_NFS_V4_1
3910 /* Destroy the slot table */
3911 static void nfs4_destroy_slot_table(struct nfs4_session *session)
3913 if (session->fc_slot_table.slots == NULL)
3915 kfree(session->fc_slot_table.slots);
3916 session->fc_slot_table.slots = NULL;
3920 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
3922 struct nfs4_session *session;
3923 struct nfs4_slot_table *tbl;
3925 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
3928 tbl = &session->fc_slot_table;
3929 spin_lock_init(&tbl->slot_tbl_lock);
3930 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
3935 void nfs4_destroy_session(struct nfs4_session *session)
3937 nfs4_destroy_slot_table(session);
3941 #endif /* CONFIG_NFS_V4_1 */
3943 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3944 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
3945 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
3946 .recover_open = nfs4_open_reclaim,
3947 .recover_lock = nfs4_lock_reclaim,
3950 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
3951 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
3952 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
3953 .recover_open = nfs4_open_expired,
3954 .recover_lock = nfs4_lock_expired,
3957 static const struct inode_operations nfs4_file_inode_operations = {
3958 .permission = nfs_permission,
3959 .getattr = nfs_getattr,
3960 .setattr = nfs_setattr,
3961 .getxattr = nfs4_getxattr,
3962 .setxattr = nfs4_setxattr,
3963 .listxattr = nfs4_listxattr,
3966 const struct nfs_rpc_ops nfs_v4_clientops = {
3967 .version = 4, /* protocol version */
3968 .dentry_ops = &nfs4_dentry_operations,
3969 .dir_inode_ops = &nfs4_dir_inode_operations,
3970 .file_inode_ops = &nfs4_file_inode_operations,
3971 .getroot = nfs4_proc_get_root,
3972 .getattr = nfs4_proc_getattr,
3973 .setattr = nfs4_proc_setattr,
3974 .lookupfh = nfs4_proc_lookupfh,
3975 .lookup = nfs4_proc_lookup,
3976 .access = nfs4_proc_access,
3977 .readlink = nfs4_proc_readlink,
3978 .create = nfs4_proc_create,
3979 .remove = nfs4_proc_remove,
3980 .unlink_setup = nfs4_proc_unlink_setup,
3981 .unlink_done = nfs4_proc_unlink_done,
3982 .rename = nfs4_proc_rename,
3983 .link = nfs4_proc_link,
3984 .symlink = nfs4_proc_symlink,
3985 .mkdir = nfs4_proc_mkdir,
3986 .rmdir = nfs4_proc_remove,
3987 .readdir = nfs4_proc_readdir,
3988 .mknod = nfs4_proc_mknod,
3989 .statfs = nfs4_proc_statfs,
3990 .fsinfo = nfs4_proc_fsinfo,
3991 .pathconf = nfs4_proc_pathconf,
3992 .set_capabilities = nfs4_server_capabilities,
3993 .decode_dirent = nfs4_decode_dirent,
3994 .read_setup = nfs4_proc_read_setup,
3995 .read_done = nfs4_read_done,
3996 .write_setup = nfs4_proc_write_setup,
3997 .write_done = nfs4_write_done,
3998 .commit_setup = nfs4_proc_commit_setup,
3999 .commit_done = nfs4_commit_done,
4000 .lock = nfs4_proc_lock,
4001 .clear_acl_cache = nfs4_zap_acl_attr,
4002 .close_context = nfs4_close_context,