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"
56 #define NFSDBG_FACILITY NFSDBG_PROC
58 #define NFS4_POLL_RETRY_MIN (HZ/10)
59 #define NFS4_POLL_RETRY_MAX (15*HZ)
62 static int _nfs4_proc_open(struct nfs4_opendata *data);
63 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
64 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
65 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
66 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
67 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp);
68 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags);
70 /* Prevent leaks of NFSv4 errors into userland */
71 int nfs4_map_errors(int err)
74 dprintk("%s could not handle NFSv4 error %d\n",
82 * This is our standard bitmap for GETATTR requests.
84 const u32 nfs4_fattr_bitmap[2] = {
89 | FATTR4_WORD0_FILEID,
91 | FATTR4_WORD1_NUMLINKS
93 | FATTR4_WORD1_OWNER_GROUP
95 | FATTR4_WORD1_SPACE_USED
96 | FATTR4_WORD1_TIME_ACCESS
97 | FATTR4_WORD1_TIME_METADATA
98 | FATTR4_WORD1_TIME_MODIFY
101 const u32 nfs4_statfs_bitmap[2] = {
102 FATTR4_WORD0_FILES_AVAIL
103 | FATTR4_WORD0_FILES_FREE
104 | FATTR4_WORD0_FILES_TOTAL,
105 FATTR4_WORD1_SPACE_AVAIL
106 | FATTR4_WORD1_SPACE_FREE
107 | FATTR4_WORD1_SPACE_TOTAL
110 const u32 nfs4_pathconf_bitmap[2] = {
112 | FATTR4_WORD0_MAXNAME,
116 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
117 | FATTR4_WORD0_MAXREAD
118 | FATTR4_WORD0_MAXWRITE
119 | FATTR4_WORD0_LEASE_TIME,
123 const u32 nfs4_fs_locations_bitmap[2] = {
125 | FATTR4_WORD0_CHANGE
128 | FATTR4_WORD0_FILEID
129 | FATTR4_WORD0_FS_LOCATIONS,
131 | FATTR4_WORD1_NUMLINKS
133 | FATTR4_WORD1_OWNER_GROUP
134 | FATTR4_WORD1_RAWDEV
135 | FATTR4_WORD1_SPACE_USED
136 | FATTR4_WORD1_TIME_ACCESS
137 | FATTR4_WORD1_TIME_METADATA
138 | FATTR4_WORD1_TIME_MODIFY
139 | FATTR4_WORD1_MOUNTED_ON_FILEID
142 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
143 struct nfs4_readdir_arg *readdir)
147 BUG_ON(readdir->count < 80);
149 readdir->cookie = cookie;
150 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
155 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
160 * NFSv4 servers do not return entries for '.' and '..'
161 * Therefore, we fake these entries here. We let '.'
162 * have cookie 0 and '..' have cookie 1. Note that
163 * when talking to the server, we always send cookie 0
166 start = p = kmap_atomic(*readdir->pages, KM_USER0);
169 *p++ = xdr_one; /* next */
170 *p++ = xdr_zero; /* cookie, first word */
171 *p++ = xdr_one; /* cookie, second word */
172 *p++ = xdr_one; /* entry len */
173 memcpy(p, ".\0\0\0", 4); /* entry */
175 *p++ = xdr_one; /* bitmap length */
176 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
177 *p++ = htonl(8); /* attribute buffer length */
178 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_two; /* cookie, second word */
184 *p++ = xdr_two; /* entry len */
185 memcpy(p, "..\0\0", 4); /* entry */
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
190 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
192 readdir->pgbase = (char *)p - (char *)start;
193 readdir->count -= readdir->pgbase;
194 kunmap_atomic(start, KM_USER0);
197 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
199 struct nfs_client *clp = server->nfs_client;
200 spin_lock(&clp->cl_lock);
201 if (time_before(clp->cl_last_renewal,timestamp))
202 clp->cl_last_renewal = timestamp;
203 spin_unlock(&clp->cl_lock);
206 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
208 struct nfs_inode *nfsi = NFS_I(dir);
210 spin_lock(&dir->i_lock);
211 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
212 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
213 nfsi->change_attr = cinfo->after;
214 spin_unlock(&dir->i_lock);
217 struct nfs4_opendata {
219 struct nfs_openargs o_arg;
220 struct nfs_openres o_res;
221 struct nfs_open_confirmargs c_arg;
222 struct nfs_open_confirmres c_res;
223 struct nfs_fattr f_attr;
224 struct nfs_fattr dir_attr;
227 struct nfs4_state_owner *owner;
228 struct nfs4_state *state;
230 unsigned long timestamp;
231 unsigned int rpc_done : 1;
237 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
239 p->o_res.f_attr = &p->f_attr;
240 p->o_res.dir_attr = &p->dir_attr;
241 p->o_res.server = p->o_arg.server;
242 nfs_fattr_init(&p->f_attr);
243 nfs_fattr_init(&p->dir_attr);
246 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
247 struct nfs4_state_owner *sp, int flags,
248 const struct iattr *attrs)
250 struct dentry *parent = dget_parent(path->dentry);
251 struct inode *dir = parent->d_inode;
252 struct nfs_server *server = NFS_SERVER(dir);
253 struct nfs4_opendata *p;
255 p = kzalloc(sizeof(*p), GFP_KERNEL);
258 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
259 if (p->o_arg.seqid == NULL)
261 p->path.mnt = mntget(path->mnt);
262 p->path.dentry = dget(path->dentry);
265 atomic_inc(&sp->so_count);
266 p->o_arg.fh = NFS_FH(dir);
267 p->o_arg.open_flags = flags,
268 p->o_arg.clientid = server->nfs_client->cl_clientid;
269 p->o_arg.id = sp->so_owner_id.id;
270 p->o_arg.name = &p->path.dentry->d_name;
271 p->o_arg.server = server;
272 p->o_arg.bitmask = server->attr_bitmask;
273 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
274 if (flags & O_EXCL) {
275 u32 *s = (u32 *) p->o_arg.u.verifier.data;
278 } else if (flags & O_CREAT) {
279 p->o_arg.u.attrs = &p->attrs;
280 memcpy(&p->attrs, attrs, sizeof(p->attrs));
282 p->c_arg.fh = &p->o_res.fh;
283 p->c_arg.stateid = &p->o_res.stateid;
284 p->c_arg.seqid = p->o_arg.seqid;
285 nfs4_init_opendata_res(p);
295 static void nfs4_opendata_free(struct kref *kref)
297 struct nfs4_opendata *p = container_of(kref,
298 struct nfs4_opendata, kref);
300 nfs_free_seqid(p->o_arg.seqid);
301 if (p->state != NULL)
302 nfs4_put_open_state(p->state);
303 nfs4_put_state_owner(p->owner);
305 dput(p->path.dentry);
310 static void nfs4_opendata_put(struct nfs4_opendata *p)
313 kref_put(&p->kref, nfs4_opendata_free);
316 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
321 rpc_clnt_sigmask(task->tk_client, &oldset);
322 ret = rpc_wait_for_completion_task(task);
323 rpc_clnt_sigunmask(task->tk_client, &oldset);
327 static int can_open_delegated(struct nfs_delegation *delegation, mode_t open_flags)
329 if ((delegation->type & open_flags) != open_flags)
331 if (delegation->flags & NFS_DELEGATION_NEED_RECLAIM)
336 static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
338 switch (open_flags) {
345 case FMODE_READ|FMODE_WRITE:
348 nfs4_state_set_mode_locked(state, state->state | open_flags);
351 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
353 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
354 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
355 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
356 switch (open_flags) {
358 set_bit(NFS_O_RDONLY_STATE, &state->flags);
361 set_bit(NFS_O_WRONLY_STATE, &state->flags);
363 case FMODE_READ|FMODE_WRITE:
364 set_bit(NFS_O_RDWR_STATE, &state->flags);
368 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
370 spin_lock(&state->owner->so_lock);
371 spin_lock(&state->inode->i_lock);
372 nfs_set_open_stateid_locked(state, stateid, open_flags);
373 spin_unlock(&state->inode->i_lock);
374 spin_unlock(&state->owner->so_lock);
377 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *deleg_stateid, int open_flags)
379 struct inode *inode = state->inode;
381 open_flags &= (FMODE_READ|FMODE_WRITE);
382 /* Protect against nfs4_find_state_byowner() */
383 spin_lock(&state->owner->so_lock);
384 spin_lock(&inode->i_lock);
385 if (deleg_stateid != NULL) {
386 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
387 set_bit(NFS_DELEGATED_STATE, &state->flags);
389 if (open_stateid != NULL)
390 nfs_set_open_stateid_locked(state, open_stateid, open_flags);
391 update_open_stateflags(state, open_flags);
392 spin_unlock(&inode->i_lock);
393 spin_unlock(&state->owner->so_lock);
396 static void nfs4_return_incompatible_delegation(struct inode *inode, mode_t open_flags)
398 struct nfs_delegation *delegation;
401 delegation = rcu_dereference(NFS_I(inode)->delegation);
402 if (delegation == NULL || (delegation->type & open_flags) == open_flags) {
407 nfs_inode_return_delegation(inode);
410 static struct nfs4_state *nfs4_try_open_delegated(struct nfs4_opendata *opendata)
412 struct nfs4_state *state = opendata->state;
413 struct nfs_inode *nfsi = NFS_I(state->inode);
414 struct nfs_delegation *delegation;
415 int open_mode = opendata->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL);
416 nfs4_stateid stateid;
420 delegation = rcu_dereference(nfsi->delegation);
421 if (delegation == NULL)
424 if (!can_open_delegated(delegation, open_mode))
426 /* Save the delegation */
427 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
430 ret = _nfs4_do_access(state->inode, state->owner->so_cred, open_mode);
436 delegation = rcu_dereference(nfsi->delegation);
437 if (delegation == NULL)
439 /* Is the delegation still valid? */
440 if (memcmp(stateid.data, delegation->stateid.data, sizeof(stateid.data)) != 0)
443 update_open_stateid(state, NULL, &stateid, open_mode);
444 goto out_return_state;
451 atomic_inc(&state->count);
455 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
458 struct nfs4_state *state = NULL;
459 struct nfs_delegation *delegation;
460 nfs4_stateid *deleg_stateid = NULL;
463 if (!data->rpc_done) {
464 state = nfs4_try_open_delegated(data);
469 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
471 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
472 ret = PTR_ERR(inode);
476 state = nfs4_get_open_state(inode, data->owner);
479 if (data->o_res.delegation_type != 0) {
480 int delegation_flags = 0;
483 delegation = rcu_dereference(NFS_I(inode)->delegation);
485 delegation_flags = delegation->flags;
487 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
488 nfs_inode_set_delegation(state->inode,
489 data->owner->so_cred,
492 nfs_inode_reclaim_delegation(state->inode,
493 data->owner->so_cred,
497 delegation = rcu_dereference(NFS_I(inode)->delegation);
498 if (delegation != NULL)
499 deleg_stateid = &delegation->stateid;
500 update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags);
511 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
513 struct nfs_inode *nfsi = NFS_I(state->inode);
514 struct nfs_open_context *ctx;
516 spin_lock(&state->inode->i_lock);
517 list_for_each_entry(ctx, &nfsi->open_files, list) {
518 if (ctx->state != state)
520 get_nfs_open_context(ctx);
521 spin_unlock(&state->inode->i_lock);
524 spin_unlock(&state->inode->i_lock);
525 return ERR_PTR(-ENOENT);
528 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
530 struct nfs4_state *newstate;
533 opendata->o_arg.open_flags = openflags;
534 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
535 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
536 nfs4_init_opendata_res(opendata);
537 ret = _nfs4_proc_open(opendata);
540 newstate = nfs4_opendata_to_nfs4_state(opendata);
541 if (IS_ERR(newstate))
542 return PTR_ERR(newstate);
543 nfs4_close_state(&opendata->path, newstate, openflags);
548 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
550 struct nfs4_state *newstate;
553 /* memory barrier prior to reading state->n_* */
554 clear_bit(NFS_DELEGATED_STATE, &state->flags);
556 if (state->n_rdwr != 0) {
557 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
560 if (newstate != state)
563 if (state->n_wronly != 0) {
564 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
567 if (newstate != state)
570 if (state->n_rdonly != 0) {
571 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
574 if (newstate != state)
582 * reclaim state on the server after a reboot.
584 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
586 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
587 struct nfs4_opendata *opendata;
588 int delegation_type = 0;
591 if (delegation != NULL) {
592 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
593 memcpy(&state->stateid, &delegation->stateid,
594 sizeof(state->stateid));
595 set_bit(NFS_DELEGATED_STATE, &state->flags);
598 delegation_type = delegation->type;
600 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
601 if (opendata == NULL)
603 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
604 opendata->o_arg.fh = NFS_FH(state->inode);
605 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
606 opendata->o_arg.u.delegation_type = delegation_type;
607 status = nfs4_open_recover(opendata, state);
608 nfs4_opendata_put(opendata);
612 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
614 struct nfs_server *server = NFS_SERVER(state->inode);
615 struct nfs4_exception exception = { };
618 err = _nfs4_do_open_reclaim(ctx, state);
619 if (err != -NFS4ERR_DELAY)
621 nfs4_handle_exception(server, err, &exception);
622 } while (exception.retry);
626 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
628 struct nfs_open_context *ctx;
631 ctx = nfs4_state_find_open_context(state);
634 ret = nfs4_do_open_reclaim(ctx, state);
635 put_nfs_open_context(ctx);
639 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
641 struct nfs4_state_owner *sp = state->owner;
642 struct nfs4_opendata *opendata;
645 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
646 if (opendata == NULL)
648 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
649 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
650 sizeof(opendata->o_arg.u.delegation.data));
651 ret = nfs4_open_recover(opendata, state);
652 nfs4_opendata_put(opendata);
656 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
658 struct nfs4_exception exception = { };
659 struct nfs_server *server = NFS_SERVER(state->inode);
662 err = _nfs4_open_delegation_recall(ctx, state, stateid);
666 case -NFS4ERR_STALE_CLIENTID:
667 case -NFS4ERR_STALE_STATEID:
668 case -NFS4ERR_EXPIRED:
669 /* Don't recall a delegation if it was lost */
670 nfs4_schedule_state_recovery(server->nfs_client);
673 err = nfs4_handle_exception(server, err, &exception);
674 } while (exception.retry);
678 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
680 struct nfs4_opendata *data = calldata;
681 struct rpc_message msg = {
682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
683 .rpc_argp = &data->c_arg,
684 .rpc_resp = &data->c_res,
685 .rpc_cred = data->owner->so_cred,
687 data->timestamp = jiffies;
688 rpc_call_setup(task, &msg, 0);
691 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
693 struct nfs4_opendata *data = calldata;
695 data->rpc_status = task->tk_status;
696 if (RPC_ASSASSINATED(task))
698 if (data->rpc_status == 0) {
699 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
700 sizeof(data->o_res.stateid.data));
701 renew_lease(data->o_res.server, data->timestamp);
704 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
705 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
708 static void nfs4_open_confirm_release(void *calldata)
710 struct nfs4_opendata *data = calldata;
711 struct nfs4_state *state = NULL;
713 /* If this request hasn't been cancelled, do nothing */
714 if (data->cancelled == 0)
716 /* In case of error, no cleanup! */
719 nfs_confirm_seqid(&data->owner->so_seqid, 0);
720 state = nfs4_opendata_to_nfs4_state(data);
722 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
724 nfs4_opendata_put(data);
727 static const struct rpc_call_ops nfs4_open_confirm_ops = {
728 .rpc_call_prepare = nfs4_open_confirm_prepare,
729 .rpc_call_done = nfs4_open_confirm_done,
730 .rpc_release = nfs4_open_confirm_release,
734 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
736 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
738 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
739 struct rpc_task *task;
742 kref_get(&data->kref);
744 data->rpc_status = 0;
745 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
747 return PTR_ERR(task);
748 status = nfs4_wait_for_completion_rpc_task(task);
753 status = data->rpc_status;
758 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
760 struct nfs4_opendata *data = calldata;
761 struct nfs4_state_owner *sp = data->owner;
762 struct rpc_message msg = {
763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
764 .rpc_argp = &data->o_arg,
765 .rpc_resp = &data->o_res,
766 .rpc_cred = sp->so_cred,
769 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
772 * Check if we still need to send an OPEN call, or if we can use
773 * a delegation instead.
775 if (data->state != NULL) {
776 struct nfs_delegation *delegation;
779 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
780 if (delegation != NULL &&
781 (delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0) {
783 task->tk_action = NULL;
788 /* Update sequence id. */
789 data->o_arg.id = sp->so_owner_id.id;
790 data->o_arg.clientid = sp->so_client->cl_clientid;
791 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
792 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
793 data->timestamp = jiffies;
794 rpc_call_setup(task, &msg, 0);
797 static void nfs4_open_done(struct rpc_task *task, void *calldata)
799 struct nfs4_opendata *data = calldata;
801 data->rpc_status = task->tk_status;
802 if (RPC_ASSASSINATED(task))
804 if (task->tk_status == 0) {
805 switch (data->o_res.f_attr->mode & S_IFMT) {
809 data->rpc_status = -ELOOP;
812 data->rpc_status = -EISDIR;
815 data->rpc_status = -ENOTDIR;
817 renew_lease(data->o_res.server, data->timestamp);
818 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
819 nfs_confirm_seqid(&data->owner->so_seqid, 0);
821 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
825 static void nfs4_open_release(void *calldata)
827 struct nfs4_opendata *data = calldata;
828 struct nfs4_state *state = NULL;
830 /* If this request hasn't been cancelled, do nothing */
831 if (data->cancelled == 0)
833 /* In case of error, no cleanup! */
834 if (data->rpc_status != 0 || !data->rpc_done)
836 /* In case we need an open_confirm, no cleanup! */
837 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
839 nfs_confirm_seqid(&data->owner->so_seqid, 0);
840 state = nfs4_opendata_to_nfs4_state(data);
842 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
844 nfs4_opendata_put(data);
847 static const struct rpc_call_ops nfs4_open_ops = {
848 .rpc_call_prepare = nfs4_open_prepare,
849 .rpc_call_done = nfs4_open_done,
850 .rpc_release = nfs4_open_release,
854 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
856 static int _nfs4_proc_open(struct nfs4_opendata *data)
858 struct inode *dir = data->dir->d_inode;
859 struct nfs_server *server = NFS_SERVER(dir);
860 struct nfs_openargs *o_arg = &data->o_arg;
861 struct nfs_openres *o_res = &data->o_res;
862 struct rpc_task *task;
865 kref_get(&data->kref);
867 data->rpc_status = 0;
869 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
871 return PTR_ERR(task);
872 status = nfs4_wait_for_completion_rpc_task(task);
877 status = data->rpc_status;
879 if (status != 0 || !data->rpc_done)
882 if (o_arg->open_flags & O_CREAT) {
883 update_changeattr(dir, &o_res->cinfo);
884 nfs_post_op_update_inode(dir, o_res->dir_attr);
886 nfs_refresh_inode(dir, o_res->dir_attr);
887 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
888 status = _nfs4_proc_open_confirm(data);
892 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
893 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
897 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
899 struct nfs_access_entry cache;
903 if (openflags & FMODE_READ)
905 if (openflags & FMODE_WRITE)
907 if (openflags & FMODE_EXEC)
909 status = nfs_access_get_cached(inode, cred, &cache);
913 /* Be clever: ask server to check for all possible rights */
914 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
916 cache.jiffies = jiffies;
917 status = _nfs4_proc_access(inode, &cache);
920 nfs_access_add_cache(inode, &cache);
922 if ((cache.mask & mask) == mask)
927 static int nfs4_recover_expired_lease(struct nfs_server *server)
929 struct nfs_client *clp = server->nfs_client;
933 ret = nfs4_wait_clnt_recover(server->client, clp);
936 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
938 nfs4_schedule_state_recovery(clp);
945 * reclaim state on the server after a network partition.
946 * Assumes caller holds the appropriate lock
948 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
950 struct inode *inode = state->inode;
951 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
952 struct nfs4_opendata *opendata;
953 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
956 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
957 ret = _nfs4_do_access(inode, ctx->cred, openflags);
960 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
961 set_bit(NFS_DELEGATED_STATE, &state->flags);
964 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
965 if (opendata == NULL)
967 ret = nfs4_open_recover(opendata, state);
968 if (ret == -ESTALE) {
969 /* Invalidate the state owner so we don't ever use it again */
970 nfs4_drop_state_owner(state->owner);
971 d_drop(ctx->path.dentry);
973 nfs4_opendata_put(opendata);
977 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
979 struct nfs_server *server = NFS_SERVER(state->inode);
980 struct nfs4_exception exception = { };
984 err = _nfs4_open_expired(ctx, state);
985 if (err == -NFS4ERR_DELAY)
986 nfs4_handle_exception(server, err, &exception);
987 } while (exception.retry);
991 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
993 struct nfs_open_context *ctx;
996 ctx = nfs4_state_find_open_context(state);
999 ret = nfs4_do_open_expired(ctx, state);
1000 put_nfs_open_context(ctx);
1005 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1006 * fields corresponding to attributes that were used to store the verifier.
1007 * Make sure we clobber those fields in the later setattr call
1009 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1011 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1012 !(sattr->ia_valid & ATTR_ATIME_SET))
1013 sattr->ia_valid |= ATTR_ATIME;
1015 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1016 !(sattr->ia_valid & ATTR_MTIME_SET))
1017 sattr->ia_valid |= ATTR_MTIME;
1021 * Returns a referenced nfs4_state
1023 static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1025 struct nfs4_state_owner *sp;
1026 struct nfs4_state *state = NULL;
1027 struct nfs_server *server = NFS_SERVER(dir);
1028 struct nfs_client *clp = server->nfs_client;
1029 struct nfs4_opendata *opendata;
1032 /* Protect against reboot recovery conflicts */
1034 if (!(sp = nfs4_get_state_owner(server, cred))) {
1035 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1038 status = nfs4_recover_expired_lease(server);
1040 goto err_put_state_owner;
1041 if (path->dentry->d_inode != NULL)
1042 nfs4_return_incompatible_delegation(path->dentry->d_inode, flags & (FMODE_READ|FMODE_WRITE));
1043 down_read(&clp->cl_sem);
1045 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
1046 if (opendata == NULL)
1047 goto err_release_rwsem;
1049 if (path->dentry->d_inode != NULL)
1050 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1052 status = _nfs4_proc_open(opendata);
1054 goto err_opendata_put;
1056 if (opendata->o_arg.open_flags & O_EXCL)
1057 nfs4_exclusive_attrset(opendata, sattr);
1059 state = nfs4_opendata_to_nfs4_state(opendata);
1060 status = PTR_ERR(state);
1062 goto err_opendata_put;
1063 nfs4_opendata_put(opendata);
1064 nfs4_put_state_owner(sp);
1065 up_read(&clp->cl_sem);
1069 nfs4_opendata_put(opendata);
1071 up_read(&clp->cl_sem);
1072 err_put_state_owner:
1073 nfs4_put_state_owner(sp);
1080 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
1082 struct nfs4_exception exception = { };
1083 struct nfs4_state *res;
1087 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
1090 /* NOTE: BAD_SEQID means the server and client disagree about the
1091 * book-keeping w.r.t. state-changing operations
1092 * (OPEN/CLOSE/LOCK/LOCKU...)
1093 * It is actually a sign of a bug on the client or on the server.
1095 * If we receive a BAD_SEQID error in the particular case of
1096 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1097 * have unhashed the old state_owner for us, and that we can
1098 * therefore safely retry using a new one. We should still warn
1099 * the user though...
1101 if (status == -NFS4ERR_BAD_SEQID) {
1102 printk(KERN_WARNING "NFS: v4 server %s "
1103 " returned a bad sequence-id error!\n",
1104 NFS_SERVER(dir)->nfs_client->cl_hostname);
1105 exception.retry = 1;
1109 * BAD_STATEID on OPEN means that the server cancelled our
1110 * state before it received the OPEN_CONFIRM.
1111 * Recover by retrying the request as per the discussion
1112 * on Page 181 of RFC3530.
1114 if (status == -NFS4ERR_BAD_STATEID) {
1115 exception.retry = 1;
1118 if (status == -EAGAIN) {
1119 /* We must have found a delegation */
1120 exception.retry = 1;
1123 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1124 status, &exception));
1125 } while (exception.retry);
1129 static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1130 struct iattr *sattr, struct nfs4_state *state)
1132 struct nfs_server *server = NFS_SERVER(inode);
1133 struct nfs_setattrargs arg = {
1134 .fh = NFS_FH(inode),
1137 .bitmask = server->attr_bitmask,
1139 struct nfs_setattrres res = {
1143 struct rpc_message msg = {
1144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1148 unsigned long timestamp = jiffies;
1151 nfs_fattr_init(fattr);
1153 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1154 /* Use that stateid */
1155 } else if (state != NULL) {
1156 msg.rpc_cred = state->owner->so_cred;
1157 nfs4_copy_stateid(&arg.stateid, state, current->files);
1159 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1161 status = rpc_call_sync(server->client, &msg, 0);
1162 if (status == 0 && state != NULL)
1163 renew_lease(server, timestamp);
1167 static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1168 struct iattr *sattr, struct nfs4_state *state)
1170 struct nfs_server *server = NFS_SERVER(inode);
1171 struct nfs4_exception exception = { };
1174 err = nfs4_handle_exception(server,
1175 _nfs4_do_setattr(inode, fattr, sattr, state),
1177 } while (exception.retry);
1181 struct nfs4_closedata {
1183 struct inode *inode;
1184 struct nfs4_state *state;
1185 struct nfs_closeargs arg;
1186 struct nfs_closeres res;
1187 struct nfs_fattr fattr;
1188 unsigned long timestamp;
1191 static void nfs4_free_closedata(void *data)
1193 struct nfs4_closedata *calldata = data;
1194 struct nfs4_state_owner *sp = calldata->state->owner;
1196 nfs4_put_open_state(calldata->state);
1197 nfs_free_seqid(calldata->arg.seqid);
1198 nfs4_put_state_owner(sp);
1199 dput(calldata->path.dentry);
1200 mntput(calldata->path.mnt);
1204 static void nfs4_close_done(struct rpc_task *task, void *data)
1206 struct nfs4_closedata *calldata = data;
1207 struct nfs4_state *state = calldata->state;
1208 struct nfs_server *server = NFS_SERVER(calldata->inode);
1210 if (RPC_ASSASSINATED(task))
1212 /* hmm. we are done with the inode, and in the process of freeing
1213 * the state_owner. we keep this around to process errors
1215 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1216 switch (task->tk_status) {
1218 nfs_set_open_stateid(state, &calldata->res.stateid, calldata->arg.open_flags);
1219 renew_lease(server, calldata->timestamp);
1221 case -NFS4ERR_STALE_STATEID:
1222 case -NFS4ERR_EXPIRED:
1225 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1226 rpc_restart_call(task);
1230 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1233 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1235 struct nfs4_closedata *calldata = data;
1236 struct nfs4_state *state = calldata->state;
1237 struct rpc_message msg = {
1238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1239 .rpc_argp = &calldata->arg,
1240 .rpc_resp = &calldata->res,
1241 .rpc_cred = state->owner->so_cred,
1243 int clear_rd, clear_wr, clear_rdwr;
1246 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1249 mode = FMODE_READ|FMODE_WRITE;
1250 clear_rd = clear_wr = clear_rdwr = 0;
1251 spin_lock(&state->owner->so_lock);
1252 spin_lock(&calldata->inode->i_lock);
1253 /* Calculate the change in open mode */
1254 if (state->n_rdwr == 0) {
1255 if (state->n_rdonly == 0) {
1256 mode &= ~FMODE_READ;
1257 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1258 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1260 if (state->n_wronly == 0) {
1261 mode &= ~FMODE_WRITE;
1262 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1263 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1266 spin_unlock(&calldata->inode->i_lock);
1267 spin_unlock(&state->owner->so_lock);
1268 if (!clear_rd && !clear_wr && !clear_rdwr) {
1269 /* Note: exit _without_ calling nfs4_close_done */
1270 task->tk_action = NULL;
1273 nfs_fattr_init(calldata->res.fattr);
1275 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1276 calldata->arg.open_flags = mode;
1277 calldata->timestamp = jiffies;
1278 rpc_call_setup(task, &msg, 0);
1281 static const struct rpc_call_ops nfs4_close_ops = {
1282 .rpc_call_prepare = nfs4_close_prepare,
1283 .rpc_call_done = nfs4_close_done,
1284 .rpc_release = nfs4_free_closedata,
1288 * It is possible for data to be read/written from a mem-mapped file
1289 * after the sys_close call (which hits the vfs layer as a flush).
1290 * This means that we can't safely call nfsv4 close on a file until
1291 * the inode is cleared. This in turn means that we are not good
1292 * NFSv4 citizens - we do not indicate to the server to update the file's
1293 * share state even when we are done with one of the three share
1294 * stateid's in the inode.
1296 * NOTE: Caller must be holding the sp->so_owner semaphore!
1298 int nfs4_do_close(struct path *path, struct nfs4_state *state)
1300 struct nfs_server *server = NFS_SERVER(state->inode);
1301 struct nfs4_closedata *calldata;
1302 struct nfs4_state_owner *sp = state->owner;
1303 struct rpc_task *task;
1304 int status = -ENOMEM;
1306 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1307 if (calldata == NULL)
1309 calldata->inode = state->inode;
1310 calldata->state = state;
1311 calldata->arg.fh = NFS_FH(state->inode);
1312 calldata->arg.stateid = &state->open_stateid;
1313 /* Serialization for the sequence id */
1314 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1315 if (calldata->arg.seqid == NULL)
1316 goto out_free_calldata;
1317 calldata->arg.bitmask = server->attr_bitmask;
1318 calldata->res.fattr = &calldata->fattr;
1319 calldata->res.server = server;
1320 calldata->path.mnt = mntget(path->mnt);
1321 calldata->path.dentry = dget(path->dentry);
1323 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1325 return PTR_ERR(task);
1331 nfs4_put_open_state(state);
1332 nfs4_put_state_owner(sp);
1336 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
1341 /* If the open_intent is for execute, we have an extra check to make */
1342 if (nd->intent.open.flags & FMODE_EXEC) {
1343 ret = _nfs4_do_access(state->inode,
1344 state->owner->so_cred,
1345 nd->intent.open.flags);
1349 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1350 if (!IS_ERR(filp)) {
1351 struct nfs_open_context *ctx;
1352 ctx = (struct nfs_open_context *)filp->private_data;
1356 ret = PTR_ERR(filp);
1358 nfs4_close_state(path, state, nd->intent.open.flags);
1363 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1365 struct path path = {
1370 struct rpc_cred *cred;
1371 struct nfs4_state *state;
1374 if (nd->flags & LOOKUP_CREATE) {
1375 attr.ia_mode = nd->intent.open.create_mode;
1376 attr.ia_valid = ATTR_MODE;
1377 if (!IS_POSIXACL(dir))
1378 attr.ia_mode &= ~current->fs->umask;
1381 BUG_ON(nd->intent.open.flags & O_CREAT);
1384 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1386 return (struct dentry *)cred;
1387 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
1389 if (IS_ERR(state)) {
1390 if (PTR_ERR(state) == -ENOENT)
1391 d_add(dentry, NULL);
1392 return (struct dentry *)state;
1394 res = d_add_unique(dentry, igrab(state->inode));
1397 nfs4_intent_set_file(nd, &path, state);
1402 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1404 struct path path = {
1408 struct rpc_cred *cred;
1409 struct nfs4_state *state;
1411 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1413 return PTR_ERR(cred);
1414 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
1416 if (IS_ERR(state)) {
1417 switch (PTR_ERR(state)) {
1423 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1429 if (state->inode == dentry->d_inode) {
1430 nfs4_intent_set_file(nd, &path, state);
1433 nfs4_close_state(&path, state, openflags);
1440 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1442 struct nfs4_server_caps_res res = {};
1443 struct rpc_message msg = {
1444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1445 .rpc_argp = fhandle,
1450 status = rpc_call_sync(server->client, &msg, 0);
1452 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1453 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1454 server->caps |= NFS_CAP_ACLS;
1455 if (res.has_links != 0)
1456 server->caps |= NFS_CAP_HARDLINKS;
1457 if (res.has_symlinks != 0)
1458 server->caps |= NFS_CAP_SYMLINKS;
1459 server->acl_bitmask = res.acl_bitmask;
1464 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1466 struct nfs4_exception exception = { };
1469 err = nfs4_handle_exception(server,
1470 _nfs4_server_capabilities(server, fhandle),
1472 } while (exception.retry);
1476 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1477 struct nfs_fsinfo *info)
1479 struct nfs4_lookup_root_arg args = {
1480 .bitmask = nfs4_fattr_bitmap,
1482 struct nfs4_lookup_res res = {
1484 .fattr = info->fattr,
1487 struct rpc_message msg = {
1488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1492 nfs_fattr_init(info->fattr);
1493 return rpc_call_sync(server->client, &msg, 0);
1496 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1497 struct nfs_fsinfo *info)
1499 struct nfs4_exception exception = { };
1502 err = nfs4_handle_exception(server,
1503 _nfs4_lookup_root(server, fhandle, info),
1505 } while (exception.retry);
1510 * get the file handle for the "/" directory on the server
1512 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1513 struct nfs_fsinfo *info)
1517 status = nfs4_lookup_root(server, fhandle, info);
1519 status = nfs4_server_capabilities(server, fhandle);
1521 status = nfs4_do_fsinfo(server, fhandle, info);
1522 return nfs4_map_errors(status);
1526 * Get locations and (maybe) other attributes of a referral.
1527 * Note that we'll actually follow the referral later when
1528 * we detect fsid mismatch in inode revalidation
1530 static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1532 int status = -ENOMEM;
1533 struct page *page = NULL;
1534 struct nfs4_fs_locations *locations = NULL;
1536 page = alloc_page(GFP_KERNEL);
1539 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1540 if (locations == NULL)
1543 status = nfs4_proc_fs_locations(dir, name, locations, page);
1546 /* Make sure server returned a different fsid for the referral */
1547 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1548 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1553 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1554 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1556 fattr->mode = S_IFDIR;
1557 memset(fhandle, 0, sizeof(struct nfs_fh));
1566 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1568 struct nfs4_getattr_arg args = {
1570 .bitmask = server->attr_bitmask,
1572 struct nfs4_getattr_res res = {
1576 struct rpc_message msg = {
1577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1582 nfs_fattr_init(fattr);
1583 return rpc_call_sync(server->client, &msg, 0);
1586 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1588 struct nfs4_exception exception = { };
1591 err = nfs4_handle_exception(server,
1592 _nfs4_proc_getattr(server, fhandle, fattr),
1594 } while (exception.retry);
1599 * The file is not closed if it is opened due to the a request to change
1600 * the size of the file. The open call will not be needed once the
1601 * VFS layer lookup-intents are implemented.
1603 * Close is called when the inode is destroyed.
1604 * If we haven't opened the file for O_WRONLY, we
1605 * need to in the size_change case to obtain a stateid.
1608 * Because OPEN is always done by name in nfsv4, it is
1609 * possible that we opened a different file by the same
1610 * name. We can recognize this race condition, but we
1611 * can't do anything about it besides returning an error.
1613 * This will be fixed with VFS changes (lookup-intent).
1616 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1617 struct iattr *sattr)
1619 struct rpc_cred *cred;
1620 struct inode *inode = dentry->d_inode;
1621 struct nfs_open_context *ctx;
1622 struct nfs4_state *state = NULL;
1625 nfs_fattr_init(fattr);
1627 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1629 return PTR_ERR(cred);
1631 /* Search for an existing open(O_WRITE) file */
1632 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1636 status = nfs4_do_setattr(inode, fattr, sattr, state);
1638 nfs_setattr_update_inode(inode, sattr);
1640 put_nfs_open_context(ctx);
1645 static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1646 struct qstr *name, struct nfs_fh *fhandle,
1647 struct nfs_fattr *fattr)
1650 struct nfs4_lookup_arg args = {
1651 .bitmask = server->attr_bitmask,
1655 struct nfs4_lookup_res res = {
1660 struct rpc_message msg = {
1661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1666 nfs_fattr_init(fattr);
1668 dprintk("NFS call lookupfh %s\n", name->name);
1669 status = rpc_call_sync(server->client, &msg, 0);
1670 dprintk("NFS reply lookupfh: %d\n", status);
1674 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1675 struct qstr *name, struct nfs_fh *fhandle,
1676 struct nfs_fattr *fattr)
1678 struct nfs4_exception exception = { };
1681 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1683 if (err == -NFS4ERR_MOVED) {
1687 err = nfs4_handle_exception(server, err, &exception);
1688 } while (exception.retry);
1692 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1693 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1697 dprintk("NFS call lookup %s\n", name->name);
1698 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
1699 if (status == -NFS4ERR_MOVED)
1700 status = nfs4_get_referral(dir, name, fattr, fhandle);
1701 dprintk("NFS reply lookup: %d\n", status);
1705 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1707 struct nfs4_exception exception = { };
1710 err = nfs4_handle_exception(NFS_SERVER(dir),
1711 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1713 } while (exception.retry);
1717 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1719 struct nfs4_accessargs args = {
1720 .fh = NFS_FH(inode),
1722 struct nfs4_accessres res = { 0 };
1723 struct rpc_message msg = {
1724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1727 .rpc_cred = entry->cred,
1729 int mode = entry->mask;
1733 * Determine which access bits we want to ask for...
1735 if (mode & MAY_READ)
1736 args.access |= NFS4_ACCESS_READ;
1737 if (S_ISDIR(inode->i_mode)) {
1738 if (mode & MAY_WRITE)
1739 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1740 if (mode & MAY_EXEC)
1741 args.access |= NFS4_ACCESS_LOOKUP;
1743 if (mode & MAY_WRITE)
1744 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1745 if (mode & MAY_EXEC)
1746 args.access |= NFS4_ACCESS_EXECUTE;
1748 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1751 if (res.access & NFS4_ACCESS_READ)
1752 entry->mask |= MAY_READ;
1753 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1754 entry->mask |= MAY_WRITE;
1755 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1756 entry->mask |= MAY_EXEC;
1761 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1763 struct nfs4_exception exception = { };
1766 err = nfs4_handle_exception(NFS_SERVER(inode),
1767 _nfs4_proc_access(inode, entry),
1769 } while (exception.retry);
1774 * TODO: For the time being, we don't try to get any attributes
1775 * along with any of the zero-copy operations READ, READDIR,
1778 * In the case of the first three, we want to put the GETATTR
1779 * after the read-type operation -- this is because it is hard
1780 * to predict the length of a GETATTR response in v4, and thus
1781 * align the READ data correctly. This means that the GETATTR
1782 * may end up partially falling into the page cache, and we should
1783 * shift it into the 'tail' of the xdr_buf before processing.
1784 * To do this efficiently, we need to know the total length
1785 * of data received, which doesn't seem to be available outside
1788 * In the case of WRITE, we also want to put the GETATTR after
1789 * the operation -- in this case because we want to make sure
1790 * we get the post-operation mtime and size. This means that
1791 * we can't use xdr_encode_pages() as written: we need a variant
1792 * of it which would leave room in the 'tail' iovec.
1794 * Both of these changes to the XDR layer would in fact be quite
1795 * minor, but I decided to leave them for a subsequent patch.
1797 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1798 unsigned int pgbase, unsigned int pglen)
1800 struct nfs4_readlink args = {
1801 .fh = NFS_FH(inode),
1806 struct rpc_message msg = {
1807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1812 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1815 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1816 unsigned int pgbase, unsigned int pglen)
1818 struct nfs4_exception exception = { };
1821 err = nfs4_handle_exception(NFS_SERVER(inode),
1822 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1824 } while (exception.retry);
1830 * We will need to arrange for the VFS layer to provide an atomic open.
1831 * Until then, this create/open method is prone to inefficiency and race
1832 * conditions due to the lookup, create, and open VFS calls from sys_open()
1833 * placed on the wire.
1835 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1836 * The file will be opened again in the subsequent VFS open call
1837 * (nfs4_proc_file_open).
1839 * The open for read will just hang around to be used by any process that
1840 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1844 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1845 int flags, struct nameidata *nd)
1847 struct path path = {
1851 struct nfs4_state *state;
1852 struct rpc_cred *cred;
1855 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1857 status = PTR_ERR(cred);
1860 state = nfs4_do_open(dir, &path, flags, sattr, cred);
1862 if (IS_ERR(state)) {
1863 status = PTR_ERR(state);
1866 d_instantiate(dentry, igrab(state->inode));
1867 if (flags & O_EXCL) {
1868 struct nfs_fattr fattr;
1869 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
1871 nfs_setattr_update_inode(state->inode, sattr);
1872 nfs_post_op_update_inode(state->inode, &fattr);
1874 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
1875 status = nfs4_intent_set_file(nd, &path, state);
1877 nfs4_close_state(&path, state, flags);
1882 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1884 struct nfs_server *server = NFS_SERVER(dir);
1885 struct nfs4_remove_arg args = {
1888 .bitmask = server->attr_bitmask,
1890 struct nfs_fattr dir_attr;
1891 struct nfs4_remove_res res = {
1893 .dir_attr = &dir_attr,
1895 struct rpc_message msg = {
1896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1902 nfs_fattr_init(res.dir_attr);
1903 status = rpc_call_sync(server->client, &msg, 0);
1905 update_changeattr(dir, &res.cinfo);
1906 nfs_post_op_update_inode(dir, res.dir_attr);
1911 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1913 struct nfs4_exception exception = { };
1916 err = nfs4_handle_exception(NFS_SERVER(dir),
1917 _nfs4_proc_remove(dir, name),
1919 } while (exception.retry);
1923 struct unlink_desc {
1924 struct nfs4_remove_arg args;
1925 struct nfs4_remove_res res;
1926 struct nfs_fattr dir_attr;
1929 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1932 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1933 struct unlink_desc *up;
1935 up = kmalloc(sizeof(*up), GFP_KERNEL);
1939 up->args.fh = NFS_FH(dir->d_inode);
1940 up->args.name = name;
1941 up->args.bitmask = server->attr_bitmask;
1942 up->res.server = server;
1943 up->res.dir_attr = &up->dir_attr;
1945 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1946 msg->rpc_argp = &up->args;
1947 msg->rpc_resp = &up->res;
1951 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1953 struct rpc_message *msg = &task->tk_msg;
1954 struct unlink_desc *up;
1956 if (msg->rpc_resp != NULL) {
1957 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1958 update_changeattr(dir->d_inode, &up->res.cinfo);
1959 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1961 msg->rpc_resp = NULL;
1962 msg->rpc_argp = NULL;
1967 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1968 struct inode *new_dir, struct qstr *new_name)
1970 struct nfs_server *server = NFS_SERVER(old_dir);
1971 struct nfs4_rename_arg arg = {
1972 .old_dir = NFS_FH(old_dir),
1973 .new_dir = NFS_FH(new_dir),
1974 .old_name = old_name,
1975 .new_name = new_name,
1976 .bitmask = server->attr_bitmask,
1978 struct nfs_fattr old_fattr, new_fattr;
1979 struct nfs4_rename_res res = {
1981 .old_fattr = &old_fattr,
1982 .new_fattr = &new_fattr,
1984 struct rpc_message msg = {
1985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1991 nfs_fattr_init(res.old_fattr);
1992 nfs_fattr_init(res.new_fattr);
1993 status = rpc_call_sync(server->client, &msg, 0);
1996 update_changeattr(old_dir, &res.old_cinfo);
1997 nfs_post_op_update_inode(old_dir, res.old_fattr);
1998 update_changeattr(new_dir, &res.new_cinfo);
1999 nfs_post_op_update_inode(new_dir, res.new_fattr);
2004 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2005 struct inode *new_dir, struct qstr *new_name)
2007 struct nfs4_exception exception = { };
2010 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2011 _nfs4_proc_rename(old_dir, old_name,
2014 } while (exception.retry);
2018 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2020 struct nfs_server *server = NFS_SERVER(inode);
2021 struct nfs4_link_arg arg = {
2022 .fh = NFS_FH(inode),
2023 .dir_fh = NFS_FH(dir),
2025 .bitmask = server->attr_bitmask,
2027 struct nfs_fattr fattr, dir_attr;
2028 struct nfs4_link_res res = {
2031 .dir_attr = &dir_attr,
2033 struct rpc_message msg = {
2034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2040 nfs_fattr_init(res.fattr);
2041 nfs_fattr_init(res.dir_attr);
2042 status = rpc_call_sync(server->client, &msg, 0);
2044 update_changeattr(dir, &res.cinfo);
2045 nfs_post_op_update_inode(dir, res.dir_attr);
2046 nfs_post_op_update_inode(inode, res.fattr);
2052 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2054 struct nfs4_exception exception = { };
2057 err = nfs4_handle_exception(NFS_SERVER(inode),
2058 _nfs4_proc_link(inode, dir, name),
2060 } while (exception.retry);
2064 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2065 struct page *page, unsigned int len, struct iattr *sattr)
2067 struct nfs_server *server = NFS_SERVER(dir);
2068 struct nfs_fh fhandle;
2069 struct nfs_fattr fattr, dir_fattr;
2070 struct nfs4_create_arg arg = {
2071 .dir_fh = NFS_FH(dir),
2073 .name = &dentry->d_name,
2076 .bitmask = server->attr_bitmask,
2078 struct nfs4_create_res res = {
2082 .dir_fattr = &dir_fattr,
2084 struct rpc_message msg = {
2085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2091 if (len > NFS4_MAXPATHLEN)
2092 return -ENAMETOOLONG;
2094 arg.u.symlink.pages = &page;
2095 arg.u.symlink.len = len;
2096 nfs_fattr_init(&fattr);
2097 nfs_fattr_init(&dir_fattr);
2099 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2101 update_changeattr(dir, &res.dir_cinfo);
2102 nfs_post_op_update_inode(dir, res.dir_fattr);
2103 status = nfs_instantiate(dentry, &fhandle, &fattr);
2108 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2109 struct page *page, unsigned int len, struct iattr *sattr)
2111 struct nfs4_exception exception = { };
2114 err = nfs4_handle_exception(NFS_SERVER(dir),
2115 _nfs4_proc_symlink(dir, dentry, page,
2118 } while (exception.retry);
2122 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2123 struct iattr *sattr)
2125 struct nfs_server *server = NFS_SERVER(dir);
2126 struct nfs_fh fhandle;
2127 struct nfs_fattr fattr, dir_fattr;
2128 struct nfs4_create_arg arg = {
2129 .dir_fh = NFS_FH(dir),
2131 .name = &dentry->d_name,
2134 .bitmask = server->attr_bitmask,
2136 struct nfs4_create_res res = {
2140 .dir_fattr = &dir_fattr,
2142 struct rpc_message msg = {
2143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2149 nfs_fattr_init(&fattr);
2150 nfs_fattr_init(&dir_fattr);
2152 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2154 update_changeattr(dir, &res.dir_cinfo);
2155 nfs_post_op_update_inode(dir, res.dir_fattr);
2156 status = nfs_instantiate(dentry, &fhandle, &fattr);
2161 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2162 struct iattr *sattr)
2164 struct nfs4_exception exception = { };
2167 err = nfs4_handle_exception(NFS_SERVER(dir),
2168 _nfs4_proc_mkdir(dir, dentry, sattr),
2170 } while (exception.retry);
2174 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2175 u64 cookie, struct page *page, unsigned int count, int plus)
2177 struct inode *dir = dentry->d_inode;
2178 struct nfs4_readdir_arg args = {
2183 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2185 struct nfs4_readdir_res res;
2186 struct rpc_message msg = {
2187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2194 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2195 dentry->d_parent->d_name.name,
2196 dentry->d_name.name,
2197 (unsigned long long)cookie);
2198 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2199 res.pgbase = args.pgbase;
2200 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2202 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2203 dprintk("%s: returns %d\n", __FUNCTION__, status);
2207 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2208 u64 cookie, struct page *page, unsigned int count, int plus)
2210 struct nfs4_exception exception = { };
2213 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2214 _nfs4_proc_readdir(dentry, cred, cookie,
2217 } while (exception.retry);
2221 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2222 struct iattr *sattr, dev_t rdev)
2224 struct nfs_server *server = NFS_SERVER(dir);
2226 struct nfs_fattr fattr, dir_fattr;
2227 struct nfs4_create_arg arg = {
2228 .dir_fh = NFS_FH(dir),
2230 .name = &dentry->d_name,
2232 .bitmask = server->attr_bitmask,
2234 struct nfs4_create_res res = {
2238 .dir_fattr = &dir_fattr,
2240 struct rpc_message msg = {
2241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2246 int mode = sattr->ia_mode;
2248 nfs_fattr_init(&fattr);
2249 nfs_fattr_init(&dir_fattr);
2251 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2252 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2254 arg.ftype = NF4FIFO;
2255 else if (S_ISBLK(mode)) {
2257 arg.u.device.specdata1 = MAJOR(rdev);
2258 arg.u.device.specdata2 = MINOR(rdev);
2260 else if (S_ISCHR(mode)) {
2262 arg.u.device.specdata1 = MAJOR(rdev);
2263 arg.u.device.specdata2 = MINOR(rdev);
2266 arg.ftype = NF4SOCK;
2268 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2270 update_changeattr(dir, &res.dir_cinfo);
2271 nfs_post_op_update_inode(dir, res.dir_fattr);
2272 status = nfs_instantiate(dentry, &fh, &fattr);
2277 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2278 struct iattr *sattr, dev_t rdev)
2280 struct nfs4_exception exception = { };
2283 err = nfs4_handle_exception(NFS_SERVER(dir),
2284 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2286 } while (exception.retry);
2290 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2291 struct nfs_fsstat *fsstat)
2293 struct nfs4_statfs_arg args = {
2295 .bitmask = server->attr_bitmask,
2297 struct rpc_message msg = {
2298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2303 nfs_fattr_init(fsstat->fattr);
2304 return rpc_call_sync(server->client, &msg, 0);
2307 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2309 struct nfs4_exception exception = { };
2312 err = nfs4_handle_exception(server,
2313 _nfs4_proc_statfs(server, fhandle, fsstat),
2315 } while (exception.retry);
2319 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2320 struct nfs_fsinfo *fsinfo)
2322 struct nfs4_fsinfo_arg args = {
2324 .bitmask = server->attr_bitmask,
2326 struct rpc_message msg = {
2327 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2332 return rpc_call_sync(server->client, &msg, 0);
2335 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2337 struct nfs4_exception exception = { };
2341 err = nfs4_handle_exception(server,
2342 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2344 } while (exception.retry);
2348 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2350 nfs_fattr_init(fsinfo->fattr);
2351 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2354 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2355 struct nfs_pathconf *pathconf)
2357 struct nfs4_pathconf_arg args = {
2359 .bitmask = server->attr_bitmask,
2361 struct rpc_message msg = {
2362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2364 .rpc_resp = pathconf,
2367 /* None of the pathconf attributes are mandatory to implement */
2368 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2369 memset(pathconf, 0, sizeof(*pathconf));
2373 nfs_fattr_init(pathconf->fattr);
2374 return rpc_call_sync(server->client, &msg, 0);
2377 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2378 struct nfs_pathconf *pathconf)
2380 struct nfs4_exception exception = { };
2384 err = nfs4_handle_exception(server,
2385 _nfs4_proc_pathconf(server, fhandle, pathconf),
2387 } while (exception.retry);
2391 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2393 struct nfs_server *server = NFS_SERVER(data->inode);
2395 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
2396 rpc_restart_call(task);
2399 if (task->tk_status > 0)
2400 renew_lease(server, data->timestamp);
2404 static void nfs4_proc_read_setup(struct nfs_read_data *data)
2406 struct rpc_message msg = {
2407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2408 .rpc_argp = &data->args,
2409 .rpc_resp = &data->res,
2410 .rpc_cred = data->cred,
2413 data->timestamp = jiffies;
2415 rpc_call_setup(&data->task, &msg, 0);
2418 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2420 struct inode *inode = data->inode;
2422 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2423 rpc_restart_call(task);
2426 if (task->tk_status >= 0) {
2427 renew_lease(NFS_SERVER(inode), data->timestamp);
2428 nfs_post_op_update_inode(inode, data->res.fattr);
2433 static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2435 struct rpc_message msg = {
2436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2437 .rpc_argp = &data->args,
2438 .rpc_resp = &data->res,
2439 .rpc_cred = data->cred,
2441 struct inode *inode = data->inode;
2442 struct nfs_server *server = NFS_SERVER(inode);
2445 if (how & FLUSH_STABLE) {
2446 if (!NFS_I(inode)->ncommit)
2447 stable = NFS_FILE_SYNC;
2449 stable = NFS_DATA_SYNC;
2451 stable = NFS_UNSTABLE;
2452 data->args.stable = stable;
2453 data->args.bitmask = server->attr_bitmask;
2454 data->res.server = server;
2456 data->timestamp = jiffies;
2458 /* Finalize the task. */
2459 rpc_call_setup(&data->task, &msg, 0);
2462 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2464 struct inode *inode = data->inode;
2466 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2467 rpc_restart_call(task);
2470 if (task->tk_status >= 0)
2471 nfs_post_op_update_inode(inode, data->res.fattr);
2475 static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2477 struct rpc_message msg = {
2478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2479 .rpc_argp = &data->args,
2480 .rpc_resp = &data->res,
2481 .rpc_cred = data->cred,
2483 struct nfs_server *server = NFS_SERVER(data->inode);
2485 data->args.bitmask = server->attr_bitmask;
2486 data->res.server = server;
2488 rpc_call_setup(&data->task, &msg, 0);
2492 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2493 * standalone procedure for queueing an asynchronous RENEW.
2495 static void nfs4_renew_done(struct rpc_task *task, void *data)
2497 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2498 unsigned long timestamp = (unsigned long)data;
2500 if (task->tk_status < 0) {
2501 switch (task->tk_status) {
2502 case -NFS4ERR_STALE_CLIENTID:
2503 case -NFS4ERR_EXPIRED:
2504 case -NFS4ERR_CB_PATH_DOWN:
2505 nfs4_schedule_state_recovery(clp);
2509 spin_lock(&clp->cl_lock);
2510 if (time_before(clp->cl_last_renewal,timestamp))
2511 clp->cl_last_renewal = timestamp;
2512 spin_unlock(&clp->cl_lock);
2515 static const struct rpc_call_ops nfs4_renew_ops = {
2516 .rpc_call_done = nfs4_renew_done,
2519 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2521 struct rpc_message msg = {
2522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2527 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2528 &nfs4_renew_ops, (void *)jiffies);
2531 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2533 struct rpc_message msg = {
2534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2538 unsigned long now = jiffies;
2541 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2544 spin_lock(&clp->cl_lock);
2545 if (time_before(clp->cl_last_renewal,now))
2546 clp->cl_last_renewal = now;
2547 spin_unlock(&clp->cl_lock);
2551 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2553 return (server->caps & NFS_CAP_ACLS)
2554 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2555 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2558 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2559 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2562 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2564 static void buf_to_pages(const void *buf, size_t buflen,
2565 struct page **pages, unsigned int *pgbase)
2567 const void *p = buf;
2569 *pgbase = offset_in_page(buf);
2571 while (p < buf + buflen) {
2572 *(pages++) = virt_to_page(p);
2573 p += PAGE_CACHE_SIZE;
2577 struct nfs4_cached_acl {
2583 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2585 struct nfs_inode *nfsi = NFS_I(inode);
2587 spin_lock(&inode->i_lock);
2588 kfree(nfsi->nfs4_acl);
2589 nfsi->nfs4_acl = acl;
2590 spin_unlock(&inode->i_lock);
2593 static void nfs4_zap_acl_attr(struct inode *inode)
2595 nfs4_set_cached_acl(inode, NULL);
2598 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2600 struct nfs_inode *nfsi = NFS_I(inode);
2601 struct nfs4_cached_acl *acl;
2604 spin_lock(&inode->i_lock);
2605 acl = nfsi->nfs4_acl;
2608 if (buf == NULL) /* user is just asking for length */
2610 if (acl->cached == 0)
2612 ret = -ERANGE; /* see getxattr(2) man page */
2613 if (acl->len > buflen)
2615 memcpy(buf, acl->data, acl->len);
2619 spin_unlock(&inode->i_lock);
2623 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2625 struct nfs4_cached_acl *acl;
2627 if (buf && acl_len <= PAGE_SIZE) {
2628 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2632 memcpy(acl->data, buf, acl_len);
2634 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2641 nfs4_set_cached_acl(inode, acl);
2644 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2646 struct page *pages[NFS4ACL_MAXPAGES];
2647 struct nfs_getaclargs args = {
2648 .fh = NFS_FH(inode),
2652 size_t resp_len = buflen;
2654 struct rpc_message msg = {
2655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2657 .rpc_resp = &resp_len,
2659 struct page *localpage = NULL;
2662 if (buflen < PAGE_SIZE) {
2663 /* As long as we're doing a round trip to the server anyway,
2664 * let's be prepared for a page of acl data. */
2665 localpage = alloc_page(GFP_KERNEL);
2666 resp_buf = page_address(localpage);
2667 if (localpage == NULL)
2669 args.acl_pages[0] = localpage;
2670 args.acl_pgbase = 0;
2671 resp_len = args.acl_len = PAGE_SIZE;
2674 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2676 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2679 if (resp_len > args.acl_len)
2680 nfs4_write_cached_acl(inode, NULL, resp_len);
2682 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2685 if (resp_len > buflen)
2688 memcpy(buf, resp_buf, resp_len);
2693 __free_page(localpage);
2697 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2699 struct nfs4_exception exception = { };
2702 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2705 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2706 } while (exception.retry);
2710 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2712 struct nfs_server *server = NFS_SERVER(inode);
2715 if (!nfs4_server_supports_acls(server))
2717 ret = nfs_revalidate_inode(server, inode);
2720 ret = nfs4_read_cached_acl(inode, buf, buflen);
2723 return nfs4_get_acl_uncached(inode, buf, buflen);
2726 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2728 struct nfs_server *server = NFS_SERVER(inode);
2729 struct page *pages[NFS4ACL_MAXPAGES];
2730 struct nfs_setaclargs arg = {
2731 .fh = NFS_FH(inode),
2735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2742 if (!nfs4_server_supports_acls(server))
2744 nfs_inode_return_delegation(inode);
2745 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2746 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2747 nfs_zap_caches(inode);
2751 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2753 struct nfs4_exception exception = { };
2756 err = nfs4_handle_exception(NFS_SERVER(inode),
2757 __nfs4_proc_set_acl(inode, buf, buflen),
2759 } while (exception.retry);
2764 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2766 struct nfs_client *clp = server->nfs_client;
2768 if (!clp || task->tk_status >= 0)
2770 switch(task->tk_status) {
2771 case -NFS4ERR_STALE_CLIENTID:
2772 case -NFS4ERR_STALE_STATEID:
2773 case -NFS4ERR_EXPIRED:
2774 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2775 nfs4_schedule_state_recovery(clp);
2776 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
2777 rpc_wake_up_task(task);
2778 task->tk_status = 0;
2780 case -NFS4ERR_DELAY:
2781 nfs_inc_server_stats((struct nfs_server *) server,
2783 case -NFS4ERR_GRACE:
2784 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2785 task->tk_status = 0;
2787 case -NFS4ERR_OLD_STATEID:
2788 task->tk_status = 0;
2791 task->tk_status = nfs4_map_errors(task->tk_status);
2795 static int nfs4_wait_bit_interruptible(void *word)
2797 if (signal_pending(current))
2798 return -ERESTARTSYS;
2803 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
2810 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2812 rpc_clnt_sigmask(clnt, &oldset);
2813 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2814 nfs4_wait_bit_interruptible,
2815 TASK_INTERRUPTIBLE);
2816 rpc_clnt_sigunmask(clnt, &oldset);
2818 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
2822 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2830 *timeout = NFS4_POLL_RETRY_MIN;
2831 if (*timeout > NFS4_POLL_RETRY_MAX)
2832 *timeout = NFS4_POLL_RETRY_MAX;
2833 rpc_clnt_sigmask(clnt, &oldset);
2834 if (clnt->cl_intr) {
2835 schedule_timeout_interruptible(*timeout);
2839 schedule_timeout_uninterruptible(*timeout);
2840 rpc_clnt_sigunmask(clnt, &oldset);
2845 /* This is the error handling routine for processes that are allowed
2848 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2850 struct nfs_client *clp = server->nfs_client;
2851 int ret = errorcode;
2853 exception->retry = 0;
2857 case -NFS4ERR_STALE_CLIENTID:
2858 case -NFS4ERR_STALE_STATEID:
2859 case -NFS4ERR_EXPIRED:
2860 nfs4_schedule_state_recovery(clp);
2861 ret = nfs4_wait_clnt_recover(server->client, clp);
2863 exception->retry = 1;
2865 case -NFS4ERR_FILE_OPEN:
2866 case -NFS4ERR_GRACE:
2867 case -NFS4ERR_DELAY:
2868 ret = nfs4_delay(server->client, &exception->timeout);
2871 case -NFS4ERR_OLD_STATEID:
2872 exception->retry = 1;
2874 /* We failed to handle the error */
2875 return nfs4_map_errors(ret);
2878 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
2880 nfs4_verifier sc_verifier;
2881 struct nfs4_setclientid setclientid = {
2882 .sc_verifier = &sc_verifier,
2885 struct rpc_message msg = {
2886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2887 .rpc_argp = &setclientid,
2895 p = (__be32*)sc_verifier.data;
2896 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2897 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2900 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2901 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2902 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
2903 cred->cr_ops->cr_name,
2904 clp->cl_id_uniquifier);
2905 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2906 sizeof(setclientid.sc_netid), "tcp");
2907 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2908 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2909 clp->cl_ipaddr, port >> 8, port & 255);
2911 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2912 if (status != -NFS4ERR_CLID_INUSE)
2917 ssleep(clp->cl_lease_time + 1);
2919 if (++clp->cl_id_uniquifier == 0)
2925 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2927 struct nfs_fsinfo fsinfo;
2928 struct rpc_message msg = {
2929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2931 .rpc_resp = &fsinfo,
2938 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2940 spin_lock(&clp->cl_lock);
2941 clp->cl_lease_time = fsinfo.lease_time * HZ;
2942 clp->cl_last_renewal = now;
2943 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2944 spin_unlock(&clp->cl_lock);
2949 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2954 err = _nfs4_proc_setclientid_confirm(clp, cred);
2958 case -NFS4ERR_RESOURCE:
2959 /* The IBM lawyers misread another document! */
2960 case -NFS4ERR_DELAY:
2961 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2967 struct nfs4_delegreturndata {
2968 struct nfs4_delegreturnargs args;
2969 struct nfs4_delegreturnres res;
2971 nfs4_stateid stateid;
2972 struct rpc_cred *cred;
2973 unsigned long timestamp;
2974 struct nfs_fattr fattr;
2978 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
2980 struct nfs4_delegreturndata *data = calldata;
2981 struct rpc_message msg = {
2982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2983 .rpc_argp = &data->args,
2984 .rpc_resp = &data->res,
2985 .rpc_cred = data->cred,
2987 nfs_fattr_init(data->res.fattr);
2988 rpc_call_setup(task, &msg, 0);
2991 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2993 struct nfs4_delegreturndata *data = calldata;
2994 data->rpc_status = task->tk_status;
2995 if (data->rpc_status == 0)
2996 renew_lease(data->res.server, data->timestamp);
2999 static void nfs4_delegreturn_release(void *calldata)
3001 struct nfs4_delegreturndata *data = calldata;
3003 put_rpccred(data->cred);
3007 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3008 .rpc_call_prepare = nfs4_delegreturn_prepare,
3009 .rpc_call_done = nfs4_delegreturn_done,
3010 .rpc_release = nfs4_delegreturn_release,
3013 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3015 struct nfs4_delegreturndata *data;
3016 struct nfs_server *server = NFS_SERVER(inode);
3017 struct rpc_task *task;
3020 data = kmalloc(sizeof(*data), GFP_KERNEL);
3023 data->args.fhandle = &data->fh;
3024 data->args.stateid = &data->stateid;
3025 data->args.bitmask = server->attr_bitmask;
3026 nfs_copy_fh(&data->fh, NFS_FH(inode));
3027 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3028 data->res.fattr = &data->fattr;
3029 data->res.server = server;
3030 data->cred = get_rpccred(cred);
3031 data->timestamp = jiffies;
3032 data->rpc_status = 0;
3034 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
3036 return PTR_ERR(task);
3037 status = nfs4_wait_for_completion_rpc_task(task);
3039 status = data->rpc_status;
3041 nfs_post_op_update_inode(inode, &data->fattr);
3047 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3049 struct nfs_server *server = NFS_SERVER(inode);
3050 struct nfs4_exception exception = { };
3053 err = _nfs4_proc_delegreturn(inode, cred, stateid);
3055 case -NFS4ERR_STALE_STATEID:
3056 case -NFS4ERR_EXPIRED:
3060 err = nfs4_handle_exception(server, err, &exception);
3061 } while (exception.retry);
3065 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3066 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3069 * sleep, with exponential backoff, and retry the LOCK operation.
3071 static unsigned long
3072 nfs4_set_lock_task_retry(unsigned long timeout)
3074 schedule_timeout_interruptible(timeout);
3076 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3077 return NFS4_LOCK_MAXTIMEOUT;
3081 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3083 struct inode *inode = state->inode;
3084 struct nfs_server *server = NFS_SERVER(inode);
3085 struct nfs_client *clp = server->nfs_client;
3086 struct nfs_lockt_args arg = {
3087 .fh = NFS_FH(inode),
3090 struct nfs_lockt_res res = {
3093 struct rpc_message msg = {
3094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3097 .rpc_cred = state->owner->so_cred,
3099 struct nfs4_lock_state *lsp;
3102 down_read(&clp->cl_sem);
3103 arg.lock_owner.clientid = clp->cl_clientid;
3104 status = nfs4_set_lock_state(state, request);
3107 lsp = request->fl_u.nfs4_fl.owner;
3108 arg.lock_owner.id = lsp->ls_id.id;
3109 status = rpc_call_sync(server->client, &msg, 0);
3112 request->fl_type = F_UNLCK;
3114 case -NFS4ERR_DENIED:
3117 request->fl_ops->fl_release_private(request);
3119 up_read(&clp->cl_sem);
3123 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3125 struct nfs4_exception exception = { };
3129 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3130 _nfs4_proc_getlk(state, cmd, request),
3132 } while (exception.retry);
3136 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3139 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3141 res = posix_lock_file_wait(file, fl);
3144 res = flock_lock_file_wait(file, fl);
3152 struct nfs4_unlockdata {
3153 struct nfs_locku_args arg;
3154 struct nfs_locku_res res;
3155 struct nfs4_lock_state *lsp;
3156 struct nfs_open_context *ctx;
3157 struct file_lock fl;
3158 const struct nfs_server *server;
3159 unsigned long timestamp;
3162 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3163 struct nfs_open_context *ctx,
3164 struct nfs4_lock_state *lsp,
3165 struct nfs_seqid *seqid)
3167 struct nfs4_unlockdata *p;
3168 struct inode *inode = lsp->ls_state->inode;
3170 p = kmalloc(sizeof(*p), GFP_KERNEL);
3173 p->arg.fh = NFS_FH(inode);
3175 p->arg.seqid = seqid;
3176 p->arg.stateid = &lsp->ls_stateid;
3178 atomic_inc(&lsp->ls_count);
3179 /* Ensure we don't close file until we're done freeing locks! */
3180 p->ctx = get_nfs_open_context(ctx);
3181 memcpy(&p->fl, fl, sizeof(p->fl));
3182 p->server = NFS_SERVER(inode);
3186 static void nfs4_locku_release_calldata(void *data)
3188 struct nfs4_unlockdata *calldata = data;
3189 nfs_free_seqid(calldata->arg.seqid);
3190 nfs4_put_lock_state(calldata->lsp);
3191 put_nfs_open_context(calldata->ctx);
3195 static void nfs4_locku_done(struct rpc_task *task, void *data)
3197 struct nfs4_unlockdata *calldata = data;
3199 if (RPC_ASSASSINATED(task))
3201 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
3202 switch (task->tk_status) {
3204 memcpy(calldata->lsp->ls_stateid.data,
3205 calldata->res.stateid.data,
3206 sizeof(calldata->lsp->ls_stateid.data));
3207 renew_lease(calldata->server, calldata->timestamp);
3209 case -NFS4ERR_STALE_STATEID:
3210 case -NFS4ERR_EXPIRED:
3213 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
3214 rpc_restart_call(task);
3218 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3220 struct nfs4_unlockdata *calldata = data;
3221 struct rpc_message msg = {
3222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3223 .rpc_argp = &calldata->arg,
3224 .rpc_resp = &calldata->res,
3225 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
3228 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3230 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3231 /* Note: exit _without_ running nfs4_locku_done */
3232 task->tk_action = NULL;
3235 calldata->timestamp = jiffies;
3236 rpc_call_setup(task, &msg, 0);
3239 static const struct rpc_call_ops nfs4_locku_ops = {
3240 .rpc_call_prepare = nfs4_locku_prepare,
3241 .rpc_call_done = nfs4_locku_done,
3242 .rpc_release = nfs4_locku_release_calldata,
3245 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3246 struct nfs_open_context *ctx,
3247 struct nfs4_lock_state *lsp,
3248 struct nfs_seqid *seqid)
3250 struct nfs4_unlockdata *data;
3252 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3254 nfs_free_seqid(seqid);
3255 return ERR_PTR(-ENOMEM);
3258 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
3261 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3263 struct nfs_seqid *seqid;
3264 struct nfs4_lock_state *lsp;
3265 struct rpc_task *task;
3268 status = nfs4_set_lock_state(state, request);
3269 /* Unlock _before_ we do the RPC call */
3270 request->fl_flags |= FL_EXISTS;
3271 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3275 /* Is this a delegated lock? */
3276 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3278 lsp = request->fl_u.nfs4_fl.owner;
3279 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3283 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3284 status = PTR_ERR(task);
3287 status = nfs4_wait_for_completion_rpc_task(task);
3293 struct nfs4_lockdata {
3294 struct nfs_lock_args arg;
3295 struct nfs_lock_res res;
3296 struct nfs4_lock_state *lsp;
3297 struct nfs_open_context *ctx;
3298 struct file_lock fl;
3299 unsigned long timestamp;
3304 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3305 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3307 struct nfs4_lockdata *p;
3308 struct inode *inode = lsp->ls_state->inode;
3309 struct nfs_server *server = NFS_SERVER(inode);
3311 p = kzalloc(sizeof(*p), GFP_KERNEL);
3315 p->arg.fh = NFS_FH(inode);
3317 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3318 if (p->arg.lock_seqid == NULL)
3320 p->arg.lock_stateid = &lsp->ls_stateid;
3321 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3322 p->arg.lock_owner.id = lsp->ls_id.id;
3324 atomic_inc(&lsp->ls_count);
3325 p->ctx = get_nfs_open_context(ctx);
3326 memcpy(&p->fl, fl, sizeof(p->fl));
3333 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3335 struct nfs4_lockdata *data = calldata;
3336 struct nfs4_state *state = data->lsp->ls_state;
3337 struct nfs4_state_owner *sp = state->owner;
3338 struct rpc_message msg = {
3339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3340 .rpc_argp = &data->arg,
3341 .rpc_resp = &data->res,
3342 .rpc_cred = sp->so_cred,
3345 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3347 dprintk("%s: begin!\n", __FUNCTION__);
3348 /* Do we need to do an open_to_lock_owner? */
3349 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3350 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3351 if (data->arg.open_seqid == NULL) {
3352 data->rpc_status = -ENOMEM;
3353 task->tk_action = NULL;
3356 data->arg.open_stateid = &state->stateid;
3357 data->arg.new_lock_owner = 1;
3359 data->timestamp = jiffies;
3360 rpc_call_setup(task, &msg, 0);
3362 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3365 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3367 struct nfs4_lockdata *data = calldata;
3369 dprintk("%s: begin!\n", __FUNCTION__);
3371 data->rpc_status = task->tk_status;
3372 if (RPC_ASSASSINATED(task))
3374 if (data->arg.new_lock_owner != 0) {
3375 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3376 if (data->rpc_status == 0)
3377 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3381 if (data->rpc_status == 0) {
3382 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3383 sizeof(data->lsp->ls_stateid.data));
3384 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3385 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3387 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3389 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3392 static void nfs4_lock_release(void *calldata)
3394 struct nfs4_lockdata *data = calldata;
3396 dprintk("%s: begin!\n", __FUNCTION__);
3397 if (data->arg.open_seqid != NULL)
3398 nfs_free_seqid(data->arg.open_seqid);
3399 if (data->cancelled != 0) {
3400 struct rpc_task *task;
3401 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3402 data->arg.lock_seqid);
3405 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3407 nfs_free_seqid(data->arg.lock_seqid);
3408 nfs4_put_lock_state(data->lsp);
3409 put_nfs_open_context(data->ctx);
3411 dprintk("%s: done!\n", __FUNCTION__);
3414 static const struct rpc_call_ops nfs4_lock_ops = {
3415 .rpc_call_prepare = nfs4_lock_prepare,
3416 .rpc_call_done = nfs4_lock_done,
3417 .rpc_release = nfs4_lock_release,
3420 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3422 struct nfs4_lockdata *data;
3423 struct rpc_task *task;
3426 dprintk("%s: begin!\n", __FUNCTION__);
3427 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3428 fl->fl_u.nfs4_fl.owner);
3432 data->arg.block = 1;
3434 data->arg.reclaim = 1;
3435 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3436 &nfs4_lock_ops, data);
3438 return PTR_ERR(task);
3439 ret = nfs4_wait_for_completion_rpc_task(task);
3441 ret = data->rpc_status;
3442 if (ret == -NFS4ERR_DENIED)
3445 data->cancelled = 1;
3447 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3451 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3453 struct nfs_server *server = NFS_SERVER(state->inode);
3454 struct nfs4_exception exception = { };
3458 /* Cache the lock if possible... */
3459 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3461 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3462 if (err != -NFS4ERR_DELAY)
3464 nfs4_handle_exception(server, err, &exception);
3465 } while (exception.retry);
3469 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3471 struct nfs_server *server = NFS_SERVER(state->inode);
3472 struct nfs4_exception exception = { };
3475 err = nfs4_set_lock_state(state, request);
3479 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3481 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3482 if (err != -NFS4ERR_DELAY)
3484 nfs4_handle_exception(server, err, &exception);
3485 } while (exception.retry);
3489 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3491 struct nfs_client *clp = state->owner->so_client;
3492 unsigned char fl_flags = request->fl_flags;
3495 /* Is this a delegated open? */
3496 status = nfs4_set_lock_state(state, request);
3499 request->fl_flags |= FL_ACCESS;
3500 status = do_vfs_lock(request->fl_file, request);
3503 down_read(&clp->cl_sem);
3504 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3505 struct nfs_inode *nfsi = NFS_I(state->inode);
3506 /* Yes: cache locks! */
3507 down_read(&nfsi->rwsem);
3508 /* ...but avoid races with delegation recall... */
3509 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3510 request->fl_flags = fl_flags & ~FL_SLEEP;
3511 status = do_vfs_lock(request->fl_file, request);
3512 up_read(&nfsi->rwsem);
3515 up_read(&nfsi->rwsem);
3517 status = _nfs4_do_setlk(state, cmd, request, 0);
3520 /* Note: we always want to sleep here! */
3521 request->fl_flags = fl_flags | FL_SLEEP;
3522 if (do_vfs_lock(request->fl_file, request) < 0)
3523 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3525 up_read(&clp->cl_sem);
3527 request->fl_flags = fl_flags;
3531 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3533 struct nfs4_exception exception = { };
3537 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3538 _nfs4_proc_setlk(state, cmd, request),
3540 } while (exception.retry);
3545 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3547 struct nfs_open_context *ctx;
3548 struct nfs4_state *state;
3549 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3552 /* verify open state */
3553 ctx = (struct nfs_open_context *)filp->private_data;
3556 if (request->fl_start < 0 || request->fl_end < 0)
3560 return nfs4_proc_getlk(state, F_GETLK, request);
3562 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3565 if (request->fl_type == F_UNLCK)
3566 return nfs4_proc_unlck(state, cmd, request);
3569 status = nfs4_proc_setlk(state, cmd, request);
3570 if ((status != -EAGAIN) || IS_SETLK(cmd))
3572 timeout = nfs4_set_lock_task_retry(timeout);
3573 status = -ERESTARTSYS;
3576 } while(status < 0);
3580 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3582 struct nfs_server *server = NFS_SERVER(state->inode);
3583 struct nfs4_exception exception = { };
3586 err = nfs4_set_lock_state(state, fl);
3590 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3591 if (err != -NFS4ERR_DELAY)
3593 err = nfs4_handle_exception(server, err, &exception);
3594 } while (exception.retry);
3599 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3601 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3602 size_t buflen, int flags)
3604 struct inode *inode = dentry->d_inode;
3606 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3609 if (!S_ISREG(inode->i_mode) &&
3610 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3613 return nfs4_proc_set_acl(inode, buf, buflen);
3616 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3617 * and that's what we'll do for e.g. user attributes that haven't been set.
3618 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3619 * attributes in kernel-managed attribute namespaces. */
3620 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3623 struct inode *inode = dentry->d_inode;
3625 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3628 return nfs4_proc_get_acl(inode, buf, buflen);
3631 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3633 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3635 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3637 if (buf && buflen < len)
3640 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3644 int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
3645 struct nfs4_fs_locations *fs_locations, struct page *page)
3647 struct nfs_server *server = NFS_SERVER(dir);
3649 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3650 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3652 struct nfs4_fs_locations_arg args = {
3653 .dir_fh = NFS_FH(dir),
3658 struct rpc_message msg = {
3659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3661 .rpc_resp = fs_locations,
3665 dprintk("%s: start\n", __FUNCTION__);
3666 nfs_fattr_init(&fs_locations->fattr);
3667 fs_locations->server = server;
3668 fs_locations->nlocations = 0;
3669 status = rpc_call_sync(server->client, &msg, 0);
3670 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3674 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3675 .recover_open = nfs4_open_reclaim,
3676 .recover_lock = nfs4_lock_reclaim,
3679 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3680 .recover_open = nfs4_open_expired,
3681 .recover_lock = nfs4_lock_expired,
3684 static const struct inode_operations nfs4_file_inode_operations = {
3685 .permission = nfs_permission,
3686 .getattr = nfs_getattr,
3687 .setattr = nfs_setattr,
3688 .getxattr = nfs4_getxattr,
3689 .setxattr = nfs4_setxattr,
3690 .listxattr = nfs4_listxattr,
3693 const struct nfs_rpc_ops nfs_v4_clientops = {
3694 .version = 4, /* protocol version */
3695 .dentry_ops = &nfs4_dentry_operations,
3696 .dir_inode_ops = &nfs4_dir_inode_operations,
3697 .file_inode_ops = &nfs4_file_inode_operations,
3698 .getroot = nfs4_proc_get_root,
3699 .getattr = nfs4_proc_getattr,
3700 .setattr = nfs4_proc_setattr,
3701 .lookupfh = nfs4_proc_lookupfh,
3702 .lookup = nfs4_proc_lookup,
3703 .access = nfs4_proc_access,
3704 .readlink = nfs4_proc_readlink,
3705 .create = nfs4_proc_create,
3706 .remove = nfs4_proc_remove,
3707 .unlink_setup = nfs4_proc_unlink_setup,
3708 .unlink_done = nfs4_proc_unlink_done,
3709 .rename = nfs4_proc_rename,
3710 .link = nfs4_proc_link,
3711 .symlink = nfs4_proc_symlink,
3712 .mkdir = nfs4_proc_mkdir,
3713 .rmdir = nfs4_proc_remove,
3714 .readdir = nfs4_proc_readdir,
3715 .mknod = nfs4_proc_mknod,
3716 .statfs = nfs4_proc_statfs,
3717 .fsinfo = nfs4_proc_fsinfo,
3718 .pathconf = nfs4_proc_pathconf,
3719 .set_capabilities = nfs4_server_capabilities,
3720 .decode_dirent = nfs4_decode_dirent,
3721 .read_setup = nfs4_proc_read_setup,
3722 .read_done = nfs4_read_done,
3723 .write_setup = nfs4_proc_write_setup,
3724 .write_done = nfs4_write_done,
3725 .commit_setup = nfs4_proc_commit_setup,
3726 .commit_done = nfs4_commit_done,
3727 .file_open = nfs_open,
3728 .file_release = nfs_release,
3729 .lock = nfs4_proc_lock,
3730 .clear_acl_cache = nfs4_zap_acl_attr,