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);
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, dentry->d_inode->i_ino);
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, dentry->d_parent->d_inode->i_ino);
191 readdir->pgbase = (char *)p - (char *)start;
192 readdir->count -= readdir->pgbase;
193 kunmap_atomic(start, KM_USER0);
196 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
198 struct nfs_client *clp = server->nfs_client;
199 spin_lock(&clp->cl_lock);
200 if (time_before(clp->cl_last_renewal,timestamp))
201 clp->cl_last_renewal = timestamp;
202 spin_unlock(&clp->cl_lock);
205 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
207 struct nfs_inode *nfsi = NFS_I(dir);
209 spin_lock(&dir->i_lock);
210 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
211 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
212 nfsi->change_attr = cinfo->after;
213 spin_unlock(&dir->i_lock);
216 struct nfs4_opendata {
218 struct nfs_openargs o_arg;
219 struct nfs_openres o_res;
220 struct nfs_open_confirmargs c_arg;
221 struct nfs_open_confirmres c_res;
222 struct nfs_fattr f_attr;
223 struct nfs_fattr dir_attr;
226 struct nfs4_state_owner *owner;
228 unsigned long timestamp;
234 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
236 p->o_res.f_attr = &p->f_attr;
237 p->o_res.dir_attr = &p->dir_attr;
238 p->o_res.server = p->o_arg.server;
239 nfs_fattr_init(&p->f_attr);
240 nfs_fattr_init(&p->dir_attr);
243 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
244 struct nfs4_state_owner *sp, int flags,
245 const struct iattr *attrs)
247 struct dentry *parent = dget_parent(path->dentry);
248 struct inode *dir = parent->d_inode;
249 struct nfs_server *server = NFS_SERVER(dir);
250 struct nfs4_opendata *p;
252 p = kzalloc(sizeof(*p), GFP_KERNEL);
255 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
256 if (p->o_arg.seqid == NULL)
258 p->path.mnt = mntget(path->mnt);
259 p->path.dentry = dget(path->dentry);
262 atomic_inc(&sp->so_count);
263 p->o_arg.fh = NFS_FH(dir);
264 p->o_arg.open_flags = flags,
265 p->o_arg.clientid = server->nfs_client->cl_clientid;
266 p->o_arg.id = sp->so_owner_id.id;
267 p->o_arg.name = &p->path.dentry->d_name;
268 p->o_arg.server = server;
269 p->o_arg.bitmask = server->attr_bitmask;
270 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
271 if (flags & O_EXCL) {
272 u32 *s = (u32 *) p->o_arg.u.verifier.data;
275 } else if (flags & O_CREAT) {
276 p->o_arg.u.attrs = &p->attrs;
277 memcpy(&p->attrs, attrs, sizeof(p->attrs));
279 p->c_arg.fh = &p->o_res.fh;
280 p->c_arg.stateid = &p->o_res.stateid;
281 p->c_arg.seqid = p->o_arg.seqid;
282 nfs4_init_opendata_res(p);
292 static void nfs4_opendata_free(struct kref *kref)
294 struct nfs4_opendata *p = container_of(kref,
295 struct nfs4_opendata, kref);
297 nfs_free_seqid(p->o_arg.seqid);
298 nfs4_put_state_owner(p->owner);
300 dput(p->path.dentry);
305 static void nfs4_opendata_put(struct nfs4_opendata *p)
308 kref_put(&p->kref, nfs4_opendata_free);
311 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
316 rpc_clnt_sigmask(task->tk_client, &oldset);
317 ret = rpc_wait_for_completion_task(task);
318 rpc_clnt_sigunmask(task->tk_client, &oldset);
322 static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
324 switch (open_flags) {
331 case FMODE_READ|FMODE_WRITE:
334 nfs4_state_set_mode_locked(state, state->state | open_flags);
337 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
339 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
340 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
341 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
342 switch (open_flags) {
344 set_bit(NFS_O_RDONLY_STATE, &state->flags);
347 set_bit(NFS_O_WRONLY_STATE, &state->flags);
349 case FMODE_READ|FMODE_WRITE:
350 set_bit(NFS_O_RDWR_STATE, &state->flags);
354 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
356 spin_lock(&state->owner->so_lock);
357 spin_lock(&state->inode->i_lock);
358 nfs_set_open_stateid_locked(state, stateid, open_flags);
359 spin_unlock(&state->inode->i_lock);
360 spin_unlock(&state->owner->so_lock);
363 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *deleg_stateid, int open_flags)
365 struct inode *inode = state->inode;
367 open_flags &= (FMODE_READ|FMODE_WRITE);
368 /* Protect against nfs4_find_state_byowner() */
369 spin_lock(&state->owner->so_lock);
370 spin_lock(&inode->i_lock);
371 if (deleg_stateid != NULL) {
372 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
373 set_bit(NFS_DELEGATED_STATE, &state->flags);
375 if (open_stateid != NULL)
376 nfs_set_open_stateid_locked(state, open_stateid, open_flags);
377 update_open_stateflags(state, open_flags);
378 spin_unlock(&inode->i_lock);
379 spin_unlock(&state->owner->so_lock);
382 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
385 struct nfs4_state *state = NULL;
386 struct nfs_delegation *delegation;
387 nfs4_stateid *deleg_stateid = NULL;
389 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
391 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
394 state = nfs4_get_open_state(inode, data->owner);
397 if (data->o_res.delegation_type != 0) {
398 int delegation_flags = 0;
401 delegation = rcu_dereference(NFS_I(inode)->delegation);
403 delegation_flags = delegation->flags;
405 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
406 nfs_inode_set_delegation(state->inode,
407 data->owner->so_cred,
410 nfs_inode_reclaim_delegation(state->inode,
411 data->owner->so_cred,
415 delegation = rcu_dereference(NFS_I(inode)->delegation);
416 if (delegation != NULL)
417 deleg_stateid = &delegation->stateid;
418 update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags);
426 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
428 struct nfs_inode *nfsi = NFS_I(state->inode);
429 struct nfs_open_context *ctx;
431 spin_lock(&state->inode->i_lock);
432 list_for_each_entry(ctx, &nfsi->open_files, list) {
433 if (ctx->state != state)
435 get_nfs_open_context(ctx);
436 spin_unlock(&state->inode->i_lock);
439 spin_unlock(&state->inode->i_lock);
440 return ERR_PTR(-ENOENT);
443 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
445 struct nfs4_state *newstate;
448 opendata->o_arg.open_flags = openflags;
449 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
450 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
451 nfs4_init_opendata_res(opendata);
452 ret = _nfs4_proc_open(opendata);
455 newstate = nfs4_opendata_to_nfs4_state(opendata);
456 if (newstate != NULL)
457 nfs4_close_state(&opendata->path, newstate, openflags);
462 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
464 struct nfs4_state *newstate;
467 /* memory barrier prior to reading state->n_* */
468 clear_bit(NFS_DELEGATED_STATE, &state->flags);
470 if (state->n_rdwr != 0) {
471 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
474 if (newstate != state)
477 if (state->n_wronly != 0) {
478 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
481 if (newstate != state)
484 if (state->n_rdonly != 0) {
485 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
488 if (newstate != state)
496 * reclaim state on the server after a reboot.
498 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
500 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
501 struct nfs4_opendata *opendata;
502 int delegation_type = 0;
505 if (delegation != NULL) {
506 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
507 memcpy(&state->stateid, &delegation->stateid,
508 sizeof(state->stateid));
509 set_bit(NFS_DELEGATED_STATE, &state->flags);
512 delegation_type = delegation->type;
514 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
515 if (opendata == NULL)
517 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
518 opendata->o_arg.fh = NFS_FH(state->inode);
519 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
520 opendata->o_arg.u.delegation_type = delegation_type;
521 status = nfs4_open_recover(opendata, state);
522 nfs4_opendata_put(opendata);
526 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
528 struct nfs_server *server = NFS_SERVER(state->inode);
529 struct nfs4_exception exception = { };
532 err = _nfs4_do_open_reclaim(ctx, state);
533 if (err != -NFS4ERR_DELAY)
535 nfs4_handle_exception(server, err, &exception);
536 } while (exception.retry);
540 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
542 struct nfs_open_context *ctx;
545 ctx = nfs4_state_find_open_context(state);
548 ret = nfs4_do_open_reclaim(ctx, state);
549 put_nfs_open_context(ctx);
553 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
555 struct nfs4_state_owner *sp = state->owner;
556 struct nfs4_opendata *opendata;
559 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
560 if (opendata == NULL)
562 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
563 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
564 sizeof(opendata->o_arg.u.delegation.data));
565 ret = nfs4_open_recover(opendata, state);
566 nfs4_opendata_put(opendata);
570 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
572 struct nfs4_exception exception = { };
573 struct nfs_server *server = NFS_SERVER(state->inode);
576 err = _nfs4_open_delegation_recall(ctx, state, stateid);
580 case -NFS4ERR_STALE_CLIENTID:
581 case -NFS4ERR_STALE_STATEID:
582 case -NFS4ERR_EXPIRED:
583 /* Don't recall a delegation if it was lost */
584 nfs4_schedule_state_recovery(server->nfs_client);
587 err = nfs4_handle_exception(server, err, &exception);
588 } while (exception.retry);
592 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
594 struct nfs4_opendata *data = calldata;
595 struct rpc_message msg = {
596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
597 .rpc_argp = &data->c_arg,
598 .rpc_resp = &data->c_res,
599 .rpc_cred = data->owner->so_cred,
601 data->timestamp = jiffies;
602 rpc_call_setup(task, &msg, 0);
605 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
607 struct nfs4_opendata *data = calldata;
609 data->rpc_status = task->tk_status;
610 if (RPC_ASSASSINATED(task))
612 if (data->rpc_status == 0) {
613 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
614 sizeof(data->o_res.stateid.data));
615 renew_lease(data->o_res.server, data->timestamp);
617 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
618 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
621 static void nfs4_open_confirm_release(void *calldata)
623 struct nfs4_opendata *data = calldata;
624 struct nfs4_state *state = NULL;
626 /* If this request hasn't been cancelled, do nothing */
627 if (data->cancelled == 0)
629 /* In case of error, no cleanup! */
630 if (data->rpc_status != 0)
632 nfs_confirm_seqid(&data->owner->so_seqid, 0);
633 state = nfs4_opendata_to_nfs4_state(data);
635 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
637 nfs4_opendata_put(data);
640 static const struct rpc_call_ops nfs4_open_confirm_ops = {
641 .rpc_call_prepare = nfs4_open_confirm_prepare,
642 .rpc_call_done = nfs4_open_confirm_done,
643 .rpc_release = nfs4_open_confirm_release,
647 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
649 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
651 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
652 struct rpc_task *task;
655 kref_get(&data->kref);
657 * If rpc_run_task() ends up calling ->rpc_release(), we
658 * want to ensure that it takes the 'error' code path.
660 data->rpc_status = -ENOMEM;
661 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
663 return PTR_ERR(task);
664 status = nfs4_wait_for_completion_rpc_task(task);
669 status = data->rpc_status;
674 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
676 struct nfs4_opendata *data = calldata;
677 struct nfs4_state_owner *sp = data->owner;
678 struct rpc_message msg = {
679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
680 .rpc_argp = &data->o_arg,
681 .rpc_resp = &data->o_res,
682 .rpc_cred = sp->so_cred,
685 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
687 /* Update sequence id. */
688 data->o_arg.id = sp->so_owner_id.id;
689 data->o_arg.clientid = sp->so_client->cl_clientid;
690 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
691 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
692 data->timestamp = jiffies;
693 rpc_call_setup(task, &msg, 0);
696 static void nfs4_open_done(struct rpc_task *task, void *calldata)
698 struct nfs4_opendata *data = calldata;
700 data->rpc_status = task->tk_status;
701 if (RPC_ASSASSINATED(task))
703 if (task->tk_status == 0) {
704 switch (data->o_res.f_attr->mode & S_IFMT) {
708 data->rpc_status = -ELOOP;
711 data->rpc_status = -EISDIR;
714 data->rpc_status = -ENOTDIR;
716 renew_lease(data->o_res.server, data->timestamp);
717 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
718 nfs_confirm_seqid(&data->owner->so_seqid, 0);
720 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
723 static void nfs4_open_release(void *calldata)
725 struct nfs4_opendata *data = calldata;
726 struct nfs4_state *state = NULL;
728 /* If this request hasn't been cancelled, do nothing */
729 if (data->cancelled == 0)
731 /* In case of error, no cleanup! */
732 if (data->rpc_status != 0)
734 /* In case we need an open_confirm, no cleanup! */
735 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
737 nfs_confirm_seqid(&data->owner->so_seqid, 0);
738 state = nfs4_opendata_to_nfs4_state(data);
740 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
742 nfs4_opendata_put(data);
745 static const struct rpc_call_ops nfs4_open_ops = {
746 .rpc_call_prepare = nfs4_open_prepare,
747 .rpc_call_done = nfs4_open_done,
748 .rpc_release = nfs4_open_release,
752 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
754 static int _nfs4_proc_open(struct nfs4_opendata *data)
756 struct inode *dir = data->dir->d_inode;
757 struct nfs_server *server = NFS_SERVER(dir);
758 struct nfs_openargs *o_arg = &data->o_arg;
759 struct nfs_openres *o_res = &data->o_res;
760 struct rpc_task *task;
763 kref_get(&data->kref);
765 * If rpc_run_task() ends up calling ->rpc_release(), we
766 * want to ensure that it takes the 'error' code path.
768 data->rpc_status = -ENOMEM;
770 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
772 return PTR_ERR(task);
773 status = nfs4_wait_for_completion_rpc_task(task);
778 status = data->rpc_status;
783 if (o_arg->open_flags & O_CREAT) {
784 update_changeattr(dir, &o_res->cinfo);
785 nfs_post_op_update_inode(dir, o_res->dir_attr);
787 nfs_refresh_inode(dir, o_res->dir_attr);
788 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
789 status = _nfs4_proc_open_confirm(data);
793 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
794 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
798 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
800 struct nfs_access_entry cache;
804 if (openflags & FMODE_READ)
806 if (openflags & FMODE_WRITE)
808 if (openflags & FMODE_EXEC)
810 status = nfs_access_get_cached(inode, cred, &cache);
814 /* Be clever: ask server to check for all possible rights */
815 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
817 cache.jiffies = jiffies;
818 status = _nfs4_proc_access(inode, &cache);
821 nfs_access_add_cache(inode, &cache);
823 if ((cache.mask & mask) == mask)
828 static int nfs4_recover_expired_lease(struct nfs_server *server)
830 struct nfs_client *clp = server->nfs_client;
834 ret = nfs4_wait_clnt_recover(server->client, clp);
837 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
839 nfs4_schedule_state_recovery(clp);
846 * reclaim state on the server after a network partition.
847 * Assumes caller holds the appropriate lock
849 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
851 struct inode *inode = state->inode;
852 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
853 struct nfs4_opendata *opendata;
854 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
857 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
858 ret = _nfs4_do_access(inode, ctx->cred, openflags);
861 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
862 set_bit(NFS_DELEGATED_STATE, &state->flags);
865 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
866 if (opendata == NULL)
868 ret = nfs4_open_recover(opendata, state);
869 if (ret == -ESTALE) {
870 /* Invalidate the state owner so we don't ever use it again */
871 nfs4_drop_state_owner(state->owner);
872 d_drop(ctx->path.dentry);
874 nfs4_opendata_put(opendata);
878 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
880 struct nfs_server *server = NFS_SERVER(state->inode);
881 struct nfs4_exception exception = { };
885 err = _nfs4_open_expired(ctx, state);
886 if (err == -NFS4ERR_DELAY)
887 nfs4_handle_exception(server, err, &exception);
888 } while (exception.retry);
892 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
894 struct nfs_open_context *ctx;
897 ctx = nfs4_state_find_open_context(state);
900 ret = nfs4_do_open_expired(ctx, state);
901 put_nfs_open_context(ctx);
906 * Returns a referenced nfs4_state if there is an open delegation on the file
908 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
910 struct nfs_delegation *delegation;
911 struct nfs_server *server = NFS_SERVER(inode);
912 struct nfs_client *clp = server->nfs_client;
913 struct nfs_inode *nfsi = NFS_I(inode);
914 struct nfs4_state_owner *sp = NULL;
915 struct nfs4_state *state = NULL;
916 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
920 if (!(sp = nfs4_get_state_owner(server, cred))) {
921 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
924 err = nfs4_recover_expired_lease(server);
926 goto out_put_state_owner;
927 /* Protect against reboot recovery - NOTE ORDER! */
928 down_read(&clp->cl_sem);
929 /* Protect against delegation recall */
930 down_read(&nfsi->rwsem);
931 delegation = NFS_I(inode)->delegation;
933 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
936 state = nfs4_get_open_state(inode, sp);
941 if ((state->state & open_flags) == open_flags) {
942 spin_lock(&inode->i_lock);
943 update_open_stateflags(state, open_flags);
944 spin_unlock(&inode->i_lock);
946 } else if (state->state != 0)
947 goto out_put_open_state;
950 err = _nfs4_do_access(inode, cred, open_flags);
953 goto out_put_open_state;
954 update_open_stateid(state, NULL, &delegation->stateid, open_flags);
956 nfs4_put_state_owner(sp);
957 up_read(&nfsi->rwsem);
958 up_read(&clp->cl_sem);
962 nfs4_put_open_state(state);
964 up_read(&nfsi->rwsem);
965 up_read(&clp->cl_sem);
967 nfs_inode_return_delegation(inode);
969 nfs4_put_state_owner(sp);
973 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
975 struct nfs4_exception exception = { };
976 struct nfs4_state *res = ERR_PTR(-EIO);
980 err = _nfs4_open_delegated(inode, flags, cred, &res);
983 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
985 } while (exception.retry);
990 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
991 * fields corresponding to attributes that were used to store the verifier.
992 * Make sure we clobber those fields in the later setattr call
994 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
996 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
997 !(sattr->ia_valid & ATTR_ATIME_SET))
998 sattr->ia_valid |= ATTR_ATIME;
1000 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1001 !(sattr->ia_valid & ATTR_MTIME_SET))
1002 sattr->ia_valid |= ATTR_MTIME;
1006 * Returns a referenced nfs4_state
1008 static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1010 struct nfs4_state_owner *sp;
1011 struct nfs4_state *state = NULL;
1012 struct nfs_server *server = NFS_SERVER(dir);
1013 struct nfs_client *clp = server->nfs_client;
1014 struct nfs4_opendata *opendata;
1017 /* Protect against reboot recovery conflicts */
1019 if (!(sp = nfs4_get_state_owner(server, cred))) {
1020 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1023 status = nfs4_recover_expired_lease(server);
1025 goto err_put_state_owner;
1026 down_read(&clp->cl_sem);
1028 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
1029 if (opendata == NULL)
1030 goto err_release_rwsem;
1032 status = _nfs4_proc_open(opendata);
1034 goto err_opendata_put;
1036 if (opendata->o_arg.open_flags & O_EXCL)
1037 nfs4_exclusive_attrset(opendata, sattr);
1040 state = nfs4_opendata_to_nfs4_state(opendata);
1042 goto err_opendata_put;
1043 nfs4_opendata_put(opendata);
1044 nfs4_put_state_owner(sp);
1045 up_read(&clp->cl_sem);
1049 nfs4_opendata_put(opendata);
1051 up_read(&clp->cl_sem);
1052 err_put_state_owner:
1053 nfs4_put_state_owner(sp);
1060 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
1062 struct nfs4_exception exception = { };
1063 struct nfs4_state *res;
1067 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
1070 /* NOTE: BAD_SEQID means the server and client disagree about the
1071 * book-keeping w.r.t. state-changing operations
1072 * (OPEN/CLOSE/LOCK/LOCKU...)
1073 * It is actually a sign of a bug on the client or on the server.
1075 * If we receive a BAD_SEQID error in the particular case of
1076 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1077 * have unhashed the old state_owner for us, and that we can
1078 * therefore safely retry using a new one. We should still warn
1079 * the user though...
1081 if (status == -NFS4ERR_BAD_SEQID) {
1082 printk(KERN_WARNING "NFS: v4 server %s "
1083 " returned a bad sequence-id error!\n",
1084 NFS_SERVER(dir)->nfs_client->cl_hostname);
1085 exception.retry = 1;
1089 * BAD_STATEID on OPEN means that the server cancelled our
1090 * state before it received the OPEN_CONFIRM.
1091 * Recover by retrying the request as per the discussion
1092 * on Page 181 of RFC3530.
1094 if (status == -NFS4ERR_BAD_STATEID) {
1095 exception.retry = 1;
1098 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1099 status, &exception));
1100 } while (exception.retry);
1104 static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1105 struct iattr *sattr, struct nfs4_state *state)
1107 struct nfs_server *server = NFS_SERVER(inode);
1108 struct nfs_setattrargs arg = {
1109 .fh = NFS_FH(inode),
1112 .bitmask = server->attr_bitmask,
1114 struct nfs_setattrres res = {
1118 struct rpc_message msg = {
1119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1123 unsigned long timestamp = jiffies;
1126 nfs_fattr_init(fattr);
1128 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1129 /* Use that stateid */
1130 } else if (state != NULL) {
1131 msg.rpc_cred = state->owner->so_cred;
1132 nfs4_copy_stateid(&arg.stateid, state, current->files);
1134 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1136 status = rpc_call_sync(server->client, &msg, 0);
1137 if (status == 0 && state != NULL)
1138 renew_lease(server, timestamp);
1142 static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1143 struct iattr *sattr, struct nfs4_state *state)
1145 struct nfs_server *server = NFS_SERVER(inode);
1146 struct nfs4_exception exception = { };
1149 err = nfs4_handle_exception(server,
1150 _nfs4_do_setattr(inode, fattr, sattr, state),
1152 } while (exception.retry);
1156 struct nfs4_closedata {
1158 struct inode *inode;
1159 struct nfs4_state *state;
1160 struct nfs_closeargs arg;
1161 struct nfs_closeres res;
1162 struct nfs_fattr fattr;
1163 unsigned long timestamp;
1166 static void nfs4_free_closedata(void *data)
1168 struct nfs4_closedata *calldata = data;
1169 struct nfs4_state_owner *sp = calldata->state->owner;
1171 nfs4_put_open_state(calldata->state);
1172 nfs_free_seqid(calldata->arg.seqid);
1173 nfs4_put_state_owner(sp);
1174 dput(calldata->path.dentry);
1175 mntput(calldata->path.mnt);
1179 static void nfs4_close_done(struct rpc_task *task, void *data)
1181 struct nfs4_closedata *calldata = data;
1182 struct nfs4_state *state = calldata->state;
1183 struct nfs_server *server = NFS_SERVER(calldata->inode);
1185 if (RPC_ASSASSINATED(task))
1187 /* hmm. we are done with the inode, and in the process of freeing
1188 * the state_owner. we keep this around to process errors
1190 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1191 switch (task->tk_status) {
1193 nfs_set_open_stateid(state, &calldata->res.stateid, calldata->arg.open_flags);
1194 renew_lease(server, calldata->timestamp);
1196 case -NFS4ERR_STALE_STATEID:
1197 case -NFS4ERR_EXPIRED:
1200 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1201 rpc_restart_call(task);
1205 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1208 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1210 struct nfs4_closedata *calldata = data;
1211 struct nfs4_state *state = calldata->state;
1212 struct rpc_message msg = {
1213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1214 .rpc_argp = &calldata->arg,
1215 .rpc_resp = &calldata->res,
1216 .rpc_cred = state->owner->so_cred,
1218 int clear_rd, clear_wr, clear_rdwr;
1221 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1224 mode = FMODE_READ|FMODE_WRITE;
1225 clear_rd = clear_wr = clear_rdwr = 0;
1226 spin_lock(&state->owner->so_lock);
1227 spin_lock(&calldata->inode->i_lock);
1228 /* Calculate the change in open mode */
1229 if (state->n_rdwr == 0) {
1230 if (state->n_rdonly == 0) {
1231 mode &= ~FMODE_READ;
1232 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1233 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1235 if (state->n_wronly == 0) {
1236 mode &= ~FMODE_WRITE;
1237 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1238 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1241 spin_unlock(&calldata->inode->i_lock);
1242 spin_unlock(&state->owner->so_lock);
1243 if (!clear_rd && !clear_wr && !clear_rdwr) {
1244 /* Note: exit _without_ calling nfs4_close_done */
1245 task->tk_action = NULL;
1248 nfs_fattr_init(calldata->res.fattr);
1250 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1251 calldata->arg.open_flags = mode;
1252 calldata->timestamp = jiffies;
1253 rpc_call_setup(task, &msg, 0);
1256 static const struct rpc_call_ops nfs4_close_ops = {
1257 .rpc_call_prepare = nfs4_close_prepare,
1258 .rpc_call_done = nfs4_close_done,
1259 .rpc_release = nfs4_free_closedata,
1263 * It is possible for data to be read/written from a mem-mapped file
1264 * after the sys_close call (which hits the vfs layer as a flush).
1265 * This means that we can't safely call nfsv4 close on a file until
1266 * the inode is cleared. This in turn means that we are not good
1267 * NFSv4 citizens - we do not indicate to the server to update the file's
1268 * share state even when we are done with one of the three share
1269 * stateid's in the inode.
1271 * NOTE: Caller must be holding the sp->so_owner semaphore!
1273 int nfs4_do_close(struct path *path, struct nfs4_state *state)
1275 struct nfs_server *server = NFS_SERVER(state->inode);
1276 struct nfs4_closedata *calldata;
1277 struct nfs4_state_owner *sp = state->owner;
1278 struct rpc_task *task;
1279 int status = -ENOMEM;
1281 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1282 if (calldata == NULL)
1284 calldata->inode = state->inode;
1285 calldata->state = state;
1286 calldata->arg.fh = NFS_FH(state->inode);
1287 calldata->arg.stateid = &state->open_stateid;
1288 /* Serialization for the sequence id */
1289 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1290 if (calldata->arg.seqid == NULL)
1291 goto out_free_calldata;
1292 calldata->arg.bitmask = server->attr_bitmask;
1293 calldata->res.fattr = &calldata->fattr;
1294 calldata->res.server = server;
1295 calldata->path.mnt = mntget(path->mnt);
1296 calldata->path.dentry = dget(path->dentry);
1298 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1300 return PTR_ERR(task);
1306 nfs4_put_open_state(state);
1307 nfs4_put_state_owner(sp);
1311 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
1316 /* If the open_intent is for execute, we have an extra check to make */
1317 if (nd->intent.open.flags & FMODE_EXEC) {
1318 ret = _nfs4_do_access(state->inode,
1319 state->owner->so_cred,
1320 nd->intent.open.flags);
1324 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1325 if (!IS_ERR(filp)) {
1326 struct nfs_open_context *ctx;
1327 ctx = (struct nfs_open_context *)filp->private_data;
1331 ret = PTR_ERR(filp);
1333 nfs4_close_state(path, state, nd->intent.open.flags);
1338 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1340 struct path path = {
1345 struct rpc_cred *cred;
1346 struct nfs4_state *state;
1349 if (nd->flags & LOOKUP_CREATE) {
1350 attr.ia_mode = nd->intent.open.create_mode;
1351 attr.ia_valid = ATTR_MODE;
1352 if (!IS_POSIXACL(dir))
1353 attr.ia_mode &= ~current->fs->umask;
1356 BUG_ON(nd->intent.open.flags & O_CREAT);
1359 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1361 return (struct dentry *)cred;
1362 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
1364 if (IS_ERR(state)) {
1365 if (PTR_ERR(state) == -ENOENT)
1366 d_add(dentry, NULL);
1367 return (struct dentry *)state;
1369 res = d_add_unique(dentry, igrab(state->inode));
1372 nfs4_intent_set_file(nd, &path, state);
1377 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1379 struct path path = {
1383 struct rpc_cred *cred;
1384 struct nfs4_state *state;
1386 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1388 return PTR_ERR(cred);
1389 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1391 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
1393 if (IS_ERR(state)) {
1394 switch (PTR_ERR(state)) {
1400 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1406 if (state->inode == dentry->d_inode) {
1407 nfs4_intent_set_file(nd, &path, state);
1410 nfs4_close_state(&path, state, openflags);
1417 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1419 struct nfs4_server_caps_res res = {};
1420 struct rpc_message msg = {
1421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1422 .rpc_argp = fhandle,
1427 status = rpc_call_sync(server->client, &msg, 0);
1429 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1430 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1431 server->caps |= NFS_CAP_ACLS;
1432 if (res.has_links != 0)
1433 server->caps |= NFS_CAP_HARDLINKS;
1434 if (res.has_symlinks != 0)
1435 server->caps |= NFS_CAP_SYMLINKS;
1436 server->acl_bitmask = res.acl_bitmask;
1441 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1443 struct nfs4_exception exception = { };
1446 err = nfs4_handle_exception(server,
1447 _nfs4_server_capabilities(server, fhandle),
1449 } while (exception.retry);
1453 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1454 struct nfs_fsinfo *info)
1456 struct nfs4_lookup_root_arg args = {
1457 .bitmask = nfs4_fattr_bitmap,
1459 struct nfs4_lookup_res res = {
1461 .fattr = info->fattr,
1464 struct rpc_message msg = {
1465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1469 nfs_fattr_init(info->fattr);
1470 return rpc_call_sync(server->client, &msg, 0);
1473 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1474 struct nfs_fsinfo *info)
1476 struct nfs4_exception exception = { };
1479 err = nfs4_handle_exception(server,
1480 _nfs4_lookup_root(server, fhandle, info),
1482 } while (exception.retry);
1487 * get the file handle for the "/" directory on the server
1489 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1490 struct nfs_fsinfo *info)
1494 status = nfs4_lookup_root(server, fhandle, info);
1496 status = nfs4_server_capabilities(server, fhandle);
1498 status = nfs4_do_fsinfo(server, fhandle, info);
1499 return nfs4_map_errors(status);
1503 * Get locations and (maybe) other attributes of a referral.
1504 * Note that we'll actually follow the referral later when
1505 * we detect fsid mismatch in inode revalidation
1507 static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1509 int status = -ENOMEM;
1510 struct page *page = NULL;
1511 struct nfs4_fs_locations *locations = NULL;
1513 page = alloc_page(GFP_KERNEL);
1516 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1517 if (locations == NULL)
1520 status = nfs4_proc_fs_locations(dir, name, locations, page);
1523 /* Make sure server returned a different fsid for the referral */
1524 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1525 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1530 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1531 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1533 fattr->mode = S_IFDIR;
1534 memset(fhandle, 0, sizeof(struct nfs_fh));
1543 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1545 struct nfs4_getattr_arg args = {
1547 .bitmask = server->attr_bitmask,
1549 struct nfs4_getattr_res res = {
1553 struct rpc_message msg = {
1554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1559 nfs_fattr_init(fattr);
1560 return rpc_call_sync(server->client, &msg, 0);
1563 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1565 struct nfs4_exception exception = { };
1568 err = nfs4_handle_exception(server,
1569 _nfs4_proc_getattr(server, fhandle, fattr),
1571 } while (exception.retry);
1576 * The file is not closed if it is opened due to the a request to change
1577 * the size of the file. The open call will not be needed once the
1578 * VFS layer lookup-intents are implemented.
1580 * Close is called when the inode is destroyed.
1581 * If we haven't opened the file for O_WRONLY, we
1582 * need to in the size_change case to obtain a stateid.
1585 * Because OPEN is always done by name in nfsv4, it is
1586 * possible that we opened a different file by the same
1587 * name. We can recognize this race condition, but we
1588 * can't do anything about it besides returning an error.
1590 * This will be fixed with VFS changes (lookup-intent).
1593 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1594 struct iattr *sattr)
1596 struct rpc_cred *cred;
1597 struct inode *inode = dentry->d_inode;
1598 struct nfs_open_context *ctx;
1599 struct nfs4_state *state = NULL;
1602 nfs_fattr_init(fattr);
1604 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1606 return PTR_ERR(cred);
1608 /* Search for an existing open(O_WRITE) file */
1609 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1613 status = nfs4_do_setattr(inode, fattr, sattr, state);
1615 nfs_setattr_update_inode(inode, sattr);
1617 put_nfs_open_context(ctx);
1622 static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1623 struct qstr *name, struct nfs_fh *fhandle,
1624 struct nfs_fattr *fattr)
1627 struct nfs4_lookup_arg args = {
1628 .bitmask = server->attr_bitmask,
1632 struct nfs4_lookup_res res = {
1637 struct rpc_message msg = {
1638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1643 nfs_fattr_init(fattr);
1645 dprintk("NFS call lookupfh %s\n", name->name);
1646 status = rpc_call_sync(server->client, &msg, 0);
1647 dprintk("NFS reply lookupfh: %d\n", status);
1651 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1652 struct qstr *name, struct nfs_fh *fhandle,
1653 struct nfs_fattr *fattr)
1655 struct nfs4_exception exception = { };
1658 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1660 if (err == -NFS4ERR_MOVED) {
1664 err = nfs4_handle_exception(server, err, &exception);
1665 } while (exception.retry);
1669 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1670 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1674 dprintk("NFS call lookup %s\n", name->name);
1675 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
1676 if (status == -NFS4ERR_MOVED)
1677 status = nfs4_get_referral(dir, name, fattr, fhandle);
1678 dprintk("NFS reply lookup: %d\n", status);
1682 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1684 struct nfs4_exception exception = { };
1687 err = nfs4_handle_exception(NFS_SERVER(dir),
1688 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1690 } while (exception.retry);
1694 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1696 struct nfs4_accessargs args = {
1697 .fh = NFS_FH(inode),
1699 struct nfs4_accessres res = { 0 };
1700 struct rpc_message msg = {
1701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1704 .rpc_cred = entry->cred,
1706 int mode = entry->mask;
1710 * Determine which access bits we want to ask for...
1712 if (mode & MAY_READ)
1713 args.access |= NFS4_ACCESS_READ;
1714 if (S_ISDIR(inode->i_mode)) {
1715 if (mode & MAY_WRITE)
1716 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1717 if (mode & MAY_EXEC)
1718 args.access |= NFS4_ACCESS_LOOKUP;
1720 if (mode & MAY_WRITE)
1721 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1722 if (mode & MAY_EXEC)
1723 args.access |= NFS4_ACCESS_EXECUTE;
1725 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1728 if (res.access & NFS4_ACCESS_READ)
1729 entry->mask |= MAY_READ;
1730 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1731 entry->mask |= MAY_WRITE;
1732 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1733 entry->mask |= MAY_EXEC;
1738 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1740 struct nfs4_exception exception = { };
1743 err = nfs4_handle_exception(NFS_SERVER(inode),
1744 _nfs4_proc_access(inode, entry),
1746 } while (exception.retry);
1751 * TODO: For the time being, we don't try to get any attributes
1752 * along with any of the zero-copy operations READ, READDIR,
1755 * In the case of the first three, we want to put the GETATTR
1756 * after the read-type operation -- this is because it is hard
1757 * to predict the length of a GETATTR response in v4, and thus
1758 * align the READ data correctly. This means that the GETATTR
1759 * may end up partially falling into the page cache, and we should
1760 * shift it into the 'tail' of the xdr_buf before processing.
1761 * To do this efficiently, we need to know the total length
1762 * of data received, which doesn't seem to be available outside
1765 * In the case of WRITE, we also want to put the GETATTR after
1766 * the operation -- in this case because we want to make sure
1767 * we get the post-operation mtime and size. This means that
1768 * we can't use xdr_encode_pages() as written: we need a variant
1769 * of it which would leave room in the 'tail' iovec.
1771 * Both of these changes to the XDR layer would in fact be quite
1772 * minor, but I decided to leave them for a subsequent patch.
1774 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1775 unsigned int pgbase, unsigned int pglen)
1777 struct nfs4_readlink args = {
1778 .fh = NFS_FH(inode),
1783 struct rpc_message msg = {
1784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1789 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1792 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1793 unsigned int pgbase, unsigned int pglen)
1795 struct nfs4_exception exception = { };
1798 err = nfs4_handle_exception(NFS_SERVER(inode),
1799 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1801 } while (exception.retry);
1807 * We will need to arrange for the VFS layer to provide an atomic open.
1808 * Until then, this create/open method is prone to inefficiency and race
1809 * conditions due to the lookup, create, and open VFS calls from sys_open()
1810 * placed on the wire.
1812 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1813 * The file will be opened again in the subsequent VFS open call
1814 * (nfs4_proc_file_open).
1816 * The open for read will just hang around to be used by any process that
1817 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1821 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1822 int flags, struct nameidata *nd)
1824 struct path path = {
1828 struct nfs4_state *state;
1829 struct rpc_cred *cred;
1832 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1834 status = PTR_ERR(cred);
1837 state = nfs4_do_open(dir, &path, flags, sattr, cred);
1839 if (IS_ERR(state)) {
1840 status = PTR_ERR(state);
1843 d_instantiate(dentry, igrab(state->inode));
1844 if (flags & O_EXCL) {
1845 struct nfs_fattr fattr;
1846 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
1848 nfs_setattr_update_inode(state->inode, sattr);
1849 nfs_post_op_update_inode(state->inode, &fattr);
1851 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
1852 status = nfs4_intent_set_file(nd, &path, state);
1854 nfs4_close_state(&path, state, flags);
1859 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1861 struct nfs_server *server = NFS_SERVER(dir);
1862 struct nfs4_remove_arg args = {
1865 .bitmask = server->attr_bitmask,
1867 struct nfs_fattr dir_attr;
1868 struct nfs4_remove_res res = {
1870 .dir_attr = &dir_attr,
1872 struct rpc_message msg = {
1873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1879 nfs_fattr_init(res.dir_attr);
1880 status = rpc_call_sync(server->client, &msg, 0);
1882 update_changeattr(dir, &res.cinfo);
1883 nfs_post_op_update_inode(dir, res.dir_attr);
1888 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1890 struct nfs4_exception exception = { };
1893 err = nfs4_handle_exception(NFS_SERVER(dir),
1894 _nfs4_proc_remove(dir, name),
1896 } while (exception.retry);
1900 struct unlink_desc {
1901 struct nfs4_remove_arg args;
1902 struct nfs4_remove_res res;
1903 struct nfs_fattr dir_attr;
1906 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1909 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1910 struct unlink_desc *up;
1912 up = kmalloc(sizeof(*up), GFP_KERNEL);
1916 up->args.fh = NFS_FH(dir->d_inode);
1917 up->args.name = name;
1918 up->args.bitmask = server->attr_bitmask;
1919 up->res.server = server;
1920 up->res.dir_attr = &up->dir_attr;
1922 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1923 msg->rpc_argp = &up->args;
1924 msg->rpc_resp = &up->res;
1928 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1930 struct rpc_message *msg = &task->tk_msg;
1931 struct unlink_desc *up;
1933 if (msg->rpc_resp != NULL) {
1934 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1935 update_changeattr(dir->d_inode, &up->res.cinfo);
1936 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1938 msg->rpc_resp = NULL;
1939 msg->rpc_argp = NULL;
1944 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1945 struct inode *new_dir, struct qstr *new_name)
1947 struct nfs_server *server = NFS_SERVER(old_dir);
1948 struct nfs4_rename_arg arg = {
1949 .old_dir = NFS_FH(old_dir),
1950 .new_dir = NFS_FH(new_dir),
1951 .old_name = old_name,
1952 .new_name = new_name,
1953 .bitmask = server->attr_bitmask,
1955 struct nfs_fattr old_fattr, new_fattr;
1956 struct nfs4_rename_res res = {
1958 .old_fattr = &old_fattr,
1959 .new_fattr = &new_fattr,
1961 struct rpc_message msg = {
1962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1968 nfs_fattr_init(res.old_fattr);
1969 nfs_fattr_init(res.new_fattr);
1970 status = rpc_call_sync(server->client, &msg, 0);
1973 update_changeattr(old_dir, &res.old_cinfo);
1974 nfs_post_op_update_inode(old_dir, res.old_fattr);
1975 update_changeattr(new_dir, &res.new_cinfo);
1976 nfs_post_op_update_inode(new_dir, res.new_fattr);
1981 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1982 struct inode *new_dir, struct qstr *new_name)
1984 struct nfs4_exception exception = { };
1987 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1988 _nfs4_proc_rename(old_dir, old_name,
1991 } while (exception.retry);
1995 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1997 struct nfs_server *server = NFS_SERVER(inode);
1998 struct nfs4_link_arg arg = {
1999 .fh = NFS_FH(inode),
2000 .dir_fh = NFS_FH(dir),
2002 .bitmask = server->attr_bitmask,
2004 struct nfs_fattr fattr, dir_attr;
2005 struct nfs4_link_res res = {
2008 .dir_attr = &dir_attr,
2010 struct rpc_message msg = {
2011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2017 nfs_fattr_init(res.fattr);
2018 nfs_fattr_init(res.dir_attr);
2019 status = rpc_call_sync(server->client, &msg, 0);
2021 update_changeattr(dir, &res.cinfo);
2022 nfs_post_op_update_inode(dir, res.dir_attr);
2023 nfs_post_op_update_inode(inode, res.fattr);
2029 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2031 struct nfs4_exception exception = { };
2034 err = nfs4_handle_exception(NFS_SERVER(inode),
2035 _nfs4_proc_link(inode, dir, name),
2037 } while (exception.retry);
2041 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2042 struct page *page, unsigned int len, struct iattr *sattr)
2044 struct nfs_server *server = NFS_SERVER(dir);
2045 struct nfs_fh fhandle;
2046 struct nfs_fattr fattr, dir_fattr;
2047 struct nfs4_create_arg arg = {
2048 .dir_fh = NFS_FH(dir),
2050 .name = &dentry->d_name,
2053 .bitmask = server->attr_bitmask,
2055 struct nfs4_create_res res = {
2059 .dir_fattr = &dir_fattr,
2061 struct rpc_message msg = {
2062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2068 if (len > NFS4_MAXPATHLEN)
2069 return -ENAMETOOLONG;
2071 arg.u.symlink.pages = &page;
2072 arg.u.symlink.len = len;
2073 nfs_fattr_init(&fattr);
2074 nfs_fattr_init(&dir_fattr);
2076 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2078 update_changeattr(dir, &res.dir_cinfo);
2079 nfs_post_op_update_inode(dir, res.dir_fattr);
2080 status = nfs_instantiate(dentry, &fhandle, &fattr);
2085 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2086 struct page *page, unsigned int len, struct iattr *sattr)
2088 struct nfs4_exception exception = { };
2091 err = nfs4_handle_exception(NFS_SERVER(dir),
2092 _nfs4_proc_symlink(dir, dentry, page,
2095 } while (exception.retry);
2099 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2100 struct iattr *sattr)
2102 struct nfs_server *server = NFS_SERVER(dir);
2103 struct nfs_fh fhandle;
2104 struct nfs_fattr fattr, dir_fattr;
2105 struct nfs4_create_arg arg = {
2106 .dir_fh = NFS_FH(dir),
2108 .name = &dentry->d_name,
2111 .bitmask = server->attr_bitmask,
2113 struct nfs4_create_res res = {
2117 .dir_fattr = &dir_fattr,
2119 struct rpc_message msg = {
2120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2126 nfs_fattr_init(&fattr);
2127 nfs_fattr_init(&dir_fattr);
2129 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2131 update_changeattr(dir, &res.dir_cinfo);
2132 nfs_post_op_update_inode(dir, res.dir_fattr);
2133 status = nfs_instantiate(dentry, &fhandle, &fattr);
2138 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2139 struct iattr *sattr)
2141 struct nfs4_exception exception = { };
2144 err = nfs4_handle_exception(NFS_SERVER(dir),
2145 _nfs4_proc_mkdir(dir, dentry, sattr),
2147 } while (exception.retry);
2151 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2152 u64 cookie, struct page *page, unsigned int count, int plus)
2154 struct inode *dir = dentry->d_inode;
2155 struct nfs4_readdir_arg args = {
2160 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2162 struct nfs4_readdir_res res;
2163 struct rpc_message msg = {
2164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2171 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2172 dentry->d_parent->d_name.name,
2173 dentry->d_name.name,
2174 (unsigned long long)cookie);
2175 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2176 res.pgbase = args.pgbase;
2177 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2179 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2180 dprintk("%s: returns %d\n", __FUNCTION__, status);
2184 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2185 u64 cookie, struct page *page, unsigned int count, int plus)
2187 struct nfs4_exception exception = { };
2190 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2191 _nfs4_proc_readdir(dentry, cred, cookie,
2194 } while (exception.retry);
2198 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2199 struct iattr *sattr, dev_t rdev)
2201 struct nfs_server *server = NFS_SERVER(dir);
2203 struct nfs_fattr fattr, dir_fattr;
2204 struct nfs4_create_arg arg = {
2205 .dir_fh = NFS_FH(dir),
2207 .name = &dentry->d_name,
2209 .bitmask = server->attr_bitmask,
2211 struct nfs4_create_res res = {
2215 .dir_fattr = &dir_fattr,
2217 struct rpc_message msg = {
2218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2223 int mode = sattr->ia_mode;
2225 nfs_fattr_init(&fattr);
2226 nfs_fattr_init(&dir_fattr);
2228 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2229 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2231 arg.ftype = NF4FIFO;
2232 else if (S_ISBLK(mode)) {
2234 arg.u.device.specdata1 = MAJOR(rdev);
2235 arg.u.device.specdata2 = MINOR(rdev);
2237 else if (S_ISCHR(mode)) {
2239 arg.u.device.specdata1 = MAJOR(rdev);
2240 arg.u.device.specdata2 = MINOR(rdev);
2243 arg.ftype = NF4SOCK;
2245 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2247 update_changeattr(dir, &res.dir_cinfo);
2248 nfs_post_op_update_inode(dir, res.dir_fattr);
2249 status = nfs_instantiate(dentry, &fh, &fattr);
2254 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2255 struct iattr *sattr, dev_t rdev)
2257 struct nfs4_exception exception = { };
2260 err = nfs4_handle_exception(NFS_SERVER(dir),
2261 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2263 } while (exception.retry);
2267 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2268 struct nfs_fsstat *fsstat)
2270 struct nfs4_statfs_arg args = {
2272 .bitmask = server->attr_bitmask,
2274 struct rpc_message msg = {
2275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2280 nfs_fattr_init(fsstat->fattr);
2281 return rpc_call_sync(server->client, &msg, 0);
2284 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2286 struct nfs4_exception exception = { };
2289 err = nfs4_handle_exception(server,
2290 _nfs4_proc_statfs(server, fhandle, fsstat),
2292 } while (exception.retry);
2296 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2297 struct nfs_fsinfo *fsinfo)
2299 struct nfs4_fsinfo_arg args = {
2301 .bitmask = server->attr_bitmask,
2303 struct rpc_message msg = {
2304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2309 return rpc_call_sync(server->client, &msg, 0);
2312 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2314 struct nfs4_exception exception = { };
2318 err = nfs4_handle_exception(server,
2319 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2321 } while (exception.retry);
2325 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2327 nfs_fattr_init(fsinfo->fattr);
2328 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2331 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2332 struct nfs_pathconf *pathconf)
2334 struct nfs4_pathconf_arg args = {
2336 .bitmask = server->attr_bitmask,
2338 struct rpc_message msg = {
2339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2341 .rpc_resp = pathconf,
2344 /* None of the pathconf attributes are mandatory to implement */
2345 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2346 memset(pathconf, 0, sizeof(*pathconf));
2350 nfs_fattr_init(pathconf->fattr);
2351 return rpc_call_sync(server->client, &msg, 0);
2354 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2355 struct nfs_pathconf *pathconf)
2357 struct nfs4_exception exception = { };
2361 err = nfs4_handle_exception(server,
2362 _nfs4_proc_pathconf(server, fhandle, pathconf),
2364 } while (exception.retry);
2368 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2370 struct nfs_server *server = NFS_SERVER(data->inode);
2372 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
2373 rpc_restart_call(task);
2376 if (task->tk_status > 0)
2377 renew_lease(server, data->timestamp);
2381 static void nfs4_proc_read_setup(struct nfs_read_data *data)
2383 struct rpc_message msg = {
2384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2385 .rpc_argp = &data->args,
2386 .rpc_resp = &data->res,
2387 .rpc_cred = data->cred,
2390 data->timestamp = jiffies;
2392 rpc_call_setup(&data->task, &msg, 0);
2395 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2397 struct inode *inode = data->inode;
2399 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2400 rpc_restart_call(task);
2403 if (task->tk_status >= 0) {
2404 renew_lease(NFS_SERVER(inode), data->timestamp);
2405 nfs_post_op_update_inode(inode, data->res.fattr);
2410 static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2412 struct rpc_message msg = {
2413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2414 .rpc_argp = &data->args,
2415 .rpc_resp = &data->res,
2416 .rpc_cred = data->cred,
2418 struct inode *inode = data->inode;
2419 struct nfs_server *server = NFS_SERVER(inode);
2422 if (how & FLUSH_STABLE) {
2423 if (!NFS_I(inode)->ncommit)
2424 stable = NFS_FILE_SYNC;
2426 stable = NFS_DATA_SYNC;
2428 stable = NFS_UNSTABLE;
2429 data->args.stable = stable;
2430 data->args.bitmask = server->attr_bitmask;
2431 data->res.server = server;
2433 data->timestamp = jiffies;
2435 /* Finalize the task. */
2436 rpc_call_setup(&data->task, &msg, 0);
2439 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2441 struct inode *inode = data->inode;
2443 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2444 rpc_restart_call(task);
2447 if (task->tk_status >= 0)
2448 nfs_post_op_update_inode(inode, data->res.fattr);
2452 static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2454 struct rpc_message msg = {
2455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2456 .rpc_argp = &data->args,
2457 .rpc_resp = &data->res,
2458 .rpc_cred = data->cred,
2460 struct nfs_server *server = NFS_SERVER(data->inode);
2462 data->args.bitmask = server->attr_bitmask;
2463 data->res.server = server;
2465 rpc_call_setup(&data->task, &msg, 0);
2469 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2470 * standalone procedure for queueing an asynchronous RENEW.
2472 static void nfs4_renew_done(struct rpc_task *task, void *data)
2474 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2475 unsigned long timestamp = (unsigned long)data;
2477 if (task->tk_status < 0) {
2478 switch (task->tk_status) {
2479 case -NFS4ERR_STALE_CLIENTID:
2480 case -NFS4ERR_EXPIRED:
2481 case -NFS4ERR_CB_PATH_DOWN:
2482 nfs4_schedule_state_recovery(clp);
2486 spin_lock(&clp->cl_lock);
2487 if (time_before(clp->cl_last_renewal,timestamp))
2488 clp->cl_last_renewal = timestamp;
2489 spin_unlock(&clp->cl_lock);
2492 static const struct rpc_call_ops nfs4_renew_ops = {
2493 .rpc_call_done = nfs4_renew_done,
2496 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2498 struct rpc_message msg = {
2499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2504 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2505 &nfs4_renew_ops, (void *)jiffies);
2508 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2510 struct rpc_message msg = {
2511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2515 unsigned long now = jiffies;
2518 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2521 spin_lock(&clp->cl_lock);
2522 if (time_before(clp->cl_last_renewal,now))
2523 clp->cl_last_renewal = now;
2524 spin_unlock(&clp->cl_lock);
2528 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2530 return (server->caps & NFS_CAP_ACLS)
2531 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2532 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2535 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2536 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2539 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2541 static void buf_to_pages(const void *buf, size_t buflen,
2542 struct page **pages, unsigned int *pgbase)
2544 const void *p = buf;
2546 *pgbase = offset_in_page(buf);
2548 while (p < buf + buflen) {
2549 *(pages++) = virt_to_page(p);
2550 p += PAGE_CACHE_SIZE;
2554 struct nfs4_cached_acl {
2560 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2562 struct nfs_inode *nfsi = NFS_I(inode);
2564 spin_lock(&inode->i_lock);
2565 kfree(nfsi->nfs4_acl);
2566 nfsi->nfs4_acl = acl;
2567 spin_unlock(&inode->i_lock);
2570 static void nfs4_zap_acl_attr(struct inode *inode)
2572 nfs4_set_cached_acl(inode, NULL);
2575 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2577 struct nfs_inode *nfsi = NFS_I(inode);
2578 struct nfs4_cached_acl *acl;
2581 spin_lock(&inode->i_lock);
2582 acl = nfsi->nfs4_acl;
2585 if (buf == NULL) /* user is just asking for length */
2587 if (acl->cached == 0)
2589 ret = -ERANGE; /* see getxattr(2) man page */
2590 if (acl->len > buflen)
2592 memcpy(buf, acl->data, acl->len);
2596 spin_unlock(&inode->i_lock);
2600 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2602 struct nfs4_cached_acl *acl;
2604 if (buf && acl_len <= PAGE_SIZE) {
2605 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2609 memcpy(acl->data, buf, acl_len);
2611 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2618 nfs4_set_cached_acl(inode, acl);
2621 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2623 struct page *pages[NFS4ACL_MAXPAGES];
2624 struct nfs_getaclargs args = {
2625 .fh = NFS_FH(inode),
2629 size_t resp_len = buflen;
2631 struct rpc_message msg = {
2632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2634 .rpc_resp = &resp_len,
2636 struct page *localpage = NULL;
2639 if (buflen < PAGE_SIZE) {
2640 /* As long as we're doing a round trip to the server anyway,
2641 * let's be prepared for a page of acl data. */
2642 localpage = alloc_page(GFP_KERNEL);
2643 resp_buf = page_address(localpage);
2644 if (localpage == NULL)
2646 args.acl_pages[0] = localpage;
2647 args.acl_pgbase = 0;
2648 resp_len = args.acl_len = PAGE_SIZE;
2651 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2653 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2656 if (resp_len > args.acl_len)
2657 nfs4_write_cached_acl(inode, NULL, resp_len);
2659 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2662 if (resp_len > buflen)
2665 memcpy(buf, resp_buf, resp_len);
2670 __free_page(localpage);
2674 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2676 struct nfs4_exception exception = { };
2679 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2682 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2683 } while (exception.retry);
2687 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2689 struct nfs_server *server = NFS_SERVER(inode);
2692 if (!nfs4_server_supports_acls(server))
2694 ret = nfs_revalidate_inode(server, inode);
2697 ret = nfs4_read_cached_acl(inode, buf, buflen);
2700 return nfs4_get_acl_uncached(inode, buf, buflen);
2703 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2705 struct nfs_server *server = NFS_SERVER(inode);
2706 struct page *pages[NFS4ACL_MAXPAGES];
2707 struct nfs_setaclargs arg = {
2708 .fh = NFS_FH(inode),
2712 struct rpc_message msg = {
2713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2719 if (!nfs4_server_supports_acls(server))
2721 nfs_inode_return_delegation(inode);
2722 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2723 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2724 nfs_zap_caches(inode);
2728 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2730 struct nfs4_exception exception = { };
2733 err = nfs4_handle_exception(NFS_SERVER(inode),
2734 __nfs4_proc_set_acl(inode, buf, buflen),
2736 } while (exception.retry);
2741 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2743 struct nfs_client *clp = server->nfs_client;
2745 if (!clp || task->tk_status >= 0)
2747 switch(task->tk_status) {
2748 case -NFS4ERR_STALE_CLIENTID:
2749 case -NFS4ERR_STALE_STATEID:
2750 case -NFS4ERR_EXPIRED:
2751 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2752 nfs4_schedule_state_recovery(clp);
2753 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
2754 rpc_wake_up_task(task);
2755 task->tk_status = 0;
2757 case -NFS4ERR_DELAY:
2758 nfs_inc_server_stats((struct nfs_server *) server,
2760 case -NFS4ERR_GRACE:
2761 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2762 task->tk_status = 0;
2764 case -NFS4ERR_OLD_STATEID:
2765 task->tk_status = 0;
2768 task->tk_status = nfs4_map_errors(task->tk_status);
2772 static int nfs4_wait_bit_interruptible(void *word)
2774 if (signal_pending(current))
2775 return -ERESTARTSYS;
2780 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
2787 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2789 rpc_clnt_sigmask(clnt, &oldset);
2790 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2791 nfs4_wait_bit_interruptible,
2792 TASK_INTERRUPTIBLE);
2793 rpc_clnt_sigunmask(clnt, &oldset);
2795 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
2799 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2807 *timeout = NFS4_POLL_RETRY_MIN;
2808 if (*timeout > NFS4_POLL_RETRY_MAX)
2809 *timeout = NFS4_POLL_RETRY_MAX;
2810 rpc_clnt_sigmask(clnt, &oldset);
2811 if (clnt->cl_intr) {
2812 schedule_timeout_interruptible(*timeout);
2816 schedule_timeout_uninterruptible(*timeout);
2817 rpc_clnt_sigunmask(clnt, &oldset);
2822 /* This is the error handling routine for processes that are allowed
2825 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2827 struct nfs_client *clp = server->nfs_client;
2828 int ret = errorcode;
2830 exception->retry = 0;
2834 case -NFS4ERR_STALE_CLIENTID:
2835 case -NFS4ERR_STALE_STATEID:
2836 case -NFS4ERR_EXPIRED:
2837 nfs4_schedule_state_recovery(clp);
2838 ret = nfs4_wait_clnt_recover(server->client, clp);
2840 exception->retry = 1;
2842 case -NFS4ERR_FILE_OPEN:
2843 case -NFS4ERR_GRACE:
2844 case -NFS4ERR_DELAY:
2845 ret = nfs4_delay(server->client, &exception->timeout);
2848 case -NFS4ERR_OLD_STATEID:
2849 exception->retry = 1;
2851 /* We failed to handle the error */
2852 return nfs4_map_errors(ret);
2855 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
2857 nfs4_verifier sc_verifier;
2858 struct nfs4_setclientid setclientid = {
2859 .sc_verifier = &sc_verifier,
2862 struct rpc_message msg = {
2863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2864 .rpc_argp = &setclientid,
2872 p = (__be32*)sc_verifier.data;
2873 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2874 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2877 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2878 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2879 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
2880 cred->cr_ops->cr_name,
2881 clp->cl_id_uniquifier);
2882 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2883 sizeof(setclientid.sc_netid), "tcp");
2884 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2885 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2886 clp->cl_ipaddr, port >> 8, port & 255);
2888 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2889 if (status != -NFS4ERR_CLID_INUSE)
2894 ssleep(clp->cl_lease_time + 1);
2896 if (++clp->cl_id_uniquifier == 0)
2902 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2904 struct nfs_fsinfo fsinfo;
2905 struct rpc_message msg = {
2906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2908 .rpc_resp = &fsinfo,
2915 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2917 spin_lock(&clp->cl_lock);
2918 clp->cl_lease_time = fsinfo.lease_time * HZ;
2919 clp->cl_last_renewal = now;
2920 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2921 spin_unlock(&clp->cl_lock);
2926 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2931 err = _nfs4_proc_setclientid_confirm(clp, cred);
2935 case -NFS4ERR_RESOURCE:
2936 /* The IBM lawyers misread another document! */
2937 case -NFS4ERR_DELAY:
2938 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2944 struct nfs4_delegreturndata {
2945 struct nfs4_delegreturnargs args;
2946 struct nfs4_delegreturnres res;
2948 nfs4_stateid stateid;
2949 struct rpc_cred *cred;
2950 unsigned long timestamp;
2951 struct nfs_fattr fattr;
2955 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
2957 struct nfs4_delegreturndata *data = calldata;
2958 struct rpc_message msg = {
2959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2960 .rpc_argp = &data->args,
2961 .rpc_resp = &data->res,
2962 .rpc_cred = data->cred,
2964 nfs_fattr_init(data->res.fattr);
2965 rpc_call_setup(task, &msg, 0);
2968 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2970 struct nfs4_delegreturndata *data = calldata;
2971 data->rpc_status = task->tk_status;
2972 if (data->rpc_status == 0)
2973 renew_lease(data->res.server, data->timestamp);
2976 static void nfs4_delegreturn_release(void *calldata)
2978 struct nfs4_delegreturndata *data = calldata;
2980 put_rpccred(data->cred);
2984 static const struct rpc_call_ops nfs4_delegreturn_ops = {
2985 .rpc_call_prepare = nfs4_delegreturn_prepare,
2986 .rpc_call_done = nfs4_delegreturn_done,
2987 .rpc_release = nfs4_delegreturn_release,
2990 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2992 struct nfs4_delegreturndata *data;
2993 struct nfs_server *server = NFS_SERVER(inode);
2994 struct rpc_task *task;
2997 data = kmalloc(sizeof(*data), GFP_KERNEL);
3000 data->args.fhandle = &data->fh;
3001 data->args.stateid = &data->stateid;
3002 data->args.bitmask = server->attr_bitmask;
3003 nfs_copy_fh(&data->fh, NFS_FH(inode));
3004 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3005 data->res.fattr = &data->fattr;
3006 data->res.server = server;
3007 data->cred = get_rpccred(cred);
3008 data->timestamp = jiffies;
3009 data->rpc_status = 0;
3011 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
3013 return PTR_ERR(task);
3014 status = nfs4_wait_for_completion_rpc_task(task);
3016 status = data->rpc_status;
3018 nfs_post_op_update_inode(inode, &data->fattr);
3024 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3026 struct nfs_server *server = NFS_SERVER(inode);
3027 struct nfs4_exception exception = { };
3030 err = _nfs4_proc_delegreturn(inode, cred, stateid);
3032 case -NFS4ERR_STALE_STATEID:
3033 case -NFS4ERR_EXPIRED:
3037 err = nfs4_handle_exception(server, err, &exception);
3038 } while (exception.retry);
3042 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3043 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3046 * sleep, with exponential backoff, and retry the LOCK operation.
3048 static unsigned long
3049 nfs4_set_lock_task_retry(unsigned long timeout)
3051 schedule_timeout_interruptible(timeout);
3053 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3054 return NFS4_LOCK_MAXTIMEOUT;
3058 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3060 struct inode *inode = state->inode;
3061 struct nfs_server *server = NFS_SERVER(inode);
3062 struct nfs_client *clp = server->nfs_client;
3063 struct nfs_lockt_args arg = {
3064 .fh = NFS_FH(inode),
3067 struct nfs_lockt_res res = {
3070 struct rpc_message msg = {
3071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3074 .rpc_cred = state->owner->so_cred,
3076 struct nfs4_lock_state *lsp;
3079 down_read(&clp->cl_sem);
3080 arg.lock_owner.clientid = clp->cl_clientid;
3081 status = nfs4_set_lock_state(state, request);
3084 lsp = request->fl_u.nfs4_fl.owner;
3085 arg.lock_owner.id = lsp->ls_id.id;
3086 status = rpc_call_sync(server->client, &msg, 0);
3089 request->fl_type = F_UNLCK;
3091 case -NFS4ERR_DENIED:
3094 request->fl_ops->fl_release_private(request);
3096 up_read(&clp->cl_sem);
3100 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3102 struct nfs4_exception exception = { };
3106 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3107 _nfs4_proc_getlk(state, cmd, request),
3109 } while (exception.retry);
3113 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3116 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3118 res = posix_lock_file_wait(file, fl);
3121 res = flock_lock_file_wait(file, fl);
3129 struct nfs4_unlockdata {
3130 struct nfs_locku_args arg;
3131 struct nfs_locku_res res;
3132 struct nfs4_lock_state *lsp;
3133 struct nfs_open_context *ctx;
3134 struct file_lock fl;
3135 const struct nfs_server *server;
3136 unsigned long timestamp;
3139 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3140 struct nfs_open_context *ctx,
3141 struct nfs4_lock_state *lsp,
3142 struct nfs_seqid *seqid)
3144 struct nfs4_unlockdata *p;
3145 struct inode *inode = lsp->ls_state->inode;
3147 p = kmalloc(sizeof(*p), GFP_KERNEL);
3150 p->arg.fh = NFS_FH(inode);
3152 p->arg.seqid = seqid;
3153 p->arg.stateid = &lsp->ls_stateid;
3155 atomic_inc(&lsp->ls_count);
3156 /* Ensure we don't close file until we're done freeing locks! */
3157 p->ctx = get_nfs_open_context(ctx);
3158 memcpy(&p->fl, fl, sizeof(p->fl));
3159 p->server = NFS_SERVER(inode);
3163 static void nfs4_locku_release_calldata(void *data)
3165 struct nfs4_unlockdata *calldata = data;
3166 nfs_free_seqid(calldata->arg.seqid);
3167 nfs4_put_lock_state(calldata->lsp);
3168 put_nfs_open_context(calldata->ctx);
3172 static void nfs4_locku_done(struct rpc_task *task, void *data)
3174 struct nfs4_unlockdata *calldata = data;
3176 if (RPC_ASSASSINATED(task))
3178 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
3179 switch (task->tk_status) {
3181 memcpy(calldata->lsp->ls_stateid.data,
3182 calldata->res.stateid.data,
3183 sizeof(calldata->lsp->ls_stateid.data));
3184 renew_lease(calldata->server, calldata->timestamp);
3186 case -NFS4ERR_STALE_STATEID:
3187 case -NFS4ERR_EXPIRED:
3190 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
3191 rpc_restart_call(task);
3195 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3197 struct nfs4_unlockdata *calldata = data;
3198 struct rpc_message msg = {
3199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3200 .rpc_argp = &calldata->arg,
3201 .rpc_resp = &calldata->res,
3202 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
3205 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3207 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3208 /* Note: exit _without_ running nfs4_locku_done */
3209 task->tk_action = NULL;
3212 calldata->timestamp = jiffies;
3213 rpc_call_setup(task, &msg, 0);
3216 static const struct rpc_call_ops nfs4_locku_ops = {
3217 .rpc_call_prepare = nfs4_locku_prepare,
3218 .rpc_call_done = nfs4_locku_done,
3219 .rpc_release = nfs4_locku_release_calldata,
3222 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3223 struct nfs_open_context *ctx,
3224 struct nfs4_lock_state *lsp,
3225 struct nfs_seqid *seqid)
3227 struct nfs4_unlockdata *data;
3229 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3231 nfs_free_seqid(seqid);
3232 return ERR_PTR(-ENOMEM);
3235 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
3238 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3240 struct nfs_seqid *seqid;
3241 struct nfs4_lock_state *lsp;
3242 struct rpc_task *task;
3245 status = nfs4_set_lock_state(state, request);
3246 /* Unlock _before_ we do the RPC call */
3247 request->fl_flags |= FL_EXISTS;
3248 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3252 /* Is this a delegated lock? */
3253 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3255 lsp = request->fl_u.nfs4_fl.owner;
3256 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3260 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3261 status = PTR_ERR(task);
3264 status = nfs4_wait_for_completion_rpc_task(task);
3270 struct nfs4_lockdata {
3271 struct nfs_lock_args arg;
3272 struct nfs_lock_res res;
3273 struct nfs4_lock_state *lsp;
3274 struct nfs_open_context *ctx;
3275 struct file_lock fl;
3276 unsigned long timestamp;
3281 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3282 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3284 struct nfs4_lockdata *p;
3285 struct inode *inode = lsp->ls_state->inode;
3286 struct nfs_server *server = NFS_SERVER(inode);
3288 p = kzalloc(sizeof(*p), GFP_KERNEL);
3292 p->arg.fh = NFS_FH(inode);
3294 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3295 if (p->arg.lock_seqid == NULL)
3297 p->arg.lock_stateid = &lsp->ls_stateid;
3298 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3299 p->arg.lock_owner.id = lsp->ls_id.id;
3301 atomic_inc(&lsp->ls_count);
3302 p->ctx = get_nfs_open_context(ctx);
3303 memcpy(&p->fl, fl, sizeof(p->fl));
3310 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3312 struct nfs4_lockdata *data = calldata;
3313 struct nfs4_state *state = data->lsp->ls_state;
3314 struct nfs4_state_owner *sp = state->owner;
3315 struct rpc_message msg = {
3316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3317 .rpc_argp = &data->arg,
3318 .rpc_resp = &data->res,
3319 .rpc_cred = sp->so_cred,
3322 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3324 dprintk("%s: begin!\n", __FUNCTION__);
3325 /* Do we need to do an open_to_lock_owner? */
3326 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3327 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3328 if (data->arg.open_seqid == NULL) {
3329 data->rpc_status = -ENOMEM;
3330 task->tk_action = NULL;
3333 data->arg.open_stateid = &state->stateid;
3334 data->arg.new_lock_owner = 1;
3336 data->timestamp = jiffies;
3337 rpc_call_setup(task, &msg, 0);
3339 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3342 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3344 struct nfs4_lockdata *data = calldata;
3346 dprintk("%s: begin!\n", __FUNCTION__);
3348 data->rpc_status = task->tk_status;
3349 if (RPC_ASSASSINATED(task))
3351 if (data->arg.new_lock_owner != 0) {
3352 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3353 if (data->rpc_status == 0)
3354 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3358 if (data->rpc_status == 0) {
3359 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3360 sizeof(data->lsp->ls_stateid.data));
3361 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3362 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3364 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3366 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3369 static void nfs4_lock_release(void *calldata)
3371 struct nfs4_lockdata *data = calldata;
3373 dprintk("%s: begin!\n", __FUNCTION__);
3374 if (data->arg.open_seqid != NULL)
3375 nfs_free_seqid(data->arg.open_seqid);
3376 if (data->cancelled != 0) {
3377 struct rpc_task *task;
3378 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3379 data->arg.lock_seqid);
3382 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3384 nfs_free_seqid(data->arg.lock_seqid);
3385 nfs4_put_lock_state(data->lsp);
3386 put_nfs_open_context(data->ctx);
3388 dprintk("%s: done!\n", __FUNCTION__);
3391 static const struct rpc_call_ops nfs4_lock_ops = {
3392 .rpc_call_prepare = nfs4_lock_prepare,
3393 .rpc_call_done = nfs4_lock_done,
3394 .rpc_release = nfs4_lock_release,
3397 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3399 struct nfs4_lockdata *data;
3400 struct rpc_task *task;
3403 dprintk("%s: begin!\n", __FUNCTION__);
3404 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3405 fl->fl_u.nfs4_fl.owner);
3409 data->arg.block = 1;
3411 data->arg.reclaim = 1;
3412 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3413 &nfs4_lock_ops, data);
3415 return PTR_ERR(task);
3416 ret = nfs4_wait_for_completion_rpc_task(task);
3418 ret = data->rpc_status;
3419 if (ret == -NFS4ERR_DENIED)
3422 data->cancelled = 1;
3424 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3428 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3430 struct nfs_server *server = NFS_SERVER(state->inode);
3431 struct nfs4_exception exception = { };
3435 /* Cache the lock if possible... */
3436 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3438 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3439 if (err != -NFS4ERR_DELAY)
3441 nfs4_handle_exception(server, err, &exception);
3442 } while (exception.retry);
3446 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3448 struct nfs_server *server = NFS_SERVER(state->inode);
3449 struct nfs4_exception exception = { };
3452 err = nfs4_set_lock_state(state, request);
3456 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3458 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3459 if (err != -NFS4ERR_DELAY)
3461 nfs4_handle_exception(server, err, &exception);
3462 } while (exception.retry);
3466 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3468 struct nfs_client *clp = state->owner->so_client;
3469 unsigned char fl_flags = request->fl_flags;
3472 /* Is this a delegated open? */
3473 status = nfs4_set_lock_state(state, request);
3476 request->fl_flags |= FL_ACCESS;
3477 status = do_vfs_lock(request->fl_file, request);
3480 down_read(&clp->cl_sem);
3481 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3482 struct nfs_inode *nfsi = NFS_I(state->inode);
3483 /* Yes: cache locks! */
3484 down_read(&nfsi->rwsem);
3485 /* ...but avoid races with delegation recall... */
3486 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3487 request->fl_flags = fl_flags & ~FL_SLEEP;
3488 status = do_vfs_lock(request->fl_file, request);
3489 up_read(&nfsi->rwsem);
3492 up_read(&nfsi->rwsem);
3494 status = _nfs4_do_setlk(state, cmd, request, 0);
3497 /* Note: we always want to sleep here! */
3498 request->fl_flags = fl_flags | FL_SLEEP;
3499 if (do_vfs_lock(request->fl_file, request) < 0)
3500 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3502 up_read(&clp->cl_sem);
3504 request->fl_flags = fl_flags;
3508 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3510 struct nfs4_exception exception = { };
3514 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3515 _nfs4_proc_setlk(state, cmd, request),
3517 } while (exception.retry);
3522 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3524 struct nfs_open_context *ctx;
3525 struct nfs4_state *state;
3526 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3529 /* verify open state */
3530 ctx = (struct nfs_open_context *)filp->private_data;
3533 if (request->fl_start < 0 || request->fl_end < 0)
3537 return nfs4_proc_getlk(state, F_GETLK, request);
3539 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3542 if (request->fl_type == F_UNLCK)
3543 return nfs4_proc_unlck(state, cmd, request);
3546 status = nfs4_proc_setlk(state, cmd, request);
3547 if ((status != -EAGAIN) || IS_SETLK(cmd))
3549 timeout = nfs4_set_lock_task_retry(timeout);
3550 status = -ERESTARTSYS;
3553 } while(status < 0);
3557 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3559 struct nfs_server *server = NFS_SERVER(state->inode);
3560 struct nfs4_exception exception = { };
3563 err = nfs4_set_lock_state(state, fl);
3567 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3568 if (err != -NFS4ERR_DELAY)
3570 err = nfs4_handle_exception(server, err, &exception);
3571 } while (exception.retry);
3576 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3578 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3579 size_t buflen, int flags)
3581 struct inode *inode = dentry->d_inode;
3583 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3586 if (!S_ISREG(inode->i_mode) &&
3587 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3590 return nfs4_proc_set_acl(inode, buf, buflen);
3593 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3594 * and that's what we'll do for e.g. user attributes that haven't been set.
3595 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3596 * attributes in kernel-managed attribute namespaces. */
3597 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3600 struct inode *inode = dentry->d_inode;
3602 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3605 return nfs4_proc_get_acl(inode, buf, buflen);
3608 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3610 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3612 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3614 if (buf && buflen < len)
3617 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3621 int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
3622 struct nfs4_fs_locations *fs_locations, struct page *page)
3624 struct nfs_server *server = NFS_SERVER(dir);
3626 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3627 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3629 struct nfs4_fs_locations_arg args = {
3630 .dir_fh = NFS_FH(dir),
3635 struct rpc_message msg = {
3636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3638 .rpc_resp = fs_locations,
3642 dprintk("%s: start\n", __FUNCTION__);
3643 nfs_fattr_init(&fs_locations->fattr);
3644 fs_locations->server = server;
3645 fs_locations->nlocations = 0;
3646 status = rpc_call_sync(server->client, &msg, 0);
3647 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3651 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3652 .recover_open = nfs4_open_reclaim,
3653 .recover_lock = nfs4_lock_reclaim,
3656 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3657 .recover_open = nfs4_open_expired,
3658 .recover_lock = nfs4_lock_expired,
3661 static const struct inode_operations nfs4_file_inode_operations = {
3662 .permission = nfs_permission,
3663 .getattr = nfs_getattr,
3664 .setattr = nfs_setattr,
3665 .getxattr = nfs4_getxattr,
3666 .setxattr = nfs4_setxattr,
3667 .listxattr = nfs4_listxattr,
3670 const struct nfs_rpc_ops nfs_v4_clientops = {
3671 .version = 4, /* protocol version */
3672 .dentry_ops = &nfs4_dentry_operations,
3673 .dir_inode_ops = &nfs4_dir_inode_operations,
3674 .file_inode_ops = &nfs4_file_inode_operations,
3675 .getroot = nfs4_proc_get_root,
3676 .getattr = nfs4_proc_getattr,
3677 .setattr = nfs4_proc_setattr,
3678 .lookupfh = nfs4_proc_lookupfh,
3679 .lookup = nfs4_proc_lookup,
3680 .access = nfs4_proc_access,
3681 .readlink = nfs4_proc_readlink,
3682 .create = nfs4_proc_create,
3683 .remove = nfs4_proc_remove,
3684 .unlink_setup = nfs4_proc_unlink_setup,
3685 .unlink_done = nfs4_proc_unlink_done,
3686 .rename = nfs4_proc_rename,
3687 .link = nfs4_proc_link,
3688 .symlink = nfs4_proc_symlink,
3689 .mkdir = nfs4_proc_mkdir,
3690 .rmdir = nfs4_proc_remove,
3691 .readdir = nfs4_proc_readdir,
3692 .mknod = nfs4_proc_mknod,
3693 .statfs = nfs4_proc_statfs,
3694 .fsinfo = nfs4_proc_fsinfo,
3695 .pathconf = nfs4_proc_pathconf,
3696 .set_capabilities = nfs4_server_capabilities,
3697 .decode_dirent = nfs4_decode_dirent,
3698 .read_setup = nfs4_proc_read_setup,
3699 .read_done = nfs4_read_done,
3700 .write_setup = nfs4_proc_write_setup,
3701 .write_done = nfs4_write_done,
3702 .commit_setup = nfs4_proc_commit_setup,
3703 .commit_done = nfs4_commit_done,
3704 .file_open = nfs_open,
3705 .file_release = nfs_release,
3706 .lock = nfs4_proc_lock,
3707 .clear_acl_cache = nfs4_zap_acl_attr,