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 (1*HZ)
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 nfs4_client *clp);
68 extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
69 extern struct rpc_procinfo nfs4_procedures[];
71 /* Prevent leaks of NFSv4 errors into userland */
72 int nfs4_map_errors(int err)
75 dprintk("%s could not handle NFSv4 error %d\n",
83 * This is our standard bitmap for GETATTR requests.
85 const u32 nfs4_fattr_bitmap[2] = {
90 | FATTR4_WORD0_FILEID,
92 | FATTR4_WORD1_NUMLINKS
94 | FATTR4_WORD1_OWNER_GROUP
96 | FATTR4_WORD1_SPACE_USED
97 | FATTR4_WORD1_TIME_ACCESS
98 | FATTR4_WORD1_TIME_METADATA
99 | FATTR4_WORD1_TIME_MODIFY
102 const u32 nfs4_statfs_bitmap[2] = {
103 FATTR4_WORD0_FILES_AVAIL
104 | FATTR4_WORD0_FILES_FREE
105 | FATTR4_WORD0_FILES_TOTAL,
106 FATTR4_WORD1_SPACE_AVAIL
107 | FATTR4_WORD1_SPACE_FREE
108 | FATTR4_WORD1_SPACE_TOTAL
111 const u32 nfs4_pathconf_bitmap[2] = {
113 | FATTR4_WORD0_MAXNAME,
117 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
118 | FATTR4_WORD0_MAXREAD
119 | FATTR4_WORD0_MAXWRITE
120 | FATTR4_WORD0_LEASE_TIME,
124 static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
125 struct nfs4_readdir_arg *readdir)
129 BUG_ON(readdir->count < 80);
131 readdir->cookie = cookie;
132 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
137 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
142 * NFSv4 servers do not return entries for '.' and '..'
143 * Therefore, we fake these entries here. We let '.'
144 * have cookie 0 and '..' have cookie 1. Note that
145 * when talking to the server, we always send cookie 0
148 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
151 *p++ = xdr_one; /* next */
152 *p++ = xdr_zero; /* cookie, first word */
153 *p++ = xdr_one; /* cookie, second word */
154 *p++ = xdr_one; /* entry len */
155 memcpy(p, ".\0\0\0", 4); /* entry */
157 *p++ = xdr_one; /* bitmap length */
158 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
159 *p++ = htonl(8); /* attribute buffer length */
160 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
163 *p++ = xdr_one; /* next */
164 *p++ = xdr_zero; /* cookie, first word */
165 *p++ = xdr_two; /* cookie, second word */
166 *p++ = xdr_two; /* entry len */
167 memcpy(p, "..\0\0", 4); /* entry */
169 *p++ = xdr_one; /* bitmap length */
170 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
171 *p++ = htonl(8); /* attribute buffer length */
172 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
174 readdir->pgbase = (char *)p - (char *)start;
175 readdir->count -= readdir->pgbase;
176 kunmap_atomic(start, KM_USER0);
179 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
181 struct nfs4_client *clp = server->nfs4_state;
182 spin_lock(&clp->cl_lock);
183 if (time_before(clp->cl_last_renewal,timestamp))
184 clp->cl_last_renewal = timestamp;
185 spin_unlock(&clp->cl_lock);
188 static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
190 struct nfs_inode *nfsi = NFS_I(inode);
192 spin_lock(&inode->i_lock);
193 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
194 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
195 nfsi->change_attr = cinfo->after;
196 spin_unlock(&inode->i_lock);
199 struct nfs4_opendata {
201 struct nfs_openargs o_arg;
202 struct nfs_openres o_res;
203 struct nfs_open_confirmargs c_arg;
204 struct nfs_open_confirmres c_res;
205 struct nfs_fattr f_attr;
206 struct nfs_fattr dir_attr;
207 struct dentry *dentry;
209 struct nfs4_state_owner *owner;
211 unsigned long timestamp;
216 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
217 struct nfs4_state_owner *sp, int flags,
218 const struct iattr *attrs)
220 struct dentry *parent = dget_parent(dentry);
221 struct inode *dir = parent->d_inode;
222 struct nfs_server *server = NFS_SERVER(dir);
223 struct nfs4_opendata *p;
225 p = kzalloc(sizeof(*p), GFP_KERNEL);
228 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
229 if (p->o_arg.seqid == NULL)
231 atomic_set(&p->count, 1);
232 p->dentry = dget(dentry);
235 atomic_inc(&sp->so_count);
236 p->o_arg.fh = NFS_FH(dir);
237 p->o_arg.open_flags = flags,
238 p->o_arg.clientid = server->nfs4_state->cl_clientid;
239 p->o_arg.id = sp->so_id;
240 p->o_arg.name = &dentry->d_name;
241 p->o_arg.server = server;
242 p->o_arg.bitmask = server->attr_bitmask;
243 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
244 p->o_res.f_attr = &p->f_attr;
245 p->o_res.dir_attr = &p->dir_attr;
246 p->o_res.server = server;
247 nfs_fattr_init(&p->f_attr);
248 nfs_fattr_init(&p->dir_attr);
249 if (flags & O_EXCL) {
250 u32 *s = (u32 *) p->o_arg.u.verifier.data;
253 } else if (flags & O_CREAT) {
254 p->o_arg.u.attrs = &p->attrs;
255 memcpy(&p->attrs, attrs, sizeof(p->attrs));
257 p->c_arg.fh = &p->o_res.fh;
258 p->c_arg.stateid = &p->o_res.stateid;
259 p->c_arg.seqid = p->o_arg.seqid;
268 static void nfs4_opendata_free(struct nfs4_opendata *p)
270 if (p != NULL && atomic_dec_and_test(&p->count)) {
271 nfs_free_seqid(p->o_arg.seqid);
272 nfs4_put_state_owner(p->owner);
279 /* Helper for asynchronous RPC calls */
280 static int nfs4_call_async(struct rpc_clnt *clnt,
281 const struct rpc_call_ops *tk_ops, void *calldata)
283 struct rpc_task *task;
285 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
291 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
296 rpc_clnt_sigmask(task->tk_client, &oldset);
297 ret = rpc_wait_for_completion_task(task);
298 rpc_clnt_sigunmask(task->tk_client, &oldset);
302 static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
304 switch (open_flags) {
311 case FMODE_READ|FMODE_WRITE:
316 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
318 struct inode *inode = state->inode;
320 open_flags &= (FMODE_READ|FMODE_WRITE);
321 /* Protect against nfs4_find_state_byowner() */
322 spin_lock(&state->owner->so_lock);
323 spin_lock(&inode->i_lock);
324 memcpy(&state->stateid, stateid, sizeof(state->stateid));
325 update_open_stateflags(state, open_flags);
326 nfs4_state_set_mode_locked(state, state->state | open_flags);
327 spin_unlock(&inode->i_lock);
328 spin_unlock(&state->owner->so_lock);
331 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
334 struct nfs4_state *state = NULL;
336 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
338 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
341 state = nfs4_get_open_state(inode, data->owner);
344 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
351 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
353 struct nfs_inode *nfsi = NFS_I(state->inode);
354 struct nfs_open_context *ctx;
356 spin_lock(&state->inode->i_lock);
357 list_for_each_entry(ctx, &nfsi->open_files, list) {
358 if (ctx->state != state)
360 get_nfs_open_context(ctx);
361 spin_unlock(&state->inode->i_lock);
364 spin_unlock(&state->inode->i_lock);
365 return ERR_PTR(-ENOENT);
368 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, nfs4_stateid *stateid)
372 opendata->o_arg.open_flags = openflags;
373 ret = _nfs4_proc_open(opendata);
376 memcpy(stateid->data, opendata->o_res.stateid.data,
377 sizeof(stateid->data));
381 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
383 nfs4_stateid stateid;
384 struct nfs4_state *newstate;
389 /* memory barrier prior to reading state->n_* */
391 if (state->n_rdwr != 0) {
392 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &stateid);
395 mode |= FMODE_READ|FMODE_WRITE;
396 if (opendata->o_res.delegation_type != 0)
397 delegation = opendata->o_res.delegation_type;
400 if (state->n_wronly != 0) {
401 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &stateid);
405 if (opendata->o_res.delegation_type != 0)
406 delegation = opendata->o_res.delegation_type;
409 if (state->n_rdonly != 0) {
410 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &stateid);
415 clear_bit(NFS_DELEGATED_STATE, &state->flags);
418 if (opendata->o_res.delegation_type == 0)
419 opendata->o_res.delegation_type = delegation;
420 opendata->o_arg.open_flags |= mode;
421 newstate = nfs4_opendata_to_nfs4_state(opendata);
422 if (newstate != NULL) {
423 if (opendata->o_res.delegation_type != 0) {
424 struct nfs_inode *nfsi = NFS_I(newstate->inode);
425 int delegation_flags = 0;
426 if (nfsi->delegation)
427 delegation_flags = nfsi->delegation->flags;
428 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
429 nfs_inode_set_delegation(newstate->inode,
430 opendata->owner->so_cred,
433 nfs_inode_reclaim_delegation(newstate->inode,
434 opendata->owner->so_cred,
437 nfs4_close_state(newstate, opendata->o_arg.open_flags);
439 if (newstate != state)
446 * reclaim state on the server after a reboot.
448 static int _nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
450 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
451 struct nfs4_opendata *opendata;
452 int delegation_type = 0;
455 if (delegation != NULL) {
456 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
457 memcpy(&state->stateid, &delegation->stateid,
458 sizeof(state->stateid));
459 set_bit(NFS_DELEGATED_STATE, &state->flags);
462 delegation_type = delegation->type;
464 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
465 if (opendata == NULL)
467 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
468 opendata->o_arg.fh = NFS_FH(state->inode);
469 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
470 opendata->o_arg.u.delegation_type = delegation_type;
471 status = nfs4_open_recover(opendata, state);
472 nfs4_opendata_free(opendata);
476 static int nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
478 struct nfs_server *server = NFS_SERVER(state->inode);
479 struct nfs4_exception exception = { };
482 err = _nfs4_do_open_reclaim(sp, state, dentry);
483 if (err != -NFS4ERR_DELAY)
485 nfs4_handle_exception(server, err, &exception);
486 } while (exception.retry);
490 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
492 struct nfs_open_context *ctx;
495 ctx = nfs4_state_find_open_context(state);
498 ret = nfs4_do_open_reclaim(sp, state, ctx->dentry);
499 put_nfs_open_context(ctx);
503 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
505 struct nfs4_state_owner *sp = state->owner;
506 struct nfs4_opendata *opendata;
509 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
511 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
512 if (opendata == NULL)
514 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
515 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
516 sizeof(opendata->o_arg.u.delegation.data));
517 ret = nfs4_open_recover(opendata, state);
518 nfs4_opendata_free(opendata);
522 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
524 struct nfs4_exception exception = { };
525 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
528 err = _nfs4_open_delegation_recall(dentry, state);
532 case -NFS4ERR_STALE_CLIENTID:
533 case -NFS4ERR_STALE_STATEID:
534 case -NFS4ERR_EXPIRED:
535 /* Don't recall a delegation if it was lost */
536 nfs4_schedule_state_recovery(server->nfs4_state);
539 err = nfs4_handle_exception(server, err, &exception);
540 } while (exception.retry);
544 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
546 struct nfs4_opendata *data = calldata;
547 struct rpc_message msg = {
548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
549 .rpc_argp = &data->c_arg,
550 .rpc_resp = &data->c_res,
551 .rpc_cred = data->owner->so_cred,
553 data->timestamp = jiffies;
554 rpc_call_setup(task, &msg, 0);
557 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
559 struct nfs4_opendata *data = calldata;
561 data->rpc_status = task->tk_status;
562 if (RPC_ASSASSINATED(task))
564 if (data->rpc_status == 0) {
565 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
566 sizeof(data->o_res.stateid.data));
567 renew_lease(data->o_res.server, data->timestamp);
569 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
570 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
573 static void nfs4_open_confirm_release(void *calldata)
575 struct nfs4_opendata *data = calldata;
576 struct nfs4_state *state = NULL;
578 /* If this request hasn't been cancelled, do nothing */
579 if (data->cancelled == 0)
581 /* In case of error, no cleanup! */
582 if (data->rpc_status != 0)
584 nfs_confirm_seqid(&data->owner->so_seqid, 0);
585 state = nfs4_opendata_to_nfs4_state(data);
587 nfs4_close_state(state, data->o_arg.open_flags);
589 nfs4_opendata_free(data);
592 static const struct rpc_call_ops nfs4_open_confirm_ops = {
593 .rpc_call_prepare = nfs4_open_confirm_prepare,
594 .rpc_call_done = nfs4_open_confirm_done,
595 .rpc_release = nfs4_open_confirm_release,
599 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
601 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
603 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
604 struct rpc_task *task;
607 atomic_inc(&data->count);
608 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
610 nfs4_opendata_free(data);
611 return PTR_ERR(task);
613 status = nfs4_wait_for_completion_rpc_task(task);
618 status = data->rpc_status;
619 rpc_release_task(task);
623 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
625 struct nfs4_opendata *data = calldata;
626 struct nfs4_state_owner *sp = data->owner;
627 struct rpc_message msg = {
628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
629 .rpc_argp = &data->o_arg,
630 .rpc_resp = &data->o_res,
631 .rpc_cred = sp->so_cred,
634 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
636 /* Update sequence id. */
637 data->o_arg.id = sp->so_id;
638 data->o_arg.clientid = sp->so_client->cl_clientid;
639 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
640 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
641 data->timestamp = jiffies;
642 rpc_call_setup(task, &msg, 0);
645 static void nfs4_open_done(struct rpc_task *task, void *calldata)
647 struct nfs4_opendata *data = calldata;
649 data->rpc_status = task->tk_status;
650 if (RPC_ASSASSINATED(task))
652 if (task->tk_status == 0) {
653 switch (data->o_res.f_attr->mode & S_IFMT) {
657 data->rpc_status = -ELOOP;
660 data->rpc_status = -EISDIR;
663 data->rpc_status = -ENOTDIR;
665 renew_lease(data->o_res.server, data->timestamp);
667 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
670 static void nfs4_open_release(void *calldata)
672 struct nfs4_opendata *data = calldata;
673 struct nfs4_state *state = NULL;
675 /* If this request hasn't been cancelled, do nothing */
676 if (data->cancelled == 0)
678 /* In case of error, no cleanup! */
679 if (data->rpc_status != 0)
681 /* In case we need an open_confirm, no cleanup! */
682 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
684 nfs_confirm_seqid(&data->owner->so_seqid, 0);
685 state = nfs4_opendata_to_nfs4_state(data);
687 nfs4_close_state(state, data->o_arg.open_flags);
689 nfs4_opendata_free(data);
692 static const struct rpc_call_ops nfs4_open_ops = {
693 .rpc_call_prepare = nfs4_open_prepare,
694 .rpc_call_done = nfs4_open_done,
695 .rpc_release = nfs4_open_release,
699 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
701 static int _nfs4_proc_open(struct nfs4_opendata *data)
703 struct inode *dir = data->dir->d_inode;
704 struct nfs_server *server = NFS_SERVER(dir);
705 struct nfs_openargs *o_arg = &data->o_arg;
706 struct nfs_openres *o_res = &data->o_res;
707 struct rpc_task *task;
710 atomic_inc(&data->count);
711 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
713 nfs4_opendata_free(data);
714 return PTR_ERR(task);
716 status = nfs4_wait_for_completion_rpc_task(task);
721 status = data->rpc_status;
722 rpc_release_task(task);
726 if (o_arg->open_flags & O_CREAT) {
727 update_changeattr(dir, &o_res->cinfo);
728 nfs_post_op_update_inode(dir, o_res->dir_attr);
730 nfs_refresh_inode(dir, o_res->dir_attr);
731 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
732 status = _nfs4_proc_open_confirm(data);
736 nfs_confirm_seqid(&data->owner->so_seqid, 0);
737 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
738 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
742 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
744 struct nfs_access_entry cache;
748 if (openflags & FMODE_READ)
750 if (openflags & FMODE_WRITE)
752 status = nfs_access_get_cached(inode, cred, &cache);
756 /* Be clever: ask server to check for all possible rights */
757 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
759 cache.jiffies = jiffies;
760 status = _nfs4_proc_access(inode, &cache);
763 nfs_access_add_cache(inode, &cache);
765 if ((cache.mask & mask) == mask)
770 int nfs4_recover_expired_lease(struct nfs_server *server)
772 struct nfs4_client *clp = server->nfs4_state;
774 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
775 nfs4_schedule_state_recovery(clp);
776 return nfs4_wait_clnt_recover(server->client, clp);
781 * reclaim state on the server after a network partition.
782 * Assumes caller holds the appropriate lock
784 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
786 struct inode *inode = state->inode;
787 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
788 struct nfs4_opendata *opendata;
789 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
792 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
793 ret = _nfs4_do_access(inode, sp->so_cred, openflags);
796 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
797 set_bit(NFS_DELEGATED_STATE, &state->flags);
800 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
801 if (opendata == NULL)
803 ret = nfs4_open_recover(opendata, state);
804 if (ret == -ESTALE) {
805 /* Invalidate the state owner so we don't ever use it again */
806 nfs4_drop_state_owner(sp);
809 nfs4_opendata_free(opendata);
813 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
815 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
816 struct nfs4_exception exception = { };
820 err = _nfs4_open_expired(sp, state, dentry);
821 if (err == -NFS4ERR_DELAY)
822 nfs4_handle_exception(server, err, &exception);
823 } while (exception.retry);
827 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
829 struct nfs_open_context *ctx;
832 ctx = nfs4_state_find_open_context(state);
835 ret = nfs4_do_open_expired(sp, state, ctx->dentry);
836 put_nfs_open_context(ctx);
841 * Returns a referenced nfs4_state if there is an open delegation on the file
843 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
845 struct nfs_delegation *delegation;
846 struct nfs_server *server = NFS_SERVER(inode);
847 struct nfs4_client *clp = server->nfs4_state;
848 struct nfs_inode *nfsi = NFS_I(inode);
849 struct nfs4_state_owner *sp = NULL;
850 struct nfs4_state *state = NULL;
851 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
855 if (!(sp = nfs4_get_state_owner(server, cred))) {
856 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
859 err = nfs4_recover_expired_lease(server);
861 goto out_put_state_owner;
862 /* Protect against reboot recovery - NOTE ORDER! */
863 down_read(&clp->cl_sem);
864 /* Protect against delegation recall */
865 down_read(&nfsi->rwsem);
866 delegation = NFS_I(inode)->delegation;
868 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
871 state = nfs4_get_open_state(inode, sp);
876 if ((state->state & open_flags) == open_flags) {
877 spin_lock(&inode->i_lock);
878 update_open_stateflags(state, open_flags);
879 spin_unlock(&inode->i_lock);
881 } else if (state->state != 0)
882 goto out_put_open_state;
885 err = _nfs4_do_access(inode, cred, open_flags);
888 goto out_put_open_state;
889 set_bit(NFS_DELEGATED_STATE, &state->flags);
890 update_open_stateid(state, &delegation->stateid, open_flags);
892 nfs4_put_state_owner(sp);
893 up_read(&nfsi->rwsem);
894 up_read(&clp->cl_sem);
898 nfs4_put_open_state(state);
900 up_read(&nfsi->rwsem);
901 up_read(&clp->cl_sem);
903 nfs_inode_return_delegation(inode);
905 nfs4_put_state_owner(sp);
909 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
911 struct nfs4_exception exception = { };
912 struct nfs4_state *res = ERR_PTR(-EIO);
916 err = _nfs4_open_delegated(inode, flags, cred, &res);
919 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
921 } while (exception.retry);
926 * Returns a referenced nfs4_state
928 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
930 struct nfs4_state_owner *sp;
931 struct nfs4_state *state = NULL;
932 struct nfs_server *server = NFS_SERVER(dir);
933 struct nfs4_client *clp = server->nfs4_state;
934 struct nfs4_opendata *opendata;
937 /* Protect against reboot recovery conflicts */
939 if (!(sp = nfs4_get_state_owner(server, cred))) {
940 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
943 status = nfs4_recover_expired_lease(server);
945 goto err_put_state_owner;
946 down_read(&clp->cl_sem);
948 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
949 if (opendata == NULL)
950 goto err_put_state_owner;
952 status = _nfs4_proc_open(opendata);
954 goto err_opendata_free;
957 state = nfs4_opendata_to_nfs4_state(opendata);
959 goto err_opendata_free;
960 if (opendata->o_res.delegation_type != 0)
961 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
962 nfs4_opendata_free(opendata);
963 nfs4_put_state_owner(sp);
964 up_read(&clp->cl_sem);
968 nfs4_opendata_free(opendata);
970 nfs4_put_state_owner(sp);
972 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
973 up_read(&clp->cl_sem);
979 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
981 struct nfs4_exception exception = { };
982 struct nfs4_state *res;
986 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
989 /* NOTE: BAD_SEQID means the server and client disagree about the
990 * book-keeping w.r.t. state-changing operations
991 * (OPEN/CLOSE/LOCK/LOCKU...)
992 * It is actually a sign of a bug on the client or on the server.
994 * If we receive a BAD_SEQID error in the particular case of
995 * doing an OPEN, we assume that nfs_increment_open_seqid() will
996 * have unhashed the old state_owner for us, and that we can
997 * therefore safely retry using a new one. We should still warn
1000 if (status == -NFS4ERR_BAD_SEQID) {
1001 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
1002 exception.retry = 1;
1006 * BAD_STATEID on OPEN means that the server cancelled our
1007 * state before it received the OPEN_CONFIRM.
1008 * Recover by retrying the request as per the discussion
1009 * on Page 181 of RFC3530.
1011 if (status == -NFS4ERR_BAD_STATEID) {
1012 exception.retry = 1;
1015 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1016 status, &exception));
1017 } while (exception.retry);
1021 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1022 struct nfs_fh *fhandle, struct iattr *sattr,
1023 struct nfs4_state *state)
1025 struct nfs_setattrargs arg = {
1029 .bitmask = server->attr_bitmask,
1031 struct nfs_setattrres res = {
1035 struct rpc_message msg = {
1036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1040 unsigned long timestamp = jiffies;
1043 nfs_fattr_init(fattr);
1045 if (state != NULL) {
1046 msg.rpc_cred = state->owner->so_cred;
1047 nfs4_copy_stateid(&arg.stateid, state, current->files);
1049 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1051 status = rpc_call_sync(server->client, &msg, 0);
1052 if (status == 0 && state != NULL)
1053 renew_lease(server, timestamp);
1057 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1058 struct nfs_fh *fhandle, struct iattr *sattr,
1059 struct nfs4_state *state)
1061 struct nfs4_exception exception = { };
1064 err = nfs4_handle_exception(server,
1065 _nfs4_do_setattr(server, fattr, fhandle, sattr,
1068 } while (exception.retry);
1072 struct nfs4_closedata {
1073 struct inode *inode;
1074 struct nfs4_state *state;
1075 struct nfs_closeargs arg;
1076 struct nfs_closeres res;
1077 struct nfs_fattr fattr;
1078 unsigned long timestamp;
1081 static void nfs4_free_closedata(void *data)
1083 struct nfs4_closedata *calldata = data;
1084 struct nfs4_state_owner *sp = calldata->state->owner;
1086 nfs4_put_open_state(calldata->state);
1087 nfs_free_seqid(calldata->arg.seqid);
1088 nfs4_put_state_owner(sp);
1092 static void nfs4_close_done(struct rpc_task *task, void *data)
1094 struct nfs4_closedata *calldata = data;
1095 struct nfs4_state *state = calldata->state;
1096 struct nfs_server *server = NFS_SERVER(calldata->inode);
1098 if (RPC_ASSASSINATED(task))
1100 /* hmm. we are done with the inode, and in the process of freeing
1101 * the state_owner. we keep this around to process errors
1103 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1104 switch (task->tk_status) {
1106 memcpy(&state->stateid, &calldata->res.stateid,
1107 sizeof(state->stateid));
1108 renew_lease(server, calldata->timestamp);
1110 case -NFS4ERR_STALE_STATEID:
1111 case -NFS4ERR_EXPIRED:
1112 nfs4_schedule_state_recovery(server->nfs4_state);
1115 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1116 rpc_restart_call(task);
1120 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1123 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1125 struct nfs4_closedata *calldata = data;
1126 struct nfs4_state *state = calldata->state;
1127 struct rpc_message msg = {
1128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1129 .rpc_argp = &calldata->arg,
1130 .rpc_resp = &calldata->res,
1131 .rpc_cred = state->owner->so_cred,
1133 int mode = 0, old_mode;
1135 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1137 /* Recalculate the new open mode in case someone reopened the file
1138 * while we were waiting in line to be scheduled.
1140 spin_lock(&state->owner->so_lock);
1141 spin_lock(&calldata->inode->i_lock);
1142 mode = old_mode = state->state;
1143 if (state->n_rdwr == 0) {
1144 if (state->n_rdonly == 0)
1145 mode &= ~FMODE_READ;
1146 if (state->n_wronly == 0)
1147 mode &= ~FMODE_WRITE;
1149 nfs4_state_set_mode_locked(state, mode);
1150 spin_unlock(&calldata->inode->i_lock);
1151 spin_unlock(&state->owner->so_lock);
1152 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1153 /* Note: exit _without_ calling nfs4_close_done */
1154 task->tk_action = NULL;
1157 nfs_fattr_init(calldata->res.fattr);
1159 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1160 calldata->arg.open_flags = mode;
1161 calldata->timestamp = jiffies;
1162 rpc_call_setup(task, &msg, 0);
1165 static const struct rpc_call_ops nfs4_close_ops = {
1166 .rpc_call_prepare = nfs4_close_prepare,
1167 .rpc_call_done = nfs4_close_done,
1168 .rpc_release = nfs4_free_closedata,
1172 * It is possible for data to be read/written from a mem-mapped file
1173 * after the sys_close call (which hits the vfs layer as a flush).
1174 * This means that we can't safely call nfsv4 close on a file until
1175 * the inode is cleared. This in turn means that we are not good
1176 * NFSv4 citizens - we do not indicate to the server to update the file's
1177 * share state even when we are done with one of the three share
1178 * stateid's in the inode.
1180 * NOTE: Caller must be holding the sp->so_owner semaphore!
1182 int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
1184 struct nfs_server *server = NFS_SERVER(inode);
1185 struct nfs4_closedata *calldata;
1186 int status = -ENOMEM;
1188 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1189 if (calldata == NULL)
1191 calldata->inode = inode;
1192 calldata->state = state;
1193 calldata->arg.fh = NFS_FH(inode);
1194 calldata->arg.stateid = &state->stateid;
1195 /* Serialization for the sequence id */
1196 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1197 if (calldata->arg.seqid == NULL)
1198 goto out_free_calldata;
1199 calldata->arg.bitmask = server->attr_bitmask;
1200 calldata->res.fattr = &calldata->fattr;
1201 calldata->res.server = server;
1203 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
1207 nfs_free_seqid(calldata->arg.seqid);
1214 static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1218 filp = lookup_instantiate_filp(nd, dentry, NULL);
1219 if (!IS_ERR(filp)) {
1220 struct nfs_open_context *ctx;
1221 ctx = (struct nfs_open_context *)filp->private_data;
1224 nfs4_close_state(state, nd->intent.open.flags);
1228 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1231 struct rpc_cred *cred;
1232 struct nfs4_state *state;
1235 if (nd->flags & LOOKUP_CREATE) {
1236 attr.ia_mode = nd->intent.open.create_mode;
1237 attr.ia_valid = ATTR_MODE;
1238 if (!IS_POSIXACL(dir))
1239 attr.ia_mode &= ~current->fs->umask;
1242 BUG_ON(nd->intent.open.flags & O_CREAT);
1245 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1247 return (struct dentry *)cred;
1248 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1250 if (IS_ERR(state)) {
1251 if (PTR_ERR(state) == -ENOENT)
1252 d_add(dentry, NULL);
1253 return (struct dentry *)state;
1255 res = d_add_unique(dentry, igrab(state->inode));
1258 nfs4_intent_set_file(nd, dentry, state);
1263 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1265 struct rpc_cred *cred;
1266 struct nfs4_state *state;
1268 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1270 return PTR_ERR(cred);
1271 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1273 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1275 if (IS_ERR(state)) {
1276 switch (PTR_ERR(state)) {
1282 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1285 if (dentry->d_inode == NULL)
1290 if (state->inode == dentry->d_inode) {
1291 nfs4_intent_set_file(nd, dentry, state);
1294 nfs4_close_state(state, openflags);
1301 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1303 struct nfs4_server_caps_res res = {};
1304 struct rpc_message msg = {
1305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1306 .rpc_argp = fhandle,
1311 status = rpc_call_sync(server->client, &msg, 0);
1313 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1314 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1315 server->caps |= NFS_CAP_ACLS;
1316 if (res.has_links != 0)
1317 server->caps |= NFS_CAP_HARDLINKS;
1318 if (res.has_symlinks != 0)
1319 server->caps |= NFS_CAP_SYMLINKS;
1320 server->acl_bitmask = res.acl_bitmask;
1325 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1327 struct nfs4_exception exception = { };
1330 err = nfs4_handle_exception(server,
1331 _nfs4_server_capabilities(server, fhandle),
1333 } while (exception.retry);
1337 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1338 struct nfs_fsinfo *info)
1340 struct nfs4_lookup_root_arg args = {
1341 .bitmask = nfs4_fattr_bitmap,
1343 struct nfs4_lookup_res res = {
1345 .fattr = info->fattr,
1348 struct rpc_message msg = {
1349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1353 nfs_fattr_init(info->fattr);
1354 return rpc_call_sync(server->client, &msg, 0);
1357 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1358 struct nfs_fsinfo *info)
1360 struct nfs4_exception exception = { };
1363 err = nfs4_handle_exception(server,
1364 _nfs4_lookup_root(server, fhandle, info),
1366 } while (exception.retry);
1370 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1371 struct nfs_fsinfo *info)
1373 struct nfs_fattr * fattr = info->fattr;
1376 struct nfs4_lookup_arg args = {
1379 .bitmask = nfs4_fattr_bitmap,
1381 struct nfs4_lookup_res res = {
1386 struct rpc_message msg = {
1387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1394 * Now we do a separate LOOKUP for each component of the mount path.
1395 * The LOOKUPs are done separately so that we can conveniently
1396 * catch an ERR_WRONGSEC if it occurs along the way...
1398 status = nfs4_lookup_root(server, fhandle, info);
1402 p = server->mnt_path;
1404 struct nfs4_exception exception = { };
1411 while (*p && (*p != '/'))
1416 nfs_fattr_init(fattr);
1417 status = nfs4_handle_exception(server,
1418 rpc_call_sync(server->client, &msg, 0),
1420 } while (exception.retry);
1423 if (status == -ENOENT) {
1424 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1425 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1430 status = nfs4_server_capabilities(server, fhandle);
1432 status = nfs4_do_fsinfo(server, fhandle, info);
1434 return nfs4_map_errors(status);
1437 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1439 struct nfs4_getattr_arg args = {
1441 .bitmask = server->attr_bitmask,
1443 struct nfs4_getattr_res res = {
1447 struct rpc_message msg = {
1448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1453 nfs_fattr_init(fattr);
1454 return rpc_call_sync(server->client, &msg, 0);
1457 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1459 struct nfs4_exception exception = { };
1462 err = nfs4_handle_exception(server,
1463 _nfs4_proc_getattr(server, fhandle, fattr),
1465 } while (exception.retry);
1470 * The file is not closed if it is opened due to the a request to change
1471 * the size of the file. The open call will not be needed once the
1472 * VFS layer lookup-intents are implemented.
1474 * Close is called when the inode is destroyed.
1475 * If we haven't opened the file for O_WRONLY, we
1476 * need to in the size_change case to obtain a stateid.
1479 * Because OPEN is always done by name in nfsv4, it is
1480 * possible that we opened a different file by the same
1481 * name. We can recognize this race condition, but we
1482 * can't do anything about it besides returning an error.
1484 * This will be fixed with VFS changes (lookup-intent).
1487 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1488 struct iattr *sattr)
1490 struct rpc_cred *cred;
1491 struct inode *inode = dentry->d_inode;
1492 struct nfs_open_context *ctx;
1493 struct nfs4_state *state = NULL;
1496 nfs_fattr_init(fattr);
1498 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1500 return PTR_ERR(cred);
1502 /* Search for an existing open(O_WRITE) file */
1503 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1507 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1508 NFS_FH(inode), sattr, state);
1510 nfs_setattr_update_inode(inode, sattr);
1512 put_nfs_open_context(ctx);
1517 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1518 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1521 struct nfs_server *server = NFS_SERVER(dir);
1522 struct nfs4_lookup_arg args = {
1523 .bitmask = server->attr_bitmask,
1524 .dir_fh = NFS_FH(dir),
1527 struct nfs4_lookup_res res = {
1532 struct rpc_message msg = {
1533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1538 nfs_fattr_init(fattr);
1540 dprintk("NFS call lookup %s\n", name->name);
1541 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1542 dprintk("NFS reply lookup: %d\n", status);
1546 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1548 struct nfs4_exception exception = { };
1551 err = nfs4_handle_exception(NFS_SERVER(dir),
1552 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1554 } while (exception.retry);
1558 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1560 struct nfs4_accessargs args = {
1561 .fh = NFS_FH(inode),
1563 struct nfs4_accessres res = { 0 };
1564 struct rpc_message msg = {
1565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1568 .rpc_cred = entry->cred,
1570 int mode = entry->mask;
1574 * Determine which access bits we want to ask for...
1576 if (mode & MAY_READ)
1577 args.access |= NFS4_ACCESS_READ;
1578 if (S_ISDIR(inode->i_mode)) {
1579 if (mode & MAY_WRITE)
1580 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1581 if (mode & MAY_EXEC)
1582 args.access |= NFS4_ACCESS_LOOKUP;
1584 if (mode & MAY_WRITE)
1585 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1586 if (mode & MAY_EXEC)
1587 args.access |= NFS4_ACCESS_EXECUTE;
1589 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1592 if (res.access & NFS4_ACCESS_READ)
1593 entry->mask |= MAY_READ;
1594 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1595 entry->mask |= MAY_WRITE;
1596 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1597 entry->mask |= MAY_EXEC;
1602 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1604 struct nfs4_exception exception = { };
1607 err = nfs4_handle_exception(NFS_SERVER(inode),
1608 _nfs4_proc_access(inode, entry),
1610 } while (exception.retry);
1615 * TODO: For the time being, we don't try to get any attributes
1616 * along with any of the zero-copy operations READ, READDIR,
1619 * In the case of the first three, we want to put the GETATTR
1620 * after the read-type operation -- this is because it is hard
1621 * to predict the length of a GETATTR response in v4, and thus
1622 * align the READ data correctly. This means that the GETATTR
1623 * may end up partially falling into the page cache, and we should
1624 * shift it into the 'tail' of the xdr_buf before processing.
1625 * To do this efficiently, we need to know the total length
1626 * of data received, which doesn't seem to be available outside
1629 * In the case of WRITE, we also want to put the GETATTR after
1630 * the operation -- in this case because we want to make sure
1631 * we get the post-operation mtime and size. This means that
1632 * we can't use xdr_encode_pages() as written: we need a variant
1633 * of it which would leave room in the 'tail' iovec.
1635 * Both of these changes to the XDR layer would in fact be quite
1636 * minor, but I decided to leave them for a subsequent patch.
1638 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1639 unsigned int pgbase, unsigned int pglen)
1641 struct nfs4_readlink args = {
1642 .fh = NFS_FH(inode),
1647 struct rpc_message msg = {
1648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1653 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1656 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1657 unsigned int pgbase, unsigned int pglen)
1659 struct nfs4_exception exception = { };
1662 err = nfs4_handle_exception(NFS_SERVER(inode),
1663 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1665 } while (exception.retry);
1669 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1671 int flags = rdata->flags;
1672 struct inode *inode = rdata->inode;
1673 struct nfs_fattr *fattr = rdata->res.fattr;
1674 struct nfs_server *server = NFS_SERVER(inode);
1675 struct rpc_message msg = {
1676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1677 .rpc_argp = &rdata->args,
1678 .rpc_resp = &rdata->res,
1679 .rpc_cred = rdata->cred,
1681 unsigned long timestamp = jiffies;
1684 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1685 (long long) rdata->args.offset);
1687 nfs_fattr_init(fattr);
1688 status = rpc_call_sync(server->client, &msg, flags);
1690 renew_lease(server, timestamp);
1691 dprintk("NFS reply read: %d\n", status);
1695 static int nfs4_proc_read(struct nfs_read_data *rdata)
1697 struct nfs4_exception exception = { };
1700 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1701 _nfs4_proc_read(rdata),
1703 } while (exception.retry);
1707 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1709 int rpcflags = wdata->flags;
1710 struct inode *inode = wdata->inode;
1711 struct nfs_fattr *fattr = wdata->res.fattr;
1712 struct nfs_server *server = NFS_SERVER(inode);
1713 struct rpc_message msg = {
1714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1715 .rpc_argp = &wdata->args,
1716 .rpc_resp = &wdata->res,
1717 .rpc_cred = wdata->cred,
1721 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1722 (long long) wdata->args.offset);
1724 wdata->args.bitmask = server->attr_bitmask;
1725 wdata->res.server = server;
1726 wdata->timestamp = jiffies;
1727 nfs_fattr_init(fattr);
1728 status = rpc_call_sync(server->client, &msg, rpcflags);
1729 dprintk("NFS reply write: %d\n", status);
1732 renew_lease(server, wdata->timestamp);
1733 nfs_post_op_update_inode(inode, fattr);
1734 return wdata->res.count;
1737 static int nfs4_proc_write(struct nfs_write_data *wdata)
1739 struct nfs4_exception exception = { };
1742 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1743 _nfs4_proc_write(wdata),
1745 } while (exception.retry);
1749 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1751 struct inode *inode = cdata->inode;
1752 struct nfs_fattr *fattr = cdata->res.fattr;
1753 struct nfs_server *server = NFS_SERVER(inode);
1754 struct rpc_message msg = {
1755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1756 .rpc_argp = &cdata->args,
1757 .rpc_resp = &cdata->res,
1758 .rpc_cred = cdata->cred,
1762 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1763 (long long) cdata->args.offset);
1765 cdata->args.bitmask = server->attr_bitmask;
1766 cdata->res.server = server;
1767 cdata->timestamp = jiffies;
1768 nfs_fattr_init(fattr);
1769 status = rpc_call_sync(server->client, &msg, 0);
1771 renew_lease(server, cdata->timestamp);
1772 dprintk("NFS reply commit: %d\n", status);
1774 nfs_post_op_update_inode(inode, fattr);
1778 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1780 struct nfs4_exception exception = { };
1783 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1784 _nfs4_proc_commit(cdata),
1786 } while (exception.retry);
1792 * We will need to arrange for the VFS layer to provide an atomic open.
1793 * Until then, this create/open method is prone to inefficiency and race
1794 * conditions due to the lookup, create, and open VFS calls from sys_open()
1795 * placed on the wire.
1797 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1798 * The file will be opened again in the subsequent VFS open call
1799 * (nfs4_proc_file_open).
1801 * The open for read will just hang around to be used by any process that
1802 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1806 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1807 int flags, struct nameidata *nd)
1809 struct nfs4_state *state;
1810 struct rpc_cred *cred;
1813 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1815 status = PTR_ERR(cred);
1818 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1820 if (IS_ERR(state)) {
1821 status = PTR_ERR(state);
1824 d_instantiate(dentry, igrab(state->inode));
1825 if (flags & O_EXCL) {
1826 struct nfs_fattr fattr;
1827 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1828 NFS_FH(state->inode), sattr, state);
1830 nfs_setattr_update_inode(state->inode, sattr);
1832 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1833 nfs4_intent_set_file(nd, dentry, state);
1835 nfs4_close_state(state, flags);
1840 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1842 struct nfs_server *server = NFS_SERVER(dir);
1843 struct nfs4_remove_arg args = {
1846 .bitmask = server->attr_bitmask,
1848 struct nfs_fattr dir_attr;
1849 struct nfs4_remove_res res = {
1851 .dir_attr = &dir_attr,
1853 struct rpc_message msg = {
1854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1860 nfs_fattr_init(res.dir_attr);
1861 status = rpc_call_sync(server->client, &msg, 0);
1863 update_changeattr(dir, &res.cinfo);
1864 nfs_post_op_update_inode(dir, res.dir_attr);
1869 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1871 struct nfs4_exception exception = { };
1874 err = nfs4_handle_exception(NFS_SERVER(dir),
1875 _nfs4_proc_remove(dir, name),
1877 } while (exception.retry);
1881 struct unlink_desc {
1882 struct nfs4_remove_arg args;
1883 struct nfs4_remove_res res;
1884 struct nfs_fattr dir_attr;
1887 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1890 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1891 struct unlink_desc *up;
1893 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1897 up->args.fh = NFS_FH(dir->d_inode);
1898 up->args.name = name;
1899 up->args.bitmask = server->attr_bitmask;
1900 up->res.server = server;
1901 up->res.dir_attr = &up->dir_attr;
1903 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1904 msg->rpc_argp = &up->args;
1905 msg->rpc_resp = &up->res;
1909 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1911 struct rpc_message *msg = &task->tk_msg;
1912 struct unlink_desc *up;
1914 if (msg->rpc_resp != NULL) {
1915 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1916 update_changeattr(dir->d_inode, &up->res.cinfo);
1917 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1919 msg->rpc_resp = NULL;
1920 msg->rpc_argp = NULL;
1925 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1926 struct inode *new_dir, struct qstr *new_name)
1928 struct nfs_server *server = NFS_SERVER(old_dir);
1929 struct nfs4_rename_arg arg = {
1930 .old_dir = NFS_FH(old_dir),
1931 .new_dir = NFS_FH(new_dir),
1932 .old_name = old_name,
1933 .new_name = new_name,
1934 .bitmask = server->attr_bitmask,
1936 struct nfs_fattr old_fattr, new_fattr;
1937 struct nfs4_rename_res res = {
1939 .old_fattr = &old_fattr,
1940 .new_fattr = &new_fattr,
1942 struct rpc_message msg = {
1943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1949 nfs_fattr_init(res.old_fattr);
1950 nfs_fattr_init(res.new_fattr);
1951 status = rpc_call_sync(server->client, &msg, 0);
1954 update_changeattr(old_dir, &res.old_cinfo);
1955 nfs_post_op_update_inode(old_dir, res.old_fattr);
1956 update_changeattr(new_dir, &res.new_cinfo);
1957 nfs_post_op_update_inode(new_dir, res.new_fattr);
1962 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1963 struct inode *new_dir, struct qstr *new_name)
1965 struct nfs4_exception exception = { };
1968 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1969 _nfs4_proc_rename(old_dir, old_name,
1972 } while (exception.retry);
1976 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1978 struct nfs_server *server = NFS_SERVER(inode);
1979 struct nfs4_link_arg arg = {
1980 .fh = NFS_FH(inode),
1981 .dir_fh = NFS_FH(dir),
1983 .bitmask = server->attr_bitmask,
1985 struct nfs_fattr fattr, dir_attr;
1986 struct nfs4_link_res res = {
1989 .dir_attr = &dir_attr,
1991 struct rpc_message msg = {
1992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1998 nfs_fattr_init(res.fattr);
1999 nfs_fattr_init(res.dir_attr);
2000 status = rpc_call_sync(server->client, &msg, 0);
2002 update_changeattr(dir, &res.cinfo);
2003 nfs_post_op_update_inode(dir, res.dir_attr);
2004 nfs_refresh_inode(inode, res.fattr);
2010 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2012 struct nfs4_exception exception = { };
2015 err = nfs4_handle_exception(NFS_SERVER(inode),
2016 _nfs4_proc_link(inode, dir, name),
2018 } while (exception.retry);
2022 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2023 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2024 struct nfs_fattr *fattr)
2026 struct nfs_server *server = NFS_SERVER(dir);
2027 struct nfs_fattr dir_fattr;
2028 struct nfs4_create_arg arg = {
2029 .dir_fh = NFS_FH(dir),
2034 .bitmask = server->attr_bitmask,
2036 struct nfs4_create_res res = {
2040 .dir_fattr = &dir_fattr,
2042 struct rpc_message msg = {
2043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2049 if (path->len > NFS4_MAXPATHLEN)
2050 return -ENAMETOOLONG;
2051 arg.u.symlink = path;
2052 nfs_fattr_init(fattr);
2053 nfs_fattr_init(&dir_fattr);
2055 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2057 update_changeattr(dir, &res.dir_cinfo);
2058 nfs_post_op_update_inode(dir, res.dir_fattr);
2062 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2063 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2064 struct nfs_fattr *fattr)
2066 struct nfs4_exception exception = { };
2069 err = nfs4_handle_exception(NFS_SERVER(dir),
2070 _nfs4_proc_symlink(dir, name, path, sattr,
2073 } while (exception.retry);
2077 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2078 struct iattr *sattr)
2080 struct nfs_server *server = NFS_SERVER(dir);
2081 struct nfs_fh fhandle;
2082 struct nfs_fattr fattr, dir_fattr;
2083 struct nfs4_create_arg arg = {
2084 .dir_fh = NFS_FH(dir),
2086 .name = &dentry->d_name,
2089 .bitmask = server->attr_bitmask,
2091 struct nfs4_create_res res = {
2095 .dir_fattr = &dir_fattr,
2097 struct rpc_message msg = {
2098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2104 nfs_fattr_init(&fattr);
2105 nfs_fattr_init(&dir_fattr);
2107 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2109 update_changeattr(dir, &res.dir_cinfo);
2110 nfs_post_op_update_inode(dir, res.dir_fattr);
2111 status = nfs_instantiate(dentry, &fhandle, &fattr);
2116 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2117 struct iattr *sattr)
2119 struct nfs4_exception exception = { };
2122 err = nfs4_handle_exception(NFS_SERVER(dir),
2123 _nfs4_proc_mkdir(dir, dentry, sattr),
2125 } while (exception.retry);
2129 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2130 u64 cookie, struct page *page, unsigned int count, int plus)
2132 struct inode *dir = dentry->d_inode;
2133 struct nfs4_readdir_arg args = {
2138 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2140 struct nfs4_readdir_res res;
2141 struct rpc_message msg = {
2142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2149 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2150 dentry->d_parent->d_name.name,
2151 dentry->d_name.name,
2152 (unsigned long long)cookie);
2154 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2155 res.pgbase = args.pgbase;
2156 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2158 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2160 dprintk("%s: returns %d\n", __FUNCTION__, status);
2164 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2165 u64 cookie, struct page *page, unsigned int count, int plus)
2167 struct nfs4_exception exception = { };
2170 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2171 _nfs4_proc_readdir(dentry, cred, cookie,
2174 } while (exception.retry);
2178 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2179 struct iattr *sattr, dev_t rdev)
2181 struct nfs_server *server = NFS_SERVER(dir);
2183 struct nfs_fattr fattr, dir_fattr;
2184 struct nfs4_create_arg arg = {
2185 .dir_fh = NFS_FH(dir),
2187 .name = &dentry->d_name,
2189 .bitmask = server->attr_bitmask,
2191 struct nfs4_create_res res = {
2195 .dir_fattr = &dir_fattr,
2197 struct rpc_message msg = {
2198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2203 int mode = sattr->ia_mode;
2205 nfs_fattr_init(&fattr);
2206 nfs_fattr_init(&dir_fattr);
2208 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2209 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2211 arg.ftype = NF4FIFO;
2212 else if (S_ISBLK(mode)) {
2214 arg.u.device.specdata1 = MAJOR(rdev);
2215 arg.u.device.specdata2 = MINOR(rdev);
2217 else if (S_ISCHR(mode)) {
2219 arg.u.device.specdata1 = MAJOR(rdev);
2220 arg.u.device.specdata2 = MINOR(rdev);
2223 arg.ftype = NF4SOCK;
2225 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2227 update_changeattr(dir, &res.dir_cinfo);
2228 nfs_post_op_update_inode(dir, res.dir_fattr);
2229 status = nfs_instantiate(dentry, &fh, &fattr);
2234 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2235 struct iattr *sattr, dev_t rdev)
2237 struct nfs4_exception exception = { };
2240 err = nfs4_handle_exception(NFS_SERVER(dir),
2241 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2243 } while (exception.retry);
2247 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2248 struct nfs_fsstat *fsstat)
2250 struct nfs4_statfs_arg args = {
2252 .bitmask = server->attr_bitmask,
2254 struct rpc_message msg = {
2255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2260 nfs_fattr_init(fsstat->fattr);
2261 return rpc_call_sync(server->client, &msg, 0);
2264 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2266 struct nfs4_exception exception = { };
2269 err = nfs4_handle_exception(server,
2270 _nfs4_proc_statfs(server, fhandle, fsstat),
2272 } while (exception.retry);
2276 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2277 struct nfs_fsinfo *fsinfo)
2279 struct nfs4_fsinfo_arg args = {
2281 .bitmask = server->attr_bitmask,
2283 struct rpc_message msg = {
2284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2289 return rpc_call_sync(server->client, &msg, 0);
2292 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2294 struct nfs4_exception exception = { };
2298 err = nfs4_handle_exception(server,
2299 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2301 } while (exception.retry);
2305 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2307 nfs_fattr_init(fsinfo->fattr);
2308 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2311 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2312 struct nfs_pathconf *pathconf)
2314 struct nfs4_pathconf_arg args = {
2316 .bitmask = server->attr_bitmask,
2318 struct rpc_message msg = {
2319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2321 .rpc_resp = pathconf,
2324 /* None of the pathconf attributes are mandatory to implement */
2325 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2326 memset(pathconf, 0, sizeof(*pathconf));
2330 nfs_fattr_init(pathconf->fattr);
2331 return rpc_call_sync(server->client, &msg, 0);
2334 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2335 struct nfs_pathconf *pathconf)
2337 struct nfs4_exception exception = { };
2341 err = nfs4_handle_exception(server,
2342 _nfs4_proc_pathconf(server, fhandle, pathconf),
2344 } while (exception.retry);
2348 static void nfs4_read_done(struct rpc_task *task, void *calldata)
2350 struct nfs_read_data *data = calldata;
2351 struct inode *inode = data->inode;
2353 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2354 rpc_restart_call(task);
2357 if (task->tk_status > 0)
2358 renew_lease(NFS_SERVER(inode), data->timestamp);
2359 /* Call back common NFS readpage processing */
2360 nfs_readpage_result(task, calldata);
2363 static const struct rpc_call_ops nfs4_read_ops = {
2364 .rpc_call_done = nfs4_read_done,
2365 .rpc_release = nfs_readdata_release,
2369 nfs4_proc_read_setup(struct nfs_read_data *data)
2371 struct rpc_task *task = &data->task;
2372 struct rpc_message msg = {
2373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2374 .rpc_argp = &data->args,
2375 .rpc_resp = &data->res,
2376 .rpc_cred = data->cred,
2378 struct inode *inode = data->inode;
2381 data->timestamp = jiffies;
2383 /* N.B. Do we need to test? Never called for swapfile inode */
2384 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2386 /* Finalize the task. */
2387 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
2388 rpc_call_setup(task, &msg, 0);
2391 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2393 struct inode *inode = data->inode;
2395 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2396 rpc_restart_call(task);
2399 if (task->tk_status >= 0) {
2400 renew_lease(NFS_SERVER(inode), data->timestamp);
2401 nfs_post_op_update_inode(inode, data->res.fattr);
2406 static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2408 struct rpc_message msg = {
2409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2410 .rpc_argp = &data->args,
2411 .rpc_resp = &data->res,
2412 .rpc_cred = data->cred,
2414 struct inode *inode = data->inode;
2415 struct nfs_server *server = NFS_SERVER(inode);
2418 if (how & FLUSH_STABLE) {
2419 if (!NFS_I(inode)->ncommit)
2420 stable = NFS_FILE_SYNC;
2422 stable = NFS_DATA_SYNC;
2424 stable = NFS_UNSTABLE;
2425 data->args.stable = stable;
2426 data->args.bitmask = server->attr_bitmask;
2427 data->res.server = server;
2429 data->timestamp = jiffies;
2431 /* Finalize the task. */
2432 rpc_call_setup(&data->task, &msg, 0);
2435 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2437 struct inode *inode = data->inode;
2439 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2440 rpc_restart_call(task);
2443 if (task->tk_status >= 0)
2444 nfs_post_op_update_inode(inode, data->res.fattr);
2448 static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2450 struct rpc_message msg = {
2451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2452 .rpc_argp = &data->args,
2453 .rpc_resp = &data->res,
2454 .rpc_cred = data->cred,
2456 struct nfs_server *server = NFS_SERVER(data->inode);
2458 data->args.bitmask = server->attr_bitmask;
2459 data->res.server = server;
2461 rpc_call_setup(&data->task, &msg, 0);
2465 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2466 * standalone procedure for queueing an asynchronous RENEW.
2468 static void nfs4_renew_done(struct rpc_task *task, void *data)
2470 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2471 unsigned long timestamp = (unsigned long)data;
2473 if (task->tk_status < 0) {
2474 switch (task->tk_status) {
2475 case -NFS4ERR_STALE_CLIENTID:
2476 case -NFS4ERR_EXPIRED:
2477 case -NFS4ERR_CB_PATH_DOWN:
2478 nfs4_schedule_state_recovery(clp);
2482 spin_lock(&clp->cl_lock);
2483 if (time_before(clp->cl_last_renewal,timestamp))
2484 clp->cl_last_renewal = timestamp;
2485 spin_unlock(&clp->cl_lock);
2488 static const struct rpc_call_ops nfs4_renew_ops = {
2489 .rpc_call_done = nfs4_renew_done,
2492 int nfs4_proc_async_renew(struct nfs4_client *clp, struct rpc_cred *cred)
2494 struct rpc_message msg = {
2495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2500 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2501 &nfs4_renew_ops, (void *)jiffies);
2504 int nfs4_proc_renew(struct nfs4_client *clp, struct rpc_cred *cred)
2506 struct rpc_message msg = {
2507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2511 unsigned long now = jiffies;
2514 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2517 spin_lock(&clp->cl_lock);
2518 if (time_before(clp->cl_last_renewal,now))
2519 clp->cl_last_renewal = now;
2520 spin_unlock(&clp->cl_lock);
2524 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2526 return (server->caps & NFS_CAP_ACLS)
2527 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2528 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2531 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2532 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2535 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2537 static void buf_to_pages(const void *buf, size_t buflen,
2538 struct page **pages, unsigned int *pgbase)
2540 const void *p = buf;
2542 *pgbase = offset_in_page(buf);
2544 while (p < buf + buflen) {
2545 *(pages++) = virt_to_page(p);
2546 p += PAGE_CACHE_SIZE;
2550 struct nfs4_cached_acl {
2556 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2558 struct nfs_inode *nfsi = NFS_I(inode);
2560 spin_lock(&inode->i_lock);
2561 kfree(nfsi->nfs4_acl);
2562 nfsi->nfs4_acl = acl;
2563 spin_unlock(&inode->i_lock);
2566 static void nfs4_zap_acl_attr(struct inode *inode)
2568 nfs4_set_cached_acl(inode, NULL);
2571 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2573 struct nfs_inode *nfsi = NFS_I(inode);
2574 struct nfs4_cached_acl *acl;
2577 spin_lock(&inode->i_lock);
2578 acl = nfsi->nfs4_acl;
2581 if (buf == NULL) /* user is just asking for length */
2583 if (acl->cached == 0)
2585 ret = -ERANGE; /* see getxattr(2) man page */
2586 if (acl->len > buflen)
2588 memcpy(buf, acl->data, acl->len);
2592 spin_unlock(&inode->i_lock);
2596 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2598 struct nfs4_cached_acl *acl;
2600 if (buf && acl_len <= PAGE_SIZE) {
2601 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2605 memcpy(acl->data, buf, acl_len);
2607 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2614 nfs4_set_cached_acl(inode, acl);
2617 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2619 struct page *pages[NFS4ACL_MAXPAGES];
2620 struct nfs_getaclargs args = {
2621 .fh = NFS_FH(inode),
2625 size_t resp_len = buflen;
2627 struct rpc_message msg = {
2628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2630 .rpc_resp = &resp_len,
2632 struct page *localpage = NULL;
2635 if (buflen < PAGE_SIZE) {
2636 /* As long as we're doing a round trip to the server anyway,
2637 * let's be prepared for a page of acl data. */
2638 localpage = alloc_page(GFP_KERNEL);
2639 resp_buf = page_address(localpage);
2640 if (localpage == NULL)
2642 args.acl_pages[0] = localpage;
2643 args.acl_pgbase = 0;
2644 resp_len = args.acl_len = PAGE_SIZE;
2647 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2649 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2652 if (resp_len > args.acl_len)
2653 nfs4_write_cached_acl(inode, NULL, resp_len);
2655 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2658 if (resp_len > buflen)
2661 memcpy(buf, resp_buf, resp_len);
2666 __free_page(localpage);
2670 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2672 struct nfs_server *server = NFS_SERVER(inode);
2675 if (!nfs4_server_supports_acls(server))
2677 ret = nfs_revalidate_inode(server, inode);
2680 ret = nfs4_read_cached_acl(inode, buf, buflen);
2683 return nfs4_get_acl_uncached(inode, buf, buflen);
2686 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2688 struct nfs_server *server = NFS_SERVER(inode);
2689 struct page *pages[NFS4ACL_MAXPAGES];
2690 struct nfs_setaclargs arg = {
2691 .fh = NFS_FH(inode),
2695 struct rpc_message msg = {
2696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2702 if (!nfs4_server_supports_acls(server))
2704 nfs_inode_return_delegation(inode);
2705 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2706 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2708 nfs4_write_cached_acl(inode, buf, buflen);
2713 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2715 struct nfs4_client *clp = server->nfs4_state;
2717 if (!clp || task->tk_status >= 0)
2719 switch(task->tk_status) {
2720 case -NFS4ERR_STALE_CLIENTID:
2721 case -NFS4ERR_STALE_STATEID:
2722 case -NFS4ERR_EXPIRED:
2723 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2724 nfs4_schedule_state_recovery(clp);
2725 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
2726 rpc_wake_up_task(task);
2727 task->tk_status = 0;
2729 case -NFS4ERR_DELAY:
2730 nfs_inc_server_stats((struct nfs_server *) server,
2732 case -NFS4ERR_GRACE:
2733 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2734 task->tk_status = 0;
2736 case -NFS4ERR_OLD_STATEID:
2737 task->tk_status = 0;
2740 task->tk_status = nfs4_map_errors(task->tk_status);
2744 static int nfs4_wait_bit_interruptible(void *word)
2746 if (signal_pending(current))
2747 return -ERESTARTSYS;
2752 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2759 rpc_clnt_sigmask(clnt, &oldset);
2760 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2761 nfs4_wait_bit_interruptible,
2762 TASK_INTERRUPTIBLE);
2763 rpc_clnt_sigunmask(clnt, &oldset);
2767 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2775 *timeout = NFS4_POLL_RETRY_MIN;
2776 if (*timeout > NFS4_POLL_RETRY_MAX)
2777 *timeout = NFS4_POLL_RETRY_MAX;
2778 rpc_clnt_sigmask(clnt, &oldset);
2779 if (clnt->cl_intr) {
2780 schedule_timeout_interruptible(*timeout);
2784 schedule_timeout_uninterruptible(*timeout);
2785 rpc_clnt_sigunmask(clnt, &oldset);
2790 /* This is the error handling routine for processes that are allowed
2793 int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2795 struct nfs4_client *clp = server->nfs4_state;
2796 int ret = errorcode;
2798 exception->retry = 0;
2802 case -NFS4ERR_STALE_CLIENTID:
2803 case -NFS4ERR_STALE_STATEID:
2804 case -NFS4ERR_EXPIRED:
2805 nfs4_schedule_state_recovery(clp);
2806 ret = nfs4_wait_clnt_recover(server->client, clp);
2808 exception->retry = 1;
2810 case -NFS4ERR_GRACE:
2811 case -NFS4ERR_DELAY:
2812 ret = nfs4_delay(server->client, &exception->timeout);
2815 case -NFS4ERR_OLD_STATEID:
2816 exception->retry = 1;
2818 /* We failed to handle the error */
2819 return nfs4_map_errors(ret);
2822 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
2824 nfs4_verifier sc_verifier;
2825 struct nfs4_setclientid setclientid = {
2826 .sc_verifier = &sc_verifier,
2829 struct rpc_message msg = {
2830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2831 .rpc_argp = &setclientid,
2839 p = (u32*)sc_verifier.data;
2840 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2841 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2844 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2845 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2846 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2847 cred->cr_ops->cr_name,
2848 clp->cl_id_uniquifier);
2849 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2850 sizeof(setclientid.sc_netid), "tcp");
2851 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2852 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2853 clp->cl_ipaddr, port >> 8, port & 255);
2855 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2856 if (status != -NFS4ERR_CLID_INUSE)
2861 ssleep(clp->cl_lease_time + 1);
2863 if (++clp->cl_id_uniquifier == 0)
2870 nfs4_proc_setclientid_confirm(struct nfs4_client *clp, struct rpc_cred *cred)
2872 struct nfs_fsinfo fsinfo;
2873 struct rpc_message msg = {
2874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2876 .rpc_resp = &fsinfo,
2883 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2885 spin_lock(&clp->cl_lock);
2886 clp->cl_lease_time = fsinfo.lease_time * HZ;
2887 clp->cl_last_renewal = now;
2888 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2889 spin_unlock(&clp->cl_lock);
2894 struct nfs4_delegreturndata {
2895 struct nfs4_delegreturnargs args;
2896 struct nfs4_delegreturnres res;
2898 nfs4_stateid stateid;
2899 struct rpc_cred *cred;
2900 unsigned long timestamp;
2901 struct nfs_fattr fattr;
2905 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
2907 struct nfs4_delegreturndata *data = calldata;
2908 struct rpc_message msg = {
2909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2910 .rpc_argp = &data->args,
2911 .rpc_resp = &data->res,
2912 .rpc_cred = data->cred,
2914 nfs_fattr_init(data->res.fattr);
2915 rpc_call_setup(task, &msg, 0);
2918 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2920 struct nfs4_delegreturndata *data = calldata;
2921 data->rpc_status = task->tk_status;
2922 if (data->rpc_status == 0)
2923 renew_lease(data->res.server, data->timestamp);
2926 static void nfs4_delegreturn_release(void *calldata)
2928 struct nfs4_delegreturndata *data = calldata;
2930 put_rpccred(data->cred);
2934 static const struct rpc_call_ops nfs4_delegreturn_ops = {
2935 .rpc_call_prepare = nfs4_delegreturn_prepare,
2936 .rpc_call_done = nfs4_delegreturn_done,
2937 .rpc_release = nfs4_delegreturn_release,
2940 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2942 struct nfs4_delegreturndata *data;
2943 struct nfs_server *server = NFS_SERVER(inode);
2944 struct rpc_task *task;
2947 data = kmalloc(sizeof(*data), GFP_KERNEL);
2950 data->args.fhandle = &data->fh;
2951 data->args.stateid = &data->stateid;
2952 data->args.bitmask = server->attr_bitmask;
2953 nfs_copy_fh(&data->fh, NFS_FH(inode));
2954 memcpy(&data->stateid, stateid, sizeof(data->stateid));
2955 data->res.fattr = &data->fattr;
2956 data->res.server = server;
2957 data->cred = get_rpccred(cred);
2958 data->timestamp = jiffies;
2959 data->rpc_status = 0;
2961 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
2963 nfs4_delegreturn_release(data);
2964 return PTR_ERR(task);
2966 status = nfs4_wait_for_completion_rpc_task(task);
2968 status = data->rpc_status;
2970 nfs_post_op_update_inode(inode, &data->fattr);
2972 rpc_release_task(task);
2976 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2978 struct nfs_server *server = NFS_SERVER(inode);
2979 struct nfs4_exception exception = { };
2982 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2984 case -NFS4ERR_STALE_STATEID:
2985 case -NFS4ERR_EXPIRED:
2986 nfs4_schedule_state_recovery(server->nfs4_state);
2990 err = nfs4_handle_exception(server, err, &exception);
2991 } while (exception.retry);
2995 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2996 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2999 * sleep, with exponential backoff, and retry the LOCK operation.
3001 static unsigned long
3002 nfs4_set_lock_task_retry(unsigned long timeout)
3004 schedule_timeout_interruptible(timeout);
3006 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3007 return NFS4_LOCK_MAXTIMEOUT;
3011 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3013 struct inode *inode = state->inode;
3014 struct nfs_server *server = NFS_SERVER(inode);
3015 struct nfs4_client *clp = server->nfs4_state;
3016 struct nfs_lockt_args arg = {
3017 .fh = NFS_FH(inode),
3020 struct nfs_lockt_res res = {
3023 struct rpc_message msg = {
3024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3027 .rpc_cred = state->owner->so_cred,
3029 struct nfs4_lock_state *lsp;
3032 down_read(&clp->cl_sem);
3033 arg.lock_owner.clientid = clp->cl_clientid;
3034 status = nfs4_set_lock_state(state, request);
3037 lsp = request->fl_u.nfs4_fl.owner;
3038 arg.lock_owner.id = lsp->ls_id;
3039 status = rpc_call_sync(server->client, &msg, 0);
3042 request->fl_type = F_UNLCK;
3044 case -NFS4ERR_DENIED:
3048 up_read(&clp->cl_sem);
3052 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3054 struct nfs4_exception exception = { };
3058 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3059 _nfs4_proc_getlk(state, cmd, request),
3061 } while (exception.retry);
3065 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3068 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3070 res = posix_lock_file_wait(file, fl);
3073 res = flock_lock_file_wait(file, fl);
3079 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
3084 struct nfs4_unlockdata {
3085 struct nfs_locku_args arg;
3086 struct nfs_locku_res res;
3087 struct nfs4_lock_state *lsp;
3088 struct nfs_open_context *ctx;
3089 struct file_lock fl;
3090 const struct nfs_server *server;
3091 unsigned long timestamp;
3094 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3095 struct nfs_open_context *ctx,
3096 struct nfs4_lock_state *lsp,
3097 struct nfs_seqid *seqid)
3099 struct nfs4_unlockdata *p;
3100 struct inode *inode = lsp->ls_state->inode;
3102 p = kmalloc(sizeof(*p), GFP_KERNEL);
3105 p->arg.fh = NFS_FH(inode);
3107 p->arg.seqid = seqid;
3108 p->arg.stateid = &lsp->ls_stateid;
3110 atomic_inc(&lsp->ls_count);
3111 /* Ensure we don't close file until we're done freeing locks! */
3112 p->ctx = get_nfs_open_context(ctx);
3113 memcpy(&p->fl, fl, sizeof(p->fl));
3114 p->server = NFS_SERVER(inode);
3118 static void nfs4_locku_release_calldata(void *data)
3120 struct nfs4_unlockdata *calldata = data;
3121 nfs_free_seqid(calldata->arg.seqid);
3122 nfs4_put_lock_state(calldata->lsp);
3123 put_nfs_open_context(calldata->ctx);
3127 static void nfs4_locku_done(struct rpc_task *task, void *data)
3129 struct nfs4_unlockdata *calldata = data;
3131 if (RPC_ASSASSINATED(task))
3133 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
3134 switch (task->tk_status) {
3136 memcpy(calldata->lsp->ls_stateid.data,
3137 calldata->res.stateid.data,
3138 sizeof(calldata->lsp->ls_stateid.data));
3139 renew_lease(calldata->server, calldata->timestamp);
3141 case -NFS4ERR_STALE_STATEID:
3142 case -NFS4ERR_EXPIRED:
3143 nfs4_schedule_state_recovery(calldata->server->nfs4_state);
3146 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN) {
3147 rpc_restart_call(task);
3152 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3154 struct nfs4_unlockdata *calldata = data;
3155 struct rpc_message msg = {
3156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3157 .rpc_argp = &calldata->arg,
3158 .rpc_resp = &calldata->res,
3159 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
3162 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3164 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3165 /* Note: exit _without_ running nfs4_locku_done */
3166 task->tk_action = NULL;
3169 calldata->timestamp = jiffies;
3170 rpc_call_setup(task, &msg, 0);
3173 static const struct rpc_call_ops nfs4_locku_ops = {
3174 .rpc_call_prepare = nfs4_locku_prepare,
3175 .rpc_call_done = nfs4_locku_done,
3176 .rpc_release = nfs4_locku_release_calldata,
3179 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3180 struct nfs_open_context *ctx,
3181 struct nfs4_lock_state *lsp,
3182 struct nfs_seqid *seqid)
3184 struct nfs4_unlockdata *data;
3185 struct rpc_task *task;
3187 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3189 nfs_free_seqid(seqid);
3190 return ERR_PTR(-ENOMEM);
3193 /* Unlock _before_ we do the RPC call */
3194 do_vfs_lock(fl->fl_file, fl);
3195 task = rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
3197 nfs4_locku_release_calldata(data);
3201 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3203 struct nfs_seqid *seqid;
3204 struct nfs4_lock_state *lsp;
3205 struct rpc_task *task;
3208 /* Is this a delegated lock? */
3209 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3211 /* Is this open_owner holding any locks on the server? */
3212 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
3215 status = nfs4_set_lock_state(state, request);
3218 lsp = request->fl_u.nfs4_fl.owner;
3220 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3223 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3224 status = PTR_ERR(task);
3227 status = nfs4_wait_for_completion_rpc_task(task);
3228 rpc_release_task(task);
3231 do_vfs_lock(request->fl_file, request);
3235 struct nfs4_lockdata {
3236 struct nfs_lock_args arg;
3237 struct nfs_lock_res res;
3238 struct nfs4_lock_state *lsp;
3239 struct nfs_open_context *ctx;
3240 struct file_lock fl;
3241 unsigned long timestamp;
3246 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3247 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3249 struct nfs4_lockdata *p;
3250 struct inode *inode = lsp->ls_state->inode;
3251 struct nfs_server *server = NFS_SERVER(inode);
3253 p = kzalloc(sizeof(*p), GFP_KERNEL);
3257 p->arg.fh = NFS_FH(inode);
3259 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3260 if (p->arg.lock_seqid == NULL)
3262 p->arg.lock_stateid = &lsp->ls_stateid;
3263 p->arg.lock_owner.clientid = server->nfs4_state->cl_clientid;
3264 p->arg.lock_owner.id = lsp->ls_id;
3266 atomic_inc(&lsp->ls_count);
3267 p->ctx = get_nfs_open_context(ctx);
3268 memcpy(&p->fl, fl, sizeof(p->fl));
3275 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3277 struct nfs4_lockdata *data = calldata;
3278 struct nfs4_state *state = data->lsp->ls_state;
3279 struct nfs4_state_owner *sp = state->owner;
3280 struct rpc_message msg = {
3281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3282 .rpc_argp = &data->arg,
3283 .rpc_resp = &data->res,
3284 .rpc_cred = sp->so_cred,
3287 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3289 dprintk("%s: begin!\n", __FUNCTION__);
3290 /* Do we need to do an open_to_lock_owner? */
3291 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3292 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3293 if (data->arg.open_seqid == NULL) {
3294 data->rpc_status = -ENOMEM;
3295 task->tk_action = NULL;
3298 data->arg.open_stateid = &state->stateid;
3299 data->arg.new_lock_owner = 1;
3301 data->timestamp = jiffies;
3302 rpc_call_setup(task, &msg, 0);
3304 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3307 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3309 struct nfs4_lockdata *data = calldata;
3311 dprintk("%s: begin!\n", __FUNCTION__);
3313 data->rpc_status = task->tk_status;
3314 if (RPC_ASSASSINATED(task))
3316 if (data->arg.new_lock_owner != 0) {
3317 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3318 if (data->rpc_status == 0)
3319 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3323 if (data->rpc_status == 0) {
3324 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3325 sizeof(data->lsp->ls_stateid.data));
3326 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3327 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
3329 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3331 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3334 static void nfs4_lock_release(void *calldata)
3336 struct nfs4_lockdata *data = calldata;
3338 dprintk("%s: begin!\n", __FUNCTION__);
3339 if (data->arg.open_seqid != NULL)
3340 nfs_free_seqid(data->arg.open_seqid);
3341 if (data->cancelled != 0) {
3342 struct rpc_task *task;
3343 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3344 data->arg.lock_seqid);
3346 rpc_release_task(task);
3347 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3349 nfs_free_seqid(data->arg.lock_seqid);
3350 nfs4_put_lock_state(data->lsp);
3351 put_nfs_open_context(data->ctx);
3353 dprintk("%s: done!\n", __FUNCTION__);
3356 static const struct rpc_call_ops nfs4_lock_ops = {
3357 .rpc_call_prepare = nfs4_lock_prepare,
3358 .rpc_call_done = nfs4_lock_done,
3359 .rpc_release = nfs4_lock_release,
3362 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3364 struct nfs4_lockdata *data;
3365 struct rpc_task *task;
3368 dprintk("%s: begin!\n", __FUNCTION__);
3369 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3370 fl->fl_u.nfs4_fl.owner);
3374 data->arg.block = 1;
3376 data->arg.reclaim = 1;
3377 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3378 &nfs4_lock_ops, data);
3380 nfs4_lock_release(data);
3381 return PTR_ERR(task);
3383 ret = nfs4_wait_for_completion_rpc_task(task);
3385 ret = data->rpc_status;
3386 if (ret == -NFS4ERR_DENIED)
3389 data->cancelled = 1;
3390 rpc_release_task(task);
3391 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3395 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3397 struct nfs_server *server = NFS_SERVER(state->inode);
3398 struct nfs4_exception exception = { };
3401 /* Cache the lock if possible... */
3402 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3405 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3406 if (err != -NFS4ERR_DELAY)
3408 nfs4_handle_exception(server, err, &exception);
3409 } while (exception.retry);
3413 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3415 struct nfs_server *server = NFS_SERVER(state->inode);
3416 struct nfs4_exception exception = { };
3419 err = nfs4_set_lock_state(state, request);
3423 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3424 if (err != -NFS4ERR_DELAY)
3426 nfs4_handle_exception(server, err, &exception);
3427 } while (exception.retry);
3431 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3433 struct nfs4_client *clp = state->owner->so_client;
3436 /* Is this a delegated open? */
3437 if (NFS_I(state->inode)->delegation_state != 0) {
3438 /* Yes: cache locks! */
3439 status = do_vfs_lock(request->fl_file, request);
3440 /* ...but avoid races with delegation recall... */
3441 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
3444 down_read(&clp->cl_sem);
3445 status = nfs4_set_lock_state(state, request);
3448 status = _nfs4_do_setlk(state, cmd, request, 0);
3451 /* Note: we always want to sleep here! */
3452 request->fl_flags |= FL_SLEEP;
3453 if (do_vfs_lock(request->fl_file, request) < 0)
3454 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3456 up_read(&clp->cl_sem);
3460 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3462 struct nfs4_exception exception = { };
3466 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3467 _nfs4_proc_setlk(state, cmd, request),
3469 } while (exception.retry);
3474 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3476 struct nfs_open_context *ctx;
3477 struct nfs4_state *state;
3478 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3481 /* verify open state */
3482 ctx = (struct nfs_open_context *)filp->private_data;
3485 if (request->fl_start < 0 || request->fl_end < 0)
3489 return nfs4_proc_getlk(state, F_GETLK, request);
3491 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3494 if (request->fl_type == F_UNLCK)
3495 return nfs4_proc_unlck(state, cmd, request);
3498 status = nfs4_proc_setlk(state, cmd, request);
3499 if ((status != -EAGAIN) || IS_SETLK(cmd))
3501 timeout = nfs4_set_lock_task_retry(timeout);
3502 status = -ERESTARTSYS;
3505 } while(status < 0);
3509 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3511 struct nfs_server *server = NFS_SERVER(state->inode);
3512 struct nfs4_exception exception = { };
3515 err = nfs4_set_lock_state(state, fl);
3519 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3520 if (err != -NFS4ERR_DELAY)
3522 err = nfs4_handle_exception(server, err, &exception);
3523 } while (exception.retry);
3528 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3530 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3531 size_t buflen, int flags)
3533 struct inode *inode = dentry->d_inode;
3535 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3538 if (!S_ISREG(inode->i_mode) &&
3539 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3542 return nfs4_proc_set_acl(inode, buf, buflen);
3545 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3546 * and that's what we'll do for e.g. user attributes that haven't been set.
3547 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3548 * attributes in kernel-managed attribute namespaces. */
3549 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3552 struct inode *inode = dentry->d_inode;
3554 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3557 return nfs4_proc_get_acl(inode, buf, buflen);
3560 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3562 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3564 if (buf && buflen < len)
3567 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3571 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3572 .recover_open = nfs4_open_reclaim,
3573 .recover_lock = nfs4_lock_reclaim,
3576 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3577 .recover_open = nfs4_open_expired,
3578 .recover_lock = nfs4_lock_expired,
3581 static struct inode_operations nfs4_file_inode_operations = {
3582 .permission = nfs_permission,
3583 .getattr = nfs_getattr,
3584 .setattr = nfs_setattr,
3585 .getxattr = nfs4_getxattr,
3586 .setxattr = nfs4_setxattr,
3587 .listxattr = nfs4_listxattr,
3590 struct nfs_rpc_ops nfs_v4_clientops = {
3591 .version = 4, /* protocol version */
3592 .dentry_ops = &nfs4_dentry_operations,
3593 .dir_inode_ops = &nfs4_dir_inode_operations,
3594 .file_inode_ops = &nfs4_file_inode_operations,
3595 .getroot = nfs4_proc_get_root,
3596 .getattr = nfs4_proc_getattr,
3597 .setattr = nfs4_proc_setattr,
3598 .lookup = nfs4_proc_lookup,
3599 .access = nfs4_proc_access,
3600 .readlink = nfs4_proc_readlink,
3601 .read = nfs4_proc_read,
3602 .write = nfs4_proc_write,
3603 .commit = nfs4_proc_commit,
3604 .create = nfs4_proc_create,
3605 .remove = nfs4_proc_remove,
3606 .unlink_setup = nfs4_proc_unlink_setup,
3607 .unlink_done = nfs4_proc_unlink_done,
3608 .rename = nfs4_proc_rename,
3609 .link = nfs4_proc_link,
3610 .symlink = nfs4_proc_symlink,
3611 .mkdir = nfs4_proc_mkdir,
3612 .rmdir = nfs4_proc_remove,
3613 .readdir = nfs4_proc_readdir,
3614 .mknod = nfs4_proc_mknod,
3615 .statfs = nfs4_proc_statfs,
3616 .fsinfo = nfs4_proc_fsinfo,
3617 .pathconf = nfs4_proc_pathconf,
3618 .decode_dirent = nfs4_decode_dirent,
3619 .read_setup = nfs4_proc_read_setup,
3620 .write_setup = nfs4_proc_write_setup,
3621 .write_done = nfs4_write_done,
3622 .commit_setup = nfs4_proc_commit_setup,
3623 .commit_done = nfs4_commit_done,
3624 .file_open = nfs_open,
3625 .file_release = nfs_release,
3626 .lock = nfs4_proc_lock,
3627 .clear_acl_cache = nfs4_zap_acl_attr,