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.
227 * Assumes caller is holding the sp->so_sem
229 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
231 struct inode *inode = state->inode;
232 struct nfs_server *server = NFS_SERVER(inode);
233 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
234 struct nfs_openargs o_arg = {
237 .open_flags = state->state,
238 .clientid = server->nfs4_state->cl_clientid,
239 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
240 .bitmask = server->attr_bitmask,
242 struct nfs_openres o_res = {
243 .server = server, /* Grrr */
245 struct rpc_message msg = {
246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
249 .rpc_cred = sp->so_cred,
253 if (delegation != NULL) {
254 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
255 memcpy(&state->stateid, &delegation->stateid,
256 sizeof(state->stateid));
257 set_bit(NFS_DELEGATED_STATE, &state->flags);
260 o_arg.u.delegation_type = delegation->type;
262 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
263 if (o_arg.seqid == NULL)
265 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
266 /* Confirm the sequence as being established */
267 nfs_confirm_seqid(&sp->so_seqid, status);
268 nfs_increment_open_seqid(status, o_arg.seqid);
270 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
271 if (o_res.delegation_type != 0) {
272 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
273 /* Did the server issue an immediate delegation recall? */
275 nfs_async_inode_return_delegation(inode, &o_res.stateid);
278 nfs_free_seqid(o_arg.seqid);
279 clear_bit(NFS_DELEGATED_STATE, &state->flags);
280 /* Ensure we update the inode attributes */
285 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
287 struct nfs_server *server = NFS_SERVER(state->inode);
288 struct nfs4_exception exception = { };
291 err = _nfs4_open_reclaim(sp, state);
292 if (err != -NFS4ERR_DELAY)
294 nfs4_handle_exception(server, err, &exception);
295 } while (exception.retry);
299 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
301 struct nfs4_state_owner *sp = state->owner;
302 struct inode *inode = dentry->d_inode;
303 struct nfs_server *server = NFS_SERVER(inode);
304 struct dentry *parent = dget_parent(dentry);
305 struct nfs_openargs arg = {
306 .fh = NFS_FH(parent->d_inode),
307 .clientid = server->nfs4_state->cl_clientid,
308 .name = &dentry->d_name,
311 .bitmask = server->attr_bitmask,
312 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
314 struct nfs_openres res = {
317 struct rpc_message msg = {
318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
321 .rpc_cred = sp->so_cred,
326 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
328 if (state->state == 0)
330 arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
332 if (arg.seqid == NULL)
334 arg.open_flags = state->state;
335 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
336 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
337 nfs_increment_open_seqid(status, arg.seqid);
339 memcpy(state->stateid.data, res.stateid.data,
340 sizeof(state->stateid.data));
341 clear_bit(NFS_DELEGATED_STATE, &state->flags);
343 nfs_free_seqid(arg.seqid);
350 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
352 struct nfs4_exception exception = { };
353 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
356 err = _nfs4_open_delegation_recall(dentry, state);
360 case -NFS4ERR_STALE_CLIENTID:
361 case -NFS4ERR_STALE_STATEID:
362 case -NFS4ERR_EXPIRED:
363 /* Don't recall a delegation if it was lost */
364 nfs4_schedule_state_recovery(server->nfs4_state);
367 err = nfs4_handle_exception(server, err, &exception);
368 } while (exception.retry);
372 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)
374 struct nfs_open_confirmargs arg = {
379 struct nfs_open_confirmres res;
380 struct rpc_message msg = {
381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
384 .rpc_cred = sp->so_cred,
388 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
389 /* Confirm the sequence as being established */
390 nfs_confirm_seqid(&sp->so_seqid, status);
391 nfs_increment_open_seqid(status, seqid);
393 memcpy(stateid, &res.stateid, sizeof(*stateid));
397 static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
399 struct nfs_server *server = NFS_SERVER(dir);
400 struct rpc_message msg = {
401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
404 .rpc_cred = sp->so_cred,
408 /* Update sequence id. The caller must serialize! */
409 o_arg->id = sp->so_id;
410 o_arg->clientid = sp->so_client->cl_clientid;
412 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
413 nfs_increment_open_seqid(status, o_arg->seqid);
416 update_changeattr(dir, &o_res->cinfo);
417 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
418 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
419 sp, &o_res->stateid, o_arg->seqid);
423 nfs_confirm_seqid(&sp->so_seqid, 0);
424 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
425 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
430 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
432 struct nfs_access_entry cache;
436 if (openflags & FMODE_READ)
438 if (openflags & FMODE_WRITE)
440 status = nfs_access_get_cached(inode, cred, &cache);
444 /* Be clever: ask server to check for all possible rights */
445 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
447 cache.jiffies = jiffies;
448 status = _nfs4_proc_access(inode, &cache);
451 nfs_access_add_cache(inode, &cache);
453 if ((cache.mask & mask) == mask)
460 * reclaim state on the server after a network partition.
461 * Assumes caller holds the appropriate lock
463 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
465 struct dentry *parent = dget_parent(dentry);
466 struct inode *dir = parent->d_inode;
467 struct inode *inode = state->inode;
468 struct nfs_server *server = NFS_SERVER(dir);
469 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
470 struct nfs_fattr f_attr = {
473 struct nfs_openargs o_arg = {
475 .open_flags = state->state,
476 .name = &dentry->d_name,
477 .bitmask = server->attr_bitmask,
478 .claim = NFS4_OPEN_CLAIM_NULL,
480 struct nfs_openres o_res = {
486 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
487 status = _nfs4_do_access(inode, sp->so_cred, state->state);
490 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
491 set_bit(NFS_DELEGATED_STATE, &state->flags);
494 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
496 if (o_arg.seqid == NULL)
498 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
501 /* Check if files differ */
502 if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
504 /* Has the file handle changed? */
505 if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
506 /* Verify if the change attributes are the same */
507 if (f_attr.change_attr != NFS_I(inode)->change_attr)
509 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
511 /* Lets just pretend that this is the same file */
512 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
513 NFS_I(inode)->fileid = f_attr.fileid;
515 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
516 if (o_res.delegation_type != 0) {
517 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
518 nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
520 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
523 nfs_free_seqid(o_arg.seqid);
524 clear_bit(NFS_DELEGATED_STATE, &state->flags);
530 /* Invalidate the state owner so we don't ever use it again */
531 nfs4_drop_state_owner(sp);
533 /* Should we be trying to close that stateid? */
537 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
539 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
540 struct nfs4_exception exception = { };
544 err = _nfs4_open_expired(sp, state, dentry);
545 if (err == -NFS4ERR_DELAY)
546 nfs4_handle_exception(server, err, &exception);
547 } while (exception.retry);
551 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
553 struct nfs_inode *nfsi = NFS_I(state->inode);
554 struct nfs_open_context *ctx;
557 spin_lock(&state->inode->i_lock);
558 list_for_each_entry(ctx, &nfsi->open_files, list) {
559 if (ctx->state != state)
561 get_nfs_open_context(ctx);
562 spin_unlock(&state->inode->i_lock);
563 status = nfs4_do_open_expired(sp, state, ctx->dentry);
564 put_nfs_open_context(ctx);
567 spin_unlock(&state->inode->i_lock);
572 * Returns an nfs4_state + an extra reference to the inode
574 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
576 struct nfs_delegation *delegation;
577 struct nfs_server *server = NFS_SERVER(inode);
578 struct nfs4_client *clp = server->nfs4_state;
579 struct nfs_inode *nfsi = NFS_I(inode);
580 struct nfs4_state_owner *sp = NULL;
581 struct nfs4_state *state = NULL;
582 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
585 /* Protect against reboot recovery - NOTE ORDER! */
586 down_read(&clp->cl_sem);
587 /* Protect against delegation recall */
588 down_read(&nfsi->rwsem);
589 delegation = NFS_I(inode)->delegation;
591 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
594 if (!(sp = nfs4_get_state_owner(server, cred))) {
595 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
599 state = nfs4_get_open_state(inode, sp);
604 if ((state->state & open_flags) == open_flags) {
605 spin_lock(&inode->i_lock);
606 if (open_flags & FMODE_READ)
608 if (open_flags & FMODE_WRITE)
610 spin_unlock(&inode->i_lock);
612 } else if (state->state != 0)
616 err = _nfs4_do_access(inode, cred, open_flags);
620 set_bit(NFS_DELEGATED_STATE, &state->flags);
621 update_open_stateid(state, &delegation->stateid, open_flags);
624 nfs4_put_state_owner(sp);
625 up_read(&nfsi->rwsem);
626 up_read(&clp->cl_sem);
633 nfs4_put_open_state(state);
635 nfs4_put_state_owner(sp);
637 up_read(&nfsi->rwsem);
638 up_read(&clp->cl_sem);
642 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
644 struct nfs4_exception exception = { };
645 struct nfs4_state *res;
649 err = _nfs4_open_delegated(inode, flags, cred, &res);
652 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
654 } while (exception.retry);
659 * Returns an nfs4_state + an referenced inode
661 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
663 struct nfs4_state_owner *sp;
664 struct nfs4_state *state = NULL;
665 struct nfs_server *server = NFS_SERVER(dir);
666 struct nfs4_client *clp = server->nfs4_state;
667 struct inode *inode = NULL;
669 struct nfs_fattr f_attr = {
672 struct nfs_openargs o_arg = {
675 .name = &dentry->d_name,
677 .bitmask = server->attr_bitmask,
678 .claim = NFS4_OPEN_CLAIM_NULL,
680 struct nfs_openres o_res = {
685 /* Protect against reboot recovery conflicts */
686 down_read(&clp->cl_sem);
688 if (!(sp = nfs4_get_state_owner(server, cred))) {
689 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
692 if (flags & O_EXCL) {
693 u32 *p = (u32 *) o_arg.u.verifier.data;
697 o_arg.u.attrs = sattr;
698 /* Serialization for the sequence id */
701 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
702 if (o_arg.seqid == NULL)
704 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
709 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
712 state = nfs4_get_open_state(inode, sp);
715 update_open_stateid(state, &o_res.stateid, flags);
716 if (o_res.delegation_type != 0)
717 nfs_inode_set_delegation(inode, cred, &o_res);
718 nfs_free_seqid(o_arg.seqid);
720 nfs4_put_state_owner(sp);
721 up_read(&clp->cl_sem);
727 nfs4_put_open_state(state);
728 nfs_free_seqid(o_arg.seqid);
730 nfs4_put_state_owner(sp);
732 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
733 up_read(&clp->cl_sem);
741 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
743 struct nfs4_exception exception = { };
744 struct nfs4_state *res;
748 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
751 /* NOTE: BAD_SEQID means the server and client disagree about the
752 * book-keeping w.r.t. state-changing operations
753 * (OPEN/CLOSE/LOCK/LOCKU...)
754 * It is actually a sign of a bug on the client or on the server.
756 * If we receive a BAD_SEQID error in the particular case of
757 * doing an OPEN, we assume that nfs_increment_open_seqid() will
758 * have unhashed the old state_owner for us, and that we can
759 * therefore safely retry using a new one. We should still warn
762 if (status == -NFS4ERR_BAD_SEQID) {
763 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
767 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
768 status, &exception));
769 } while (exception.retry);
773 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
774 struct nfs_fh *fhandle, struct iattr *sattr,
775 struct nfs4_state *state)
777 struct nfs_setattrargs arg = {
781 .bitmask = server->attr_bitmask,
783 struct nfs_setattrres res = {
787 struct rpc_message msg = {
788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
797 msg.rpc_cred = state->owner->so_cred;
798 nfs4_copy_stateid(&arg.stateid, state, current->files);
800 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
802 status = rpc_call_sync(server->client, &msg, 0);
806 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
807 struct nfs_fh *fhandle, struct iattr *sattr,
808 struct nfs4_state *state)
810 struct nfs4_exception exception = { };
813 err = nfs4_handle_exception(server,
814 _nfs4_do_setattr(server, fattr, fhandle, sattr,
817 } while (exception.retry);
821 struct nfs4_closedata {
823 struct nfs4_state *state;
824 struct nfs_closeargs arg;
825 struct nfs_closeres res;
828 static void nfs4_free_closedata(struct nfs4_closedata *calldata)
830 struct nfs4_state *state = calldata->state;
831 struct nfs4_state_owner *sp = state->owner;
832 struct nfs_server *server = NFS_SERVER(calldata->inode);
834 nfs4_put_open_state(calldata->state);
835 nfs_free_seqid(calldata->arg.seqid);
837 nfs4_put_state_owner(sp);
838 up_read(&server->nfs4_state->cl_sem);
842 static void nfs4_close_done(struct rpc_task *task)
844 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
845 struct nfs4_state *state = calldata->state;
846 struct nfs_server *server = NFS_SERVER(calldata->inode);
848 /* hmm. we are done with the inode, and in the process of freeing
849 * the state_owner. we keep this around to process errors
851 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
852 switch (task->tk_status) {
854 memcpy(&state->stateid, &calldata->res.stateid,
855 sizeof(state->stateid));
857 case -NFS4ERR_STALE_STATEID:
858 case -NFS4ERR_EXPIRED:
859 state->state = calldata->arg.open_flags;
860 nfs4_schedule_state_recovery(server->nfs4_state);
863 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
864 rpc_restart_call(task);
868 state->state = calldata->arg.open_flags;
869 nfs4_free_closedata(calldata);
872 static void nfs4_close_begin(struct rpc_task *task)
874 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
875 struct nfs4_state *state = calldata->state;
876 struct rpc_message msg = {
877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
878 .rpc_argp = &calldata->arg,
879 .rpc_resp = &calldata->res,
880 .rpc_cred = state->owner->so_cred,
885 status = nfs_wait_on_sequence(calldata->arg.seqid, task);
888 /* Don't reorder reads */
890 /* Recalculate the new open mode in case someone reopened the file
891 * while we were waiting in line to be scheduled.
893 if (state->nreaders != 0)
895 if (state->nwriters != 0)
897 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
899 if (mode == state->state) {
900 nfs4_free_closedata(calldata);
901 task->tk_exit = NULL;
906 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
907 calldata->arg.open_flags = mode;
908 rpc_call_setup(task, &msg, 0);
912 * It is possible for data to be read/written from a mem-mapped file
913 * after the sys_close call (which hits the vfs layer as a flush).
914 * This means that we can't safely call nfsv4 close on a file until
915 * the inode is cleared. This in turn means that we are not good
916 * NFSv4 citizens - we do not indicate to the server to update the file's
917 * share state even when we are done with one of the three share
918 * stateid's in the inode.
920 * NOTE: Caller must be holding the sp->so_owner semaphore!
922 int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
924 struct nfs4_closedata *calldata;
925 int status = -ENOMEM;
927 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
928 if (calldata == NULL)
930 calldata->inode = inode;
931 calldata->state = state;
932 calldata->arg.fh = NFS_FH(inode);
933 calldata->arg.stateid = &state->stateid;
934 /* Serialization for the sequence id */
935 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
936 if (calldata->arg.seqid == NULL)
937 goto out_free_calldata;
939 status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_close_begin,
940 nfs4_close_done, calldata);
944 nfs_free_seqid(calldata->arg.seqid);
952 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
955 struct rpc_cred *cred;
956 struct nfs4_state *state;
958 if (nd->flags & LOOKUP_CREATE) {
959 attr.ia_mode = nd->intent.open.create_mode;
960 attr.ia_valid = ATTR_MODE;
961 if (!IS_POSIXACL(dir))
962 attr.ia_mode &= ~current->fs->umask;
965 BUG_ON(nd->intent.open.flags & O_CREAT);
968 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
970 return (struct inode *)cred;
971 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
974 return (struct inode *)state;
979 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
981 struct rpc_cred *cred;
982 struct nfs4_state *state;
985 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
987 return PTR_ERR(cred);
988 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
990 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
992 if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
996 inode = state->inode;
997 if (inode == dentry->d_inode) {
1002 nfs4_close_state(state, openflags);
1008 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1010 struct nfs4_server_caps_res res = {};
1011 struct rpc_message msg = {
1012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1013 .rpc_argp = fhandle,
1018 status = rpc_call_sync(server->client, &msg, 0);
1020 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1021 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1022 server->caps |= NFS_CAP_ACLS;
1023 if (res.has_links != 0)
1024 server->caps |= NFS_CAP_HARDLINKS;
1025 if (res.has_symlinks != 0)
1026 server->caps |= NFS_CAP_SYMLINKS;
1027 server->acl_bitmask = res.acl_bitmask;
1032 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1034 struct nfs4_exception exception = { };
1037 err = nfs4_handle_exception(server,
1038 _nfs4_server_capabilities(server, fhandle),
1040 } while (exception.retry);
1044 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1045 struct nfs_fsinfo *info)
1047 struct nfs_fattr * fattr = info->fattr;
1048 struct nfs4_lookup_root_arg args = {
1049 .bitmask = nfs4_fattr_bitmap,
1051 struct nfs4_lookup_res res = {
1056 struct rpc_message msg = {
1057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1062 return rpc_call_sync(server->client, &msg, 0);
1065 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1066 struct nfs_fsinfo *info)
1068 struct nfs4_exception exception = { };
1071 err = nfs4_handle_exception(server,
1072 _nfs4_lookup_root(server, fhandle, info),
1074 } while (exception.retry);
1078 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1079 struct nfs_fsinfo *info)
1081 struct nfs_fattr * fattr = info->fattr;
1084 struct nfs4_lookup_arg args = {
1087 .bitmask = nfs4_fattr_bitmap,
1089 struct nfs4_lookup_res res = {
1094 struct rpc_message msg = {
1095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1102 * Now we do a separate LOOKUP for each component of the mount path.
1103 * The LOOKUPs are done separately so that we can conveniently
1104 * catch an ERR_WRONGSEC if it occurs along the way...
1106 status = nfs4_lookup_root(server, fhandle, info);
1110 p = server->mnt_path;
1112 struct nfs4_exception exception = { };
1119 while (*p && (*p != '/'))
1125 status = nfs4_handle_exception(server,
1126 rpc_call_sync(server->client, &msg, 0),
1128 } while (exception.retry);
1131 if (status == -ENOENT) {
1132 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1133 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1138 status = nfs4_server_capabilities(server, fhandle);
1140 status = nfs4_do_fsinfo(server, fhandle, info);
1145 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1147 struct nfs4_getattr_arg args = {
1149 .bitmask = server->attr_bitmask,
1151 struct nfs4_getattr_res res = {
1155 struct rpc_message msg = {
1156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1162 return rpc_call_sync(server->client, &msg, 0);
1165 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1167 struct nfs4_exception exception = { };
1170 err = nfs4_handle_exception(server,
1171 _nfs4_proc_getattr(server, fhandle, fattr),
1173 } while (exception.retry);
1178 * The file is not closed if it is opened due to the a request to change
1179 * the size of the file. The open call will not be needed once the
1180 * VFS layer lookup-intents are implemented.
1182 * Close is called when the inode is destroyed.
1183 * If we haven't opened the file for O_WRONLY, we
1184 * need to in the size_change case to obtain a stateid.
1187 * Because OPEN is always done by name in nfsv4, it is
1188 * possible that we opened a different file by the same
1189 * name. We can recognize this race condition, but we
1190 * can't do anything about it besides returning an error.
1192 * This will be fixed with VFS changes (lookup-intent).
1195 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1196 struct iattr *sattr)
1198 struct rpc_cred *cred;
1199 struct inode *inode = dentry->d_inode;
1200 struct nfs4_state *state;
1205 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1207 return PTR_ERR(cred);
1208 /* Search for an existing WRITE delegation first */
1209 state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
1210 if (!IS_ERR(state)) {
1211 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1214 /* Search for an existing open(O_WRITE) stateid */
1215 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1218 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1219 NFS_FH(inode), sattr, state);
1221 nfs_setattr_update_inode(inode, sattr);
1223 nfs4_close_state(state, FMODE_WRITE);
1228 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1229 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1232 struct nfs_server *server = NFS_SERVER(dir);
1233 struct nfs4_lookup_arg args = {
1234 .bitmask = server->attr_bitmask,
1235 .dir_fh = NFS_FH(dir),
1238 struct nfs4_lookup_res res = {
1243 struct rpc_message msg = {
1244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1251 dprintk("NFS call lookup %s\n", name->name);
1252 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1253 dprintk("NFS reply lookup: %d\n", status);
1257 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1259 struct nfs4_exception exception = { };
1262 err = nfs4_handle_exception(NFS_SERVER(dir),
1263 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1265 } while (exception.retry);
1269 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1271 struct nfs4_accessargs args = {
1272 .fh = NFS_FH(inode),
1274 struct nfs4_accessres res = { 0 };
1275 struct rpc_message msg = {
1276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1279 .rpc_cred = entry->cred,
1281 int mode = entry->mask;
1285 * Determine which access bits we want to ask for...
1287 if (mode & MAY_READ)
1288 args.access |= NFS4_ACCESS_READ;
1289 if (S_ISDIR(inode->i_mode)) {
1290 if (mode & MAY_WRITE)
1291 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1292 if (mode & MAY_EXEC)
1293 args.access |= NFS4_ACCESS_LOOKUP;
1295 if (mode & MAY_WRITE)
1296 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1297 if (mode & MAY_EXEC)
1298 args.access |= NFS4_ACCESS_EXECUTE;
1300 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1303 if (res.access & NFS4_ACCESS_READ)
1304 entry->mask |= MAY_READ;
1305 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1306 entry->mask |= MAY_WRITE;
1307 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1308 entry->mask |= MAY_EXEC;
1313 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1315 struct nfs4_exception exception = { };
1318 err = nfs4_handle_exception(NFS_SERVER(inode),
1319 _nfs4_proc_access(inode, entry),
1321 } while (exception.retry);
1326 * TODO: For the time being, we don't try to get any attributes
1327 * along with any of the zero-copy operations READ, READDIR,
1330 * In the case of the first three, we want to put the GETATTR
1331 * after the read-type operation -- this is because it is hard
1332 * to predict the length of a GETATTR response in v4, and thus
1333 * align the READ data correctly. This means that the GETATTR
1334 * may end up partially falling into the page cache, and we should
1335 * shift it into the 'tail' of the xdr_buf before processing.
1336 * To do this efficiently, we need to know the total length
1337 * of data received, which doesn't seem to be available outside
1340 * In the case of WRITE, we also want to put the GETATTR after
1341 * the operation -- in this case because we want to make sure
1342 * we get the post-operation mtime and size. This means that
1343 * we can't use xdr_encode_pages() as written: we need a variant
1344 * of it which would leave room in the 'tail' iovec.
1346 * Both of these changes to the XDR layer would in fact be quite
1347 * minor, but I decided to leave them for a subsequent patch.
1349 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1350 unsigned int pgbase, unsigned int pglen)
1352 struct nfs4_readlink args = {
1353 .fh = NFS_FH(inode),
1358 struct rpc_message msg = {
1359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1364 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1367 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1368 unsigned int pgbase, unsigned int pglen)
1370 struct nfs4_exception exception = { };
1373 err = nfs4_handle_exception(NFS_SERVER(inode),
1374 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1376 } while (exception.retry);
1380 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1382 int flags = rdata->flags;
1383 struct inode *inode = rdata->inode;
1384 struct nfs_fattr *fattr = rdata->res.fattr;
1385 struct nfs_server *server = NFS_SERVER(inode);
1386 struct rpc_message msg = {
1387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1388 .rpc_argp = &rdata->args,
1389 .rpc_resp = &rdata->res,
1390 .rpc_cred = rdata->cred,
1392 unsigned long timestamp = jiffies;
1395 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1396 (long long) rdata->args.offset);
1399 status = rpc_call_sync(server->client, &msg, flags);
1401 renew_lease(server, timestamp);
1402 dprintk("NFS reply read: %d\n", status);
1406 static int nfs4_proc_read(struct nfs_read_data *rdata)
1408 struct nfs4_exception exception = { };
1411 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1412 _nfs4_proc_read(rdata),
1414 } while (exception.retry);
1418 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1420 int rpcflags = wdata->flags;
1421 struct inode *inode = wdata->inode;
1422 struct nfs_fattr *fattr = wdata->res.fattr;
1423 struct nfs_server *server = NFS_SERVER(inode);
1424 struct rpc_message msg = {
1425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1426 .rpc_argp = &wdata->args,
1427 .rpc_resp = &wdata->res,
1428 .rpc_cred = wdata->cred,
1432 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1433 (long long) wdata->args.offset);
1436 status = rpc_call_sync(server->client, &msg, rpcflags);
1437 dprintk("NFS reply write: %d\n", status);
1441 static int nfs4_proc_write(struct nfs_write_data *wdata)
1443 struct nfs4_exception exception = { };
1446 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1447 _nfs4_proc_write(wdata),
1449 } while (exception.retry);
1453 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1455 struct inode *inode = cdata->inode;
1456 struct nfs_fattr *fattr = cdata->res.fattr;
1457 struct nfs_server *server = NFS_SERVER(inode);
1458 struct rpc_message msg = {
1459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1460 .rpc_argp = &cdata->args,
1461 .rpc_resp = &cdata->res,
1462 .rpc_cred = cdata->cred,
1466 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1467 (long long) cdata->args.offset);
1470 status = rpc_call_sync(server->client, &msg, 0);
1471 dprintk("NFS reply commit: %d\n", status);
1475 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1477 struct nfs4_exception exception = { };
1480 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1481 _nfs4_proc_commit(cdata),
1483 } while (exception.retry);
1489 * We will need to arrange for the VFS layer to provide an atomic open.
1490 * Until then, this create/open method is prone to inefficiency and race
1491 * conditions due to the lookup, create, and open VFS calls from sys_open()
1492 * placed on the wire.
1494 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1495 * The file will be opened again in the subsequent VFS open call
1496 * (nfs4_proc_file_open).
1498 * The open for read will just hang around to be used by any process that
1499 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1503 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1506 struct nfs4_state *state;
1507 struct rpc_cred *cred;
1510 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1512 status = PTR_ERR(cred);
1515 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1517 if (IS_ERR(state)) {
1518 status = PTR_ERR(state);
1521 d_instantiate(dentry, state->inode);
1522 if (flags & O_EXCL) {
1523 struct nfs_fattr fattr;
1524 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1525 NFS_FH(state->inode), sattr, state);
1527 nfs_setattr_update_inode(state->inode, sattr);
1530 } else if (flags != 0)
1532 nfs4_close_state(state, flags);
1537 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1539 struct nfs4_remove_arg args = {
1543 struct nfs4_change_info res;
1544 struct rpc_message msg = {
1545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1551 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1553 update_changeattr(dir, &res);
1557 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1559 struct nfs4_exception exception = { };
1562 err = nfs4_handle_exception(NFS_SERVER(dir),
1563 _nfs4_proc_remove(dir, name),
1565 } while (exception.retry);
1569 struct unlink_desc {
1570 struct nfs4_remove_arg args;
1571 struct nfs4_change_info res;
1574 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1577 struct unlink_desc *up;
1579 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1583 up->args.fh = NFS_FH(dir->d_inode);
1584 up->args.name = name;
1586 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1587 msg->rpc_argp = &up->args;
1588 msg->rpc_resp = &up->res;
1592 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1594 struct rpc_message *msg = &task->tk_msg;
1595 struct unlink_desc *up;
1597 if (msg->rpc_resp != NULL) {
1598 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1599 update_changeattr(dir->d_inode, &up->res);
1601 msg->rpc_resp = NULL;
1602 msg->rpc_argp = NULL;
1607 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1608 struct inode *new_dir, struct qstr *new_name)
1610 struct nfs4_rename_arg arg = {
1611 .old_dir = NFS_FH(old_dir),
1612 .new_dir = NFS_FH(new_dir),
1613 .old_name = old_name,
1614 .new_name = new_name,
1616 struct nfs4_rename_res res = { };
1617 struct rpc_message msg = {
1618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1624 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1627 update_changeattr(old_dir, &res.old_cinfo);
1628 update_changeattr(new_dir, &res.new_cinfo);
1633 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1634 struct inode *new_dir, struct qstr *new_name)
1636 struct nfs4_exception exception = { };
1639 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1640 _nfs4_proc_rename(old_dir, old_name,
1643 } while (exception.retry);
1647 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1649 struct nfs4_link_arg arg = {
1650 .fh = NFS_FH(inode),
1651 .dir_fh = NFS_FH(dir),
1654 struct nfs4_change_info cinfo = { };
1655 struct rpc_message msg = {
1656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1662 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1664 update_changeattr(dir, &cinfo);
1669 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1671 struct nfs4_exception exception = { };
1674 err = nfs4_handle_exception(NFS_SERVER(inode),
1675 _nfs4_proc_link(inode, dir, name),
1677 } while (exception.retry);
1681 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1682 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1683 struct nfs_fattr *fattr)
1685 struct nfs_server *server = NFS_SERVER(dir);
1686 struct nfs4_create_arg arg = {
1687 .dir_fh = NFS_FH(dir),
1692 .bitmask = server->attr_bitmask,
1694 struct nfs4_create_res res = {
1699 struct rpc_message msg = {
1700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1706 if (path->len > NFS4_MAXPATHLEN)
1707 return -ENAMETOOLONG;
1708 arg.u.symlink = path;
1711 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1713 update_changeattr(dir, &res.dir_cinfo);
1717 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1718 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1719 struct nfs_fattr *fattr)
1721 struct nfs4_exception exception = { };
1724 err = nfs4_handle_exception(NFS_SERVER(dir),
1725 _nfs4_proc_symlink(dir, name, path, sattr,
1728 } while (exception.retry);
1732 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1733 struct iattr *sattr)
1735 struct nfs_server *server = NFS_SERVER(dir);
1736 struct nfs_fh fhandle;
1737 struct nfs_fattr fattr;
1738 struct nfs4_create_arg arg = {
1739 .dir_fh = NFS_FH(dir),
1741 .name = &dentry->d_name,
1744 .bitmask = server->attr_bitmask,
1746 struct nfs4_create_res res = {
1751 struct rpc_message msg = {
1752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1760 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1762 update_changeattr(dir, &res.dir_cinfo);
1763 status = nfs_instantiate(dentry, &fhandle, &fattr);
1768 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1769 struct iattr *sattr)
1771 struct nfs4_exception exception = { };
1774 err = nfs4_handle_exception(NFS_SERVER(dir),
1775 _nfs4_proc_mkdir(dir, dentry, sattr),
1777 } while (exception.retry);
1781 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1782 u64 cookie, struct page *page, unsigned int count, int plus)
1784 struct inode *dir = dentry->d_inode;
1785 struct nfs4_readdir_arg args = {
1790 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1792 struct nfs4_readdir_res res;
1793 struct rpc_message msg = {
1794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1801 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1802 dentry->d_parent->d_name.name,
1803 dentry->d_name.name,
1804 (unsigned long long)cookie);
1806 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1807 res.pgbase = args.pgbase;
1808 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1810 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1812 dprintk("%s: returns %d\n", __FUNCTION__, status);
1816 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1817 u64 cookie, struct page *page, unsigned int count, int plus)
1819 struct nfs4_exception exception = { };
1822 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1823 _nfs4_proc_readdir(dentry, cred, cookie,
1826 } while (exception.retry);
1830 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1831 struct iattr *sattr, dev_t rdev)
1833 struct nfs_server *server = NFS_SERVER(dir);
1835 struct nfs_fattr fattr;
1836 struct nfs4_create_arg arg = {
1837 .dir_fh = NFS_FH(dir),
1839 .name = &dentry->d_name,
1841 .bitmask = server->attr_bitmask,
1843 struct nfs4_create_res res = {
1848 struct rpc_message msg = {
1849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1854 int mode = sattr->ia_mode;
1858 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1859 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1861 arg.ftype = NF4FIFO;
1862 else if (S_ISBLK(mode)) {
1864 arg.u.device.specdata1 = MAJOR(rdev);
1865 arg.u.device.specdata2 = MINOR(rdev);
1867 else if (S_ISCHR(mode)) {
1869 arg.u.device.specdata1 = MAJOR(rdev);
1870 arg.u.device.specdata2 = MINOR(rdev);
1873 arg.ftype = NF4SOCK;
1875 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1877 update_changeattr(dir, &res.dir_cinfo);
1878 status = nfs_instantiate(dentry, &fh, &fattr);
1883 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1884 struct iattr *sattr, dev_t rdev)
1886 struct nfs4_exception exception = { };
1889 err = nfs4_handle_exception(NFS_SERVER(dir),
1890 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1892 } while (exception.retry);
1896 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1897 struct nfs_fsstat *fsstat)
1899 struct nfs4_statfs_arg args = {
1901 .bitmask = server->attr_bitmask,
1903 struct rpc_message msg = {
1904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1909 fsstat->fattr->valid = 0;
1910 return rpc_call_sync(server->client, &msg, 0);
1913 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1915 struct nfs4_exception exception = { };
1918 err = nfs4_handle_exception(server,
1919 _nfs4_proc_statfs(server, fhandle, fsstat),
1921 } while (exception.retry);
1925 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1926 struct nfs_fsinfo *fsinfo)
1928 struct nfs4_fsinfo_arg args = {
1930 .bitmask = server->attr_bitmask,
1932 struct rpc_message msg = {
1933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1938 return rpc_call_sync(server->client, &msg, 0);
1941 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1943 struct nfs4_exception exception = { };
1947 err = nfs4_handle_exception(server,
1948 _nfs4_do_fsinfo(server, fhandle, fsinfo),
1950 } while (exception.retry);
1954 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1956 fsinfo->fattr->valid = 0;
1957 return nfs4_do_fsinfo(server, fhandle, fsinfo);
1960 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1961 struct nfs_pathconf *pathconf)
1963 struct nfs4_pathconf_arg args = {
1965 .bitmask = server->attr_bitmask,
1967 struct rpc_message msg = {
1968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1970 .rpc_resp = pathconf,
1973 /* None of the pathconf attributes are mandatory to implement */
1974 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
1975 memset(pathconf, 0, sizeof(*pathconf));
1979 pathconf->fattr->valid = 0;
1980 return rpc_call_sync(server->client, &msg, 0);
1983 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1984 struct nfs_pathconf *pathconf)
1986 struct nfs4_exception exception = { };
1990 err = nfs4_handle_exception(server,
1991 _nfs4_proc_pathconf(server, fhandle, pathconf),
1993 } while (exception.retry);
1998 nfs4_read_done(struct rpc_task *task)
2000 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
2001 struct inode *inode = data->inode;
2003 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2004 rpc_restart_call(task);
2007 if (task->tk_status > 0)
2008 renew_lease(NFS_SERVER(inode), data->timestamp);
2009 /* Call back common NFS readpage processing */
2010 nfs_readpage_result(task);
2014 nfs4_proc_read_setup(struct nfs_read_data *data)
2016 struct rpc_task *task = &data->task;
2017 struct rpc_message msg = {
2018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2019 .rpc_argp = &data->args,
2020 .rpc_resp = &data->res,
2021 .rpc_cred = data->cred,
2023 struct inode *inode = data->inode;
2026 data->timestamp = jiffies;
2028 /* N.B. Do we need to test? Never called for swapfile inode */
2029 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2031 /* Finalize the task. */
2032 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
2033 rpc_call_setup(task, &msg, 0);
2037 nfs4_write_done(struct rpc_task *task)
2039 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2040 struct inode *inode = data->inode;
2042 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2043 rpc_restart_call(task);
2046 if (task->tk_status >= 0)
2047 renew_lease(NFS_SERVER(inode), data->timestamp);
2048 /* Call back common NFS writeback processing */
2049 nfs_writeback_done(task);
2053 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2055 struct rpc_task *task = &data->task;
2056 struct rpc_message msg = {
2057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2058 .rpc_argp = &data->args,
2059 .rpc_resp = &data->res,
2060 .rpc_cred = data->cred,
2062 struct inode *inode = data->inode;
2066 if (how & FLUSH_STABLE) {
2067 if (!NFS_I(inode)->ncommit)
2068 stable = NFS_FILE_SYNC;
2070 stable = NFS_DATA_SYNC;
2072 stable = NFS_UNSTABLE;
2073 data->args.stable = stable;
2075 data->timestamp = jiffies;
2077 /* Set the initial flags for the task. */
2078 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2080 /* Finalize the task. */
2081 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2082 rpc_call_setup(task, &msg, 0);
2086 nfs4_commit_done(struct rpc_task *task)
2088 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2089 struct inode *inode = data->inode;
2091 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2092 rpc_restart_call(task);
2095 /* Call back common NFS writeback processing */
2096 nfs_commit_done(task);
2100 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2102 struct rpc_task *task = &data->task;
2103 struct rpc_message msg = {
2104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2105 .rpc_argp = &data->args,
2106 .rpc_resp = &data->res,
2107 .rpc_cred = data->cred,
2109 struct inode *inode = data->inode;
2112 /* Set the initial flags for the task. */
2113 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2115 /* Finalize the task. */
2116 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2117 rpc_call_setup(task, &msg, 0);
2121 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2122 * standalone procedure for queueing an asynchronous RENEW.
2125 renew_done(struct rpc_task *task)
2127 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2128 unsigned long timestamp = (unsigned long)task->tk_calldata;
2130 if (task->tk_status < 0) {
2131 switch (task->tk_status) {
2132 case -NFS4ERR_STALE_CLIENTID:
2133 case -NFS4ERR_EXPIRED:
2134 case -NFS4ERR_CB_PATH_DOWN:
2135 nfs4_schedule_state_recovery(clp);
2139 spin_lock(&clp->cl_lock);
2140 if (time_before(clp->cl_last_renewal,timestamp))
2141 clp->cl_last_renewal = timestamp;
2142 spin_unlock(&clp->cl_lock);
2146 nfs4_proc_async_renew(struct nfs4_client *clp)
2148 struct rpc_message msg = {
2149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2151 .rpc_cred = clp->cl_cred,
2154 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2155 renew_done, (void *)jiffies);
2159 nfs4_proc_renew(struct nfs4_client *clp)
2161 struct rpc_message msg = {
2162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2164 .rpc_cred = clp->cl_cred,
2166 unsigned long now = jiffies;
2169 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2172 spin_lock(&clp->cl_lock);
2173 if (time_before(clp->cl_last_renewal,now))
2174 clp->cl_last_renewal = now;
2175 spin_unlock(&clp->cl_lock);
2180 * We will need to arrange for the VFS layer to provide an atomic open.
2181 * Until then, this open method is prone to inefficiency and race conditions
2182 * due to the lookup, potential create, and open VFS calls from sys_open()
2183 * placed on the wire.
2186 nfs4_proc_file_open(struct inode *inode, struct file *filp)
2188 struct dentry *dentry = filp->f_dentry;
2189 struct nfs_open_context *ctx;
2190 struct nfs4_state *state = NULL;
2191 struct rpc_cred *cred;
2192 int status = -ENOMEM;
2194 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2195 (int)dentry->d_parent->d_name.len,
2196 dentry->d_parent->d_name.name,
2197 (int)dentry->d_name.len, dentry->d_name.name);
2200 /* Find our open stateid */
2201 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
2203 return PTR_ERR(cred);
2204 ctx = alloc_nfs_open_context(dentry, cred);
2206 if (unlikely(ctx == NULL))
2208 status = -EIO; /* ERACE actually */
2209 state = nfs4_find_state(inode, cred, filp->f_mode);
2210 if (unlikely(state == NULL))
2213 nfs4_close_state(state, filp->f_mode);
2214 ctx->mode = filp->f_mode;
2215 nfs_file_set_open_context(filp, ctx);
2216 put_nfs_open_context(ctx);
2217 if (filp->f_mode & FMODE_WRITE)
2218 nfs_begin_data_update(inode);
2221 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
2222 put_nfs_open_context(ctx);
2230 nfs4_proc_file_release(struct inode *inode, struct file *filp)
2232 if (filp->f_mode & FMODE_WRITE)
2233 nfs_end_data_update(inode);
2234 nfs_file_clear_open_context(filp);
2238 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2240 return (server->caps & NFS_CAP_ACLS)
2241 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2242 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2245 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2246 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2249 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2251 static void buf_to_pages(const void *buf, size_t buflen,
2252 struct page **pages, unsigned int *pgbase)
2254 const void *p = buf;
2256 *pgbase = offset_in_page(buf);
2258 while (p < buf + buflen) {
2259 *(pages++) = virt_to_page(p);
2260 p += PAGE_CACHE_SIZE;
2264 struct nfs4_cached_acl {
2270 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2272 struct nfs_inode *nfsi = NFS_I(inode);
2274 spin_lock(&inode->i_lock);
2275 kfree(nfsi->nfs4_acl);
2276 nfsi->nfs4_acl = acl;
2277 spin_unlock(&inode->i_lock);
2280 static void nfs4_zap_acl_attr(struct inode *inode)
2282 nfs4_set_cached_acl(inode, NULL);
2285 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2287 struct nfs_inode *nfsi = NFS_I(inode);
2288 struct nfs4_cached_acl *acl;
2291 spin_lock(&inode->i_lock);
2292 acl = nfsi->nfs4_acl;
2295 if (buf == NULL) /* user is just asking for length */
2297 if (acl->cached == 0)
2299 ret = -ERANGE; /* see getxattr(2) man page */
2300 if (acl->len > buflen)
2302 memcpy(buf, acl->data, acl->len);
2306 spin_unlock(&inode->i_lock);
2310 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2312 struct nfs4_cached_acl *acl;
2314 if (buf && acl_len <= PAGE_SIZE) {
2315 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2319 memcpy(acl->data, buf, acl_len);
2321 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2328 nfs4_set_cached_acl(inode, acl);
2331 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2333 struct page *pages[NFS4ACL_MAXPAGES];
2334 struct nfs_getaclargs args = {
2335 .fh = NFS_FH(inode),
2339 size_t resp_len = buflen;
2341 struct rpc_message msg = {
2342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2344 .rpc_resp = &resp_len,
2346 struct page *localpage = NULL;
2349 if (buflen < PAGE_SIZE) {
2350 /* As long as we're doing a round trip to the server anyway,
2351 * let's be prepared for a page of acl data. */
2352 localpage = alloc_page(GFP_KERNEL);
2353 resp_buf = page_address(localpage);
2354 if (localpage == NULL)
2356 args.acl_pages[0] = localpage;
2357 args.acl_pgbase = 0;
2358 args.acl_len = PAGE_SIZE;
2361 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2363 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2366 if (resp_len > args.acl_len)
2367 nfs4_write_cached_acl(inode, NULL, resp_len);
2369 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2372 if (resp_len > buflen)
2375 memcpy(buf, resp_buf, resp_len);
2380 __free_page(localpage);
2384 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2386 struct nfs_server *server = NFS_SERVER(inode);
2389 if (!nfs4_server_supports_acls(server))
2391 ret = nfs_revalidate_inode(server, inode);
2394 ret = nfs4_read_cached_acl(inode, buf, buflen);
2397 return nfs4_get_acl_uncached(inode, buf, buflen);
2400 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2402 struct nfs_server *server = NFS_SERVER(inode);
2403 struct page *pages[NFS4ACL_MAXPAGES];
2404 struct nfs_setaclargs arg = {
2405 .fh = NFS_FH(inode),
2409 struct rpc_message msg = {
2410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2416 if (!nfs4_server_supports_acls(server))
2418 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2419 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2421 nfs4_write_cached_acl(inode, buf, buflen);
2426 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
2428 struct nfs4_client *clp = server->nfs4_state;
2430 if (!clp || task->tk_status >= 0)
2432 switch(task->tk_status) {
2433 case -NFS4ERR_STALE_CLIENTID:
2434 case -NFS4ERR_STALE_STATEID:
2435 case -NFS4ERR_EXPIRED:
2436 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2437 nfs4_schedule_state_recovery(clp);
2438 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2439 rpc_wake_up_task(task);
2440 task->tk_status = 0;
2442 case -NFS4ERR_GRACE:
2443 case -NFS4ERR_DELAY:
2444 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2445 task->tk_status = 0;
2447 case -NFS4ERR_OLD_STATEID:
2448 task->tk_status = 0;
2451 task->tk_status = nfs4_map_errors(task->tk_status);
2455 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2459 int interruptible, res = 0;
2463 rpc_clnt_sigmask(clnt, &oldset);
2464 interruptible = TASK_UNINTERRUPTIBLE;
2466 interruptible = TASK_INTERRUPTIBLE;
2467 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2468 nfs4_schedule_state_recovery(clp);
2469 if (clnt->cl_intr && signalled())
2471 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2473 finish_wait(&clp->cl_waitq, &wait);
2474 rpc_clnt_sigunmask(clnt, &oldset);
2478 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2486 *timeout = NFS4_POLL_RETRY_MIN;
2487 if (*timeout > NFS4_POLL_RETRY_MAX)
2488 *timeout = NFS4_POLL_RETRY_MAX;
2489 rpc_clnt_sigmask(clnt, &oldset);
2490 if (clnt->cl_intr) {
2491 schedule_timeout_interruptible(*timeout);
2495 schedule_timeout_uninterruptible(*timeout);
2496 rpc_clnt_sigunmask(clnt, &oldset);
2501 /* This is the error handling routine for processes that are allowed
2504 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2506 struct nfs4_client *clp = server->nfs4_state;
2507 int ret = errorcode;
2509 exception->retry = 0;
2513 case -NFS4ERR_STALE_CLIENTID:
2514 case -NFS4ERR_STALE_STATEID:
2515 case -NFS4ERR_EXPIRED:
2516 ret = nfs4_wait_clnt_recover(server->client, clp);
2518 exception->retry = 1;
2520 case -NFS4ERR_GRACE:
2521 case -NFS4ERR_DELAY:
2522 ret = nfs4_delay(server->client, &exception->timeout);
2524 exception->retry = 1;
2526 case -NFS4ERR_OLD_STATEID:
2528 exception->retry = 1;
2530 /* We failed to handle the error */
2531 return nfs4_map_errors(ret);
2534 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2536 nfs4_verifier sc_verifier;
2537 struct nfs4_setclientid setclientid = {
2538 .sc_verifier = &sc_verifier,
2541 struct rpc_message msg = {
2542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2543 .rpc_argp = &setclientid,
2545 .rpc_cred = clp->cl_cred,
2551 p = (u32*)sc_verifier.data;
2552 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2553 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2556 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2557 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2558 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2559 clp->cl_cred->cr_ops->cr_name,
2560 clp->cl_id_uniquifier);
2561 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2562 sizeof(setclientid.sc_netid), "tcp");
2563 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2564 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2565 clp->cl_ipaddr, port >> 8, port & 255);
2567 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2568 if (status != -NFS4ERR_CLID_INUSE)
2573 ssleep(clp->cl_lease_time + 1);
2575 if (++clp->cl_id_uniquifier == 0)
2582 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2584 struct nfs_fsinfo fsinfo;
2585 struct rpc_message msg = {
2586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2588 .rpc_resp = &fsinfo,
2589 .rpc_cred = clp->cl_cred,
2595 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2597 spin_lock(&clp->cl_lock);
2598 clp->cl_lease_time = fsinfo.lease_time * HZ;
2599 clp->cl_last_renewal = now;
2600 spin_unlock(&clp->cl_lock);
2605 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2607 struct nfs4_delegreturnargs args = {
2608 .fhandle = NFS_FH(inode),
2611 struct rpc_message msg = {
2612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2617 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2620 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2622 struct nfs_server *server = NFS_SERVER(inode);
2623 struct nfs4_exception exception = { };
2626 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2628 case -NFS4ERR_STALE_STATEID:
2629 case -NFS4ERR_EXPIRED:
2630 nfs4_schedule_state_recovery(server->nfs4_state);
2634 err = nfs4_handle_exception(server, err, &exception);
2635 } while (exception.retry);
2639 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2640 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2643 * sleep, with exponential backoff, and retry the LOCK operation.
2645 static unsigned long
2646 nfs4_set_lock_task_retry(unsigned long timeout)
2648 schedule_timeout_interruptible(timeout);
2650 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2651 return NFS4_LOCK_MAXTIMEOUT;
2656 nfs4_lck_type(int cmd, struct file_lock *request)
2659 switch (request->fl_type) {
2661 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2663 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2665 return NFS4_WRITE_LT;
2671 static inline uint64_t
2672 nfs4_lck_length(struct file_lock *request)
2674 if (request->fl_end == OFFSET_MAX)
2675 return ~(uint64_t)0;
2676 return request->fl_end - request->fl_start + 1;
2679 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2681 struct inode *inode = state->inode;
2682 struct nfs_server *server = NFS_SERVER(inode);
2683 struct nfs4_client *clp = server->nfs4_state;
2684 struct nfs_lockargs arg = {
2685 .fh = NFS_FH(inode),
2686 .type = nfs4_lck_type(cmd, request),
2687 .offset = request->fl_start,
2688 .length = nfs4_lck_length(request),
2690 struct nfs_lockres res = {
2693 struct rpc_message msg = {
2694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2697 .rpc_cred = state->owner->so_cred,
2699 struct nfs_lowner nlo;
2700 struct nfs4_lock_state *lsp;
2703 down_read(&clp->cl_sem);
2704 nlo.clientid = clp->cl_clientid;
2705 down(&state->lock_sema);
2706 status = nfs4_set_lock_state(state, request);
2709 lsp = request->fl_u.nfs4_fl.owner;
2710 nlo.id = lsp->ls_id;
2712 status = rpc_call_sync(server->client, &msg, 0);
2714 request->fl_type = F_UNLCK;
2715 } else if (status == -NFS4ERR_DENIED) {
2716 int64_t len, start, end;
2717 start = res.u.denied.offset;
2718 len = res.u.denied.length;
2719 end = start + len - 1;
2720 if (end < 0 || len == 0)
2721 request->fl_end = OFFSET_MAX;
2723 request->fl_end = (loff_t)end;
2724 request->fl_start = (loff_t)start;
2725 request->fl_type = F_WRLCK;
2726 if (res.u.denied.type & 1)
2727 request->fl_type = F_RDLCK;
2728 request->fl_pid = 0;
2732 up(&state->lock_sema);
2733 up_read(&clp->cl_sem);
2737 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2739 struct nfs4_exception exception = { };
2743 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2744 _nfs4_proc_getlk(state, cmd, request),
2746 } while (exception.retry);
2750 static int do_vfs_lock(struct file *file, struct file_lock *fl)
2753 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2755 res = posix_lock_file_wait(file, fl);
2758 res = flock_lock_file_wait(file, fl);
2764 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2769 static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2771 struct inode *inode = state->inode;
2772 struct nfs_server *server = NFS_SERVER(inode);
2773 struct nfs4_client *clp = server->nfs4_state;
2774 struct nfs_lockargs arg = {
2775 .fh = NFS_FH(inode),
2776 .type = nfs4_lck_type(cmd, request),
2777 .offset = request->fl_start,
2778 .length = nfs4_lck_length(request),
2780 struct nfs_lockres res = {
2783 struct rpc_message msg = {
2784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2787 .rpc_cred = state->owner->so_cred,
2789 struct nfs4_lock_state *lsp;
2790 struct nfs_locku_opargs luargs;
2793 down_read(&clp->cl_sem);
2794 down(&state->lock_sema);
2795 status = nfs4_set_lock_state(state, request);
2798 lsp = request->fl_u.nfs4_fl.owner;
2799 /* We might have lost the locks! */
2800 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2802 luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2804 if (luargs.seqid == NULL)
2806 memcpy(luargs.stateid.data, lsp->ls_stateid.data, sizeof(luargs.stateid.data));
2807 arg.u.locku = &luargs;
2808 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2809 nfs_increment_lock_seqid(status, luargs.seqid);
2812 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
2813 sizeof(lsp->ls_stateid.data));
2814 nfs_free_seqid(luargs.seqid);
2816 up(&state->lock_sema);
2818 do_vfs_lock(request->fl_file, request);
2819 up_read(&clp->cl_sem);
2823 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2825 struct nfs4_exception exception = { };
2829 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2830 _nfs4_proc_unlck(state, cmd, request),
2832 } while (exception.retry);
2836 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2838 struct inode *inode = state->inode;
2839 struct nfs_server *server = NFS_SERVER(inode);
2840 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
2841 struct nfs_lockargs arg = {
2842 .fh = NFS_FH(inode),
2843 .type = nfs4_lck_type(cmd, request),
2844 .offset = request->fl_start,
2845 .length = nfs4_lck_length(request),
2847 struct nfs_lockres res = {
2850 struct rpc_message msg = {
2851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2854 .rpc_cred = state->owner->so_cred,
2856 struct nfs_lock_opargs largs = {
2858 .new_lock_owner = 0,
2860 struct nfs_seqid *lock_seqid;
2861 int status = -ENOMEM;
2863 lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2864 if (lock_seqid == NULL)
2866 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
2867 struct nfs4_state_owner *owner = state->owner;
2868 struct nfs_open_to_lock otl = {
2870 .clientid = server->nfs4_state->cl_clientid,
2874 otl.lock_seqid = lock_seqid;
2875 otl.lock_owner.id = lsp->ls_id;
2876 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
2877 largs.u.open_lock = &otl;
2878 largs.new_lock_owner = 1;
2879 arg.u.lock = &largs;
2880 down(&owner->so_sema);
2881 otl.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
2882 if (otl.open_seqid != NULL) {
2883 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2884 /* increment seqid on success, and seqid mutating errors */
2885 nfs_increment_open_seqid(status, otl.open_seqid);
2886 nfs_free_seqid(otl.open_seqid);
2888 up(&owner->so_sema);
2890 nfs_confirm_seqid(&lsp->ls_seqid, 0);
2892 struct nfs_exist_lock el;
2893 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
2894 largs.u.exist_lock = ⪙
2895 arg.u.lock = &largs;
2896 el.seqid = lock_seqid;
2897 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2899 /* increment seqid on success, and seqid mutating errors*/
2900 nfs_increment_lock_seqid(status, lock_seqid);
2901 /* save the returned stateid. */
2903 memcpy(lsp->ls_stateid.data, res.u.stateid.data, sizeof(lsp->ls_stateid.data));
2904 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2905 } else if (status == -NFS4ERR_DENIED)
2907 nfs_free_seqid(lock_seqid);
2911 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2913 struct nfs_server *server = NFS_SERVER(state->inode);
2914 struct nfs4_exception exception = { };
2918 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
2919 if (err != -NFS4ERR_DELAY)
2921 nfs4_handle_exception(server, err, &exception);
2922 } while (exception.retry);
2926 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2928 struct nfs_server *server = NFS_SERVER(state->inode);
2929 struct nfs4_exception exception = { };
2933 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
2934 if (err != -NFS4ERR_DELAY)
2936 nfs4_handle_exception(server, err, &exception);
2937 } while (exception.retry);
2941 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2943 struct nfs4_client *clp = state->owner->so_client;
2946 down_read(&clp->cl_sem);
2947 down(&state->lock_sema);
2948 status = nfs4_set_lock_state(state, request);
2950 status = _nfs4_do_setlk(state, cmd, request, 0);
2951 up(&state->lock_sema);
2953 /* Note: we always want to sleep here! */
2954 request->fl_flags |= FL_SLEEP;
2955 if (do_vfs_lock(request->fl_file, request) < 0)
2956 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
2958 up_read(&clp->cl_sem);
2962 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2964 struct nfs4_exception exception = { };
2968 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2969 _nfs4_proc_setlk(state, cmd, request),
2971 } while (exception.retry);
2976 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
2978 struct nfs_open_context *ctx;
2979 struct nfs4_state *state;
2980 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
2983 /* verify open state */
2984 ctx = (struct nfs_open_context *)filp->private_data;
2987 if (request->fl_start < 0 || request->fl_end < 0)
2991 return nfs4_proc_getlk(state, F_GETLK, request);
2993 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
2996 if (request->fl_type == F_UNLCK)
2997 return nfs4_proc_unlck(state, cmd, request);
3000 status = nfs4_proc_setlk(state, cmd, request);
3001 if ((status != -EAGAIN) || IS_SETLK(cmd))
3003 timeout = nfs4_set_lock_task_retry(timeout);
3004 status = -ERESTARTSYS;
3007 } while(status < 0);
3012 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3014 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3015 size_t buflen, int flags)
3017 struct inode *inode = dentry->d_inode;
3019 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3022 if (!S_ISREG(inode->i_mode) &&
3023 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3026 return nfs4_proc_set_acl(inode, buf, buflen);
3029 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3030 * and that's what we'll do for e.g. user attributes that haven't been set.
3031 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3032 * attributes in kernel-managed attribute namespaces. */
3033 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3036 struct inode *inode = dentry->d_inode;
3038 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3041 return nfs4_proc_get_acl(inode, buf, buflen);
3044 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3046 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3048 if (buf && buflen < len)
3051 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3055 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3056 .recover_open = nfs4_open_reclaim,
3057 .recover_lock = nfs4_lock_reclaim,
3060 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3061 .recover_open = nfs4_open_expired,
3062 .recover_lock = nfs4_lock_expired,
3065 static struct inode_operations nfs4_file_inode_operations = {
3066 .permission = nfs_permission,
3067 .getattr = nfs_getattr,
3068 .setattr = nfs_setattr,
3069 .getxattr = nfs4_getxattr,
3070 .setxattr = nfs4_setxattr,
3071 .listxattr = nfs4_listxattr,
3074 struct nfs_rpc_ops nfs_v4_clientops = {
3075 .version = 4, /* protocol version */
3076 .dentry_ops = &nfs4_dentry_operations,
3077 .dir_inode_ops = &nfs4_dir_inode_operations,
3078 .file_inode_ops = &nfs4_file_inode_operations,
3079 .getroot = nfs4_proc_get_root,
3080 .getattr = nfs4_proc_getattr,
3081 .setattr = nfs4_proc_setattr,
3082 .lookup = nfs4_proc_lookup,
3083 .access = nfs4_proc_access,
3084 .readlink = nfs4_proc_readlink,
3085 .read = nfs4_proc_read,
3086 .write = nfs4_proc_write,
3087 .commit = nfs4_proc_commit,
3088 .create = nfs4_proc_create,
3089 .remove = nfs4_proc_remove,
3090 .unlink_setup = nfs4_proc_unlink_setup,
3091 .unlink_done = nfs4_proc_unlink_done,
3092 .rename = nfs4_proc_rename,
3093 .link = nfs4_proc_link,
3094 .symlink = nfs4_proc_symlink,
3095 .mkdir = nfs4_proc_mkdir,
3096 .rmdir = nfs4_proc_remove,
3097 .readdir = nfs4_proc_readdir,
3098 .mknod = nfs4_proc_mknod,
3099 .statfs = nfs4_proc_statfs,
3100 .fsinfo = nfs4_proc_fsinfo,
3101 .pathconf = nfs4_proc_pathconf,
3102 .decode_dirent = nfs4_decode_dirent,
3103 .read_setup = nfs4_proc_read_setup,
3104 .write_setup = nfs4_proc_write_setup,
3105 .commit_setup = nfs4_proc_commit_setup,
3106 .file_open = nfs4_proc_file_open,
3107 .file_release = nfs4_proc_file_release,
3108 .lock = nfs4_proc_lock,
3109 .clear_acl_cache = nfs4_zap_acl_attr,