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>
52 #include "delegation.h"
54 #define NFSDBG_FACILITY NFSDBG_PROC
56 #define NFS4_POLL_RETRY_MIN (1*HZ)
57 #define NFS4_POLL_RETRY_MAX (15*HZ)
59 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
60 static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *);
61 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
62 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
63 extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
64 extern struct rpc_procinfo nfs4_procedures[];
66 /* Prevent leaks of NFSv4 errors into userland */
67 int nfs4_map_errors(int err)
70 dprintk("%s could not handle NFSv4 error %d\n",
78 * This is our standard bitmap for GETATTR requests.
80 const u32 nfs4_fattr_bitmap[2] = {
85 | FATTR4_WORD0_FILEID,
87 | FATTR4_WORD1_NUMLINKS
89 | FATTR4_WORD1_OWNER_GROUP
91 | FATTR4_WORD1_SPACE_USED
92 | FATTR4_WORD1_TIME_ACCESS
93 | FATTR4_WORD1_TIME_METADATA
94 | FATTR4_WORD1_TIME_MODIFY
97 const u32 nfs4_statfs_bitmap[2] = {
98 FATTR4_WORD0_FILES_AVAIL
99 | FATTR4_WORD0_FILES_FREE
100 | FATTR4_WORD0_FILES_TOTAL,
101 FATTR4_WORD1_SPACE_AVAIL
102 | FATTR4_WORD1_SPACE_FREE
103 | FATTR4_WORD1_SPACE_TOTAL
106 const u32 nfs4_pathconf_bitmap[2] = {
108 | FATTR4_WORD0_MAXNAME,
112 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
113 | FATTR4_WORD0_MAXREAD
114 | FATTR4_WORD0_MAXWRITE
115 | FATTR4_WORD0_LEASE_TIME,
119 static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
120 struct nfs4_readdir_arg *readdir)
124 BUG_ON(readdir->count < 80);
126 readdir->cookie = cookie;
127 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
132 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
137 * NFSv4 servers do not return entries for '.' and '..'
138 * Therefore, we fake these entries here. We let '.'
139 * have cookie 0 and '..' have cookie 1. Note that
140 * when talking to the server, we always send cookie 0
143 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
146 *p++ = xdr_one; /* next */
147 *p++ = xdr_zero; /* cookie, first word */
148 *p++ = xdr_one; /* cookie, second word */
149 *p++ = xdr_one; /* entry len */
150 memcpy(p, ".\0\0\0", 4); /* entry */
152 *p++ = xdr_one; /* bitmap length */
153 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
154 *p++ = htonl(8); /* attribute buffer length */
155 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
158 *p++ = xdr_one; /* next */
159 *p++ = xdr_zero; /* cookie, first word */
160 *p++ = xdr_two; /* cookie, second word */
161 *p++ = xdr_two; /* entry len */
162 memcpy(p, "..\0\0", 4); /* entry */
164 *p++ = xdr_one; /* bitmap length */
165 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
166 *p++ = htonl(8); /* attribute buffer length */
167 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
169 readdir->pgbase = (char *)p - (char *)start;
170 readdir->count -= readdir->pgbase;
171 kunmap_atomic(start, KM_USER0);
175 renew_lease(struct nfs_server *server, unsigned long timestamp)
177 struct nfs4_client *clp = server->nfs4_state;
178 spin_lock(&clp->cl_lock);
179 if (time_before(clp->cl_last_renewal,timestamp))
180 clp->cl_last_renewal = timestamp;
181 spin_unlock(&clp->cl_lock);
184 static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
186 struct nfs_inode *nfsi = NFS_I(inode);
188 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
189 nfsi->change_attr = cinfo->after;
192 /* Helper for asynchronous RPC calls */
193 static int nfs4_call_async(struct rpc_clnt *clnt, rpc_action tk_begin,
194 rpc_action tk_exit, void *calldata)
196 struct rpc_task *task;
198 if (!(task = rpc_new_task(clnt, tk_exit, RPC_TASK_ASYNC)))
201 task->tk_calldata = calldata;
202 task->tk_action = tk_begin;
207 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
209 struct inode *inode = state->inode;
211 open_flags &= (FMODE_READ|FMODE_WRITE);
212 /* Protect against nfs4_find_state() */
213 spin_lock(&inode->i_lock);
214 state->state |= open_flags;
215 /* NB! List reordering - see the reclaim code for why. */
216 if ((open_flags & FMODE_WRITE) && 0 == state->nwriters++)
217 list_move(&state->open_states, &state->owner->so_states);
218 if (open_flags & FMODE_READ)
220 memcpy(&state->stateid, stateid, sizeof(state->stateid));
221 spin_unlock(&inode->i_lock);
226 * reclaim state on the server after a reboot.
228 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
230 struct inode *inode = state->inode;
231 struct nfs_server *server = NFS_SERVER(inode);
232 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
233 struct nfs_openargs o_arg = {
236 .open_flags = state->state,
237 .clientid = server->nfs4_state->cl_clientid,
238 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
239 .bitmask = server->attr_bitmask,
241 struct nfs_openres o_res = {
242 .server = server, /* Grrr */
244 struct rpc_message msg = {
245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
248 .rpc_cred = sp->so_cred,
252 if (delegation != NULL) {
253 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
254 memcpy(&state->stateid, &delegation->stateid,
255 sizeof(state->stateid));
256 set_bit(NFS_DELEGATED_STATE, &state->flags);
259 o_arg.u.delegation_type = delegation->type;
261 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
262 if (o_arg.seqid == NULL)
264 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
265 /* Confirm the sequence as being established */
266 nfs_confirm_seqid(&sp->so_seqid, status);
267 nfs_increment_open_seqid(status, o_arg.seqid);
269 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
270 if (o_res.delegation_type != 0) {
271 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
272 /* Did the server issue an immediate delegation recall? */
274 nfs_async_inode_return_delegation(inode, &o_res.stateid);
277 nfs_free_seqid(o_arg.seqid);
278 clear_bit(NFS_DELEGATED_STATE, &state->flags);
279 /* Ensure we update the inode attributes */
284 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
286 struct nfs_server *server = NFS_SERVER(state->inode);
287 struct nfs4_exception exception = { };
290 err = _nfs4_open_reclaim(sp, state);
291 if (err != -NFS4ERR_DELAY)
293 nfs4_handle_exception(server, err, &exception);
294 } while (exception.retry);
298 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
300 struct nfs4_state_owner *sp = state->owner;
301 struct inode *inode = dentry->d_inode;
302 struct nfs_server *server = NFS_SERVER(inode);
303 struct dentry *parent = dget_parent(dentry);
304 struct nfs_openargs arg = {
305 .fh = NFS_FH(parent->d_inode),
306 .clientid = server->nfs4_state->cl_clientid,
307 .name = &dentry->d_name,
310 .bitmask = server->attr_bitmask,
311 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
313 struct nfs_openres res = {
316 struct rpc_message msg = {
317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
320 .rpc_cred = sp->so_cred,
324 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
326 if (state->state == 0)
328 arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
330 if (arg.seqid == NULL)
332 arg.open_flags = state->state;
333 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
334 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
335 nfs_increment_open_seqid(status, arg.seqid);
337 memcpy(state->stateid.data, res.stateid.data,
338 sizeof(state->stateid.data));
339 clear_bit(NFS_DELEGATED_STATE, &state->flags);
341 nfs_free_seqid(arg.seqid);
347 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
349 struct nfs4_exception exception = { };
350 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
353 err = _nfs4_open_delegation_recall(dentry, state);
357 case -NFS4ERR_STALE_CLIENTID:
358 case -NFS4ERR_STALE_STATEID:
359 case -NFS4ERR_EXPIRED:
360 /* Don't recall a delegation if it was lost */
361 nfs4_schedule_state_recovery(server->nfs4_state);
364 err = nfs4_handle_exception(server, err, &exception);
365 } while (exception.retry);
369 static inline int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid)
371 struct nfs_open_confirmargs arg = {
376 struct nfs_open_confirmres res;
377 struct rpc_message msg = {
378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
381 .rpc_cred = sp->so_cred,
385 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
386 /* Confirm the sequence as being established */
387 nfs_confirm_seqid(&sp->so_seqid, status);
388 nfs_increment_open_seqid(status, seqid);
390 memcpy(stateid, &res.stateid, sizeof(*stateid));
394 static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
396 struct nfs_server *server = NFS_SERVER(dir);
397 struct rpc_message msg = {
398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
401 .rpc_cred = sp->so_cred,
405 /* Update sequence id. The caller must serialize! */
406 o_arg->id = sp->so_id;
407 o_arg->clientid = sp->so_client->cl_clientid;
409 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
410 nfs_increment_open_seqid(status, o_arg->seqid);
413 update_changeattr(dir, &o_res->cinfo);
414 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
415 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
416 sp, &o_res->stateid, o_arg->seqid);
420 nfs_confirm_seqid(&sp->so_seqid, 0);
421 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
422 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
427 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
429 struct nfs_access_entry cache;
433 if (openflags & FMODE_READ)
435 if (openflags & FMODE_WRITE)
437 status = nfs_access_get_cached(inode, cred, &cache);
441 /* Be clever: ask server to check for all possible rights */
442 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
444 cache.jiffies = jiffies;
445 status = _nfs4_proc_access(inode, &cache);
448 nfs_access_add_cache(inode, &cache);
450 if ((cache.mask & mask) == mask)
457 * reclaim state on the server after a network partition.
458 * Assumes caller holds the appropriate lock
460 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
462 struct dentry *parent = dget_parent(dentry);
463 struct inode *dir = parent->d_inode;
464 struct inode *inode = state->inode;
465 struct nfs_server *server = NFS_SERVER(dir);
466 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
467 struct nfs_fattr f_attr = {
470 struct nfs_openargs o_arg = {
472 .open_flags = state->state,
473 .name = &dentry->d_name,
474 .bitmask = server->attr_bitmask,
475 .claim = NFS4_OPEN_CLAIM_NULL,
477 struct nfs_openres o_res = {
483 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
484 status = _nfs4_do_access(inode, sp->so_cred, state->state);
487 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
488 set_bit(NFS_DELEGATED_STATE, &state->flags);
491 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
493 if (o_arg.seqid == NULL)
495 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
498 /* Check if files differ */
499 if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
501 /* Has the file handle changed? */
502 if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
503 /* Verify if the change attributes are the same */
504 if (f_attr.change_attr != NFS_I(inode)->change_attr)
506 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
508 /* Lets just pretend that this is the same file */
509 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
510 NFS_I(inode)->fileid = f_attr.fileid;
512 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
513 if (o_res.delegation_type != 0) {
514 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
515 nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
517 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
520 nfs_free_seqid(o_arg.seqid);
521 clear_bit(NFS_DELEGATED_STATE, &state->flags);
527 /* Invalidate the state owner so we don't ever use it again */
528 nfs4_drop_state_owner(sp);
530 /* Should we be trying to close that stateid? */
534 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
536 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
537 struct nfs4_exception exception = { };
541 err = _nfs4_open_expired(sp, state, dentry);
542 if (err == -NFS4ERR_DELAY)
543 nfs4_handle_exception(server, err, &exception);
544 } while (exception.retry);
548 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
550 struct nfs_inode *nfsi = NFS_I(state->inode);
551 struct nfs_open_context *ctx;
554 spin_lock(&state->inode->i_lock);
555 list_for_each_entry(ctx, &nfsi->open_files, list) {
556 if (ctx->state != state)
558 get_nfs_open_context(ctx);
559 spin_unlock(&state->inode->i_lock);
560 status = nfs4_do_open_expired(sp, state, ctx->dentry);
561 put_nfs_open_context(ctx);
564 spin_unlock(&state->inode->i_lock);
569 * Returns an nfs4_state + an extra reference to the inode
571 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
573 struct nfs_delegation *delegation;
574 struct nfs_server *server = NFS_SERVER(inode);
575 struct nfs4_client *clp = server->nfs4_state;
576 struct nfs_inode *nfsi = NFS_I(inode);
577 struct nfs4_state_owner *sp = NULL;
578 struct nfs4_state *state = NULL;
579 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
582 /* Protect against reboot recovery - NOTE ORDER! */
583 down_read(&clp->cl_sem);
584 /* Protect against delegation recall */
585 down_read(&nfsi->rwsem);
586 delegation = NFS_I(inode)->delegation;
588 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
591 if (!(sp = nfs4_get_state_owner(server, cred))) {
592 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
595 state = nfs4_get_open_state(inode, sp);
600 if ((state->state & open_flags) == open_flags) {
601 spin_lock(&inode->i_lock);
602 if (open_flags & FMODE_READ)
604 if (open_flags & FMODE_WRITE)
606 spin_unlock(&inode->i_lock);
608 } else if (state->state != 0)
612 err = _nfs4_do_access(inode, cred, open_flags);
616 set_bit(NFS_DELEGATED_STATE, &state->flags);
617 update_open_stateid(state, &delegation->stateid, open_flags);
619 nfs4_put_state_owner(sp);
620 up_read(&nfsi->rwsem);
621 up_read(&clp->cl_sem);
628 nfs4_put_open_state(state);
629 nfs4_put_state_owner(sp);
631 up_read(&nfsi->rwsem);
632 up_read(&clp->cl_sem);
636 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
638 struct nfs4_exception exception = { };
639 struct nfs4_state *res;
643 err = _nfs4_open_delegated(inode, flags, cred, &res);
646 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
648 } while (exception.retry);
653 * Returns an nfs4_state + an referenced inode
655 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
657 struct nfs4_state_owner *sp;
658 struct nfs4_state *state = NULL;
659 struct nfs_server *server = NFS_SERVER(dir);
660 struct nfs4_client *clp = server->nfs4_state;
661 struct inode *inode = NULL;
663 struct nfs_fattr f_attr = {
666 struct nfs_openargs o_arg = {
669 .name = &dentry->d_name,
671 .bitmask = server->attr_bitmask,
672 .claim = NFS4_OPEN_CLAIM_NULL,
674 struct nfs_openres o_res = {
679 /* Protect against reboot recovery conflicts */
680 down_read(&clp->cl_sem);
682 if (!(sp = nfs4_get_state_owner(server, cred))) {
683 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
686 if (flags & O_EXCL) {
687 u32 *p = (u32 *) o_arg.u.verifier.data;
691 o_arg.u.attrs = sattr;
692 /* Serialization for the sequence id */
694 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
695 if (o_arg.seqid == NULL)
697 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
702 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
705 state = nfs4_get_open_state(inode, sp);
708 update_open_stateid(state, &o_res.stateid, flags);
709 if (o_res.delegation_type != 0)
710 nfs_inode_set_delegation(inode, cred, &o_res);
711 nfs_free_seqid(o_arg.seqid);
712 nfs4_put_state_owner(sp);
713 up_read(&clp->cl_sem);
719 nfs4_put_open_state(state);
720 nfs_free_seqid(o_arg.seqid);
721 nfs4_put_state_owner(sp);
723 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
724 up_read(&clp->cl_sem);
732 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
734 struct nfs4_exception exception = { };
735 struct nfs4_state *res;
739 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
742 /* NOTE: BAD_SEQID means the server and client disagree about the
743 * book-keeping w.r.t. state-changing operations
744 * (OPEN/CLOSE/LOCK/LOCKU...)
745 * It is actually a sign of a bug on the client or on the server.
747 * If we receive a BAD_SEQID error in the particular case of
748 * doing an OPEN, we assume that nfs_increment_open_seqid() will
749 * have unhashed the old state_owner for us, and that we can
750 * therefore safely retry using a new one. We should still warn
753 if (status == -NFS4ERR_BAD_SEQID) {
754 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
758 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
759 status, &exception));
760 } while (exception.retry);
764 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
765 struct nfs_fh *fhandle, struct iattr *sattr,
766 struct nfs4_state *state)
768 struct nfs_setattrargs arg = {
772 .bitmask = server->attr_bitmask,
774 struct nfs_setattrres res = {
778 struct rpc_message msg = {
779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
788 msg.rpc_cred = state->owner->so_cred;
789 nfs4_copy_stateid(&arg.stateid, state, current->files);
791 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
793 status = rpc_call_sync(server->client, &msg, 0);
797 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
798 struct nfs_fh *fhandle, struct iattr *sattr,
799 struct nfs4_state *state)
801 struct nfs4_exception exception = { };
804 err = nfs4_handle_exception(server,
805 _nfs4_do_setattr(server, fattr, fhandle, sattr,
808 } while (exception.retry);
812 struct nfs4_closedata {
814 struct nfs4_state *state;
815 struct nfs_closeargs arg;
816 struct nfs_closeres res;
819 static void nfs4_free_closedata(struct nfs4_closedata *calldata)
821 struct nfs4_state *state = calldata->state;
822 struct nfs4_state_owner *sp = state->owner;
823 struct nfs_server *server = NFS_SERVER(calldata->inode);
825 nfs4_put_open_state(calldata->state);
826 nfs_free_seqid(calldata->arg.seqid);
827 nfs4_put_state_owner(sp);
828 up_read(&server->nfs4_state->cl_sem);
832 static void nfs4_close_done(struct rpc_task *task)
834 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
835 struct nfs4_state *state = calldata->state;
836 struct nfs_server *server = NFS_SERVER(calldata->inode);
838 /* hmm. we are done with the inode, and in the process of freeing
839 * the state_owner. we keep this around to process errors
841 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
842 switch (task->tk_status) {
844 memcpy(&state->stateid, &calldata->res.stateid,
845 sizeof(state->stateid));
847 case -NFS4ERR_STALE_STATEID:
848 case -NFS4ERR_EXPIRED:
849 state->state = calldata->arg.open_flags;
850 nfs4_schedule_state_recovery(server->nfs4_state);
853 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
854 rpc_restart_call(task);
858 state->state = calldata->arg.open_flags;
859 nfs4_free_closedata(calldata);
862 static void nfs4_close_begin(struct rpc_task *task)
864 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
865 struct nfs4_state *state = calldata->state;
866 struct rpc_message msg = {
867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
868 .rpc_argp = &calldata->arg,
869 .rpc_resp = &calldata->res,
870 .rpc_cred = state->owner->so_cred,
875 status = nfs_wait_on_sequence(calldata->arg.seqid, task);
878 /* Don't reorder reads */
880 /* Recalculate the new open mode in case someone reopened the file
881 * while we were waiting in line to be scheduled.
883 if (state->nreaders != 0)
885 if (state->nwriters != 0)
887 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
889 if (mode == state->state) {
890 nfs4_free_closedata(calldata);
891 task->tk_exit = NULL;
896 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
897 calldata->arg.open_flags = mode;
898 rpc_call_setup(task, &msg, 0);
902 * It is possible for data to be read/written from a mem-mapped file
903 * after the sys_close call (which hits the vfs layer as a flush).
904 * This means that we can't safely call nfsv4 close on a file until
905 * the inode is cleared. This in turn means that we are not good
906 * NFSv4 citizens - we do not indicate to the server to update the file's
907 * share state even when we are done with one of the three share
908 * stateid's in the inode.
910 * NOTE: Caller must be holding the sp->so_owner semaphore!
912 int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
914 struct nfs4_closedata *calldata;
915 int status = -ENOMEM;
917 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
918 if (calldata == NULL)
920 calldata->inode = inode;
921 calldata->state = state;
922 calldata->arg.fh = NFS_FH(inode);
923 calldata->arg.stateid = &state->stateid;
924 /* Serialization for the sequence id */
925 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
926 if (calldata->arg.seqid == NULL)
927 goto out_free_calldata;
929 status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_close_begin,
930 nfs4_close_done, calldata);
934 nfs_free_seqid(calldata->arg.seqid);
942 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
945 struct rpc_cred *cred;
946 struct nfs4_state *state;
948 if (nd->flags & LOOKUP_CREATE) {
949 attr.ia_mode = nd->intent.open.create_mode;
950 attr.ia_valid = ATTR_MODE;
951 if (!IS_POSIXACL(dir))
952 attr.ia_mode &= ~current->fs->umask;
955 BUG_ON(nd->intent.open.flags & O_CREAT);
958 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
960 return (struct inode *)cred;
961 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
964 return (struct inode *)state;
969 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
971 struct rpc_cred *cred;
972 struct nfs4_state *state;
975 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
977 return PTR_ERR(cred);
978 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
980 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
982 if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
986 inode = state->inode;
987 if (inode == dentry->d_inode) {
992 nfs4_close_state(state, openflags);
998 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1000 struct nfs4_server_caps_res res = {};
1001 struct rpc_message msg = {
1002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1003 .rpc_argp = fhandle,
1008 status = rpc_call_sync(server->client, &msg, 0);
1010 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1011 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1012 server->caps |= NFS_CAP_ACLS;
1013 if (res.has_links != 0)
1014 server->caps |= NFS_CAP_HARDLINKS;
1015 if (res.has_symlinks != 0)
1016 server->caps |= NFS_CAP_SYMLINKS;
1017 server->acl_bitmask = res.acl_bitmask;
1022 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1024 struct nfs4_exception exception = { };
1027 err = nfs4_handle_exception(server,
1028 _nfs4_server_capabilities(server, fhandle),
1030 } while (exception.retry);
1034 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1035 struct nfs_fsinfo *info)
1037 struct nfs_fattr * fattr = info->fattr;
1038 struct nfs4_lookup_root_arg args = {
1039 .bitmask = nfs4_fattr_bitmap,
1041 struct nfs4_lookup_res res = {
1046 struct rpc_message msg = {
1047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1052 return rpc_call_sync(server->client, &msg, 0);
1055 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1056 struct nfs_fsinfo *info)
1058 struct nfs4_exception exception = { };
1061 err = nfs4_handle_exception(server,
1062 _nfs4_lookup_root(server, fhandle, info),
1064 } while (exception.retry);
1068 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1069 struct nfs_fsinfo *info)
1071 struct nfs_fattr * fattr = info->fattr;
1074 struct nfs4_lookup_arg args = {
1077 .bitmask = nfs4_fattr_bitmap,
1079 struct nfs4_lookup_res res = {
1084 struct rpc_message msg = {
1085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1092 * Now we do a separate LOOKUP for each component of the mount path.
1093 * The LOOKUPs are done separately so that we can conveniently
1094 * catch an ERR_WRONGSEC if it occurs along the way...
1096 status = nfs4_lookup_root(server, fhandle, info);
1100 p = server->mnt_path;
1102 struct nfs4_exception exception = { };
1109 while (*p && (*p != '/'))
1115 status = nfs4_handle_exception(server,
1116 rpc_call_sync(server->client, &msg, 0),
1118 } while (exception.retry);
1121 if (status == -ENOENT) {
1122 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1123 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1128 status = nfs4_server_capabilities(server, fhandle);
1130 status = nfs4_do_fsinfo(server, fhandle, info);
1135 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1137 struct nfs4_getattr_arg args = {
1139 .bitmask = server->attr_bitmask,
1141 struct nfs4_getattr_res res = {
1145 struct rpc_message msg = {
1146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1152 return rpc_call_sync(server->client, &msg, 0);
1155 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1157 struct nfs4_exception exception = { };
1160 err = nfs4_handle_exception(server,
1161 _nfs4_proc_getattr(server, fhandle, fattr),
1163 } while (exception.retry);
1168 * The file is not closed if it is opened due to the a request to change
1169 * the size of the file. The open call will not be needed once the
1170 * VFS layer lookup-intents are implemented.
1172 * Close is called when the inode is destroyed.
1173 * If we haven't opened the file for O_WRONLY, we
1174 * need to in the size_change case to obtain a stateid.
1177 * Because OPEN is always done by name in nfsv4, it is
1178 * possible that we opened a different file by the same
1179 * name. We can recognize this race condition, but we
1180 * can't do anything about it besides returning an error.
1182 * This will be fixed with VFS changes (lookup-intent).
1185 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1186 struct iattr *sattr)
1188 struct rpc_cred *cred;
1189 struct inode *inode = dentry->d_inode;
1190 struct nfs4_state *state;
1195 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1197 return PTR_ERR(cred);
1198 /* Search for an existing WRITE delegation first */
1199 state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
1200 if (!IS_ERR(state)) {
1201 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1204 /* Search for an existing open(O_WRITE) stateid */
1205 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1208 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1209 NFS_FH(inode), sattr, state);
1211 nfs_setattr_update_inode(inode, sattr);
1213 nfs4_close_state(state, FMODE_WRITE);
1218 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1219 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1222 struct nfs_server *server = NFS_SERVER(dir);
1223 struct nfs4_lookup_arg args = {
1224 .bitmask = server->attr_bitmask,
1225 .dir_fh = NFS_FH(dir),
1228 struct nfs4_lookup_res res = {
1233 struct rpc_message msg = {
1234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1241 dprintk("NFS call lookup %s\n", name->name);
1242 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1243 dprintk("NFS reply lookup: %d\n", status);
1247 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1249 struct nfs4_exception exception = { };
1252 err = nfs4_handle_exception(NFS_SERVER(dir),
1253 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1255 } while (exception.retry);
1259 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1261 struct nfs4_accessargs args = {
1262 .fh = NFS_FH(inode),
1264 struct nfs4_accessres res = { 0 };
1265 struct rpc_message msg = {
1266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1269 .rpc_cred = entry->cred,
1271 int mode = entry->mask;
1275 * Determine which access bits we want to ask for...
1277 if (mode & MAY_READ)
1278 args.access |= NFS4_ACCESS_READ;
1279 if (S_ISDIR(inode->i_mode)) {
1280 if (mode & MAY_WRITE)
1281 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1282 if (mode & MAY_EXEC)
1283 args.access |= NFS4_ACCESS_LOOKUP;
1285 if (mode & MAY_WRITE)
1286 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1287 if (mode & MAY_EXEC)
1288 args.access |= NFS4_ACCESS_EXECUTE;
1290 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1293 if (res.access & NFS4_ACCESS_READ)
1294 entry->mask |= MAY_READ;
1295 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1296 entry->mask |= MAY_WRITE;
1297 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1298 entry->mask |= MAY_EXEC;
1303 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1305 struct nfs4_exception exception = { };
1308 err = nfs4_handle_exception(NFS_SERVER(inode),
1309 _nfs4_proc_access(inode, entry),
1311 } while (exception.retry);
1316 * TODO: For the time being, we don't try to get any attributes
1317 * along with any of the zero-copy operations READ, READDIR,
1320 * In the case of the first three, we want to put the GETATTR
1321 * after the read-type operation -- this is because it is hard
1322 * to predict the length of a GETATTR response in v4, and thus
1323 * align the READ data correctly. This means that the GETATTR
1324 * may end up partially falling into the page cache, and we should
1325 * shift it into the 'tail' of the xdr_buf before processing.
1326 * To do this efficiently, we need to know the total length
1327 * of data received, which doesn't seem to be available outside
1330 * In the case of WRITE, we also want to put the GETATTR after
1331 * the operation -- in this case because we want to make sure
1332 * we get the post-operation mtime and size. This means that
1333 * we can't use xdr_encode_pages() as written: we need a variant
1334 * of it which would leave room in the 'tail' iovec.
1336 * Both of these changes to the XDR layer would in fact be quite
1337 * minor, but I decided to leave them for a subsequent patch.
1339 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1340 unsigned int pgbase, unsigned int pglen)
1342 struct nfs4_readlink args = {
1343 .fh = NFS_FH(inode),
1348 struct rpc_message msg = {
1349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1354 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1357 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1358 unsigned int pgbase, unsigned int pglen)
1360 struct nfs4_exception exception = { };
1363 err = nfs4_handle_exception(NFS_SERVER(inode),
1364 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1366 } while (exception.retry);
1370 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1372 int flags = rdata->flags;
1373 struct inode *inode = rdata->inode;
1374 struct nfs_fattr *fattr = rdata->res.fattr;
1375 struct nfs_server *server = NFS_SERVER(inode);
1376 struct rpc_message msg = {
1377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1378 .rpc_argp = &rdata->args,
1379 .rpc_resp = &rdata->res,
1380 .rpc_cred = rdata->cred,
1382 unsigned long timestamp = jiffies;
1385 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1386 (long long) rdata->args.offset);
1389 status = rpc_call_sync(server->client, &msg, flags);
1391 renew_lease(server, timestamp);
1392 dprintk("NFS reply read: %d\n", status);
1396 static int nfs4_proc_read(struct nfs_read_data *rdata)
1398 struct nfs4_exception exception = { };
1401 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1402 _nfs4_proc_read(rdata),
1404 } while (exception.retry);
1408 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1410 int rpcflags = wdata->flags;
1411 struct inode *inode = wdata->inode;
1412 struct nfs_fattr *fattr = wdata->res.fattr;
1413 struct nfs_server *server = NFS_SERVER(inode);
1414 struct rpc_message msg = {
1415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1416 .rpc_argp = &wdata->args,
1417 .rpc_resp = &wdata->res,
1418 .rpc_cred = wdata->cred,
1422 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1423 (long long) wdata->args.offset);
1426 status = rpc_call_sync(server->client, &msg, rpcflags);
1427 dprintk("NFS reply write: %d\n", status);
1431 static int nfs4_proc_write(struct nfs_write_data *wdata)
1433 struct nfs4_exception exception = { };
1436 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1437 _nfs4_proc_write(wdata),
1439 } while (exception.retry);
1443 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1445 struct inode *inode = cdata->inode;
1446 struct nfs_fattr *fattr = cdata->res.fattr;
1447 struct nfs_server *server = NFS_SERVER(inode);
1448 struct rpc_message msg = {
1449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1450 .rpc_argp = &cdata->args,
1451 .rpc_resp = &cdata->res,
1452 .rpc_cred = cdata->cred,
1456 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1457 (long long) cdata->args.offset);
1460 status = rpc_call_sync(server->client, &msg, 0);
1461 dprintk("NFS reply commit: %d\n", status);
1465 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1467 struct nfs4_exception exception = { };
1470 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1471 _nfs4_proc_commit(cdata),
1473 } while (exception.retry);
1479 * We will need to arrange for the VFS layer to provide an atomic open.
1480 * Until then, this create/open method is prone to inefficiency and race
1481 * conditions due to the lookup, create, and open VFS calls from sys_open()
1482 * placed on the wire.
1484 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1485 * The file will be opened again in the subsequent VFS open call
1486 * (nfs4_proc_file_open).
1488 * The open for read will just hang around to be used by any process that
1489 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1493 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1496 struct nfs4_state *state;
1497 struct rpc_cred *cred;
1500 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1502 status = PTR_ERR(cred);
1505 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1507 if (IS_ERR(state)) {
1508 status = PTR_ERR(state);
1511 d_instantiate(dentry, state->inode);
1512 if (flags & O_EXCL) {
1513 struct nfs_fattr fattr;
1514 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1515 NFS_FH(state->inode), sattr, state);
1517 nfs_setattr_update_inode(state->inode, sattr);
1520 } else if (flags != 0)
1522 nfs4_close_state(state, flags);
1527 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1529 struct nfs4_remove_arg args = {
1533 struct nfs4_change_info res;
1534 struct rpc_message msg = {
1535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1541 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1543 update_changeattr(dir, &res);
1547 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1549 struct nfs4_exception exception = { };
1552 err = nfs4_handle_exception(NFS_SERVER(dir),
1553 _nfs4_proc_remove(dir, name),
1555 } while (exception.retry);
1559 struct unlink_desc {
1560 struct nfs4_remove_arg args;
1561 struct nfs4_change_info res;
1564 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1567 struct unlink_desc *up;
1569 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1573 up->args.fh = NFS_FH(dir->d_inode);
1574 up->args.name = name;
1576 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1577 msg->rpc_argp = &up->args;
1578 msg->rpc_resp = &up->res;
1582 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1584 struct rpc_message *msg = &task->tk_msg;
1585 struct unlink_desc *up;
1587 if (msg->rpc_resp != NULL) {
1588 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1589 update_changeattr(dir->d_inode, &up->res);
1591 msg->rpc_resp = NULL;
1592 msg->rpc_argp = NULL;
1597 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1598 struct inode *new_dir, struct qstr *new_name)
1600 struct nfs4_rename_arg arg = {
1601 .old_dir = NFS_FH(old_dir),
1602 .new_dir = NFS_FH(new_dir),
1603 .old_name = old_name,
1604 .new_name = new_name,
1606 struct nfs4_rename_res res = { };
1607 struct rpc_message msg = {
1608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1614 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1617 update_changeattr(old_dir, &res.old_cinfo);
1618 update_changeattr(new_dir, &res.new_cinfo);
1623 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1624 struct inode *new_dir, struct qstr *new_name)
1626 struct nfs4_exception exception = { };
1629 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1630 _nfs4_proc_rename(old_dir, old_name,
1633 } while (exception.retry);
1637 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1639 struct nfs4_link_arg arg = {
1640 .fh = NFS_FH(inode),
1641 .dir_fh = NFS_FH(dir),
1644 struct nfs4_change_info cinfo = { };
1645 struct rpc_message msg = {
1646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1652 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1654 update_changeattr(dir, &cinfo);
1659 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1661 struct nfs4_exception exception = { };
1664 err = nfs4_handle_exception(NFS_SERVER(inode),
1665 _nfs4_proc_link(inode, dir, name),
1667 } while (exception.retry);
1671 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1672 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1673 struct nfs_fattr *fattr)
1675 struct nfs_server *server = NFS_SERVER(dir);
1676 struct nfs4_create_arg arg = {
1677 .dir_fh = NFS_FH(dir),
1682 .bitmask = server->attr_bitmask,
1684 struct nfs4_create_res res = {
1689 struct rpc_message msg = {
1690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1696 if (path->len > NFS4_MAXPATHLEN)
1697 return -ENAMETOOLONG;
1698 arg.u.symlink = path;
1701 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1703 update_changeattr(dir, &res.dir_cinfo);
1707 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1708 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1709 struct nfs_fattr *fattr)
1711 struct nfs4_exception exception = { };
1714 err = nfs4_handle_exception(NFS_SERVER(dir),
1715 _nfs4_proc_symlink(dir, name, path, sattr,
1718 } while (exception.retry);
1722 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1723 struct iattr *sattr)
1725 struct nfs_server *server = NFS_SERVER(dir);
1726 struct nfs_fh fhandle;
1727 struct nfs_fattr fattr;
1728 struct nfs4_create_arg arg = {
1729 .dir_fh = NFS_FH(dir),
1731 .name = &dentry->d_name,
1734 .bitmask = server->attr_bitmask,
1736 struct nfs4_create_res res = {
1741 struct rpc_message msg = {
1742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1750 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1752 update_changeattr(dir, &res.dir_cinfo);
1753 status = nfs_instantiate(dentry, &fhandle, &fattr);
1758 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1759 struct iattr *sattr)
1761 struct nfs4_exception exception = { };
1764 err = nfs4_handle_exception(NFS_SERVER(dir),
1765 _nfs4_proc_mkdir(dir, dentry, sattr),
1767 } while (exception.retry);
1771 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1772 u64 cookie, struct page *page, unsigned int count, int plus)
1774 struct inode *dir = dentry->d_inode;
1775 struct nfs4_readdir_arg args = {
1780 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1782 struct nfs4_readdir_res res;
1783 struct rpc_message msg = {
1784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1791 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1792 dentry->d_parent->d_name.name,
1793 dentry->d_name.name,
1794 (unsigned long long)cookie);
1796 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1797 res.pgbase = args.pgbase;
1798 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1800 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1802 dprintk("%s: returns %d\n", __FUNCTION__, status);
1806 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1807 u64 cookie, struct page *page, unsigned int count, int plus)
1809 struct nfs4_exception exception = { };
1812 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1813 _nfs4_proc_readdir(dentry, cred, cookie,
1816 } while (exception.retry);
1820 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1821 struct iattr *sattr, dev_t rdev)
1823 struct nfs_server *server = NFS_SERVER(dir);
1825 struct nfs_fattr fattr;
1826 struct nfs4_create_arg arg = {
1827 .dir_fh = NFS_FH(dir),
1829 .name = &dentry->d_name,
1831 .bitmask = server->attr_bitmask,
1833 struct nfs4_create_res res = {
1838 struct rpc_message msg = {
1839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1844 int mode = sattr->ia_mode;
1848 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1849 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1851 arg.ftype = NF4FIFO;
1852 else if (S_ISBLK(mode)) {
1854 arg.u.device.specdata1 = MAJOR(rdev);
1855 arg.u.device.specdata2 = MINOR(rdev);
1857 else if (S_ISCHR(mode)) {
1859 arg.u.device.specdata1 = MAJOR(rdev);
1860 arg.u.device.specdata2 = MINOR(rdev);
1863 arg.ftype = NF4SOCK;
1865 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1867 update_changeattr(dir, &res.dir_cinfo);
1868 status = nfs_instantiate(dentry, &fh, &fattr);
1873 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1874 struct iattr *sattr, dev_t rdev)
1876 struct nfs4_exception exception = { };
1879 err = nfs4_handle_exception(NFS_SERVER(dir),
1880 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1882 } while (exception.retry);
1886 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1887 struct nfs_fsstat *fsstat)
1889 struct nfs4_statfs_arg args = {
1891 .bitmask = server->attr_bitmask,
1893 struct rpc_message msg = {
1894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1899 fsstat->fattr->valid = 0;
1900 return rpc_call_sync(server->client, &msg, 0);
1903 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1905 struct nfs4_exception exception = { };
1908 err = nfs4_handle_exception(server,
1909 _nfs4_proc_statfs(server, fhandle, fsstat),
1911 } while (exception.retry);
1915 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1916 struct nfs_fsinfo *fsinfo)
1918 struct nfs4_fsinfo_arg args = {
1920 .bitmask = server->attr_bitmask,
1922 struct rpc_message msg = {
1923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1928 return rpc_call_sync(server->client, &msg, 0);
1931 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1933 struct nfs4_exception exception = { };
1937 err = nfs4_handle_exception(server,
1938 _nfs4_do_fsinfo(server, fhandle, fsinfo),
1940 } while (exception.retry);
1944 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1946 fsinfo->fattr->valid = 0;
1947 return nfs4_do_fsinfo(server, fhandle, fsinfo);
1950 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1951 struct nfs_pathconf *pathconf)
1953 struct nfs4_pathconf_arg args = {
1955 .bitmask = server->attr_bitmask,
1957 struct rpc_message msg = {
1958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1960 .rpc_resp = pathconf,
1963 /* None of the pathconf attributes are mandatory to implement */
1964 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
1965 memset(pathconf, 0, sizeof(*pathconf));
1969 pathconf->fattr->valid = 0;
1970 return rpc_call_sync(server->client, &msg, 0);
1973 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1974 struct nfs_pathconf *pathconf)
1976 struct nfs4_exception exception = { };
1980 err = nfs4_handle_exception(server,
1981 _nfs4_proc_pathconf(server, fhandle, pathconf),
1983 } while (exception.retry);
1988 nfs4_read_done(struct rpc_task *task)
1990 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
1991 struct inode *inode = data->inode;
1993 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1994 rpc_restart_call(task);
1997 if (task->tk_status > 0)
1998 renew_lease(NFS_SERVER(inode), data->timestamp);
1999 /* Call back common NFS readpage processing */
2000 nfs_readpage_result(task);
2004 nfs4_proc_read_setup(struct nfs_read_data *data)
2006 struct rpc_task *task = &data->task;
2007 struct rpc_message msg = {
2008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2009 .rpc_argp = &data->args,
2010 .rpc_resp = &data->res,
2011 .rpc_cred = data->cred,
2013 struct inode *inode = data->inode;
2016 data->timestamp = jiffies;
2018 /* N.B. Do we need to test? Never called for swapfile inode */
2019 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2021 /* Finalize the task. */
2022 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
2023 rpc_call_setup(task, &msg, 0);
2027 nfs4_write_done(struct rpc_task *task)
2029 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2030 struct inode *inode = data->inode;
2032 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2033 rpc_restart_call(task);
2036 if (task->tk_status >= 0)
2037 renew_lease(NFS_SERVER(inode), data->timestamp);
2038 /* Call back common NFS writeback processing */
2039 nfs_writeback_done(task);
2043 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2045 struct rpc_task *task = &data->task;
2046 struct rpc_message msg = {
2047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2048 .rpc_argp = &data->args,
2049 .rpc_resp = &data->res,
2050 .rpc_cred = data->cred,
2052 struct inode *inode = data->inode;
2056 if (how & FLUSH_STABLE) {
2057 if (!NFS_I(inode)->ncommit)
2058 stable = NFS_FILE_SYNC;
2060 stable = NFS_DATA_SYNC;
2062 stable = NFS_UNSTABLE;
2063 data->args.stable = stable;
2065 data->timestamp = jiffies;
2067 /* Set the initial flags for the task. */
2068 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2070 /* Finalize the task. */
2071 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2072 rpc_call_setup(task, &msg, 0);
2076 nfs4_commit_done(struct rpc_task *task)
2078 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2079 struct inode *inode = data->inode;
2081 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2082 rpc_restart_call(task);
2085 /* Call back common NFS writeback processing */
2086 nfs_commit_done(task);
2090 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2092 struct rpc_task *task = &data->task;
2093 struct rpc_message msg = {
2094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2095 .rpc_argp = &data->args,
2096 .rpc_resp = &data->res,
2097 .rpc_cred = data->cred,
2099 struct inode *inode = data->inode;
2102 /* Set the initial flags for the task. */
2103 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2105 /* Finalize the task. */
2106 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2107 rpc_call_setup(task, &msg, 0);
2111 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2112 * standalone procedure for queueing an asynchronous RENEW.
2115 renew_done(struct rpc_task *task)
2117 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2118 unsigned long timestamp = (unsigned long)task->tk_calldata;
2120 if (task->tk_status < 0) {
2121 switch (task->tk_status) {
2122 case -NFS4ERR_STALE_CLIENTID:
2123 case -NFS4ERR_EXPIRED:
2124 case -NFS4ERR_CB_PATH_DOWN:
2125 nfs4_schedule_state_recovery(clp);
2129 spin_lock(&clp->cl_lock);
2130 if (time_before(clp->cl_last_renewal,timestamp))
2131 clp->cl_last_renewal = timestamp;
2132 spin_unlock(&clp->cl_lock);
2136 nfs4_proc_async_renew(struct nfs4_client *clp)
2138 struct rpc_message msg = {
2139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2141 .rpc_cred = clp->cl_cred,
2144 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2145 renew_done, (void *)jiffies);
2149 nfs4_proc_renew(struct nfs4_client *clp)
2151 struct rpc_message msg = {
2152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2154 .rpc_cred = clp->cl_cred,
2156 unsigned long now = jiffies;
2159 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2162 spin_lock(&clp->cl_lock);
2163 if (time_before(clp->cl_last_renewal,now))
2164 clp->cl_last_renewal = now;
2165 spin_unlock(&clp->cl_lock);
2170 * We will need to arrange for the VFS layer to provide an atomic open.
2171 * Until then, this open method is prone to inefficiency and race conditions
2172 * due to the lookup, potential create, and open VFS calls from sys_open()
2173 * placed on the wire.
2176 nfs4_proc_file_open(struct inode *inode, struct file *filp)
2178 struct dentry *dentry = filp->f_dentry;
2179 struct nfs_open_context *ctx;
2180 struct nfs4_state *state = NULL;
2181 struct rpc_cred *cred;
2182 int status = -ENOMEM;
2184 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2185 (int)dentry->d_parent->d_name.len,
2186 dentry->d_parent->d_name.name,
2187 (int)dentry->d_name.len, dentry->d_name.name);
2190 /* Find our open stateid */
2191 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
2193 return PTR_ERR(cred);
2194 ctx = alloc_nfs_open_context(dentry, cred);
2196 if (unlikely(ctx == NULL))
2198 status = -EIO; /* ERACE actually */
2199 state = nfs4_find_state(inode, cred, filp->f_mode);
2200 if (unlikely(state == NULL))
2203 nfs4_close_state(state, filp->f_mode);
2204 ctx->mode = filp->f_mode;
2205 nfs_file_set_open_context(filp, ctx);
2206 put_nfs_open_context(ctx);
2207 if (filp->f_mode & FMODE_WRITE)
2208 nfs_begin_data_update(inode);
2211 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
2212 put_nfs_open_context(ctx);
2220 nfs4_proc_file_release(struct inode *inode, struct file *filp)
2222 if (filp->f_mode & FMODE_WRITE)
2223 nfs_end_data_update(inode);
2224 nfs_file_clear_open_context(filp);
2228 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2230 return (server->caps & NFS_CAP_ACLS)
2231 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2232 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2235 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2236 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2239 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2241 static void buf_to_pages(const void *buf, size_t buflen,
2242 struct page **pages, unsigned int *pgbase)
2244 const void *p = buf;
2246 *pgbase = offset_in_page(buf);
2248 while (p < buf + buflen) {
2249 *(pages++) = virt_to_page(p);
2250 p += PAGE_CACHE_SIZE;
2254 struct nfs4_cached_acl {
2260 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2262 struct nfs_inode *nfsi = NFS_I(inode);
2264 spin_lock(&inode->i_lock);
2265 kfree(nfsi->nfs4_acl);
2266 nfsi->nfs4_acl = acl;
2267 spin_unlock(&inode->i_lock);
2270 static void nfs4_zap_acl_attr(struct inode *inode)
2272 nfs4_set_cached_acl(inode, NULL);
2275 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2277 struct nfs_inode *nfsi = NFS_I(inode);
2278 struct nfs4_cached_acl *acl;
2281 spin_lock(&inode->i_lock);
2282 acl = nfsi->nfs4_acl;
2285 if (buf == NULL) /* user is just asking for length */
2287 if (acl->cached == 0)
2289 ret = -ERANGE; /* see getxattr(2) man page */
2290 if (acl->len > buflen)
2292 memcpy(buf, acl->data, acl->len);
2296 spin_unlock(&inode->i_lock);
2300 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2302 struct nfs4_cached_acl *acl;
2304 if (buf && acl_len <= PAGE_SIZE) {
2305 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2309 memcpy(acl->data, buf, acl_len);
2311 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2318 nfs4_set_cached_acl(inode, acl);
2321 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2323 struct page *pages[NFS4ACL_MAXPAGES];
2324 struct nfs_getaclargs args = {
2325 .fh = NFS_FH(inode),
2329 size_t resp_len = buflen;
2331 struct rpc_message msg = {
2332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2334 .rpc_resp = &resp_len,
2336 struct page *localpage = NULL;
2339 if (buflen < PAGE_SIZE) {
2340 /* As long as we're doing a round trip to the server anyway,
2341 * let's be prepared for a page of acl data. */
2342 localpage = alloc_page(GFP_KERNEL);
2343 resp_buf = page_address(localpage);
2344 if (localpage == NULL)
2346 args.acl_pages[0] = localpage;
2347 args.acl_pgbase = 0;
2348 args.acl_len = PAGE_SIZE;
2351 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2353 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2356 if (resp_len > args.acl_len)
2357 nfs4_write_cached_acl(inode, NULL, resp_len);
2359 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2362 if (resp_len > buflen)
2365 memcpy(buf, resp_buf, resp_len);
2370 __free_page(localpage);
2374 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2376 struct nfs_server *server = NFS_SERVER(inode);
2379 if (!nfs4_server_supports_acls(server))
2381 ret = nfs_revalidate_inode(server, inode);
2384 ret = nfs4_read_cached_acl(inode, buf, buflen);
2387 return nfs4_get_acl_uncached(inode, buf, buflen);
2390 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2392 struct nfs_server *server = NFS_SERVER(inode);
2393 struct page *pages[NFS4ACL_MAXPAGES];
2394 struct nfs_setaclargs arg = {
2395 .fh = NFS_FH(inode),
2399 struct rpc_message msg = {
2400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2406 if (!nfs4_server_supports_acls(server))
2408 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2409 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2411 nfs4_write_cached_acl(inode, buf, buflen);
2416 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
2418 struct nfs4_client *clp = server->nfs4_state;
2420 if (!clp || task->tk_status >= 0)
2422 switch(task->tk_status) {
2423 case -NFS4ERR_STALE_CLIENTID:
2424 case -NFS4ERR_STALE_STATEID:
2425 case -NFS4ERR_EXPIRED:
2426 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2427 nfs4_schedule_state_recovery(clp);
2428 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2429 rpc_wake_up_task(task);
2430 task->tk_status = 0;
2432 case -NFS4ERR_GRACE:
2433 case -NFS4ERR_DELAY:
2434 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2435 task->tk_status = 0;
2437 case -NFS4ERR_OLD_STATEID:
2438 task->tk_status = 0;
2441 task->tk_status = nfs4_map_errors(task->tk_status);
2445 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2449 int interruptible, res = 0;
2453 rpc_clnt_sigmask(clnt, &oldset);
2454 interruptible = TASK_UNINTERRUPTIBLE;
2456 interruptible = TASK_INTERRUPTIBLE;
2457 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2458 nfs4_schedule_state_recovery(clp);
2459 if (clnt->cl_intr && signalled())
2461 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2463 finish_wait(&clp->cl_waitq, &wait);
2464 rpc_clnt_sigunmask(clnt, &oldset);
2468 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2476 *timeout = NFS4_POLL_RETRY_MIN;
2477 if (*timeout > NFS4_POLL_RETRY_MAX)
2478 *timeout = NFS4_POLL_RETRY_MAX;
2479 rpc_clnt_sigmask(clnt, &oldset);
2480 if (clnt->cl_intr) {
2481 schedule_timeout_interruptible(*timeout);
2485 schedule_timeout_uninterruptible(*timeout);
2486 rpc_clnt_sigunmask(clnt, &oldset);
2491 /* This is the error handling routine for processes that are allowed
2494 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2496 struct nfs4_client *clp = server->nfs4_state;
2497 int ret = errorcode;
2499 exception->retry = 0;
2503 case -NFS4ERR_STALE_CLIENTID:
2504 case -NFS4ERR_STALE_STATEID:
2505 case -NFS4ERR_EXPIRED:
2506 ret = nfs4_wait_clnt_recover(server->client, clp);
2508 exception->retry = 1;
2510 case -NFS4ERR_GRACE:
2511 case -NFS4ERR_DELAY:
2512 ret = nfs4_delay(server->client, &exception->timeout);
2514 exception->retry = 1;
2516 case -NFS4ERR_OLD_STATEID:
2518 exception->retry = 1;
2520 /* We failed to handle the error */
2521 return nfs4_map_errors(ret);
2524 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2526 nfs4_verifier sc_verifier;
2527 struct nfs4_setclientid setclientid = {
2528 .sc_verifier = &sc_verifier,
2531 struct rpc_message msg = {
2532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2533 .rpc_argp = &setclientid,
2535 .rpc_cred = clp->cl_cred,
2541 p = (u32*)sc_verifier.data;
2542 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2543 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2546 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2547 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2548 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2549 clp->cl_cred->cr_ops->cr_name,
2550 clp->cl_id_uniquifier);
2551 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2552 sizeof(setclientid.sc_netid), "tcp");
2553 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2554 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2555 clp->cl_ipaddr, port >> 8, port & 255);
2557 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2558 if (status != -NFS4ERR_CLID_INUSE)
2563 ssleep(clp->cl_lease_time + 1);
2565 if (++clp->cl_id_uniquifier == 0)
2572 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2574 struct nfs_fsinfo fsinfo;
2575 struct rpc_message msg = {
2576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2578 .rpc_resp = &fsinfo,
2579 .rpc_cred = clp->cl_cred,
2585 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2587 spin_lock(&clp->cl_lock);
2588 clp->cl_lease_time = fsinfo.lease_time * HZ;
2589 clp->cl_last_renewal = now;
2590 spin_unlock(&clp->cl_lock);
2595 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2597 struct nfs4_delegreturnargs args = {
2598 .fhandle = NFS_FH(inode),
2601 struct rpc_message msg = {
2602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2607 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2610 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2612 struct nfs_server *server = NFS_SERVER(inode);
2613 struct nfs4_exception exception = { };
2616 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2618 case -NFS4ERR_STALE_STATEID:
2619 case -NFS4ERR_EXPIRED:
2620 nfs4_schedule_state_recovery(server->nfs4_state);
2624 err = nfs4_handle_exception(server, err, &exception);
2625 } while (exception.retry);
2629 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2630 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2633 * sleep, with exponential backoff, and retry the LOCK operation.
2635 static unsigned long
2636 nfs4_set_lock_task_retry(unsigned long timeout)
2638 schedule_timeout_interruptible(timeout);
2640 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2641 return NFS4_LOCK_MAXTIMEOUT;
2646 nfs4_lck_type(int cmd, struct file_lock *request)
2649 switch (request->fl_type) {
2651 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2653 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2655 return NFS4_WRITE_LT;
2661 static inline uint64_t
2662 nfs4_lck_length(struct file_lock *request)
2664 if (request->fl_end == OFFSET_MAX)
2665 return ~(uint64_t)0;
2666 return request->fl_end - request->fl_start + 1;
2669 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2671 struct inode *inode = state->inode;
2672 struct nfs_server *server = NFS_SERVER(inode);
2673 struct nfs4_client *clp = server->nfs4_state;
2674 struct nfs_lockargs arg = {
2675 .fh = NFS_FH(inode),
2676 .type = nfs4_lck_type(cmd, request),
2677 .offset = request->fl_start,
2678 .length = nfs4_lck_length(request),
2680 struct nfs_lockres res = {
2683 struct rpc_message msg = {
2684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2687 .rpc_cred = state->owner->so_cred,
2689 struct nfs_lowner nlo;
2690 struct nfs4_lock_state *lsp;
2693 down_read(&clp->cl_sem);
2694 nlo.clientid = clp->cl_clientid;
2695 status = nfs4_set_lock_state(state, request);
2698 lsp = request->fl_u.nfs4_fl.owner;
2699 nlo.id = lsp->ls_id;
2701 status = rpc_call_sync(server->client, &msg, 0);
2703 request->fl_type = F_UNLCK;
2704 } else if (status == -NFS4ERR_DENIED) {
2705 int64_t len, start, end;
2706 start = res.u.denied.offset;
2707 len = res.u.denied.length;
2708 end = start + len - 1;
2709 if (end < 0 || len == 0)
2710 request->fl_end = OFFSET_MAX;
2712 request->fl_end = (loff_t)end;
2713 request->fl_start = (loff_t)start;
2714 request->fl_type = F_WRLCK;
2715 if (res.u.denied.type & 1)
2716 request->fl_type = F_RDLCK;
2717 request->fl_pid = 0;
2721 up_read(&clp->cl_sem);
2725 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2727 struct nfs4_exception exception = { };
2731 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2732 _nfs4_proc_getlk(state, cmd, request),
2734 } while (exception.retry);
2738 static int do_vfs_lock(struct file *file, struct file_lock *fl)
2741 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2743 res = posix_lock_file_wait(file, fl);
2746 res = flock_lock_file_wait(file, fl);
2752 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2757 static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2759 struct inode *inode = state->inode;
2760 struct nfs_server *server = NFS_SERVER(inode);
2761 struct nfs4_client *clp = server->nfs4_state;
2762 struct nfs_lockargs arg = {
2763 .fh = NFS_FH(inode),
2764 .type = nfs4_lck_type(cmd, request),
2765 .offset = request->fl_start,
2766 .length = nfs4_lck_length(request),
2768 struct nfs_lockres res = {
2771 struct rpc_message msg = {
2772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2775 .rpc_cred = state->owner->so_cred,
2777 struct nfs4_lock_state *lsp;
2778 struct nfs_locku_opargs luargs;
2781 down_read(&clp->cl_sem);
2782 status = nfs4_set_lock_state(state, request);
2785 lsp = request->fl_u.nfs4_fl.owner;
2786 /* We might have lost the locks! */
2787 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2789 luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2791 if (luargs.seqid == NULL)
2793 memcpy(luargs.stateid.data, lsp->ls_stateid.data, sizeof(luargs.stateid.data));
2794 arg.u.locku = &luargs;
2795 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2796 nfs_increment_lock_seqid(status, luargs.seqid);
2799 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
2800 sizeof(lsp->ls_stateid.data));
2801 nfs_free_seqid(luargs.seqid);
2804 do_vfs_lock(request->fl_file, request);
2805 up_read(&clp->cl_sem);
2809 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2811 struct nfs4_exception exception = { };
2815 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2816 _nfs4_proc_unlck(state, cmd, request),
2818 } while (exception.retry);
2822 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2824 struct inode *inode = state->inode;
2825 struct nfs_server *server = NFS_SERVER(inode);
2826 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
2827 struct nfs_lockargs arg = {
2828 .fh = NFS_FH(inode),
2829 .type = nfs4_lck_type(cmd, request),
2830 .offset = request->fl_start,
2831 .length = nfs4_lck_length(request),
2833 struct nfs_lockres res = {
2836 struct rpc_message msg = {
2837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2840 .rpc_cred = state->owner->so_cred,
2842 struct nfs_lock_opargs largs = {
2844 .new_lock_owner = 0,
2846 struct nfs_seqid *lock_seqid;
2847 int status = -ENOMEM;
2849 lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2850 if (lock_seqid == NULL)
2852 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
2853 struct nfs4_state_owner *owner = state->owner;
2854 struct nfs_open_to_lock otl = {
2856 .clientid = server->nfs4_state->cl_clientid,
2860 otl.lock_seqid = lock_seqid;
2861 otl.lock_owner.id = lsp->ls_id;
2862 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
2863 largs.u.open_lock = &otl;
2864 largs.new_lock_owner = 1;
2865 arg.u.lock = &largs;
2866 otl.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
2867 if (otl.open_seqid != NULL) {
2868 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2869 /* increment seqid on success, and seqid mutating errors */
2870 nfs_increment_open_seqid(status, otl.open_seqid);
2871 nfs_free_seqid(otl.open_seqid);
2874 nfs_confirm_seqid(&lsp->ls_seqid, 0);
2876 struct nfs_exist_lock el;
2877 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
2878 largs.u.exist_lock = ⪙
2879 arg.u.lock = &largs;
2880 el.seqid = lock_seqid;
2881 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2883 /* increment seqid on success, and seqid mutating errors*/
2884 nfs_increment_lock_seqid(status, lock_seqid);
2885 /* save the returned stateid. */
2887 memcpy(lsp->ls_stateid.data, res.u.stateid.data, sizeof(lsp->ls_stateid.data));
2888 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2889 } else if (status == -NFS4ERR_DENIED)
2891 nfs_free_seqid(lock_seqid);
2895 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2897 struct nfs_server *server = NFS_SERVER(state->inode);
2898 struct nfs4_exception exception = { };
2902 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
2903 if (err != -NFS4ERR_DELAY)
2905 nfs4_handle_exception(server, err, &exception);
2906 } while (exception.retry);
2910 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2912 struct nfs_server *server = NFS_SERVER(state->inode);
2913 struct nfs4_exception exception = { };
2917 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
2918 if (err != -NFS4ERR_DELAY)
2920 nfs4_handle_exception(server, err, &exception);
2921 } while (exception.retry);
2925 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2927 struct nfs4_client *clp = state->owner->so_client;
2930 down_read(&clp->cl_sem);
2931 status = nfs4_set_lock_state(state, request);
2933 status = _nfs4_do_setlk(state, cmd, request, 0);
2935 /* Note: we always want to sleep here! */
2936 request->fl_flags |= FL_SLEEP;
2937 if (do_vfs_lock(request->fl_file, request) < 0)
2938 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
2940 up_read(&clp->cl_sem);
2944 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2946 struct nfs4_exception exception = { };
2950 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2951 _nfs4_proc_setlk(state, cmd, request),
2953 } while (exception.retry);
2958 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
2960 struct nfs_open_context *ctx;
2961 struct nfs4_state *state;
2962 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
2965 /* verify open state */
2966 ctx = (struct nfs_open_context *)filp->private_data;
2969 if (request->fl_start < 0 || request->fl_end < 0)
2973 return nfs4_proc_getlk(state, F_GETLK, request);
2975 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
2978 if (request->fl_type == F_UNLCK)
2979 return nfs4_proc_unlck(state, cmd, request);
2982 status = nfs4_proc_setlk(state, cmd, request);
2983 if ((status != -EAGAIN) || IS_SETLK(cmd))
2985 timeout = nfs4_set_lock_task_retry(timeout);
2986 status = -ERESTARTSYS;
2989 } while(status < 0);
2994 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
2996 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
2997 size_t buflen, int flags)
2999 struct inode *inode = dentry->d_inode;
3001 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3004 if (!S_ISREG(inode->i_mode) &&
3005 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3008 return nfs4_proc_set_acl(inode, buf, buflen);
3011 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3012 * and that's what we'll do for e.g. user attributes that haven't been set.
3013 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3014 * attributes in kernel-managed attribute namespaces. */
3015 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3018 struct inode *inode = dentry->d_inode;
3020 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3023 return nfs4_proc_get_acl(inode, buf, buflen);
3026 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3028 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3030 if (buf && buflen < len)
3033 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3037 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3038 .recover_open = nfs4_open_reclaim,
3039 .recover_lock = nfs4_lock_reclaim,
3042 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3043 .recover_open = nfs4_open_expired,
3044 .recover_lock = nfs4_lock_expired,
3047 static struct inode_operations nfs4_file_inode_operations = {
3048 .permission = nfs_permission,
3049 .getattr = nfs_getattr,
3050 .setattr = nfs_setattr,
3051 .getxattr = nfs4_getxattr,
3052 .setxattr = nfs4_setxattr,
3053 .listxattr = nfs4_listxattr,
3056 struct nfs_rpc_ops nfs_v4_clientops = {
3057 .version = 4, /* protocol version */
3058 .dentry_ops = &nfs4_dentry_operations,
3059 .dir_inode_ops = &nfs4_dir_inode_operations,
3060 .file_inode_ops = &nfs4_file_inode_operations,
3061 .getroot = nfs4_proc_get_root,
3062 .getattr = nfs4_proc_getattr,
3063 .setattr = nfs4_proc_setattr,
3064 .lookup = nfs4_proc_lookup,
3065 .access = nfs4_proc_access,
3066 .readlink = nfs4_proc_readlink,
3067 .read = nfs4_proc_read,
3068 .write = nfs4_proc_write,
3069 .commit = nfs4_proc_commit,
3070 .create = nfs4_proc_create,
3071 .remove = nfs4_proc_remove,
3072 .unlink_setup = nfs4_proc_unlink_setup,
3073 .unlink_done = nfs4_proc_unlink_done,
3074 .rename = nfs4_proc_rename,
3075 .link = nfs4_proc_link,
3076 .symlink = nfs4_proc_symlink,
3077 .mkdir = nfs4_proc_mkdir,
3078 .rmdir = nfs4_proc_remove,
3079 .readdir = nfs4_proc_readdir,
3080 .mknod = nfs4_proc_mknod,
3081 .statfs = nfs4_proc_statfs,
3082 .fsinfo = nfs4_proc_fsinfo,
3083 .pathconf = nfs4_proc_pathconf,
3084 .decode_dirent = nfs4_decode_dirent,
3085 .read_setup = nfs4_proc_read_setup,
3086 .write_setup = nfs4_proc_write_setup,
3087 .commit_setup = nfs4_proc_commit_setup,
3088 .file_open = nfs4_proc_file_open,
3089 .file_release = nfs4_proc_file_release,
3090 .lock = nfs4_proc_lock,
3091 .clear_acl_cache = nfs4_zap_acl_attr,