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 *);
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 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_bit_killable(void *word)
198 if (fatal_signal_pending(current))
204 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
211 nfs4_wait_bit_killable, TASK_KILLABLE);
215 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
222 *timeout = NFS4_POLL_RETRY_MIN;
223 if (*timeout > NFS4_POLL_RETRY_MAX)
224 *timeout = NFS4_POLL_RETRY_MAX;
225 schedule_timeout_killable(*timeout);
226 if (fatal_signal_pending(current))
232 /* This is the error handling routine for processes that are allowed
235 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
237 struct nfs_client *clp = server->nfs_client;
240 exception->retry = 0;
244 case -NFS4ERR_STALE_CLIENTID:
245 case -NFS4ERR_STALE_STATEID:
246 case -NFS4ERR_EXPIRED:
247 nfs4_schedule_state_recovery(clp);
248 ret = nfs4_wait_clnt_recover(clp);
250 exception->retry = 1;
252 case -NFS4ERR_FILE_OPEN:
255 ret = nfs4_delay(server->client, &exception->timeout);
258 case -NFS4ERR_OLD_STATEID:
259 exception->retry = 1;
261 /* We failed to handle the error */
262 return nfs4_map_errors(ret);
266 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
268 struct nfs_client *clp = server->nfs_client;
269 spin_lock(&clp->cl_lock);
270 if (time_before(clp->cl_last_renewal,timestamp))
271 clp->cl_last_renewal = timestamp;
272 spin_unlock(&clp->cl_lock);
275 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
277 struct nfs_inode *nfsi = NFS_I(dir);
279 spin_lock(&dir->i_lock);
280 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
281 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
282 nfs_force_lookup_revalidate(dir);
283 nfsi->change_attr = cinfo->after;
284 spin_unlock(&dir->i_lock);
287 struct nfs4_opendata {
289 struct nfs_openargs o_arg;
290 struct nfs_openres o_res;
291 struct nfs_open_confirmargs c_arg;
292 struct nfs_open_confirmres c_res;
293 struct nfs_fattr f_attr;
294 struct nfs_fattr dir_attr;
297 struct nfs4_state_owner *owner;
298 struct nfs4_state *state;
300 unsigned long timestamp;
301 unsigned int rpc_done : 1;
307 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
309 p->o_res.f_attr = &p->f_attr;
310 p->o_res.dir_attr = &p->dir_attr;
311 p->o_res.seqid = p->o_arg.seqid;
312 p->c_res.seqid = p->c_arg.seqid;
313 p->o_res.server = p->o_arg.server;
314 nfs_fattr_init(&p->f_attr);
315 nfs_fattr_init(&p->dir_attr);
318 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
319 struct nfs4_state_owner *sp, int flags,
320 const struct iattr *attrs)
322 struct dentry *parent = dget_parent(path->dentry);
323 struct inode *dir = parent->d_inode;
324 struct nfs_server *server = NFS_SERVER(dir);
325 struct nfs4_opendata *p;
327 p = kzalloc(sizeof(*p), GFP_KERNEL);
330 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
331 if (p->o_arg.seqid == NULL)
333 p->path.mnt = mntget(path->mnt);
334 p->path.dentry = dget(path->dentry);
337 atomic_inc(&sp->so_count);
338 p->o_arg.fh = NFS_FH(dir);
339 p->o_arg.open_flags = flags,
340 p->o_arg.clientid = server->nfs_client->cl_clientid;
341 p->o_arg.id = sp->so_owner_id.id;
342 p->o_arg.name = &p->path.dentry->d_name;
343 p->o_arg.server = server;
344 p->o_arg.bitmask = server->attr_bitmask;
345 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
346 if (flags & O_EXCL) {
347 u32 *s = (u32 *) p->o_arg.u.verifier.data;
350 } else if (flags & O_CREAT) {
351 p->o_arg.u.attrs = &p->attrs;
352 memcpy(&p->attrs, attrs, sizeof(p->attrs));
354 p->c_arg.fh = &p->o_res.fh;
355 p->c_arg.stateid = &p->o_res.stateid;
356 p->c_arg.seqid = p->o_arg.seqid;
357 nfs4_init_opendata_res(p);
367 static void nfs4_opendata_free(struct kref *kref)
369 struct nfs4_opendata *p = container_of(kref,
370 struct nfs4_opendata, kref);
372 nfs_free_seqid(p->o_arg.seqid);
373 if (p->state != NULL)
374 nfs4_put_open_state(p->state);
375 nfs4_put_state_owner(p->owner);
381 static void nfs4_opendata_put(struct nfs4_opendata *p)
384 kref_put(&p->kref, nfs4_opendata_free);
387 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
391 ret = rpc_wait_for_completion_task(task);
395 static int can_open_cached(struct nfs4_state *state, int mode)
398 switch (mode & (FMODE_READ|FMODE_WRITE|O_EXCL)) {
400 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
403 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
405 case FMODE_READ|FMODE_WRITE:
406 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
411 static int can_open_delegated(struct nfs_delegation *delegation, mode_t open_flags)
413 if ((delegation->type & open_flags) != open_flags)
415 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
420 static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
422 switch (open_flags) {
429 case FMODE_READ|FMODE_WRITE:
432 nfs4_state_set_mode_locked(state, state->state | open_flags);
435 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
437 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
438 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
439 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
440 switch (open_flags) {
442 set_bit(NFS_O_RDONLY_STATE, &state->flags);
445 set_bit(NFS_O_WRONLY_STATE, &state->flags);
447 case FMODE_READ|FMODE_WRITE:
448 set_bit(NFS_O_RDWR_STATE, &state->flags);
452 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
454 write_seqlock(&state->seqlock);
455 nfs_set_open_stateid_locked(state, stateid, open_flags);
456 write_sequnlock(&state->seqlock);
459 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, int open_flags)
462 * Protect the call to nfs4_state_set_mode_locked and
463 * serialise the stateid update
465 write_seqlock(&state->seqlock);
466 if (deleg_stateid != NULL) {
467 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
468 set_bit(NFS_DELEGATED_STATE, &state->flags);
470 if (open_stateid != NULL)
471 nfs_set_open_stateid_locked(state, open_stateid, open_flags);
472 write_sequnlock(&state->seqlock);
473 spin_lock(&state->owner->so_lock);
474 update_open_stateflags(state, open_flags);
475 spin_unlock(&state->owner->so_lock);
478 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, int open_flags)
480 struct nfs_inode *nfsi = NFS_I(state->inode);
481 struct nfs_delegation *deleg_cur;
484 open_flags &= (FMODE_READ|FMODE_WRITE);
487 deleg_cur = rcu_dereference(nfsi->delegation);
488 if (deleg_cur == NULL)
491 spin_lock(&deleg_cur->lock);
492 if (nfsi->delegation != deleg_cur ||
493 (deleg_cur->type & open_flags) != open_flags)
494 goto no_delegation_unlock;
496 if (delegation == NULL)
497 delegation = &deleg_cur->stateid;
498 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
499 goto no_delegation_unlock;
501 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, open_flags);
503 no_delegation_unlock:
504 spin_unlock(&deleg_cur->lock);
508 if (!ret && open_stateid != NULL) {
509 __update_open_stateid(state, open_stateid, NULL, open_flags);
517 static void nfs4_return_incompatible_delegation(struct inode *inode, mode_t open_flags)
519 struct nfs_delegation *delegation;
522 delegation = rcu_dereference(NFS_I(inode)->delegation);
523 if (delegation == NULL || (delegation->type & open_flags) == open_flags) {
528 nfs_inode_return_delegation(inode);
531 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
533 struct nfs4_state *state = opendata->state;
534 struct nfs_inode *nfsi = NFS_I(state->inode);
535 struct nfs_delegation *delegation;
536 int open_mode = opendata->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL);
537 nfs4_stateid stateid;
541 if (can_open_cached(state, open_mode)) {
542 spin_lock(&state->owner->so_lock);
543 if (can_open_cached(state, open_mode)) {
544 update_open_stateflags(state, open_mode);
545 spin_unlock(&state->owner->so_lock);
546 goto out_return_state;
548 spin_unlock(&state->owner->so_lock);
551 delegation = rcu_dereference(nfsi->delegation);
552 if (delegation == NULL ||
553 !can_open_delegated(delegation, open_mode)) {
557 /* Save the delegation */
558 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
560 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
565 /* Try to update the stateid using the delegation */
566 if (update_open_stateid(state, NULL, &stateid, open_mode))
567 goto out_return_state;
572 atomic_inc(&state->count);
576 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
579 struct nfs4_state *state = NULL;
580 struct nfs_delegation *delegation;
583 if (!data->rpc_done) {
584 state = nfs4_try_open_cached(data);
589 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
591 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
592 ret = PTR_ERR(inode);
596 state = nfs4_get_open_state(inode, data->owner);
599 if (data->o_res.delegation_type != 0) {
600 int delegation_flags = 0;
603 delegation = rcu_dereference(NFS_I(inode)->delegation);
605 delegation_flags = delegation->flags;
607 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
608 nfs_inode_set_delegation(state->inode,
609 data->owner->so_cred,
612 nfs_inode_reclaim_delegation(state->inode,
613 data->owner->so_cred,
617 update_open_stateid(state, &data->o_res.stateid, NULL,
618 data->o_arg.open_flags);
628 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
630 struct nfs_inode *nfsi = NFS_I(state->inode);
631 struct nfs_open_context *ctx;
633 spin_lock(&state->inode->i_lock);
634 list_for_each_entry(ctx, &nfsi->open_files, list) {
635 if (ctx->state != state)
637 get_nfs_open_context(ctx);
638 spin_unlock(&state->inode->i_lock);
641 spin_unlock(&state->inode->i_lock);
642 return ERR_PTR(-ENOENT);
645 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
647 struct nfs4_opendata *opendata;
649 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
650 if (opendata == NULL)
651 return ERR_PTR(-ENOMEM);
652 opendata->state = state;
653 atomic_inc(&state->count);
657 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
659 struct nfs4_state *newstate;
662 opendata->o_arg.open_flags = openflags;
663 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
664 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
665 nfs4_init_opendata_res(opendata);
666 ret = _nfs4_proc_open(opendata);
669 newstate = nfs4_opendata_to_nfs4_state(opendata);
670 if (IS_ERR(newstate))
671 return PTR_ERR(newstate);
672 nfs4_close_state(&opendata->path, newstate, openflags);
677 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
679 struct nfs4_state *newstate;
682 /* memory barrier prior to reading state->n_* */
683 clear_bit(NFS_DELEGATED_STATE, &state->flags);
685 if (state->n_rdwr != 0) {
686 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
689 if (newstate != state)
692 if (state->n_wronly != 0) {
693 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
696 if (newstate != state)
699 if (state->n_rdonly != 0) {
700 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
703 if (newstate != state)
707 * We may have performed cached opens for all three recoveries.
708 * Check if we need to update the current stateid.
710 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
711 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
712 write_seqlock(&state->seqlock);
713 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
714 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
715 write_sequnlock(&state->seqlock);
722 * reclaim state on the server after a reboot.
724 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
726 struct nfs_delegation *delegation;
727 struct nfs4_opendata *opendata;
728 int delegation_type = 0;
731 opendata = nfs4_open_recoverdata_alloc(ctx, state);
732 if (IS_ERR(opendata))
733 return PTR_ERR(opendata);
734 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
735 opendata->o_arg.fh = NFS_FH(state->inode);
737 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
738 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
739 delegation_type = delegation->type;
741 opendata->o_arg.u.delegation_type = delegation_type;
742 status = nfs4_open_recover(opendata, state);
743 nfs4_opendata_put(opendata);
747 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
749 struct nfs_server *server = NFS_SERVER(state->inode);
750 struct nfs4_exception exception = { };
753 err = _nfs4_do_open_reclaim(ctx, state);
754 if (err != -NFS4ERR_DELAY)
756 nfs4_handle_exception(server, err, &exception);
757 } while (exception.retry);
761 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
763 struct nfs_open_context *ctx;
766 ctx = nfs4_state_find_open_context(state);
769 ret = nfs4_do_open_reclaim(ctx, state);
770 put_nfs_open_context(ctx);
774 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
776 struct nfs4_opendata *opendata;
779 opendata = nfs4_open_recoverdata_alloc(ctx, state);
780 if (IS_ERR(opendata))
781 return PTR_ERR(opendata);
782 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
783 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
784 sizeof(opendata->o_arg.u.delegation.data));
785 ret = nfs4_open_recover(opendata, state);
786 nfs4_opendata_put(opendata);
790 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
792 struct nfs4_exception exception = { };
793 struct nfs_server *server = NFS_SERVER(state->inode);
796 err = _nfs4_open_delegation_recall(ctx, state, stateid);
800 case -NFS4ERR_STALE_CLIENTID:
801 case -NFS4ERR_STALE_STATEID:
802 case -NFS4ERR_EXPIRED:
803 /* Don't recall a delegation if it was lost */
804 nfs4_schedule_state_recovery(server->nfs_client);
807 err = nfs4_handle_exception(server, err, &exception);
808 } while (exception.retry);
812 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
814 struct nfs4_opendata *data = calldata;
816 data->rpc_status = task->tk_status;
817 if (RPC_ASSASSINATED(task))
819 if (data->rpc_status == 0) {
820 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
821 sizeof(data->o_res.stateid.data));
822 nfs_confirm_seqid(&data->owner->so_seqid, 0);
823 renew_lease(data->o_res.server, data->timestamp);
828 static void nfs4_open_confirm_release(void *calldata)
830 struct nfs4_opendata *data = calldata;
831 struct nfs4_state *state = NULL;
833 /* If this request hasn't been cancelled, do nothing */
834 if (data->cancelled == 0)
836 /* In case of error, no cleanup! */
839 state = nfs4_opendata_to_nfs4_state(data);
841 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
843 nfs4_opendata_put(data);
846 static const struct rpc_call_ops nfs4_open_confirm_ops = {
847 .rpc_call_done = nfs4_open_confirm_done,
848 .rpc_release = nfs4_open_confirm_release,
852 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
854 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
856 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
857 struct rpc_task *task;
858 struct rpc_message msg = {
859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
860 .rpc_argp = &data->c_arg,
861 .rpc_resp = &data->c_res,
862 .rpc_cred = data->owner->so_cred,
864 struct rpc_task_setup task_setup_data = {
865 .rpc_client = server->client,
867 .callback_ops = &nfs4_open_confirm_ops,
868 .callback_data = data,
869 .workqueue = nfsiod_workqueue,
870 .flags = RPC_TASK_ASYNC,
874 kref_get(&data->kref);
876 data->rpc_status = 0;
877 data->timestamp = jiffies;
878 task = rpc_run_task(&task_setup_data);
880 return PTR_ERR(task);
881 status = nfs4_wait_for_completion_rpc_task(task);
886 status = data->rpc_status;
891 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
893 struct nfs4_opendata *data = calldata;
894 struct nfs4_state_owner *sp = data->owner;
896 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
899 * Check if we still need to send an OPEN call, or if we can use
900 * a delegation instead.
902 if (data->state != NULL) {
903 struct nfs_delegation *delegation;
905 if (can_open_cached(data->state, data->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL)))
908 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
909 if (delegation != NULL &&
910 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
916 /* Update sequence id. */
917 data->o_arg.id = sp->so_owner_id.id;
918 data->o_arg.clientid = sp->so_client->cl_clientid;
919 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
920 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
921 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
923 data->timestamp = jiffies;
924 rpc_call_start(task);
927 task->tk_action = NULL;
931 static void nfs4_open_done(struct rpc_task *task, void *calldata)
933 struct nfs4_opendata *data = calldata;
935 data->rpc_status = task->tk_status;
936 if (RPC_ASSASSINATED(task))
938 if (task->tk_status == 0) {
939 switch (data->o_res.f_attr->mode & S_IFMT) {
943 data->rpc_status = -ELOOP;
946 data->rpc_status = -EISDIR;
949 data->rpc_status = -ENOTDIR;
951 renew_lease(data->o_res.server, data->timestamp);
952 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
953 nfs_confirm_seqid(&data->owner->so_seqid, 0);
958 static void nfs4_open_release(void *calldata)
960 struct nfs4_opendata *data = calldata;
961 struct nfs4_state *state = NULL;
963 /* If this request hasn't been cancelled, do nothing */
964 if (data->cancelled == 0)
966 /* In case of error, no cleanup! */
967 if (data->rpc_status != 0 || !data->rpc_done)
969 /* In case we need an open_confirm, no cleanup! */
970 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
972 state = nfs4_opendata_to_nfs4_state(data);
974 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
976 nfs4_opendata_put(data);
979 static const struct rpc_call_ops nfs4_open_ops = {
980 .rpc_call_prepare = nfs4_open_prepare,
981 .rpc_call_done = nfs4_open_done,
982 .rpc_release = nfs4_open_release,
986 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
988 static int _nfs4_proc_open(struct nfs4_opendata *data)
990 struct inode *dir = data->dir->d_inode;
991 struct nfs_server *server = NFS_SERVER(dir);
992 struct nfs_openargs *o_arg = &data->o_arg;
993 struct nfs_openres *o_res = &data->o_res;
994 struct rpc_task *task;
995 struct rpc_message msg = {
996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
999 .rpc_cred = data->owner->so_cred,
1001 struct rpc_task_setup task_setup_data = {
1002 .rpc_client = server->client,
1003 .rpc_message = &msg,
1004 .callback_ops = &nfs4_open_ops,
1005 .callback_data = data,
1006 .workqueue = nfsiod_workqueue,
1007 .flags = RPC_TASK_ASYNC,
1011 kref_get(&data->kref);
1013 data->rpc_status = 0;
1014 data->cancelled = 0;
1015 task = rpc_run_task(&task_setup_data);
1017 return PTR_ERR(task);
1018 status = nfs4_wait_for_completion_rpc_task(task);
1020 data->cancelled = 1;
1023 status = data->rpc_status;
1025 if (status != 0 || !data->rpc_done)
1028 if (o_res->fh.size == 0)
1029 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1031 if (o_arg->open_flags & O_CREAT) {
1032 update_changeattr(dir, &o_res->cinfo);
1033 nfs_post_op_update_inode(dir, o_res->dir_attr);
1035 nfs_refresh_inode(dir, o_res->dir_attr);
1036 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1037 status = _nfs4_proc_open_confirm(data);
1041 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1042 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1046 static int nfs4_recover_expired_lease(struct nfs_server *server)
1048 struct nfs_client *clp = server->nfs_client;
1052 ret = nfs4_wait_clnt_recover(clp);
1055 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1056 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1058 nfs4_schedule_state_recovery(clp);
1065 * reclaim state on the server after a network partition.
1066 * Assumes caller holds the appropriate lock
1068 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1070 struct nfs4_opendata *opendata;
1073 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1074 if (IS_ERR(opendata))
1075 return PTR_ERR(opendata);
1076 ret = nfs4_open_recover(opendata, state);
1078 d_drop(ctx->path.dentry);
1079 nfs4_opendata_put(opendata);
1083 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1085 struct nfs_server *server = NFS_SERVER(state->inode);
1086 struct nfs4_exception exception = { };
1090 err = _nfs4_open_expired(ctx, state);
1091 if (err == -NFS4ERR_DELAY)
1092 nfs4_handle_exception(server, err, &exception);
1093 } while (exception.retry);
1097 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1099 struct nfs_open_context *ctx;
1102 ctx = nfs4_state_find_open_context(state);
1104 return PTR_ERR(ctx);
1105 ret = nfs4_do_open_expired(ctx, state);
1106 put_nfs_open_context(ctx);
1111 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1112 * fields corresponding to attributes that were used to store the verifier.
1113 * Make sure we clobber those fields in the later setattr call
1115 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1117 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1118 !(sattr->ia_valid & ATTR_ATIME_SET))
1119 sattr->ia_valid |= ATTR_ATIME;
1121 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1122 !(sattr->ia_valid & ATTR_MTIME_SET))
1123 sattr->ia_valid |= ATTR_MTIME;
1127 * Returns a referenced nfs4_state
1129 static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1131 struct nfs4_state_owner *sp;
1132 struct nfs4_state *state = NULL;
1133 struct nfs_server *server = NFS_SERVER(dir);
1134 struct nfs4_opendata *opendata;
1137 /* Protect against reboot recovery conflicts */
1139 if (!(sp = nfs4_get_state_owner(server, cred))) {
1140 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1143 status = nfs4_recover_expired_lease(server);
1145 goto err_put_state_owner;
1146 if (path->dentry->d_inode != NULL)
1147 nfs4_return_incompatible_delegation(path->dentry->d_inode, flags & (FMODE_READ|FMODE_WRITE));
1149 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
1150 if (opendata == NULL)
1151 goto err_put_state_owner;
1153 if (path->dentry->d_inode != NULL)
1154 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1156 status = _nfs4_proc_open(opendata);
1158 goto err_opendata_put;
1160 if (opendata->o_arg.open_flags & O_EXCL)
1161 nfs4_exclusive_attrset(opendata, sattr);
1163 state = nfs4_opendata_to_nfs4_state(opendata);
1164 status = PTR_ERR(state);
1166 goto err_opendata_put;
1167 nfs4_opendata_put(opendata);
1168 nfs4_put_state_owner(sp);
1172 nfs4_opendata_put(opendata);
1173 err_put_state_owner:
1174 nfs4_put_state_owner(sp);
1181 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
1183 struct nfs4_exception exception = { };
1184 struct nfs4_state *res;
1188 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
1191 /* NOTE: BAD_SEQID means the server and client disagree about the
1192 * book-keeping w.r.t. state-changing operations
1193 * (OPEN/CLOSE/LOCK/LOCKU...)
1194 * It is actually a sign of a bug on the client or on the server.
1196 * If we receive a BAD_SEQID error in the particular case of
1197 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1198 * have unhashed the old state_owner for us, and that we can
1199 * therefore safely retry using a new one. We should still warn
1200 * the user though...
1202 if (status == -NFS4ERR_BAD_SEQID) {
1203 printk(KERN_WARNING "NFS: v4 server %s "
1204 " returned a bad sequence-id error!\n",
1205 NFS_SERVER(dir)->nfs_client->cl_hostname);
1206 exception.retry = 1;
1210 * BAD_STATEID on OPEN means that the server cancelled our
1211 * state before it received the OPEN_CONFIRM.
1212 * Recover by retrying the request as per the discussion
1213 * on Page 181 of RFC3530.
1215 if (status == -NFS4ERR_BAD_STATEID) {
1216 exception.retry = 1;
1219 if (status == -EAGAIN) {
1220 /* We must have found a delegation */
1221 exception.retry = 1;
1224 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1225 status, &exception));
1226 } while (exception.retry);
1230 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1231 struct nfs_fattr *fattr, struct iattr *sattr,
1232 struct nfs4_state *state)
1234 struct nfs_server *server = NFS_SERVER(inode);
1235 struct nfs_setattrargs arg = {
1236 .fh = NFS_FH(inode),
1239 .bitmask = server->attr_bitmask,
1241 struct nfs_setattrres res = {
1245 struct rpc_message msg = {
1246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1251 unsigned long timestamp = jiffies;
1254 nfs_fattr_init(fattr);
1256 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1257 /* Use that stateid */
1258 } else if (state != NULL) {
1259 nfs4_copy_stateid(&arg.stateid, state, current->files);
1261 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1263 status = rpc_call_sync(server->client, &msg, 0);
1264 if (status == 0 && state != NULL)
1265 renew_lease(server, timestamp);
1269 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1270 struct nfs_fattr *fattr, struct iattr *sattr,
1271 struct nfs4_state *state)
1273 struct nfs_server *server = NFS_SERVER(inode);
1274 struct nfs4_exception exception = { };
1277 err = nfs4_handle_exception(server,
1278 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1280 } while (exception.retry);
1284 struct nfs4_closedata {
1286 struct inode *inode;
1287 struct nfs4_state *state;
1288 struct nfs_closeargs arg;
1289 struct nfs_closeres res;
1290 struct nfs_fattr fattr;
1291 unsigned long timestamp;
1294 static void nfs4_free_closedata(void *data)
1296 struct nfs4_closedata *calldata = data;
1297 struct nfs4_state_owner *sp = calldata->state->owner;
1299 nfs4_put_open_state(calldata->state);
1300 nfs_free_seqid(calldata->arg.seqid);
1301 nfs4_put_state_owner(sp);
1302 path_put(&calldata->path);
1306 static void nfs4_close_done(struct rpc_task *task, void *data)
1308 struct nfs4_closedata *calldata = data;
1309 struct nfs4_state *state = calldata->state;
1310 struct nfs_server *server = NFS_SERVER(calldata->inode);
1312 if (RPC_ASSASSINATED(task))
1314 /* hmm. we are done with the inode, and in the process of freeing
1315 * the state_owner. we keep this around to process errors
1317 switch (task->tk_status) {
1319 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1320 renew_lease(server, calldata->timestamp);
1322 case -NFS4ERR_STALE_STATEID:
1323 case -NFS4ERR_EXPIRED:
1326 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1327 rpc_restart_call(task);
1331 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1334 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1336 struct nfs4_closedata *calldata = data;
1337 struct nfs4_state *state = calldata->state;
1338 int clear_rd, clear_wr, clear_rdwr;
1340 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1343 clear_rd = clear_wr = clear_rdwr = 0;
1344 spin_lock(&state->owner->so_lock);
1345 /* Calculate the change in open mode */
1346 if (state->n_rdwr == 0) {
1347 if (state->n_rdonly == 0) {
1348 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1349 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1351 if (state->n_wronly == 0) {
1352 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1353 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1356 spin_unlock(&state->owner->so_lock);
1357 if (!clear_rd && !clear_wr && !clear_rdwr) {
1358 /* Note: exit _without_ calling nfs4_close_done */
1359 task->tk_action = NULL;
1362 nfs_fattr_init(calldata->res.fattr);
1363 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1364 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1365 calldata->arg.open_flags = FMODE_READ;
1366 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1367 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1368 calldata->arg.open_flags = FMODE_WRITE;
1370 calldata->timestamp = jiffies;
1371 rpc_call_start(task);
1374 static const struct rpc_call_ops nfs4_close_ops = {
1375 .rpc_call_prepare = nfs4_close_prepare,
1376 .rpc_call_done = nfs4_close_done,
1377 .rpc_release = nfs4_free_closedata,
1381 * It is possible for data to be read/written from a mem-mapped file
1382 * after the sys_close call (which hits the vfs layer as a flush).
1383 * This means that we can't safely call nfsv4 close on a file until
1384 * the inode is cleared. This in turn means that we are not good
1385 * NFSv4 citizens - we do not indicate to the server to update the file's
1386 * share state even when we are done with one of the three share
1387 * stateid's in the inode.
1389 * NOTE: Caller must be holding the sp->so_owner semaphore!
1391 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1393 struct nfs_server *server = NFS_SERVER(state->inode);
1394 struct nfs4_closedata *calldata;
1395 struct nfs4_state_owner *sp = state->owner;
1396 struct rpc_task *task;
1397 struct rpc_message msg = {
1398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1399 .rpc_cred = state->owner->so_cred,
1401 struct rpc_task_setup task_setup_data = {
1402 .rpc_client = server->client,
1403 .rpc_message = &msg,
1404 .callback_ops = &nfs4_close_ops,
1405 .workqueue = nfsiod_workqueue,
1406 .flags = RPC_TASK_ASYNC,
1408 int status = -ENOMEM;
1410 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1411 if (calldata == NULL)
1413 calldata->inode = state->inode;
1414 calldata->state = state;
1415 calldata->arg.fh = NFS_FH(state->inode);
1416 calldata->arg.stateid = &state->open_stateid;
1417 /* Serialization for the sequence id */
1418 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1419 if (calldata->arg.seqid == NULL)
1420 goto out_free_calldata;
1421 calldata->arg.bitmask = server->attr_bitmask;
1422 calldata->res.fattr = &calldata->fattr;
1423 calldata->res.seqid = calldata->arg.seqid;
1424 calldata->res.server = server;
1425 calldata->path.mnt = mntget(path->mnt);
1426 calldata->path.dentry = dget(path->dentry);
1428 msg.rpc_argp = &calldata->arg,
1429 msg.rpc_resp = &calldata->res,
1430 task_setup_data.callback_data = calldata;
1431 task = rpc_run_task(&task_setup_data);
1433 return PTR_ERR(task);
1436 status = rpc_wait_for_completion_task(task);
1442 nfs4_put_open_state(state);
1443 nfs4_put_state_owner(sp);
1447 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
1452 /* If the open_intent is for execute, we have an extra check to make */
1453 if (nd->intent.open.flags & FMODE_EXEC) {
1454 ret = nfs_may_open(state->inode,
1455 state->owner->so_cred,
1456 nd->intent.open.flags);
1460 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1461 if (!IS_ERR(filp)) {
1462 struct nfs_open_context *ctx;
1463 ctx = nfs_file_open_context(filp);
1467 ret = PTR_ERR(filp);
1469 nfs4_close_sync(path, state, nd->intent.open.flags);
1474 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1476 struct path path = {
1477 .mnt = nd->path.mnt,
1480 struct dentry *parent;
1482 struct rpc_cred *cred;
1483 struct nfs4_state *state;
1486 if (nd->flags & LOOKUP_CREATE) {
1487 attr.ia_mode = nd->intent.open.create_mode;
1488 attr.ia_valid = ATTR_MODE;
1489 if (!IS_POSIXACL(dir))
1490 attr.ia_mode &= ~current->fs->umask;
1493 BUG_ON(nd->intent.open.flags & O_CREAT);
1496 cred = rpc_lookup_cred();
1498 return (struct dentry *)cred;
1499 parent = dentry->d_parent;
1500 /* Protect against concurrent sillydeletes */
1501 nfs_block_sillyrename(parent);
1502 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
1504 if (IS_ERR(state)) {
1505 if (PTR_ERR(state) == -ENOENT) {
1506 d_add(dentry, NULL);
1507 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1509 nfs_unblock_sillyrename(parent);
1510 return (struct dentry *)state;
1512 res = d_add_unique(dentry, igrab(state->inode));
1515 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1516 nfs_unblock_sillyrename(parent);
1517 nfs4_intent_set_file(nd, &path, state);
1522 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1524 struct path path = {
1525 .mnt = nd->path.mnt,
1528 struct rpc_cred *cred;
1529 struct nfs4_state *state;
1531 cred = rpc_lookup_cred();
1533 return PTR_ERR(cred);
1534 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
1536 if (IS_ERR(state)) {
1537 switch (PTR_ERR(state)) {
1543 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1549 if (state->inode == dentry->d_inode) {
1550 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1551 nfs4_intent_set_file(nd, &path, state);
1554 nfs4_close_sync(&path, state, openflags);
1561 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1563 struct nfs4_server_caps_res res = {};
1564 struct rpc_message msg = {
1565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1566 .rpc_argp = fhandle,
1571 status = rpc_call_sync(server->client, &msg, 0);
1573 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1574 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1575 server->caps |= NFS_CAP_ACLS;
1576 if (res.has_links != 0)
1577 server->caps |= NFS_CAP_HARDLINKS;
1578 if (res.has_symlinks != 0)
1579 server->caps |= NFS_CAP_SYMLINKS;
1580 server->acl_bitmask = res.acl_bitmask;
1585 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1587 struct nfs4_exception exception = { };
1590 err = nfs4_handle_exception(server,
1591 _nfs4_server_capabilities(server, fhandle),
1593 } while (exception.retry);
1597 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1598 struct nfs_fsinfo *info)
1600 struct nfs4_lookup_root_arg args = {
1601 .bitmask = nfs4_fattr_bitmap,
1603 struct nfs4_lookup_res res = {
1605 .fattr = info->fattr,
1608 struct rpc_message msg = {
1609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1613 nfs_fattr_init(info->fattr);
1614 return rpc_call_sync(server->client, &msg, 0);
1617 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1618 struct nfs_fsinfo *info)
1620 struct nfs4_exception exception = { };
1623 err = nfs4_handle_exception(server,
1624 _nfs4_lookup_root(server, fhandle, info),
1626 } while (exception.retry);
1631 * get the file handle for the "/" directory on the server
1633 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1634 struct nfs_fsinfo *info)
1638 status = nfs4_lookup_root(server, fhandle, info);
1640 status = nfs4_server_capabilities(server, fhandle);
1642 status = nfs4_do_fsinfo(server, fhandle, info);
1643 return nfs4_map_errors(status);
1647 * Get locations and (maybe) other attributes of a referral.
1648 * Note that we'll actually follow the referral later when
1649 * we detect fsid mismatch in inode revalidation
1651 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1653 int status = -ENOMEM;
1654 struct page *page = NULL;
1655 struct nfs4_fs_locations *locations = NULL;
1657 page = alloc_page(GFP_KERNEL);
1660 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1661 if (locations == NULL)
1664 status = nfs4_proc_fs_locations(dir, name, locations, page);
1667 /* Make sure server returned a different fsid for the referral */
1668 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1669 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
1674 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1675 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1677 fattr->mode = S_IFDIR;
1678 memset(fhandle, 0, sizeof(struct nfs_fh));
1687 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1689 struct nfs4_getattr_arg args = {
1691 .bitmask = server->attr_bitmask,
1693 struct nfs4_getattr_res res = {
1697 struct rpc_message msg = {
1698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1703 nfs_fattr_init(fattr);
1704 return rpc_call_sync(server->client, &msg, 0);
1707 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1709 struct nfs4_exception exception = { };
1712 err = nfs4_handle_exception(server,
1713 _nfs4_proc_getattr(server, fhandle, fattr),
1715 } while (exception.retry);
1720 * The file is not closed if it is opened due to the a request to change
1721 * the size of the file. The open call will not be needed once the
1722 * VFS layer lookup-intents are implemented.
1724 * Close is called when the inode is destroyed.
1725 * If we haven't opened the file for O_WRONLY, we
1726 * need to in the size_change case to obtain a stateid.
1729 * Because OPEN is always done by name in nfsv4, it is
1730 * possible that we opened a different file by the same
1731 * name. We can recognize this race condition, but we
1732 * can't do anything about it besides returning an error.
1734 * This will be fixed with VFS changes (lookup-intent).
1737 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1738 struct iattr *sattr)
1740 struct inode *inode = dentry->d_inode;
1741 struct rpc_cred *cred = NULL;
1742 struct nfs4_state *state = NULL;
1745 nfs_fattr_init(fattr);
1747 /* Search for an existing open(O_WRITE) file */
1748 if (sattr->ia_valid & ATTR_FILE) {
1749 struct nfs_open_context *ctx;
1751 ctx = nfs_file_open_context(sattr->ia_file);
1758 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
1760 nfs_setattr_update_inode(inode, sattr);
1764 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1765 const struct qstr *name, struct nfs_fh *fhandle,
1766 struct nfs_fattr *fattr)
1769 struct nfs4_lookup_arg args = {
1770 .bitmask = server->attr_bitmask,
1774 struct nfs4_lookup_res res = {
1779 struct rpc_message msg = {
1780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1785 nfs_fattr_init(fattr);
1787 dprintk("NFS call lookupfh %s\n", name->name);
1788 status = rpc_call_sync(server->client, &msg, 0);
1789 dprintk("NFS reply lookupfh: %d\n", status);
1793 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1794 struct qstr *name, struct nfs_fh *fhandle,
1795 struct nfs_fattr *fattr)
1797 struct nfs4_exception exception = { };
1800 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1802 if (err == -NFS4ERR_MOVED) {
1806 err = nfs4_handle_exception(server, err, &exception);
1807 } while (exception.retry);
1811 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
1812 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1816 dprintk("NFS call lookup %s\n", name->name);
1817 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
1818 if (status == -NFS4ERR_MOVED)
1819 status = nfs4_get_referral(dir, name, fattr, fhandle);
1820 dprintk("NFS reply lookup: %d\n", status);
1824 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1826 struct nfs4_exception exception = { };
1829 err = nfs4_handle_exception(NFS_SERVER(dir),
1830 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1832 } while (exception.retry);
1836 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1838 struct nfs_server *server = NFS_SERVER(inode);
1839 struct nfs_fattr fattr;
1840 struct nfs4_accessargs args = {
1841 .fh = NFS_FH(inode),
1842 .bitmask = server->attr_bitmask,
1844 struct nfs4_accessres res = {
1848 struct rpc_message msg = {
1849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1852 .rpc_cred = entry->cred,
1854 int mode = entry->mask;
1858 * Determine which access bits we want to ask for...
1860 if (mode & MAY_READ)
1861 args.access |= NFS4_ACCESS_READ;
1862 if (S_ISDIR(inode->i_mode)) {
1863 if (mode & MAY_WRITE)
1864 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1865 if (mode & MAY_EXEC)
1866 args.access |= NFS4_ACCESS_LOOKUP;
1868 if (mode & MAY_WRITE)
1869 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1870 if (mode & MAY_EXEC)
1871 args.access |= NFS4_ACCESS_EXECUTE;
1873 nfs_fattr_init(&fattr);
1874 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1877 if (res.access & NFS4_ACCESS_READ)
1878 entry->mask |= MAY_READ;
1879 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1880 entry->mask |= MAY_WRITE;
1881 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1882 entry->mask |= MAY_EXEC;
1883 nfs_refresh_inode(inode, &fattr);
1888 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1890 struct nfs4_exception exception = { };
1893 err = nfs4_handle_exception(NFS_SERVER(inode),
1894 _nfs4_proc_access(inode, entry),
1896 } while (exception.retry);
1901 * TODO: For the time being, we don't try to get any attributes
1902 * along with any of the zero-copy operations READ, READDIR,
1905 * In the case of the first three, we want to put the GETATTR
1906 * after the read-type operation -- this is because it is hard
1907 * to predict the length of a GETATTR response in v4, and thus
1908 * align the READ data correctly. This means that the GETATTR
1909 * may end up partially falling into the page cache, and we should
1910 * shift it into the 'tail' of the xdr_buf before processing.
1911 * To do this efficiently, we need to know the total length
1912 * of data received, which doesn't seem to be available outside
1915 * In the case of WRITE, we also want to put the GETATTR after
1916 * the operation -- in this case because we want to make sure
1917 * we get the post-operation mtime and size. This means that
1918 * we can't use xdr_encode_pages() as written: we need a variant
1919 * of it which would leave room in the 'tail' iovec.
1921 * Both of these changes to the XDR layer would in fact be quite
1922 * minor, but I decided to leave them for a subsequent patch.
1924 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1925 unsigned int pgbase, unsigned int pglen)
1927 struct nfs4_readlink args = {
1928 .fh = NFS_FH(inode),
1933 struct rpc_message msg = {
1934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1939 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1942 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1943 unsigned int pgbase, unsigned int pglen)
1945 struct nfs4_exception exception = { };
1948 err = nfs4_handle_exception(NFS_SERVER(inode),
1949 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1951 } while (exception.retry);
1957 * We will need to arrange for the VFS layer to provide an atomic open.
1958 * Until then, this create/open method is prone to inefficiency and race
1959 * conditions due to the lookup, create, and open VFS calls from sys_open()
1960 * placed on the wire.
1962 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1963 * The file will be opened again in the subsequent VFS open call
1964 * (nfs4_proc_file_open).
1966 * The open for read will just hang around to be used by any process that
1967 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1971 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1972 int flags, struct nameidata *nd)
1974 struct path path = {
1975 .mnt = nd->path.mnt,
1978 struct nfs4_state *state;
1979 struct rpc_cred *cred;
1982 cred = rpc_lookup_cred();
1984 status = PTR_ERR(cred);
1987 state = nfs4_do_open(dir, &path, flags, sattr, cred);
1989 if (IS_ERR(state)) {
1990 status = PTR_ERR(state);
1993 d_add(dentry, igrab(state->inode));
1994 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1995 if (flags & O_EXCL) {
1996 struct nfs_fattr fattr;
1997 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
1999 nfs_setattr_update_inode(state->inode, sattr);
2000 nfs_post_op_update_inode(state->inode, &fattr);
2002 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2003 status = nfs4_intent_set_file(nd, &path, state);
2005 nfs4_close_sync(&path, state, flags);
2012 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2014 struct nfs_server *server = NFS_SERVER(dir);
2015 struct nfs_removeargs args = {
2017 .name.len = name->len,
2018 .name.name = name->name,
2019 .bitmask = server->attr_bitmask,
2021 struct nfs_removeres res = {
2024 struct rpc_message msg = {
2025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2031 nfs_fattr_init(&res.dir_attr);
2032 status = rpc_call_sync(server->client, &msg, 0);
2034 update_changeattr(dir, &res.cinfo);
2035 nfs_post_op_update_inode(dir, &res.dir_attr);
2040 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2042 struct nfs4_exception exception = { };
2045 err = nfs4_handle_exception(NFS_SERVER(dir),
2046 _nfs4_proc_remove(dir, name),
2048 } while (exception.retry);
2052 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2054 struct nfs_server *server = NFS_SERVER(dir);
2055 struct nfs_removeargs *args = msg->rpc_argp;
2056 struct nfs_removeres *res = msg->rpc_resp;
2058 args->bitmask = server->attr_bitmask;
2059 res->server = server;
2060 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2063 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2065 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2067 if (nfs4_async_handle_error(task, res->server) == -EAGAIN)
2069 update_changeattr(dir, &res->cinfo);
2070 nfs_post_op_update_inode(dir, &res->dir_attr);
2074 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2075 struct inode *new_dir, struct qstr *new_name)
2077 struct nfs_server *server = NFS_SERVER(old_dir);
2078 struct nfs4_rename_arg arg = {
2079 .old_dir = NFS_FH(old_dir),
2080 .new_dir = NFS_FH(new_dir),
2081 .old_name = old_name,
2082 .new_name = new_name,
2083 .bitmask = server->attr_bitmask,
2085 struct nfs_fattr old_fattr, new_fattr;
2086 struct nfs4_rename_res res = {
2088 .old_fattr = &old_fattr,
2089 .new_fattr = &new_fattr,
2091 struct rpc_message msg = {
2092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2098 nfs_fattr_init(res.old_fattr);
2099 nfs_fattr_init(res.new_fattr);
2100 status = rpc_call_sync(server->client, &msg, 0);
2103 update_changeattr(old_dir, &res.old_cinfo);
2104 nfs_post_op_update_inode(old_dir, res.old_fattr);
2105 update_changeattr(new_dir, &res.new_cinfo);
2106 nfs_post_op_update_inode(new_dir, res.new_fattr);
2111 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2112 struct inode *new_dir, struct qstr *new_name)
2114 struct nfs4_exception exception = { };
2117 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2118 _nfs4_proc_rename(old_dir, old_name,
2121 } while (exception.retry);
2125 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2127 struct nfs_server *server = NFS_SERVER(inode);
2128 struct nfs4_link_arg arg = {
2129 .fh = NFS_FH(inode),
2130 .dir_fh = NFS_FH(dir),
2132 .bitmask = server->attr_bitmask,
2134 struct nfs_fattr fattr, dir_attr;
2135 struct nfs4_link_res res = {
2138 .dir_attr = &dir_attr,
2140 struct rpc_message msg = {
2141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2147 nfs_fattr_init(res.fattr);
2148 nfs_fattr_init(res.dir_attr);
2149 status = rpc_call_sync(server->client, &msg, 0);
2151 update_changeattr(dir, &res.cinfo);
2152 nfs_post_op_update_inode(dir, res.dir_attr);
2153 nfs_post_op_update_inode(inode, res.fattr);
2159 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2161 struct nfs4_exception exception = { };
2164 err = nfs4_handle_exception(NFS_SERVER(inode),
2165 _nfs4_proc_link(inode, dir, name),
2167 } while (exception.retry);
2171 struct nfs4_createdata {
2172 struct rpc_message msg;
2173 struct nfs4_create_arg arg;
2174 struct nfs4_create_res res;
2176 struct nfs_fattr fattr;
2177 struct nfs_fattr dir_fattr;
2180 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2181 struct qstr *name, struct iattr *sattr, u32 ftype)
2183 struct nfs4_createdata *data;
2185 data = kzalloc(sizeof(*data), GFP_KERNEL);
2187 struct nfs_server *server = NFS_SERVER(dir);
2189 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2190 data->msg.rpc_argp = &data->arg;
2191 data->msg.rpc_resp = &data->res;
2192 data->arg.dir_fh = NFS_FH(dir);
2193 data->arg.server = server;
2194 data->arg.name = name;
2195 data->arg.attrs = sattr;
2196 data->arg.ftype = ftype;
2197 data->arg.bitmask = server->attr_bitmask;
2198 data->res.server = server;
2199 data->res.fh = &data->fh;
2200 data->res.fattr = &data->fattr;
2201 data->res.dir_fattr = &data->dir_fattr;
2202 nfs_fattr_init(data->res.fattr);
2203 nfs_fattr_init(data->res.dir_fattr);
2208 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2210 int status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
2212 update_changeattr(dir, &data->res.dir_cinfo);
2213 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2214 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2219 static void nfs4_free_createdata(struct nfs4_createdata *data)
2224 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2225 struct page *page, unsigned int len, struct iattr *sattr)
2227 struct nfs4_createdata *data;
2228 int status = -ENAMETOOLONG;
2230 if (len > NFS4_MAXPATHLEN)
2234 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2238 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2239 data->arg.u.symlink.pages = &page;
2240 data->arg.u.symlink.len = len;
2242 status = nfs4_do_create(dir, dentry, data);
2244 nfs4_free_createdata(data);
2249 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2250 struct page *page, unsigned int len, struct iattr *sattr)
2252 struct nfs4_exception exception = { };
2255 err = nfs4_handle_exception(NFS_SERVER(dir),
2256 _nfs4_proc_symlink(dir, dentry, page,
2259 } while (exception.retry);
2263 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2264 struct iattr *sattr)
2266 struct nfs4_createdata *data;
2267 int status = -ENOMEM;
2269 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2273 status = nfs4_do_create(dir, dentry, data);
2275 nfs4_free_createdata(data);
2280 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2281 struct iattr *sattr)
2283 struct nfs4_exception exception = { };
2286 err = nfs4_handle_exception(NFS_SERVER(dir),
2287 _nfs4_proc_mkdir(dir, dentry, sattr),
2289 } while (exception.retry);
2293 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2294 u64 cookie, struct page *page, unsigned int count, int plus)
2296 struct inode *dir = dentry->d_inode;
2297 struct nfs4_readdir_arg args = {
2302 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2304 struct nfs4_readdir_res res;
2305 struct rpc_message msg = {
2306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2313 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2314 dentry->d_parent->d_name.name,
2315 dentry->d_name.name,
2316 (unsigned long long)cookie);
2317 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2318 res.pgbase = args.pgbase;
2319 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2321 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2323 nfs_invalidate_atime(dir);
2325 dprintk("%s: returns %d\n", __func__, status);
2329 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2330 u64 cookie, struct page *page, unsigned int count, int plus)
2332 struct nfs4_exception exception = { };
2335 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2336 _nfs4_proc_readdir(dentry, cred, cookie,
2339 } while (exception.retry);
2343 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2344 struct iattr *sattr, dev_t rdev)
2346 struct nfs4_createdata *data;
2347 int mode = sattr->ia_mode;
2348 int status = -ENOMEM;
2350 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2351 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2353 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2358 data->arg.ftype = NF4FIFO;
2359 else if (S_ISBLK(mode)) {
2360 data->arg.ftype = NF4BLK;
2361 data->arg.u.device.specdata1 = MAJOR(rdev);
2362 data->arg.u.device.specdata2 = MINOR(rdev);
2364 else if (S_ISCHR(mode)) {
2365 data->arg.ftype = NF4CHR;
2366 data->arg.u.device.specdata1 = MAJOR(rdev);
2367 data->arg.u.device.specdata2 = MINOR(rdev);
2370 status = nfs4_do_create(dir, dentry, data);
2372 nfs4_free_createdata(data);
2377 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2378 struct iattr *sattr, dev_t rdev)
2380 struct nfs4_exception exception = { };
2383 err = nfs4_handle_exception(NFS_SERVER(dir),
2384 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2386 } while (exception.retry);
2390 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2391 struct nfs_fsstat *fsstat)
2393 struct nfs4_statfs_arg args = {
2395 .bitmask = server->attr_bitmask,
2397 struct rpc_message msg = {
2398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2403 nfs_fattr_init(fsstat->fattr);
2404 return rpc_call_sync(server->client, &msg, 0);
2407 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2409 struct nfs4_exception exception = { };
2412 err = nfs4_handle_exception(server,
2413 _nfs4_proc_statfs(server, fhandle, fsstat),
2415 } while (exception.retry);
2419 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2420 struct nfs_fsinfo *fsinfo)
2422 struct nfs4_fsinfo_arg args = {
2424 .bitmask = server->attr_bitmask,
2426 struct rpc_message msg = {
2427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2432 return rpc_call_sync(server->client, &msg, 0);
2435 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2437 struct nfs4_exception exception = { };
2441 err = nfs4_handle_exception(server,
2442 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2444 } while (exception.retry);
2448 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2450 nfs_fattr_init(fsinfo->fattr);
2451 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2454 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2455 struct nfs_pathconf *pathconf)
2457 struct nfs4_pathconf_arg args = {
2459 .bitmask = server->attr_bitmask,
2461 struct rpc_message msg = {
2462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2464 .rpc_resp = pathconf,
2467 /* None of the pathconf attributes are mandatory to implement */
2468 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2469 memset(pathconf, 0, sizeof(*pathconf));
2473 nfs_fattr_init(pathconf->fattr);
2474 return rpc_call_sync(server->client, &msg, 0);
2477 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2478 struct nfs_pathconf *pathconf)
2480 struct nfs4_exception exception = { };
2484 err = nfs4_handle_exception(server,
2485 _nfs4_proc_pathconf(server, fhandle, pathconf),
2487 } while (exception.retry);
2491 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2493 struct nfs_server *server = NFS_SERVER(data->inode);
2495 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
2496 rpc_restart_call(task);
2500 nfs_invalidate_atime(data->inode);
2501 if (task->tk_status > 0)
2502 renew_lease(server, data->timestamp);
2506 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2508 data->timestamp = jiffies;
2509 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2512 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2514 struct inode *inode = data->inode;
2516 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2517 rpc_restart_call(task);
2520 if (task->tk_status >= 0) {
2521 renew_lease(NFS_SERVER(inode), data->timestamp);
2522 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2527 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2529 struct nfs_server *server = NFS_SERVER(data->inode);
2531 data->args.bitmask = server->attr_bitmask;
2532 data->res.server = server;
2533 data->timestamp = jiffies;
2535 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2538 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2540 struct inode *inode = data->inode;
2542 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2543 rpc_restart_call(task);
2546 nfs_refresh_inode(inode, data->res.fattr);
2550 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2552 struct nfs_server *server = NFS_SERVER(data->inode);
2554 data->args.bitmask = server->attr_bitmask;
2555 data->res.server = server;
2556 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2560 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2561 * standalone procedure for queueing an asynchronous RENEW.
2563 static void nfs4_renew_done(struct rpc_task *task, void *data)
2565 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2566 unsigned long timestamp = (unsigned long)data;
2568 if (task->tk_status < 0) {
2569 switch (task->tk_status) {
2570 case -NFS4ERR_STALE_CLIENTID:
2571 case -NFS4ERR_EXPIRED:
2572 case -NFS4ERR_CB_PATH_DOWN:
2573 nfs4_schedule_state_recovery(clp);
2577 spin_lock(&clp->cl_lock);
2578 if (time_before(clp->cl_last_renewal,timestamp))
2579 clp->cl_last_renewal = timestamp;
2580 spin_unlock(&clp->cl_lock);
2583 static const struct rpc_call_ops nfs4_renew_ops = {
2584 .rpc_call_done = nfs4_renew_done,
2587 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2589 struct rpc_message msg = {
2590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2595 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2596 &nfs4_renew_ops, (void *)jiffies);
2599 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2601 struct rpc_message msg = {
2602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2606 unsigned long now = jiffies;
2609 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2612 spin_lock(&clp->cl_lock);
2613 if (time_before(clp->cl_last_renewal,now))
2614 clp->cl_last_renewal = now;
2615 spin_unlock(&clp->cl_lock);
2619 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2621 return (server->caps & NFS_CAP_ACLS)
2622 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2623 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2626 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2627 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2630 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2632 static void buf_to_pages(const void *buf, size_t buflen,
2633 struct page **pages, unsigned int *pgbase)
2635 const void *p = buf;
2637 *pgbase = offset_in_page(buf);
2639 while (p < buf + buflen) {
2640 *(pages++) = virt_to_page(p);
2641 p += PAGE_CACHE_SIZE;
2645 struct nfs4_cached_acl {
2651 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2653 struct nfs_inode *nfsi = NFS_I(inode);
2655 spin_lock(&inode->i_lock);
2656 kfree(nfsi->nfs4_acl);
2657 nfsi->nfs4_acl = acl;
2658 spin_unlock(&inode->i_lock);
2661 static void nfs4_zap_acl_attr(struct inode *inode)
2663 nfs4_set_cached_acl(inode, NULL);
2666 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2668 struct nfs_inode *nfsi = NFS_I(inode);
2669 struct nfs4_cached_acl *acl;
2672 spin_lock(&inode->i_lock);
2673 acl = nfsi->nfs4_acl;
2676 if (buf == NULL) /* user is just asking for length */
2678 if (acl->cached == 0)
2680 ret = -ERANGE; /* see getxattr(2) man page */
2681 if (acl->len > buflen)
2683 memcpy(buf, acl->data, acl->len);
2687 spin_unlock(&inode->i_lock);
2691 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2693 struct nfs4_cached_acl *acl;
2695 if (buf && acl_len <= PAGE_SIZE) {
2696 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2700 memcpy(acl->data, buf, acl_len);
2702 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2709 nfs4_set_cached_acl(inode, acl);
2712 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2714 struct page *pages[NFS4ACL_MAXPAGES];
2715 struct nfs_getaclargs args = {
2716 .fh = NFS_FH(inode),
2720 size_t resp_len = buflen;
2722 struct rpc_message msg = {
2723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2725 .rpc_resp = &resp_len,
2727 struct page *localpage = NULL;
2730 if (buflen < PAGE_SIZE) {
2731 /* As long as we're doing a round trip to the server anyway,
2732 * let's be prepared for a page of acl data. */
2733 localpage = alloc_page(GFP_KERNEL);
2734 resp_buf = page_address(localpage);
2735 if (localpage == NULL)
2737 args.acl_pages[0] = localpage;
2738 args.acl_pgbase = 0;
2739 resp_len = args.acl_len = PAGE_SIZE;
2742 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2744 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2747 if (resp_len > args.acl_len)
2748 nfs4_write_cached_acl(inode, NULL, resp_len);
2750 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2753 if (resp_len > buflen)
2756 memcpy(buf, resp_buf, resp_len);
2761 __free_page(localpage);
2765 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2767 struct nfs4_exception exception = { };
2770 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2773 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2774 } while (exception.retry);
2778 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2780 struct nfs_server *server = NFS_SERVER(inode);
2783 if (!nfs4_server_supports_acls(server))
2785 ret = nfs_revalidate_inode(server, inode);
2788 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
2789 nfs_zap_acl_cache(inode);
2790 ret = nfs4_read_cached_acl(inode, buf, buflen);
2793 return nfs4_get_acl_uncached(inode, buf, buflen);
2796 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2798 struct nfs_server *server = NFS_SERVER(inode);
2799 struct page *pages[NFS4ACL_MAXPAGES];
2800 struct nfs_setaclargs arg = {
2801 .fh = NFS_FH(inode),
2805 struct rpc_message msg = {
2806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2812 if (!nfs4_server_supports_acls(server))
2814 nfs_inode_return_delegation(inode);
2815 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2816 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2817 nfs_access_zap_cache(inode);
2818 nfs_zap_acl_cache(inode);
2822 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2824 struct nfs4_exception exception = { };
2827 err = nfs4_handle_exception(NFS_SERVER(inode),
2828 __nfs4_proc_set_acl(inode, buf, buflen),
2830 } while (exception.retry);
2835 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2837 struct nfs_client *clp = server->nfs_client;
2839 if (!clp || task->tk_status >= 0)
2841 switch(task->tk_status) {
2842 case -NFS4ERR_STALE_CLIENTID:
2843 case -NFS4ERR_STALE_STATEID:
2844 case -NFS4ERR_EXPIRED:
2845 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
2846 nfs4_schedule_state_recovery(clp);
2847 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
2848 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
2849 task->tk_status = 0;
2851 case -NFS4ERR_DELAY:
2852 nfs_inc_server_stats(server, NFSIOS_DELAY);
2853 case -NFS4ERR_GRACE:
2854 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2855 task->tk_status = 0;
2857 case -NFS4ERR_OLD_STATEID:
2858 task->tk_status = 0;
2861 task->tk_status = nfs4_map_errors(task->tk_status);
2865 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
2867 nfs4_verifier sc_verifier;
2868 struct nfs4_setclientid setclientid = {
2869 .sc_verifier = &sc_verifier,
2872 struct rpc_message msg = {
2873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2874 .rpc_argp = &setclientid,
2882 p = (__be32*)sc_verifier.data;
2883 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2884 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2887 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2888 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
2890 rpc_peeraddr2str(clp->cl_rpcclient,
2892 rpc_peeraddr2str(clp->cl_rpcclient,
2894 clp->cl_rpcclient->cl_auth->au_ops->au_name,
2895 clp->cl_id_uniquifier);
2896 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2897 sizeof(setclientid.sc_netid),
2898 rpc_peeraddr2str(clp->cl_rpcclient,
2899 RPC_DISPLAY_NETID));
2900 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2901 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
2902 clp->cl_ipaddr, port >> 8, port & 255);
2904 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2905 if (status != -NFS4ERR_CLID_INUSE)
2910 ssleep(clp->cl_lease_time + 1);
2912 if (++clp->cl_id_uniquifier == 0)
2918 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2920 struct nfs_fsinfo fsinfo;
2921 struct rpc_message msg = {
2922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2924 .rpc_resp = &fsinfo,
2931 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2933 spin_lock(&clp->cl_lock);
2934 clp->cl_lease_time = fsinfo.lease_time * HZ;
2935 clp->cl_last_renewal = now;
2936 spin_unlock(&clp->cl_lock);
2941 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2946 err = _nfs4_proc_setclientid_confirm(clp, cred);
2950 case -NFS4ERR_RESOURCE:
2951 /* The IBM lawyers misread another document! */
2952 case -NFS4ERR_DELAY:
2953 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2959 struct nfs4_delegreturndata {
2960 struct nfs4_delegreturnargs args;
2961 struct nfs4_delegreturnres res;
2963 nfs4_stateid stateid;
2964 unsigned long timestamp;
2965 struct nfs_fattr fattr;
2969 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2971 struct nfs4_delegreturndata *data = calldata;
2972 data->rpc_status = task->tk_status;
2973 if (data->rpc_status == 0)
2974 renew_lease(data->res.server, data->timestamp);
2977 static void nfs4_delegreturn_release(void *calldata)
2982 static const struct rpc_call_ops nfs4_delegreturn_ops = {
2983 .rpc_call_done = nfs4_delegreturn_done,
2984 .rpc_release = nfs4_delegreturn_release,
2987 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
2989 struct nfs4_delegreturndata *data;
2990 struct nfs_server *server = NFS_SERVER(inode);
2991 struct rpc_task *task;
2992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2996 struct rpc_task_setup task_setup_data = {
2997 .rpc_client = server->client,
2998 .rpc_message = &msg,
2999 .callback_ops = &nfs4_delegreturn_ops,
3000 .flags = RPC_TASK_ASYNC,
3004 data = kmalloc(sizeof(*data), GFP_KERNEL);
3007 data->args.fhandle = &data->fh;
3008 data->args.stateid = &data->stateid;
3009 data->args.bitmask = server->attr_bitmask;
3010 nfs_copy_fh(&data->fh, NFS_FH(inode));
3011 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3012 data->res.fattr = &data->fattr;
3013 data->res.server = server;
3014 nfs_fattr_init(data->res.fattr);
3015 data->timestamp = jiffies;
3016 data->rpc_status = 0;
3018 task_setup_data.callback_data = data;
3019 msg.rpc_argp = &data->args,
3020 msg.rpc_resp = &data->res,
3021 task = rpc_run_task(&task_setup_data);
3023 return PTR_ERR(task);
3026 status = nfs4_wait_for_completion_rpc_task(task);
3029 status = data->rpc_status;
3032 nfs_refresh_inode(inode, &data->fattr);
3038 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3040 struct nfs_server *server = NFS_SERVER(inode);
3041 struct nfs4_exception exception = { };
3044 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3046 case -NFS4ERR_STALE_STATEID:
3047 case -NFS4ERR_EXPIRED:
3051 err = nfs4_handle_exception(server, err, &exception);
3052 } while (exception.retry);
3056 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3057 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3060 * sleep, with exponential backoff, and retry the LOCK operation.
3062 static unsigned long
3063 nfs4_set_lock_task_retry(unsigned long timeout)
3065 schedule_timeout_killable(timeout);
3067 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3068 return NFS4_LOCK_MAXTIMEOUT;
3072 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3074 struct inode *inode = state->inode;
3075 struct nfs_server *server = NFS_SERVER(inode);
3076 struct nfs_client *clp = server->nfs_client;
3077 struct nfs_lockt_args arg = {
3078 .fh = NFS_FH(inode),
3081 struct nfs_lockt_res res = {
3084 struct rpc_message msg = {
3085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3088 .rpc_cred = state->owner->so_cred,
3090 struct nfs4_lock_state *lsp;
3093 arg.lock_owner.clientid = clp->cl_clientid;
3094 status = nfs4_set_lock_state(state, request);
3097 lsp = request->fl_u.nfs4_fl.owner;
3098 arg.lock_owner.id = lsp->ls_id.id;
3099 status = rpc_call_sync(server->client, &msg, 0);
3102 request->fl_type = F_UNLCK;
3104 case -NFS4ERR_DENIED:
3107 request->fl_ops->fl_release_private(request);
3112 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3114 struct nfs4_exception exception = { };
3118 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3119 _nfs4_proc_getlk(state, cmd, request),
3121 } while (exception.retry);
3125 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3128 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3130 res = posix_lock_file_wait(file, fl);
3133 res = flock_lock_file_wait(file, fl);
3141 struct nfs4_unlockdata {
3142 struct nfs_locku_args arg;
3143 struct nfs_locku_res res;
3144 struct nfs4_lock_state *lsp;
3145 struct nfs_open_context *ctx;
3146 struct file_lock fl;
3147 const struct nfs_server *server;
3148 unsigned long timestamp;
3151 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3152 struct nfs_open_context *ctx,
3153 struct nfs4_lock_state *lsp,
3154 struct nfs_seqid *seqid)
3156 struct nfs4_unlockdata *p;
3157 struct inode *inode = lsp->ls_state->inode;
3159 p = kmalloc(sizeof(*p), GFP_KERNEL);
3162 p->arg.fh = NFS_FH(inode);
3164 p->arg.seqid = seqid;
3165 p->res.seqid = seqid;
3166 p->arg.stateid = &lsp->ls_stateid;
3168 atomic_inc(&lsp->ls_count);
3169 /* Ensure we don't close file until we're done freeing locks! */
3170 p->ctx = get_nfs_open_context(ctx);
3171 memcpy(&p->fl, fl, sizeof(p->fl));
3172 p->server = NFS_SERVER(inode);
3176 static void nfs4_locku_release_calldata(void *data)
3178 struct nfs4_unlockdata *calldata = data;
3179 nfs_free_seqid(calldata->arg.seqid);
3180 nfs4_put_lock_state(calldata->lsp);
3181 put_nfs_open_context(calldata->ctx);
3185 static void nfs4_locku_done(struct rpc_task *task, void *data)
3187 struct nfs4_unlockdata *calldata = data;
3189 if (RPC_ASSASSINATED(task))
3191 switch (task->tk_status) {
3193 memcpy(calldata->lsp->ls_stateid.data,
3194 calldata->res.stateid.data,
3195 sizeof(calldata->lsp->ls_stateid.data));
3196 renew_lease(calldata->server, calldata->timestamp);
3198 case -NFS4ERR_STALE_STATEID:
3199 case -NFS4ERR_EXPIRED:
3202 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
3203 rpc_restart_call(task);
3207 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3209 struct nfs4_unlockdata *calldata = data;
3211 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3213 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3214 /* Note: exit _without_ running nfs4_locku_done */
3215 task->tk_action = NULL;
3218 calldata->timestamp = jiffies;
3219 rpc_call_start(task);
3222 static const struct rpc_call_ops nfs4_locku_ops = {
3223 .rpc_call_prepare = nfs4_locku_prepare,
3224 .rpc_call_done = nfs4_locku_done,
3225 .rpc_release = nfs4_locku_release_calldata,
3228 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3229 struct nfs_open_context *ctx,
3230 struct nfs4_lock_state *lsp,
3231 struct nfs_seqid *seqid)
3233 struct nfs4_unlockdata *data;
3234 struct rpc_message msg = {
3235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3236 .rpc_cred = ctx->cred,
3238 struct rpc_task_setup task_setup_data = {
3239 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3240 .rpc_message = &msg,
3241 .callback_ops = &nfs4_locku_ops,
3242 .workqueue = nfsiod_workqueue,
3243 .flags = RPC_TASK_ASYNC,
3246 /* Ensure this is an unlock - when canceling a lock, the
3247 * canceled lock is passed in, and it won't be an unlock.
3249 fl->fl_type = F_UNLCK;
3251 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3253 nfs_free_seqid(seqid);
3254 return ERR_PTR(-ENOMEM);
3257 msg.rpc_argp = &data->arg,
3258 msg.rpc_resp = &data->res,
3259 task_setup_data.callback_data = data;
3260 return rpc_run_task(&task_setup_data);
3263 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3265 struct nfs_inode *nfsi = NFS_I(state->inode);
3266 struct nfs_seqid *seqid;
3267 struct nfs4_lock_state *lsp;
3268 struct rpc_task *task;
3270 unsigned char fl_flags = request->fl_flags;
3272 status = nfs4_set_lock_state(state, request);
3273 /* Unlock _before_ we do the RPC call */
3274 request->fl_flags |= FL_EXISTS;
3275 down_read(&nfsi->rwsem);
3276 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3277 up_read(&nfsi->rwsem);
3280 up_read(&nfsi->rwsem);
3283 /* Is this a delegated lock? */
3284 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3286 lsp = request->fl_u.nfs4_fl.owner;
3287 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3291 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3292 status = PTR_ERR(task);
3295 status = nfs4_wait_for_completion_rpc_task(task);
3298 request->fl_flags = fl_flags;
3302 struct nfs4_lockdata {
3303 struct nfs_lock_args arg;
3304 struct nfs_lock_res res;
3305 struct nfs4_lock_state *lsp;
3306 struct nfs_open_context *ctx;
3307 struct file_lock fl;
3308 unsigned long timestamp;
3313 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3314 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3316 struct nfs4_lockdata *p;
3317 struct inode *inode = lsp->ls_state->inode;
3318 struct nfs_server *server = NFS_SERVER(inode);
3320 p = kzalloc(sizeof(*p), GFP_KERNEL);
3324 p->arg.fh = NFS_FH(inode);
3326 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3327 if (p->arg.open_seqid == NULL)
3329 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3330 if (p->arg.lock_seqid == NULL)
3331 goto out_free_seqid;
3332 p->arg.lock_stateid = &lsp->ls_stateid;
3333 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3334 p->arg.lock_owner.id = lsp->ls_id.id;
3335 p->res.lock_seqid = p->arg.lock_seqid;
3337 atomic_inc(&lsp->ls_count);
3338 p->ctx = get_nfs_open_context(ctx);
3339 memcpy(&p->fl, fl, sizeof(p->fl));
3342 nfs_free_seqid(p->arg.open_seqid);
3348 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3350 struct nfs4_lockdata *data = calldata;
3351 struct nfs4_state *state = data->lsp->ls_state;
3353 dprintk("%s: begin!\n", __func__);
3354 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3356 /* Do we need to do an open_to_lock_owner? */
3357 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3358 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3360 data->arg.open_stateid = &state->stateid;
3361 data->arg.new_lock_owner = 1;
3362 data->res.open_seqid = data->arg.open_seqid;
3364 data->arg.new_lock_owner = 0;
3365 data->timestamp = jiffies;
3366 rpc_call_start(task);
3367 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3370 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3372 struct nfs4_lockdata *data = calldata;
3374 dprintk("%s: begin!\n", __func__);
3376 data->rpc_status = task->tk_status;
3377 if (RPC_ASSASSINATED(task))
3379 if (data->arg.new_lock_owner != 0) {
3380 if (data->rpc_status == 0)
3381 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3385 if (data->rpc_status == 0) {
3386 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3387 sizeof(data->lsp->ls_stateid.data));
3388 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3389 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3392 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3395 static void nfs4_lock_release(void *calldata)
3397 struct nfs4_lockdata *data = calldata;
3399 dprintk("%s: begin!\n", __func__);
3400 nfs_free_seqid(data->arg.open_seqid);
3401 if (data->cancelled != 0) {
3402 struct rpc_task *task;
3403 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3404 data->arg.lock_seqid);
3407 dprintk("%s: cancelling lock!\n", __func__);
3409 nfs_free_seqid(data->arg.lock_seqid);
3410 nfs4_put_lock_state(data->lsp);
3411 put_nfs_open_context(data->ctx);
3413 dprintk("%s: done!\n", __func__);
3416 static const struct rpc_call_ops nfs4_lock_ops = {
3417 .rpc_call_prepare = nfs4_lock_prepare,
3418 .rpc_call_done = nfs4_lock_done,
3419 .rpc_release = nfs4_lock_release,
3422 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3424 struct nfs4_lockdata *data;
3425 struct rpc_task *task;
3426 struct rpc_message msg = {
3427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3428 .rpc_cred = state->owner->so_cred,
3430 struct rpc_task_setup task_setup_data = {
3431 .rpc_client = NFS_CLIENT(state->inode),
3432 .rpc_message = &msg,
3433 .callback_ops = &nfs4_lock_ops,
3434 .workqueue = nfsiod_workqueue,
3435 .flags = RPC_TASK_ASYNC,
3439 dprintk("%s: begin!\n", __func__);
3440 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3441 fl->fl_u.nfs4_fl.owner);
3445 data->arg.block = 1;
3447 data->arg.reclaim = 1;
3448 msg.rpc_argp = &data->arg,
3449 msg.rpc_resp = &data->res,
3450 task_setup_data.callback_data = data;
3451 task = rpc_run_task(&task_setup_data);
3453 return PTR_ERR(task);
3454 ret = nfs4_wait_for_completion_rpc_task(task);
3456 ret = data->rpc_status;
3457 if (ret == -NFS4ERR_DENIED)
3460 data->cancelled = 1;
3462 dprintk("%s: done, ret = %d!\n", __func__, ret);
3466 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3468 struct nfs_server *server = NFS_SERVER(state->inode);
3469 struct nfs4_exception exception = { };
3473 /* Cache the lock if possible... */
3474 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3476 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3477 if (err != -NFS4ERR_DELAY)
3479 nfs4_handle_exception(server, err, &exception);
3480 } while (exception.retry);
3484 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3486 struct nfs_server *server = NFS_SERVER(state->inode);
3487 struct nfs4_exception exception = { };
3490 err = nfs4_set_lock_state(state, request);
3494 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3496 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3497 if (err != -NFS4ERR_DELAY)
3499 nfs4_handle_exception(server, err, &exception);
3500 } while (exception.retry);
3504 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3506 struct nfs_inode *nfsi = NFS_I(state->inode);
3507 unsigned char fl_flags = request->fl_flags;
3510 /* Is this a delegated open? */
3511 status = nfs4_set_lock_state(state, request);
3514 request->fl_flags |= FL_ACCESS;
3515 status = do_vfs_lock(request->fl_file, request);
3518 down_read(&nfsi->rwsem);
3519 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3520 /* Yes: cache locks! */
3521 /* ...but avoid races with delegation recall... */
3522 request->fl_flags = fl_flags & ~FL_SLEEP;
3523 status = do_vfs_lock(request->fl_file, request);
3526 status = _nfs4_do_setlk(state, cmd, request, 0);
3529 /* Note: we always want to sleep here! */
3530 request->fl_flags = fl_flags | FL_SLEEP;
3531 if (do_vfs_lock(request->fl_file, request) < 0)
3532 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
3534 up_read(&nfsi->rwsem);
3536 request->fl_flags = fl_flags;
3540 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3542 struct nfs4_exception exception = { };
3546 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3547 _nfs4_proc_setlk(state, cmd, request),
3549 } while (exception.retry);
3554 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3556 struct nfs_open_context *ctx;
3557 struct nfs4_state *state;
3558 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3561 /* verify open state */
3562 ctx = nfs_file_open_context(filp);
3565 if (request->fl_start < 0 || request->fl_end < 0)
3569 return nfs4_proc_getlk(state, F_GETLK, request);
3571 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3574 if (request->fl_type == F_UNLCK)
3575 return nfs4_proc_unlck(state, cmd, request);
3578 status = nfs4_proc_setlk(state, cmd, request);
3579 if ((status != -EAGAIN) || IS_SETLK(cmd))
3581 timeout = nfs4_set_lock_task_retry(timeout);
3582 status = -ERESTARTSYS;
3585 } while(status < 0);
3589 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3591 struct nfs_server *server = NFS_SERVER(state->inode);
3592 struct nfs4_exception exception = { };
3595 err = nfs4_set_lock_state(state, fl);
3599 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3600 if (err != -NFS4ERR_DELAY)
3602 err = nfs4_handle_exception(server, err, &exception);
3603 } while (exception.retry);
3608 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3610 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3611 size_t buflen, int flags)
3613 struct inode *inode = dentry->d_inode;
3615 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3618 return nfs4_proc_set_acl(inode, buf, buflen);
3621 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3622 * and that's what we'll do for e.g. user attributes that haven't been set.
3623 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3624 * attributes in kernel-managed attribute namespaces. */
3625 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3628 struct inode *inode = dentry->d_inode;
3630 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3633 return nfs4_proc_get_acl(inode, buf, buflen);
3636 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3638 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3640 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3642 if (buf && buflen < len)
3645 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3649 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
3650 struct nfs4_fs_locations *fs_locations, struct page *page)
3652 struct nfs_server *server = NFS_SERVER(dir);
3654 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3655 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3657 struct nfs4_fs_locations_arg args = {
3658 .dir_fh = NFS_FH(dir),
3663 struct rpc_message msg = {
3664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3666 .rpc_resp = fs_locations,
3670 dprintk("%s: start\n", __func__);
3671 nfs_fattr_init(&fs_locations->fattr);
3672 fs_locations->server = server;
3673 fs_locations->nlocations = 0;
3674 status = rpc_call_sync(server->client, &msg, 0);
3675 dprintk("%s: returned status = %d\n", __func__, status);
3679 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3680 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
3681 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
3682 .recover_open = nfs4_open_reclaim,
3683 .recover_lock = nfs4_lock_reclaim,
3686 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
3687 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
3688 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
3689 .recover_open = nfs4_open_expired,
3690 .recover_lock = nfs4_lock_expired,
3693 static const struct inode_operations nfs4_file_inode_operations = {
3694 .permission = nfs_permission,
3695 .getattr = nfs_getattr,
3696 .setattr = nfs_setattr,
3697 .getxattr = nfs4_getxattr,
3698 .setxattr = nfs4_setxattr,
3699 .listxattr = nfs4_listxattr,
3702 const struct nfs_rpc_ops nfs_v4_clientops = {
3703 .version = 4, /* protocol version */
3704 .dentry_ops = &nfs4_dentry_operations,
3705 .dir_inode_ops = &nfs4_dir_inode_operations,
3706 .file_inode_ops = &nfs4_file_inode_operations,
3707 .getroot = nfs4_proc_get_root,
3708 .getattr = nfs4_proc_getattr,
3709 .setattr = nfs4_proc_setattr,
3710 .lookupfh = nfs4_proc_lookupfh,
3711 .lookup = nfs4_proc_lookup,
3712 .access = nfs4_proc_access,
3713 .readlink = nfs4_proc_readlink,
3714 .create = nfs4_proc_create,
3715 .remove = nfs4_proc_remove,
3716 .unlink_setup = nfs4_proc_unlink_setup,
3717 .unlink_done = nfs4_proc_unlink_done,
3718 .rename = nfs4_proc_rename,
3719 .link = nfs4_proc_link,
3720 .symlink = nfs4_proc_symlink,
3721 .mkdir = nfs4_proc_mkdir,
3722 .rmdir = nfs4_proc_remove,
3723 .readdir = nfs4_proc_readdir,
3724 .mknod = nfs4_proc_mknod,
3725 .statfs = nfs4_proc_statfs,
3726 .fsinfo = nfs4_proc_fsinfo,
3727 .pathconf = nfs4_proc_pathconf,
3728 .set_capabilities = nfs4_server_capabilities,
3729 .decode_dirent = nfs4_decode_dirent,
3730 .read_setup = nfs4_proc_read_setup,
3731 .read_done = nfs4_read_done,
3732 .write_setup = nfs4_proc_write_setup,
3733 .write_done = nfs4_write_done,
3734 .commit_setup = nfs4_proc_commit_setup,
3735 .commit_done = nfs4_commit_done,
3736 .lock = nfs4_proc_lock,
3737 .clear_acl_cache = nfs4_zap_acl_attr,