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;
233 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
234 struct nfs4_state_owner *sp, int flags,
235 const struct iattr *attrs)
237 struct dentry *parent = dget_parent(path->dentry);
238 struct inode *dir = parent->d_inode;
239 struct nfs_server *server = NFS_SERVER(dir);
240 struct nfs4_opendata *p;
242 p = kzalloc(sizeof(*p), GFP_KERNEL);
245 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
246 if (p->o_arg.seqid == NULL)
248 p->path.mnt = mntget(path->mnt);
249 p->path.dentry = dget(path->dentry);
252 atomic_inc(&sp->so_count);
253 p->o_arg.fh = NFS_FH(dir);
254 p->o_arg.open_flags = flags,
255 p->o_arg.clientid = server->nfs_client->cl_clientid;
256 p->o_arg.id = sp->so_owner_id.id;
257 p->o_arg.name = &p->path.dentry->d_name;
258 p->o_arg.server = server;
259 p->o_arg.bitmask = server->attr_bitmask;
260 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
261 p->o_res.f_attr = &p->f_attr;
262 p->o_res.dir_attr = &p->dir_attr;
263 p->o_res.server = server;
264 nfs_fattr_init(&p->f_attr);
265 nfs_fattr_init(&p->dir_attr);
266 if (flags & O_EXCL) {
267 u32 *s = (u32 *) p->o_arg.u.verifier.data;
270 } else if (flags & O_CREAT) {
271 p->o_arg.u.attrs = &p->attrs;
272 memcpy(&p->attrs, attrs, sizeof(p->attrs));
274 p->c_arg.fh = &p->o_res.fh;
275 p->c_arg.stateid = &p->o_res.stateid;
276 p->c_arg.seqid = p->o_arg.seqid;
286 static void nfs4_opendata_free(struct kref *kref)
288 struct nfs4_opendata *p = container_of(kref,
289 struct nfs4_opendata, kref);
291 nfs_free_seqid(p->o_arg.seqid);
292 nfs4_put_state_owner(p->owner);
294 dput(p->path.dentry);
299 static void nfs4_opendata_put(struct nfs4_opendata *p)
302 kref_put(&p->kref, nfs4_opendata_free);
305 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
310 rpc_clnt_sigmask(task->tk_client, &oldset);
311 ret = rpc_wait_for_completion_task(task);
312 rpc_clnt_sigunmask(task->tk_client, &oldset);
316 static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
318 switch (open_flags) {
325 case FMODE_READ|FMODE_WRITE:
330 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
332 struct inode *inode = state->inode;
334 open_flags &= (FMODE_READ|FMODE_WRITE);
335 /* Protect against nfs4_find_state_byowner() */
336 spin_lock(&state->owner->so_lock);
337 spin_lock(&inode->i_lock);
338 memcpy(&state->stateid, stateid, sizeof(state->stateid));
339 update_open_stateflags(state, open_flags);
340 nfs4_state_set_mode_locked(state, state->state | open_flags);
341 spin_unlock(&inode->i_lock);
342 spin_unlock(&state->owner->so_lock);
345 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
348 struct nfs4_state *state = NULL;
350 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
352 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
355 state = nfs4_get_open_state(inode, data->owner);
358 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
365 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
367 struct nfs_inode *nfsi = NFS_I(state->inode);
368 struct nfs_open_context *ctx;
370 spin_lock(&state->inode->i_lock);
371 list_for_each_entry(ctx, &nfsi->open_files, list) {
372 if (ctx->state != state)
374 get_nfs_open_context(ctx);
375 spin_unlock(&state->inode->i_lock);
378 spin_unlock(&state->inode->i_lock);
379 return ERR_PTR(-ENOENT);
382 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, nfs4_stateid *stateid)
386 opendata->o_arg.open_flags = openflags;
387 ret = _nfs4_proc_open(opendata);
390 memcpy(stateid->data, opendata->o_res.stateid.data,
391 sizeof(stateid->data));
395 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
397 nfs4_stateid stateid;
398 struct nfs4_state *newstate;
403 /* memory barrier prior to reading state->n_* */
405 if (state->n_rdwr != 0) {
406 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &stateid);
409 mode |= FMODE_READ|FMODE_WRITE;
410 if (opendata->o_res.delegation_type != 0)
411 delegation = opendata->o_res.delegation_type;
414 if (state->n_wronly != 0) {
415 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &stateid);
419 if (opendata->o_res.delegation_type != 0)
420 delegation = opendata->o_res.delegation_type;
423 if (state->n_rdonly != 0) {
424 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &stateid);
429 clear_bit(NFS_DELEGATED_STATE, &state->flags);
432 if (opendata->o_res.delegation_type == 0)
433 opendata->o_res.delegation_type = delegation;
434 opendata->o_arg.open_flags |= mode;
435 newstate = nfs4_opendata_to_nfs4_state(opendata);
436 if (newstate != NULL) {
437 if (opendata->o_res.delegation_type != 0) {
438 struct nfs_inode *nfsi = NFS_I(newstate->inode);
439 int delegation_flags = 0;
440 if (nfsi->delegation)
441 delegation_flags = nfsi->delegation->flags;
442 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
443 nfs_inode_set_delegation(newstate->inode,
444 opendata->owner->so_cred,
447 nfs_inode_reclaim_delegation(newstate->inode,
448 opendata->owner->so_cred,
451 nfs4_close_state(&opendata->path, newstate, opendata->o_arg.open_flags);
453 if (newstate != state)
460 * reclaim state on the server after a reboot.
462 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
464 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
465 struct nfs4_opendata *opendata;
466 int delegation_type = 0;
469 if (delegation != NULL) {
470 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
471 memcpy(&state->stateid, &delegation->stateid,
472 sizeof(state->stateid));
473 set_bit(NFS_DELEGATED_STATE, &state->flags);
476 delegation_type = delegation->type;
478 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
479 if (opendata == NULL)
481 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
482 opendata->o_arg.fh = NFS_FH(state->inode);
483 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
484 opendata->o_arg.u.delegation_type = delegation_type;
485 status = nfs4_open_recover(opendata, state);
486 nfs4_opendata_put(opendata);
490 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
492 struct nfs_server *server = NFS_SERVER(state->inode);
493 struct nfs4_exception exception = { };
496 err = _nfs4_do_open_reclaim(ctx, state);
497 if (err != -NFS4ERR_DELAY)
499 nfs4_handle_exception(server, err, &exception);
500 } while (exception.retry);
504 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
506 struct nfs_open_context *ctx;
509 ctx = nfs4_state_find_open_context(state);
512 ret = nfs4_do_open_reclaim(ctx, state);
513 put_nfs_open_context(ctx);
517 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state)
519 struct nfs4_state_owner *sp = state->owner;
520 struct nfs4_opendata *opendata;
523 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
525 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
526 if (opendata == NULL)
528 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
529 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
530 sizeof(opendata->o_arg.u.delegation.data));
531 ret = nfs4_open_recover(opendata, state);
532 nfs4_opendata_put(opendata);
536 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state)
538 struct nfs4_exception exception = { };
539 struct nfs_server *server = NFS_SERVER(state->inode);
542 err = _nfs4_open_delegation_recall(ctx, state);
546 case -NFS4ERR_STALE_CLIENTID:
547 case -NFS4ERR_STALE_STATEID:
548 case -NFS4ERR_EXPIRED:
549 /* Don't recall a delegation if it was lost */
550 nfs4_schedule_state_recovery(server->nfs_client);
553 err = nfs4_handle_exception(server, err, &exception);
554 } while (exception.retry);
558 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
560 struct nfs4_opendata *data = calldata;
561 struct rpc_message msg = {
562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
563 .rpc_argp = &data->c_arg,
564 .rpc_resp = &data->c_res,
565 .rpc_cred = data->owner->so_cred,
567 data->timestamp = jiffies;
568 rpc_call_setup(task, &msg, 0);
571 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
573 struct nfs4_opendata *data = calldata;
575 data->rpc_status = task->tk_status;
576 if (RPC_ASSASSINATED(task))
578 if (data->rpc_status == 0) {
579 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
580 sizeof(data->o_res.stateid.data));
581 renew_lease(data->o_res.server, data->timestamp);
583 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
584 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
587 static void nfs4_open_confirm_release(void *calldata)
589 struct nfs4_opendata *data = calldata;
590 struct nfs4_state *state = NULL;
592 /* If this request hasn't been cancelled, do nothing */
593 if (data->cancelled == 0)
595 /* In case of error, no cleanup! */
596 if (data->rpc_status != 0)
598 nfs_confirm_seqid(&data->owner->so_seqid, 0);
599 state = nfs4_opendata_to_nfs4_state(data);
601 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
603 nfs4_opendata_put(data);
606 static const struct rpc_call_ops nfs4_open_confirm_ops = {
607 .rpc_call_prepare = nfs4_open_confirm_prepare,
608 .rpc_call_done = nfs4_open_confirm_done,
609 .rpc_release = nfs4_open_confirm_release,
613 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
615 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
617 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
618 struct rpc_task *task;
621 kref_get(&data->kref);
623 * If rpc_run_task() ends up calling ->rpc_release(), we
624 * want to ensure that it takes the 'error' code path.
626 data->rpc_status = -ENOMEM;
627 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
629 return PTR_ERR(task);
630 status = nfs4_wait_for_completion_rpc_task(task);
635 status = data->rpc_status;
640 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
642 struct nfs4_opendata *data = calldata;
643 struct nfs4_state_owner *sp = data->owner;
644 struct rpc_message msg = {
645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
646 .rpc_argp = &data->o_arg,
647 .rpc_resp = &data->o_res,
648 .rpc_cred = sp->so_cred,
651 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
653 /* Update sequence id. */
654 data->o_arg.id = sp->so_owner_id.id;
655 data->o_arg.clientid = sp->so_client->cl_clientid;
656 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
657 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
658 data->timestamp = jiffies;
659 rpc_call_setup(task, &msg, 0);
662 static void nfs4_open_done(struct rpc_task *task, void *calldata)
664 struct nfs4_opendata *data = calldata;
666 data->rpc_status = task->tk_status;
667 if (RPC_ASSASSINATED(task))
669 if (task->tk_status == 0) {
670 switch (data->o_res.f_attr->mode & S_IFMT) {
674 data->rpc_status = -ELOOP;
677 data->rpc_status = -EISDIR;
680 data->rpc_status = -ENOTDIR;
682 renew_lease(data->o_res.server, data->timestamp);
684 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
687 static void nfs4_open_release(void *calldata)
689 struct nfs4_opendata *data = calldata;
690 struct nfs4_state *state = NULL;
692 /* If this request hasn't been cancelled, do nothing */
693 if (data->cancelled == 0)
695 /* In case of error, no cleanup! */
696 if (data->rpc_status != 0)
698 /* In case we need an open_confirm, no cleanup! */
699 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
701 nfs_confirm_seqid(&data->owner->so_seqid, 0);
702 state = nfs4_opendata_to_nfs4_state(data);
704 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
706 nfs4_opendata_put(data);
709 static const struct rpc_call_ops nfs4_open_ops = {
710 .rpc_call_prepare = nfs4_open_prepare,
711 .rpc_call_done = nfs4_open_done,
712 .rpc_release = nfs4_open_release,
716 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
718 static int _nfs4_proc_open(struct nfs4_opendata *data)
720 struct inode *dir = data->dir->d_inode;
721 struct nfs_server *server = NFS_SERVER(dir);
722 struct nfs_openargs *o_arg = &data->o_arg;
723 struct nfs_openres *o_res = &data->o_res;
724 struct rpc_task *task;
727 kref_get(&data->kref);
729 * If rpc_run_task() ends up calling ->rpc_release(), we
730 * want to ensure that it takes the 'error' code path.
732 data->rpc_status = -ENOMEM;
733 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
735 return PTR_ERR(task);
736 status = nfs4_wait_for_completion_rpc_task(task);
741 status = data->rpc_status;
746 if (o_arg->open_flags & O_CREAT) {
747 update_changeattr(dir, &o_res->cinfo);
748 nfs_post_op_update_inode(dir, o_res->dir_attr);
750 nfs_refresh_inode(dir, o_res->dir_attr);
751 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
752 status = _nfs4_proc_open_confirm(data);
756 nfs_confirm_seqid(&data->owner->so_seqid, 0);
757 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
758 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
762 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
764 struct nfs_access_entry cache;
768 if (openflags & FMODE_READ)
770 if (openflags & FMODE_WRITE)
772 if (openflags & FMODE_EXEC)
774 status = nfs_access_get_cached(inode, cred, &cache);
778 /* Be clever: ask server to check for all possible rights */
779 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
781 cache.jiffies = jiffies;
782 status = _nfs4_proc_access(inode, &cache);
785 nfs_access_add_cache(inode, &cache);
787 if ((cache.mask & mask) == mask)
792 static int nfs4_recover_expired_lease(struct nfs_server *server)
794 struct nfs_client *clp = server->nfs_client;
798 ret = nfs4_wait_clnt_recover(server->client, clp);
801 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
803 nfs4_schedule_state_recovery(clp);
810 * reclaim state on the server after a network partition.
811 * Assumes caller holds the appropriate lock
813 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
815 struct inode *inode = state->inode;
816 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
817 struct nfs4_opendata *opendata;
818 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
821 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
822 ret = _nfs4_do_access(inode, ctx->cred, openflags);
825 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
826 set_bit(NFS_DELEGATED_STATE, &state->flags);
829 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
830 if (opendata == NULL)
832 ret = nfs4_open_recover(opendata, state);
833 if (ret == -ESTALE) {
834 /* Invalidate the state owner so we don't ever use it again */
835 nfs4_drop_state_owner(state->owner);
836 d_drop(ctx->path.dentry);
838 nfs4_opendata_put(opendata);
842 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
844 struct nfs_server *server = NFS_SERVER(state->inode);
845 struct nfs4_exception exception = { };
849 err = _nfs4_open_expired(ctx, state);
850 if (err == -NFS4ERR_DELAY)
851 nfs4_handle_exception(server, err, &exception);
852 } while (exception.retry);
856 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
858 struct nfs_open_context *ctx;
861 ctx = nfs4_state_find_open_context(state);
864 ret = nfs4_do_open_expired(ctx, state);
865 put_nfs_open_context(ctx);
870 * Returns a referenced nfs4_state if there is an open delegation on the file
872 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
874 struct nfs_delegation *delegation;
875 struct nfs_server *server = NFS_SERVER(inode);
876 struct nfs_client *clp = server->nfs_client;
877 struct nfs_inode *nfsi = NFS_I(inode);
878 struct nfs4_state_owner *sp = NULL;
879 struct nfs4_state *state = NULL;
880 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
884 if (!(sp = nfs4_get_state_owner(server, cred))) {
885 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
888 err = nfs4_recover_expired_lease(server);
890 goto out_put_state_owner;
891 /* Protect against reboot recovery - NOTE ORDER! */
892 down_read(&clp->cl_sem);
893 /* Protect against delegation recall */
894 down_read(&nfsi->rwsem);
895 delegation = NFS_I(inode)->delegation;
897 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
900 state = nfs4_get_open_state(inode, sp);
905 if ((state->state & open_flags) == open_flags) {
906 spin_lock(&inode->i_lock);
907 update_open_stateflags(state, open_flags);
908 spin_unlock(&inode->i_lock);
910 } else if (state->state != 0)
911 goto out_put_open_state;
914 err = _nfs4_do_access(inode, cred, open_flags);
917 goto out_put_open_state;
918 set_bit(NFS_DELEGATED_STATE, &state->flags);
919 update_open_stateid(state, &delegation->stateid, open_flags);
921 nfs4_put_state_owner(sp);
922 up_read(&nfsi->rwsem);
923 up_read(&clp->cl_sem);
927 nfs4_put_open_state(state);
929 up_read(&nfsi->rwsem);
930 up_read(&clp->cl_sem);
932 nfs_inode_return_delegation(inode);
934 nfs4_put_state_owner(sp);
938 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
940 struct nfs4_exception exception = { };
941 struct nfs4_state *res = ERR_PTR(-EIO);
945 err = _nfs4_open_delegated(inode, flags, cred, &res);
948 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
950 } while (exception.retry);
955 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
956 * fields corresponding to attributes that were used to store the verifier.
957 * Make sure we clobber those fields in the later setattr call
959 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
961 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
962 !(sattr->ia_valid & ATTR_ATIME_SET))
963 sattr->ia_valid |= ATTR_ATIME;
965 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
966 !(sattr->ia_valid & ATTR_MTIME_SET))
967 sattr->ia_valid |= ATTR_MTIME;
971 * Returns a referenced nfs4_state
973 static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
975 struct nfs4_state_owner *sp;
976 struct nfs4_state *state = NULL;
977 struct nfs_server *server = NFS_SERVER(dir);
978 struct nfs_client *clp = server->nfs_client;
979 struct nfs4_opendata *opendata;
982 /* Protect against reboot recovery conflicts */
984 if (!(sp = nfs4_get_state_owner(server, cred))) {
985 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
988 status = nfs4_recover_expired_lease(server);
990 goto err_put_state_owner;
991 down_read(&clp->cl_sem);
993 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
994 if (opendata == NULL)
995 goto err_release_rwsem;
997 status = _nfs4_proc_open(opendata);
999 goto err_opendata_put;
1001 if (opendata->o_arg.open_flags & O_EXCL)
1002 nfs4_exclusive_attrset(opendata, sattr);
1005 state = nfs4_opendata_to_nfs4_state(opendata);
1007 goto err_opendata_put;
1008 if (opendata->o_res.delegation_type != 0)
1009 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
1010 nfs4_opendata_put(opendata);
1011 nfs4_put_state_owner(sp);
1012 up_read(&clp->cl_sem);
1016 nfs4_opendata_put(opendata);
1018 up_read(&clp->cl_sem);
1019 err_put_state_owner:
1020 nfs4_put_state_owner(sp);
1027 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
1029 struct nfs4_exception exception = { };
1030 struct nfs4_state *res;
1034 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
1037 /* NOTE: BAD_SEQID means the server and client disagree about the
1038 * book-keeping w.r.t. state-changing operations
1039 * (OPEN/CLOSE/LOCK/LOCKU...)
1040 * It is actually a sign of a bug on the client or on the server.
1042 * If we receive a BAD_SEQID error in the particular case of
1043 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1044 * have unhashed the old state_owner for us, and that we can
1045 * therefore safely retry using a new one. We should still warn
1046 * the user though...
1048 if (status == -NFS4ERR_BAD_SEQID) {
1049 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
1050 exception.retry = 1;
1054 * BAD_STATEID on OPEN means that the server cancelled our
1055 * state before it received the OPEN_CONFIRM.
1056 * Recover by retrying the request as per the discussion
1057 * on Page 181 of RFC3530.
1059 if (status == -NFS4ERR_BAD_STATEID) {
1060 exception.retry = 1;
1063 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1064 status, &exception));
1065 } while (exception.retry);
1069 static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1070 struct iattr *sattr, struct nfs4_state *state)
1072 struct nfs_server *server = NFS_SERVER(inode);
1073 struct nfs_setattrargs arg = {
1074 .fh = NFS_FH(inode),
1077 .bitmask = server->attr_bitmask,
1079 struct nfs_setattrres res = {
1083 struct rpc_message msg = {
1084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1088 unsigned long timestamp = jiffies;
1091 nfs_fattr_init(fattr);
1093 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1094 /* Use that stateid */
1095 } else if (state != NULL) {
1096 msg.rpc_cred = state->owner->so_cred;
1097 nfs4_copy_stateid(&arg.stateid, state, current->files);
1099 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1101 status = rpc_call_sync(server->client, &msg, 0);
1102 if (status == 0 && state != NULL)
1103 renew_lease(server, timestamp);
1107 static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1108 struct iattr *sattr, struct nfs4_state *state)
1110 struct nfs_server *server = NFS_SERVER(inode);
1111 struct nfs4_exception exception = { };
1114 err = nfs4_handle_exception(server,
1115 _nfs4_do_setattr(inode, fattr, sattr, state),
1117 } while (exception.retry);
1121 struct nfs4_closedata {
1123 struct inode *inode;
1124 struct nfs4_state *state;
1125 struct nfs_closeargs arg;
1126 struct nfs_closeres res;
1127 struct nfs_fattr fattr;
1128 unsigned long timestamp;
1131 static void nfs4_free_closedata(void *data)
1133 struct nfs4_closedata *calldata = data;
1134 struct nfs4_state_owner *sp = calldata->state->owner;
1136 nfs4_put_open_state(calldata->state);
1137 nfs_free_seqid(calldata->arg.seqid);
1138 nfs4_put_state_owner(sp);
1139 dput(calldata->path.dentry);
1140 mntput(calldata->path.mnt);
1144 static void nfs4_close_done(struct rpc_task *task, void *data)
1146 struct nfs4_closedata *calldata = data;
1147 struct nfs4_state *state = calldata->state;
1148 struct nfs_server *server = NFS_SERVER(calldata->inode);
1150 if (RPC_ASSASSINATED(task))
1152 /* hmm. we are done with the inode, and in the process of freeing
1153 * the state_owner. we keep this around to process errors
1155 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1156 switch (task->tk_status) {
1158 memcpy(&state->stateid, &calldata->res.stateid,
1159 sizeof(state->stateid));
1160 renew_lease(server, calldata->timestamp);
1162 case -NFS4ERR_STALE_STATEID:
1163 case -NFS4ERR_EXPIRED:
1166 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1167 rpc_restart_call(task);
1171 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1174 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1176 struct nfs4_closedata *calldata = data;
1177 struct nfs4_state *state = calldata->state;
1178 struct rpc_message msg = {
1179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1180 .rpc_argp = &calldata->arg,
1181 .rpc_resp = &calldata->res,
1182 .rpc_cred = state->owner->so_cred,
1184 int mode = 0, old_mode;
1186 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1188 /* Recalculate the new open mode in case someone reopened the file
1189 * while we were waiting in line to be scheduled.
1191 spin_lock(&state->owner->so_lock);
1192 spin_lock(&calldata->inode->i_lock);
1193 mode = old_mode = state->state;
1194 if (state->n_rdwr == 0) {
1195 if (state->n_rdonly == 0)
1196 mode &= ~FMODE_READ;
1197 if (state->n_wronly == 0)
1198 mode &= ~FMODE_WRITE;
1200 nfs4_state_set_mode_locked(state, mode);
1201 spin_unlock(&calldata->inode->i_lock);
1202 spin_unlock(&state->owner->so_lock);
1203 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1204 /* Note: exit _without_ calling nfs4_close_done */
1205 task->tk_action = NULL;
1208 nfs_fattr_init(calldata->res.fattr);
1210 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1211 calldata->arg.open_flags = mode;
1212 calldata->timestamp = jiffies;
1213 rpc_call_setup(task, &msg, 0);
1216 static const struct rpc_call_ops nfs4_close_ops = {
1217 .rpc_call_prepare = nfs4_close_prepare,
1218 .rpc_call_done = nfs4_close_done,
1219 .rpc_release = nfs4_free_closedata,
1223 * It is possible for data to be read/written from a mem-mapped file
1224 * after the sys_close call (which hits the vfs layer as a flush).
1225 * This means that we can't safely call nfsv4 close on a file until
1226 * the inode is cleared. This in turn means that we are not good
1227 * NFSv4 citizens - we do not indicate to the server to update the file's
1228 * share state even when we are done with one of the three share
1229 * stateid's in the inode.
1231 * NOTE: Caller must be holding the sp->so_owner semaphore!
1233 int nfs4_do_close(struct path *path, struct nfs4_state *state)
1235 struct nfs_server *server = NFS_SERVER(state->inode);
1236 struct nfs4_closedata *calldata;
1237 struct nfs4_state_owner *sp = state->owner;
1238 struct rpc_task *task;
1239 int status = -ENOMEM;
1241 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1242 if (calldata == NULL)
1244 calldata->inode = state->inode;
1245 calldata->state = state;
1246 calldata->arg.fh = NFS_FH(state->inode);
1247 calldata->arg.stateid = &state->stateid;
1248 /* Serialization for the sequence id */
1249 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1250 if (calldata->arg.seqid == NULL)
1251 goto out_free_calldata;
1252 calldata->arg.bitmask = server->attr_bitmask;
1253 calldata->res.fattr = &calldata->fattr;
1254 calldata->res.server = server;
1255 calldata->path.mnt = mntget(path->mnt);
1256 calldata->path.dentry = dget(path->dentry);
1258 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1260 return PTR_ERR(task);
1266 nfs4_put_open_state(state);
1267 nfs4_put_state_owner(sp);
1271 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
1276 /* If the open_intent is for execute, we have an extra check to make */
1277 if (nd->intent.open.flags & FMODE_EXEC) {
1278 ret = _nfs4_do_access(state->inode,
1279 state->owner->so_cred,
1280 nd->intent.open.flags);
1284 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1285 if (!IS_ERR(filp)) {
1286 struct nfs_open_context *ctx;
1287 ctx = (struct nfs_open_context *)filp->private_data;
1291 ret = PTR_ERR(filp);
1293 nfs4_close_state(path, state, nd->intent.open.flags);
1298 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1300 struct path path = {
1305 struct rpc_cred *cred;
1306 struct nfs4_state *state;
1309 if (nd->flags & LOOKUP_CREATE) {
1310 attr.ia_mode = nd->intent.open.create_mode;
1311 attr.ia_valid = ATTR_MODE;
1312 if (!IS_POSIXACL(dir))
1313 attr.ia_mode &= ~current->fs->umask;
1316 BUG_ON(nd->intent.open.flags & O_CREAT);
1319 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1321 return (struct dentry *)cred;
1322 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
1324 if (IS_ERR(state)) {
1325 if (PTR_ERR(state) == -ENOENT)
1326 d_add(dentry, NULL);
1327 return (struct dentry *)state;
1329 res = d_add_unique(dentry, igrab(state->inode));
1332 nfs4_intent_set_file(nd, &path, state);
1337 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1339 struct path path = {
1343 struct rpc_cred *cred;
1344 struct nfs4_state *state;
1346 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1348 return PTR_ERR(cred);
1349 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1351 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
1353 if (IS_ERR(state)) {
1354 switch (PTR_ERR(state)) {
1360 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1366 if (state->inode == dentry->d_inode) {
1367 nfs4_intent_set_file(nd, &path, state);
1370 nfs4_close_state(&path, state, openflags);
1377 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1379 struct nfs4_server_caps_res res = {};
1380 struct rpc_message msg = {
1381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1382 .rpc_argp = fhandle,
1387 status = rpc_call_sync(server->client, &msg, 0);
1389 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1390 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1391 server->caps |= NFS_CAP_ACLS;
1392 if (res.has_links != 0)
1393 server->caps |= NFS_CAP_HARDLINKS;
1394 if (res.has_symlinks != 0)
1395 server->caps |= NFS_CAP_SYMLINKS;
1396 server->acl_bitmask = res.acl_bitmask;
1401 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1403 struct nfs4_exception exception = { };
1406 err = nfs4_handle_exception(server,
1407 _nfs4_server_capabilities(server, fhandle),
1409 } while (exception.retry);
1413 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1414 struct nfs_fsinfo *info)
1416 struct nfs4_lookup_root_arg args = {
1417 .bitmask = nfs4_fattr_bitmap,
1419 struct nfs4_lookup_res res = {
1421 .fattr = info->fattr,
1424 struct rpc_message msg = {
1425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1429 nfs_fattr_init(info->fattr);
1430 return rpc_call_sync(server->client, &msg, 0);
1433 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1434 struct nfs_fsinfo *info)
1436 struct nfs4_exception exception = { };
1439 err = nfs4_handle_exception(server,
1440 _nfs4_lookup_root(server, fhandle, info),
1442 } while (exception.retry);
1447 * get the file handle for the "/" directory on the server
1449 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1450 struct nfs_fsinfo *info)
1454 status = nfs4_lookup_root(server, fhandle, info);
1456 status = nfs4_server_capabilities(server, fhandle);
1458 status = nfs4_do_fsinfo(server, fhandle, info);
1459 return nfs4_map_errors(status);
1463 * Get locations and (maybe) other attributes of a referral.
1464 * Note that we'll actually follow the referral later when
1465 * we detect fsid mismatch in inode revalidation
1467 static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1469 int status = -ENOMEM;
1470 struct page *page = NULL;
1471 struct nfs4_fs_locations *locations = NULL;
1473 page = alloc_page(GFP_KERNEL);
1476 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1477 if (locations == NULL)
1480 status = nfs4_proc_fs_locations(dir, name, locations, page);
1483 /* Make sure server returned a different fsid for the referral */
1484 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1485 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1490 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1491 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1493 fattr->mode = S_IFDIR;
1494 memset(fhandle, 0, sizeof(struct nfs_fh));
1503 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1505 struct nfs4_getattr_arg args = {
1507 .bitmask = server->attr_bitmask,
1509 struct nfs4_getattr_res res = {
1513 struct rpc_message msg = {
1514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1519 nfs_fattr_init(fattr);
1520 return rpc_call_sync(server->client, &msg, 0);
1523 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1525 struct nfs4_exception exception = { };
1528 err = nfs4_handle_exception(server,
1529 _nfs4_proc_getattr(server, fhandle, fattr),
1531 } while (exception.retry);
1536 * The file is not closed if it is opened due to the a request to change
1537 * the size of the file. The open call will not be needed once the
1538 * VFS layer lookup-intents are implemented.
1540 * Close is called when the inode is destroyed.
1541 * If we haven't opened the file for O_WRONLY, we
1542 * need to in the size_change case to obtain a stateid.
1545 * Because OPEN is always done by name in nfsv4, it is
1546 * possible that we opened a different file by the same
1547 * name. We can recognize this race condition, but we
1548 * can't do anything about it besides returning an error.
1550 * This will be fixed with VFS changes (lookup-intent).
1553 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1554 struct iattr *sattr)
1556 struct rpc_cred *cred;
1557 struct inode *inode = dentry->d_inode;
1558 struct nfs_open_context *ctx;
1559 struct nfs4_state *state = NULL;
1562 nfs_fattr_init(fattr);
1564 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1566 return PTR_ERR(cred);
1568 /* Search for an existing open(O_WRITE) file */
1569 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1573 status = nfs4_do_setattr(inode, fattr, sattr, state);
1575 nfs_setattr_update_inode(inode, sattr);
1577 put_nfs_open_context(ctx);
1582 static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1583 struct qstr *name, struct nfs_fh *fhandle,
1584 struct nfs_fattr *fattr)
1587 struct nfs4_lookup_arg args = {
1588 .bitmask = server->attr_bitmask,
1592 struct nfs4_lookup_res res = {
1597 struct rpc_message msg = {
1598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1603 nfs_fattr_init(fattr);
1605 dprintk("NFS call lookupfh %s\n", name->name);
1606 status = rpc_call_sync(server->client, &msg, 0);
1607 dprintk("NFS reply lookupfh: %d\n", status);
1611 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1612 struct qstr *name, struct nfs_fh *fhandle,
1613 struct nfs_fattr *fattr)
1615 struct nfs4_exception exception = { };
1618 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1620 if (err == -NFS4ERR_MOVED) {
1624 err = nfs4_handle_exception(server, err, &exception);
1625 } while (exception.retry);
1629 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1630 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1634 dprintk("NFS call lookup %s\n", name->name);
1635 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
1636 if (status == -NFS4ERR_MOVED)
1637 status = nfs4_get_referral(dir, name, fattr, fhandle);
1638 dprintk("NFS reply lookup: %d\n", status);
1642 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1644 struct nfs4_exception exception = { };
1647 err = nfs4_handle_exception(NFS_SERVER(dir),
1648 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1650 } while (exception.retry);
1654 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1656 struct nfs4_accessargs args = {
1657 .fh = NFS_FH(inode),
1659 struct nfs4_accessres res = { 0 };
1660 struct rpc_message msg = {
1661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1664 .rpc_cred = entry->cred,
1666 int mode = entry->mask;
1670 * Determine which access bits we want to ask for...
1672 if (mode & MAY_READ)
1673 args.access |= NFS4_ACCESS_READ;
1674 if (S_ISDIR(inode->i_mode)) {
1675 if (mode & MAY_WRITE)
1676 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1677 if (mode & MAY_EXEC)
1678 args.access |= NFS4_ACCESS_LOOKUP;
1680 if (mode & MAY_WRITE)
1681 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1682 if (mode & MAY_EXEC)
1683 args.access |= NFS4_ACCESS_EXECUTE;
1685 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1688 if (res.access & NFS4_ACCESS_READ)
1689 entry->mask |= MAY_READ;
1690 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1691 entry->mask |= MAY_WRITE;
1692 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1693 entry->mask |= MAY_EXEC;
1698 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1700 struct nfs4_exception exception = { };
1703 err = nfs4_handle_exception(NFS_SERVER(inode),
1704 _nfs4_proc_access(inode, entry),
1706 } while (exception.retry);
1711 * TODO: For the time being, we don't try to get any attributes
1712 * along with any of the zero-copy operations READ, READDIR,
1715 * In the case of the first three, we want to put the GETATTR
1716 * after the read-type operation -- this is because it is hard
1717 * to predict the length of a GETATTR response in v4, and thus
1718 * align the READ data correctly. This means that the GETATTR
1719 * may end up partially falling into the page cache, and we should
1720 * shift it into the 'tail' of the xdr_buf before processing.
1721 * To do this efficiently, we need to know the total length
1722 * of data received, which doesn't seem to be available outside
1725 * In the case of WRITE, we also want to put the GETATTR after
1726 * the operation -- in this case because we want to make sure
1727 * we get the post-operation mtime and size. This means that
1728 * we can't use xdr_encode_pages() as written: we need a variant
1729 * of it which would leave room in the 'tail' iovec.
1731 * Both of these changes to the XDR layer would in fact be quite
1732 * minor, but I decided to leave them for a subsequent patch.
1734 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1735 unsigned int pgbase, unsigned int pglen)
1737 struct nfs4_readlink args = {
1738 .fh = NFS_FH(inode),
1743 struct rpc_message msg = {
1744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1749 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1752 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1753 unsigned int pgbase, unsigned int pglen)
1755 struct nfs4_exception exception = { };
1758 err = nfs4_handle_exception(NFS_SERVER(inode),
1759 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1761 } while (exception.retry);
1767 * We will need to arrange for the VFS layer to provide an atomic open.
1768 * Until then, this create/open method is prone to inefficiency and race
1769 * conditions due to the lookup, create, and open VFS calls from sys_open()
1770 * placed on the wire.
1772 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1773 * The file will be opened again in the subsequent VFS open call
1774 * (nfs4_proc_file_open).
1776 * The open for read will just hang around to be used by any process that
1777 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1781 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1782 int flags, struct nameidata *nd)
1784 struct path path = {
1788 struct nfs4_state *state;
1789 struct rpc_cred *cred;
1792 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1794 status = PTR_ERR(cred);
1797 state = nfs4_do_open(dir, &path, flags, sattr, cred);
1799 if (IS_ERR(state)) {
1800 status = PTR_ERR(state);
1803 d_instantiate(dentry, igrab(state->inode));
1804 if (flags & O_EXCL) {
1805 struct nfs_fattr fattr;
1806 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
1808 nfs_setattr_update_inode(state->inode, sattr);
1809 nfs_post_op_update_inode(state->inode, &fattr);
1811 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
1812 status = nfs4_intent_set_file(nd, &path, state);
1814 nfs4_close_state(&path, state, flags);
1819 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1821 struct nfs_server *server = NFS_SERVER(dir);
1822 struct nfs4_remove_arg args = {
1825 .bitmask = server->attr_bitmask,
1827 struct nfs_fattr dir_attr;
1828 struct nfs4_remove_res res = {
1830 .dir_attr = &dir_attr,
1832 struct rpc_message msg = {
1833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1839 nfs_fattr_init(res.dir_attr);
1840 status = rpc_call_sync(server->client, &msg, 0);
1842 update_changeattr(dir, &res.cinfo);
1843 nfs_post_op_update_inode(dir, res.dir_attr);
1848 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1850 struct nfs4_exception exception = { };
1853 err = nfs4_handle_exception(NFS_SERVER(dir),
1854 _nfs4_proc_remove(dir, name),
1856 } while (exception.retry);
1860 struct unlink_desc {
1861 struct nfs4_remove_arg args;
1862 struct nfs4_remove_res res;
1863 struct nfs_fattr dir_attr;
1866 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1869 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1870 struct unlink_desc *up;
1872 up = kmalloc(sizeof(*up), GFP_KERNEL);
1876 up->args.fh = NFS_FH(dir->d_inode);
1877 up->args.name = name;
1878 up->args.bitmask = server->attr_bitmask;
1879 up->res.server = server;
1880 up->res.dir_attr = &up->dir_attr;
1882 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1883 msg->rpc_argp = &up->args;
1884 msg->rpc_resp = &up->res;
1888 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1890 struct rpc_message *msg = &task->tk_msg;
1891 struct unlink_desc *up;
1893 if (msg->rpc_resp != NULL) {
1894 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1895 update_changeattr(dir->d_inode, &up->res.cinfo);
1896 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1898 msg->rpc_resp = NULL;
1899 msg->rpc_argp = NULL;
1904 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1905 struct inode *new_dir, struct qstr *new_name)
1907 struct nfs_server *server = NFS_SERVER(old_dir);
1908 struct nfs4_rename_arg arg = {
1909 .old_dir = NFS_FH(old_dir),
1910 .new_dir = NFS_FH(new_dir),
1911 .old_name = old_name,
1912 .new_name = new_name,
1913 .bitmask = server->attr_bitmask,
1915 struct nfs_fattr old_fattr, new_fattr;
1916 struct nfs4_rename_res res = {
1918 .old_fattr = &old_fattr,
1919 .new_fattr = &new_fattr,
1921 struct rpc_message msg = {
1922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1928 nfs_fattr_init(res.old_fattr);
1929 nfs_fattr_init(res.new_fattr);
1930 status = rpc_call_sync(server->client, &msg, 0);
1933 update_changeattr(old_dir, &res.old_cinfo);
1934 nfs_post_op_update_inode(old_dir, res.old_fattr);
1935 update_changeattr(new_dir, &res.new_cinfo);
1936 nfs_post_op_update_inode(new_dir, res.new_fattr);
1941 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1942 struct inode *new_dir, struct qstr *new_name)
1944 struct nfs4_exception exception = { };
1947 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1948 _nfs4_proc_rename(old_dir, old_name,
1951 } while (exception.retry);
1955 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1957 struct nfs_server *server = NFS_SERVER(inode);
1958 struct nfs4_link_arg arg = {
1959 .fh = NFS_FH(inode),
1960 .dir_fh = NFS_FH(dir),
1962 .bitmask = server->attr_bitmask,
1964 struct nfs_fattr fattr, dir_attr;
1965 struct nfs4_link_res res = {
1968 .dir_attr = &dir_attr,
1970 struct rpc_message msg = {
1971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1977 nfs_fattr_init(res.fattr);
1978 nfs_fattr_init(res.dir_attr);
1979 status = rpc_call_sync(server->client, &msg, 0);
1981 update_changeattr(dir, &res.cinfo);
1982 nfs_post_op_update_inode(dir, res.dir_attr);
1983 nfs_post_op_update_inode(inode, res.fattr);
1989 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1991 struct nfs4_exception exception = { };
1994 err = nfs4_handle_exception(NFS_SERVER(inode),
1995 _nfs4_proc_link(inode, dir, name),
1997 } while (exception.retry);
2001 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2002 struct page *page, unsigned int len, struct iattr *sattr)
2004 struct nfs_server *server = NFS_SERVER(dir);
2005 struct nfs_fh fhandle;
2006 struct nfs_fattr fattr, dir_fattr;
2007 struct nfs4_create_arg arg = {
2008 .dir_fh = NFS_FH(dir),
2010 .name = &dentry->d_name,
2013 .bitmask = server->attr_bitmask,
2015 struct nfs4_create_res res = {
2019 .dir_fattr = &dir_fattr,
2021 struct rpc_message msg = {
2022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2028 if (len > NFS4_MAXPATHLEN)
2029 return -ENAMETOOLONG;
2031 arg.u.symlink.pages = &page;
2032 arg.u.symlink.len = len;
2033 nfs_fattr_init(&fattr);
2034 nfs_fattr_init(&dir_fattr);
2036 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2038 update_changeattr(dir, &res.dir_cinfo);
2039 nfs_post_op_update_inode(dir, res.dir_fattr);
2040 status = nfs_instantiate(dentry, &fhandle, &fattr);
2045 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2046 struct page *page, unsigned int len, struct iattr *sattr)
2048 struct nfs4_exception exception = { };
2051 err = nfs4_handle_exception(NFS_SERVER(dir),
2052 _nfs4_proc_symlink(dir, dentry, page,
2055 } while (exception.retry);
2059 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2060 struct iattr *sattr)
2062 struct nfs_server *server = NFS_SERVER(dir);
2063 struct nfs_fh fhandle;
2064 struct nfs_fattr fattr, dir_fattr;
2065 struct nfs4_create_arg arg = {
2066 .dir_fh = NFS_FH(dir),
2068 .name = &dentry->d_name,
2071 .bitmask = server->attr_bitmask,
2073 struct nfs4_create_res res = {
2077 .dir_fattr = &dir_fattr,
2079 struct rpc_message msg = {
2080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2086 nfs_fattr_init(&fattr);
2087 nfs_fattr_init(&dir_fattr);
2089 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2091 update_changeattr(dir, &res.dir_cinfo);
2092 nfs_post_op_update_inode(dir, res.dir_fattr);
2093 status = nfs_instantiate(dentry, &fhandle, &fattr);
2098 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2099 struct iattr *sattr)
2101 struct nfs4_exception exception = { };
2104 err = nfs4_handle_exception(NFS_SERVER(dir),
2105 _nfs4_proc_mkdir(dir, dentry, sattr),
2107 } while (exception.retry);
2111 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2112 u64 cookie, struct page *page, unsigned int count, int plus)
2114 struct inode *dir = dentry->d_inode;
2115 struct nfs4_readdir_arg args = {
2120 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2122 struct nfs4_readdir_res res;
2123 struct rpc_message msg = {
2124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2131 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2132 dentry->d_parent->d_name.name,
2133 dentry->d_name.name,
2134 (unsigned long long)cookie);
2135 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2136 res.pgbase = args.pgbase;
2137 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2139 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2140 dprintk("%s: returns %d\n", __FUNCTION__, status);
2144 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2145 u64 cookie, struct page *page, unsigned int count, int plus)
2147 struct nfs4_exception exception = { };
2150 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2151 _nfs4_proc_readdir(dentry, cred, cookie,
2154 } while (exception.retry);
2158 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2159 struct iattr *sattr, dev_t rdev)
2161 struct nfs_server *server = NFS_SERVER(dir);
2163 struct nfs_fattr fattr, dir_fattr;
2164 struct nfs4_create_arg arg = {
2165 .dir_fh = NFS_FH(dir),
2167 .name = &dentry->d_name,
2169 .bitmask = server->attr_bitmask,
2171 struct nfs4_create_res res = {
2175 .dir_fattr = &dir_fattr,
2177 struct rpc_message msg = {
2178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2183 int mode = sattr->ia_mode;
2185 nfs_fattr_init(&fattr);
2186 nfs_fattr_init(&dir_fattr);
2188 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2189 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2191 arg.ftype = NF4FIFO;
2192 else if (S_ISBLK(mode)) {
2194 arg.u.device.specdata1 = MAJOR(rdev);
2195 arg.u.device.specdata2 = MINOR(rdev);
2197 else if (S_ISCHR(mode)) {
2199 arg.u.device.specdata1 = MAJOR(rdev);
2200 arg.u.device.specdata2 = MINOR(rdev);
2203 arg.ftype = NF4SOCK;
2205 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2207 update_changeattr(dir, &res.dir_cinfo);
2208 nfs_post_op_update_inode(dir, res.dir_fattr);
2209 status = nfs_instantiate(dentry, &fh, &fattr);
2214 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2215 struct iattr *sattr, dev_t rdev)
2217 struct nfs4_exception exception = { };
2220 err = nfs4_handle_exception(NFS_SERVER(dir),
2221 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2223 } while (exception.retry);
2227 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2228 struct nfs_fsstat *fsstat)
2230 struct nfs4_statfs_arg args = {
2232 .bitmask = server->attr_bitmask,
2234 struct rpc_message msg = {
2235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2240 nfs_fattr_init(fsstat->fattr);
2241 return rpc_call_sync(server->client, &msg, 0);
2244 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2246 struct nfs4_exception exception = { };
2249 err = nfs4_handle_exception(server,
2250 _nfs4_proc_statfs(server, fhandle, fsstat),
2252 } while (exception.retry);
2256 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2257 struct nfs_fsinfo *fsinfo)
2259 struct nfs4_fsinfo_arg args = {
2261 .bitmask = server->attr_bitmask,
2263 struct rpc_message msg = {
2264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2269 return rpc_call_sync(server->client, &msg, 0);
2272 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2274 struct nfs4_exception exception = { };
2278 err = nfs4_handle_exception(server,
2279 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2281 } while (exception.retry);
2285 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2287 nfs_fattr_init(fsinfo->fattr);
2288 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2291 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2292 struct nfs_pathconf *pathconf)
2294 struct nfs4_pathconf_arg args = {
2296 .bitmask = server->attr_bitmask,
2298 struct rpc_message msg = {
2299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2301 .rpc_resp = pathconf,
2304 /* None of the pathconf attributes are mandatory to implement */
2305 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2306 memset(pathconf, 0, sizeof(*pathconf));
2310 nfs_fattr_init(pathconf->fattr);
2311 return rpc_call_sync(server->client, &msg, 0);
2314 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2315 struct nfs_pathconf *pathconf)
2317 struct nfs4_exception exception = { };
2321 err = nfs4_handle_exception(server,
2322 _nfs4_proc_pathconf(server, fhandle, pathconf),
2324 } while (exception.retry);
2328 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2330 struct nfs_server *server = NFS_SERVER(data->inode);
2332 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
2333 rpc_restart_call(task);
2336 if (task->tk_status > 0)
2337 renew_lease(server, data->timestamp);
2341 static void nfs4_proc_read_setup(struct nfs_read_data *data)
2343 struct rpc_message msg = {
2344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2345 .rpc_argp = &data->args,
2346 .rpc_resp = &data->res,
2347 .rpc_cred = data->cred,
2350 data->timestamp = jiffies;
2352 rpc_call_setup(&data->task, &msg, 0);
2355 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2357 struct inode *inode = data->inode;
2359 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2360 rpc_restart_call(task);
2363 if (task->tk_status >= 0) {
2364 renew_lease(NFS_SERVER(inode), data->timestamp);
2365 nfs_post_op_update_inode(inode, data->res.fattr);
2370 static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2372 struct rpc_message msg = {
2373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2374 .rpc_argp = &data->args,
2375 .rpc_resp = &data->res,
2376 .rpc_cred = data->cred,
2378 struct inode *inode = data->inode;
2379 struct nfs_server *server = NFS_SERVER(inode);
2382 if (how & FLUSH_STABLE) {
2383 if (!NFS_I(inode)->ncommit)
2384 stable = NFS_FILE_SYNC;
2386 stable = NFS_DATA_SYNC;
2388 stable = NFS_UNSTABLE;
2389 data->args.stable = stable;
2390 data->args.bitmask = server->attr_bitmask;
2391 data->res.server = server;
2393 data->timestamp = jiffies;
2395 /* Finalize the task. */
2396 rpc_call_setup(&data->task, &msg, 0);
2399 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2401 struct inode *inode = data->inode;
2403 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2404 rpc_restart_call(task);
2407 if (task->tk_status >= 0)
2408 nfs_post_op_update_inode(inode, data->res.fattr);
2412 static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2414 struct rpc_message msg = {
2415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2416 .rpc_argp = &data->args,
2417 .rpc_resp = &data->res,
2418 .rpc_cred = data->cred,
2420 struct nfs_server *server = NFS_SERVER(data->inode);
2422 data->args.bitmask = server->attr_bitmask;
2423 data->res.server = server;
2425 rpc_call_setup(&data->task, &msg, 0);
2429 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2430 * standalone procedure for queueing an asynchronous RENEW.
2432 static void nfs4_renew_done(struct rpc_task *task, void *data)
2434 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2435 unsigned long timestamp = (unsigned long)data;
2437 if (task->tk_status < 0) {
2438 switch (task->tk_status) {
2439 case -NFS4ERR_STALE_CLIENTID:
2440 case -NFS4ERR_EXPIRED:
2441 case -NFS4ERR_CB_PATH_DOWN:
2442 nfs4_schedule_state_recovery(clp);
2446 spin_lock(&clp->cl_lock);
2447 if (time_before(clp->cl_last_renewal,timestamp))
2448 clp->cl_last_renewal = timestamp;
2449 spin_unlock(&clp->cl_lock);
2452 static const struct rpc_call_ops nfs4_renew_ops = {
2453 .rpc_call_done = nfs4_renew_done,
2456 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2458 struct rpc_message msg = {
2459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2464 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2465 &nfs4_renew_ops, (void *)jiffies);
2468 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2470 struct rpc_message msg = {
2471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2475 unsigned long now = jiffies;
2478 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2481 spin_lock(&clp->cl_lock);
2482 if (time_before(clp->cl_last_renewal,now))
2483 clp->cl_last_renewal = now;
2484 spin_unlock(&clp->cl_lock);
2488 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2490 return (server->caps & NFS_CAP_ACLS)
2491 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2492 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2495 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2496 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2499 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2501 static void buf_to_pages(const void *buf, size_t buflen,
2502 struct page **pages, unsigned int *pgbase)
2504 const void *p = buf;
2506 *pgbase = offset_in_page(buf);
2508 while (p < buf + buflen) {
2509 *(pages++) = virt_to_page(p);
2510 p += PAGE_CACHE_SIZE;
2514 struct nfs4_cached_acl {
2520 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2522 struct nfs_inode *nfsi = NFS_I(inode);
2524 spin_lock(&inode->i_lock);
2525 kfree(nfsi->nfs4_acl);
2526 nfsi->nfs4_acl = acl;
2527 spin_unlock(&inode->i_lock);
2530 static void nfs4_zap_acl_attr(struct inode *inode)
2532 nfs4_set_cached_acl(inode, NULL);
2535 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2537 struct nfs_inode *nfsi = NFS_I(inode);
2538 struct nfs4_cached_acl *acl;
2541 spin_lock(&inode->i_lock);
2542 acl = nfsi->nfs4_acl;
2545 if (buf == NULL) /* user is just asking for length */
2547 if (acl->cached == 0)
2549 ret = -ERANGE; /* see getxattr(2) man page */
2550 if (acl->len > buflen)
2552 memcpy(buf, acl->data, acl->len);
2556 spin_unlock(&inode->i_lock);
2560 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2562 struct nfs4_cached_acl *acl;
2564 if (buf && acl_len <= PAGE_SIZE) {
2565 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2569 memcpy(acl->data, buf, acl_len);
2571 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2578 nfs4_set_cached_acl(inode, acl);
2581 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2583 struct page *pages[NFS4ACL_MAXPAGES];
2584 struct nfs_getaclargs args = {
2585 .fh = NFS_FH(inode),
2589 size_t resp_len = buflen;
2591 struct rpc_message msg = {
2592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2594 .rpc_resp = &resp_len,
2596 struct page *localpage = NULL;
2599 if (buflen < PAGE_SIZE) {
2600 /* As long as we're doing a round trip to the server anyway,
2601 * let's be prepared for a page of acl data. */
2602 localpage = alloc_page(GFP_KERNEL);
2603 resp_buf = page_address(localpage);
2604 if (localpage == NULL)
2606 args.acl_pages[0] = localpage;
2607 args.acl_pgbase = 0;
2608 resp_len = args.acl_len = PAGE_SIZE;
2611 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2613 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2616 if (resp_len > args.acl_len)
2617 nfs4_write_cached_acl(inode, NULL, resp_len);
2619 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2622 if (resp_len > buflen)
2625 memcpy(buf, resp_buf, resp_len);
2630 __free_page(localpage);
2634 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2636 struct nfs4_exception exception = { };
2639 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2642 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2643 } while (exception.retry);
2647 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2649 struct nfs_server *server = NFS_SERVER(inode);
2652 if (!nfs4_server_supports_acls(server))
2654 ret = nfs_revalidate_inode(server, inode);
2657 ret = nfs4_read_cached_acl(inode, buf, buflen);
2660 return nfs4_get_acl_uncached(inode, buf, buflen);
2663 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2665 struct nfs_server *server = NFS_SERVER(inode);
2666 struct page *pages[NFS4ACL_MAXPAGES];
2667 struct nfs_setaclargs arg = {
2668 .fh = NFS_FH(inode),
2672 struct rpc_message msg = {
2673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2679 if (!nfs4_server_supports_acls(server))
2681 nfs_inode_return_delegation(inode);
2682 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2683 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2684 nfs_zap_caches(inode);
2688 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2690 struct nfs4_exception exception = { };
2693 err = nfs4_handle_exception(NFS_SERVER(inode),
2694 __nfs4_proc_set_acl(inode, buf, buflen),
2696 } while (exception.retry);
2701 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2703 struct nfs_client *clp = server->nfs_client;
2705 if (!clp || task->tk_status >= 0)
2707 switch(task->tk_status) {
2708 case -NFS4ERR_STALE_CLIENTID:
2709 case -NFS4ERR_STALE_STATEID:
2710 case -NFS4ERR_EXPIRED:
2711 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2712 nfs4_schedule_state_recovery(clp);
2713 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
2714 rpc_wake_up_task(task);
2715 task->tk_status = 0;
2717 case -NFS4ERR_DELAY:
2718 nfs_inc_server_stats((struct nfs_server *) server,
2720 case -NFS4ERR_GRACE:
2721 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2722 task->tk_status = 0;
2724 case -NFS4ERR_OLD_STATEID:
2725 task->tk_status = 0;
2728 task->tk_status = nfs4_map_errors(task->tk_status);
2732 static int nfs4_wait_bit_interruptible(void *word)
2734 if (signal_pending(current))
2735 return -ERESTARTSYS;
2740 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
2747 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2749 rpc_clnt_sigmask(clnt, &oldset);
2750 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2751 nfs4_wait_bit_interruptible,
2752 TASK_INTERRUPTIBLE);
2753 rpc_clnt_sigunmask(clnt, &oldset);
2755 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
2759 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2767 *timeout = NFS4_POLL_RETRY_MIN;
2768 if (*timeout > NFS4_POLL_RETRY_MAX)
2769 *timeout = NFS4_POLL_RETRY_MAX;
2770 rpc_clnt_sigmask(clnt, &oldset);
2771 if (clnt->cl_intr) {
2772 schedule_timeout_interruptible(*timeout);
2776 schedule_timeout_uninterruptible(*timeout);
2777 rpc_clnt_sigunmask(clnt, &oldset);
2782 /* This is the error handling routine for processes that are allowed
2785 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2787 struct nfs_client *clp = server->nfs_client;
2788 int ret = errorcode;
2790 exception->retry = 0;
2794 case -NFS4ERR_STALE_CLIENTID:
2795 case -NFS4ERR_STALE_STATEID:
2796 case -NFS4ERR_EXPIRED:
2797 nfs4_schedule_state_recovery(clp);
2798 ret = nfs4_wait_clnt_recover(server->client, clp);
2800 exception->retry = 1;
2802 case -NFS4ERR_FILE_OPEN:
2803 case -NFS4ERR_GRACE:
2804 case -NFS4ERR_DELAY:
2805 ret = nfs4_delay(server->client, &exception->timeout);
2808 case -NFS4ERR_OLD_STATEID:
2809 exception->retry = 1;
2811 /* We failed to handle the error */
2812 return nfs4_map_errors(ret);
2815 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
2817 nfs4_verifier sc_verifier;
2818 struct nfs4_setclientid setclientid = {
2819 .sc_verifier = &sc_verifier,
2822 struct rpc_message msg = {
2823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2824 .rpc_argp = &setclientid,
2832 p = (__be32*)sc_verifier.data;
2833 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2834 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2837 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2838 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2839 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
2840 cred->cr_ops->cr_name,
2841 clp->cl_id_uniquifier);
2842 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2843 sizeof(setclientid.sc_netid), "tcp");
2844 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2845 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2846 clp->cl_ipaddr, port >> 8, port & 255);
2848 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2849 if (status != -NFS4ERR_CLID_INUSE)
2854 ssleep(clp->cl_lease_time + 1);
2856 if (++clp->cl_id_uniquifier == 0)
2862 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2864 struct nfs_fsinfo fsinfo;
2865 struct rpc_message msg = {
2866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2868 .rpc_resp = &fsinfo,
2875 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2877 spin_lock(&clp->cl_lock);
2878 clp->cl_lease_time = fsinfo.lease_time * HZ;
2879 clp->cl_last_renewal = now;
2880 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2881 spin_unlock(&clp->cl_lock);
2886 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2891 err = _nfs4_proc_setclientid_confirm(clp, cred);
2895 case -NFS4ERR_RESOURCE:
2896 /* The IBM lawyers misread another document! */
2897 case -NFS4ERR_DELAY:
2898 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2904 struct nfs4_delegreturndata {
2905 struct nfs4_delegreturnargs args;
2906 struct nfs4_delegreturnres res;
2908 nfs4_stateid stateid;
2909 struct rpc_cred *cred;
2910 unsigned long timestamp;
2911 struct nfs_fattr fattr;
2915 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
2917 struct nfs4_delegreturndata *data = calldata;
2918 struct rpc_message msg = {
2919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2920 .rpc_argp = &data->args,
2921 .rpc_resp = &data->res,
2922 .rpc_cred = data->cred,
2924 nfs_fattr_init(data->res.fattr);
2925 rpc_call_setup(task, &msg, 0);
2928 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2930 struct nfs4_delegreturndata *data = calldata;
2931 data->rpc_status = task->tk_status;
2932 if (data->rpc_status == 0)
2933 renew_lease(data->res.server, data->timestamp);
2936 static void nfs4_delegreturn_release(void *calldata)
2938 struct nfs4_delegreturndata *data = calldata;
2940 put_rpccred(data->cred);
2944 static const struct rpc_call_ops nfs4_delegreturn_ops = {
2945 .rpc_call_prepare = nfs4_delegreturn_prepare,
2946 .rpc_call_done = nfs4_delegreturn_done,
2947 .rpc_release = nfs4_delegreturn_release,
2950 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2952 struct nfs4_delegreturndata *data;
2953 struct nfs_server *server = NFS_SERVER(inode);
2954 struct rpc_task *task;
2957 data = kmalloc(sizeof(*data), GFP_KERNEL);
2960 data->args.fhandle = &data->fh;
2961 data->args.stateid = &data->stateid;
2962 data->args.bitmask = server->attr_bitmask;
2963 nfs_copy_fh(&data->fh, NFS_FH(inode));
2964 memcpy(&data->stateid, stateid, sizeof(data->stateid));
2965 data->res.fattr = &data->fattr;
2966 data->res.server = server;
2967 data->cred = get_rpccred(cred);
2968 data->timestamp = jiffies;
2969 data->rpc_status = 0;
2971 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
2973 return PTR_ERR(task);
2974 status = nfs4_wait_for_completion_rpc_task(task);
2976 status = data->rpc_status;
2978 nfs_post_op_update_inode(inode, &data->fattr);
2984 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2986 struct nfs_server *server = NFS_SERVER(inode);
2987 struct nfs4_exception exception = { };
2990 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2992 case -NFS4ERR_STALE_STATEID:
2993 case -NFS4ERR_EXPIRED:
2997 err = nfs4_handle_exception(server, err, &exception);
2998 } while (exception.retry);
3002 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3003 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3006 * sleep, with exponential backoff, and retry the LOCK operation.
3008 static unsigned long
3009 nfs4_set_lock_task_retry(unsigned long timeout)
3011 schedule_timeout_interruptible(timeout);
3013 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3014 return NFS4_LOCK_MAXTIMEOUT;
3018 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3020 struct inode *inode = state->inode;
3021 struct nfs_server *server = NFS_SERVER(inode);
3022 struct nfs_client *clp = server->nfs_client;
3023 struct nfs_lockt_args arg = {
3024 .fh = NFS_FH(inode),
3027 struct nfs_lockt_res res = {
3030 struct rpc_message msg = {
3031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3034 .rpc_cred = state->owner->so_cred,
3036 struct nfs4_lock_state *lsp;
3039 down_read(&clp->cl_sem);
3040 arg.lock_owner.clientid = clp->cl_clientid;
3041 status = nfs4_set_lock_state(state, request);
3044 lsp = request->fl_u.nfs4_fl.owner;
3045 arg.lock_owner.id = lsp->ls_id.id;
3046 status = rpc_call_sync(server->client, &msg, 0);
3049 request->fl_type = F_UNLCK;
3051 case -NFS4ERR_DENIED:
3054 request->fl_ops->fl_release_private(request);
3056 up_read(&clp->cl_sem);
3060 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3062 struct nfs4_exception exception = { };
3066 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3067 _nfs4_proc_getlk(state, cmd, request),
3069 } while (exception.retry);
3073 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3076 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3078 res = posix_lock_file_wait(file, fl);
3081 res = flock_lock_file_wait(file, fl);
3089 struct nfs4_unlockdata {
3090 struct nfs_locku_args arg;
3091 struct nfs_locku_res res;
3092 struct nfs4_lock_state *lsp;
3093 struct nfs_open_context *ctx;
3094 struct file_lock fl;
3095 const struct nfs_server *server;
3096 unsigned long timestamp;
3099 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3100 struct nfs_open_context *ctx,
3101 struct nfs4_lock_state *lsp,
3102 struct nfs_seqid *seqid)
3104 struct nfs4_unlockdata *p;
3105 struct inode *inode = lsp->ls_state->inode;
3107 p = kmalloc(sizeof(*p), GFP_KERNEL);
3110 p->arg.fh = NFS_FH(inode);
3112 p->arg.seqid = seqid;
3113 p->arg.stateid = &lsp->ls_stateid;
3115 atomic_inc(&lsp->ls_count);
3116 /* Ensure we don't close file until we're done freeing locks! */
3117 p->ctx = get_nfs_open_context(ctx);
3118 memcpy(&p->fl, fl, sizeof(p->fl));
3119 p->server = NFS_SERVER(inode);
3123 static void nfs4_locku_release_calldata(void *data)
3125 struct nfs4_unlockdata *calldata = data;
3126 nfs_free_seqid(calldata->arg.seqid);
3127 nfs4_put_lock_state(calldata->lsp);
3128 put_nfs_open_context(calldata->ctx);
3132 static void nfs4_locku_done(struct rpc_task *task, void *data)
3134 struct nfs4_unlockdata *calldata = data;
3136 if (RPC_ASSASSINATED(task))
3138 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
3139 switch (task->tk_status) {
3141 memcpy(calldata->lsp->ls_stateid.data,
3142 calldata->res.stateid.data,
3143 sizeof(calldata->lsp->ls_stateid.data));
3144 renew_lease(calldata->server, calldata->timestamp);
3146 case -NFS4ERR_STALE_STATEID:
3147 case -NFS4ERR_EXPIRED:
3150 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
3151 rpc_restart_call(task);
3155 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3157 struct nfs4_unlockdata *calldata = data;
3158 struct rpc_message msg = {
3159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3160 .rpc_argp = &calldata->arg,
3161 .rpc_resp = &calldata->res,
3162 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
3165 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3167 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3168 /* Note: exit _without_ running nfs4_locku_done */
3169 task->tk_action = NULL;
3172 calldata->timestamp = jiffies;
3173 rpc_call_setup(task, &msg, 0);
3176 static const struct rpc_call_ops nfs4_locku_ops = {
3177 .rpc_call_prepare = nfs4_locku_prepare,
3178 .rpc_call_done = nfs4_locku_done,
3179 .rpc_release = nfs4_locku_release_calldata,
3182 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3183 struct nfs_open_context *ctx,
3184 struct nfs4_lock_state *lsp,
3185 struct nfs_seqid *seqid)
3187 struct nfs4_unlockdata *data;
3189 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3191 nfs_free_seqid(seqid);
3192 return ERR_PTR(-ENOMEM);
3195 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
3198 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3200 struct nfs_seqid *seqid;
3201 struct nfs4_lock_state *lsp;
3202 struct rpc_task *task;
3205 status = nfs4_set_lock_state(state, request);
3206 /* Unlock _before_ we do the RPC call */
3207 request->fl_flags |= FL_EXISTS;
3208 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3212 /* Is this a delegated lock? */
3213 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3215 lsp = request->fl_u.nfs4_fl.owner;
3216 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3220 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3221 status = PTR_ERR(task);
3224 status = nfs4_wait_for_completion_rpc_task(task);
3230 struct nfs4_lockdata {
3231 struct nfs_lock_args arg;
3232 struct nfs_lock_res res;
3233 struct nfs4_lock_state *lsp;
3234 struct nfs_open_context *ctx;
3235 struct file_lock fl;
3236 unsigned long timestamp;
3241 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3242 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3244 struct nfs4_lockdata *p;
3245 struct inode *inode = lsp->ls_state->inode;
3246 struct nfs_server *server = NFS_SERVER(inode);
3248 p = kzalloc(sizeof(*p), GFP_KERNEL);
3252 p->arg.fh = NFS_FH(inode);
3254 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3255 if (p->arg.lock_seqid == NULL)
3257 p->arg.lock_stateid = &lsp->ls_stateid;
3258 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3259 p->arg.lock_owner.id = lsp->ls_id.id;
3261 atomic_inc(&lsp->ls_count);
3262 p->ctx = get_nfs_open_context(ctx);
3263 memcpy(&p->fl, fl, sizeof(p->fl));
3270 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3272 struct nfs4_lockdata *data = calldata;
3273 struct nfs4_state *state = data->lsp->ls_state;
3274 struct nfs4_state_owner *sp = state->owner;
3275 struct rpc_message msg = {
3276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3277 .rpc_argp = &data->arg,
3278 .rpc_resp = &data->res,
3279 .rpc_cred = sp->so_cred,
3282 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3284 dprintk("%s: begin!\n", __FUNCTION__);
3285 /* Do we need to do an open_to_lock_owner? */
3286 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3287 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3288 if (data->arg.open_seqid == NULL) {
3289 data->rpc_status = -ENOMEM;
3290 task->tk_action = NULL;
3293 data->arg.open_stateid = &state->stateid;
3294 data->arg.new_lock_owner = 1;
3296 data->timestamp = jiffies;
3297 rpc_call_setup(task, &msg, 0);
3299 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3302 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3304 struct nfs4_lockdata *data = calldata;
3306 dprintk("%s: begin!\n", __FUNCTION__);
3308 data->rpc_status = task->tk_status;
3309 if (RPC_ASSASSINATED(task))
3311 if (data->arg.new_lock_owner != 0) {
3312 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3313 if (data->rpc_status == 0)
3314 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3318 if (data->rpc_status == 0) {
3319 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3320 sizeof(data->lsp->ls_stateid.data));
3321 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3322 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3324 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3326 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3329 static void nfs4_lock_release(void *calldata)
3331 struct nfs4_lockdata *data = calldata;
3333 dprintk("%s: begin!\n", __FUNCTION__);
3334 if (data->arg.open_seqid != NULL)
3335 nfs_free_seqid(data->arg.open_seqid);
3336 if (data->cancelled != 0) {
3337 struct rpc_task *task;
3338 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3339 data->arg.lock_seqid);
3342 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3344 nfs_free_seqid(data->arg.lock_seqid);
3345 nfs4_put_lock_state(data->lsp);
3346 put_nfs_open_context(data->ctx);
3348 dprintk("%s: done!\n", __FUNCTION__);
3351 static const struct rpc_call_ops nfs4_lock_ops = {
3352 .rpc_call_prepare = nfs4_lock_prepare,
3353 .rpc_call_done = nfs4_lock_done,
3354 .rpc_release = nfs4_lock_release,
3357 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3359 struct nfs4_lockdata *data;
3360 struct rpc_task *task;
3363 dprintk("%s: begin!\n", __FUNCTION__);
3364 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3365 fl->fl_u.nfs4_fl.owner);
3369 data->arg.block = 1;
3371 data->arg.reclaim = 1;
3372 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3373 &nfs4_lock_ops, data);
3375 return PTR_ERR(task);
3376 ret = nfs4_wait_for_completion_rpc_task(task);
3378 ret = data->rpc_status;
3379 if (ret == -NFS4ERR_DENIED)
3382 data->cancelled = 1;
3384 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3388 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3390 struct nfs_server *server = NFS_SERVER(state->inode);
3391 struct nfs4_exception exception = { };
3395 /* Cache the lock if possible... */
3396 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3398 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3399 if (err != -NFS4ERR_DELAY)
3401 nfs4_handle_exception(server, err, &exception);
3402 } while (exception.retry);
3406 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3408 struct nfs_server *server = NFS_SERVER(state->inode);
3409 struct nfs4_exception exception = { };
3412 err = nfs4_set_lock_state(state, request);
3416 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3418 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3419 if (err != -NFS4ERR_DELAY)
3421 nfs4_handle_exception(server, err, &exception);
3422 } while (exception.retry);
3426 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3428 struct nfs_client *clp = state->owner->so_client;
3429 unsigned char fl_flags = request->fl_flags;
3432 /* Is this a delegated open? */
3433 status = nfs4_set_lock_state(state, request);
3436 request->fl_flags |= FL_ACCESS;
3437 status = do_vfs_lock(request->fl_file, request);
3440 down_read(&clp->cl_sem);
3441 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3442 struct nfs_inode *nfsi = NFS_I(state->inode);
3443 /* Yes: cache locks! */
3444 down_read(&nfsi->rwsem);
3445 /* ...but avoid races with delegation recall... */
3446 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3447 request->fl_flags = fl_flags & ~FL_SLEEP;
3448 status = do_vfs_lock(request->fl_file, request);
3449 up_read(&nfsi->rwsem);
3452 up_read(&nfsi->rwsem);
3454 status = _nfs4_do_setlk(state, cmd, request, 0);
3457 /* Note: we always want to sleep here! */
3458 request->fl_flags = fl_flags | FL_SLEEP;
3459 if (do_vfs_lock(request->fl_file, request) < 0)
3460 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3462 up_read(&clp->cl_sem);
3464 request->fl_flags = fl_flags;
3468 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3470 struct nfs4_exception exception = { };
3474 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3475 _nfs4_proc_setlk(state, cmd, request),
3477 } while (exception.retry);
3482 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3484 struct nfs_open_context *ctx;
3485 struct nfs4_state *state;
3486 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3489 /* verify open state */
3490 ctx = (struct nfs_open_context *)filp->private_data;
3493 if (request->fl_start < 0 || request->fl_end < 0)
3497 return nfs4_proc_getlk(state, F_GETLK, request);
3499 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3502 if (request->fl_type == F_UNLCK)
3503 return nfs4_proc_unlck(state, cmd, request);
3506 status = nfs4_proc_setlk(state, cmd, request);
3507 if ((status != -EAGAIN) || IS_SETLK(cmd))
3509 timeout = nfs4_set_lock_task_retry(timeout);
3510 status = -ERESTARTSYS;
3513 } while(status < 0);
3517 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3519 struct nfs_server *server = NFS_SERVER(state->inode);
3520 struct nfs4_exception exception = { };
3523 err = nfs4_set_lock_state(state, fl);
3527 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3528 if (err != -NFS4ERR_DELAY)
3530 err = nfs4_handle_exception(server, err, &exception);
3531 } while (exception.retry);
3536 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3538 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3539 size_t buflen, int flags)
3541 struct inode *inode = dentry->d_inode;
3543 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3546 if (!S_ISREG(inode->i_mode) &&
3547 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3550 return nfs4_proc_set_acl(inode, buf, buflen);
3553 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3554 * and that's what we'll do for e.g. user attributes that haven't been set.
3555 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3556 * attributes in kernel-managed attribute namespaces. */
3557 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3560 struct inode *inode = dentry->d_inode;
3562 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3565 return nfs4_proc_get_acl(inode, buf, buflen);
3568 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3570 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3572 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3574 if (buf && buflen < len)
3577 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3581 int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
3582 struct nfs4_fs_locations *fs_locations, struct page *page)
3584 struct nfs_server *server = NFS_SERVER(dir);
3586 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3587 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3589 struct nfs4_fs_locations_arg args = {
3590 .dir_fh = NFS_FH(dir),
3595 struct rpc_message msg = {
3596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3598 .rpc_resp = fs_locations,
3602 dprintk("%s: start\n", __FUNCTION__);
3603 nfs_fattr_init(&fs_locations->fattr);
3604 fs_locations->server = server;
3605 fs_locations->nlocations = 0;
3606 status = rpc_call_sync(server->client, &msg, 0);
3607 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3611 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3612 .recover_open = nfs4_open_reclaim,
3613 .recover_lock = nfs4_lock_reclaim,
3616 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3617 .recover_open = nfs4_open_expired,
3618 .recover_lock = nfs4_lock_expired,
3621 static const struct inode_operations nfs4_file_inode_operations = {
3622 .permission = nfs_permission,
3623 .getattr = nfs_getattr,
3624 .setattr = nfs_setattr,
3625 .getxattr = nfs4_getxattr,
3626 .setxattr = nfs4_setxattr,
3627 .listxattr = nfs4_listxattr,
3630 const struct nfs_rpc_ops nfs_v4_clientops = {
3631 .version = 4, /* protocol version */
3632 .dentry_ops = &nfs4_dentry_operations,
3633 .dir_inode_ops = &nfs4_dir_inode_operations,
3634 .file_inode_ops = &nfs4_file_inode_operations,
3635 .getroot = nfs4_proc_get_root,
3636 .getattr = nfs4_proc_getattr,
3637 .setattr = nfs4_proc_setattr,
3638 .lookupfh = nfs4_proc_lookupfh,
3639 .lookup = nfs4_proc_lookup,
3640 .access = nfs4_proc_access,
3641 .readlink = nfs4_proc_readlink,
3642 .create = nfs4_proc_create,
3643 .remove = nfs4_proc_remove,
3644 .unlink_setup = nfs4_proc_unlink_setup,
3645 .unlink_done = nfs4_proc_unlink_done,
3646 .rename = nfs4_proc_rename,
3647 .link = nfs4_proc_link,
3648 .symlink = nfs4_proc_symlink,
3649 .mkdir = nfs4_proc_mkdir,
3650 .rmdir = nfs4_proc_remove,
3651 .readdir = nfs4_proc_readdir,
3652 .mknod = nfs4_proc_mknod,
3653 .statfs = nfs4_proc_statfs,
3654 .fsinfo = nfs4_proc_fsinfo,
3655 .pathconf = nfs4_proc_pathconf,
3656 .set_capabilities = nfs4_server_capabilities,
3657 .decode_dirent = nfs4_decode_dirent,
3658 .read_setup = nfs4_proc_read_setup,
3659 .read_done = nfs4_read_done,
3660 .write_setup = nfs4_proc_write_setup,
3661 .write_done = nfs4_write_done,
3662 .commit_setup = nfs4_proc_commit_setup,
3663 .commit_done = nfs4_commit_done,
3664 .file_open = nfs_open,
3665 .file_release = nfs_release,
3666 .lock = nfs4_proc_lock,
3667 .clear_acl_cache = nfs4_zap_acl_attr,