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;
824 nfs4_put_open_state(calldata->state);
825 nfs_free_seqid(calldata->arg.seqid);
826 nfs4_put_state_owner(sp);
830 static void nfs4_close_done(struct rpc_task *task)
832 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
833 struct nfs4_state *state = calldata->state;
834 struct nfs_server *server = NFS_SERVER(calldata->inode);
836 /* hmm. we are done with the inode, and in the process of freeing
837 * the state_owner. we keep this around to process errors
839 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
840 switch (task->tk_status) {
842 memcpy(&state->stateid, &calldata->res.stateid,
843 sizeof(state->stateid));
845 case -NFS4ERR_STALE_STATEID:
846 case -NFS4ERR_EXPIRED:
847 state->state = calldata->arg.open_flags;
848 nfs4_schedule_state_recovery(server->nfs4_state);
851 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
852 rpc_restart_call(task);
856 state->state = calldata->arg.open_flags;
857 nfs4_free_closedata(calldata);
860 static void nfs4_close_begin(struct rpc_task *task)
862 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
863 struct nfs4_state *state = calldata->state;
864 struct rpc_message msg = {
865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
866 .rpc_argp = &calldata->arg,
867 .rpc_resp = &calldata->res,
868 .rpc_cred = state->owner->so_cred,
873 status = nfs_wait_on_sequence(calldata->arg.seqid, task);
876 /* Don't reorder reads */
878 /* Recalculate the new open mode in case someone reopened the file
879 * while we were waiting in line to be scheduled.
881 if (state->nreaders != 0)
883 if (state->nwriters != 0)
885 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
887 if (mode == state->state) {
888 nfs4_free_closedata(calldata);
889 task->tk_exit = NULL;
894 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
895 calldata->arg.open_flags = mode;
896 rpc_call_setup(task, &msg, 0);
900 * It is possible for data to be read/written from a mem-mapped file
901 * after the sys_close call (which hits the vfs layer as a flush).
902 * This means that we can't safely call nfsv4 close on a file until
903 * the inode is cleared. This in turn means that we are not good
904 * NFSv4 citizens - we do not indicate to the server to update the file's
905 * share state even when we are done with one of the three share
906 * stateid's in the inode.
908 * NOTE: Caller must be holding the sp->so_owner semaphore!
910 int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
912 struct nfs4_closedata *calldata;
913 int status = -ENOMEM;
915 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
916 if (calldata == NULL)
918 calldata->inode = inode;
919 calldata->state = state;
920 calldata->arg.fh = NFS_FH(inode);
921 calldata->arg.stateid = &state->stateid;
922 /* Serialization for the sequence id */
923 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
924 if (calldata->arg.seqid == NULL)
925 goto out_free_calldata;
927 status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_close_begin,
928 nfs4_close_done, calldata);
932 nfs_free_seqid(calldata->arg.seqid);
940 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
943 struct rpc_cred *cred;
944 struct nfs4_state *state;
946 if (nd->flags & LOOKUP_CREATE) {
947 attr.ia_mode = nd->intent.open.create_mode;
948 attr.ia_valid = ATTR_MODE;
949 if (!IS_POSIXACL(dir))
950 attr.ia_mode &= ~current->fs->umask;
953 BUG_ON(nd->intent.open.flags & O_CREAT);
956 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
958 return (struct inode *)cred;
959 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
962 return (struct inode *)state;
967 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
969 struct rpc_cred *cred;
970 struct nfs4_state *state;
973 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
975 return PTR_ERR(cred);
976 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
978 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
980 if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
984 inode = state->inode;
985 if (inode == dentry->d_inode) {
990 nfs4_close_state(state, openflags);
996 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
998 struct nfs4_server_caps_res res = {};
999 struct rpc_message msg = {
1000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1001 .rpc_argp = fhandle,
1006 status = rpc_call_sync(server->client, &msg, 0);
1008 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1009 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1010 server->caps |= NFS_CAP_ACLS;
1011 if (res.has_links != 0)
1012 server->caps |= NFS_CAP_HARDLINKS;
1013 if (res.has_symlinks != 0)
1014 server->caps |= NFS_CAP_SYMLINKS;
1015 server->acl_bitmask = res.acl_bitmask;
1020 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1022 struct nfs4_exception exception = { };
1025 err = nfs4_handle_exception(server,
1026 _nfs4_server_capabilities(server, fhandle),
1028 } while (exception.retry);
1032 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1033 struct nfs_fsinfo *info)
1035 struct nfs_fattr * fattr = info->fattr;
1036 struct nfs4_lookup_root_arg args = {
1037 .bitmask = nfs4_fattr_bitmap,
1039 struct nfs4_lookup_res res = {
1044 struct rpc_message msg = {
1045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1050 return rpc_call_sync(server->client, &msg, 0);
1053 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1054 struct nfs_fsinfo *info)
1056 struct nfs4_exception exception = { };
1059 err = nfs4_handle_exception(server,
1060 _nfs4_lookup_root(server, fhandle, info),
1062 } while (exception.retry);
1066 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1067 struct nfs_fsinfo *info)
1069 struct nfs_fattr * fattr = info->fattr;
1072 struct nfs4_lookup_arg args = {
1075 .bitmask = nfs4_fattr_bitmap,
1077 struct nfs4_lookup_res res = {
1082 struct rpc_message msg = {
1083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1090 * Now we do a separate LOOKUP for each component of the mount path.
1091 * The LOOKUPs are done separately so that we can conveniently
1092 * catch an ERR_WRONGSEC if it occurs along the way...
1094 status = nfs4_lookup_root(server, fhandle, info);
1098 p = server->mnt_path;
1100 struct nfs4_exception exception = { };
1107 while (*p && (*p != '/'))
1113 status = nfs4_handle_exception(server,
1114 rpc_call_sync(server->client, &msg, 0),
1116 } while (exception.retry);
1119 if (status == -ENOENT) {
1120 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1121 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1126 status = nfs4_server_capabilities(server, fhandle);
1128 status = nfs4_do_fsinfo(server, fhandle, info);
1133 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1135 struct nfs4_getattr_arg args = {
1137 .bitmask = server->attr_bitmask,
1139 struct nfs4_getattr_res res = {
1143 struct rpc_message msg = {
1144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1150 return rpc_call_sync(server->client, &msg, 0);
1153 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1155 struct nfs4_exception exception = { };
1158 err = nfs4_handle_exception(server,
1159 _nfs4_proc_getattr(server, fhandle, fattr),
1161 } while (exception.retry);
1166 * The file is not closed if it is opened due to the a request to change
1167 * the size of the file. The open call will not be needed once the
1168 * VFS layer lookup-intents are implemented.
1170 * Close is called when the inode is destroyed.
1171 * If we haven't opened the file for O_WRONLY, we
1172 * need to in the size_change case to obtain a stateid.
1175 * Because OPEN is always done by name in nfsv4, it is
1176 * possible that we opened a different file by the same
1177 * name. We can recognize this race condition, but we
1178 * can't do anything about it besides returning an error.
1180 * This will be fixed with VFS changes (lookup-intent).
1183 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1184 struct iattr *sattr)
1186 struct rpc_cred *cred;
1187 struct inode *inode = dentry->d_inode;
1188 struct nfs4_state *state;
1193 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1195 return PTR_ERR(cred);
1196 /* Search for an existing WRITE delegation first */
1197 state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
1198 if (!IS_ERR(state)) {
1199 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1202 /* Search for an existing open(O_WRITE) stateid */
1203 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1206 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1207 NFS_FH(inode), sattr, state);
1209 nfs_setattr_update_inode(inode, sattr);
1211 nfs4_close_state(state, FMODE_WRITE);
1216 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1217 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1220 struct nfs_server *server = NFS_SERVER(dir);
1221 struct nfs4_lookup_arg args = {
1222 .bitmask = server->attr_bitmask,
1223 .dir_fh = NFS_FH(dir),
1226 struct nfs4_lookup_res res = {
1231 struct rpc_message msg = {
1232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1239 dprintk("NFS call lookup %s\n", name->name);
1240 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1241 dprintk("NFS reply lookup: %d\n", status);
1245 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1247 struct nfs4_exception exception = { };
1250 err = nfs4_handle_exception(NFS_SERVER(dir),
1251 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1253 } while (exception.retry);
1257 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1259 struct nfs4_accessargs args = {
1260 .fh = NFS_FH(inode),
1262 struct nfs4_accessres res = { 0 };
1263 struct rpc_message msg = {
1264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1267 .rpc_cred = entry->cred,
1269 int mode = entry->mask;
1273 * Determine which access bits we want to ask for...
1275 if (mode & MAY_READ)
1276 args.access |= NFS4_ACCESS_READ;
1277 if (S_ISDIR(inode->i_mode)) {
1278 if (mode & MAY_WRITE)
1279 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1280 if (mode & MAY_EXEC)
1281 args.access |= NFS4_ACCESS_LOOKUP;
1283 if (mode & MAY_WRITE)
1284 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1285 if (mode & MAY_EXEC)
1286 args.access |= NFS4_ACCESS_EXECUTE;
1288 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1291 if (res.access & NFS4_ACCESS_READ)
1292 entry->mask |= MAY_READ;
1293 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1294 entry->mask |= MAY_WRITE;
1295 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1296 entry->mask |= MAY_EXEC;
1301 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1303 struct nfs4_exception exception = { };
1306 err = nfs4_handle_exception(NFS_SERVER(inode),
1307 _nfs4_proc_access(inode, entry),
1309 } while (exception.retry);
1314 * TODO: For the time being, we don't try to get any attributes
1315 * along with any of the zero-copy operations READ, READDIR,
1318 * In the case of the first three, we want to put the GETATTR
1319 * after the read-type operation -- this is because it is hard
1320 * to predict the length of a GETATTR response in v4, and thus
1321 * align the READ data correctly. This means that the GETATTR
1322 * may end up partially falling into the page cache, and we should
1323 * shift it into the 'tail' of the xdr_buf before processing.
1324 * To do this efficiently, we need to know the total length
1325 * of data received, which doesn't seem to be available outside
1328 * In the case of WRITE, we also want to put the GETATTR after
1329 * the operation -- in this case because we want to make sure
1330 * we get the post-operation mtime and size. This means that
1331 * we can't use xdr_encode_pages() as written: we need a variant
1332 * of it which would leave room in the 'tail' iovec.
1334 * Both of these changes to the XDR layer would in fact be quite
1335 * minor, but I decided to leave them for a subsequent patch.
1337 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1338 unsigned int pgbase, unsigned int pglen)
1340 struct nfs4_readlink args = {
1341 .fh = NFS_FH(inode),
1346 struct rpc_message msg = {
1347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1352 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1355 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1356 unsigned int pgbase, unsigned int pglen)
1358 struct nfs4_exception exception = { };
1361 err = nfs4_handle_exception(NFS_SERVER(inode),
1362 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1364 } while (exception.retry);
1368 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1370 int flags = rdata->flags;
1371 struct inode *inode = rdata->inode;
1372 struct nfs_fattr *fattr = rdata->res.fattr;
1373 struct nfs_server *server = NFS_SERVER(inode);
1374 struct rpc_message msg = {
1375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1376 .rpc_argp = &rdata->args,
1377 .rpc_resp = &rdata->res,
1378 .rpc_cred = rdata->cred,
1380 unsigned long timestamp = jiffies;
1383 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1384 (long long) rdata->args.offset);
1387 status = rpc_call_sync(server->client, &msg, flags);
1389 renew_lease(server, timestamp);
1390 dprintk("NFS reply read: %d\n", status);
1394 static int nfs4_proc_read(struct nfs_read_data *rdata)
1396 struct nfs4_exception exception = { };
1399 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1400 _nfs4_proc_read(rdata),
1402 } while (exception.retry);
1406 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1408 int rpcflags = wdata->flags;
1409 struct inode *inode = wdata->inode;
1410 struct nfs_fattr *fattr = wdata->res.fattr;
1411 struct nfs_server *server = NFS_SERVER(inode);
1412 struct rpc_message msg = {
1413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1414 .rpc_argp = &wdata->args,
1415 .rpc_resp = &wdata->res,
1416 .rpc_cred = wdata->cred,
1420 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1421 (long long) wdata->args.offset);
1424 status = rpc_call_sync(server->client, &msg, rpcflags);
1425 dprintk("NFS reply write: %d\n", status);
1429 static int nfs4_proc_write(struct nfs_write_data *wdata)
1431 struct nfs4_exception exception = { };
1434 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1435 _nfs4_proc_write(wdata),
1437 } while (exception.retry);
1441 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1443 struct inode *inode = cdata->inode;
1444 struct nfs_fattr *fattr = cdata->res.fattr;
1445 struct nfs_server *server = NFS_SERVER(inode);
1446 struct rpc_message msg = {
1447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1448 .rpc_argp = &cdata->args,
1449 .rpc_resp = &cdata->res,
1450 .rpc_cred = cdata->cred,
1454 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1455 (long long) cdata->args.offset);
1458 status = rpc_call_sync(server->client, &msg, 0);
1459 dprintk("NFS reply commit: %d\n", status);
1463 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1465 struct nfs4_exception exception = { };
1468 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1469 _nfs4_proc_commit(cdata),
1471 } while (exception.retry);
1477 * We will need to arrange for the VFS layer to provide an atomic open.
1478 * Until then, this create/open method is prone to inefficiency and race
1479 * conditions due to the lookup, create, and open VFS calls from sys_open()
1480 * placed on the wire.
1482 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1483 * The file will be opened again in the subsequent VFS open call
1484 * (nfs4_proc_file_open).
1486 * The open for read will just hang around to be used by any process that
1487 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1491 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1494 struct nfs4_state *state;
1495 struct rpc_cred *cred;
1498 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1500 status = PTR_ERR(cred);
1503 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1505 if (IS_ERR(state)) {
1506 status = PTR_ERR(state);
1509 d_instantiate(dentry, state->inode);
1510 if (flags & O_EXCL) {
1511 struct nfs_fattr fattr;
1512 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1513 NFS_FH(state->inode), sattr, state);
1515 nfs_setattr_update_inode(state->inode, sattr);
1518 } else if (flags != 0)
1520 nfs4_close_state(state, flags);
1525 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1527 struct nfs4_remove_arg args = {
1531 struct nfs4_change_info res;
1532 struct rpc_message msg = {
1533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1539 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1541 update_changeattr(dir, &res);
1545 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1547 struct nfs4_exception exception = { };
1550 err = nfs4_handle_exception(NFS_SERVER(dir),
1551 _nfs4_proc_remove(dir, name),
1553 } while (exception.retry);
1557 struct unlink_desc {
1558 struct nfs4_remove_arg args;
1559 struct nfs4_change_info res;
1562 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1565 struct unlink_desc *up;
1567 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1571 up->args.fh = NFS_FH(dir->d_inode);
1572 up->args.name = name;
1574 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1575 msg->rpc_argp = &up->args;
1576 msg->rpc_resp = &up->res;
1580 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1582 struct rpc_message *msg = &task->tk_msg;
1583 struct unlink_desc *up;
1585 if (msg->rpc_resp != NULL) {
1586 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1587 update_changeattr(dir->d_inode, &up->res);
1589 msg->rpc_resp = NULL;
1590 msg->rpc_argp = NULL;
1595 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1596 struct inode *new_dir, struct qstr *new_name)
1598 struct nfs4_rename_arg arg = {
1599 .old_dir = NFS_FH(old_dir),
1600 .new_dir = NFS_FH(new_dir),
1601 .old_name = old_name,
1602 .new_name = new_name,
1604 struct nfs4_rename_res res = { };
1605 struct rpc_message msg = {
1606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1612 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1615 update_changeattr(old_dir, &res.old_cinfo);
1616 update_changeattr(new_dir, &res.new_cinfo);
1621 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1622 struct inode *new_dir, struct qstr *new_name)
1624 struct nfs4_exception exception = { };
1627 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1628 _nfs4_proc_rename(old_dir, old_name,
1631 } while (exception.retry);
1635 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1637 struct nfs4_link_arg arg = {
1638 .fh = NFS_FH(inode),
1639 .dir_fh = NFS_FH(dir),
1642 struct nfs4_change_info cinfo = { };
1643 struct rpc_message msg = {
1644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1650 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1652 update_changeattr(dir, &cinfo);
1657 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1659 struct nfs4_exception exception = { };
1662 err = nfs4_handle_exception(NFS_SERVER(inode),
1663 _nfs4_proc_link(inode, dir, name),
1665 } while (exception.retry);
1669 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1670 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1671 struct nfs_fattr *fattr)
1673 struct nfs_server *server = NFS_SERVER(dir);
1674 struct nfs4_create_arg arg = {
1675 .dir_fh = NFS_FH(dir),
1680 .bitmask = server->attr_bitmask,
1682 struct nfs4_create_res res = {
1687 struct rpc_message msg = {
1688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1694 if (path->len > NFS4_MAXPATHLEN)
1695 return -ENAMETOOLONG;
1696 arg.u.symlink = path;
1699 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1701 update_changeattr(dir, &res.dir_cinfo);
1705 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1706 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1707 struct nfs_fattr *fattr)
1709 struct nfs4_exception exception = { };
1712 err = nfs4_handle_exception(NFS_SERVER(dir),
1713 _nfs4_proc_symlink(dir, name, path, sattr,
1716 } while (exception.retry);
1720 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1721 struct iattr *sattr)
1723 struct nfs_server *server = NFS_SERVER(dir);
1724 struct nfs_fh fhandle;
1725 struct nfs_fattr fattr;
1726 struct nfs4_create_arg arg = {
1727 .dir_fh = NFS_FH(dir),
1729 .name = &dentry->d_name,
1732 .bitmask = server->attr_bitmask,
1734 struct nfs4_create_res res = {
1739 struct rpc_message msg = {
1740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1748 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1750 update_changeattr(dir, &res.dir_cinfo);
1751 status = nfs_instantiate(dentry, &fhandle, &fattr);
1756 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1757 struct iattr *sattr)
1759 struct nfs4_exception exception = { };
1762 err = nfs4_handle_exception(NFS_SERVER(dir),
1763 _nfs4_proc_mkdir(dir, dentry, sattr),
1765 } while (exception.retry);
1769 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1770 u64 cookie, struct page *page, unsigned int count, int plus)
1772 struct inode *dir = dentry->d_inode;
1773 struct nfs4_readdir_arg args = {
1778 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1780 struct nfs4_readdir_res res;
1781 struct rpc_message msg = {
1782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1789 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1790 dentry->d_parent->d_name.name,
1791 dentry->d_name.name,
1792 (unsigned long long)cookie);
1794 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1795 res.pgbase = args.pgbase;
1796 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1798 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1800 dprintk("%s: returns %d\n", __FUNCTION__, status);
1804 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1805 u64 cookie, struct page *page, unsigned int count, int plus)
1807 struct nfs4_exception exception = { };
1810 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1811 _nfs4_proc_readdir(dentry, cred, cookie,
1814 } while (exception.retry);
1818 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1819 struct iattr *sattr, dev_t rdev)
1821 struct nfs_server *server = NFS_SERVER(dir);
1823 struct nfs_fattr fattr;
1824 struct nfs4_create_arg arg = {
1825 .dir_fh = NFS_FH(dir),
1827 .name = &dentry->d_name,
1829 .bitmask = server->attr_bitmask,
1831 struct nfs4_create_res res = {
1836 struct rpc_message msg = {
1837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1842 int mode = sattr->ia_mode;
1846 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1847 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1849 arg.ftype = NF4FIFO;
1850 else if (S_ISBLK(mode)) {
1852 arg.u.device.specdata1 = MAJOR(rdev);
1853 arg.u.device.specdata2 = MINOR(rdev);
1855 else if (S_ISCHR(mode)) {
1857 arg.u.device.specdata1 = MAJOR(rdev);
1858 arg.u.device.specdata2 = MINOR(rdev);
1861 arg.ftype = NF4SOCK;
1863 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1865 update_changeattr(dir, &res.dir_cinfo);
1866 status = nfs_instantiate(dentry, &fh, &fattr);
1871 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1872 struct iattr *sattr, dev_t rdev)
1874 struct nfs4_exception exception = { };
1877 err = nfs4_handle_exception(NFS_SERVER(dir),
1878 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1880 } while (exception.retry);
1884 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1885 struct nfs_fsstat *fsstat)
1887 struct nfs4_statfs_arg args = {
1889 .bitmask = server->attr_bitmask,
1891 struct rpc_message msg = {
1892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1897 fsstat->fattr->valid = 0;
1898 return rpc_call_sync(server->client, &msg, 0);
1901 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1903 struct nfs4_exception exception = { };
1906 err = nfs4_handle_exception(server,
1907 _nfs4_proc_statfs(server, fhandle, fsstat),
1909 } while (exception.retry);
1913 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1914 struct nfs_fsinfo *fsinfo)
1916 struct nfs4_fsinfo_arg args = {
1918 .bitmask = server->attr_bitmask,
1920 struct rpc_message msg = {
1921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1926 return rpc_call_sync(server->client, &msg, 0);
1929 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1931 struct nfs4_exception exception = { };
1935 err = nfs4_handle_exception(server,
1936 _nfs4_do_fsinfo(server, fhandle, fsinfo),
1938 } while (exception.retry);
1942 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1944 fsinfo->fattr->valid = 0;
1945 return nfs4_do_fsinfo(server, fhandle, fsinfo);
1948 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1949 struct nfs_pathconf *pathconf)
1951 struct nfs4_pathconf_arg args = {
1953 .bitmask = server->attr_bitmask,
1955 struct rpc_message msg = {
1956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1958 .rpc_resp = pathconf,
1961 /* None of the pathconf attributes are mandatory to implement */
1962 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
1963 memset(pathconf, 0, sizeof(*pathconf));
1967 pathconf->fattr->valid = 0;
1968 return rpc_call_sync(server->client, &msg, 0);
1971 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1972 struct nfs_pathconf *pathconf)
1974 struct nfs4_exception exception = { };
1978 err = nfs4_handle_exception(server,
1979 _nfs4_proc_pathconf(server, fhandle, pathconf),
1981 } while (exception.retry);
1986 nfs4_read_done(struct rpc_task *task)
1988 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
1989 struct inode *inode = data->inode;
1991 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1992 rpc_restart_call(task);
1995 if (task->tk_status > 0)
1996 renew_lease(NFS_SERVER(inode), data->timestamp);
1997 /* Call back common NFS readpage processing */
1998 nfs_readpage_result(task);
2002 nfs4_proc_read_setup(struct nfs_read_data *data)
2004 struct rpc_task *task = &data->task;
2005 struct rpc_message msg = {
2006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2007 .rpc_argp = &data->args,
2008 .rpc_resp = &data->res,
2009 .rpc_cred = data->cred,
2011 struct inode *inode = data->inode;
2014 data->timestamp = jiffies;
2016 /* N.B. Do we need to test? Never called for swapfile inode */
2017 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2019 /* Finalize the task. */
2020 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
2021 rpc_call_setup(task, &msg, 0);
2025 nfs4_write_done(struct rpc_task *task)
2027 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2028 struct inode *inode = data->inode;
2030 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2031 rpc_restart_call(task);
2034 if (task->tk_status >= 0)
2035 renew_lease(NFS_SERVER(inode), data->timestamp);
2036 /* Call back common NFS writeback processing */
2037 nfs_writeback_done(task);
2041 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2043 struct rpc_task *task = &data->task;
2044 struct rpc_message msg = {
2045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2046 .rpc_argp = &data->args,
2047 .rpc_resp = &data->res,
2048 .rpc_cred = data->cred,
2050 struct inode *inode = data->inode;
2054 if (how & FLUSH_STABLE) {
2055 if (!NFS_I(inode)->ncommit)
2056 stable = NFS_FILE_SYNC;
2058 stable = NFS_DATA_SYNC;
2060 stable = NFS_UNSTABLE;
2061 data->args.stable = stable;
2063 data->timestamp = jiffies;
2065 /* Set the initial flags for the task. */
2066 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2068 /* Finalize the task. */
2069 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2070 rpc_call_setup(task, &msg, 0);
2074 nfs4_commit_done(struct rpc_task *task)
2076 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2077 struct inode *inode = data->inode;
2079 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2080 rpc_restart_call(task);
2083 /* Call back common NFS writeback processing */
2084 nfs_commit_done(task);
2088 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2090 struct rpc_task *task = &data->task;
2091 struct rpc_message msg = {
2092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2093 .rpc_argp = &data->args,
2094 .rpc_resp = &data->res,
2095 .rpc_cred = data->cred,
2097 struct inode *inode = data->inode;
2100 /* Set the initial flags for the task. */
2101 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2103 /* Finalize the task. */
2104 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2105 rpc_call_setup(task, &msg, 0);
2109 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2110 * standalone procedure for queueing an asynchronous RENEW.
2113 renew_done(struct rpc_task *task)
2115 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2116 unsigned long timestamp = (unsigned long)task->tk_calldata;
2118 if (task->tk_status < 0) {
2119 switch (task->tk_status) {
2120 case -NFS4ERR_STALE_CLIENTID:
2121 case -NFS4ERR_EXPIRED:
2122 case -NFS4ERR_CB_PATH_DOWN:
2123 nfs4_schedule_state_recovery(clp);
2127 spin_lock(&clp->cl_lock);
2128 if (time_before(clp->cl_last_renewal,timestamp))
2129 clp->cl_last_renewal = timestamp;
2130 spin_unlock(&clp->cl_lock);
2134 nfs4_proc_async_renew(struct nfs4_client *clp)
2136 struct rpc_message msg = {
2137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2139 .rpc_cred = clp->cl_cred,
2142 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2143 renew_done, (void *)jiffies);
2147 nfs4_proc_renew(struct nfs4_client *clp)
2149 struct rpc_message msg = {
2150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2152 .rpc_cred = clp->cl_cred,
2154 unsigned long now = jiffies;
2157 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2160 spin_lock(&clp->cl_lock);
2161 if (time_before(clp->cl_last_renewal,now))
2162 clp->cl_last_renewal = now;
2163 spin_unlock(&clp->cl_lock);
2168 * We will need to arrange for the VFS layer to provide an atomic open.
2169 * Until then, this open method is prone to inefficiency and race conditions
2170 * due to the lookup, potential create, and open VFS calls from sys_open()
2171 * placed on the wire.
2174 nfs4_proc_file_open(struct inode *inode, struct file *filp)
2176 struct dentry *dentry = filp->f_dentry;
2177 struct nfs_open_context *ctx;
2178 struct nfs4_state *state = NULL;
2179 struct rpc_cred *cred;
2180 int status = -ENOMEM;
2182 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2183 (int)dentry->d_parent->d_name.len,
2184 dentry->d_parent->d_name.name,
2185 (int)dentry->d_name.len, dentry->d_name.name);
2188 /* Find our open stateid */
2189 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
2191 return PTR_ERR(cred);
2192 ctx = alloc_nfs_open_context(dentry, cred);
2194 if (unlikely(ctx == NULL))
2196 status = -EIO; /* ERACE actually */
2197 state = nfs4_find_state(inode, cred, filp->f_mode);
2198 if (unlikely(state == NULL))
2201 nfs4_close_state(state, filp->f_mode);
2202 ctx->mode = filp->f_mode;
2203 nfs_file_set_open_context(filp, ctx);
2204 put_nfs_open_context(ctx);
2205 if (filp->f_mode & FMODE_WRITE)
2206 nfs_begin_data_update(inode);
2209 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
2210 put_nfs_open_context(ctx);
2218 nfs4_proc_file_release(struct inode *inode, struct file *filp)
2220 if (filp->f_mode & FMODE_WRITE)
2221 nfs_end_data_update(inode);
2222 nfs_file_clear_open_context(filp);
2226 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2228 return (server->caps & NFS_CAP_ACLS)
2229 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2230 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2233 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2234 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2237 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2239 static void buf_to_pages(const void *buf, size_t buflen,
2240 struct page **pages, unsigned int *pgbase)
2242 const void *p = buf;
2244 *pgbase = offset_in_page(buf);
2246 while (p < buf + buflen) {
2247 *(pages++) = virt_to_page(p);
2248 p += PAGE_CACHE_SIZE;
2252 struct nfs4_cached_acl {
2258 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2260 struct nfs_inode *nfsi = NFS_I(inode);
2262 spin_lock(&inode->i_lock);
2263 kfree(nfsi->nfs4_acl);
2264 nfsi->nfs4_acl = acl;
2265 spin_unlock(&inode->i_lock);
2268 static void nfs4_zap_acl_attr(struct inode *inode)
2270 nfs4_set_cached_acl(inode, NULL);
2273 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2275 struct nfs_inode *nfsi = NFS_I(inode);
2276 struct nfs4_cached_acl *acl;
2279 spin_lock(&inode->i_lock);
2280 acl = nfsi->nfs4_acl;
2283 if (buf == NULL) /* user is just asking for length */
2285 if (acl->cached == 0)
2287 ret = -ERANGE; /* see getxattr(2) man page */
2288 if (acl->len > buflen)
2290 memcpy(buf, acl->data, acl->len);
2294 spin_unlock(&inode->i_lock);
2298 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2300 struct nfs4_cached_acl *acl;
2302 if (buf && acl_len <= PAGE_SIZE) {
2303 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2307 memcpy(acl->data, buf, acl_len);
2309 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2316 nfs4_set_cached_acl(inode, acl);
2319 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2321 struct page *pages[NFS4ACL_MAXPAGES];
2322 struct nfs_getaclargs args = {
2323 .fh = NFS_FH(inode),
2327 size_t resp_len = buflen;
2329 struct rpc_message msg = {
2330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2332 .rpc_resp = &resp_len,
2334 struct page *localpage = NULL;
2337 if (buflen < PAGE_SIZE) {
2338 /* As long as we're doing a round trip to the server anyway,
2339 * let's be prepared for a page of acl data. */
2340 localpage = alloc_page(GFP_KERNEL);
2341 resp_buf = page_address(localpage);
2342 if (localpage == NULL)
2344 args.acl_pages[0] = localpage;
2345 args.acl_pgbase = 0;
2346 args.acl_len = PAGE_SIZE;
2349 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2351 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2354 if (resp_len > args.acl_len)
2355 nfs4_write_cached_acl(inode, NULL, resp_len);
2357 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2360 if (resp_len > buflen)
2363 memcpy(buf, resp_buf, resp_len);
2368 __free_page(localpage);
2372 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2374 struct nfs_server *server = NFS_SERVER(inode);
2377 if (!nfs4_server_supports_acls(server))
2379 ret = nfs_revalidate_inode(server, inode);
2382 ret = nfs4_read_cached_acl(inode, buf, buflen);
2385 return nfs4_get_acl_uncached(inode, buf, buflen);
2388 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2390 struct nfs_server *server = NFS_SERVER(inode);
2391 struct page *pages[NFS4ACL_MAXPAGES];
2392 struct nfs_setaclargs arg = {
2393 .fh = NFS_FH(inode),
2397 struct rpc_message msg = {
2398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2404 if (!nfs4_server_supports_acls(server))
2406 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2407 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2409 nfs4_write_cached_acl(inode, buf, buflen);
2414 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
2416 struct nfs4_client *clp = server->nfs4_state;
2418 if (!clp || task->tk_status >= 0)
2420 switch(task->tk_status) {
2421 case -NFS4ERR_STALE_CLIENTID:
2422 case -NFS4ERR_STALE_STATEID:
2423 case -NFS4ERR_EXPIRED:
2424 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2425 nfs4_schedule_state_recovery(clp);
2426 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2427 rpc_wake_up_task(task);
2428 task->tk_status = 0;
2430 case -NFS4ERR_GRACE:
2431 case -NFS4ERR_DELAY:
2432 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2433 task->tk_status = 0;
2435 case -NFS4ERR_OLD_STATEID:
2436 task->tk_status = 0;
2439 task->tk_status = nfs4_map_errors(task->tk_status);
2443 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2447 int interruptible, res = 0;
2451 rpc_clnt_sigmask(clnt, &oldset);
2452 interruptible = TASK_UNINTERRUPTIBLE;
2454 interruptible = TASK_INTERRUPTIBLE;
2455 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2456 nfs4_schedule_state_recovery(clp);
2457 if (clnt->cl_intr && signalled())
2459 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2461 finish_wait(&clp->cl_waitq, &wait);
2462 rpc_clnt_sigunmask(clnt, &oldset);
2466 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2474 *timeout = NFS4_POLL_RETRY_MIN;
2475 if (*timeout > NFS4_POLL_RETRY_MAX)
2476 *timeout = NFS4_POLL_RETRY_MAX;
2477 rpc_clnt_sigmask(clnt, &oldset);
2478 if (clnt->cl_intr) {
2479 schedule_timeout_interruptible(*timeout);
2483 schedule_timeout_uninterruptible(*timeout);
2484 rpc_clnt_sigunmask(clnt, &oldset);
2489 /* This is the error handling routine for processes that are allowed
2492 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2494 struct nfs4_client *clp = server->nfs4_state;
2495 int ret = errorcode;
2497 exception->retry = 0;
2501 case -NFS4ERR_STALE_CLIENTID:
2502 case -NFS4ERR_STALE_STATEID:
2503 case -NFS4ERR_EXPIRED:
2504 ret = nfs4_wait_clnt_recover(server->client, clp);
2506 exception->retry = 1;
2508 case -NFS4ERR_GRACE:
2509 case -NFS4ERR_DELAY:
2510 ret = nfs4_delay(server->client, &exception->timeout);
2512 exception->retry = 1;
2514 case -NFS4ERR_OLD_STATEID:
2516 exception->retry = 1;
2518 /* We failed to handle the error */
2519 return nfs4_map_errors(ret);
2522 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2524 nfs4_verifier sc_verifier;
2525 struct nfs4_setclientid setclientid = {
2526 .sc_verifier = &sc_verifier,
2529 struct rpc_message msg = {
2530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2531 .rpc_argp = &setclientid,
2533 .rpc_cred = clp->cl_cred,
2539 p = (u32*)sc_verifier.data;
2540 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2541 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2544 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2545 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2546 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2547 clp->cl_cred->cr_ops->cr_name,
2548 clp->cl_id_uniquifier);
2549 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2550 sizeof(setclientid.sc_netid), "tcp");
2551 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2552 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2553 clp->cl_ipaddr, port >> 8, port & 255);
2555 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2556 if (status != -NFS4ERR_CLID_INUSE)
2561 ssleep(clp->cl_lease_time + 1);
2563 if (++clp->cl_id_uniquifier == 0)
2570 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2572 struct nfs_fsinfo fsinfo;
2573 struct rpc_message msg = {
2574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2576 .rpc_resp = &fsinfo,
2577 .rpc_cred = clp->cl_cred,
2583 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2585 spin_lock(&clp->cl_lock);
2586 clp->cl_lease_time = fsinfo.lease_time * HZ;
2587 clp->cl_last_renewal = now;
2588 spin_unlock(&clp->cl_lock);
2593 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2595 struct nfs4_delegreturnargs args = {
2596 .fhandle = NFS_FH(inode),
2599 struct rpc_message msg = {
2600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2605 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2608 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2610 struct nfs_server *server = NFS_SERVER(inode);
2611 struct nfs4_exception exception = { };
2614 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2616 case -NFS4ERR_STALE_STATEID:
2617 case -NFS4ERR_EXPIRED:
2618 nfs4_schedule_state_recovery(server->nfs4_state);
2622 err = nfs4_handle_exception(server, err, &exception);
2623 } while (exception.retry);
2627 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2628 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2631 * sleep, with exponential backoff, and retry the LOCK operation.
2633 static unsigned long
2634 nfs4_set_lock_task_retry(unsigned long timeout)
2636 schedule_timeout_interruptible(timeout);
2638 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2639 return NFS4_LOCK_MAXTIMEOUT;
2644 nfs4_lck_type(int cmd, struct file_lock *request)
2647 switch (request->fl_type) {
2649 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2651 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2653 return NFS4_WRITE_LT;
2659 static inline uint64_t
2660 nfs4_lck_length(struct file_lock *request)
2662 if (request->fl_end == OFFSET_MAX)
2663 return ~(uint64_t)0;
2664 return request->fl_end - request->fl_start + 1;
2667 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2669 struct inode *inode = state->inode;
2670 struct nfs_server *server = NFS_SERVER(inode);
2671 struct nfs4_client *clp = server->nfs4_state;
2672 struct nfs_lockargs arg = {
2673 .fh = NFS_FH(inode),
2674 .type = nfs4_lck_type(cmd, request),
2675 .offset = request->fl_start,
2676 .length = nfs4_lck_length(request),
2678 struct nfs_lockres res = {
2681 struct rpc_message msg = {
2682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2685 .rpc_cred = state->owner->so_cred,
2687 struct nfs_lowner nlo;
2688 struct nfs4_lock_state *lsp;
2691 down_read(&clp->cl_sem);
2692 nlo.clientid = clp->cl_clientid;
2693 status = nfs4_set_lock_state(state, request);
2696 lsp = request->fl_u.nfs4_fl.owner;
2697 nlo.id = lsp->ls_id;
2699 status = rpc_call_sync(server->client, &msg, 0);
2701 request->fl_type = F_UNLCK;
2702 } else if (status == -NFS4ERR_DENIED) {
2703 int64_t len, start, end;
2704 start = res.u.denied.offset;
2705 len = res.u.denied.length;
2706 end = start + len - 1;
2707 if (end < 0 || len == 0)
2708 request->fl_end = OFFSET_MAX;
2710 request->fl_end = (loff_t)end;
2711 request->fl_start = (loff_t)start;
2712 request->fl_type = F_WRLCK;
2713 if (res.u.denied.type & 1)
2714 request->fl_type = F_RDLCK;
2715 request->fl_pid = 0;
2719 up_read(&clp->cl_sem);
2723 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2725 struct nfs4_exception exception = { };
2729 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2730 _nfs4_proc_getlk(state, cmd, request),
2732 } while (exception.retry);
2736 static int do_vfs_lock(struct file *file, struct file_lock *fl)
2739 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2741 res = posix_lock_file_wait(file, fl);
2744 res = flock_lock_file_wait(file, fl);
2750 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2755 static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2757 struct inode *inode = state->inode;
2758 struct nfs_server *server = NFS_SERVER(inode);
2759 struct nfs_lockargs arg = {
2760 .fh = NFS_FH(inode),
2761 .type = nfs4_lck_type(cmd, request),
2762 .offset = request->fl_start,
2763 .length = nfs4_lck_length(request),
2765 struct nfs_lockres res = {
2768 struct rpc_message msg = {
2769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2772 .rpc_cred = state->owner->so_cred,
2774 struct nfs4_lock_state *lsp;
2775 struct nfs_locku_opargs luargs;
2778 status = nfs4_set_lock_state(state, request);
2781 lsp = request->fl_u.nfs4_fl.owner;
2782 /* We might have lost the locks! */
2783 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2785 luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2787 if (luargs.seqid == NULL)
2789 memcpy(luargs.stateid.data, lsp->ls_stateid.data, sizeof(luargs.stateid.data));
2790 arg.u.locku = &luargs;
2791 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2792 nfs_increment_lock_seqid(status, luargs.seqid);
2795 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
2796 sizeof(lsp->ls_stateid.data));
2797 nfs_free_seqid(luargs.seqid);
2800 do_vfs_lock(request->fl_file, request);
2804 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2806 struct nfs4_exception exception = { };
2810 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2811 _nfs4_proc_unlck(state, cmd, request),
2813 } while (exception.retry);
2817 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2819 struct inode *inode = state->inode;
2820 struct nfs_server *server = NFS_SERVER(inode);
2821 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
2822 struct nfs_lockargs arg = {
2823 .fh = NFS_FH(inode),
2824 .type = nfs4_lck_type(cmd, request),
2825 .offset = request->fl_start,
2826 .length = nfs4_lck_length(request),
2828 struct nfs_lockres res = {
2831 struct rpc_message msg = {
2832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2835 .rpc_cred = state->owner->so_cred,
2837 struct nfs_lock_opargs largs = {
2839 .new_lock_owner = 0,
2841 struct nfs_seqid *lock_seqid;
2842 int status = -ENOMEM;
2844 lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2845 if (lock_seqid == NULL)
2847 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
2848 struct nfs4_state_owner *owner = state->owner;
2849 struct nfs_open_to_lock otl = {
2851 .clientid = server->nfs4_state->cl_clientid,
2855 otl.lock_seqid = lock_seqid;
2856 otl.lock_owner.id = lsp->ls_id;
2857 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
2858 largs.u.open_lock = &otl;
2859 largs.new_lock_owner = 1;
2860 arg.u.lock = &largs;
2861 otl.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
2862 if (otl.open_seqid != NULL) {
2863 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2864 /* increment seqid on success, and seqid mutating errors */
2865 nfs_increment_open_seqid(status, otl.open_seqid);
2866 nfs_free_seqid(otl.open_seqid);
2869 nfs_confirm_seqid(&lsp->ls_seqid, 0);
2871 struct nfs_exist_lock el;
2872 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
2873 largs.u.exist_lock = ⪙
2874 arg.u.lock = &largs;
2875 el.seqid = lock_seqid;
2876 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2878 /* increment seqid on success, and seqid mutating errors*/
2879 nfs_increment_lock_seqid(status, lock_seqid);
2880 /* save the returned stateid. */
2882 memcpy(lsp->ls_stateid.data, res.u.stateid.data, sizeof(lsp->ls_stateid.data));
2883 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2884 } else if (status == -NFS4ERR_DENIED)
2886 nfs_free_seqid(lock_seqid);
2890 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2892 struct nfs_server *server = NFS_SERVER(state->inode);
2893 struct nfs4_exception exception = { };
2897 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
2898 if (err != -NFS4ERR_DELAY)
2900 nfs4_handle_exception(server, err, &exception);
2901 } while (exception.retry);
2905 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2907 struct nfs_server *server = NFS_SERVER(state->inode);
2908 struct nfs4_exception exception = { };
2912 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
2913 if (err != -NFS4ERR_DELAY)
2915 nfs4_handle_exception(server, err, &exception);
2916 } while (exception.retry);
2920 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2922 struct nfs4_client *clp = state->owner->so_client;
2925 down_read(&clp->cl_sem);
2926 status = nfs4_set_lock_state(state, request);
2928 status = _nfs4_do_setlk(state, cmd, request, 0);
2930 /* Note: we always want to sleep here! */
2931 request->fl_flags |= FL_SLEEP;
2932 if (do_vfs_lock(request->fl_file, request) < 0)
2933 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
2935 up_read(&clp->cl_sem);
2939 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2941 struct nfs4_exception exception = { };
2945 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2946 _nfs4_proc_setlk(state, cmd, request),
2948 } while (exception.retry);
2953 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
2955 struct nfs_open_context *ctx;
2956 struct nfs4_state *state;
2957 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
2960 /* verify open state */
2961 ctx = (struct nfs_open_context *)filp->private_data;
2964 if (request->fl_start < 0 || request->fl_end < 0)
2968 return nfs4_proc_getlk(state, F_GETLK, request);
2970 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
2973 if (request->fl_type == F_UNLCK)
2974 return nfs4_proc_unlck(state, cmd, request);
2977 status = nfs4_proc_setlk(state, cmd, request);
2978 if ((status != -EAGAIN) || IS_SETLK(cmd))
2980 timeout = nfs4_set_lock_task_retry(timeout);
2981 status = -ERESTARTSYS;
2984 } while(status < 0);
2989 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
2991 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
2992 size_t buflen, int flags)
2994 struct inode *inode = dentry->d_inode;
2996 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
2999 if (!S_ISREG(inode->i_mode) &&
3000 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3003 return nfs4_proc_set_acl(inode, buf, buflen);
3006 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3007 * and that's what we'll do for e.g. user attributes that haven't been set.
3008 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3009 * attributes in kernel-managed attribute namespaces. */
3010 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3013 struct inode *inode = dentry->d_inode;
3015 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3018 return nfs4_proc_get_acl(inode, buf, buflen);
3021 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3023 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3025 if (buf && buflen < len)
3028 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3032 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3033 .recover_open = nfs4_open_reclaim,
3034 .recover_lock = nfs4_lock_reclaim,
3037 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3038 .recover_open = nfs4_open_expired,
3039 .recover_lock = nfs4_lock_expired,
3042 static struct inode_operations nfs4_file_inode_operations = {
3043 .permission = nfs_permission,
3044 .getattr = nfs_getattr,
3045 .setattr = nfs_setattr,
3046 .getxattr = nfs4_getxattr,
3047 .setxattr = nfs4_setxattr,
3048 .listxattr = nfs4_listxattr,
3051 struct nfs_rpc_ops nfs_v4_clientops = {
3052 .version = 4, /* protocol version */
3053 .dentry_ops = &nfs4_dentry_operations,
3054 .dir_inode_ops = &nfs4_dir_inode_operations,
3055 .file_inode_ops = &nfs4_file_inode_operations,
3056 .getroot = nfs4_proc_get_root,
3057 .getattr = nfs4_proc_getattr,
3058 .setattr = nfs4_proc_setattr,
3059 .lookup = nfs4_proc_lookup,
3060 .access = nfs4_proc_access,
3061 .readlink = nfs4_proc_readlink,
3062 .read = nfs4_proc_read,
3063 .write = nfs4_proc_write,
3064 .commit = nfs4_proc_commit,
3065 .create = nfs4_proc_create,
3066 .remove = nfs4_proc_remove,
3067 .unlink_setup = nfs4_proc_unlink_setup,
3068 .unlink_done = nfs4_proc_unlink_done,
3069 .rename = nfs4_proc_rename,
3070 .link = nfs4_proc_link,
3071 .symlink = nfs4_proc_symlink,
3072 .mkdir = nfs4_proc_mkdir,
3073 .rmdir = nfs4_proc_remove,
3074 .readdir = nfs4_proc_readdir,
3075 .mknod = nfs4_proc_mknod,
3076 .statfs = nfs4_proc_statfs,
3077 .fsinfo = nfs4_proc_fsinfo,
3078 .pathconf = nfs4_proc_pathconf,
3079 .decode_dirent = nfs4_decode_dirent,
3080 .read_setup = nfs4_proc_read_setup,
3081 .write_setup = nfs4_proc_write_setup,
3082 .commit_setup = nfs4_proc_commit_setup,
3083 .file_open = nfs4_proc_file_open,
3084 .file_release = nfs4_proc_file_release,
3085 .lock = nfs4_proc_lock,
3086 .clear_acl_cache = nfs4_zap_acl_attr,