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 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
194 struct inode *inode = state->inode;
196 open_flags &= (FMODE_READ|FMODE_WRITE);
197 /* Protect against nfs4_find_state() */
198 spin_lock(&inode->i_lock);
199 state->state |= open_flags;
200 /* NB! List reordering - see the reclaim code for why. */
201 if ((open_flags & FMODE_WRITE) && 0 == state->nwriters++)
202 list_move(&state->open_states, &state->owner->so_states);
203 if (open_flags & FMODE_READ)
205 memcpy(&state->stateid, stateid, sizeof(state->stateid));
206 spin_unlock(&inode->i_lock);
211 * reclaim state on the server after a reboot.
212 * Assumes caller is holding the sp->so_sem
214 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
216 struct inode *inode = state->inode;
217 struct nfs_server *server = NFS_SERVER(inode);
218 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
219 struct nfs_openargs o_arg = {
222 .open_flags = state->state,
223 .clientid = server->nfs4_state->cl_clientid,
224 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
225 .bitmask = server->attr_bitmask,
227 struct nfs_openres o_res = {
228 .server = server, /* Grrr */
230 struct rpc_message msg = {
231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
234 .rpc_cred = sp->so_cred,
238 if (delegation != NULL) {
239 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
240 memcpy(&state->stateid, &delegation->stateid,
241 sizeof(state->stateid));
242 set_bit(NFS_DELEGATED_STATE, &state->flags);
245 o_arg.u.delegation_type = delegation->type;
247 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
248 if (o_arg.seqid == NULL)
250 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
251 /* Confirm the sequence as being established */
252 nfs_confirm_seqid(&sp->so_seqid, status);
253 nfs_increment_open_seqid(status, o_arg.seqid);
255 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
256 if (o_res.delegation_type != 0) {
257 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
258 /* Did the server issue an immediate delegation recall? */
260 nfs_async_inode_return_delegation(inode, &o_res.stateid);
263 nfs_free_seqid(o_arg.seqid);
264 clear_bit(NFS_DELEGATED_STATE, &state->flags);
265 /* Ensure we update the inode attributes */
270 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
272 struct nfs_server *server = NFS_SERVER(state->inode);
273 struct nfs4_exception exception = { };
276 err = _nfs4_open_reclaim(sp, state);
277 if (err != -NFS4ERR_DELAY)
279 nfs4_handle_exception(server, err, &exception);
280 } while (exception.retry);
284 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
286 struct nfs4_state_owner *sp = state->owner;
287 struct inode *inode = dentry->d_inode;
288 struct nfs_server *server = NFS_SERVER(inode);
289 struct dentry *parent = dget_parent(dentry);
290 struct nfs_openargs arg = {
291 .fh = NFS_FH(parent->d_inode),
292 .clientid = server->nfs4_state->cl_clientid,
293 .name = &dentry->d_name,
296 .bitmask = server->attr_bitmask,
297 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
299 struct nfs_openres res = {
302 struct rpc_message msg = {
303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
306 .rpc_cred = sp->so_cred,
311 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
313 if (state->state == 0)
315 arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
317 if (arg.seqid == NULL)
319 arg.open_flags = state->state;
320 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
321 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
322 nfs_increment_open_seqid(status, arg.seqid);
324 memcpy(state->stateid.data, res.stateid.data,
325 sizeof(state->stateid.data));
326 clear_bit(NFS_DELEGATED_STATE, &state->flags);
328 nfs_free_seqid(arg.seqid);
335 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
337 struct nfs4_exception exception = { };
338 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
341 err = _nfs4_open_delegation_recall(dentry, state);
345 case -NFS4ERR_STALE_CLIENTID:
346 case -NFS4ERR_STALE_STATEID:
347 case -NFS4ERR_EXPIRED:
348 /* Don't recall a delegation if it was lost */
349 nfs4_schedule_state_recovery(server->nfs4_state);
352 err = nfs4_handle_exception(server, err, &exception);
353 } while (exception.retry);
357 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)
359 struct nfs_open_confirmargs arg = {
364 struct nfs_open_confirmres res;
365 struct rpc_message msg = {
366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
369 .rpc_cred = sp->so_cred,
373 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
374 /* Confirm the sequence as being established */
375 nfs_confirm_seqid(&sp->so_seqid, status);
376 nfs_increment_open_seqid(status, seqid);
378 memcpy(stateid, &res.stateid, sizeof(*stateid));
382 static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
384 struct nfs_server *server = NFS_SERVER(dir);
385 struct rpc_message msg = {
386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
389 .rpc_cred = sp->so_cred,
393 /* Update sequence id. The caller must serialize! */
394 o_arg->id = sp->so_id;
395 o_arg->clientid = sp->so_client->cl_clientid;
397 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
398 nfs_increment_open_seqid(status, o_arg->seqid);
401 update_changeattr(dir, &o_res->cinfo);
402 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
403 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
404 sp, &o_res->stateid, o_arg->seqid);
408 nfs_confirm_seqid(&sp->so_seqid, 0);
409 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
410 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
415 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
417 struct nfs_access_entry cache;
421 if (openflags & FMODE_READ)
423 if (openflags & FMODE_WRITE)
425 status = nfs_access_get_cached(inode, cred, &cache);
429 /* Be clever: ask server to check for all possible rights */
430 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
432 cache.jiffies = jiffies;
433 status = _nfs4_proc_access(inode, &cache);
436 nfs_access_add_cache(inode, &cache);
438 if ((cache.mask & mask) == mask)
445 * reclaim state on the server after a network partition.
446 * Assumes caller holds the appropriate lock
448 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
450 struct dentry *parent = dget_parent(dentry);
451 struct inode *dir = parent->d_inode;
452 struct inode *inode = state->inode;
453 struct nfs_server *server = NFS_SERVER(dir);
454 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
455 struct nfs_fattr f_attr = {
458 struct nfs_openargs o_arg = {
460 .open_flags = state->state,
461 .name = &dentry->d_name,
462 .bitmask = server->attr_bitmask,
463 .claim = NFS4_OPEN_CLAIM_NULL,
465 struct nfs_openres o_res = {
471 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
472 status = _nfs4_do_access(inode, sp->so_cred, state->state);
475 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
476 set_bit(NFS_DELEGATED_STATE, &state->flags);
479 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
481 if (o_arg.seqid == NULL)
483 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
486 /* Check if files differ */
487 if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
489 /* Has the file handle changed? */
490 if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
491 /* Verify if the change attributes are the same */
492 if (f_attr.change_attr != NFS_I(inode)->change_attr)
494 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
496 /* Lets just pretend that this is the same file */
497 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
498 NFS_I(inode)->fileid = f_attr.fileid;
500 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
501 if (o_res.delegation_type != 0) {
502 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
503 nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
505 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
508 nfs_free_seqid(o_arg.seqid);
509 clear_bit(NFS_DELEGATED_STATE, &state->flags);
515 /* Invalidate the state owner so we don't ever use it again */
516 nfs4_drop_state_owner(sp);
518 /* Should we be trying to close that stateid? */
522 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
524 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
525 struct nfs4_exception exception = { };
529 err = _nfs4_open_expired(sp, state, dentry);
530 if (err == -NFS4ERR_DELAY)
531 nfs4_handle_exception(server, err, &exception);
532 } while (exception.retry);
536 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
538 struct nfs_inode *nfsi = NFS_I(state->inode);
539 struct nfs_open_context *ctx;
542 spin_lock(&state->inode->i_lock);
543 list_for_each_entry(ctx, &nfsi->open_files, list) {
544 if (ctx->state != state)
546 get_nfs_open_context(ctx);
547 spin_unlock(&state->inode->i_lock);
548 status = nfs4_do_open_expired(sp, state, ctx->dentry);
549 put_nfs_open_context(ctx);
552 spin_unlock(&state->inode->i_lock);
557 * Returns an nfs4_state + an extra reference to the inode
559 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
561 struct nfs_delegation *delegation;
562 struct nfs_server *server = NFS_SERVER(inode);
563 struct nfs4_client *clp = server->nfs4_state;
564 struct nfs_inode *nfsi = NFS_I(inode);
565 struct nfs4_state_owner *sp = NULL;
566 struct nfs4_state *state = NULL;
567 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
570 /* Protect against reboot recovery - NOTE ORDER! */
571 down_read(&clp->cl_sem);
572 /* Protect against delegation recall */
573 down_read(&nfsi->rwsem);
574 delegation = NFS_I(inode)->delegation;
576 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
579 if (!(sp = nfs4_get_state_owner(server, cred))) {
580 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
584 state = nfs4_get_open_state(inode, sp);
589 if ((state->state & open_flags) == open_flags) {
590 spin_lock(&inode->i_lock);
591 if (open_flags & FMODE_READ)
593 if (open_flags & FMODE_WRITE)
595 spin_unlock(&inode->i_lock);
597 } else if (state->state != 0)
601 err = _nfs4_do_access(inode, cred, open_flags);
605 set_bit(NFS_DELEGATED_STATE, &state->flags);
606 update_open_stateid(state, &delegation->stateid, open_flags);
609 nfs4_put_state_owner(sp);
610 up_read(&nfsi->rwsem);
611 up_read(&clp->cl_sem);
618 nfs4_put_open_state(state);
620 nfs4_put_state_owner(sp);
622 up_read(&nfsi->rwsem);
623 up_read(&clp->cl_sem);
627 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
629 struct nfs4_exception exception = { };
630 struct nfs4_state *res;
634 err = _nfs4_open_delegated(inode, flags, cred, &res);
637 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
639 } while (exception.retry);
644 * Returns an nfs4_state + an referenced inode
646 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
648 struct nfs4_state_owner *sp;
649 struct nfs4_state *state = NULL;
650 struct nfs_server *server = NFS_SERVER(dir);
651 struct nfs4_client *clp = server->nfs4_state;
652 struct inode *inode = NULL;
654 struct nfs_fattr f_attr = {
657 struct nfs_openargs o_arg = {
660 .name = &dentry->d_name,
662 .bitmask = server->attr_bitmask,
663 .claim = NFS4_OPEN_CLAIM_NULL,
665 struct nfs_openres o_res = {
670 /* Protect against reboot recovery conflicts */
671 down_read(&clp->cl_sem);
673 if (!(sp = nfs4_get_state_owner(server, cred))) {
674 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
677 if (flags & O_EXCL) {
678 u32 *p = (u32 *) o_arg.u.verifier.data;
682 o_arg.u.attrs = sattr;
683 /* Serialization for the sequence id */
686 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
687 if (o_arg.seqid == NULL)
689 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
694 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
697 state = nfs4_get_open_state(inode, sp);
700 update_open_stateid(state, &o_res.stateid, flags);
701 if (o_res.delegation_type != 0)
702 nfs_inode_set_delegation(inode, cred, &o_res);
703 nfs_free_seqid(o_arg.seqid);
705 nfs4_put_state_owner(sp);
706 up_read(&clp->cl_sem);
712 nfs4_put_open_state(state);
713 nfs_free_seqid(o_arg.seqid);
715 nfs4_put_state_owner(sp);
717 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
718 up_read(&clp->cl_sem);
726 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
728 struct nfs4_exception exception = { };
729 struct nfs4_state *res;
733 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
736 /* NOTE: BAD_SEQID means the server and client disagree about the
737 * book-keeping w.r.t. state-changing operations
738 * (OPEN/CLOSE/LOCK/LOCKU...)
739 * It is actually a sign of a bug on the client or on the server.
741 * If we receive a BAD_SEQID error in the particular case of
742 * doing an OPEN, we assume that nfs_increment_open_seqid() will
743 * have unhashed the old state_owner for us, and that we can
744 * therefore safely retry using a new one. We should still warn
747 if (status == -NFS4ERR_BAD_SEQID) {
748 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
752 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
753 status, &exception));
754 } while (exception.retry);
758 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
759 struct nfs_fh *fhandle, struct iattr *sattr,
760 struct nfs4_state *state)
762 struct nfs_setattrargs arg = {
766 .bitmask = server->attr_bitmask,
768 struct nfs_setattrres res = {
772 struct rpc_message msg = {
773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
782 msg.rpc_cred = state->owner->so_cred;
783 nfs4_copy_stateid(&arg.stateid, state, current->files);
785 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
787 status = rpc_call_sync(server->client, &msg, 0);
791 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
792 struct nfs_fh *fhandle, struct iattr *sattr,
793 struct nfs4_state *state)
795 struct nfs4_exception exception = { };
798 err = nfs4_handle_exception(server,
799 _nfs4_do_setattr(server, fattr, fhandle, sattr,
802 } while (exception.retry);
806 struct nfs4_closedata {
808 struct nfs4_state *state;
809 struct nfs_closeargs arg;
810 struct nfs_closeres res;
813 static void nfs4_close_done(struct rpc_task *task)
815 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
816 struct nfs4_state *state = calldata->state;
817 struct nfs4_state_owner *sp = state->owner;
818 struct nfs_server *server = NFS_SERVER(calldata->inode);
820 /* hmm. we are done with the inode, and in the process of freeing
821 * the state_owner. we keep this around to process errors
823 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
824 switch (task->tk_status) {
826 memcpy(&state->stateid, &calldata->res.stateid,
827 sizeof(state->stateid));
829 case -NFS4ERR_STALE_STATEID:
830 case -NFS4ERR_EXPIRED:
831 state->state = calldata->arg.open_flags;
832 nfs4_schedule_state_recovery(server->nfs4_state);
835 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
836 rpc_restart_call(task);
840 state->state = calldata->arg.open_flags;
841 nfs4_put_open_state(state);
842 nfs_free_seqid(calldata->arg.seqid);
844 nfs4_put_state_owner(sp);
845 up_read(&server->nfs4_state->cl_sem);
849 static inline int nfs4_close_call(struct rpc_clnt *clnt, struct nfs4_closedata *calldata)
851 struct rpc_message msg = {
852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
853 .rpc_argp = &calldata->arg,
854 .rpc_resp = &calldata->res,
855 .rpc_cred = calldata->state->owner->so_cred,
857 if (calldata->arg.open_flags != 0)
858 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
859 return rpc_call_async(clnt, &msg, 0, nfs4_close_done, calldata);
863 * It is possible for data to be read/written from a mem-mapped file
864 * after the sys_close call (which hits the vfs layer as a flush).
865 * This means that we can't safely call nfsv4 close on a file until
866 * the inode is cleared. This in turn means that we are not good
867 * NFSv4 citizens - we do not indicate to the server to update the file's
868 * share state even when we are done with one of the three share
869 * stateid's in the inode.
871 * NOTE: Caller must be holding the sp->so_owner semaphore!
873 int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
875 struct nfs4_closedata *calldata;
878 /* Tell caller we're done */
879 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
883 calldata = (struct nfs4_closedata *)kmalloc(sizeof(*calldata), GFP_KERNEL);
884 if (calldata == NULL)
886 calldata->inode = inode;
887 calldata->state = state;
888 calldata->arg.fh = NFS_FH(inode);
889 /* Serialization for the sequence id */
890 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
891 if (calldata->arg.seqid == NULL) {
895 calldata->arg.open_flags = mode;
896 memcpy(&calldata->arg.stateid, &state->stateid,
897 sizeof(calldata->arg.stateid));
898 status = nfs4_close_call(NFS_SERVER(inode)->client, calldata);
900 * Return -EINPROGRESS on success in order to indicate to the
901 * caller that an asynchronous RPC call has been launched, and
902 * that it will release the semaphores on completion.
904 return (status == 0) ? -EINPROGRESS : status;
908 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
911 struct rpc_cred *cred;
912 struct nfs4_state *state;
914 if (nd->flags & LOOKUP_CREATE) {
915 attr.ia_mode = nd->intent.open.create_mode;
916 attr.ia_valid = ATTR_MODE;
917 if (!IS_POSIXACL(dir))
918 attr.ia_mode &= ~current->fs->umask;
921 BUG_ON(nd->intent.open.flags & O_CREAT);
924 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
926 return (struct inode *)cred;
927 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
930 return (struct inode *)state;
935 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
937 struct rpc_cred *cred;
938 struct nfs4_state *state;
941 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
943 return PTR_ERR(cred);
944 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
946 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
948 if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
952 inode = state->inode;
953 if (inode == dentry->d_inode) {
958 nfs4_close_state(state, openflags);
964 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
966 struct nfs4_server_caps_res res = {};
967 struct rpc_message msg = {
968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
974 status = rpc_call_sync(server->client, &msg, 0);
976 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
977 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
978 server->caps |= NFS_CAP_ACLS;
979 if (res.has_links != 0)
980 server->caps |= NFS_CAP_HARDLINKS;
981 if (res.has_symlinks != 0)
982 server->caps |= NFS_CAP_SYMLINKS;
983 server->acl_bitmask = res.acl_bitmask;
988 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
990 struct nfs4_exception exception = { };
993 err = nfs4_handle_exception(server,
994 _nfs4_server_capabilities(server, fhandle),
996 } while (exception.retry);
1000 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1001 struct nfs_fsinfo *info)
1003 struct nfs_fattr * fattr = info->fattr;
1004 struct nfs4_lookup_root_arg args = {
1005 .bitmask = nfs4_fattr_bitmap,
1007 struct nfs4_lookup_res res = {
1012 struct rpc_message msg = {
1013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1018 return rpc_call_sync(server->client, &msg, 0);
1021 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1022 struct nfs_fsinfo *info)
1024 struct nfs4_exception exception = { };
1027 err = nfs4_handle_exception(server,
1028 _nfs4_lookup_root(server, fhandle, info),
1030 } while (exception.retry);
1034 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1035 struct nfs_fsinfo *info)
1037 struct nfs_fattr * fattr = info->fattr;
1040 struct nfs4_lookup_arg args = {
1043 .bitmask = nfs4_fattr_bitmap,
1045 struct nfs4_lookup_res res = {
1050 struct rpc_message msg = {
1051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1058 * Now we do a separate LOOKUP for each component of the mount path.
1059 * The LOOKUPs are done separately so that we can conveniently
1060 * catch an ERR_WRONGSEC if it occurs along the way...
1062 status = nfs4_lookup_root(server, fhandle, info);
1066 p = server->mnt_path;
1068 struct nfs4_exception exception = { };
1075 while (*p && (*p != '/'))
1081 status = nfs4_handle_exception(server,
1082 rpc_call_sync(server->client, &msg, 0),
1084 } while (exception.retry);
1087 if (status == -ENOENT) {
1088 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1089 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1094 status = nfs4_server_capabilities(server, fhandle);
1096 status = nfs4_do_fsinfo(server, fhandle, info);
1101 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1103 struct nfs4_getattr_arg args = {
1105 .bitmask = server->attr_bitmask,
1107 struct nfs4_getattr_res res = {
1111 struct rpc_message msg = {
1112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1118 return rpc_call_sync(server->client, &msg, 0);
1121 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1123 struct nfs4_exception exception = { };
1126 err = nfs4_handle_exception(server,
1127 _nfs4_proc_getattr(server, fhandle, fattr),
1129 } while (exception.retry);
1134 * The file is not closed if it is opened due to the a request to change
1135 * the size of the file. The open call will not be needed once the
1136 * VFS layer lookup-intents are implemented.
1138 * Close is called when the inode is destroyed.
1139 * If we haven't opened the file for O_WRONLY, we
1140 * need to in the size_change case to obtain a stateid.
1143 * Because OPEN is always done by name in nfsv4, it is
1144 * possible that we opened a different file by the same
1145 * name. We can recognize this race condition, but we
1146 * can't do anything about it besides returning an error.
1148 * This will be fixed with VFS changes (lookup-intent).
1151 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1152 struct iattr *sattr)
1154 struct rpc_cred *cred;
1155 struct inode *inode = dentry->d_inode;
1156 struct nfs4_state *state;
1161 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1163 return PTR_ERR(cred);
1164 /* Search for an existing WRITE delegation first */
1165 state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
1166 if (!IS_ERR(state)) {
1167 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1170 /* Search for an existing open(O_WRITE) stateid */
1171 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1174 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1175 NFS_FH(inode), sattr, state);
1177 nfs_setattr_update_inode(inode, sattr);
1179 nfs4_close_state(state, FMODE_WRITE);
1184 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1185 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1188 struct nfs_server *server = NFS_SERVER(dir);
1189 struct nfs4_lookup_arg args = {
1190 .bitmask = server->attr_bitmask,
1191 .dir_fh = NFS_FH(dir),
1194 struct nfs4_lookup_res res = {
1199 struct rpc_message msg = {
1200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1207 dprintk("NFS call lookup %s\n", name->name);
1208 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1209 dprintk("NFS reply lookup: %d\n", status);
1213 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1215 struct nfs4_exception exception = { };
1218 err = nfs4_handle_exception(NFS_SERVER(dir),
1219 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1221 } while (exception.retry);
1225 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1227 struct nfs4_accessargs args = {
1228 .fh = NFS_FH(inode),
1230 struct nfs4_accessres res = { 0 };
1231 struct rpc_message msg = {
1232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1235 .rpc_cred = entry->cred,
1237 int mode = entry->mask;
1241 * Determine which access bits we want to ask for...
1243 if (mode & MAY_READ)
1244 args.access |= NFS4_ACCESS_READ;
1245 if (S_ISDIR(inode->i_mode)) {
1246 if (mode & MAY_WRITE)
1247 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1248 if (mode & MAY_EXEC)
1249 args.access |= NFS4_ACCESS_LOOKUP;
1251 if (mode & MAY_WRITE)
1252 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1253 if (mode & MAY_EXEC)
1254 args.access |= NFS4_ACCESS_EXECUTE;
1256 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1259 if (res.access & NFS4_ACCESS_READ)
1260 entry->mask |= MAY_READ;
1261 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1262 entry->mask |= MAY_WRITE;
1263 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1264 entry->mask |= MAY_EXEC;
1269 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1271 struct nfs4_exception exception = { };
1274 err = nfs4_handle_exception(NFS_SERVER(inode),
1275 _nfs4_proc_access(inode, entry),
1277 } while (exception.retry);
1282 * TODO: For the time being, we don't try to get any attributes
1283 * along with any of the zero-copy operations READ, READDIR,
1286 * In the case of the first three, we want to put the GETATTR
1287 * after the read-type operation -- this is because it is hard
1288 * to predict the length of a GETATTR response in v4, and thus
1289 * align the READ data correctly. This means that the GETATTR
1290 * may end up partially falling into the page cache, and we should
1291 * shift it into the 'tail' of the xdr_buf before processing.
1292 * To do this efficiently, we need to know the total length
1293 * of data received, which doesn't seem to be available outside
1296 * In the case of WRITE, we also want to put the GETATTR after
1297 * the operation -- in this case because we want to make sure
1298 * we get the post-operation mtime and size. This means that
1299 * we can't use xdr_encode_pages() as written: we need a variant
1300 * of it which would leave room in the 'tail' iovec.
1302 * Both of these changes to the XDR layer would in fact be quite
1303 * minor, but I decided to leave them for a subsequent patch.
1305 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1306 unsigned int pgbase, unsigned int pglen)
1308 struct nfs4_readlink args = {
1309 .fh = NFS_FH(inode),
1314 struct rpc_message msg = {
1315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1320 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1323 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1324 unsigned int pgbase, unsigned int pglen)
1326 struct nfs4_exception exception = { };
1329 err = nfs4_handle_exception(NFS_SERVER(inode),
1330 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1332 } while (exception.retry);
1336 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1338 int flags = rdata->flags;
1339 struct inode *inode = rdata->inode;
1340 struct nfs_fattr *fattr = rdata->res.fattr;
1341 struct nfs_server *server = NFS_SERVER(inode);
1342 struct rpc_message msg = {
1343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1344 .rpc_argp = &rdata->args,
1345 .rpc_resp = &rdata->res,
1346 .rpc_cred = rdata->cred,
1348 unsigned long timestamp = jiffies;
1351 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1352 (long long) rdata->args.offset);
1355 status = rpc_call_sync(server->client, &msg, flags);
1357 renew_lease(server, timestamp);
1358 dprintk("NFS reply read: %d\n", status);
1362 static int nfs4_proc_read(struct nfs_read_data *rdata)
1364 struct nfs4_exception exception = { };
1367 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1368 _nfs4_proc_read(rdata),
1370 } while (exception.retry);
1374 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1376 int rpcflags = wdata->flags;
1377 struct inode *inode = wdata->inode;
1378 struct nfs_fattr *fattr = wdata->res.fattr;
1379 struct nfs_server *server = NFS_SERVER(inode);
1380 struct rpc_message msg = {
1381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1382 .rpc_argp = &wdata->args,
1383 .rpc_resp = &wdata->res,
1384 .rpc_cred = wdata->cred,
1388 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1389 (long long) wdata->args.offset);
1392 status = rpc_call_sync(server->client, &msg, rpcflags);
1393 dprintk("NFS reply write: %d\n", status);
1397 static int nfs4_proc_write(struct nfs_write_data *wdata)
1399 struct nfs4_exception exception = { };
1402 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1403 _nfs4_proc_write(wdata),
1405 } while (exception.retry);
1409 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1411 struct inode *inode = cdata->inode;
1412 struct nfs_fattr *fattr = cdata->res.fattr;
1413 struct nfs_server *server = NFS_SERVER(inode);
1414 struct rpc_message msg = {
1415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1416 .rpc_argp = &cdata->args,
1417 .rpc_resp = &cdata->res,
1418 .rpc_cred = cdata->cred,
1422 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1423 (long long) cdata->args.offset);
1426 status = rpc_call_sync(server->client, &msg, 0);
1427 dprintk("NFS reply commit: %d\n", status);
1431 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1433 struct nfs4_exception exception = { };
1436 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1437 _nfs4_proc_commit(cdata),
1439 } while (exception.retry);
1445 * We will need to arrange for the VFS layer to provide an atomic open.
1446 * Until then, this create/open method is prone to inefficiency and race
1447 * conditions due to the lookup, create, and open VFS calls from sys_open()
1448 * placed on the wire.
1450 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1451 * The file will be opened again in the subsequent VFS open call
1452 * (nfs4_proc_file_open).
1454 * The open for read will just hang around to be used by any process that
1455 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1459 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1462 struct nfs4_state *state;
1463 struct rpc_cred *cred;
1466 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1468 status = PTR_ERR(cred);
1471 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1473 if (IS_ERR(state)) {
1474 status = PTR_ERR(state);
1477 d_instantiate(dentry, state->inode);
1478 if (flags & O_EXCL) {
1479 struct nfs_fattr fattr;
1480 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1481 NFS_FH(state->inode), sattr, state);
1483 nfs_setattr_update_inode(state->inode, sattr);
1486 } else if (flags != 0)
1488 nfs4_close_state(state, flags);
1493 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1495 struct nfs4_remove_arg args = {
1499 struct nfs4_change_info res;
1500 struct rpc_message msg = {
1501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1507 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1509 update_changeattr(dir, &res);
1513 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1515 struct nfs4_exception exception = { };
1518 err = nfs4_handle_exception(NFS_SERVER(dir),
1519 _nfs4_proc_remove(dir, name),
1521 } while (exception.retry);
1525 struct unlink_desc {
1526 struct nfs4_remove_arg args;
1527 struct nfs4_change_info res;
1530 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1533 struct unlink_desc *up;
1535 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1539 up->args.fh = NFS_FH(dir->d_inode);
1540 up->args.name = name;
1542 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1543 msg->rpc_argp = &up->args;
1544 msg->rpc_resp = &up->res;
1548 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1550 struct rpc_message *msg = &task->tk_msg;
1551 struct unlink_desc *up;
1553 if (msg->rpc_resp != NULL) {
1554 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1555 update_changeattr(dir->d_inode, &up->res);
1557 msg->rpc_resp = NULL;
1558 msg->rpc_argp = NULL;
1563 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1564 struct inode *new_dir, struct qstr *new_name)
1566 struct nfs4_rename_arg arg = {
1567 .old_dir = NFS_FH(old_dir),
1568 .new_dir = NFS_FH(new_dir),
1569 .old_name = old_name,
1570 .new_name = new_name,
1572 struct nfs4_rename_res res = { };
1573 struct rpc_message msg = {
1574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1580 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1583 update_changeattr(old_dir, &res.old_cinfo);
1584 update_changeattr(new_dir, &res.new_cinfo);
1589 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1590 struct inode *new_dir, struct qstr *new_name)
1592 struct nfs4_exception exception = { };
1595 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1596 _nfs4_proc_rename(old_dir, old_name,
1599 } while (exception.retry);
1603 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1605 struct nfs4_link_arg arg = {
1606 .fh = NFS_FH(inode),
1607 .dir_fh = NFS_FH(dir),
1610 struct nfs4_change_info cinfo = { };
1611 struct rpc_message msg = {
1612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1618 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1620 update_changeattr(dir, &cinfo);
1625 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1627 struct nfs4_exception exception = { };
1630 err = nfs4_handle_exception(NFS_SERVER(inode),
1631 _nfs4_proc_link(inode, dir, name),
1633 } while (exception.retry);
1637 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1638 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1639 struct nfs_fattr *fattr)
1641 struct nfs_server *server = NFS_SERVER(dir);
1642 struct nfs4_create_arg arg = {
1643 .dir_fh = NFS_FH(dir),
1648 .bitmask = server->attr_bitmask,
1650 struct nfs4_create_res res = {
1655 struct rpc_message msg = {
1656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1662 if (path->len > NFS4_MAXPATHLEN)
1663 return -ENAMETOOLONG;
1664 arg.u.symlink = path;
1667 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1669 update_changeattr(dir, &res.dir_cinfo);
1673 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1674 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1675 struct nfs_fattr *fattr)
1677 struct nfs4_exception exception = { };
1680 err = nfs4_handle_exception(NFS_SERVER(dir),
1681 _nfs4_proc_symlink(dir, name, path, sattr,
1684 } while (exception.retry);
1688 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1689 struct iattr *sattr)
1691 struct nfs_server *server = NFS_SERVER(dir);
1692 struct nfs_fh fhandle;
1693 struct nfs_fattr fattr;
1694 struct nfs4_create_arg arg = {
1695 .dir_fh = NFS_FH(dir),
1697 .name = &dentry->d_name,
1700 .bitmask = server->attr_bitmask,
1702 struct nfs4_create_res res = {
1707 struct rpc_message msg = {
1708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1716 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1718 update_changeattr(dir, &res.dir_cinfo);
1719 status = nfs_instantiate(dentry, &fhandle, &fattr);
1724 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1725 struct iattr *sattr)
1727 struct nfs4_exception exception = { };
1730 err = nfs4_handle_exception(NFS_SERVER(dir),
1731 _nfs4_proc_mkdir(dir, dentry, sattr),
1733 } while (exception.retry);
1737 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1738 u64 cookie, struct page *page, unsigned int count, int plus)
1740 struct inode *dir = dentry->d_inode;
1741 struct nfs4_readdir_arg args = {
1746 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1748 struct nfs4_readdir_res res;
1749 struct rpc_message msg = {
1750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1757 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1758 dentry->d_parent->d_name.name,
1759 dentry->d_name.name,
1760 (unsigned long long)cookie);
1762 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1763 res.pgbase = args.pgbase;
1764 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1766 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1768 dprintk("%s: returns %d\n", __FUNCTION__, status);
1772 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1773 u64 cookie, struct page *page, unsigned int count, int plus)
1775 struct nfs4_exception exception = { };
1778 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1779 _nfs4_proc_readdir(dentry, cred, cookie,
1782 } while (exception.retry);
1786 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1787 struct iattr *sattr, dev_t rdev)
1789 struct nfs_server *server = NFS_SERVER(dir);
1791 struct nfs_fattr fattr;
1792 struct nfs4_create_arg arg = {
1793 .dir_fh = NFS_FH(dir),
1795 .name = &dentry->d_name,
1797 .bitmask = server->attr_bitmask,
1799 struct nfs4_create_res res = {
1804 struct rpc_message msg = {
1805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1810 int mode = sattr->ia_mode;
1814 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1815 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1817 arg.ftype = NF4FIFO;
1818 else if (S_ISBLK(mode)) {
1820 arg.u.device.specdata1 = MAJOR(rdev);
1821 arg.u.device.specdata2 = MINOR(rdev);
1823 else if (S_ISCHR(mode)) {
1825 arg.u.device.specdata1 = MAJOR(rdev);
1826 arg.u.device.specdata2 = MINOR(rdev);
1829 arg.ftype = NF4SOCK;
1831 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1833 update_changeattr(dir, &res.dir_cinfo);
1834 status = nfs_instantiate(dentry, &fh, &fattr);
1839 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1840 struct iattr *sattr, dev_t rdev)
1842 struct nfs4_exception exception = { };
1845 err = nfs4_handle_exception(NFS_SERVER(dir),
1846 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1848 } while (exception.retry);
1852 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1853 struct nfs_fsstat *fsstat)
1855 struct nfs4_statfs_arg args = {
1857 .bitmask = server->attr_bitmask,
1859 struct rpc_message msg = {
1860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1865 fsstat->fattr->valid = 0;
1866 return rpc_call_sync(server->client, &msg, 0);
1869 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1871 struct nfs4_exception exception = { };
1874 err = nfs4_handle_exception(server,
1875 _nfs4_proc_statfs(server, fhandle, fsstat),
1877 } while (exception.retry);
1881 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1882 struct nfs_fsinfo *fsinfo)
1884 struct nfs4_fsinfo_arg args = {
1886 .bitmask = server->attr_bitmask,
1888 struct rpc_message msg = {
1889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1894 return rpc_call_sync(server->client, &msg, 0);
1897 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1899 struct nfs4_exception exception = { };
1903 err = nfs4_handle_exception(server,
1904 _nfs4_do_fsinfo(server, fhandle, fsinfo),
1906 } while (exception.retry);
1910 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1912 fsinfo->fattr->valid = 0;
1913 return nfs4_do_fsinfo(server, fhandle, fsinfo);
1916 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1917 struct nfs_pathconf *pathconf)
1919 struct nfs4_pathconf_arg args = {
1921 .bitmask = server->attr_bitmask,
1923 struct rpc_message msg = {
1924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1926 .rpc_resp = pathconf,
1929 /* None of the pathconf attributes are mandatory to implement */
1930 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
1931 memset(pathconf, 0, sizeof(*pathconf));
1935 pathconf->fattr->valid = 0;
1936 return rpc_call_sync(server->client, &msg, 0);
1939 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1940 struct nfs_pathconf *pathconf)
1942 struct nfs4_exception exception = { };
1946 err = nfs4_handle_exception(server,
1947 _nfs4_proc_pathconf(server, fhandle, pathconf),
1949 } while (exception.retry);
1954 nfs4_read_done(struct rpc_task *task)
1956 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
1957 struct inode *inode = data->inode;
1959 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1960 rpc_restart_call(task);
1963 if (task->tk_status > 0)
1964 renew_lease(NFS_SERVER(inode), data->timestamp);
1965 /* Call back common NFS readpage processing */
1966 nfs_readpage_result(task);
1970 nfs4_proc_read_setup(struct nfs_read_data *data)
1972 struct rpc_task *task = &data->task;
1973 struct rpc_message msg = {
1974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1975 .rpc_argp = &data->args,
1976 .rpc_resp = &data->res,
1977 .rpc_cred = data->cred,
1979 struct inode *inode = data->inode;
1982 data->timestamp = jiffies;
1984 /* N.B. Do we need to test? Never called for swapfile inode */
1985 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
1987 /* Finalize the task. */
1988 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
1989 rpc_call_setup(task, &msg, 0);
1993 nfs4_write_done(struct rpc_task *task)
1995 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1996 struct inode *inode = data->inode;
1998 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1999 rpc_restart_call(task);
2002 if (task->tk_status >= 0)
2003 renew_lease(NFS_SERVER(inode), data->timestamp);
2004 /* Call back common NFS writeback processing */
2005 nfs_writeback_done(task);
2009 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2011 struct rpc_task *task = &data->task;
2012 struct rpc_message msg = {
2013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2014 .rpc_argp = &data->args,
2015 .rpc_resp = &data->res,
2016 .rpc_cred = data->cred,
2018 struct inode *inode = data->inode;
2022 if (how & FLUSH_STABLE) {
2023 if (!NFS_I(inode)->ncommit)
2024 stable = NFS_FILE_SYNC;
2026 stable = NFS_DATA_SYNC;
2028 stable = NFS_UNSTABLE;
2029 data->args.stable = stable;
2031 data->timestamp = jiffies;
2033 /* Set the initial flags for the task. */
2034 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2036 /* Finalize the task. */
2037 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2038 rpc_call_setup(task, &msg, 0);
2042 nfs4_commit_done(struct rpc_task *task)
2044 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2045 struct inode *inode = data->inode;
2047 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2048 rpc_restart_call(task);
2051 /* Call back common NFS writeback processing */
2052 nfs_commit_done(task);
2056 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2058 struct rpc_task *task = &data->task;
2059 struct rpc_message msg = {
2060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2061 .rpc_argp = &data->args,
2062 .rpc_resp = &data->res,
2063 .rpc_cred = data->cred,
2065 struct inode *inode = data->inode;
2068 /* Set the initial flags for the task. */
2069 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2071 /* Finalize the task. */
2072 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2073 rpc_call_setup(task, &msg, 0);
2077 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2078 * standalone procedure for queueing an asynchronous RENEW.
2081 renew_done(struct rpc_task *task)
2083 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2084 unsigned long timestamp = (unsigned long)task->tk_calldata;
2086 if (task->tk_status < 0) {
2087 switch (task->tk_status) {
2088 case -NFS4ERR_STALE_CLIENTID:
2089 case -NFS4ERR_EXPIRED:
2090 case -NFS4ERR_CB_PATH_DOWN:
2091 nfs4_schedule_state_recovery(clp);
2095 spin_lock(&clp->cl_lock);
2096 if (time_before(clp->cl_last_renewal,timestamp))
2097 clp->cl_last_renewal = timestamp;
2098 spin_unlock(&clp->cl_lock);
2102 nfs4_proc_async_renew(struct nfs4_client *clp)
2104 struct rpc_message msg = {
2105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2107 .rpc_cred = clp->cl_cred,
2110 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2111 renew_done, (void *)jiffies);
2115 nfs4_proc_renew(struct nfs4_client *clp)
2117 struct rpc_message msg = {
2118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2120 .rpc_cred = clp->cl_cred,
2122 unsigned long now = jiffies;
2125 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2128 spin_lock(&clp->cl_lock);
2129 if (time_before(clp->cl_last_renewal,now))
2130 clp->cl_last_renewal = now;
2131 spin_unlock(&clp->cl_lock);
2136 * We will need to arrange for the VFS layer to provide an atomic open.
2137 * Until then, this open method is prone to inefficiency and race conditions
2138 * due to the lookup, potential create, and open VFS calls from sys_open()
2139 * placed on the wire.
2142 nfs4_proc_file_open(struct inode *inode, struct file *filp)
2144 struct dentry *dentry = filp->f_dentry;
2145 struct nfs_open_context *ctx;
2146 struct nfs4_state *state = NULL;
2147 struct rpc_cred *cred;
2148 int status = -ENOMEM;
2150 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2151 (int)dentry->d_parent->d_name.len,
2152 dentry->d_parent->d_name.name,
2153 (int)dentry->d_name.len, dentry->d_name.name);
2156 /* Find our open stateid */
2157 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
2159 return PTR_ERR(cred);
2160 ctx = alloc_nfs_open_context(dentry, cred);
2162 if (unlikely(ctx == NULL))
2164 status = -EIO; /* ERACE actually */
2165 state = nfs4_find_state(inode, cred, filp->f_mode);
2166 if (unlikely(state == NULL))
2169 nfs4_close_state(state, filp->f_mode);
2170 ctx->mode = filp->f_mode;
2171 nfs_file_set_open_context(filp, ctx);
2172 put_nfs_open_context(ctx);
2173 if (filp->f_mode & FMODE_WRITE)
2174 nfs_begin_data_update(inode);
2177 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
2178 put_nfs_open_context(ctx);
2186 nfs4_proc_file_release(struct inode *inode, struct file *filp)
2188 if (filp->f_mode & FMODE_WRITE)
2189 nfs_end_data_update(inode);
2190 nfs_file_clear_open_context(filp);
2194 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2196 return (server->caps & NFS_CAP_ACLS)
2197 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2198 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2201 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2202 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2205 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2207 static void buf_to_pages(const void *buf, size_t buflen,
2208 struct page **pages, unsigned int *pgbase)
2210 const void *p = buf;
2212 *pgbase = offset_in_page(buf);
2214 while (p < buf + buflen) {
2215 *(pages++) = virt_to_page(p);
2216 p += PAGE_CACHE_SIZE;
2220 struct nfs4_cached_acl {
2226 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2228 struct nfs_inode *nfsi = NFS_I(inode);
2230 spin_lock(&inode->i_lock);
2231 kfree(nfsi->nfs4_acl);
2232 nfsi->nfs4_acl = acl;
2233 spin_unlock(&inode->i_lock);
2236 static void nfs4_zap_acl_attr(struct inode *inode)
2238 nfs4_set_cached_acl(inode, NULL);
2241 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2243 struct nfs_inode *nfsi = NFS_I(inode);
2244 struct nfs4_cached_acl *acl;
2247 spin_lock(&inode->i_lock);
2248 acl = nfsi->nfs4_acl;
2251 if (buf == NULL) /* user is just asking for length */
2253 if (acl->cached == 0)
2255 ret = -ERANGE; /* see getxattr(2) man page */
2256 if (acl->len > buflen)
2258 memcpy(buf, acl->data, acl->len);
2262 spin_unlock(&inode->i_lock);
2266 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2268 struct nfs4_cached_acl *acl;
2270 if (buf && acl_len <= PAGE_SIZE) {
2271 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2275 memcpy(acl->data, buf, acl_len);
2277 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2284 nfs4_set_cached_acl(inode, acl);
2287 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2289 struct page *pages[NFS4ACL_MAXPAGES];
2290 struct nfs_getaclargs args = {
2291 .fh = NFS_FH(inode),
2295 size_t resp_len = buflen;
2297 struct rpc_message msg = {
2298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2300 .rpc_resp = &resp_len,
2302 struct page *localpage = NULL;
2305 if (buflen < PAGE_SIZE) {
2306 /* As long as we're doing a round trip to the server anyway,
2307 * let's be prepared for a page of acl data. */
2308 localpage = alloc_page(GFP_KERNEL);
2309 resp_buf = page_address(localpage);
2310 if (localpage == NULL)
2312 args.acl_pages[0] = localpage;
2313 args.acl_pgbase = 0;
2314 args.acl_len = PAGE_SIZE;
2317 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2319 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2322 if (resp_len > args.acl_len)
2323 nfs4_write_cached_acl(inode, NULL, resp_len);
2325 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2328 if (resp_len > buflen)
2331 memcpy(buf, resp_buf, resp_len);
2336 __free_page(localpage);
2340 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2342 struct nfs_server *server = NFS_SERVER(inode);
2345 if (!nfs4_server_supports_acls(server))
2347 ret = nfs_revalidate_inode(server, inode);
2350 ret = nfs4_read_cached_acl(inode, buf, buflen);
2353 return nfs4_get_acl_uncached(inode, buf, buflen);
2356 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2358 struct nfs_server *server = NFS_SERVER(inode);
2359 struct page *pages[NFS4ACL_MAXPAGES];
2360 struct nfs_setaclargs arg = {
2361 .fh = NFS_FH(inode),
2365 struct rpc_message msg = {
2366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2372 if (!nfs4_server_supports_acls(server))
2374 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2375 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2377 nfs4_write_cached_acl(inode, buf, buflen);
2382 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
2384 struct nfs4_client *clp = server->nfs4_state;
2386 if (!clp || task->tk_status >= 0)
2388 switch(task->tk_status) {
2389 case -NFS4ERR_STALE_CLIENTID:
2390 case -NFS4ERR_STALE_STATEID:
2391 case -NFS4ERR_EXPIRED:
2392 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2393 nfs4_schedule_state_recovery(clp);
2394 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2395 rpc_wake_up_task(task);
2396 task->tk_status = 0;
2398 case -NFS4ERR_GRACE:
2399 case -NFS4ERR_DELAY:
2400 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2401 task->tk_status = 0;
2403 case -NFS4ERR_OLD_STATEID:
2404 task->tk_status = 0;
2407 task->tk_status = nfs4_map_errors(task->tk_status);
2411 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2415 int interruptible, res = 0;
2419 rpc_clnt_sigmask(clnt, &oldset);
2420 interruptible = TASK_UNINTERRUPTIBLE;
2422 interruptible = TASK_INTERRUPTIBLE;
2423 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2424 nfs4_schedule_state_recovery(clp);
2425 if (clnt->cl_intr && signalled())
2427 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2429 finish_wait(&clp->cl_waitq, &wait);
2430 rpc_clnt_sigunmask(clnt, &oldset);
2434 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2442 *timeout = NFS4_POLL_RETRY_MIN;
2443 if (*timeout > NFS4_POLL_RETRY_MAX)
2444 *timeout = NFS4_POLL_RETRY_MAX;
2445 rpc_clnt_sigmask(clnt, &oldset);
2446 if (clnt->cl_intr) {
2447 schedule_timeout_interruptible(*timeout);
2451 schedule_timeout_uninterruptible(*timeout);
2452 rpc_clnt_sigunmask(clnt, &oldset);
2457 /* This is the error handling routine for processes that are allowed
2460 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2462 struct nfs4_client *clp = server->nfs4_state;
2463 int ret = errorcode;
2465 exception->retry = 0;
2469 case -NFS4ERR_STALE_CLIENTID:
2470 case -NFS4ERR_STALE_STATEID:
2471 case -NFS4ERR_EXPIRED:
2472 ret = nfs4_wait_clnt_recover(server->client, clp);
2474 exception->retry = 1;
2476 case -NFS4ERR_GRACE:
2477 case -NFS4ERR_DELAY:
2478 ret = nfs4_delay(server->client, &exception->timeout);
2480 exception->retry = 1;
2482 case -NFS4ERR_OLD_STATEID:
2484 exception->retry = 1;
2486 /* We failed to handle the error */
2487 return nfs4_map_errors(ret);
2490 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2492 nfs4_verifier sc_verifier;
2493 struct nfs4_setclientid setclientid = {
2494 .sc_verifier = &sc_verifier,
2497 struct rpc_message msg = {
2498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2499 .rpc_argp = &setclientid,
2501 .rpc_cred = clp->cl_cred,
2507 p = (u32*)sc_verifier.data;
2508 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2509 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2512 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2513 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2514 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2515 clp->cl_cred->cr_ops->cr_name,
2516 clp->cl_id_uniquifier);
2517 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2518 sizeof(setclientid.sc_netid), "tcp");
2519 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2520 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2521 clp->cl_ipaddr, port >> 8, port & 255);
2523 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2524 if (status != -NFS4ERR_CLID_INUSE)
2529 ssleep(clp->cl_lease_time + 1);
2531 if (++clp->cl_id_uniquifier == 0)
2538 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2540 struct nfs_fsinfo fsinfo;
2541 struct rpc_message msg = {
2542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2544 .rpc_resp = &fsinfo,
2545 .rpc_cred = clp->cl_cred,
2551 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2553 spin_lock(&clp->cl_lock);
2554 clp->cl_lease_time = fsinfo.lease_time * HZ;
2555 clp->cl_last_renewal = now;
2556 spin_unlock(&clp->cl_lock);
2561 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2563 struct nfs4_delegreturnargs args = {
2564 .fhandle = NFS_FH(inode),
2567 struct rpc_message msg = {
2568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2573 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2576 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2578 struct nfs_server *server = NFS_SERVER(inode);
2579 struct nfs4_exception exception = { };
2582 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2584 case -NFS4ERR_STALE_STATEID:
2585 case -NFS4ERR_EXPIRED:
2586 nfs4_schedule_state_recovery(server->nfs4_state);
2590 err = nfs4_handle_exception(server, err, &exception);
2591 } while (exception.retry);
2595 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2596 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2599 * sleep, with exponential backoff, and retry the LOCK operation.
2601 static unsigned long
2602 nfs4_set_lock_task_retry(unsigned long timeout)
2604 schedule_timeout_interruptible(timeout);
2606 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2607 return NFS4_LOCK_MAXTIMEOUT;
2612 nfs4_lck_type(int cmd, struct file_lock *request)
2615 switch (request->fl_type) {
2617 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2619 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2621 return NFS4_WRITE_LT;
2627 static inline uint64_t
2628 nfs4_lck_length(struct file_lock *request)
2630 if (request->fl_end == OFFSET_MAX)
2631 return ~(uint64_t)0;
2632 return request->fl_end - request->fl_start + 1;
2635 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2637 struct inode *inode = state->inode;
2638 struct nfs_server *server = NFS_SERVER(inode);
2639 struct nfs4_client *clp = server->nfs4_state;
2640 struct nfs_lockargs arg = {
2641 .fh = NFS_FH(inode),
2642 .type = nfs4_lck_type(cmd, request),
2643 .offset = request->fl_start,
2644 .length = nfs4_lck_length(request),
2646 struct nfs_lockres res = {
2649 struct rpc_message msg = {
2650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2653 .rpc_cred = state->owner->so_cred,
2655 struct nfs_lowner nlo;
2656 struct nfs4_lock_state *lsp;
2659 down_read(&clp->cl_sem);
2660 nlo.clientid = clp->cl_clientid;
2661 down(&state->lock_sema);
2662 status = nfs4_set_lock_state(state, request);
2665 lsp = request->fl_u.nfs4_fl.owner;
2666 nlo.id = lsp->ls_id;
2668 status = rpc_call_sync(server->client, &msg, 0);
2670 request->fl_type = F_UNLCK;
2671 } else if (status == -NFS4ERR_DENIED) {
2672 int64_t len, start, end;
2673 start = res.u.denied.offset;
2674 len = res.u.denied.length;
2675 end = start + len - 1;
2676 if (end < 0 || len == 0)
2677 request->fl_end = OFFSET_MAX;
2679 request->fl_end = (loff_t)end;
2680 request->fl_start = (loff_t)start;
2681 request->fl_type = F_WRLCK;
2682 if (res.u.denied.type & 1)
2683 request->fl_type = F_RDLCK;
2684 request->fl_pid = 0;
2688 up(&state->lock_sema);
2689 up_read(&clp->cl_sem);
2693 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2695 struct nfs4_exception exception = { };
2699 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2700 _nfs4_proc_getlk(state, cmd, request),
2702 } while (exception.retry);
2706 static int do_vfs_lock(struct file *file, struct file_lock *fl)
2709 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2711 res = posix_lock_file_wait(file, fl);
2714 res = flock_lock_file_wait(file, fl);
2720 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2725 static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2727 struct inode *inode = state->inode;
2728 struct nfs_server *server = NFS_SERVER(inode);
2729 struct nfs4_client *clp = server->nfs4_state;
2730 struct nfs_lockargs arg = {
2731 .fh = NFS_FH(inode),
2732 .type = nfs4_lck_type(cmd, request),
2733 .offset = request->fl_start,
2734 .length = nfs4_lck_length(request),
2736 struct nfs_lockres res = {
2739 struct rpc_message msg = {
2740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2743 .rpc_cred = state->owner->so_cred,
2745 struct nfs4_lock_state *lsp;
2746 struct nfs_locku_opargs luargs;
2749 down_read(&clp->cl_sem);
2750 down(&state->lock_sema);
2751 status = nfs4_set_lock_state(state, request);
2754 lsp = request->fl_u.nfs4_fl.owner;
2755 /* We might have lost the locks! */
2756 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2758 luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2760 if (luargs.seqid == NULL)
2762 memcpy(luargs.stateid.data, lsp->ls_stateid.data, sizeof(luargs.stateid.data));
2763 arg.u.locku = &luargs;
2764 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2765 nfs_increment_lock_seqid(status, luargs.seqid);
2768 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
2769 sizeof(lsp->ls_stateid.data));
2770 nfs_free_seqid(luargs.seqid);
2772 up(&state->lock_sema);
2774 do_vfs_lock(request->fl_file, request);
2775 up_read(&clp->cl_sem);
2779 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2781 struct nfs4_exception exception = { };
2785 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2786 _nfs4_proc_unlck(state, cmd, request),
2788 } while (exception.retry);
2792 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2794 struct inode *inode = state->inode;
2795 struct nfs_server *server = NFS_SERVER(inode);
2796 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
2797 struct nfs_lockargs arg = {
2798 .fh = NFS_FH(inode),
2799 .type = nfs4_lck_type(cmd, request),
2800 .offset = request->fl_start,
2801 .length = nfs4_lck_length(request),
2803 struct nfs_lockres res = {
2806 struct rpc_message msg = {
2807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2810 .rpc_cred = state->owner->so_cred,
2812 struct nfs_lock_opargs largs = {
2814 .new_lock_owner = 0,
2816 struct nfs_seqid *lock_seqid;
2817 int status = -ENOMEM;
2819 lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2820 if (lock_seqid == NULL)
2822 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
2823 struct nfs4_state_owner *owner = state->owner;
2824 struct nfs_open_to_lock otl = {
2826 .clientid = server->nfs4_state->cl_clientid,
2830 otl.lock_seqid = lock_seqid;
2831 otl.lock_owner.id = lsp->ls_id;
2832 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
2833 largs.u.open_lock = &otl;
2834 largs.new_lock_owner = 1;
2835 arg.u.lock = &largs;
2836 down(&owner->so_sema);
2837 otl.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
2838 if (otl.open_seqid != NULL) {
2839 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2840 /* increment seqid on success, and seqid mutating errors */
2841 nfs_increment_open_seqid(status, otl.open_seqid);
2842 nfs_free_seqid(otl.open_seqid);
2844 up(&owner->so_sema);
2846 nfs_confirm_seqid(&lsp->ls_seqid, 0);
2848 struct nfs_exist_lock el;
2849 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
2850 largs.u.exist_lock = ⪙
2851 arg.u.lock = &largs;
2852 el.seqid = lock_seqid;
2853 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2855 /* increment seqid on success, and seqid mutating errors*/
2856 nfs_increment_lock_seqid(status, lock_seqid);
2857 /* save the returned stateid. */
2859 memcpy(lsp->ls_stateid.data, res.u.stateid.data, sizeof(lsp->ls_stateid.data));
2860 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2861 } else if (status == -NFS4ERR_DENIED)
2863 nfs_free_seqid(lock_seqid);
2867 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2869 struct nfs_server *server = NFS_SERVER(state->inode);
2870 struct nfs4_exception exception = { };
2874 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
2875 if (err != -NFS4ERR_DELAY)
2877 nfs4_handle_exception(server, err, &exception);
2878 } while (exception.retry);
2882 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2884 struct nfs_server *server = NFS_SERVER(state->inode);
2885 struct nfs4_exception exception = { };
2889 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
2890 if (err != -NFS4ERR_DELAY)
2892 nfs4_handle_exception(server, err, &exception);
2893 } while (exception.retry);
2897 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2899 struct nfs4_client *clp = state->owner->so_client;
2902 down_read(&clp->cl_sem);
2903 down(&state->lock_sema);
2904 status = nfs4_set_lock_state(state, request);
2906 status = _nfs4_do_setlk(state, cmd, request, 0);
2907 up(&state->lock_sema);
2909 /* Note: we always want to sleep here! */
2910 request->fl_flags |= FL_SLEEP;
2911 if (do_vfs_lock(request->fl_file, request) < 0)
2912 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
2914 up_read(&clp->cl_sem);
2918 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2920 struct nfs4_exception exception = { };
2924 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2925 _nfs4_proc_setlk(state, cmd, request),
2927 } while (exception.retry);
2932 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
2934 struct nfs_open_context *ctx;
2935 struct nfs4_state *state;
2936 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
2939 /* verify open state */
2940 ctx = (struct nfs_open_context *)filp->private_data;
2943 if (request->fl_start < 0 || request->fl_end < 0)
2947 return nfs4_proc_getlk(state, F_GETLK, request);
2949 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
2952 if (request->fl_type == F_UNLCK)
2953 return nfs4_proc_unlck(state, cmd, request);
2956 status = nfs4_proc_setlk(state, cmd, request);
2957 if ((status != -EAGAIN) || IS_SETLK(cmd))
2959 timeout = nfs4_set_lock_task_retry(timeout);
2960 status = -ERESTARTSYS;
2963 } while(status < 0);
2968 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
2970 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
2971 size_t buflen, int flags)
2973 struct inode *inode = dentry->d_inode;
2975 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
2978 if (!S_ISREG(inode->i_mode) &&
2979 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
2982 return nfs4_proc_set_acl(inode, buf, buflen);
2985 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
2986 * and that's what we'll do for e.g. user attributes that haven't been set.
2987 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
2988 * attributes in kernel-managed attribute namespaces. */
2989 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
2992 struct inode *inode = dentry->d_inode;
2994 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
2997 return nfs4_proc_get_acl(inode, buf, buflen);
3000 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3002 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3004 if (buf && buflen < len)
3007 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3011 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3012 .recover_open = nfs4_open_reclaim,
3013 .recover_lock = nfs4_lock_reclaim,
3016 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3017 .recover_open = nfs4_open_expired,
3018 .recover_lock = nfs4_lock_expired,
3021 static struct inode_operations nfs4_file_inode_operations = {
3022 .permission = nfs_permission,
3023 .getattr = nfs_getattr,
3024 .setattr = nfs_setattr,
3025 .getxattr = nfs4_getxattr,
3026 .setxattr = nfs4_setxattr,
3027 .listxattr = nfs4_listxattr,
3030 struct nfs_rpc_ops nfs_v4_clientops = {
3031 .version = 4, /* protocol version */
3032 .dentry_ops = &nfs4_dentry_operations,
3033 .dir_inode_ops = &nfs4_dir_inode_operations,
3034 .file_inode_ops = &nfs4_file_inode_operations,
3035 .getroot = nfs4_proc_get_root,
3036 .getattr = nfs4_proc_getattr,
3037 .setattr = nfs4_proc_setattr,
3038 .lookup = nfs4_proc_lookup,
3039 .access = nfs4_proc_access,
3040 .readlink = nfs4_proc_readlink,
3041 .read = nfs4_proc_read,
3042 .write = nfs4_proc_write,
3043 .commit = nfs4_proc_commit,
3044 .create = nfs4_proc_create,
3045 .remove = nfs4_proc_remove,
3046 .unlink_setup = nfs4_proc_unlink_setup,
3047 .unlink_done = nfs4_proc_unlink_done,
3048 .rename = nfs4_proc_rename,
3049 .link = nfs4_proc_link,
3050 .symlink = nfs4_proc_symlink,
3051 .mkdir = nfs4_proc_mkdir,
3052 .rmdir = nfs4_proc_remove,
3053 .readdir = nfs4_proc_readdir,
3054 .mknod = nfs4_proc_mknod,
3055 .statfs = nfs4_proc_statfs,
3056 .fsinfo = nfs4_proc_fsinfo,
3057 .pathconf = nfs4_proc_pathconf,
3058 .decode_dirent = nfs4_decode_dirent,
3059 .read_setup = nfs4_proc_read_setup,
3060 .write_setup = nfs4_proc_write_setup,
3061 .commit_setup = nfs4_proc_commit_setup,
3062 .file_open = nfs4_proc_file_open,
3063 .file_release = nfs4_proc_file_release,
3064 .lock = nfs4_proc_lock,
3065 .clear_acl_cache = nfs4_zap_acl_attr,