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;
391 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
393 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
394 ret = PTR_ERR(inode);
398 state = nfs4_get_open_state(inode, data->owner);
401 if (data->o_res.delegation_type != 0) {
402 int delegation_flags = 0;
405 delegation = rcu_dereference(NFS_I(inode)->delegation);
407 delegation_flags = delegation->flags;
409 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
410 nfs_inode_set_delegation(state->inode,
411 data->owner->so_cred,
414 nfs_inode_reclaim_delegation(state->inode,
415 data->owner->so_cred,
419 delegation = rcu_dereference(NFS_I(inode)->delegation);
420 if (delegation != NULL)
421 deleg_stateid = &delegation->stateid;
422 update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags);
432 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
434 struct nfs_inode *nfsi = NFS_I(state->inode);
435 struct nfs_open_context *ctx;
437 spin_lock(&state->inode->i_lock);
438 list_for_each_entry(ctx, &nfsi->open_files, list) {
439 if (ctx->state != state)
441 get_nfs_open_context(ctx);
442 spin_unlock(&state->inode->i_lock);
445 spin_unlock(&state->inode->i_lock);
446 return ERR_PTR(-ENOENT);
449 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
451 struct nfs4_state *newstate;
454 opendata->o_arg.open_flags = openflags;
455 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
456 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
457 nfs4_init_opendata_res(opendata);
458 ret = _nfs4_proc_open(opendata);
461 newstate = nfs4_opendata_to_nfs4_state(opendata);
462 if (IS_ERR(newstate))
463 return PTR_ERR(newstate);
464 nfs4_close_state(&opendata->path, newstate, openflags);
469 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
471 struct nfs4_state *newstate;
474 /* memory barrier prior to reading state->n_* */
475 clear_bit(NFS_DELEGATED_STATE, &state->flags);
477 if (state->n_rdwr != 0) {
478 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
481 if (newstate != state)
484 if (state->n_wronly != 0) {
485 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
488 if (newstate != state)
491 if (state->n_rdonly != 0) {
492 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
495 if (newstate != state)
503 * reclaim state on the server after a reboot.
505 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
507 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
508 struct nfs4_opendata *opendata;
509 int delegation_type = 0;
512 if (delegation != NULL) {
513 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
514 memcpy(&state->stateid, &delegation->stateid,
515 sizeof(state->stateid));
516 set_bit(NFS_DELEGATED_STATE, &state->flags);
519 delegation_type = delegation->type;
521 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
522 if (opendata == NULL)
524 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
525 opendata->o_arg.fh = NFS_FH(state->inode);
526 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
527 opendata->o_arg.u.delegation_type = delegation_type;
528 status = nfs4_open_recover(opendata, state);
529 nfs4_opendata_put(opendata);
533 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
535 struct nfs_server *server = NFS_SERVER(state->inode);
536 struct nfs4_exception exception = { };
539 err = _nfs4_do_open_reclaim(ctx, state);
540 if (err != -NFS4ERR_DELAY)
542 nfs4_handle_exception(server, err, &exception);
543 } while (exception.retry);
547 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
549 struct nfs_open_context *ctx;
552 ctx = nfs4_state_find_open_context(state);
555 ret = nfs4_do_open_reclaim(ctx, state);
556 put_nfs_open_context(ctx);
560 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
562 struct nfs4_state_owner *sp = state->owner;
563 struct nfs4_opendata *opendata;
566 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
567 if (opendata == NULL)
569 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
570 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
571 sizeof(opendata->o_arg.u.delegation.data));
572 ret = nfs4_open_recover(opendata, state);
573 nfs4_opendata_put(opendata);
577 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
579 struct nfs4_exception exception = { };
580 struct nfs_server *server = NFS_SERVER(state->inode);
583 err = _nfs4_open_delegation_recall(ctx, state, stateid);
587 case -NFS4ERR_STALE_CLIENTID:
588 case -NFS4ERR_STALE_STATEID:
589 case -NFS4ERR_EXPIRED:
590 /* Don't recall a delegation if it was lost */
591 nfs4_schedule_state_recovery(server->nfs_client);
594 err = nfs4_handle_exception(server, err, &exception);
595 } while (exception.retry);
599 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
601 struct nfs4_opendata *data = calldata;
602 struct rpc_message msg = {
603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
604 .rpc_argp = &data->c_arg,
605 .rpc_resp = &data->c_res,
606 .rpc_cred = data->owner->so_cred,
608 data->timestamp = jiffies;
609 rpc_call_setup(task, &msg, 0);
612 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
614 struct nfs4_opendata *data = calldata;
616 data->rpc_status = task->tk_status;
617 if (RPC_ASSASSINATED(task))
619 if (data->rpc_status == 0) {
620 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
621 sizeof(data->o_res.stateid.data));
622 renew_lease(data->o_res.server, data->timestamp);
624 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
625 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
628 static void nfs4_open_confirm_release(void *calldata)
630 struct nfs4_opendata *data = calldata;
631 struct nfs4_state *state = NULL;
633 /* If this request hasn't been cancelled, do nothing */
634 if (data->cancelled == 0)
636 /* In case of error, no cleanup! */
637 if (data->rpc_status != 0)
639 nfs_confirm_seqid(&data->owner->so_seqid, 0);
640 state = nfs4_opendata_to_nfs4_state(data);
642 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
644 nfs4_opendata_put(data);
647 static const struct rpc_call_ops nfs4_open_confirm_ops = {
648 .rpc_call_prepare = nfs4_open_confirm_prepare,
649 .rpc_call_done = nfs4_open_confirm_done,
650 .rpc_release = nfs4_open_confirm_release,
654 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
656 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
658 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
659 struct rpc_task *task;
662 kref_get(&data->kref);
664 * If rpc_run_task() ends up calling ->rpc_release(), we
665 * want to ensure that it takes the 'error' code path.
667 data->rpc_status = -ENOMEM;
668 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
670 return PTR_ERR(task);
671 status = nfs4_wait_for_completion_rpc_task(task);
676 status = data->rpc_status;
681 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
683 struct nfs4_opendata *data = calldata;
684 struct nfs4_state_owner *sp = data->owner;
685 struct rpc_message msg = {
686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
687 .rpc_argp = &data->o_arg,
688 .rpc_resp = &data->o_res,
689 .rpc_cred = sp->so_cred,
692 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
694 /* Update sequence id. */
695 data->o_arg.id = sp->so_owner_id.id;
696 data->o_arg.clientid = sp->so_client->cl_clientid;
697 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
698 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
699 data->timestamp = jiffies;
700 rpc_call_setup(task, &msg, 0);
703 static void nfs4_open_done(struct rpc_task *task, void *calldata)
705 struct nfs4_opendata *data = calldata;
707 data->rpc_status = task->tk_status;
708 if (RPC_ASSASSINATED(task))
710 if (task->tk_status == 0) {
711 switch (data->o_res.f_attr->mode & S_IFMT) {
715 data->rpc_status = -ELOOP;
718 data->rpc_status = -EISDIR;
721 data->rpc_status = -ENOTDIR;
723 renew_lease(data->o_res.server, data->timestamp);
724 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
725 nfs_confirm_seqid(&data->owner->so_seqid, 0);
727 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
730 static void nfs4_open_release(void *calldata)
732 struct nfs4_opendata *data = calldata;
733 struct nfs4_state *state = NULL;
735 /* If this request hasn't been cancelled, do nothing */
736 if (data->cancelled == 0)
738 /* In case of error, no cleanup! */
739 if (data->rpc_status != 0)
741 /* In case we need an open_confirm, no cleanup! */
742 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
744 nfs_confirm_seqid(&data->owner->so_seqid, 0);
745 state = nfs4_opendata_to_nfs4_state(data);
747 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
749 nfs4_opendata_put(data);
752 static const struct rpc_call_ops nfs4_open_ops = {
753 .rpc_call_prepare = nfs4_open_prepare,
754 .rpc_call_done = nfs4_open_done,
755 .rpc_release = nfs4_open_release,
759 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
761 static int _nfs4_proc_open(struct nfs4_opendata *data)
763 struct inode *dir = data->dir->d_inode;
764 struct nfs_server *server = NFS_SERVER(dir);
765 struct nfs_openargs *o_arg = &data->o_arg;
766 struct nfs_openres *o_res = &data->o_res;
767 struct rpc_task *task;
770 kref_get(&data->kref);
772 * If rpc_run_task() ends up calling ->rpc_release(), we
773 * want to ensure that it takes the 'error' code path.
775 data->rpc_status = -ENOMEM;
777 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
779 return PTR_ERR(task);
780 status = nfs4_wait_for_completion_rpc_task(task);
785 status = data->rpc_status;
790 if (o_arg->open_flags & O_CREAT) {
791 update_changeattr(dir, &o_res->cinfo);
792 nfs_post_op_update_inode(dir, o_res->dir_attr);
794 nfs_refresh_inode(dir, o_res->dir_attr);
795 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
796 status = _nfs4_proc_open_confirm(data);
800 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
801 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
805 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
807 struct nfs_access_entry cache;
811 if (openflags & FMODE_READ)
813 if (openflags & FMODE_WRITE)
815 if (openflags & FMODE_EXEC)
817 status = nfs_access_get_cached(inode, cred, &cache);
821 /* Be clever: ask server to check for all possible rights */
822 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
824 cache.jiffies = jiffies;
825 status = _nfs4_proc_access(inode, &cache);
828 nfs_access_add_cache(inode, &cache);
830 if ((cache.mask & mask) == mask)
835 static int nfs4_recover_expired_lease(struct nfs_server *server)
837 struct nfs_client *clp = server->nfs_client;
841 ret = nfs4_wait_clnt_recover(server->client, clp);
844 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
846 nfs4_schedule_state_recovery(clp);
853 * reclaim state on the server after a network partition.
854 * Assumes caller holds the appropriate lock
856 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
858 struct inode *inode = state->inode;
859 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
860 struct nfs4_opendata *opendata;
861 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
864 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
865 ret = _nfs4_do_access(inode, ctx->cred, openflags);
868 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
869 set_bit(NFS_DELEGATED_STATE, &state->flags);
872 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
873 if (opendata == NULL)
875 ret = nfs4_open_recover(opendata, state);
876 if (ret == -ESTALE) {
877 /* Invalidate the state owner so we don't ever use it again */
878 nfs4_drop_state_owner(state->owner);
879 d_drop(ctx->path.dentry);
881 nfs4_opendata_put(opendata);
885 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
887 struct nfs_server *server = NFS_SERVER(state->inode);
888 struct nfs4_exception exception = { };
892 err = _nfs4_open_expired(ctx, state);
893 if (err == -NFS4ERR_DELAY)
894 nfs4_handle_exception(server, err, &exception);
895 } while (exception.retry);
899 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
901 struct nfs_open_context *ctx;
904 ctx = nfs4_state_find_open_context(state);
907 ret = nfs4_do_open_expired(ctx, state);
908 put_nfs_open_context(ctx);
913 * Returns a referenced nfs4_state if there is an open delegation on the file
915 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
917 struct nfs_delegation *delegation;
918 struct nfs_server *server = NFS_SERVER(inode);
919 struct nfs_client *clp = server->nfs_client;
920 struct nfs_inode *nfsi = NFS_I(inode);
921 struct nfs4_state_owner *sp = NULL;
922 struct nfs4_state *state = NULL;
923 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
927 if (!(sp = nfs4_get_state_owner(server, cred))) {
928 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
931 err = nfs4_recover_expired_lease(server);
933 goto out_put_state_owner;
934 /* Protect against reboot recovery - NOTE ORDER! */
935 down_read(&clp->cl_sem);
936 /* Protect against delegation recall */
937 down_read(&nfsi->rwsem);
938 delegation = NFS_I(inode)->delegation;
940 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
943 state = nfs4_get_open_state(inode, sp);
948 if ((state->state & open_flags) == open_flags) {
949 spin_lock(&inode->i_lock);
950 update_open_stateflags(state, open_flags);
951 spin_unlock(&inode->i_lock);
953 } else if (state->state != 0)
954 goto out_put_open_state;
957 err = _nfs4_do_access(inode, cred, open_flags);
960 goto out_put_open_state;
961 update_open_stateid(state, NULL, &delegation->stateid, open_flags);
963 nfs4_put_state_owner(sp);
964 up_read(&nfsi->rwsem);
965 up_read(&clp->cl_sem);
969 nfs4_put_open_state(state);
971 up_read(&nfsi->rwsem);
972 up_read(&clp->cl_sem);
974 nfs_inode_return_delegation(inode);
976 nfs4_put_state_owner(sp);
980 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
982 struct nfs4_exception exception = { };
983 struct nfs4_state *res = ERR_PTR(-EIO);
987 err = _nfs4_open_delegated(inode, flags, cred, &res);
990 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
992 } while (exception.retry);
997 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
998 * fields corresponding to attributes that were used to store the verifier.
999 * Make sure we clobber those fields in the later setattr call
1001 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1003 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1004 !(sattr->ia_valid & ATTR_ATIME_SET))
1005 sattr->ia_valid |= ATTR_ATIME;
1007 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1008 !(sattr->ia_valid & ATTR_MTIME_SET))
1009 sattr->ia_valid |= ATTR_MTIME;
1013 * Returns a referenced nfs4_state
1015 static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1017 struct nfs4_state_owner *sp;
1018 struct nfs4_state *state = NULL;
1019 struct nfs_server *server = NFS_SERVER(dir);
1020 struct nfs_client *clp = server->nfs_client;
1021 struct nfs4_opendata *opendata;
1024 /* Protect against reboot recovery conflicts */
1026 if (!(sp = nfs4_get_state_owner(server, cred))) {
1027 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1030 status = nfs4_recover_expired_lease(server);
1032 goto err_put_state_owner;
1033 down_read(&clp->cl_sem);
1035 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
1036 if (opendata == NULL)
1037 goto err_release_rwsem;
1039 status = _nfs4_proc_open(opendata);
1041 goto err_opendata_put;
1043 if (opendata->o_arg.open_flags & O_EXCL)
1044 nfs4_exclusive_attrset(opendata, sattr);
1046 state = nfs4_opendata_to_nfs4_state(opendata);
1047 status = PTR_ERR(state);
1049 goto err_opendata_put;
1050 nfs4_opendata_put(opendata);
1051 nfs4_put_state_owner(sp);
1052 up_read(&clp->cl_sem);
1056 nfs4_opendata_put(opendata);
1058 up_read(&clp->cl_sem);
1059 err_put_state_owner:
1060 nfs4_put_state_owner(sp);
1067 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
1069 struct nfs4_exception exception = { };
1070 struct nfs4_state *res;
1074 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
1077 /* NOTE: BAD_SEQID means the server and client disagree about the
1078 * book-keeping w.r.t. state-changing operations
1079 * (OPEN/CLOSE/LOCK/LOCKU...)
1080 * It is actually a sign of a bug on the client or on the server.
1082 * If we receive a BAD_SEQID error in the particular case of
1083 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1084 * have unhashed the old state_owner for us, and that we can
1085 * therefore safely retry using a new one. We should still warn
1086 * the user though...
1088 if (status == -NFS4ERR_BAD_SEQID) {
1089 printk(KERN_WARNING "NFS: v4 server %s "
1090 " returned a bad sequence-id error!\n",
1091 NFS_SERVER(dir)->nfs_client->cl_hostname);
1092 exception.retry = 1;
1096 * BAD_STATEID on OPEN means that the server cancelled our
1097 * state before it received the OPEN_CONFIRM.
1098 * Recover by retrying the request as per the discussion
1099 * on Page 181 of RFC3530.
1101 if (status == -NFS4ERR_BAD_STATEID) {
1102 exception.retry = 1;
1105 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1106 status, &exception));
1107 } while (exception.retry);
1111 static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1112 struct iattr *sattr, struct nfs4_state *state)
1114 struct nfs_server *server = NFS_SERVER(inode);
1115 struct nfs_setattrargs arg = {
1116 .fh = NFS_FH(inode),
1119 .bitmask = server->attr_bitmask,
1121 struct nfs_setattrres res = {
1125 struct rpc_message msg = {
1126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1130 unsigned long timestamp = jiffies;
1133 nfs_fattr_init(fattr);
1135 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1136 /* Use that stateid */
1137 } else if (state != NULL) {
1138 msg.rpc_cred = state->owner->so_cred;
1139 nfs4_copy_stateid(&arg.stateid, state, current->files);
1141 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1143 status = rpc_call_sync(server->client, &msg, 0);
1144 if (status == 0 && state != NULL)
1145 renew_lease(server, timestamp);
1149 static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1150 struct iattr *sattr, struct nfs4_state *state)
1152 struct nfs_server *server = NFS_SERVER(inode);
1153 struct nfs4_exception exception = { };
1156 err = nfs4_handle_exception(server,
1157 _nfs4_do_setattr(inode, fattr, sattr, state),
1159 } while (exception.retry);
1163 struct nfs4_closedata {
1165 struct inode *inode;
1166 struct nfs4_state *state;
1167 struct nfs_closeargs arg;
1168 struct nfs_closeres res;
1169 struct nfs_fattr fattr;
1170 unsigned long timestamp;
1173 static void nfs4_free_closedata(void *data)
1175 struct nfs4_closedata *calldata = data;
1176 struct nfs4_state_owner *sp = calldata->state->owner;
1178 nfs4_put_open_state(calldata->state);
1179 nfs_free_seqid(calldata->arg.seqid);
1180 nfs4_put_state_owner(sp);
1181 dput(calldata->path.dentry);
1182 mntput(calldata->path.mnt);
1186 static void nfs4_close_done(struct rpc_task *task, void *data)
1188 struct nfs4_closedata *calldata = data;
1189 struct nfs4_state *state = calldata->state;
1190 struct nfs_server *server = NFS_SERVER(calldata->inode);
1192 if (RPC_ASSASSINATED(task))
1194 /* hmm. we are done with the inode, and in the process of freeing
1195 * the state_owner. we keep this around to process errors
1197 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1198 switch (task->tk_status) {
1200 nfs_set_open_stateid(state, &calldata->res.stateid, calldata->arg.open_flags);
1201 renew_lease(server, calldata->timestamp);
1203 case -NFS4ERR_STALE_STATEID:
1204 case -NFS4ERR_EXPIRED:
1207 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1208 rpc_restart_call(task);
1212 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1215 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1217 struct nfs4_closedata *calldata = data;
1218 struct nfs4_state *state = calldata->state;
1219 struct rpc_message msg = {
1220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1221 .rpc_argp = &calldata->arg,
1222 .rpc_resp = &calldata->res,
1223 .rpc_cred = state->owner->so_cred,
1225 int clear_rd, clear_wr, clear_rdwr;
1228 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1231 mode = FMODE_READ|FMODE_WRITE;
1232 clear_rd = clear_wr = clear_rdwr = 0;
1233 spin_lock(&state->owner->so_lock);
1234 spin_lock(&calldata->inode->i_lock);
1235 /* Calculate the change in open mode */
1236 if (state->n_rdwr == 0) {
1237 if (state->n_rdonly == 0) {
1238 mode &= ~FMODE_READ;
1239 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1240 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1242 if (state->n_wronly == 0) {
1243 mode &= ~FMODE_WRITE;
1244 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1245 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1248 spin_unlock(&calldata->inode->i_lock);
1249 spin_unlock(&state->owner->so_lock);
1250 if (!clear_rd && !clear_wr && !clear_rdwr) {
1251 /* Note: exit _without_ calling nfs4_close_done */
1252 task->tk_action = NULL;
1255 nfs_fattr_init(calldata->res.fattr);
1257 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1258 calldata->arg.open_flags = mode;
1259 calldata->timestamp = jiffies;
1260 rpc_call_setup(task, &msg, 0);
1263 static const struct rpc_call_ops nfs4_close_ops = {
1264 .rpc_call_prepare = nfs4_close_prepare,
1265 .rpc_call_done = nfs4_close_done,
1266 .rpc_release = nfs4_free_closedata,
1270 * It is possible for data to be read/written from a mem-mapped file
1271 * after the sys_close call (which hits the vfs layer as a flush).
1272 * This means that we can't safely call nfsv4 close on a file until
1273 * the inode is cleared. This in turn means that we are not good
1274 * NFSv4 citizens - we do not indicate to the server to update the file's
1275 * share state even when we are done with one of the three share
1276 * stateid's in the inode.
1278 * NOTE: Caller must be holding the sp->so_owner semaphore!
1280 int nfs4_do_close(struct path *path, struct nfs4_state *state)
1282 struct nfs_server *server = NFS_SERVER(state->inode);
1283 struct nfs4_closedata *calldata;
1284 struct nfs4_state_owner *sp = state->owner;
1285 struct rpc_task *task;
1286 int status = -ENOMEM;
1288 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1289 if (calldata == NULL)
1291 calldata->inode = state->inode;
1292 calldata->state = state;
1293 calldata->arg.fh = NFS_FH(state->inode);
1294 calldata->arg.stateid = &state->open_stateid;
1295 /* Serialization for the sequence id */
1296 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1297 if (calldata->arg.seqid == NULL)
1298 goto out_free_calldata;
1299 calldata->arg.bitmask = server->attr_bitmask;
1300 calldata->res.fattr = &calldata->fattr;
1301 calldata->res.server = server;
1302 calldata->path.mnt = mntget(path->mnt);
1303 calldata->path.dentry = dget(path->dentry);
1305 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1307 return PTR_ERR(task);
1313 nfs4_put_open_state(state);
1314 nfs4_put_state_owner(sp);
1318 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
1323 /* If the open_intent is for execute, we have an extra check to make */
1324 if (nd->intent.open.flags & FMODE_EXEC) {
1325 ret = _nfs4_do_access(state->inode,
1326 state->owner->so_cred,
1327 nd->intent.open.flags);
1331 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1332 if (!IS_ERR(filp)) {
1333 struct nfs_open_context *ctx;
1334 ctx = (struct nfs_open_context *)filp->private_data;
1338 ret = PTR_ERR(filp);
1340 nfs4_close_state(path, state, nd->intent.open.flags);
1345 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1347 struct path path = {
1352 struct rpc_cred *cred;
1353 struct nfs4_state *state;
1356 if (nd->flags & LOOKUP_CREATE) {
1357 attr.ia_mode = nd->intent.open.create_mode;
1358 attr.ia_valid = ATTR_MODE;
1359 if (!IS_POSIXACL(dir))
1360 attr.ia_mode &= ~current->fs->umask;
1363 BUG_ON(nd->intent.open.flags & O_CREAT);
1366 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1368 return (struct dentry *)cred;
1369 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
1371 if (IS_ERR(state)) {
1372 if (PTR_ERR(state) == -ENOENT)
1373 d_add(dentry, NULL);
1374 return (struct dentry *)state;
1376 res = d_add_unique(dentry, igrab(state->inode));
1379 nfs4_intent_set_file(nd, &path, state);
1384 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1386 struct path path = {
1390 struct rpc_cred *cred;
1391 struct nfs4_state *state;
1393 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1395 return PTR_ERR(cred);
1396 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1398 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
1400 if (IS_ERR(state)) {
1401 switch (PTR_ERR(state)) {
1407 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1413 if (state->inode == dentry->d_inode) {
1414 nfs4_intent_set_file(nd, &path, state);
1417 nfs4_close_state(&path, state, openflags);
1424 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1426 struct nfs4_server_caps_res res = {};
1427 struct rpc_message msg = {
1428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1429 .rpc_argp = fhandle,
1434 status = rpc_call_sync(server->client, &msg, 0);
1436 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1437 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1438 server->caps |= NFS_CAP_ACLS;
1439 if (res.has_links != 0)
1440 server->caps |= NFS_CAP_HARDLINKS;
1441 if (res.has_symlinks != 0)
1442 server->caps |= NFS_CAP_SYMLINKS;
1443 server->acl_bitmask = res.acl_bitmask;
1448 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1450 struct nfs4_exception exception = { };
1453 err = nfs4_handle_exception(server,
1454 _nfs4_server_capabilities(server, fhandle),
1456 } while (exception.retry);
1460 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1461 struct nfs_fsinfo *info)
1463 struct nfs4_lookup_root_arg args = {
1464 .bitmask = nfs4_fattr_bitmap,
1466 struct nfs4_lookup_res res = {
1468 .fattr = info->fattr,
1471 struct rpc_message msg = {
1472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1476 nfs_fattr_init(info->fattr);
1477 return rpc_call_sync(server->client, &msg, 0);
1480 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1481 struct nfs_fsinfo *info)
1483 struct nfs4_exception exception = { };
1486 err = nfs4_handle_exception(server,
1487 _nfs4_lookup_root(server, fhandle, info),
1489 } while (exception.retry);
1494 * get the file handle for the "/" directory on the server
1496 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1497 struct nfs_fsinfo *info)
1501 status = nfs4_lookup_root(server, fhandle, info);
1503 status = nfs4_server_capabilities(server, fhandle);
1505 status = nfs4_do_fsinfo(server, fhandle, info);
1506 return nfs4_map_errors(status);
1510 * Get locations and (maybe) other attributes of a referral.
1511 * Note that we'll actually follow the referral later when
1512 * we detect fsid mismatch in inode revalidation
1514 static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1516 int status = -ENOMEM;
1517 struct page *page = NULL;
1518 struct nfs4_fs_locations *locations = NULL;
1520 page = alloc_page(GFP_KERNEL);
1523 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1524 if (locations == NULL)
1527 status = nfs4_proc_fs_locations(dir, name, locations, page);
1530 /* Make sure server returned a different fsid for the referral */
1531 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1532 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1537 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1538 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1540 fattr->mode = S_IFDIR;
1541 memset(fhandle, 0, sizeof(struct nfs_fh));
1550 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1552 struct nfs4_getattr_arg args = {
1554 .bitmask = server->attr_bitmask,
1556 struct nfs4_getattr_res res = {
1560 struct rpc_message msg = {
1561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1566 nfs_fattr_init(fattr);
1567 return rpc_call_sync(server->client, &msg, 0);
1570 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1572 struct nfs4_exception exception = { };
1575 err = nfs4_handle_exception(server,
1576 _nfs4_proc_getattr(server, fhandle, fattr),
1578 } while (exception.retry);
1583 * The file is not closed if it is opened due to the a request to change
1584 * the size of the file. The open call will not be needed once the
1585 * VFS layer lookup-intents are implemented.
1587 * Close is called when the inode is destroyed.
1588 * If we haven't opened the file for O_WRONLY, we
1589 * need to in the size_change case to obtain a stateid.
1592 * Because OPEN is always done by name in nfsv4, it is
1593 * possible that we opened a different file by the same
1594 * name. We can recognize this race condition, but we
1595 * can't do anything about it besides returning an error.
1597 * This will be fixed with VFS changes (lookup-intent).
1600 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1601 struct iattr *sattr)
1603 struct rpc_cred *cred;
1604 struct inode *inode = dentry->d_inode;
1605 struct nfs_open_context *ctx;
1606 struct nfs4_state *state = NULL;
1609 nfs_fattr_init(fattr);
1611 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1613 return PTR_ERR(cred);
1615 /* Search for an existing open(O_WRITE) file */
1616 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1620 status = nfs4_do_setattr(inode, fattr, sattr, state);
1622 nfs_setattr_update_inode(inode, sattr);
1624 put_nfs_open_context(ctx);
1629 static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1630 struct qstr *name, struct nfs_fh *fhandle,
1631 struct nfs_fattr *fattr)
1634 struct nfs4_lookup_arg args = {
1635 .bitmask = server->attr_bitmask,
1639 struct nfs4_lookup_res res = {
1644 struct rpc_message msg = {
1645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1650 nfs_fattr_init(fattr);
1652 dprintk("NFS call lookupfh %s\n", name->name);
1653 status = rpc_call_sync(server->client, &msg, 0);
1654 dprintk("NFS reply lookupfh: %d\n", status);
1658 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1659 struct qstr *name, struct nfs_fh *fhandle,
1660 struct nfs_fattr *fattr)
1662 struct nfs4_exception exception = { };
1665 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1667 if (err == -NFS4ERR_MOVED) {
1671 err = nfs4_handle_exception(server, err, &exception);
1672 } while (exception.retry);
1676 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1677 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1681 dprintk("NFS call lookup %s\n", name->name);
1682 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
1683 if (status == -NFS4ERR_MOVED)
1684 status = nfs4_get_referral(dir, name, fattr, fhandle);
1685 dprintk("NFS reply lookup: %d\n", status);
1689 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1691 struct nfs4_exception exception = { };
1694 err = nfs4_handle_exception(NFS_SERVER(dir),
1695 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1697 } while (exception.retry);
1701 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1703 struct nfs4_accessargs args = {
1704 .fh = NFS_FH(inode),
1706 struct nfs4_accessres res = { 0 };
1707 struct rpc_message msg = {
1708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1711 .rpc_cred = entry->cred,
1713 int mode = entry->mask;
1717 * Determine which access bits we want to ask for...
1719 if (mode & MAY_READ)
1720 args.access |= NFS4_ACCESS_READ;
1721 if (S_ISDIR(inode->i_mode)) {
1722 if (mode & MAY_WRITE)
1723 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1724 if (mode & MAY_EXEC)
1725 args.access |= NFS4_ACCESS_LOOKUP;
1727 if (mode & MAY_WRITE)
1728 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1729 if (mode & MAY_EXEC)
1730 args.access |= NFS4_ACCESS_EXECUTE;
1732 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1735 if (res.access & NFS4_ACCESS_READ)
1736 entry->mask |= MAY_READ;
1737 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1738 entry->mask |= MAY_WRITE;
1739 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1740 entry->mask |= MAY_EXEC;
1745 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1747 struct nfs4_exception exception = { };
1750 err = nfs4_handle_exception(NFS_SERVER(inode),
1751 _nfs4_proc_access(inode, entry),
1753 } while (exception.retry);
1758 * TODO: For the time being, we don't try to get any attributes
1759 * along with any of the zero-copy operations READ, READDIR,
1762 * In the case of the first three, we want to put the GETATTR
1763 * after the read-type operation -- this is because it is hard
1764 * to predict the length of a GETATTR response in v4, and thus
1765 * align the READ data correctly. This means that the GETATTR
1766 * may end up partially falling into the page cache, and we should
1767 * shift it into the 'tail' of the xdr_buf before processing.
1768 * To do this efficiently, we need to know the total length
1769 * of data received, which doesn't seem to be available outside
1772 * In the case of WRITE, we also want to put the GETATTR after
1773 * the operation -- in this case because we want to make sure
1774 * we get the post-operation mtime and size. This means that
1775 * we can't use xdr_encode_pages() as written: we need a variant
1776 * of it which would leave room in the 'tail' iovec.
1778 * Both of these changes to the XDR layer would in fact be quite
1779 * minor, but I decided to leave them for a subsequent patch.
1781 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1782 unsigned int pgbase, unsigned int pglen)
1784 struct nfs4_readlink args = {
1785 .fh = NFS_FH(inode),
1790 struct rpc_message msg = {
1791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1796 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1799 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1800 unsigned int pgbase, unsigned int pglen)
1802 struct nfs4_exception exception = { };
1805 err = nfs4_handle_exception(NFS_SERVER(inode),
1806 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1808 } while (exception.retry);
1814 * We will need to arrange for the VFS layer to provide an atomic open.
1815 * Until then, this create/open method is prone to inefficiency and race
1816 * conditions due to the lookup, create, and open VFS calls from sys_open()
1817 * placed on the wire.
1819 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1820 * The file will be opened again in the subsequent VFS open call
1821 * (nfs4_proc_file_open).
1823 * The open for read will just hang around to be used by any process that
1824 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1828 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1829 int flags, struct nameidata *nd)
1831 struct path path = {
1835 struct nfs4_state *state;
1836 struct rpc_cred *cred;
1839 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1841 status = PTR_ERR(cred);
1844 state = nfs4_do_open(dir, &path, flags, sattr, cred);
1846 if (IS_ERR(state)) {
1847 status = PTR_ERR(state);
1850 d_instantiate(dentry, igrab(state->inode));
1851 if (flags & O_EXCL) {
1852 struct nfs_fattr fattr;
1853 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
1855 nfs_setattr_update_inode(state->inode, sattr);
1856 nfs_post_op_update_inode(state->inode, &fattr);
1858 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
1859 status = nfs4_intent_set_file(nd, &path, state);
1861 nfs4_close_state(&path, state, flags);
1866 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1868 struct nfs_server *server = NFS_SERVER(dir);
1869 struct nfs4_remove_arg args = {
1872 .bitmask = server->attr_bitmask,
1874 struct nfs_fattr dir_attr;
1875 struct nfs4_remove_res res = {
1877 .dir_attr = &dir_attr,
1879 struct rpc_message msg = {
1880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1886 nfs_fattr_init(res.dir_attr);
1887 status = rpc_call_sync(server->client, &msg, 0);
1889 update_changeattr(dir, &res.cinfo);
1890 nfs_post_op_update_inode(dir, res.dir_attr);
1895 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1897 struct nfs4_exception exception = { };
1900 err = nfs4_handle_exception(NFS_SERVER(dir),
1901 _nfs4_proc_remove(dir, name),
1903 } while (exception.retry);
1907 struct unlink_desc {
1908 struct nfs4_remove_arg args;
1909 struct nfs4_remove_res res;
1910 struct nfs_fattr dir_attr;
1913 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1916 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1917 struct unlink_desc *up;
1919 up = kmalloc(sizeof(*up), GFP_KERNEL);
1923 up->args.fh = NFS_FH(dir->d_inode);
1924 up->args.name = name;
1925 up->args.bitmask = server->attr_bitmask;
1926 up->res.server = server;
1927 up->res.dir_attr = &up->dir_attr;
1929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1930 msg->rpc_argp = &up->args;
1931 msg->rpc_resp = &up->res;
1935 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1937 struct rpc_message *msg = &task->tk_msg;
1938 struct unlink_desc *up;
1940 if (msg->rpc_resp != NULL) {
1941 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1942 update_changeattr(dir->d_inode, &up->res.cinfo);
1943 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1945 msg->rpc_resp = NULL;
1946 msg->rpc_argp = NULL;
1951 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1952 struct inode *new_dir, struct qstr *new_name)
1954 struct nfs_server *server = NFS_SERVER(old_dir);
1955 struct nfs4_rename_arg arg = {
1956 .old_dir = NFS_FH(old_dir),
1957 .new_dir = NFS_FH(new_dir),
1958 .old_name = old_name,
1959 .new_name = new_name,
1960 .bitmask = server->attr_bitmask,
1962 struct nfs_fattr old_fattr, new_fattr;
1963 struct nfs4_rename_res res = {
1965 .old_fattr = &old_fattr,
1966 .new_fattr = &new_fattr,
1968 struct rpc_message msg = {
1969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1975 nfs_fattr_init(res.old_fattr);
1976 nfs_fattr_init(res.new_fattr);
1977 status = rpc_call_sync(server->client, &msg, 0);
1980 update_changeattr(old_dir, &res.old_cinfo);
1981 nfs_post_op_update_inode(old_dir, res.old_fattr);
1982 update_changeattr(new_dir, &res.new_cinfo);
1983 nfs_post_op_update_inode(new_dir, res.new_fattr);
1988 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1989 struct inode *new_dir, struct qstr *new_name)
1991 struct nfs4_exception exception = { };
1994 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1995 _nfs4_proc_rename(old_dir, old_name,
1998 } while (exception.retry);
2002 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2004 struct nfs_server *server = NFS_SERVER(inode);
2005 struct nfs4_link_arg arg = {
2006 .fh = NFS_FH(inode),
2007 .dir_fh = NFS_FH(dir),
2009 .bitmask = server->attr_bitmask,
2011 struct nfs_fattr fattr, dir_attr;
2012 struct nfs4_link_res res = {
2015 .dir_attr = &dir_attr,
2017 struct rpc_message msg = {
2018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2024 nfs_fattr_init(res.fattr);
2025 nfs_fattr_init(res.dir_attr);
2026 status = rpc_call_sync(server->client, &msg, 0);
2028 update_changeattr(dir, &res.cinfo);
2029 nfs_post_op_update_inode(dir, res.dir_attr);
2030 nfs_post_op_update_inode(inode, res.fattr);
2036 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2038 struct nfs4_exception exception = { };
2041 err = nfs4_handle_exception(NFS_SERVER(inode),
2042 _nfs4_proc_link(inode, dir, name),
2044 } while (exception.retry);
2048 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2049 struct page *page, unsigned int len, struct iattr *sattr)
2051 struct nfs_server *server = NFS_SERVER(dir);
2052 struct nfs_fh fhandle;
2053 struct nfs_fattr fattr, dir_fattr;
2054 struct nfs4_create_arg arg = {
2055 .dir_fh = NFS_FH(dir),
2057 .name = &dentry->d_name,
2060 .bitmask = server->attr_bitmask,
2062 struct nfs4_create_res res = {
2066 .dir_fattr = &dir_fattr,
2068 struct rpc_message msg = {
2069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2075 if (len > NFS4_MAXPATHLEN)
2076 return -ENAMETOOLONG;
2078 arg.u.symlink.pages = &page;
2079 arg.u.symlink.len = len;
2080 nfs_fattr_init(&fattr);
2081 nfs_fattr_init(&dir_fattr);
2083 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2085 update_changeattr(dir, &res.dir_cinfo);
2086 nfs_post_op_update_inode(dir, res.dir_fattr);
2087 status = nfs_instantiate(dentry, &fhandle, &fattr);
2092 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2093 struct page *page, unsigned int len, struct iattr *sattr)
2095 struct nfs4_exception exception = { };
2098 err = nfs4_handle_exception(NFS_SERVER(dir),
2099 _nfs4_proc_symlink(dir, dentry, page,
2102 } while (exception.retry);
2106 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2107 struct iattr *sattr)
2109 struct nfs_server *server = NFS_SERVER(dir);
2110 struct nfs_fh fhandle;
2111 struct nfs_fattr fattr, dir_fattr;
2112 struct nfs4_create_arg arg = {
2113 .dir_fh = NFS_FH(dir),
2115 .name = &dentry->d_name,
2118 .bitmask = server->attr_bitmask,
2120 struct nfs4_create_res res = {
2124 .dir_fattr = &dir_fattr,
2126 struct rpc_message msg = {
2127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2133 nfs_fattr_init(&fattr);
2134 nfs_fattr_init(&dir_fattr);
2136 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2138 update_changeattr(dir, &res.dir_cinfo);
2139 nfs_post_op_update_inode(dir, res.dir_fattr);
2140 status = nfs_instantiate(dentry, &fhandle, &fattr);
2145 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2146 struct iattr *sattr)
2148 struct nfs4_exception exception = { };
2151 err = nfs4_handle_exception(NFS_SERVER(dir),
2152 _nfs4_proc_mkdir(dir, dentry, sattr),
2154 } while (exception.retry);
2158 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2159 u64 cookie, struct page *page, unsigned int count, int plus)
2161 struct inode *dir = dentry->d_inode;
2162 struct nfs4_readdir_arg args = {
2167 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2169 struct nfs4_readdir_res res;
2170 struct rpc_message msg = {
2171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2178 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2179 dentry->d_parent->d_name.name,
2180 dentry->d_name.name,
2181 (unsigned long long)cookie);
2182 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2183 res.pgbase = args.pgbase;
2184 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2186 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2187 dprintk("%s: returns %d\n", __FUNCTION__, status);
2191 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2192 u64 cookie, struct page *page, unsigned int count, int plus)
2194 struct nfs4_exception exception = { };
2197 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2198 _nfs4_proc_readdir(dentry, cred, cookie,
2201 } while (exception.retry);
2205 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2206 struct iattr *sattr, dev_t rdev)
2208 struct nfs_server *server = NFS_SERVER(dir);
2210 struct nfs_fattr fattr, dir_fattr;
2211 struct nfs4_create_arg arg = {
2212 .dir_fh = NFS_FH(dir),
2214 .name = &dentry->d_name,
2216 .bitmask = server->attr_bitmask,
2218 struct nfs4_create_res res = {
2222 .dir_fattr = &dir_fattr,
2224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2230 int mode = sattr->ia_mode;
2232 nfs_fattr_init(&fattr);
2233 nfs_fattr_init(&dir_fattr);
2235 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2236 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2238 arg.ftype = NF4FIFO;
2239 else if (S_ISBLK(mode)) {
2241 arg.u.device.specdata1 = MAJOR(rdev);
2242 arg.u.device.specdata2 = MINOR(rdev);
2244 else if (S_ISCHR(mode)) {
2246 arg.u.device.specdata1 = MAJOR(rdev);
2247 arg.u.device.specdata2 = MINOR(rdev);
2250 arg.ftype = NF4SOCK;
2252 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2254 update_changeattr(dir, &res.dir_cinfo);
2255 nfs_post_op_update_inode(dir, res.dir_fattr);
2256 status = nfs_instantiate(dentry, &fh, &fattr);
2261 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2262 struct iattr *sattr, dev_t rdev)
2264 struct nfs4_exception exception = { };
2267 err = nfs4_handle_exception(NFS_SERVER(dir),
2268 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2270 } while (exception.retry);
2274 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2275 struct nfs_fsstat *fsstat)
2277 struct nfs4_statfs_arg args = {
2279 .bitmask = server->attr_bitmask,
2281 struct rpc_message msg = {
2282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2287 nfs_fattr_init(fsstat->fattr);
2288 return rpc_call_sync(server->client, &msg, 0);
2291 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2293 struct nfs4_exception exception = { };
2296 err = nfs4_handle_exception(server,
2297 _nfs4_proc_statfs(server, fhandle, fsstat),
2299 } while (exception.retry);
2303 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2304 struct nfs_fsinfo *fsinfo)
2306 struct nfs4_fsinfo_arg args = {
2308 .bitmask = server->attr_bitmask,
2310 struct rpc_message msg = {
2311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2316 return rpc_call_sync(server->client, &msg, 0);
2319 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2321 struct nfs4_exception exception = { };
2325 err = nfs4_handle_exception(server,
2326 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2328 } while (exception.retry);
2332 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2334 nfs_fattr_init(fsinfo->fattr);
2335 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2338 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2339 struct nfs_pathconf *pathconf)
2341 struct nfs4_pathconf_arg args = {
2343 .bitmask = server->attr_bitmask,
2345 struct rpc_message msg = {
2346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2348 .rpc_resp = pathconf,
2351 /* None of the pathconf attributes are mandatory to implement */
2352 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2353 memset(pathconf, 0, sizeof(*pathconf));
2357 nfs_fattr_init(pathconf->fattr);
2358 return rpc_call_sync(server->client, &msg, 0);
2361 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2362 struct nfs_pathconf *pathconf)
2364 struct nfs4_exception exception = { };
2368 err = nfs4_handle_exception(server,
2369 _nfs4_proc_pathconf(server, fhandle, pathconf),
2371 } while (exception.retry);
2375 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2377 struct nfs_server *server = NFS_SERVER(data->inode);
2379 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
2380 rpc_restart_call(task);
2383 if (task->tk_status > 0)
2384 renew_lease(server, data->timestamp);
2388 static void nfs4_proc_read_setup(struct nfs_read_data *data)
2390 struct rpc_message msg = {
2391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2392 .rpc_argp = &data->args,
2393 .rpc_resp = &data->res,
2394 .rpc_cred = data->cred,
2397 data->timestamp = jiffies;
2399 rpc_call_setup(&data->task, &msg, 0);
2402 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2404 struct inode *inode = data->inode;
2406 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2407 rpc_restart_call(task);
2410 if (task->tk_status >= 0) {
2411 renew_lease(NFS_SERVER(inode), data->timestamp);
2412 nfs_post_op_update_inode(inode, data->res.fattr);
2417 static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2419 struct rpc_message msg = {
2420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2421 .rpc_argp = &data->args,
2422 .rpc_resp = &data->res,
2423 .rpc_cred = data->cred,
2425 struct inode *inode = data->inode;
2426 struct nfs_server *server = NFS_SERVER(inode);
2429 if (how & FLUSH_STABLE) {
2430 if (!NFS_I(inode)->ncommit)
2431 stable = NFS_FILE_SYNC;
2433 stable = NFS_DATA_SYNC;
2435 stable = NFS_UNSTABLE;
2436 data->args.stable = stable;
2437 data->args.bitmask = server->attr_bitmask;
2438 data->res.server = server;
2440 data->timestamp = jiffies;
2442 /* Finalize the task. */
2443 rpc_call_setup(&data->task, &msg, 0);
2446 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2448 struct inode *inode = data->inode;
2450 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2451 rpc_restart_call(task);
2454 if (task->tk_status >= 0)
2455 nfs_post_op_update_inode(inode, data->res.fattr);
2459 static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2461 struct rpc_message msg = {
2462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2463 .rpc_argp = &data->args,
2464 .rpc_resp = &data->res,
2465 .rpc_cred = data->cred,
2467 struct nfs_server *server = NFS_SERVER(data->inode);
2469 data->args.bitmask = server->attr_bitmask;
2470 data->res.server = server;
2472 rpc_call_setup(&data->task, &msg, 0);
2476 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2477 * standalone procedure for queueing an asynchronous RENEW.
2479 static void nfs4_renew_done(struct rpc_task *task, void *data)
2481 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2482 unsigned long timestamp = (unsigned long)data;
2484 if (task->tk_status < 0) {
2485 switch (task->tk_status) {
2486 case -NFS4ERR_STALE_CLIENTID:
2487 case -NFS4ERR_EXPIRED:
2488 case -NFS4ERR_CB_PATH_DOWN:
2489 nfs4_schedule_state_recovery(clp);
2493 spin_lock(&clp->cl_lock);
2494 if (time_before(clp->cl_last_renewal,timestamp))
2495 clp->cl_last_renewal = timestamp;
2496 spin_unlock(&clp->cl_lock);
2499 static const struct rpc_call_ops nfs4_renew_ops = {
2500 .rpc_call_done = nfs4_renew_done,
2503 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2505 struct rpc_message msg = {
2506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2511 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2512 &nfs4_renew_ops, (void *)jiffies);
2515 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2517 struct rpc_message msg = {
2518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2522 unsigned long now = jiffies;
2525 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2528 spin_lock(&clp->cl_lock);
2529 if (time_before(clp->cl_last_renewal,now))
2530 clp->cl_last_renewal = now;
2531 spin_unlock(&clp->cl_lock);
2535 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2537 return (server->caps & NFS_CAP_ACLS)
2538 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2539 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2542 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2543 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2546 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2548 static void buf_to_pages(const void *buf, size_t buflen,
2549 struct page **pages, unsigned int *pgbase)
2551 const void *p = buf;
2553 *pgbase = offset_in_page(buf);
2555 while (p < buf + buflen) {
2556 *(pages++) = virt_to_page(p);
2557 p += PAGE_CACHE_SIZE;
2561 struct nfs4_cached_acl {
2567 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2569 struct nfs_inode *nfsi = NFS_I(inode);
2571 spin_lock(&inode->i_lock);
2572 kfree(nfsi->nfs4_acl);
2573 nfsi->nfs4_acl = acl;
2574 spin_unlock(&inode->i_lock);
2577 static void nfs4_zap_acl_attr(struct inode *inode)
2579 nfs4_set_cached_acl(inode, NULL);
2582 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2584 struct nfs_inode *nfsi = NFS_I(inode);
2585 struct nfs4_cached_acl *acl;
2588 spin_lock(&inode->i_lock);
2589 acl = nfsi->nfs4_acl;
2592 if (buf == NULL) /* user is just asking for length */
2594 if (acl->cached == 0)
2596 ret = -ERANGE; /* see getxattr(2) man page */
2597 if (acl->len > buflen)
2599 memcpy(buf, acl->data, acl->len);
2603 spin_unlock(&inode->i_lock);
2607 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2609 struct nfs4_cached_acl *acl;
2611 if (buf && acl_len <= PAGE_SIZE) {
2612 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2616 memcpy(acl->data, buf, acl_len);
2618 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2625 nfs4_set_cached_acl(inode, acl);
2628 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2630 struct page *pages[NFS4ACL_MAXPAGES];
2631 struct nfs_getaclargs args = {
2632 .fh = NFS_FH(inode),
2636 size_t resp_len = buflen;
2638 struct rpc_message msg = {
2639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2641 .rpc_resp = &resp_len,
2643 struct page *localpage = NULL;
2646 if (buflen < PAGE_SIZE) {
2647 /* As long as we're doing a round trip to the server anyway,
2648 * let's be prepared for a page of acl data. */
2649 localpage = alloc_page(GFP_KERNEL);
2650 resp_buf = page_address(localpage);
2651 if (localpage == NULL)
2653 args.acl_pages[0] = localpage;
2654 args.acl_pgbase = 0;
2655 resp_len = args.acl_len = PAGE_SIZE;
2658 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2660 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2663 if (resp_len > args.acl_len)
2664 nfs4_write_cached_acl(inode, NULL, resp_len);
2666 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2669 if (resp_len > buflen)
2672 memcpy(buf, resp_buf, resp_len);
2677 __free_page(localpage);
2681 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2683 struct nfs4_exception exception = { };
2686 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2689 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2690 } while (exception.retry);
2694 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2696 struct nfs_server *server = NFS_SERVER(inode);
2699 if (!nfs4_server_supports_acls(server))
2701 ret = nfs_revalidate_inode(server, inode);
2704 ret = nfs4_read_cached_acl(inode, buf, buflen);
2707 return nfs4_get_acl_uncached(inode, buf, buflen);
2710 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2712 struct nfs_server *server = NFS_SERVER(inode);
2713 struct page *pages[NFS4ACL_MAXPAGES];
2714 struct nfs_setaclargs arg = {
2715 .fh = NFS_FH(inode),
2719 struct rpc_message msg = {
2720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2726 if (!nfs4_server_supports_acls(server))
2728 nfs_inode_return_delegation(inode);
2729 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2730 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2731 nfs_zap_caches(inode);
2735 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2737 struct nfs4_exception exception = { };
2740 err = nfs4_handle_exception(NFS_SERVER(inode),
2741 __nfs4_proc_set_acl(inode, buf, buflen),
2743 } while (exception.retry);
2748 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2750 struct nfs_client *clp = server->nfs_client;
2752 if (!clp || task->tk_status >= 0)
2754 switch(task->tk_status) {
2755 case -NFS4ERR_STALE_CLIENTID:
2756 case -NFS4ERR_STALE_STATEID:
2757 case -NFS4ERR_EXPIRED:
2758 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2759 nfs4_schedule_state_recovery(clp);
2760 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
2761 rpc_wake_up_task(task);
2762 task->tk_status = 0;
2764 case -NFS4ERR_DELAY:
2765 nfs_inc_server_stats((struct nfs_server *) server,
2767 case -NFS4ERR_GRACE:
2768 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2769 task->tk_status = 0;
2771 case -NFS4ERR_OLD_STATEID:
2772 task->tk_status = 0;
2775 task->tk_status = nfs4_map_errors(task->tk_status);
2779 static int nfs4_wait_bit_interruptible(void *word)
2781 if (signal_pending(current))
2782 return -ERESTARTSYS;
2787 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
2794 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2796 rpc_clnt_sigmask(clnt, &oldset);
2797 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2798 nfs4_wait_bit_interruptible,
2799 TASK_INTERRUPTIBLE);
2800 rpc_clnt_sigunmask(clnt, &oldset);
2802 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
2806 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2814 *timeout = NFS4_POLL_RETRY_MIN;
2815 if (*timeout > NFS4_POLL_RETRY_MAX)
2816 *timeout = NFS4_POLL_RETRY_MAX;
2817 rpc_clnt_sigmask(clnt, &oldset);
2818 if (clnt->cl_intr) {
2819 schedule_timeout_interruptible(*timeout);
2823 schedule_timeout_uninterruptible(*timeout);
2824 rpc_clnt_sigunmask(clnt, &oldset);
2829 /* This is the error handling routine for processes that are allowed
2832 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2834 struct nfs_client *clp = server->nfs_client;
2835 int ret = errorcode;
2837 exception->retry = 0;
2841 case -NFS4ERR_STALE_CLIENTID:
2842 case -NFS4ERR_STALE_STATEID:
2843 case -NFS4ERR_EXPIRED:
2844 nfs4_schedule_state_recovery(clp);
2845 ret = nfs4_wait_clnt_recover(server->client, clp);
2847 exception->retry = 1;
2849 case -NFS4ERR_FILE_OPEN:
2850 case -NFS4ERR_GRACE:
2851 case -NFS4ERR_DELAY:
2852 ret = nfs4_delay(server->client, &exception->timeout);
2855 case -NFS4ERR_OLD_STATEID:
2856 exception->retry = 1;
2858 /* We failed to handle the error */
2859 return nfs4_map_errors(ret);
2862 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
2864 nfs4_verifier sc_verifier;
2865 struct nfs4_setclientid setclientid = {
2866 .sc_verifier = &sc_verifier,
2869 struct rpc_message msg = {
2870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2871 .rpc_argp = &setclientid,
2879 p = (__be32*)sc_verifier.data;
2880 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2881 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2884 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2885 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2886 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
2887 cred->cr_ops->cr_name,
2888 clp->cl_id_uniquifier);
2889 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2890 sizeof(setclientid.sc_netid), "tcp");
2891 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2892 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2893 clp->cl_ipaddr, port >> 8, port & 255);
2895 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2896 if (status != -NFS4ERR_CLID_INUSE)
2901 ssleep(clp->cl_lease_time + 1);
2903 if (++clp->cl_id_uniquifier == 0)
2909 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2911 struct nfs_fsinfo fsinfo;
2912 struct rpc_message msg = {
2913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2915 .rpc_resp = &fsinfo,
2922 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2924 spin_lock(&clp->cl_lock);
2925 clp->cl_lease_time = fsinfo.lease_time * HZ;
2926 clp->cl_last_renewal = now;
2927 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2928 spin_unlock(&clp->cl_lock);
2933 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2938 err = _nfs4_proc_setclientid_confirm(clp, cred);
2942 case -NFS4ERR_RESOURCE:
2943 /* The IBM lawyers misread another document! */
2944 case -NFS4ERR_DELAY:
2945 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2951 struct nfs4_delegreturndata {
2952 struct nfs4_delegreturnargs args;
2953 struct nfs4_delegreturnres res;
2955 nfs4_stateid stateid;
2956 struct rpc_cred *cred;
2957 unsigned long timestamp;
2958 struct nfs_fattr fattr;
2962 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
2964 struct nfs4_delegreturndata *data = calldata;
2965 struct rpc_message msg = {
2966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2967 .rpc_argp = &data->args,
2968 .rpc_resp = &data->res,
2969 .rpc_cred = data->cred,
2971 nfs_fattr_init(data->res.fattr);
2972 rpc_call_setup(task, &msg, 0);
2975 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2977 struct nfs4_delegreturndata *data = calldata;
2978 data->rpc_status = task->tk_status;
2979 if (data->rpc_status == 0)
2980 renew_lease(data->res.server, data->timestamp);
2983 static void nfs4_delegreturn_release(void *calldata)
2985 struct nfs4_delegreturndata *data = calldata;
2987 put_rpccred(data->cred);
2991 static const struct rpc_call_ops nfs4_delegreturn_ops = {
2992 .rpc_call_prepare = nfs4_delegreturn_prepare,
2993 .rpc_call_done = nfs4_delegreturn_done,
2994 .rpc_release = nfs4_delegreturn_release,
2997 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2999 struct nfs4_delegreturndata *data;
3000 struct nfs_server *server = NFS_SERVER(inode);
3001 struct rpc_task *task;
3004 data = kmalloc(sizeof(*data), GFP_KERNEL);
3007 data->args.fhandle = &data->fh;
3008 data->args.stateid = &data->stateid;
3009 data->args.bitmask = server->attr_bitmask;
3010 nfs_copy_fh(&data->fh, NFS_FH(inode));
3011 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3012 data->res.fattr = &data->fattr;
3013 data->res.server = server;
3014 data->cred = get_rpccred(cred);
3015 data->timestamp = jiffies;
3016 data->rpc_status = 0;
3018 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
3020 return PTR_ERR(task);
3021 status = nfs4_wait_for_completion_rpc_task(task);
3023 status = data->rpc_status;
3025 nfs_post_op_update_inode(inode, &data->fattr);
3031 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3033 struct nfs_server *server = NFS_SERVER(inode);
3034 struct nfs4_exception exception = { };
3037 err = _nfs4_proc_delegreturn(inode, cred, stateid);
3039 case -NFS4ERR_STALE_STATEID:
3040 case -NFS4ERR_EXPIRED:
3044 err = nfs4_handle_exception(server, err, &exception);
3045 } while (exception.retry);
3049 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3050 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3053 * sleep, with exponential backoff, and retry the LOCK operation.
3055 static unsigned long
3056 nfs4_set_lock_task_retry(unsigned long timeout)
3058 schedule_timeout_interruptible(timeout);
3060 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3061 return NFS4_LOCK_MAXTIMEOUT;
3065 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3067 struct inode *inode = state->inode;
3068 struct nfs_server *server = NFS_SERVER(inode);
3069 struct nfs_client *clp = server->nfs_client;
3070 struct nfs_lockt_args arg = {
3071 .fh = NFS_FH(inode),
3074 struct nfs_lockt_res res = {
3077 struct rpc_message msg = {
3078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3081 .rpc_cred = state->owner->so_cred,
3083 struct nfs4_lock_state *lsp;
3086 down_read(&clp->cl_sem);
3087 arg.lock_owner.clientid = clp->cl_clientid;
3088 status = nfs4_set_lock_state(state, request);
3091 lsp = request->fl_u.nfs4_fl.owner;
3092 arg.lock_owner.id = lsp->ls_id.id;
3093 status = rpc_call_sync(server->client, &msg, 0);
3096 request->fl_type = F_UNLCK;
3098 case -NFS4ERR_DENIED:
3101 request->fl_ops->fl_release_private(request);
3103 up_read(&clp->cl_sem);
3107 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3109 struct nfs4_exception exception = { };
3113 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3114 _nfs4_proc_getlk(state, cmd, request),
3116 } while (exception.retry);
3120 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3123 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3125 res = posix_lock_file_wait(file, fl);
3128 res = flock_lock_file_wait(file, fl);
3136 struct nfs4_unlockdata {
3137 struct nfs_locku_args arg;
3138 struct nfs_locku_res res;
3139 struct nfs4_lock_state *lsp;
3140 struct nfs_open_context *ctx;
3141 struct file_lock fl;
3142 const struct nfs_server *server;
3143 unsigned long timestamp;
3146 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3147 struct nfs_open_context *ctx,
3148 struct nfs4_lock_state *lsp,
3149 struct nfs_seqid *seqid)
3151 struct nfs4_unlockdata *p;
3152 struct inode *inode = lsp->ls_state->inode;
3154 p = kmalloc(sizeof(*p), GFP_KERNEL);
3157 p->arg.fh = NFS_FH(inode);
3159 p->arg.seqid = seqid;
3160 p->arg.stateid = &lsp->ls_stateid;
3162 atomic_inc(&lsp->ls_count);
3163 /* Ensure we don't close file until we're done freeing locks! */
3164 p->ctx = get_nfs_open_context(ctx);
3165 memcpy(&p->fl, fl, sizeof(p->fl));
3166 p->server = NFS_SERVER(inode);
3170 static void nfs4_locku_release_calldata(void *data)
3172 struct nfs4_unlockdata *calldata = data;
3173 nfs_free_seqid(calldata->arg.seqid);
3174 nfs4_put_lock_state(calldata->lsp);
3175 put_nfs_open_context(calldata->ctx);
3179 static void nfs4_locku_done(struct rpc_task *task, void *data)
3181 struct nfs4_unlockdata *calldata = data;
3183 if (RPC_ASSASSINATED(task))
3185 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
3186 switch (task->tk_status) {
3188 memcpy(calldata->lsp->ls_stateid.data,
3189 calldata->res.stateid.data,
3190 sizeof(calldata->lsp->ls_stateid.data));
3191 renew_lease(calldata->server, calldata->timestamp);
3193 case -NFS4ERR_STALE_STATEID:
3194 case -NFS4ERR_EXPIRED:
3197 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
3198 rpc_restart_call(task);
3202 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3204 struct nfs4_unlockdata *calldata = data;
3205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3207 .rpc_argp = &calldata->arg,
3208 .rpc_resp = &calldata->res,
3209 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
3212 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3214 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3215 /* Note: exit _without_ running nfs4_locku_done */
3216 task->tk_action = NULL;
3219 calldata->timestamp = jiffies;
3220 rpc_call_setup(task, &msg, 0);
3223 static const struct rpc_call_ops nfs4_locku_ops = {
3224 .rpc_call_prepare = nfs4_locku_prepare,
3225 .rpc_call_done = nfs4_locku_done,
3226 .rpc_release = nfs4_locku_release_calldata,
3229 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3230 struct nfs_open_context *ctx,
3231 struct nfs4_lock_state *lsp,
3232 struct nfs_seqid *seqid)
3234 struct nfs4_unlockdata *data;
3236 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3238 nfs_free_seqid(seqid);
3239 return ERR_PTR(-ENOMEM);
3242 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
3245 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3247 struct nfs_seqid *seqid;
3248 struct nfs4_lock_state *lsp;
3249 struct rpc_task *task;
3252 status = nfs4_set_lock_state(state, request);
3253 /* Unlock _before_ we do the RPC call */
3254 request->fl_flags |= FL_EXISTS;
3255 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3259 /* Is this a delegated lock? */
3260 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3262 lsp = request->fl_u.nfs4_fl.owner;
3263 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3267 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3268 status = PTR_ERR(task);
3271 status = nfs4_wait_for_completion_rpc_task(task);
3277 struct nfs4_lockdata {
3278 struct nfs_lock_args arg;
3279 struct nfs_lock_res res;
3280 struct nfs4_lock_state *lsp;
3281 struct nfs_open_context *ctx;
3282 struct file_lock fl;
3283 unsigned long timestamp;
3288 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3289 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3291 struct nfs4_lockdata *p;
3292 struct inode *inode = lsp->ls_state->inode;
3293 struct nfs_server *server = NFS_SERVER(inode);
3295 p = kzalloc(sizeof(*p), GFP_KERNEL);
3299 p->arg.fh = NFS_FH(inode);
3301 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3302 if (p->arg.lock_seqid == NULL)
3304 p->arg.lock_stateid = &lsp->ls_stateid;
3305 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3306 p->arg.lock_owner.id = lsp->ls_id.id;
3308 atomic_inc(&lsp->ls_count);
3309 p->ctx = get_nfs_open_context(ctx);
3310 memcpy(&p->fl, fl, sizeof(p->fl));
3317 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3319 struct nfs4_lockdata *data = calldata;
3320 struct nfs4_state *state = data->lsp->ls_state;
3321 struct nfs4_state_owner *sp = state->owner;
3322 struct rpc_message msg = {
3323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3324 .rpc_argp = &data->arg,
3325 .rpc_resp = &data->res,
3326 .rpc_cred = sp->so_cred,
3329 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3331 dprintk("%s: begin!\n", __FUNCTION__);
3332 /* Do we need to do an open_to_lock_owner? */
3333 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3334 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3335 if (data->arg.open_seqid == NULL) {
3336 data->rpc_status = -ENOMEM;
3337 task->tk_action = NULL;
3340 data->arg.open_stateid = &state->stateid;
3341 data->arg.new_lock_owner = 1;
3343 data->timestamp = jiffies;
3344 rpc_call_setup(task, &msg, 0);
3346 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3349 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3351 struct nfs4_lockdata *data = calldata;
3353 dprintk("%s: begin!\n", __FUNCTION__);
3355 data->rpc_status = task->tk_status;
3356 if (RPC_ASSASSINATED(task))
3358 if (data->arg.new_lock_owner != 0) {
3359 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3360 if (data->rpc_status == 0)
3361 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3365 if (data->rpc_status == 0) {
3366 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3367 sizeof(data->lsp->ls_stateid.data));
3368 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3369 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3371 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3373 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3376 static void nfs4_lock_release(void *calldata)
3378 struct nfs4_lockdata *data = calldata;
3380 dprintk("%s: begin!\n", __FUNCTION__);
3381 if (data->arg.open_seqid != NULL)
3382 nfs_free_seqid(data->arg.open_seqid);
3383 if (data->cancelled != 0) {
3384 struct rpc_task *task;
3385 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3386 data->arg.lock_seqid);
3389 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3391 nfs_free_seqid(data->arg.lock_seqid);
3392 nfs4_put_lock_state(data->lsp);
3393 put_nfs_open_context(data->ctx);
3395 dprintk("%s: done!\n", __FUNCTION__);
3398 static const struct rpc_call_ops nfs4_lock_ops = {
3399 .rpc_call_prepare = nfs4_lock_prepare,
3400 .rpc_call_done = nfs4_lock_done,
3401 .rpc_release = nfs4_lock_release,
3404 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3406 struct nfs4_lockdata *data;
3407 struct rpc_task *task;
3410 dprintk("%s: begin!\n", __FUNCTION__);
3411 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3412 fl->fl_u.nfs4_fl.owner);
3416 data->arg.block = 1;
3418 data->arg.reclaim = 1;
3419 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3420 &nfs4_lock_ops, data);
3422 return PTR_ERR(task);
3423 ret = nfs4_wait_for_completion_rpc_task(task);
3425 ret = data->rpc_status;
3426 if (ret == -NFS4ERR_DENIED)
3429 data->cancelled = 1;
3431 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3435 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3437 struct nfs_server *server = NFS_SERVER(state->inode);
3438 struct nfs4_exception exception = { };
3442 /* Cache the lock if possible... */
3443 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3445 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3446 if (err != -NFS4ERR_DELAY)
3448 nfs4_handle_exception(server, err, &exception);
3449 } while (exception.retry);
3453 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3455 struct nfs_server *server = NFS_SERVER(state->inode);
3456 struct nfs4_exception exception = { };
3459 err = nfs4_set_lock_state(state, request);
3463 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3465 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3466 if (err != -NFS4ERR_DELAY)
3468 nfs4_handle_exception(server, err, &exception);
3469 } while (exception.retry);
3473 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3475 struct nfs_client *clp = state->owner->so_client;
3476 unsigned char fl_flags = request->fl_flags;
3479 /* Is this a delegated open? */
3480 status = nfs4_set_lock_state(state, request);
3483 request->fl_flags |= FL_ACCESS;
3484 status = do_vfs_lock(request->fl_file, request);
3487 down_read(&clp->cl_sem);
3488 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3489 struct nfs_inode *nfsi = NFS_I(state->inode);
3490 /* Yes: cache locks! */
3491 down_read(&nfsi->rwsem);
3492 /* ...but avoid races with delegation recall... */
3493 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3494 request->fl_flags = fl_flags & ~FL_SLEEP;
3495 status = do_vfs_lock(request->fl_file, request);
3496 up_read(&nfsi->rwsem);
3499 up_read(&nfsi->rwsem);
3501 status = _nfs4_do_setlk(state, cmd, request, 0);
3504 /* Note: we always want to sleep here! */
3505 request->fl_flags = fl_flags | FL_SLEEP;
3506 if (do_vfs_lock(request->fl_file, request) < 0)
3507 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3509 up_read(&clp->cl_sem);
3511 request->fl_flags = fl_flags;
3515 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3517 struct nfs4_exception exception = { };
3521 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3522 _nfs4_proc_setlk(state, cmd, request),
3524 } while (exception.retry);
3529 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3531 struct nfs_open_context *ctx;
3532 struct nfs4_state *state;
3533 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3536 /* verify open state */
3537 ctx = (struct nfs_open_context *)filp->private_data;
3540 if (request->fl_start < 0 || request->fl_end < 0)
3544 return nfs4_proc_getlk(state, F_GETLK, request);
3546 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3549 if (request->fl_type == F_UNLCK)
3550 return nfs4_proc_unlck(state, cmd, request);
3553 status = nfs4_proc_setlk(state, cmd, request);
3554 if ((status != -EAGAIN) || IS_SETLK(cmd))
3556 timeout = nfs4_set_lock_task_retry(timeout);
3557 status = -ERESTARTSYS;
3560 } while(status < 0);
3564 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3566 struct nfs_server *server = NFS_SERVER(state->inode);
3567 struct nfs4_exception exception = { };
3570 err = nfs4_set_lock_state(state, fl);
3574 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3575 if (err != -NFS4ERR_DELAY)
3577 err = nfs4_handle_exception(server, err, &exception);
3578 } while (exception.retry);
3583 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3585 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3586 size_t buflen, int flags)
3588 struct inode *inode = dentry->d_inode;
3590 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3593 if (!S_ISREG(inode->i_mode) &&
3594 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3597 return nfs4_proc_set_acl(inode, buf, buflen);
3600 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3601 * and that's what we'll do for e.g. user attributes that haven't been set.
3602 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3603 * attributes in kernel-managed attribute namespaces. */
3604 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3607 struct inode *inode = dentry->d_inode;
3609 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3612 return nfs4_proc_get_acl(inode, buf, buflen);
3615 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3617 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3619 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3621 if (buf && buflen < len)
3624 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3628 int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
3629 struct nfs4_fs_locations *fs_locations, struct page *page)
3631 struct nfs_server *server = NFS_SERVER(dir);
3633 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3634 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3636 struct nfs4_fs_locations_arg args = {
3637 .dir_fh = NFS_FH(dir),
3642 struct rpc_message msg = {
3643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3645 .rpc_resp = fs_locations,
3649 dprintk("%s: start\n", __FUNCTION__);
3650 nfs_fattr_init(&fs_locations->fattr);
3651 fs_locations->server = server;
3652 fs_locations->nlocations = 0;
3653 status = rpc_call_sync(server->client, &msg, 0);
3654 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3658 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3659 .recover_open = nfs4_open_reclaim,
3660 .recover_lock = nfs4_lock_reclaim,
3663 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3664 .recover_open = nfs4_open_expired,
3665 .recover_lock = nfs4_lock_expired,
3668 static const struct inode_operations nfs4_file_inode_operations = {
3669 .permission = nfs_permission,
3670 .getattr = nfs_getattr,
3671 .setattr = nfs_setattr,
3672 .getxattr = nfs4_getxattr,
3673 .setxattr = nfs4_setxattr,
3674 .listxattr = nfs4_listxattr,
3677 const struct nfs_rpc_ops nfs_v4_clientops = {
3678 .version = 4, /* protocol version */
3679 .dentry_ops = &nfs4_dentry_operations,
3680 .dir_inode_ops = &nfs4_dir_inode_operations,
3681 .file_inode_ops = &nfs4_file_inode_operations,
3682 .getroot = nfs4_proc_get_root,
3683 .getattr = nfs4_proc_getattr,
3684 .setattr = nfs4_proc_setattr,
3685 .lookupfh = nfs4_proc_lookupfh,
3686 .lookup = nfs4_proc_lookup,
3687 .access = nfs4_proc_access,
3688 .readlink = nfs4_proc_readlink,
3689 .create = nfs4_proc_create,
3690 .remove = nfs4_proc_remove,
3691 .unlink_setup = nfs4_proc_unlink_setup,
3692 .unlink_done = nfs4_proc_unlink_done,
3693 .rename = nfs4_proc_rename,
3694 .link = nfs4_proc_link,
3695 .symlink = nfs4_proc_symlink,
3696 .mkdir = nfs4_proc_mkdir,
3697 .rmdir = nfs4_proc_remove,
3698 .readdir = nfs4_proc_readdir,
3699 .mknod = nfs4_proc_mknod,
3700 .statfs = nfs4_proc_statfs,
3701 .fsinfo = nfs4_proc_fsinfo,
3702 .pathconf = nfs4_proc_pathconf,
3703 .set_capabilities = nfs4_server_capabilities,
3704 .decode_dirent = nfs4_decode_dirent,
3705 .read_setup = nfs4_proc_read_setup,
3706 .read_done = nfs4_read_done,
3707 .write_setup = nfs4_proc_write_setup,
3708 .write_done = nfs4_write_done,
3709 .commit_setup = nfs4_proc_commit_setup,
3710 .commit_done = nfs4_commit_done,
3711 .file_open = nfs_open,
3712 .file_release = nfs_release,
3713 .lock = nfs4_proc_lock,
3714 .clear_acl_cache = nfs4_zap_acl_attr,