4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/utsname.h>
40 #include <linux/delay.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/smp_lock.h>
49 #include <linux/namei.h>
50 #include <linux/mount.h>
53 #include "delegation.h"
55 #define NFSDBG_FACILITY NFSDBG_PROC
57 #define NFS4_POLL_RETRY_MIN (1*HZ)
58 #define NFS4_POLL_RETRY_MAX (15*HZ)
60 static 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);
61 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
62 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
63 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
64 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
65 extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
66 extern struct rpc_procinfo nfs4_procedures[];
68 /* Prevent leaks of NFSv4 errors into userland */
69 int nfs4_map_errors(int err)
72 dprintk("%s could not handle NFSv4 error %d\n",
80 * This is our standard bitmap for GETATTR requests.
82 const u32 nfs4_fattr_bitmap[2] = {
87 | FATTR4_WORD0_FILEID,
89 | FATTR4_WORD1_NUMLINKS
91 | FATTR4_WORD1_OWNER_GROUP
93 | FATTR4_WORD1_SPACE_USED
94 | FATTR4_WORD1_TIME_ACCESS
95 | FATTR4_WORD1_TIME_METADATA
96 | FATTR4_WORD1_TIME_MODIFY
99 const u32 nfs4_statfs_bitmap[2] = {
100 FATTR4_WORD0_FILES_AVAIL
101 | FATTR4_WORD0_FILES_FREE
102 | FATTR4_WORD0_FILES_TOTAL,
103 FATTR4_WORD1_SPACE_AVAIL
104 | FATTR4_WORD1_SPACE_FREE
105 | FATTR4_WORD1_SPACE_TOTAL
108 const u32 nfs4_pathconf_bitmap[2] = {
110 | FATTR4_WORD0_MAXNAME,
114 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
115 | FATTR4_WORD0_MAXREAD
116 | FATTR4_WORD0_MAXWRITE
117 | FATTR4_WORD0_LEASE_TIME,
121 static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
122 struct nfs4_readdir_arg *readdir)
126 BUG_ON(readdir->count < 80);
128 readdir->cookie = cookie;
129 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
134 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
139 * NFSv4 servers do not return entries for '.' and '..'
140 * Therefore, we fake these entries here. We let '.'
141 * have cookie 0 and '..' have cookie 1. Note that
142 * when talking to the server, we always send cookie 0
145 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
148 *p++ = xdr_one; /* next */
149 *p++ = xdr_zero; /* cookie, first word */
150 *p++ = xdr_one; /* cookie, second word */
151 *p++ = xdr_one; /* entry len */
152 memcpy(p, ".\0\0\0", 4); /* entry */
154 *p++ = xdr_one; /* bitmap length */
155 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
156 *p++ = htonl(8); /* attribute buffer length */
157 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
160 *p++ = xdr_one; /* next */
161 *p++ = xdr_zero; /* cookie, first word */
162 *p++ = xdr_two; /* cookie, second word */
163 *p++ = xdr_two; /* entry len */
164 memcpy(p, "..\0\0", 4); /* entry */
166 *p++ = xdr_one; /* bitmap length */
167 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
168 *p++ = htonl(8); /* attribute buffer length */
169 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
171 readdir->pgbase = (char *)p - (char *)start;
172 readdir->count -= readdir->pgbase;
173 kunmap_atomic(start, KM_USER0);
177 renew_lease(struct nfs_server *server, unsigned long timestamp)
179 struct nfs4_client *clp = server->nfs4_state;
180 spin_lock(&clp->cl_lock);
181 if (time_before(clp->cl_last_renewal,timestamp))
182 clp->cl_last_renewal = timestamp;
183 spin_unlock(&clp->cl_lock);
186 static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
188 struct nfs_inode *nfsi = NFS_I(inode);
190 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
191 nfsi->change_attr = cinfo->after;
194 /* Helper for asynchronous RPC calls */
195 static int nfs4_call_async(struct rpc_clnt *clnt, rpc_action tk_begin,
196 rpc_action tk_exit, void *calldata)
198 struct rpc_task *task;
200 if (!(task = rpc_new_task(clnt, tk_exit, RPC_TASK_ASYNC)))
203 task->tk_calldata = calldata;
204 task->tk_action = tk_begin;
209 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
211 struct inode *inode = state->inode;
213 open_flags &= (FMODE_READ|FMODE_WRITE);
214 /* Protect against nfs4_find_state() */
215 spin_lock(&state->owner->so_lock);
216 spin_lock(&inode->i_lock);
217 state->state |= open_flags;
218 /* NB! List reordering - see the reclaim code for why. */
219 if ((open_flags & FMODE_WRITE) && 0 == state->nwriters++)
220 list_move(&state->open_states, &state->owner->so_states);
221 if (open_flags & FMODE_READ)
223 memcpy(&state->stateid, stateid, sizeof(state->stateid));
224 spin_unlock(&inode->i_lock);
225 spin_unlock(&state->owner->so_lock);
230 * reclaim state on the server after a reboot.
232 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
234 struct inode *inode = state->inode;
235 struct nfs_server *server = NFS_SERVER(inode);
236 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
237 struct nfs_openargs o_arg = {
240 .open_flags = state->state,
241 .clientid = server->nfs4_state->cl_clientid,
242 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
243 .bitmask = server->attr_bitmask,
245 struct nfs_openres o_res = {
246 .server = server, /* Grrr */
248 struct rpc_message msg = {
249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
252 .rpc_cred = sp->so_cred,
256 if (delegation != NULL) {
257 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
258 memcpy(&state->stateid, &delegation->stateid,
259 sizeof(state->stateid));
260 set_bit(NFS_DELEGATED_STATE, &state->flags);
263 o_arg.u.delegation_type = delegation->type;
265 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
266 if (o_arg.seqid == NULL)
268 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
269 /* Confirm the sequence as being established */
270 nfs_confirm_seqid(&sp->so_seqid, status);
271 nfs_increment_open_seqid(status, o_arg.seqid);
273 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
274 if (o_res.delegation_type != 0) {
275 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
276 /* Did the server issue an immediate delegation recall? */
278 nfs_async_inode_return_delegation(inode, &o_res.stateid);
281 nfs_free_seqid(o_arg.seqid);
282 clear_bit(NFS_DELEGATED_STATE, &state->flags);
283 /* Ensure we update the inode attributes */
288 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
290 struct nfs_server *server = NFS_SERVER(state->inode);
291 struct nfs4_exception exception = { };
294 err = _nfs4_open_reclaim(sp, state);
295 if (err != -NFS4ERR_DELAY)
297 nfs4_handle_exception(server, err, &exception);
298 } while (exception.retry);
302 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
304 struct nfs4_state_owner *sp = state->owner;
305 struct inode *inode = dentry->d_inode;
306 struct nfs_server *server = NFS_SERVER(inode);
307 struct dentry *parent = dget_parent(dentry);
308 struct nfs_openargs arg = {
309 .fh = NFS_FH(parent->d_inode),
310 .clientid = server->nfs4_state->cl_clientid,
311 .name = &dentry->d_name,
314 .bitmask = server->attr_bitmask,
315 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
317 struct nfs_openres res = {
320 struct rpc_message msg = {
321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
324 .rpc_cred = sp->so_cred,
328 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
330 if (state->state == 0)
332 arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
334 if (arg.seqid == NULL)
336 arg.open_flags = state->state;
337 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
338 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
339 nfs_increment_open_seqid(status, arg.seqid);
342 if(res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
343 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
344 sp, &res.stateid, arg.seqid);
348 nfs_confirm_seqid(&sp->so_seqid, 0);
350 memcpy(state->stateid.data, res.stateid.data,
351 sizeof(state->stateid.data));
352 clear_bit(NFS_DELEGATED_STATE, &state->flags);
355 nfs_free_seqid(arg.seqid);
361 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
363 struct nfs4_exception exception = { };
364 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
367 err = _nfs4_open_delegation_recall(dentry, state);
371 case -NFS4ERR_STALE_CLIENTID:
372 case -NFS4ERR_STALE_STATEID:
373 case -NFS4ERR_EXPIRED:
374 /* Don't recall a delegation if it was lost */
375 nfs4_schedule_state_recovery(server->nfs4_state);
378 err = nfs4_handle_exception(server, err, &exception);
379 } while (exception.retry);
383 static 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)
385 struct nfs_open_confirmargs arg = {
390 struct nfs_open_confirmres res;
391 struct rpc_message msg = {
392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
395 .rpc_cred = sp->so_cred,
399 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
400 /* Confirm the sequence as being established */
401 nfs_confirm_seqid(&sp->so_seqid, status);
402 nfs_increment_open_seqid(status, seqid);
404 memcpy(stateid, &res.stateid, sizeof(*stateid));
408 static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
410 struct nfs_server *server = NFS_SERVER(dir);
411 struct rpc_message msg = {
412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
415 .rpc_cred = sp->so_cred,
419 /* Update sequence id. The caller must serialize! */
420 o_arg->id = sp->so_id;
421 o_arg->clientid = sp->so_client->cl_clientid;
423 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
425 /* OPEN on anything except a regular file is disallowed in NFSv4 */
426 switch (o_res->f_attr->mode & S_IFMT) {
440 nfs_increment_open_seqid(status, o_arg->seqid);
443 update_changeattr(dir, &o_res->cinfo);
444 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
445 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
446 sp, &o_res->stateid, o_arg->seqid);
450 nfs_confirm_seqid(&sp->so_seqid, 0);
451 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
452 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
457 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
459 struct nfs_access_entry cache;
463 if (openflags & FMODE_READ)
465 if (openflags & FMODE_WRITE)
467 status = nfs_access_get_cached(inode, cred, &cache);
471 /* Be clever: ask server to check for all possible rights */
472 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
474 cache.jiffies = jiffies;
475 status = _nfs4_proc_access(inode, &cache);
478 nfs_access_add_cache(inode, &cache);
480 if ((cache.mask & mask) == mask)
487 * reclaim state on the server after a network partition.
488 * Assumes caller holds the appropriate lock
490 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
492 struct dentry *parent = dget_parent(dentry);
493 struct inode *dir = parent->d_inode;
494 struct inode *inode = state->inode;
495 struct nfs_server *server = NFS_SERVER(dir);
496 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
497 struct nfs_fattr f_attr = {
500 struct nfs_openargs o_arg = {
502 .open_flags = state->state,
503 .name = &dentry->d_name,
504 .bitmask = server->attr_bitmask,
505 .claim = NFS4_OPEN_CLAIM_NULL,
507 struct nfs_openres o_res = {
513 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
514 status = _nfs4_do_access(inode, sp->so_cred, state->state);
517 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
518 set_bit(NFS_DELEGATED_STATE, &state->flags);
521 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
523 if (o_arg.seqid == NULL)
525 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
528 /* Check if files differ */
529 if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
531 /* Has the file handle changed? */
532 if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
533 /* Verify if the change attributes are the same */
534 if (f_attr.change_attr != NFS_I(inode)->change_attr)
536 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
538 /* Lets just pretend that this is the same file */
539 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
540 NFS_I(inode)->fileid = f_attr.fileid;
542 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
543 if (o_res.delegation_type != 0) {
544 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
545 nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
547 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
550 nfs_free_seqid(o_arg.seqid);
551 clear_bit(NFS_DELEGATED_STATE, &state->flags);
557 /* Invalidate the state owner so we don't ever use it again */
558 nfs4_drop_state_owner(sp);
560 /* Should we be trying to close that stateid? */
564 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
566 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
567 struct nfs4_exception exception = { };
571 err = _nfs4_open_expired(sp, state, dentry);
572 if (err == -NFS4ERR_DELAY)
573 nfs4_handle_exception(server, err, &exception);
574 } while (exception.retry);
578 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
580 struct nfs_inode *nfsi = NFS_I(state->inode);
581 struct nfs_open_context *ctx;
584 spin_lock(&state->inode->i_lock);
585 list_for_each_entry(ctx, &nfsi->open_files, list) {
586 if (ctx->state != state)
588 get_nfs_open_context(ctx);
589 spin_unlock(&state->inode->i_lock);
590 status = nfs4_do_open_expired(sp, state, ctx->dentry);
591 put_nfs_open_context(ctx);
594 spin_unlock(&state->inode->i_lock);
599 * Returns an nfs4_state + an extra reference to the inode
601 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
603 struct nfs_delegation *delegation;
604 struct nfs_server *server = NFS_SERVER(inode);
605 struct nfs4_client *clp = server->nfs4_state;
606 struct nfs_inode *nfsi = NFS_I(inode);
607 struct nfs4_state_owner *sp = NULL;
608 struct nfs4_state *state = NULL;
609 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
612 /* Protect against reboot recovery - NOTE ORDER! */
613 down_read(&clp->cl_sem);
614 /* Protect against delegation recall */
615 down_read(&nfsi->rwsem);
616 delegation = NFS_I(inode)->delegation;
618 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
621 if (!(sp = nfs4_get_state_owner(server, cred))) {
622 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
625 state = nfs4_get_open_state(inode, sp);
630 if ((state->state & open_flags) == open_flags) {
631 spin_lock(&inode->i_lock);
632 if (open_flags & FMODE_READ)
634 if (open_flags & FMODE_WRITE)
636 spin_unlock(&inode->i_lock);
638 } else if (state->state != 0)
642 err = _nfs4_do_access(inode, cred, open_flags);
646 set_bit(NFS_DELEGATED_STATE, &state->flags);
647 update_open_stateid(state, &delegation->stateid, open_flags);
649 nfs4_put_state_owner(sp);
650 up_read(&nfsi->rwsem);
651 up_read(&clp->cl_sem);
658 nfs4_put_open_state(state);
659 nfs4_put_state_owner(sp);
661 up_read(&nfsi->rwsem);
662 up_read(&clp->cl_sem);
664 nfs_inode_return_delegation(inode);
668 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
670 struct nfs4_exception exception = { };
671 struct nfs4_state *res;
675 err = _nfs4_open_delegated(inode, flags, cred, &res);
678 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
680 } while (exception.retry);
685 * Returns an nfs4_state + an referenced inode
687 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
689 struct nfs4_state_owner *sp;
690 struct nfs4_state *state = NULL;
691 struct nfs_server *server = NFS_SERVER(dir);
692 struct nfs4_client *clp = server->nfs4_state;
693 struct inode *inode = NULL;
695 struct nfs_fattr f_attr = {
698 struct nfs_openargs o_arg = {
701 .name = &dentry->d_name,
703 .bitmask = server->attr_bitmask,
704 .claim = NFS4_OPEN_CLAIM_NULL,
706 struct nfs_openres o_res = {
711 /* Protect against reboot recovery conflicts */
712 down_read(&clp->cl_sem);
714 if (!(sp = nfs4_get_state_owner(server, cred))) {
715 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
718 if (flags & O_EXCL) {
719 u32 *p = (u32 *) o_arg.u.verifier.data;
723 o_arg.u.attrs = sattr;
724 /* Serialization for the sequence id */
726 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
727 if (o_arg.seqid == NULL)
729 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
734 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
737 state = nfs4_get_open_state(inode, sp);
740 update_open_stateid(state, &o_res.stateid, flags);
741 if (o_res.delegation_type != 0)
742 nfs_inode_set_delegation(inode, cred, &o_res);
743 nfs_free_seqid(o_arg.seqid);
744 nfs4_put_state_owner(sp);
745 up_read(&clp->cl_sem);
751 nfs4_put_open_state(state);
752 nfs_free_seqid(o_arg.seqid);
753 nfs4_put_state_owner(sp);
755 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
756 up_read(&clp->cl_sem);
764 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
766 struct nfs4_exception exception = { };
767 struct nfs4_state *res;
771 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
774 /* NOTE: BAD_SEQID means the server and client disagree about the
775 * book-keeping w.r.t. state-changing operations
776 * (OPEN/CLOSE/LOCK/LOCKU...)
777 * It is actually a sign of a bug on the client or on the server.
779 * If we receive a BAD_SEQID error in the particular case of
780 * doing an OPEN, we assume that nfs_increment_open_seqid() will
781 * have unhashed the old state_owner for us, and that we can
782 * therefore safely retry using a new one. We should still warn
785 if (status == -NFS4ERR_BAD_SEQID) {
786 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
791 * BAD_STATEID on OPEN means that the server cancelled our
792 * state before it received the OPEN_CONFIRM.
793 * Recover by retrying the request as per the discussion
794 * on Page 181 of RFC3530.
796 if (status == -NFS4ERR_BAD_STATEID) {
800 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
801 status, &exception));
802 } while (exception.retry);
806 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
807 struct nfs_fh *fhandle, struct iattr *sattr,
808 struct nfs4_state *state)
810 struct nfs_setattrargs arg = {
814 .bitmask = server->attr_bitmask,
816 struct nfs_setattrres res = {
820 struct rpc_message msg = {
821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
830 msg.rpc_cred = state->owner->so_cred;
831 nfs4_copy_stateid(&arg.stateid, state, current->files);
833 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
835 status = rpc_call_sync(server->client, &msg, 0);
839 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
840 struct nfs_fh *fhandle, struct iattr *sattr,
841 struct nfs4_state *state)
843 struct nfs4_exception exception = { };
846 err = nfs4_handle_exception(server,
847 _nfs4_do_setattr(server, fattr, fhandle, sattr,
850 } while (exception.retry);
854 struct nfs4_closedata {
856 struct nfs4_state *state;
857 struct nfs_closeargs arg;
858 struct nfs_closeres res;
861 static void nfs4_free_closedata(struct nfs4_closedata *calldata)
863 struct nfs4_state *state = calldata->state;
864 struct nfs4_state_owner *sp = state->owner;
866 nfs4_put_open_state(calldata->state);
867 nfs_free_seqid(calldata->arg.seqid);
868 nfs4_put_state_owner(sp);
872 static void nfs4_close_done(struct rpc_task *task)
874 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
875 struct nfs4_state *state = calldata->state;
876 struct nfs_server *server = NFS_SERVER(calldata->inode);
878 /* hmm. we are done with the inode, and in the process of freeing
879 * the state_owner. we keep this around to process errors
881 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
882 switch (task->tk_status) {
884 memcpy(&state->stateid, &calldata->res.stateid,
885 sizeof(state->stateid));
887 case -NFS4ERR_STALE_STATEID:
888 case -NFS4ERR_EXPIRED:
889 state->state = calldata->arg.open_flags;
890 nfs4_schedule_state_recovery(server->nfs4_state);
893 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
894 rpc_restart_call(task);
898 state->state = calldata->arg.open_flags;
899 nfs4_free_closedata(calldata);
902 static void nfs4_close_begin(struct rpc_task *task)
904 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
905 struct nfs4_state *state = calldata->state;
906 struct rpc_message msg = {
907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
908 .rpc_argp = &calldata->arg,
909 .rpc_resp = &calldata->res,
910 .rpc_cred = state->owner->so_cred,
915 status = nfs_wait_on_sequence(calldata->arg.seqid, task);
918 /* Don't reorder reads */
920 /* Recalculate the new open mode in case someone reopened the file
921 * while we were waiting in line to be scheduled.
923 if (state->nreaders != 0)
925 if (state->nwriters != 0)
927 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
929 if (mode == state->state) {
930 nfs4_free_closedata(calldata);
931 task->tk_exit = NULL;
936 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
937 calldata->arg.open_flags = mode;
938 rpc_call_setup(task, &msg, 0);
942 * It is possible for data to be read/written from a mem-mapped file
943 * after the sys_close call (which hits the vfs layer as a flush).
944 * This means that we can't safely call nfsv4 close on a file until
945 * the inode is cleared. This in turn means that we are not good
946 * NFSv4 citizens - we do not indicate to the server to update the file's
947 * share state even when we are done with one of the three share
948 * stateid's in the inode.
950 * NOTE: Caller must be holding the sp->so_owner semaphore!
952 int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
954 struct nfs4_closedata *calldata;
955 int status = -ENOMEM;
957 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
958 if (calldata == NULL)
960 calldata->inode = inode;
961 calldata->state = state;
962 calldata->arg.fh = NFS_FH(inode);
963 calldata->arg.stateid = &state->stateid;
964 /* Serialization for the sequence id */
965 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
966 if (calldata->arg.seqid == NULL)
967 goto out_free_calldata;
969 status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_close_begin,
970 nfs4_close_done, calldata);
974 nfs_free_seqid(calldata->arg.seqid);
981 static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
985 filp = lookup_instantiate_filp(nd, dentry, NULL);
987 struct nfs_open_context *ctx;
988 ctx = (struct nfs_open_context *)filp->private_data;
991 nfs4_close_state(state, nd->intent.open.flags);
995 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
998 struct rpc_cred *cred;
999 struct nfs4_state *state;
1002 if (nd->flags & LOOKUP_CREATE) {
1003 attr.ia_mode = nd->intent.open.create_mode;
1004 attr.ia_valid = ATTR_MODE;
1005 if (!IS_POSIXACL(dir))
1006 attr.ia_mode &= ~current->fs->umask;
1009 BUG_ON(nd->intent.open.flags & O_CREAT);
1012 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1014 return (struct dentry *)cred;
1015 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1017 if (IS_ERR(state)) {
1018 if (PTR_ERR(state) == -ENOENT)
1019 d_add(dentry, NULL);
1020 return (struct dentry *)state;
1022 res = d_add_unique(dentry, state->inode);
1025 nfs4_intent_set_file(nd, dentry, state);
1030 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1032 struct rpc_cred *cred;
1033 struct nfs4_state *state;
1034 struct inode *inode;
1036 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1038 return PTR_ERR(cred);
1039 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1041 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1043 if (IS_ERR(state)) {
1044 switch (PTR_ERR(state)) {
1050 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1053 if (dentry->d_inode == NULL)
1058 inode = state->inode;
1060 if (inode == dentry->d_inode) {
1061 nfs4_intent_set_file(nd, dentry, state);
1064 nfs4_close_state(state, openflags);
1071 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1073 struct nfs4_server_caps_res res = {};
1074 struct rpc_message msg = {
1075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1076 .rpc_argp = fhandle,
1081 status = rpc_call_sync(server->client, &msg, 0);
1083 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1084 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1085 server->caps |= NFS_CAP_ACLS;
1086 if (res.has_links != 0)
1087 server->caps |= NFS_CAP_HARDLINKS;
1088 if (res.has_symlinks != 0)
1089 server->caps |= NFS_CAP_SYMLINKS;
1090 server->acl_bitmask = res.acl_bitmask;
1095 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1097 struct nfs4_exception exception = { };
1100 err = nfs4_handle_exception(server,
1101 _nfs4_server_capabilities(server, fhandle),
1103 } while (exception.retry);
1107 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1108 struct nfs_fsinfo *info)
1110 struct nfs_fattr * fattr = info->fattr;
1111 struct nfs4_lookup_root_arg args = {
1112 .bitmask = nfs4_fattr_bitmap,
1114 struct nfs4_lookup_res res = {
1119 struct rpc_message msg = {
1120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1125 return rpc_call_sync(server->client, &msg, 0);
1128 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1129 struct nfs_fsinfo *info)
1131 struct nfs4_exception exception = { };
1134 err = nfs4_handle_exception(server,
1135 _nfs4_lookup_root(server, fhandle, info),
1137 } while (exception.retry);
1141 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1142 struct nfs_fsinfo *info)
1144 struct nfs_fattr * fattr = info->fattr;
1147 struct nfs4_lookup_arg args = {
1150 .bitmask = nfs4_fattr_bitmap,
1152 struct nfs4_lookup_res res = {
1157 struct rpc_message msg = {
1158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1165 * Now we do a separate LOOKUP for each component of the mount path.
1166 * The LOOKUPs are done separately so that we can conveniently
1167 * catch an ERR_WRONGSEC if it occurs along the way...
1169 status = nfs4_lookup_root(server, fhandle, info);
1173 p = server->mnt_path;
1175 struct nfs4_exception exception = { };
1182 while (*p && (*p != '/'))
1188 status = nfs4_handle_exception(server,
1189 rpc_call_sync(server->client, &msg, 0),
1191 } while (exception.retry);
1194 if (status == -ENOENT) {
1195 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1196 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1201 status = nfs4_server_capabilities(server, fhandle);
1203 status = nfs4_do_fsinfo(server, fhandle, info);
1208 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1210 struct nfs4_getattr_arg args = {
1212 .bitmask = server->attr_bitmask,
1214 struct nfs4_getattr_res res = {
1218 struct rpc_message msg = {
1219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1225 return rpc_call_sync(server->client, &msg, 0);
1228 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1230 struct nfs4_exception exception = { };
1233 err = nfs4_handle_exception(server,
1234 _nfs4_proc_getattr(server, fhandle, fattr),
1236 } while (exception.retry);
1241 * The file is not closed if it is opened due to the a request to change
1242 * the size of the file. The open call will not be needed once the
1243 * VFS layer lookup-intents are implemented.
1245 * Close is called when the inode is destroyed.
1246 * If we haven't opened the file for O_WRONLY, we
1247 * need to in the size_change case to obtain a stateid.
1250 * Because OPEN is always done by name in nfsv4, it is
1251 * possible that we opened a different file by the same
1252 * name. We can recognize this race condition, but we
1253 * can't do anything about it besides returning an error.
1255 * This will be fixed with VFS changes (lookup-intent).
1258 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1259 struct iattr *sattr)
1261 struct rpc_cred *cred;
1262 struct inode *inode = dentry->d_inode;
1263 struct nfs4_state *state;
1268 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1270 return PTR_ERR(cred);
1271 /* Search for an existing WRITE delegation first */
1272 state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
1273 if (!IS_ERR(state)) {
1274 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1277 /* Search for an existing open(O_WRITE) stateid */
1278 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1281 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1282 NFS_FH(inode), sattr, state);
1284 nfs_setattr_update_inode(inode, sattr);
1286 nfs4_close_state(state, FMODE_WRITE);
1291 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1292 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1295 struct nfs_server *server = NFS_SERVER(dir);
1296 struct nfs4_lookup_arg args = {
1297 .bitmask = server->attr_bitmask,
1298 .dir_fh = NFS_FH(dir),
1301 struct nfs4_lookup_res res = {
1306 struct rpc_message msg = {
1307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1314 dprintk("NFS call lookup %s\n", name->name);
1315 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1316 dprintk("NFS reply lookup: %d\n", status);
1320 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1322 struct nfs4_exception exception = { };
1325 err = nfs4_handle_exception(NFS_SERVER(dir),
1326 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1328 } while (exception.retry);
1332 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1334 struct nfs4_accessargs args = {
1335 .fh = NFS_FH(inode),
1337 struct nfs4_accessres res = { 0 };
1338 struct rpc_message msg = {
1339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1342 .rpc_cred = entry->cred,
1344 int mode = entry->mask;
1348 * Determine which access bits we want to ask for...
1350 if (mode & MAY_READ)
1351 args.access |= NFS4_ACCESS_READ;
1352 if (S_ISDIR(inode->i_mode)) {
1353 if (mode & MAY_WRITE)
1354 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1355 if (mode & MAY_EXEC)
1356 args.access |= NFS4_ACCESS_LOOKUP;
1358 if (mode & MAY_WRITE)
1359 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1360 if (mode & MAY_EXEC)
1361 args.access |= NFS4_ACCESS_EXECUTE;
1363 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1366 if (res.access & NFS4_ACCESS_READ)
1367 entry->mask |= MAY_READ;
1368 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1369 entry->mask |= MAY_WRITE;
1370 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1371 entry->mask |= MAY_EXEC;
1376 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1378 struct nfs4_exception exception = { };
1381 err = nfs4_handle_exception(NFS_SERVER(inode),
1382 _nfs4_proc_access(inode, entry),
1384 } while (exception.retry);
1389 * TODO: For the time being, we don't try to get any attributes
1390 * along with any of the zero-copy operations READ, READDIR,
1393 * In the case of the first three, we want to put the GETATTR
1394 * after the read-type operation -- this is because it is hard
1395 * to predict the length of a GETATTR response in v4, and thus
1396 * align the READ data correctly. This means that the GETATTR
1397 * may end up partially falling into the page cache, and we should
1398 * shift it into the 'tail' of the xdr_buf before processing.
1399 * To do this efficiently, we need to know the total length
1400 * of data received, which doesn't seem to be available outside
1403 * In the case of WRITE, we also want to put the GETATTR after
1404 * the operation -- in this case because we want to make sure
1405 * we get the post-operation mtime and size. This means that
1406 * we can't use xdr_encode_pages() as written: we need a variant
1407 * of it which would leave room in the 'tail' iovec.
1409 * Both of these changes to the XDR layer would in fact be quite
1410 * minor, but I decided to leave them for a subsequent patch.
1412 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1413 unsigned int pgbase, unsigned int pglen)
1415 struct nfs4_readlink args = {
1416 .fh = NFS_FH(inode),
1421 struct rpc_message msg = {
1422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1427 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1430 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1431 unsigned int pgbase, unsigned int pglen)
1433 struct nfs4_exception exception = { };
1436 err = nfs4_handle_exception(NFS_SERVER(inode),
1437 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1439 } while (exception.retry);
1443 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1445 int flags = rdata->flags;
1446 struct inode *inode = rdata->inode;
1447 struct nfs_fattr *fattr = rdata->res.fattr;
1448 struct nfs_server *server = NFS_SERVER(inode);
1449 struct rpc_message msg = {
1450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1451 .rpc_argp = &rdata->args,
1452 .rpc_resp = &rdata->res,
1453 .rpc_cred = rdata->cred,
1455 unsigned long timestamp = jiffies;
1458 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1459 (long long) rdata->args.offset);
1462 status = rpc_call_sync(server->client, &msg, flags);
1464 renew_lease(server, timestamp);
1465 dprintk("NFS reply read: %d\n", status);
1469 static int nfs4_proc_read(struct nfs_read_data *rdata)
1471 struct nfs4_exception exception = { };
1474 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1475 _nfs4_proc_read(rdata),
1477 } while (exception.retry);
1481 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1483 int rpcflags = wdata->flags;
1484 struct inode *inode = wdata->inode;
1485 struct nfs_fattr *fattr = wdata->res.fattr;
1486 struct nfs_server *server = NFS_SERVER(inode);
1487 struct rpc_message msg = {
1488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1489 .rpc_argp = &wdata->args,
1490 .rpc_resp = &wdata->res,
1491 .rpc_cred = wdata->cred,
1495 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1496 (long long) wdata->args.offset);
1499 status = rpc_call_sync(server->client, &msg, rpcflags);
1500 dprintk("NFS reply write: %d\n", status);
1504 static int nfs4_proc_write(struct nfs_write_data *wdata)
1506 struct nfs4_exception exception = { };
1509 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1510 _nfs4_proc_write(wdata),
1512 } while (exception.retry);
1516 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1518 struct inode *inode = cdata->inode;
1519 struct nfs_fattr *fattr = cdata->res.fattr;
1520 struct nfs_server *server = NFS_SERVER(inode);
1521 struct rpc_message msg = {
1522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1523 .rpc_argp = &cdata->args,
1524 .rpc_resp = &cdata->res,
1525 .rpc_cred = cdata->cred,
1529 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1530 (long long) cdata->args.offset);
1533 status = rpc_call_sync(server->client, &msg, 0);
1534 dprintk("NFS reply commit: %d\n", status);
1538 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1540 struct nfs4_exception exception = { };
1543 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1544 _nfs4_proc_commit(cdata),
1546 } while (exception.retry);
1552 * We will need to arrange for the VFS layer to provide an atomic open.
1553 * Until then, this create/open method is prone to inefficiency and race
1554 * conditions due to the lookup, create, and open VFS calls from sys_open()
1555 * placed on the wire.
1557 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1558 * The file will be opened again in the subsequent VFS open call
1559 * (nfs4_proc_file_open).
1561 * The open for read will just hang around to be used by any process that
1562 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1566 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1567 int flags, struct nameidata *nd)
1569 struct nfs4_state *state;
1570 struct rpc_cred *cred;
1573 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1575 status = PTR_ERR(cred);
1578 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1580 if (IS_ERR(state)) {
1581 status = PTR_ERR(state);
1584 d_instantiate(dentry, state->inode);
1585 if (flags & O_EXCL) {
1586 struct nfs_fattr fattr;
1587 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1588 NFS_FH(state->inode), sattr, state);
1590 nfs_setattr_update_inode(state->inode, sattr);
1592 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1593 nfs4_intent_set_file(nd, dentry, state);
1595 nfs4_close_state(state, flags);
1600 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1602 struct nfs4_remove_arg args = {
1606 struct nfs4_change_info res;
1607 struct rpc_message msg = {
1608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1614 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1616 update_changeattr(dir, &res);
1620 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1622 struct nfs4_exception exception = { };
1625 err = nfs4_handle_exception(NFS_SERVER(dir),
1626 _nfs4_proc_remove(dir, name),
1628 } while (exception.retry);
1632 struct unlink_desc {
1633 struct nfs4_remove_arg args;
1634 struct nfs4_change_info res;
1637 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1640 struct unlink_desc *up;
1642 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1646 up->args.fh = NFS_FH(dir->d_inode);
1647 up->args.name = name;
1649 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1650 msg->rpc_argp = &up->args;
1651 msg->rpc_resp = &up->res;
1655 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1657 struct rpc_message *msg = &task->tk_msg;
1658 struct unlink_desc *up;
1660 if (msg->rpc_resp != NULL) {
1661 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1662 update_changeattr(dir->d_inode, &up->res);
1664 msg->rpc_resp = NULL;
1665 msg->rpc_argp = NULL;
1670 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1671 struct inode *new_dir, struct qstr *new_name)
1673 struct nfs4_rename_arg arg = {
1674 .old_dir = NFS_FH(old_dir),
1675 .new_dir = NFS_FH(new_dir),
1676 .old_name = old_name,
1677 .new_name = new_name,
1679 struct nfs4_rename_res res = { };
1680 struct rpc_message msg = {
1681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1687 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1690 update_changeattr(old_dir, &res.old_cinfo);
1691 update_changeattr(new_dir, &res.new_cinfo);
1696 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1697 struct inode *new_dir, struct qstr *new_name)
1699 struct nfs4_exception exception = { };
1702 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1703 _nfs4_proc_rename(old_dir, old_name,
1706 } while (exception.retry);
1710 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1712 struct nfs4_link_arg arg = {
1713 .fh = NFS_FH(inode),
1714 .dir_fh = NFS_FH(dir),
1717 struct nfs4_change_info cinfo = { };
1718 struct rpc_message msg = {
1719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1725 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1727 update_changeattr(dir, &cinfo);
1732 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1734 struct nfs4_exception exception = { };
1737 err = nfs4_handle_exception(NFS_SERVER(inode),
1738 _nfs4_proc_link(inode, dir, name),
1740 } while (exception.retry);
1744 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1745 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1746 struct nfs_fattr *fattr)
1748 struct nfs_server *server = NFS_SERVER(dir);
1749 struct nfs4_create_arg arg = {
1750 .dir_fh = NFS_FH(dir),
1755 .bitmask = server->attr_bitmask,
1757 struct nfs4_create_res res = {
1762 struct rpc_message msg = {
1763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1769 if (path->len > NFS4_MAXPATHLEN)
1770 return -ENAMETOOLONG;
1771 arg.u.symlink = path;
1774 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1776 update_changeattr(dir, &res.dir_cinfo);
1780 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1781 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1782 struct nfs_fattr *fattr)
1784 struct nfs4_exception exception = { };
1787 err = nfs4_handle_exception(NFS_SERVER(dir),
1788 _nfs4_proc_symlink(dir, name, path, sattr,
1791 } while (exception.retry);
1795 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1796 struct iattr *sattr)
1798 struct nfs_server *server = NFS_SERVER(dir);
1799 struct nfs_fh fhandle;
1800 struct nfs_fattr fattr;
1801 struct nfs4_create_arg arg = {
1802 .dir_fh = NFS_FH(dir),
1804 .name = &dentry->d_name,
1807 .bitmask = server->attr_bitmask,
1809 struct nfs4_create_res res = {
1814 struct rpc_message msg = {
1815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1823 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1825 update_changeattr(dir, &res.dir_cinfo);
1826 status = nfs_instantiate(dentry, &fhandle, &fattr);
1831 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1832 struct iattr *sattr)
1834 struct nfs4_exception exception = { };
1837 err = nfs4_handle_exception(NFS_SERVER(dir),
1838 _nfs4_proc_mkdir(dir, dentry, sattr),
1840 } while (exception.retry);
1844 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1845 u64 cookie, struct page *page, unsigned int count, int plus)
1847 struct inode *dir = dentry->d_inode;
1848 struct nfs4_readdir_arg args = {
1853 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1855 struct nfs4_readdir_res res;
1856 struct rpc_message msg = {
1857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1864 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1865 dentry->d_parent->d_name.name,
1866 dentry->d_name.name,
1867 (unsigned long long)cookie);
1869 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1870 res.pgbase = args.pgbase;
1871 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1873 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1875 dprintk("%s: returns %d\n", __FUNCTION__, status);
1879 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1880 u64 cookie, struct page *page, unsigned int count, int plus)
1882 struct nfs4_exception exception = { };
1885 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1886 _nfs4_proc_readdir(dentry, cred, cookie,
1889 } while (exception.retry);
1893 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1894 struct iattr *sattr, dev_t rdev)
1896 struct nfs_server *server = NFS_SERVER(dir);
1898 struct nfs_fattr fattr;
1899 struct nfs4_create_arg arg = {
1900 .dir_fh = NFS_FH(dir),
1902 .name = &dentry->d_name,
1904 .bitmask = server->attr_bitmask,
1906 struct nfs4_create_res res = {
1911 struct rpc_message msg = {
1912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1917 int mode = sattr->ia_mode;
1921 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1922 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1924 arg.ftype = NF4FIFO;
1925 else if (S_ISBLK(mode)) {
1927 arg.u.device.specdata1 = MAJOR(rdev);
1928 arg.u.device.specdata2 = MINOR(rdev);
1930 else if (S_ISCHR(mode)) {
1932 arg.u.device.specdata1 = MAJOR(rdev);
1933 arg.u.device.specdata2 = MINOR(rdev);
1936 arg.ftype = NF4SOCK;
1938 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1940 update_changeattr(dir, &res.dir_cinfo);
1941 status = nfs_instantiate(dentry, &fh, &fattr);
1946 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1947 struct iattr *sattr, dev_t rdev)
1949 struct nfs4_exception exception = { };
1952 err = nfs4_handle_exception(NFS_SERVER(dir),
1953 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1955 } while (exception.retry);
1959 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1960 struct nfs_fsstat *fsstat)
1962 struct nfs4_statfs_arg args = {
1964 .bitmask = server->attr_bitmask,
1966 struct rpc_message msg = {
1967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1972 fsstat->fattr->valid = 0;
1973 return rpc_call_sync(server->client, &msg, 0);
1976 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1978 struct nfs4_exception exception = { };
1981 err = nfs4_handle_exception(server,
1982 _nfs4_proc_statfs(server, fhandle, fsstat),
1984 } while (exception.retry);
1988 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1989 struct nfs_fsinfo *fsinfo)
1991 struct nfs4_fsinfo_arg args = {
1993 .bitmask = server->attr_bitmask,
1995 struct rpc_message msg = {
1996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2001 return rpc_call_sync(server->client, &msg, 0);
2004 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2006 struct nfs4_exception exception = { };
2010 err = nfs4_handle_exception(server,
2011 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2013 } while (exception.retry);
2017 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2019 fsinfo->fattr->valid = 0;
2020 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2023 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2024 struct nfs_pathconf *pathconf)
2026 struct nfs4_pathconf_arg args = {
2028 .bitmask = server->attr_bitmask,
2030 struct rpc_message msg = {
2031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2033 .rpc_resp = pathconf,
2036 /* None of the pathconf attributes are mandatory to implement */
2037 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2038 memset(pathconf, 0, sizeof(*pathconf));
2042 pathconf->fattr->valid = 0;
2043 return rpc_call_sync(server->client, &msg, 0);
2046 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2047 struct nfs_pathconf *pathconf)
2049 struct nfs4_exception exception = { };
2053 err = nfs4_handle_exception(server,
2054 _nfs4_proc_pathconf(server, fhandle, pathconf),
2056 } while (exception.retry);
2061 nfs4_read_done(struct rpc_task *task)
2063 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
2064 struct inode *inode = data->inode;
2066 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2067 rpc_restart_call(task);
2070 if (task->tk_status > 0)
2071 renew_lease(NFS_SERVER(inode), data->timestamp);
2072 /* Call back common NFS readpage processing */
2073 nfs_readpage_result(task);
2077 nfs4_proc_read_setup(struct nfs_read_data *data)
2079 struct rpc_task *task = &data->task;
2080 struct rpc_message msg = {
2081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2082 .rpc_argp = &data->args,
2083 .rpc_resp = &data->res,
2084 .rpc_cred = data->cred,
2086 struct inode *inode = data->inode;
2089 data->timestamp = jiffies;
2091 /* N.B. Do we need to test? Never called for swapfile inode */
2092 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2094 /* Finalize the task. */
2095 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
2096 rpc_call_setup(task, &msg, 0);
2100 nfs4_write_done(struct rpc_task *task)
2102 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2103 struct inode *inode = data->inode;
2105 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2106 rpc_restart_call(task);
2109 if (task->tk_status >= 0)
2110 renew_lease(NFS_SERVER(inode), data->timestamp);
2111 /* Call back common NFS writeback processing */
2112 nfs_writeback_done(task);
2116 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2118 struct rpc_task *task = &data->task;
2119 struct rpc_message msg = {
2120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2121 .rpc_argp = &data->args,
2122 .rpc_resp = &data->res,
2123 .rpc_cred = data->cred,
2125 struct inode *inode = data->inode;
2129 if (how & FLUSH_STABLE) {
2130 if (!NFS_I(inode)->ncommit)
2131 stable = NFS_FILE_SYNC;
2133 stable = NFS_DATA_SYNC;
2135 stable = NFS_UNSTABLE;
2136 data->args.stable = stable;
2138 data->timestamp = jiffies;
2140 /* Set the initial flags for the task. */
2141 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2143 /* Finalize the task. */
2144 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2145 rpc_call_setup(task, &msg, 0);
2149 nfs4_commit_done(struct rpc_task *task)
2151 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2152 struct inode *inode = data->inode;
2154 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2155 rpc_restart_call(task);
2158 /* Call back common NFS writeback processing */
2159 nfs_commit_done(task);
2163 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2165 struct rpc_task *task = &data->task;
2166 struct rpc_message msg = {
2167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2168 .rpc_argp = &data->args,
2169 .rpc_resp = &data->res,
2170 .rpc_cred = data->cred,
2172 struct inode *inode = data->inode;
2175 /* Set the initial flags for the task. */
2176 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2178 /* Finalize the task. */
2179 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2180 rpc_call_setup(task, &msg, 0);
2184 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2185 * standalone procedure for queueing an asynchronous RENEW.
2188 renew_done(struct rpc_task *task)
2190 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2191 unsigned long timestamp = (unsigned long)task->tk_calldata;
2193 if (task->tk_status < 0) {
2194 switch (task->tk_status) {
2195 case -NFS4ERR_STALE_CLIENTID:
2196 case -NFS4ERR_EXPIRED:
2197 case -NFS4ERR_CB_PATH_DOWN:
2198 nfs4_schedule_state_recovery(clp);
2202 spin_lock(&clp->cl_lock);
2203 if (time_before(clp->cl_last_renewal,timestamp))
2204 clp->cl_last_renewal = timestamp;
2205 spin_unlock(&clp->cl_lock);
2209 nfs4_proc_async_renew(struct nfs4_client *clp)
2211 struct rpc_message msg = {
2212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2214 .rpc_cred = clp->cl_cred,
2217 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2218 renew_done, (void *)jiffies);
2222 nfs4_proc_renew(struct nfs4_client *clp)
2224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2227 .rpc_cred = clp->cl_cred,
2229 unsigned long now = jiffies;
2232 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2235 spin_lock(&clp->cl_lock);
2236 if (time_before(clp->cl_last_renewal,now))
2237 clp->cl_last_renewal = now;
2238 spin_unlock(&clp->cl_lock);
2242 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2244 return (server->caps & NFS_CAP_ACLS)
2245 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2246 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2249 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2250 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2253 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2255 static void buf_to_pages(const void *buf, size_t buflen,
2256 struct page **pages, unsigned int *pgbase)
2258 const void *p = buf;
2260 *pgbase = offset_in_page(buf);
2262 while (p < buf + buflen) {
2263 *(pages++) = virt_to_page(p);
2264 p += PAGE_CACHE_SIZE;
2268 struct nfs4_cached_acl {
2274 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2276 struct nfs_inode *nfsi = NFS_I(inode);
2278 spin_lock(&inode->i_lock);
2279 kfree(nfsi->nfs4_acl);
2280 nfsi->nfs4_acl = acl;
2281 spin_unlock(&inode->i_lock);
2284 static void nfs4_zap_acl_attr(struct inode *inode)
2286 nfs4_set_cached_acl(inode, NULL);
2289 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2291 struct nfs_inode *nfsi = NFS_I(inode);
2292 struct nfs4_cached_acl *acl;
2295 spin_lock(&inode->i_lock);
2296 acl = nfsi->nfs4_acl;
2299 if (buf == NULL) /* user is just asking for length */
2301 if (acl->cached == 0)
2303 ret = -ERANGE; /* see getxattr(2) man page */
2304 if (acl->len > buflen)
2306 memcpy(buf, acl->data, acl->len);
2310 spin_unlock(&inode->i_lock);
2314 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2316 struct nfs4_cached_acl *acl;
2318 if (buf && acl_len <= PAGE_SIZE) {
2319 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2323 memcpy(acl->data, buf, acl_len);
2325 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2332 nfs4_set_cached_acl(inode, acl);
2335 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2337 struct page *pages[NFS4ACL_MAXPAGES];
2338 struct nfs_getaclargs args = {
2339 .fh = NFS_FH(inode),
2343 size_t resp_len = buflen;
2345 struct rpc_message msg = {
2346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2348 .rpc_resp = &resp_len,
2350 struct page *localpage = NULL;
2353 if (buflen < PAGE_SIZE) {
2354 /* As long as we're doing a round trip to the server anyway,
2355 * let's be prepared for a page of acl data. */
2356 localpage = alloc_page(GFP_KERNEL);
2357 resp_buf = page_address(localpage);
2358 if (localpage == NULL)
2360 args.acl_pages[0] = localpage;
2361 args.acl_pgbase = 0;
2362 resp_len = args.acl_len = PAGE_SIZE;
2365 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2367 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2370 if (resp_len > args.acl_len)
2371 nfs4_write_cached_acl(inode, NULL, resp_len);
2373 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2376 if (resp_len > buflen)
2379 memcpy(buf, resp_buf, resp_len);
2384 __free_page(localpage);
2388 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2390 struct nfs_server *server = NFS_SERVER(inode);
2393 if (!nfs4_server_supports_acls(server))
2395 ret = nfs_revalidate_inode(server, inode);
2398 ret = nfs4_read_cached_acl(inode, buf, buflen);
2401 return nfs4_get_acl_uncached(inode, buf, buflen);
2404 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2406 struct nfs_server *server = NFS_SERVER(inode);
2407 struct page *pages[NFS4ACL_MAXPAGES];
2408 struct nfs_setaclargs arg = {
2409 .fh = NFS_FH(inode),
2413 struct rpc_message msg = {
2414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2420 if (!nfs4_server_supports_acls(server))
2422 nfs_inode_return_delegation(inode);
2423 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2424 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2426 nfs4_write_cached_acl(inode, buf, buflen);
2431 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2433 struct nfs4_client *clp = server->nfs4_state;
2435 if (!clp || task->tk_status >= 0)
2437 switch(task->tk_status) {
2438 case -NFS4ERR_STALE_CLIENTID:
2439 case -NFS4ERR_STALE_STATEID:
2440 case -NFS4ERR_EXPIRED:
2441 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2442 nfs4_schedule_state_recovery(clp);
2443 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2444 rpc_wake_up_task(task);
2445 task->tk_status = 0;
2447 case -NFS4ERR_GRACE:
2448 case -NFS4ERR_DELAY:
2449 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2450 task->tk_status = 0;
2452 case -NFS4ERR_OLD_STATEID:
2453 task->tk_status = 0;
2456 task->tk_status = nfs4_map_errors(task->tk_status);
2460 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2464 int interruptible, res = 0;
2468 rpc_clnt_sigmask(clnt, &oldset);
2469 interruptible = TASK_UNINTERRUPTIBLE;
2471 interruptible = TASK_INTERRUPTIBLE;
2472 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2473 nfs4_schedule_state_recovery(clp);
2474 if (clnt->cl_intr && signalled())
2476 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2478 finish_wait(&clp->cl_waitq, &wait);
2479 rpc_clnt_sigunmask(clnt, &oldset);
2483 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2491 *timeout = NFS4_POLL_RETRY_MIN;
2492 if (*timeout > NFS4_POLL_RETRY_MAX)
2493 *timeout = NFS4_POLL_RETRY_MAX;
2494 rpc_clnt_sigmask(clnt, &oldset);
2495 if (clnt->cl_intr) {
2496 schedule_timeout_interruptible(*timeout);
2500 schedule_timeout_uninterruptible(*timeout);
2501 rpc_clnt_sigunmask(clnt, &oldset);
2506 /* This is the error handling routine for processes that are allowed
2509 int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2511 struct nfs4_client *clp = server->nfs4_state;
2512 int ret = errorcode;
2514 exception->retry = 0;
2518 case -NFS4ERR_STALE_CLIENTID:
2519 case -NFS4ERR_STALE_STATEID:
2520 case -NFS4ERR_EXPIRED:
2521 ret = nfs4_wait_clnt_recover(server->client, clp);
2523 exception->retry = 1;
2525 case -NFS4ERR_GRACE:
2526 case -NFS4ERR_DELAY:
2527 ret = nfs4_delay(server->client, &exception->timeout);
2529 exception->retry = 1;
2531 case -NFS4ERR_OLD_STATEID:
2533 exception->retry = 1;
2535 /* We failed to handle the error */
2536 return nfs4_map_errors(ret);
2539 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2541 nfs4_verifier sc_verifier;
2542 struct nfs4_setclientid setclientid = {
2543 .sc_verifier = &sc_verifier,
2546 struct rpc_message msg = {
2547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2548 .rpc_argp = &setclientid,
2550 .rpc_cred = clp->cl_cred,
2556 p = (u32*)sc_verifier.data;
2557 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2558 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2561 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2562 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2563 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2564 clp->cl_cred->cr_ops->cr_name,
2565 clp->cl_id_uniquifier);
2566 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2567 sizeof(setclientid.sc_netid), "tcp");
2568 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2569 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2570 clp->cl_ipaddr, port >> 8, port & 255);
2572 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2573 if (status != -NFS4ERR_CLID_INUSE)
2578 ssleep(clp->cl_lease_time + 1);
2580 if (++clp->cl_id_uniquifier == 0)
2587 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2589 struct nfs_fsinfo fsinfo;
2590 struct rpc_message msg = {
2591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2593 .rpc_resp = &fsinfo,
2594 .rpc_cred = clp->cl_cred,
2600 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2602 spin_lock(&clp->cl_lock);
2603 clp->cl_lease_time = fsinfo.lease_time * HZ;
2604 clp->cl_last_renewal = now;
2605 spin_unlock(&clp->cl_lock);
2610 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2612 struct nfs4_delegreturnargs args = {
2613 .fhandle = NFS_FH(inode),
2616 struct rpc_message msg = {
2617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2622 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2625 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2627 struct nfs_server *server = NFS_SERVER(inode);
2628 struct nfs4_exception exception = { };
2631 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2633 case -NFS4ERR_STALE_STATEID:
2634 case -NFS4ERR_EXPIRED:
2635 nfs4_schedule_state_recovery(server->nfs4_state);
2639 err = nfs4_handle_exception(server, err, &exception);
2640 } while (exception.retry);
2644 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2645 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2648 * sleep, with exponential backoff, and retry the LOCK operation.
2650 static unsigned long
2651 nfs4_set_lock_task_retry(unsigned long timeout)
2653 schedule_timeout_interruptible(timeout);
2655 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2656 return NFS4_LOCK_MAXTIMEOUT;
2661 nfs4_lck_type(int cmd, struct file_lock *request)
2664 switch (request->fl_type) {
2666 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2668 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2670 return NFS4_WRITE_LT;
2676 static inline uint64_t
2677 nfs4_lck_length(struct file_lock *request)
2679 if (request->fl_end == OFFSET_MAX)
2680 return ~(uint64_t)0;
2681 return request->fl_end - request->fl_start + 1;
2684 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2686 struct inode *inode = state->inode;
2687 struct nfs_server *server = NFS_SERVER(inode);
2688 struct nfs4_client *clp = server->nfs4_state;
2689 struct nfs_lockargs arg = {
2690 .fh = NFS_FH(inode),
2691 .type = nfs4_lck_type(cmd, request),
2692 .offset = request->fl_start,
2693 .length = nfs4_lck_length(request),
2695 struct nfs_lockres res = {
2698 struct rpc_message msg = {
2699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2702 .rpc_cred = state->owner->so_cred,
2704 struct nfs_lowner nlo;
2705 struct nfs4_lock_state *lsp;
2708 down_read(&clp->cl_sem);
2709 nlo.clientid = clp->cl_clientid;
2710 status = nfs4_set_lock_state(state, request);
2713 lsp = request->fl_u.nfs4_fl.owner;
2714 nlo.id = lsp->ls_id;
2716 status = rpc_call_sync(server->client, &msg, 0);
2718 request->fl_type = F_UNLCK;
2719 } else if (status == -NFS4ERR_DENIED) {
2720 int64_t len, start, end;
2721 start = res.u.denied.offset;
2722 len = res.u.denied.length;
2723 end = start + len - 1;
2724 if (end < 0 || len == 0)
2725 request->fl_end = OFFSET_MAX;
2727 request->fl_end = (loff_t)end;
2728 request->fl_start = (loff_t)start;
2729 request->fl_type = F_WRLCK;
2730 if (res.u.denied.type & 1)
2731 request->fl_type = F_RDLCK;
2732 request->fl_pid = 0;
2736 up_read(&clp->cl_sem);
2740 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2742 struct nfs4_exception exception = { };
2746 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2747 _nfs4_proc_getlk(state, cmd, request),
2749 } while (exception.retry);
2753 static int do_vfs_lock(struct file *file, struct file_lock *fl)
2756 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2758 res = posix_lock_file_wait(file, fl);
2761 res = flock_lock_file_wait(file, fl);
2767 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2772 struct nfs4_unlockdata {
2773 struct nfs_lockargs arg;
2774 struct nfs_locku_opargs luargs;
2775 struct nfs_lockres res;
2776 struct nfs4_lock_state *lsp;
2777 struct nfs_open_context *ctx;
2779 struct completion completion;
2782 static void nfs4_locku_release_calldata(struct nfs4_unlockdata *calldata)
2784 if (atomic_dec_and_test(&calldata->refcount)) {
2785 nfs_free_seqid(calldata->luargs.seqid);
2786 nfs4_put_lock_state(calldata->lsp);
2787 put_nfs_open_context(calldata->ctx);
2792 static void nfs4_locku_complete(struct nfs4_unlockdata *calldata)
2794 complete(&calldata->completion);
2795 nfs4_locku_release_calldata(calldata);
2798 static void nfs4_locku_done(struct rpc_task *task)
2800 struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
2802 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2803 switch (task->tk_status) {
2805 memcpy(calldata->lsp->ls_stateid.data,
2806 calldata->res.u.stateid.data,
2807 sizeof(calldata->lsp->ls_stateid.data));
2809 case -NFS4ERR_STALE_STATEID:
2810 case -NFS4ERR_EXPIRED:
2811 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2814 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2815 rpc_restart_call(task);
2819 nfs4_locku_complete(calldata);
2822 static void nfs4_locku_begin(struct rpc_task *task)
2824 struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
2825 struct rpc_message msg = {
2826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2827 .rpc_argp = &calldata->arg,
2828 .rpc_resp = &calldata->res,
2829 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
2833 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
2836 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
2837 nfs4_locku_complete(calldata);
2838 task->tk_exit = NULL;
2842 rpc_call_setup(task, &msg, 0);
2845 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2847 struct nfs4_unlockdata *calldata;
2848 struct inode *inode = state->inode;
2849 struct nfs_server *server = NFS_SERVER(inode);
2850 struct nfs4_lock_state *lsp;
2853 status = nfs4_set_lock_state(state, request);
2856 lsp = request->fl_u.nfs4_fl.owner;
2857 /* We might have lost the locks! */
2858 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2860 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
2861 if (calldata == NULL)
2863 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2864 if (calldata->luargs.seqid == NULL) {
2868 calldata->luargs.stateid = &lsp->ls_stateid;
2869 calldata->arg.fh = NFS_FH(inode);
2870 calldata->arg.type = nfs4_lck_type(cmd, request);
2871 calldata->arg.offset = request->fl_start;
2872 calldata->arg.length = nfs4_lck_length(request);
2873 calldata->arg.u.locku = &calldata->luargs;
2874 calldata->res.server = server;
2875 calldata->lsp = lsp;
2876 atomic_inc(&lsp->ls_count);
2878 /* Ensure we don't close file until we're done freeing locks! */
2879 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
2881 atomic_set(&calldata->refcount, 2);
2882 init_completion(&calldata->completion);
2884 status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_locku_begin,
2885 nfs4_locku_done, calldata);
2887 wait_for_completion_interruptible(&calldata->completion);
2888 do_vfs_lock(request->fl_file, request);
2889 nfs4_locku_release_calldata(calldata);
2893 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2895 struct inode *inode = state->inode;
2896 struct nfs_server *server = NFS_SERVER(inode);
2897 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
2898 struct nfs_lock_opargs largs = {
2899 .lock_stateid = &lsp->ls_stateid,
2900 .open_stateid = &state->stateid,
2902 .clientid = server->nfs4_state->cl_clientid,
2907 struct nfs_lockargs arg = {
2908 .fh = NFS_FH(inode),
2909 .type = nfs4_lck_type(cmd, request),
2910 .offset = request->fl_start,
2911 .length = nfs4_lck_length(request),
2916 struct nfs_lockres res = {
2919 struct rpc_message msg = {
2920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2923 .rpc_cred = state->owner->so_cred,
2925 int status = -ENOMEM;
2927 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2928 if (largs.lock_seqid == NULL)
2930 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
2931 struct nfs4_state_owner *owner = state->owner;
2933 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
2934 if (largs.open_seqid == NULL)
2936 largs.new_lock_owner = 1;
2937 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2938 /* increment open seqid on success, and seqid mutating errors */
2939 if (largs.new_lock_owner != 0) {
2940 nfs_increment_open_seqid(status, largs.open_seqid);
2942 nfs_confirm_seqid(&lsp->ls_seqid, 0);
2944 nfs_free_seqid(largs.open_seqid);
2946 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2947 /* increment lock seqid on success, and seqid mutating errors*/
2948 nfs_increment_lock_seqid(status, largs.lock_seqid);
2949 /* save the returned stateid. */
2951 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
2952 sizeof(lsp->ls_stateid.data));
2953 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2954 } else if (status == -NFS4ERR_DENIED)
2957 nfs_free_seqid(largs.lock_seqid);
2961 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2963 struct nfs_server *server = NFS_SERVER(state->inode);
2964 struct nfs4_exception exception = { };
2968 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
2969 if (err != -NFS4ERR_DELAY)
2971 nfs4_handle_exception(server, err, &exception);
2972 } while (exception.retry);
2976 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2978 struct nfs_server *server = NFS_SERVER(state->inode);
2979 struct nfs4_exception exception = { };
2983 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
2984 if (err != -NFS4ERR_DELAY)
2986 nfs4_handle_exception(server, err, &exception);
2987 } while (exception.retry);
2991 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2993 struct nfs4_client *clp = state->owner->so_client;
2996 down_read(&clp->cl_sem);
2997 status = nfs4_set_lock_state(state, request);
2999 status = _nfs4_do_setlk(state, cmd, request, 0);
3001 /* Note: we always want to sleep here! */
3002 request->fl_flags |= FL_SLEEP;
3003 if (do_vfs_lock(request->fl_file, request) < 0)
3004 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3006 up_read(&clp->cl_sem);
3010 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3012 struct nfs4_exception exception = { };
3016 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3017 _nfs4_proc_setlk(state, cmd, request),
3019 } while (exception.retry);
3024 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3026 struct nfs_open_context *ctx;
3027 struct nfs4_state *state;
3028 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3031 /* verify open state */
3032 ctx = (struct nfs_open_context *)filp->private_data;
3035 if (request->fl_start < 0 || request->fl_end < 0)
3039 return nfs4_proc_getlk(state, F_GETLK, request);
3041 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3044 if (request->fl_type == F_UNLCK)
3045 return nfs4_proc_unlck(state, cmd, request);
3048 status = nfs4_proc_setlk(state, cmd, request);
3049 if ((status != -EAGAIN) || IS_SETLK(cmd))
3051 timeout = nfs4_set_lock_task_retry(timeout);
3052 status = -ERESTARTSYS;
3055 } while(status < 0);
3060 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3062 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3063 size_t buflen, int flags)
3065 struct inode *inode = dentry->d_inode;
3067 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3070 if (!S_ISREG(inode->i_mode) &&
3071 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3074 return nfs4_proc_set_acl(inode, buf, buflen);
3077 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3078 * and that's what we'll do for e.g. user attributes that haven't been set.
3079 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3080 * attributes in kernel-managed attribute namespaces. */
3081 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3084 struct inode *inode = dentry->d_inode;
3086 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3089 return nfs4_proc_get_acl(inode, buf, buflen);
3092 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3094 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3096 if (buf && buflen < len)
3099 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3103 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3104 .recover_open = nfs4_open_reclaim,
3105 .recover_lock = nfs4_lock_reclaim,
3108 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3109 .recover_open = nfs4_open_expired,
3110 .recover_lock = nfs4_lock_expired,
3113 static struct inode_operations nfs4_file_inode_operations = {
3114 .permission = nfs_permission,
3115 .getattr = nfs_getattr,
3116 .setattr = nfs_setattr,
3117 .getxattr = nfs4_getxattr,
3118 .setxattr = nfs4_setxattr,
3119 .listxattr = nfs4_listxattr,
3122 struct nfs_rpc_ops nfs_v4_clientops = {
3123 .version = 4, /* protocol version */
3124 .dentry_ops = &nfs4_dentry_operations,
3125 .dir_inode_ops = &nfs4_dir_inode_operations,
3126 .file_inode_ops = &nfs4_file_inode_operations,
3127 .getroot = nfs4_proc_get_root,
3128 .getattr = nfs4_proc_getattr,
3129 .setattr = nfs4_proc_setattr,
3130 .lookup = nfs4_proc_lookup,
3131 .access = nfs4_proc_access,
3132 .readlink = nfs4_proc_readlink,
3133 .read = nfs4_proc_read,
3134 .write = nfs4_proc_write,
3135 .commit = nfs4_proc_commit,
3136 .create = nfs4_proc_create,
3137 .remove = nfs4_proc_remove,
3138 .unlink_setup = nfs4_proc_unlink_setup,
3139 .unlink_done = nfs4_proc_unlink_done,
3140 .rename = nfs4_proc_rename,
3141 .link = nfs4_proc_link,
3142 .symlink = nfs4_proc_symlink,
3143 .mkdir = nfs4_proc_mkdir,
3144 .rmdir = nfs4_proc_remove,
3145 .readdir = nfs4_proc_readdir,
3146 .mknod = nfs4_proc_mknod,
3147 .statfs = nfs4_proc_statfs,
3148 .fsinfo = nfs4_proc_fsinfo,
3149 .pathconf = nfs4_proc_pathconf,
3150 .decode_dirent = nfs4_decode_dirent,
3151 .read_setup = nfs4_proc_read_setup,
3152 .write_setup = nfs4_proc_write_setup,
3153 .commit_setup = nfs4_proc_commit_setup,
3154 .file_open = nfs_open,
3155 .file_release = nfs_release,
3156 .lock = nfs4_proc_lock,
3157 .clear_acl_cache = nfs4_zap_acl_attr,