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 spin_lock(&inode->i_lock);
191 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
192 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
193 nfsi->change_attr = cinfo->after;
194 spin_unlock(&inode->i_lock);
197 /* Helper for asynchronous RPC calls */
198 static int nfs4_call_async(struct rpc_clnt *clnt,
199 const struct rpc_call_ops *tk_ops, void *calldata)
201 struct rpc_task *task;
203 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
209 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
214 rpc_clnt_sigmask(task->tk_client, &oldset);
215 ret = rpc_wait_for_completion_task(task);
216 rpc_clnt_sigunmask(task->tk_client, &oldset);
220 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
222 struct inode *inode = state->inode;
224 open_flags &= (FMODE_READ|FMODE_WRITE);
225 /* Protect against nfs4_find_state_byowner() */
226 spin_lock(&state->owner->so_lock);
227 spin_lock(&inode->i_lock);
228 memcpy(&state->stateid, stateid, sizeof(state->stateid));
229 if ((open_flags & FMODE_WRITE))
231 if (open_flags & FMODE_READ)
233 nfs4_state_set_mode_locked(state, state->state | open_flags);
234 spin_unlock(&inode->i_lock);
235 spin_unlock(&state->owner->so_lock);
240 * reclaim state on the server after a reboot.
242 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
244 struct inode *inode = state->inode;
245 struct nfs_server *server = NFS_SERVER(inode);
246 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
247 struct nfs_openargs o_arg = {
250 .open_flags = state->state,
251 .clientid = server->nfs4_state->cl_clientid,
252 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
253 .bitmask = server->attr_bitmask,
255 struct nfs_openres o_res = {
256 .server = server, /* Grrr */
258 struct rpc_message msg = {
259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
262 .rpc_cred = sp->so_cred,
266 if (delegation != NULL) {
267 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
268 memcpy(&state->stateid, &delegation->stateid,
269 sizeof(state->stateid));
270 set_bit(NFS_DELEGATED_STATE, &state->flags);
273 o_arg.u.delegation_type = delegation->type;
275 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
276 if (o_arg.seqid == NULL)
278 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
279 /* Confirm the sequence as being established */
280 nfs_confirm_seqid(&sp->so_seqid, status);
281 nfs_increment_open_seqid(status, o_arg.seqid);
283 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
284 if (o_res.delegation_type != 0) {
285 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
286 /* Did the server issue an immediate delegation recall? */
288 nfs_async_inode_return_delegation(inode, &o_res.stateid);
291 nfs_free_seqid(o_arg.seqid);
292 clear_bit(NFS_DELEGATED_STATE, &state->flags);
293 /* Ensure we update the inode attributes */
298 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
300 struct nfs_server *server = NFS_SERVER(state->inode);
301 struct nfs4_exception exception = { };
304 err = _nfs4_open_reclaim(sp, state);
305 if (err != -NFS4ERR_DELAY)
307 nfs4_handle_exception(server, err, &exception);
308 } while (exception.retry);
312 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
314 struct nfs4_state_owner *sp = state->owner;
315 struct inode *inode = dentry->d_inode;
316 struct nfs_server *server = NFS_SERVER(inode);
317 struct dentry *parent = dget_parent(dentry);
318 struct nfs_openargs arg = {
319 .fh = NFS_FH(parent->d_inode),
320 .clientid = server->nfs4_state->cl_clientid,
321 .name = &dentry->d_name,
324 .bitmask = server->attr_bitmask,
325 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
327 struct nfs_openres res = {
330 struct rpc_message msg = {
331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
334 .rpc_cred = sp->so_cred,
338 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
340 if (state->state == 0)
342 arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
344 if (arg.seqid == NULL)
346 arg.open_flags = state->state;
347 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
348 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
349 nfs_increment_open_seqid(status, arg.seqid);
352 if(res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
353 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
354 sp, &res.stateid, arg.seqid);
358 nfs_confirm_seqid(&sp->so_seqid, 0);
360 memcpy(state->stateid.data, res.stateid.data,
361 sizeof(state->stateid.data));
362 clear_bit(NFS_DELEGATED_STATE, &state->flags);
365 nfs_free_seqid(arg.seqid);
371 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
373 struct nfs4_exception exception = { };
374 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
377 err = _nfs4_open_delegation_recall(dentry, state);
381 case -NFS4ERR_STALE_CLIENTID:
382 case -NFS4ERR_STALE_STATEID:
383 case -NFS4ERR_EXPIRED:
384 /* Don't recall a delegation if it was lost */
385 nfs4_schedule_state_recovery(server->nfs4_state);
388 err = nfs4_handle_exception(server, err, &exception);
389 } while (exception.retry);
393 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)
395 struct nfs_open_confirmargs arg = {
400 struct nfs_open_confirmres res;
401 struct rpc_message msg = {
402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
405 .rpc_cred = sp->so_cred,
409 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
410 /* Confirm the sequence as being established */
411 nfs_confirm_seqid(&sp->so_seqid, status);
412 nfs_increment_open_seqid(status, seqid);
414 memcpy(stateid, &res.stateid, sizeof(*stateid));
418 static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
420 struct nfs_server *server = NFS_SERVER(dir);
421 struct rpc_message msg = {
422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
425 .rpc_cred = sp->so_cred,
429 /* Update sequence id. The caller must serialize! */
430 o_arg->id = sp->so_id;
431 o_arg->clientid = sp->so_client->cl_clientid;
433 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
435 /* OPEN on anything except a regular file is disallowed in NFSv4 */
436 switch (o_res->f_attr->mode & S_IFMT) {
450 nfs_increment_open_seqid(status, o_arg->seqid);
453 if (o_arg->open_flags & O_CREAT) {
454 update_changeattr(dir, &o_res->cinfo);
455 nfs_post_op_update_inode(dir, o_res->dir_attr);
457 nfs_refresh_inode(dir, o_res->dir_attr);
458 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
459 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
460 sp, &o_res->stateid, o_arg->seqid);
464 nfs_confirm_seqid(&sp->so_seqid, 0);
465 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
466 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
471 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
473 struct nfs_access_entry cache;
477 if (openflags & FMODE_READ)
479 if (openflags & FMODE_WRITE)
481 status = nfs_access_get_cached(inode, cred, &cache);
485 /* Be clever: ask server to check for all possible rights */
486 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
488 cache.jiffies = jiffies;
489 status = _nfs4_proc_access(inode, &cache);
492 nfs_access_add_cache(inode, &cache);
494 if ((cache.mask & mask) == mask)
501 * reclaim state on the server after a network partition.
502 * Assumes caller holds the appropriate lock
504 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
506 struct dentry *parent = dget_parent(dentry);
507 struct inode *dir = parent->d_inode;
508 struct inode *inode = state->inode;
509 struct nfs_server *server = NFS_SERVER(dir);
510 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
511 struct nfs_fattr f_attr, dir_attr;
512 struct nfs_openargs o_arg = {
514 .open_flags = state->state,
515 .name = &dentry->d_name,
516 .bitmask = server->attr_bitmask,
517 .claim = NFS4_OPEN_CLAIM_NULL,
519 struct nfs_openres o_res = {
521 .dir_attr = &dir_attr,
526 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
527 status = _nfs4_do_access(inode, sp->so_cred, state->state);
530 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
531 set_bit(NFS_DELEGATED_STATE, &state->flags);
534 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
536 if (o_arg.seqid == NULL)
538 nfs_fattr_init(&f_attr);
539 nfs_fattr_init(&dir_attr);
540 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
543 /* Check if files differ */
544 if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
546 /* Has the file handle changed? */
547 if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
548 /* Verify if the change attributes are the same */
549 if (f_attr.change_attr != NFS_I(inode)->change_attr)
551 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
553 /* Lets just pretend that this is the same file */
554 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
555 NFS_I(inode)->fileid = f_attr.fileid;
557 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
558 if (o_res.delegation_type != 0) {
559 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
560 nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
562 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
565 nfs_free_seqid(o_arg.seqid);
566 clear_bit(NFS_DELEGATED_STATE, &state->flags);
572 /* Invalidate the state owner so we don't ever use it again */
573 nfs4_drop_state_owner(sp);
575 /* Should we be trying to close that stateid? */
579 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
581 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
582 struct nfs4_exception exception = { };
586 err = _nfs4_open_expired(sp, state, dentry);
587 if (err == -NFS4ERR_DELAY)
588 nfs4_handle_exception(server, err, &exception);
589 } while (exception.retry);
593 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
595 struct nfs_inode *nfsi = NFS_I(state->inode);
596 struct nfs_open_context *ctx;
599 spin_lock(&state->inode->i_lock);
600 list_for_each_entry(ctx, &nfsi->open_files, list) {
601 if (ctx->state != state)
603 get_nfs_open_context(ctx);
604 spin_unlock(&state->inode->i_lock);
605 status = nfs4_do_open_expired(sp, state, ctx->dentry);
606 put_nfs_open_context(ctx);
609 spin_unlock(&state->inode->i_lock);
614 * Returns an nfs4_state + an extra reference to the inode
616 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
618 struct nfs_delegation *delegation;
619 struct nfs_server *server = NFS_SERVER(inode);
620 struct nfs4_client *clp = server->nfs4_state;
621 struct nfs_inode *nfsi = NFS_I(inode);
622 struct nfs4_state_owner *sp = NULL;
623 struct nfs4_state *state = NULL;
624 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
627 /* Protect against reboot recovery - NOTE ORDER! */
628 down_read(&clp->cl_sem);
629 /* Protect against delegation recall */
630 down_read(&nfsi->rwsem);
631 delegation = NFS_I(inode)->delegation;
633 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
636 if (!(sp = nfs4_get_state_owner(server, cred))) {
637 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
640 state = nfs4_get_open_state(inode, sp);
645 if ((state->state & open_flags) == open_flags) {
646 spin_lock(&inode->i_lock);
647 if (open_flags & FMODE_READ)
649 if (open_flags & FMODE_WRITE)
651 spin_unlock(&inode->i_lock);
653 } else if (state->state != 0)
657 err = _nfs4_do_access(inode, cred, open_flags);
661 set_bit(NFS_DELEGATED_STATE, &state->flags);
662 update_open_stateid(state, &delegation->stateid, open_flags);
664 nfs4_put_state_owner(sp);
665 up_read(&nfsi->rwsem);
666 up_read(&clp->cl_sem);
673 nfs4_put_open_state(state);
674 nfs4_put_state_owner(sp);
676 up_read(&nfsi->rwsem);
677 up_read(&clp->cl_sem);
679 nfs_inode_return_delegation(inode);
683 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
685 struct nfs4_exception exception = { };
686 struct nfs4_state *res;
690 err = _nfs4_open_delegated(inode, flags, cred, &res);
693 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
695 } while (exception.retry);
700 * Returns an nfs4_state + an referenced inode
702 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
704 struct nfs4_state_owner *sp;
705 struct nfs4_state *state = NULL;
706 struct nfs_server *server = NFS_SERVER(dir);
707 struct nfs4_client *clp = server->nfs4_state;
708 struct inode *inode = NULL;
710 struct nfs_fattr f_attr, dir_attr;
711 struct nfs_openargs o_arg = {
714 .name = &dentry->d_name,
716 .bitmask = server->attr_bitmask,
717 .claim = NFS4_OPEN_CLAIM_NULL,
719 struct nfs_openres o_res = {
721 .dir_attr = &dir_attr,
725 /* Protect against reboot recovery conflicts */
726 down_read(&clp->cl_sem);
728 if (!(sp = nfs4_get_state_owner(server, cred))) {
729 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
732 if (flags & O_EXCL) {
733 u32 *p = (u32 *) o_arg.u.verifier.data;
737 o_arg.u.attrs = sattr;
738 /* Serialization for the sequence id */
740 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
741 if (o_arg.seqid == NULL)
743 nfs_fattr_init(&f_attr);
744 nfs_fattr_init(&dir_attr);
745 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
750 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
753 state = nfs4_get_open_state(inode, sp);
756 update_open_stateid(state, &o_res.stateid, flags);
757 if (o_res.delegation_type != 0)
758 nfs_inode_set_delegation(inode, cred, &o_res);
759 nfs_free_seqid(o_arg.seqid);
760 nfs4_put_state_owner(sp);
761 up_read(&clp->cl_sem);
767 nfs4_put_open_state(state);
768 nfs_free_seqid(o_arg.seqid);
769 nfs4_put_state_owner(sp);
771 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
772 up_read(&clp->cl_sem);
780 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
782 struct nfs4_exception exception = { };
783 struct nfs4_state *res;
787 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
790 /* NOTE: BAD_SEQID means the server and client disagree about the
791 * book-keeping w.r.t. state-changing operations
792 * (OPEN/CLOSE/LOCK/LOCKU...)
793 * It is actually a sign of a bug on the client or on the server.
795 * If we receive a BAD_SEQID error in the particular case of
796 * doing an OPEN, we assume that nfs_increment_open_seqid() will
797 * have unhashed the old state_owner for us, and that we can
798 * therefore safely retry using a new one. We should still warn
801 if (status == -NFS4ERR_BAD_SEQID) {
802 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
807 * BAD_STATEID on OPEN means that the server cancelled our
808 * state before it received the OPEN_CONFIRM.
809 * Recover by retrying the request as per the discussion
810 * on Page 181 of RFC3530.
812 if (status == -NFS4ERR_BAD_STATEID) {
816 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
817 status, &exception));
818 } while (exception.retry);
822 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
823 struct nfs_fh *fhandle, struct iattr *sattr,
824 struct nfs4_state *state)
826 struct nfs_setattrargs arg = {
830 .bitmask = server->attr_bitmask,
832 struct nfs_setattrres res = {
836 struct rpc_message msg = {
837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
843 nfs_fattr_init(fattr);
846 msg.rpc_cred = state->owner->so_cred;
847 nfs4_copy_stateid(&arg.stateid, state, current->files);
849 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
851 status = rpc_call_sync(server->client, &msg, 0);
855 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
856 struct nfs_fh *fhandle, struct iattr *sattr,
857 struct nfs4_state *state)
859 struct nfs4_exception exception = { };
862 err = nfs4_handle_exception(server,
863 _nfs4_do_setattr(server, fattr, fhandle, sattr,
866 } while (exception.retry);
870 struct nfs4_closedata {
872 struct nfs4_state *state;
873 struct nfs_closeargs arg;
874 struct nfs_closeres res;
875 struct nfs_fattr fattr;
878 static void nfs4_free_closedata(void *data)
880 struct nfs4_closedata *calldata = data;
881 struct nfs4_state_owner *sp = calldata->state->owner;
883 nfs4_put_open_state(calldata->state);
884 nfs_free_seqid(calldata->arg.seqid);
885 nfs4_put_state_owner(sp);
889 static void nfs4_close_done(struct rpc_task *task, void *data)
891 struct nfs4_closedata *calldata = data;
892 struct nfs4_state *state = calldata->state;
893 struct nfs_server *server = NFS_SERVER(calldata->inode);
895 if (RPC_ASSASSINATED(task))
897 /* hmm. we are done with the inode, and in the process of freeing
898 * the state_owner. we keep this around to process errors
900 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
901 switch (task->tk_status) {
903 memcpy(&state->stateid, &calldata->res.stateid,
904 sizeof(state->stateid));
906 case -NFS4ERR_STALE_STATEID:
907 case -NFS4ERR_EXPIRED:
908 nfs4_schedule_state_recovery(server->nfs4_state);
911 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
912 rpc_restart_call(task);
916 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
919 static void nfs4_close_prepare(struct rpc_task *task, void *data)
921 struct nfs4_closedata *calldata = data;
922 struct nfs4_state *state = calldata->state;
923 struct rpc_message msg = {
924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
925 .rpc_argp = &calldata->arg,
926 .rpc_resp = &calldata->res,
927 .rpc_cred = state->owner->so_cred,
929 int mode = 0, old_mode;
931 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
933 /* Recalculate the new open mode in case someone reopened the file
934 * while we were waiting in line to be scheduled.
936 spin_lock(&state->owner->so_lock);
937 spin_lock(&calldata->inode->i_lock);
938 mode = old_mode = state->state;
939 if (state->nreaders == 0)
941 if (state->nwriters == 0)
942 mode &= ~FMODE_WRITE;
943 nfs4_state_set_mode_locked(state, mode);
944 spin_unlock(&calldata->inode->i_lock);
945 spin_unlock(&state->owner->so_lock);
946 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
947 /* Note: exit _without_ calling nfs4_close_done */
948 task->tk_action = NULL;
951 nfs_fattr_init(calldata->res.fattr);
953 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
954 calldata->arg.open_flags = mode;
955 rpc_call_setup(task, &msg, 0);
958 static const struct rpc_call_ops nfs4_close_ops = {
959 .rpc_call_prepare = nfs4_close_prepare,
960 .rpc_call_done = nfs4_close_done,
961 .rpc_release = nfs4_free_closedata,
965 * It is possible for data to be read/written from a mem-mapped file
966 * after the sys_close call (which hits the vfs layer as a flush).
967 * This means that we can't safely call nfsv4 close on a file until
968 * the inode is cleared. This in turn means that we are not good
969 * NFSv4 citizens - we do not indicate to the server to update the file's
970 * share state even when we are done with one of the three share
971 * stateid's in the inode.
973 * NOTE: Caller must be holding the sp->so_owner semaphore!
975 int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
977 struct nfs_server *server = NFS_SERVER(inode);
978 struct nfs4_closedata *calldata;
979 int status = -ENOMEM;
981 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
982 if (calldata == NULL)
984 calldata->inode = inode;
985 calldata->state = state;
986 calldata->arg.fh = NFS_FH(inode);
987 calldata->arg.stateid = &state->stateid;
988 /* Serialization for the sequence id */
989 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
990 if (calldata->arg.seqid == NULL)
991 goto out_free_calldata;
992 calldata->arg.bitmask = server->attr_bitmask;
993 calldata->res.fattr = &calldata->fattr;
994 calldata->res.server = server;
996 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
1000 nfs_free_seqid(calldata->arg.seqid);
1007 static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1011 filp = lookup_instantiate_filp(nd, dentry, NULL);
1012 if (!IS_ERR(filp)) {
1013 struct nfs_open_context *ctx;
1014 ctx = (struct nfs_open_context *)filp->private_data;
1017 nfs4_close_state(state, nd->intent.open.flags);
1021 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1024 struct rpc_cred *cred;
1025 struct nfs4_state *state;
1028 if (nd->flags & LOOKUP_CREATE) {
1029 attr.ia_mode = nd->intent.open.create_mode;
1030 attr.ia_valid = ATTR_MODE;
1031 if (!IS_POSIXACL(dir))
1032 attr.ia_mode &= ~current->fs->umask;
1035 BUG_ON(nd->intent.open.flags & O_CREAT);
1038 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1040 return (struct dentry *)cred;
1041 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1043 if (IS_ERR(state)) {
1044 if (PTR_ERR(state) == -ENOENT)
1045 d_add(dentry, NULL);
1046 return (struct dentry *)state;
1048 res = d_add_unique(dentry, state->inode);
1051 nfs4_intent_set_file(nd, dentry, state);
1056 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1058 struct rpc_cred *cred;
1059 struct nfs4_state *state;
1060 struct inode *inode;
1062 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1064 return PTR_ERR(cred);
1065 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1067 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1069 if (IS_ERR(state)) {
1070 switch (PTR_ERR(state)) {
1076 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1079 if (dentry->d_inode == NULL)
1084 inode = state->inode;
1086 if (inode == dentry->d_inode) {
1087 nfs4_intent_set_file(nd, dentry, state);
1090 nfs4_close_state(state, openflags);
1097 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1099 struct nfs4_server_caps_res res = {};
1100 struct rpc_message msg = {
1101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1102 .rpc_argp = fhandle,
1107 status = rpc_call_sync(server->client, &msg, 0);
1109 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1110 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1111 server->caps |= NFS_CAP_ACLS;
1112 if (res.has_links != 0)
1113 server->caps |= NFS_CAP_HARDLINKS;
1114 if (res.has_symlinks != 0)
1115 server->caps |= NFS_CAP_SYMLINKS;
1116 server->acl_bitmask = res.acl_bitmask;
1121 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1123 struct nfs4_exception exception = { };
1126 err = nfs4_handle_exception(server,
1127 _nfs4_server_capabilities(server, fhandle),
1129 } while (exception.retry);
1133 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1134 struct nfs_fsinfo *info)
1136 struct nfs4_lookup_root_arg args = {
1137 .bitmask = nfs4_fattr_bitmap,
1139 struct nfs4_lookup_res res = {
1141 .fattr = info->fattr,
1144 struct rpc_message msg = {
1145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1149 nfs_fattr_init(info->fattr);
1150 return rpc_call_sync(server->client, &msg, 0);
1153 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1154 struct nfs_fsinfo *info)
1156 struct nfs4_exception exception = { };
1159 err = nfs4_handle_exception(server,
1160 _nfs4_lookup_root(server, fhandle, info),
1162 } while (exception.retry);
1166 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1167 struct nfs_fsinfo *info)
1169 struct nfs_fattr * fattr = info->fattr;
1172 struct nfs4_lookup_arg args = {
1175 .bitmask = nfs4_fattr_bitmap,
1177 struct nfs4_lookup_res res = {
1182 struct rpc_message msg = {
1183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1190 * Now we do a separate LOOKUP for each component of the mount path.
1191 * The LOOKUPs are done separately so that we can conveniently
1192 * catch an ERR_WRONGSEC if it occurs along the way...
1194 status = nfs4_lookup_root(server, fhandle, info);
1198 p = server->mnt_path;
1200 struct nfs4_exception exception = { };
1207 while (*p && (*p != '/'))
1212 nfs_fattr_init(fattr);
1213 status = nfs4_handle_exception(server,
1214 rpc_call_sync(server->client, &msg, 0),
1216 } while (exception.retry);
1219 if (status == -ENOENT) {
1220 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1221 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1226 status = nfs4_server_capabilities(server, fhandle);
1228 status = nfs4_do_fsinfo(server, fhandle, info);
1233 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1235 struct nfs4_getattr_arg args = {
1237 .bitmask = server->attr_bitmask,
1239 struct nfs4_getattr_res res = {
1243 struct rpc_message msg = {
1244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1249 nfs_fattr_init(fattr);
1250 return rpc_call_sync(server->client, &msg, 0);
1253 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1255 struct nfs4_exception exception = { };
1258 err = nfs4_handle_exception(server,
1259 _nfs4_proc_getattr(server, fhandle, fattr),
1261 } while (exception.retry);
1266 * The file is not closed if it is opened due to the a request to change
1267 * the size of the file. The open call will not be needed once the
1268 * VFS layer lookup-intents are implemented.
1270 * Close is called when the inode is destroyed.
1271 * If we haven't opened the file for O_WRONLY, we
1272 * need to in the size_change case to obtain a stateid.
1275 * Because OPEN is always done by name in nfsv4, it is
1276 * possible that we opened a different file by the same
1277 * name. We can recognize this race condition, but we
1278 * can't do anything about it besides returning an error.
1280 * This will be fixed with VFS changes (lookup-intent).
1283 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1284 struct iattr *sattr)
1286 struct rpc_cred *cred;
1287 struct inode *inode = dentry->d_inode;
1288 struct nfs_open_context *ctx;
1289 struct nfs4_state *state = NULL;
1292 nfs_fattr_init(fattr);
1294 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1296 return PTR_ERR(cred);
1298 /* Search for an existing open(O_WRITE) file */
1299 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1303 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1304 NFS_FH(inode), sattr, state);
1306 nfs_setattr_update_inode(inode, sattr);
1308 put_nfs_open_context(ctx);
1313 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1314 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1317 struct nfs_server *server = NFS_SERVER(dir);
1318 struct nfs4_lookup_arg args = {
1319 .bitmask = server->attr_bitmask,
1320 .dir_fh = NFS_FH(dir),
1323 struct nfs4_lookup_res res = {
1328 struct rpc_message msg = {
1329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1334 nfs_fattr_init(fattr);
1336 dprintk("NFS call lookup %s\n", name->name);
1337 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1338 dprintk("NFS reply lookup: %d\n", status);
1342 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1344 struct nfs4_exception exception = { };
1347 err = nfs4_handle_exception(NFS_SERVER(dir),
1348 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1350 } while (exception.retry);
1354 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1356 struct nfs4_accessargs args = {
1357 .fh = NFS_FH(inode),
1359 struct nfs4_accessres res = { 0 };
1360 struct rpc_message msg = {
1361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1364 .rpc_cred = entry->cred,
1366 int mode = entry->mask;
1370 * Determine which access bits we want to ask for...
1372 if (mode & MAY_READ)
1373 args.access |= NFS4_ACCESS_READ;
1374 if (S_ISDIR(inode->i_mode)) {
1375 if (mode & MAY_WRITE)
1376 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1377 if (mode & MAY_EXEC)
1378 args.access |= NFS4_ACCESS_LOOKUP;
1380 if (mode & MAY_WRITE)
1381 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1382 if (mode & MAY_EXEC)
1383 args.access |= NFS4_ACCESS_EXECUTE;
1385 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1388 if (res.access & NFS4_ACCESS_READ)
1389 entry->mask |= MAY_READ;
1390 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1391 entry->mask |= MAY_WRITE;
1392 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1393 entry->mask |= MAY_EXEC;
1398 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1400 struct nfs4_exception exception = { };
1403 err = nfs4_handle_exception(NFS_SERVER(inode),
1404 _nfs4_proc_access(inode, entry),
1406 } while (exception.retry);
1411 * TODO: For the time being, we don't try to get any attributes
1412 * along with any of the zero-copy operations READ, READDIR,
1415 * In the case of the first three, we want to put the GETATTR
1416 * after the read-type operation -- this is because it is hard
1417 * to predict the length of a GETATTR response in v4, and thus
1418 * align the READ data correctly. This means that the GETATTR
1419 * may end up partially falling into the page cache, and we should
1420 * shift it into the 'tail' of the xdr_buf before processing.
1421 * To do this efficiently, we need to know the total length
1422 * of data received, which doesn't seem to be available outside
1425 * In the case of WRITE, we also want to put the GETATTR after
1426 * the operation -- in this case because we want to make sure
1427 * we get the post-operation mtime and size. This means that
1428 * we can't use xdr_encode_pages() as written: we need a variant
1429 * of it which would leave room in the 'tail' iovec.
1431 * Both of these changes to the XDR layer would in fact be quite
1432 * minor, but I decided to leave them for a subsequent patch.
1434 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1435 unsigned int pgbase, unsigned int pglen)
1437 struct nfs4_readlink args = {
1438 .fh = NFS_FH(inode),
1443 struct rpc_message msg = {
1444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1449 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1452 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1453 unsigned int pgbase, unsigned int pglen)
1455 struct nfs4_exception exception = { };
1458 err = nfs4_handle_exception(NFS_SERVER(inode),
1459 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1461 } while (exception.retry);
1465 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1467 int flags = rdata->flags;
1468 struct inode *inode = rdata->inode;
1469 struct nfs_fattr *fattr = rdata->res.fattr;
1470 struct nfs_server *server = NFS_SERVER(inode);
1471 struct rpc_message msg = {
1472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1473 .rpc_argp = &rdata->args,
1474 .rpc_resp = &rdata->res,
1475 .rpc_cred = rdata->cred,
1477 unsigned long timestamp = jiffies;
1480 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1481 (long long) rdata->args.offset);
1483 nfs_fattr_init(fattr);
1484 status = rpc_call_sync(server->client, &msg, flags);
1486 renew_lease(server, timestamp);
1487 dprintk("NFS reply read: %d\n", status);
1491 static int nfs4_proc_read(struct nfs_read_data *rdata)
1493 struct nfs4_exception exception = { };
1496 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1497 _nfs4_proc_read(rdata),
1499 } while (exception.retry);
1503 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1505 int rpcflags = wdata->flags;
1506 struct inode *inode = wdata->inode;
1507 struct nfs_fattr *fattr = wdata->res.fattr;
1508 struct nfs_server *server = NFS_SERVER(inode);
1509 struct rpc_message msg = {
1510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1511 .rpc_argp = &wdata->args,
1512 .rpc_resp = &wdata->res,
1513 .rpc_cred = wdata->cred,
1517 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1518 (long long) wdata->args.offset);
1520 wdata->args.bitmask = server->attr_bitmask;
1521 wdata->res.server = server;
1522 nfs_fattr_init(fattr);
1523 status = rpc_call_sync(server->client, &msg, rpcflags);
1524 dprintk("NFS reply write: %d\n", status);
1527 nfs_post_op_update_inode(inode, fattr);
1528 return wdata->res.count;
1531 static int nfs4_proc_write(struct nfs_write_data *wdata)
1533 struct nfs4_exception exception = { };
1536 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1537 _nfs4_proc_write(wdata),
1539 } while (exception.retry);
1543 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1545 struct inode *inode = cdata->inode;
1546 struct nfs_fattr *fattr = cdata->res.fattr;
1547 struct nfs_server *server = NFS_SERVER(inode);
1548 struct rpc_message msg = {
1549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1550 .rpc_argp = &cdata->args,
1551 .rpc_resp = &cdata->res,
1552 .rpc_cred = cdata->cred,
1556 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1557 (long long) cdata->args.offset);
1559 cdata->args.bitmask = server->attr_bitmask;
1560 cdata->res.server = server;
1561 nfs_fattr_init(fattr);
1562 status = rpc_call_sync(server->client, &msg, 0);
1563 dprintk("NFS reply commit: %d\n", status);
1565 nfs_post_op_update_inode(inode, fattr);
1569 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1571 struct nfs4_exception exception = { };
1574 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1575 _nfs4_proc_commit(cdata),
1577 } while (exception.retry);
1583 * We will need to arrange for the VFS layer to provide an atomic open.
1584 * Until then, this create/open method is prone to inefficiency and race
1585 * conditions due to the lookup, create, and open VFS calls from sys_open()
1586 * placed on the wire.
1588 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1589 * The file will be opened again in the subsequent VFS open call
1590 * (nfs4_proc_file_open).
1592 * The open for read will just hang around to be used by any process that
1593 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1597 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1598 int flags, struct nameidata *nd)
1600 struct nfs4_state *state;
1601 struct rpc_cred *cred;
1604 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1606 status = PTR_ERR(cred);
1609 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1611 if (IS_ERR(state)) {
1612 status = PTR_ERR(state);
1615 d_instantiate(dentry, state->inode);
1616 if (flags & O_EXCL) {
1617 struct nfs_fattr fattr;
1618 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1619 NFS_FH(state->inode), sattr, state);
1621 nfs_setattr_update_inode(state->inode, sattr);
1623 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1624 nfs4_intent_set_file(nd, dentry, state);
1626 nfs4_close_state(state, flags);
1631 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1633 struct nfs_server *server = NFS_SERVER(dir);
1634 struct nfs4_remove_arg args = {
1637 .bitmask = server->attr_bitmask,
1639 struct nfs_fattr dir_attr;
1640 struct nfs4_remove_res res = {
1642 .dir_attr = &dir_attr,
1644 struct rpc_message msg = {
1645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1651 nfs_fattr_init(res.dir_attr);
1652 status = rpc_call_sync(server->client, &msg, 0);
1654 update_changeattr(dir, &res.cinfo);
1655 nfs_post_op_update_inode(dir, res.dir_attr);
1660 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1662 struct nfs4_exception exception = { };
1665 err = nfs4_handle_exception(NFS_SERVER(dir),
1666 _nfs4_proc_remove(dir, name),
1668 } while (exception.retry);
1672 struct unlink_desc {
1673 struct nfs4_remove_arg args;
1674 struct nfs4_remove_res res;
1675 struct nfs_fattr dir_attr;
1678 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1681 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1682 struct unlink_desc *up;
1684 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1688 up->args.fh = NFS_FH(dir->d_inode);
1689 up->args.name = name;
1690 up->args.bitmask = server->attr_bitmask;
1691 up->res.server = server;
1692 up->res.dir_attr = &up->dir_attr;
1694 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1695 msg->rpc_argp = &up->args;
1696 msg->rpc_resp = &up->res;
1700 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1702 struct rpc_message *msg = &task->tk_msg;
1703 struct unlink_desc *up;
1705 if (msg->rpc_resp != NULL) {
1706 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1707 update_changeattr(dir->d_inode, &up->res.cinfo);
1708 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1710 msg->rpc_resp = NULL;
1711 msg->rpc_argp = NULL;
1716 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1717 struct inode *new_dir, struct qstr *new_name)
1719 struct nfs_server *server = NFS_SERVER(old_dir);
1720 struct nfs4_rename_arg arg = {
1721 .old_dir = NFS_FH(old_dir),
1722 .new_dir = NFS_FH(new_dir),
1723 .old_name = old_name,
1724 .new_name = new_name,
1725 .bitmask = server->attr_bitmask,
1727 struct nfs_fattr old_fattr, new_fattr;
1728 struct nfs4_rename_res res = {
1730 .old_fattr = &old_fattr,
1731 .new_fattr = &new_fattr,
1733 struct rpc_message msg = {
1734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1740 nfs_fattr_init(res.old_fattr);
1741 nfs_fattr_init(res.new_fattr);
1742 status = rpc_call_sync(server->client, &msg, 0);
1745 update_changeattr(old_dir, &res.old_cinfo);
1746 nfs_post_op_update_inode(old_dir, res.old_fattr);
1747 update_changeattr(new_dir, &res.new_cinfo);
1748 nfs_post_op_update_inode(new_dir, res.new_fattr);
1753 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1754 struct inode *new_dir, struct qstr *new_name)
1756 struct nfs4_exception exception = { };
1759 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1760 _nfs4_proc_rename(old_dir, old_name,
1763 } while (exception.retry);
1767 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1769 struct nfs_server *server = NFS_SERVER(inode);
1770 struct nfs4_link_arg arg = {
1771 .fh = NFS_FH(inode),
1772 .dir_fh = NFS_FH(dir),
1774 .bitmask = server->attr_bitmask,
1776 struct nfs_fattr fattr, dir_attr;
1777 struct nfs4_link_res res = {
1780 .dir_attr = &dir_attr,
1782 struct rpc_message msg = {
1783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1789 nfs_fattr_init(res.fattr);
1790 nfs_fattr_init(res.dir_attr);
1791 status = rpc_call_sync(server->client, &msg, 0);
1793 update_changeattr(dir, &res.cinfo);
1794 nfs_post_op_update_inode(dir, res.dir_attr);
1795 nfs_refresh_inode(inode, res.fattr);
1801 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1803 struct nfs4_exception exception = { };
1806 err = nfs4_handle_exception(NFS_SERVER(inode),
1807 _nfs4_proc_link(inode, dir, name),
1809 } while (exception.retry);
1813 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1814 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1815 struct nfs_fattr *fattr)
1817 struct nfs_server *server = NFS_SERVER(dir);
1818 struct nfs_fattr dir_fattr;
1819 struct nfs4_create_arg arg = {
1820 .dir_fh = NFS_FH(dir),
1825 .bitmask = server->attr_bitmask,
1827 struct nfs4_create_res res = {
1831 .dir_fattr = &dir_fattr,
1833 struct rpc_message msg = {
1834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1840 if (path->len > NFS4_MAXPATHLEN)
1841 return -ENAMETOOLONG;
1842 arg.u.symlink = path;
1843 nfs_fattr_init(fattr);
1844 nfs_fattr_init(&dir_fattr);
1846 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1848 update_changeattr(dir, &res.dir_cinfo);
1849 nfs_post_op_update_inode(dir, res.dir_fattr);
1853 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1854 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1855 struct nfs_fattr *fattr)
1857 struct nfs4_exception exception = { };
1860 err = nfs4_handle_exception(NFS_SERVER(dir),
1861 _nfs4_proc_symlink(dir, name, path, sattr,
1864 } while (exception.retry);
1868 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1869 struct iattr *sattr)
1871 struct nfs_server *server = NFS_SERVER(dir);
1872 struct nfs_fh fhandle;
1873 struct nfs_fattr fattr, dir_fattr;
1874 struct nfs4_create_arg arg = {
1875 .dir_fh = NFS_FH(dir),
1877 .name = &dentry->d_name,
1880 .bitmask = server->attr_bitmask,
1882 struct nfs4_create_res res = {
1886 .dir_fattr = &dir_fattr,
1888 struct rpc_message msg = {
1889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1895 nfs_fattr_init(&fattr);
1896 nfs_fattr_init(&dir_fattr);
1898 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1900 update_changeattr(dir, &res.dir_cinfo);
1901 nfs_post_op_update_inode(dir, res.dir_fattr);
1902 status = nfs_instantiate(dentry, &fhandle, &fattr);
1907 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1908 struct iattr *sattr)
1910 struct nfs4_exception exception = { };
1913 err = nfs4_handle_exception(NFS_SERVER(dir),
1914 _nfs4_proc_mkdir(dir, dentry, sattr),
1916 } while (exception.retry);
1920 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1921 u64 cookie, struct page *page, unsigned int count, int plus)
1923 struct inode *dir = dentry->d_inode;
1924 struct nfs4_readdir_arg args = {
1929 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1931 struct nfs4_readdir_res res;
1932 struct rpc_message msg = {
1933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1940 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1941 dentry->d_parent->d_name.name,
1942 dentry->d_name.name,
1943 (unsigned long long)cookie);
1945 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1946 res.pgbase = args.pgbase;
1947 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1949 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1951 dprintk("%s: returns %d\n", __FUNCTION__, status);
1955 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1956 u64 cookie, struct page *page, unsigned int count, int plus)
1958 struct nfs4_exception exception = { };
1961 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1962 _nfs4_proc_readdir(dentry, cred, cookie,
1965 } while (exception.retry);
1969 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1970 struct iattr *sattr, dev_t rdev)
1972 struct nfs_server *server = NFS_SERVER(dir);
1974 struct nfs_fattr fattr, dir_fattr;
1975 struct nfs4_create_arg arg = {
1976 .dir_fh = NFS_FH(dir),
1978 .name = &dentry->d_name,
1980 .bitmask = server->attr_bitmask,
1982 struct nfs4_create_res res = {
1986 .dir_fattr = &dir_fattr,
1988 struct rpc_message msg = {
1989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1994 int mode = sattr->ia_mode;
1996 nfs_fattr_init(&fattr);
1997 nfs_fattr_init(&dir_fattr);
1999 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2000 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2002 arg.ftype = NF4FIFO;
2003 else if (S_ISBLK(mode)) {
2005 arg.u.device.specdata1 = MAJOR(rdev);
2006 arg.u.device.specdata2 = MINOR(rdev);
2008 else if (S_ISCHR(mode)) {
2010 arg.u.device.specdata1 = MAJOR(rdev);
2011 arg.u.device.specdata2 = MINOR(rdev);
2014 arg.ftype = NF4SOCK;
2016 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2018 update_changeattr(dir, &res.dir_cinfo);
2019 nfs_post_op_update_inode(dir, res.dir_fattr);
2020 status = nfs_instantiate(dentry, &fh, &fattr);
2025 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2026 struct iattr *sattr, dev_t rdev)
2028 struct nfs4_exception exception = { };
2031 err = nfs4_handle_exception(NFS_SERVER(dir),
2032 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2034 } while (exception.retry);
2038 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2039 struct nfs_fsstat *fsstat)
2041 struct nfs4_statfs_arg args = {
2043 .bitmask = server->attr_bitmask,
2045 struct rpc_message msg = {
2046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2051 nfs_fattr_init(fsstat->fattr);
2052 return rpc_call_sync(server->client, &msg, 0);
2055 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2057 struct nfs4_exception exception = { };
2060 err = nfs4_handle_exception(server,
2061 _nfs4_proc_statfs(server, fhandle, fsstat),
2063 } while (exception.retry);
2067 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2068 struct nfs_fsinfo *fsinfo)
2070 struct nfs4_fsinfo_arg args = {
2072 .bitmask = server->attr_bitmask,
2074 struct rpc_message msg = {
2075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2080 return rpc_call_sync(server->client, &msg, 0);
2083 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2085 struct nfs4_exception exception = { };
2089 err = nfs4_handle_exception(server,
2090 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2092 } while (exception.retry);
2096 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2098 nfs_fattr_init(fsinfo->fattr);
2099 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2102 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2103 struct nfs_pathconf *pathconf)
2105 struct nfs4_pathconf_arg args = {
2107 .bitmask = server->attr_bitmask,
2109 struct rpc_message msg = {
2110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2112 .rpc_resp = pathconf,
2115 /* None of the pathconf attributes are mandatory to implement */
2116 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2117 memset(pathconf, 0, sizeof(*pathconf));
2121 nfs_fattr_init(pathconf->fattr);
2122 return rpc_call_sync(server->client, &msg, 0);
2125 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2126 struct nfs_pathconf *pathconf)
2128 struct nfs4_exception exception = { };
2132 err = nfs4_handle_exception(server,
2133 _nfs4_proc_pathconf(server, fhandle, pathconf),
2135 } while (exception.retry);
2139 static void nfs4_read_done(struct rpc_task *task, void *calldata)
2141 struct nfs_read_data *data = calldata;
2142 struct inode *inode = data->inode;
2144 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2145 rpc_restart_call(task);
2148 if (task->tk_status > 0)
2149 renew_lease(NFS_SERVER(inode), data->timestamp);
2150 /* Call back common NFS readpage processing */
2151 nfs_readpage_result(task, calldata);
2154 static const struct rpc_call_ops nfs4_read_ops = {
2155 .rpc_call_done = nfs4_read_done,
2156 .rpc_release = nfs_readdata_release,
2160 nfs4_proc_read_setup(struct nfs_read_data *data)
2162 struct rpc_task *task = &data->task;
2163 struct rpc_message msg = {
2164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2165 .rpc_argp = &data->args,
2166 .rpc_resp = &data->res,
2167 .rpc_cred = data->cred,
2169 struct inode *inode = data->inode;
2172 data->timestamp = jiffies;
2174 /* N.B. Do we need to test? Never called for swapfile inode */
2175 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2177 /* Finalize the task. */
2178 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
2179 rpc_call_setup(task, &msg, 0);
2182 static void nfs4_write_done(struct rpc_task *task, void *calldata)
2184 struct nfs_write_data *data = calldata;
2185 struct inode *inode = data->inode;
2187 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2188 rpc_restart_call(task);
2191 if (task->tk_status >= 0) {
2192 renew_lease(NFS_SERVER(inode), data->timestamp);
2193 nfs_post_op_update_inode(inode, data->res.fattr);
2195 /* Call back common NFS writeback processing */
2196 nfs_writeback_done(task, calldata);
2199 static const struct rpc_call_ops nfs4_write_ops = {
2200 .rpc_call_done = nfs4_write_done,
2201 .rpc_release = nfs_writedata_release,
2205 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2207 struct rpc_task *task = &data->task;
2208 struct rpc_message msg = {
2209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2210 .rpc_argp = &data->args,
2211 .rpc_resp = &data->res,
2212 .rpc_cred = data->cred,
2214 struct inode *inode = data->inode;
2215 struct nfs_server *server = NFS_SERVER(inode);
2219 if (how & FLUSH_STABLE) {
2220 if (!NFS_I(inode)->ncommit)
2221 stable = NFS_FILE_SYNC;
2223 stable = NFS_DATA_SYNC;
2225 stable = NFS_UNSTABLE;
2226 data->args.stable = stable;
2227 data->args.bitmask = server->attr_bitmask;
2228 data->res.server = server;
2230 data->timestamp = jiffies;
2232 /* Set the initial flags for the task. */
2233 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2235 /* Finalize the task. */
2236 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
2237 rpc_call_setup(task, &msg, 0);
2240 static void nfs4_commit_done(struct rpc_task *task, void *calldata)
2242 struct nfs_write_data *data = calldata;
2243 struct inode *inode = data->inode;
2245 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2246 rpc_restart_call(task);
2249 if (task->tk_status >= 0)
2250 nfs_post_op_update_inode(inode, data->res.fattr);
2251 /* Call back common NFS writeback processing */
2252 nfs_commit_done(task, calldata);
2255 static const struct rpc_call_ops nfs4_commit_ops = {
2256 .rpc_call_done = nfs4_commit_done,
2257 .rpc_release = nfs_commit_release,
2261 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2263 struct rpc_task *task = &data->task;
2264 struct rpc_message msg = {
2265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2266 .rpc_argp = &data->args,
2267 .rpc_resp = &data->res,
2268 .rpc_cred = data->cred,
2270 struct inode *inode = data->inode;
2271 struct nfs_server *server = NFS_SERVER(inode);
2274 data->args.bitmask = server->attr_bitmask;
2275 data->res.server = server;
2277 /* Set the initial flags for the task. */
2278 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2280 /* Finalize the task. */
2281 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
2282 rpc_call_setup(task, &msg, 0);
2286 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2287 * standalone procedure for queueing an asynchronous RENEW.
2289 static void nfs4_renew_done(struct rpc_task *task, void *data)
2291 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2292 unsigned long timestamp = (unsigned long)data;
2294 if (task->tk_status < 0) {
2295 switch (task->tk_status) {
2296 case -NFS4ERR_STALE_CLIENTID:
2297 case -NFS4ERR_EXPIRED:
2298 case -NFS4ERR_CB_PATH_DOWN:
2299 nfs4_schedule_state_recovery(clp);
2303 spin_lock(&clp->cl_lock);
2304 if (time_before(clp->cl_last_renewal,timestamp))
2305 clp->cl_last_renewal = timestamp;
2306 spin_unlock(&clp->cl_lock);
2309 static const struct rpc_call_ops nfs4_renew_ops = {
2310 .rpc_call_done = nfs4_renew_done,
2314 nfs4_proc_async_renew(struct nfs4_client *clp)
2316 struct rpc_message msg = {
2317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2319 .rpc_cred = clp->cl_cred,
2322 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2323 &nfs4_renew_ops, (void *)jiffies);
2327 nfs4_proc_renew(struct nfs4_client *clp)
2329 struct rpc_message msg = {
2330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2332 .rpc_cred = clp->cl_cred,
2334 unsigned long now = jiffies;
2337 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2340 spin_lock(&clp->cl_lock);
2341 if (time_before(clp->cl_last_renewal,now))
2342 clp->cl_last_renewal = now;
2343 spin_unlock(&clp->cl_lock);
2347 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2349 return (server->caps & NFS_CAP_ACLS)
2350 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2351 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2354 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2355 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2358 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2360 static void buf_to_pages(const void *buf, size_t buflen,
2361 struct page **pages, unsigned int *pgbase)
2363 const void *p = buf;
2365 *pgbase = offset_in_page(buf);
2367 while (p < buf + buflen) {
2368 *(pages++) = virt_to_page(p);
2369 p += PAGE_CACHE_SIZE;
2373 struct nfs4_cached_acl {
2379 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2381 struct nfs_inode *nfsi = NFS_I(inode);
2383 spin_lock(&inode->i_lock);
2384 kfree(nfsi->nfs4_acl);
2385 nfsi->nfs4_acl = acl;
2386 spin_unlock(&inode->i_lock);
2389 static void nfs4_zap_acl_attr(struct inode *inode)
2391 nfs4_set_cached_acl(inode, NULL);
2394 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2396 struct nfs_inode *nfsi = NFS_I(inode);
2397 struct nfs4_cached_acl *acl;
2400 spin_lock(&inode->i_lock);
2401 acl = nfsi->nfs4_acl;
2404 if (buf == NULL) /* user is just asking for length */
2406 if (acl->cached == 0)
2408 ret = -ERANGE; /* see getxattr(2) man page */
2409 if (acl->len > buflen)
2411 memcpy(buf, acl->data, acl->len);
2415 spin_unlock(&inode->i_lock);
2419 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2421 struct nfs4_cached_acl *acl;
2423 if (buf && acl_len <= PAGE_SIZE) {
2424 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2428 memcpy(acl->data, buf, acl_len);
2430 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2437 nfs4_set_cached_acl(inode, acl);
2440 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2442 struct page *pages[NFS4ACL_MAXPAGES];
2443 struct nfs_getaclargs args = {
2444 .fh = NFS_FH(inode),
2448 size_t resp_len = buflen;
2450 struct rpc_message msg = {
2451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2453 .rpc_resp = &resp_len,
2455 struct page *localpage = NULL;
2458 if (buflen < PAGE_SIZE) {
2459 /* As long as we're doing a round trip to the server anyway,
2460 * let's be prepared for a page of acl data. */
2461 localpage = alloc_page(GFP_KERNEL);
2462 resp_buf = page_address(localpage);
2463 if (localpage == NULL)
2465 args.acl_pages[0] = localpage;
2466 args.acl_pgbase = 0;
2467 resp_len = args.acl_len = PAGE_SIZE;
2470 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2472 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2475 if (resp_len > args.acl_len)
2476 nfs4_write_cached_acl(inode, NULL, resp_len);
2478 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2481 if (resp_len > buflen)
2484 memcpy(buf, resp_buf, resp_len);
2489 __free_page(localpage);
2493 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2495 struct nfs_server *server = NFS_SERVER(inode);
2498 if (!nfs4_server_supports_acls(server))
2500 ret = nfs_revalidate_inode(server, inode);
2503 ret = nfs4_read_cached_acl(inode, buf, buflen);
2506 return nfs4_get_acl_uncached(inode, buf, buflen);
2509 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2511 struct nfs_server *server = NFS_SERVER(inode);
2512 struct page *pages[NFS4ACL_MAXPAGES];
2513 struct nfs_setaclargs arg = {
2514 .fh = NFS_FH(inode),
2518 struct rpc_message msg = {
2519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2525 if (!nfs4_server_supports_acls(server))
2527 nfs_inode_return_delegation(inode);
2528 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2529 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2531 nfs4_write_cached_acl(inode, buf, buflen);
2536 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2538 struct nfs4_client *clp = server->nfs4_state;
2540 if (!clp || task->tk_status >= 0)
2542 switch(task->tk_status) {
2543 case -NFS4ERR_STALE_CLIENTID:
2544 case -NFS4ERR_STALE_STATEID:
2545 case -NFS4ERR_EXPIRED:
2546 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2547 nfs4_schedule_state_recovery(clp);
2548 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2549 rpc_wake_up_task(task);
2550 task->tk_status = 0;
2552 case -NFS4ERR_GRACE:
2553 case -NFS4ERR_DELAY:
2554 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2555 task->tk_status = 0;
2557 case -NFS4ERR_OLD_STATEID:
2558 task->tk_status = 0;
2561 task->tk_status = nfs4_map_errors(task->tk_status);
2565 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2569 int interruptible, res = 0;
2573 rpc_clnt_sigmask(clnt, &oldset);
2574 interruptible = TASK_UNINTERRUPTIBLE;
2576 interruptible = TASK_INTERRUPTIBLE;
2577 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2578 nfs4_schedule_state_recovery(clp);
2579 if (clnt->cl_intr && signalled())
2581 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2583 finish_wait(&clp->cl_waitq, &wait);
2584 rpc_clnt_sigunmask(clnt, &oldset);
2588 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2596 *timeout = NFS4_POLL_RETRY_MIN;
2597 if (*timeout > NFS4_POLL_RETRY_MAX)
2598 *timeout = NFS4_POLL_RETRY_MAX;
2599 rpc_clnt_sigmask(clnt, &oldset);
2600 if (clnt->cl_intr) {
2601 schedule_timeout_interruptible(*timeout);
2605 schedule_timeout_uninterruptible(*timeout);
2606 rpc_clnt_sigunmask(clnt, &oldset);
2611 /* This is the error handling routine for processes that are allowed
2614 int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2616 struct nfs4_client *clp = server->nfs4_state;
2617 int ret = errorcode;
2619 exception->retry = 0;
2623 case -NFS4ERR_STALE_CLIENTID:
2624 case -NFS4ERR_STALE_STATEID:
2625 case -NFS4ERR_EXPIRED:
2626 ret = nfs4_wait_clnt_recover(server->client, clp);
2628 exception->retry = 1;
2630 case -NFS4ERR_GRACE:
2631 case -NFS4ERR_DELAY:
2632 ret = nfs4_delay(server->client, &exception->timeout);
2635 case -NFS4ERR_OLD_STATEID:
2636 exception->retry = 1;
2638 /* We failed to handle the error */
2639 return nfs4_map_errors(ret);
2642 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2644 nfs4_verifier sc_verifier;
2645 struct nfs4_setclientid setclientid = {
2646 .sc_verifier = &sc_verifier,
2649 struct rpc_message msg = {
2650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2651 .rpc_argp = &setclientid,
2653 .rpc_cred = clp->cl_cred,
2659 p = (u32*)sc_verifier.data;
2660 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2661 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2664 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2665 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2666 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2667 clp->cl_cred->cr_ops->cr_name,
2668 clp->cl_id_uniquifier);
2669 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2670 sizeof(setclientid.sc_netid), "tcp");
2671 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2672 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2673 clp->cl_ipaddr, port >> 8, port & 255);
2675 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2676 if (status != -NFS4ERR_CLID_INUSE)
2681 ssleep(clp->cl_lease_time + 1);
2683 if (++clp->cl_id_uniquifier == 0)
2690 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2692 struct nfs_fsinfo fsinfo;
2693 struct rpc_message msg = {
2694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2696 .rpc_resp = &fsinfo,
2697 .rpc_cred = clp->cl_cred,
2703 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2705 spin_lock(&clp->cl_lock);
2706 clp->cl_lease_time = fsinfo.lease_time * HZ;
2707 clp->cl_last_renewal = now;
2708 spin_unlock(&clp->cl_lock);
2713 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2715 struct nfs4_delegreturnargs args = {
2716 .fhandle = NFS_FH(inode),
2719 struct rpc_message msg = {
2720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2725 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2728 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2730 struct nfs_server *server = NFS_SERVER(inode);
2731 struct nfs4_exception exception = { };
2734 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2736 case -NFS4ERR_STALE_STATEID:
2737 case -NFS4ERR_EXPIRED:
2738 nfs4_schedule_state_recovery(server->nfs4_state);
2742 err = nfs4_handle_exception(server, err, &exception);
2743 } while (exception.retry);
2747 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2748 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2751 * sleep, with exponential backoff, and retry the LOCK operation.
2753 static unsigned long
2754 nfs4_set_lock_task_retry(unsigned long timeout)
2756 schedule_timeout_interruptible(timeout);
2758 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2759 return NFS4_LOCK_MAXTIMEOUT;
2764 nfs4_lck_type(int cmd, struct file_lock *request)
2767 switch (request->fl_type) {
2769 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2771 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2773 return NFS4_WRITE_LT;
2779 static inline uint64_t
2780 nfs4_lck_length(struct file_lock *request)
2782 if (request->fl_end == OFFSET_MAX)
2783 return ~(uint64_t)0;
2784 return request->fl_end - request->fl_start + 1;
2787 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2789 struct inode *inode = state->inode;
2790 struct nfs_server *server = NFS_SERVER(inode);
2791 struct nfs4_client *clp = server->nfs4_state;
2792 struct nfs_lockargs arg = {
2793 .fh = NFS_FH(inode),
2794 .type = nfs4_lck_type(cmd, request),
2795 .offset = request->fl_start,
2796 .length = nfs4_lck_length(request),
2798 struct nfs_lockres res = {
2801 struct rpc_message msg = {
2802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2805 .rpc_cred = state->owner->so_cred,
2807 struct nfs_lowner nlo;
2808 struct nfs4_lock_state *lsp;
2811 down_read(&clp->cl_sem);
2812 nlo.clientid = clp->cl_clientid;
2813 status = nfs4_set_lock_state(state, request);
2816 lsp = request->fl_u.nfs4_fl.owner;
2817 nlo.id = lsp->ls_id;
2819 status = rpc_call_sync(server->client, &msg, 0);
2821 request->fl_type = F_UNLCK;
2822 } else if (status == -NFS4ERR_DENIED) {
2823 int64_t len, start, end;
2824 start = res.u.denied.offset;
2825 len = res.u.denied.length;
2826 end = start + len - 1;
2827 if (end < 0 || len == 0)
2828 request->fl_end = OFFSET_MAX;
2830 request->fl_end = (loff_t)end;
2831 request->fl_start = (loff_t)start;
2832 request->fl_type = F_WRLCK;
2833 if (res.u.denied.type & 1)
2834 request->fl_type = F_RDLCK;
2835 request->fl_pid = 0;
2839 up_read(&clp->cl_sem);
2843 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2845 struct nfs4_exception exception = { };
2849 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2850 _nfs4_proc_getlk(state, cmd, request),
2852 } while (exception.retry);
2856 static int do_vfs_lock(struct file *file, struct file_lock *fl)
2859 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2861 res = posix_lock_file_wait(file, fl);
2864 res = flock_lock_file_wait(file, fl);
2870 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2875 struct nfs4_unlockdata {
2876 struct nfs_lockargs arg;
2877 struct nfs_locku_opargs luargs;
2878 struct nfs_lockres res;
2879 struct nfs4_lock_state *lsp;
2880 struct nfs_open_context *ctx;
2883 static void nfs4_locku_release_calldata(void *data)
2885 struct nfs4_unlockdata *calldata = data;
2886 nfs_free_seqid(calldata->luargs.seqid);
2887 nfs4_put_lock_state(calldata->lsp);
2888 put_nfs_open_context(calldata->ctx);
2892 static void nfs4_locku_done(struct rpc_task *task, void *data)
2894 struct nfs4_unlockdata *calldata = data;
2896 if (RPC_ASSASSINATED(task))
2898 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2899 switch (task->tk_status) {
2901 memcpy(calldata->lsp->ls_stateid.data,
2902 calldata->res.u.stateid.data,
2903 sizeof(calldata->lsp->ls_stateid.data));
2905 case -NFS4ERR_STALE_STATEID:
2906 case -NFS4ERR_EXPIRED:
2907 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2910 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2911 rpc_restart_call(task);
2916 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
2918 struct nfs4_unlockdata *calldata = data;
2919 struct rpc_message msg = {
2920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2921 .rpc_argp = &calldata->arg,
2922 .rpc_resp = &calldata->res,
2923 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
2927 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
2930 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
2931 /* Note: exit _without_ running nfs4_locku_done */
2932 task->tk_action = NULL;
2935 rpc_call_setup(task, &msg, 0);
2938 static const struct rpc_call_ops nfs4_locku_ops = {
2939 .rpc_call_prepare = nfs4_locku_prepare,
2940 .rpc_call_done = nfs4_locku_done,
2941 .rpc_release = nfs4_locku_release_calldata,
2944 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2946 struct nfs4_unlockdata *calldata;
2947 struct inode *inode = state->inode;
2948 struct nfs_server *server = NFS_SERVER(inode);
2949 struct nfs4_lock_state *lsp;
2950 struct rpc_task *task;
2953 /* Is this a delegated lock? */
2954 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
2957 status = nfs4_set_lock_state(state, request);
2960 lsp = request->fl_u.nfs4_fl.owner;
2961 /* We might have lost the locks! */
2962 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2965 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
2966 if (calldata == NULL)
2968 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2969 if (calldata->luargs.seqid == NULL) {
2973 calldata->luargs.stateid = &lsp->ls_stateid;
2974 calldata->arg.fh = NFS_FH(inode);
2975 calldata->arg.type = nfs4_lck_type(cmd, request);
2976 calldata->arg.offset = request->fl_start;
2977 calldata->arg.length = nfs4_lck_length(request);
2978 calldata->arg.u.locku = &calldata->luargs;
2979 calldata->res.server = server;
2980 calldata->lsp = lsp;
2981 atomic_inc(&lsp->ls_count);
2983 /* Ensure we don't close file until we're done freeing locks! */
2984 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
2986 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_locku_ops, calldata);
2987 if (!IS_ERR(task)) {
2988 status = nfs4_wait_for_completion_rpc_task(task);
2989 rpc_release_task(task);
2991 status = PTR_ERR(task);
2992 nfs4_locku_release_calldata(calldata);
2995 do_vfs_lock(request->fl_file, request);
2999 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
3001 struct inode *inode = state->inode;
3002 struct nfs_server *server = NFS_SERVER(inode);
3003 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
3004 struct nfs_lock_opargs largs = {
3005 .lock_stateid = &lsp->ls_stateid,
3006 .open_stateid = &state->stateid,
3008 .clientid = server->nfs4_state->cl_clientid,
3013 struct nfs_lockargs arg = {
3014 .fh = NFS_FH(inode),
3015 .type = nfs4_lck_type(cmd, request),
3016 .offset = request->fl_start,
3017 .length = nfs4_lck_length(request),
3022 struct nfs_lockres res = {
3025 struct rpc_message msg = {
3026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3029 .rpc_cred = state->owner->so_cred,
3031 int status = -ENOMEM;
3033 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3034 if (largs.lock_seqid == NULL)
3036 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
3037 struct nfs4_state_owner *owner = state->owner;
3039 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
3040 if (largs.open_seqid == NULL)
3042 largs.new_lock_owner = 1;
3043 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3044 /* increment open seqid on success, and seqid mutating errors */
3045 if (largs.new_lock_owner != 0) {
3046 nfs_increment_open_seqid(status, largs.open_seqid);
3048 nfs_confirm_seqid(&lsp->ls_seqid, 0);
3050 nfs_free_seqid(largs.open_seqid);
3052 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3053 /* increment lock seqid on success, and seqid mutating errors*/
3054 nfs_increment_lock_seqid(status, largs.lock_seqid);
3055 /* save the returned stateid. */
3057 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
3058 sizeof(lsp->ls_stateid.data));
3059 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3060 } else if (status == -NFS4ERR_DENIED)
3063 nfs_free_seqid(largs.lock_seqid);
3067 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3069 struct nfs_server *server = NFS_SERVER(state->inode);
3070 struct nfs4_exception exception = { };
3073 /* Cache the lock if possible... */
3074 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3077 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3078 if (err != -NFS4ERR_DELAY)
3080 nfs4_handle_exception(server, err, &exception);
3081 } while (exception.retry);
3085 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3087 struct nfs_server *server = NFS_SERVER(state->inode);
3088 struct nfs4_exception exception = { };
3091 err = nfs4_set_lock_state(state, request);
3095 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3096 if (err != -NFS4ERR_DELAY)
3098 nfs4_handle_exception(server, err, &exception);
3099 } while (exception.retry);
3103 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3105 struct nfs4_client *clp = state->owner->so_client;
3108 /* Is this a delegated open? */
3109 if (NFS_I(state->inode)->delegation_state != 0) {
3110 /* Yes: cache locks! */
3111 status = do_vfs_lock(request->fl_file, request);
3112 /* ...but avoid races with delegation recall... */
3113 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
3116 down_read(&clp->cl_sem);
3117 status = nfs4_set_lock_state(state, request);
3120 status = _nfs4_do_setlk(state, cmd, request, 0);
3123 /* Note: we always want to sleep here! */
3124 request->fl_flags |= FL_SLEEP;
3125 if (do_vfs_lock(request->fl_file, request) < 0)
3126 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3128 up_read(&clp->cl_sem);
3132 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3134 struct nfs4_exception exception = { };
3138 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3139 _nfs4_proc_setlk(state, cmd, request),
3141 } while (exception.retry);
3146 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3148 struct nfs_open_context *ctx;
3149 struct nfs4_state *state;
3150 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3153 /* verify open state */
3154 ctx = (struct nfs_open_context *)filp->private_data;
3157 if (request->fl_start < 0 || request->fl_end < 0)
3161 return nfs4_proc_getlk(state, F_GETLK, request);
3163 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3166 if (request->fl_type == F_UNLCK)
3167 return nfs4_proc_unlck(state, cmd, request);
3170 status = nfs4_proc_setlk(state, cmd, request);
3171 if ((status != -EAGAIN) || IS_SETLK(cmd))
3173 timeout = nfs4_set_lock_task_retry(timeout);
3174 status = -ERESTARTSYS;
3177 } while(status < 0);
3181 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3183 struct nfs_server *server = NFS_SERVER(state->inode);
3184 struct nfs4_exception exception = { };
3187 err = nfs4_set_lock_state(state, fl);
3191 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3192 if (err != -NFS4ERR_DELAY)
3194 err = nfs4_handle_exception(server, err, &exception);
3195 } while (exception.retry);
3200 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3202 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3203 size_t buflen, int flags)
3205 struct inode *inode = dentry->d_inode;
3207 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3210 if (!S_ISREG(inode->i_mode) &&
3211 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3214 return nfs4_proc_set_acl(inode, buf, buflen);
3217 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3218 * and that's what we'll do for e.g. user attributes that haven't been set.
3219 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3220 * attributes in kernel-managed attribute namespaces. */
3221 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3224 struct inode *inode = dentry->d_inode;
3226 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3229 return nfs4_proc_get_acl(inode, buf, buflen);
3232 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3234 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3236 if (buf && buflen < len)
3239 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3243 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3244 .recover_open = nfs4_open_reclaim,
3245 .recover_lock = nfs4_lock_reclaim,
3248 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3249 .recover_open = nfs4_open_expired,
3250 .recover_lock = nfs4_lock_expired,
3253 static struct inode_operations nfs4_file_inode_operations = {
3254 .permission = nfs_permission,
3255 .getattr = nfs_getattr,
3256 .setattr = nfs_setattr,
3257 .getxattr = nfs4_getxattr,
3258 .setxattr = nfs4_setxattr,
3259 .listxattr = nfs4_listxattr,
3262 struct nfs_rpc_ops nfs_v4_clientops = {
3263 .version = 4, /* protocol version */
3264 .dentry_ops = &nfs4_dentry_operations,
3265 .dir_inode_ops = &nfs4_dir_inode_operations,
3266 .file_inode_ops = &nfs4_file_inode_operations,
3267 .getroot = nfs4_proc_get_root,
3268 .getattr = nfs4_proc_getattr,
3269 .setattr = nfs4_proc_setattr,
3270 .lookup = nfs4_proc_lookup,
3271 .access = nfs4_proc_access,
3272 .readlink = nfs4_proc_readlink,
3273 .read = nfs4_proc_read,
3274 .write = nfs4_proc_write,
3275 .commit = nfs4_proc_commit,
3276 .create = nfs4_proc_create,
3277 .remove = nfs4_proc_remove,
3278 .unlink_setup = nfs4_proc_unlink_setup,
3279 .unlink_done = nfs4_proc_unlink_done,
3280 .rename = nfs4_proc_rename,
3281 .link = nfs4_proc_link,
3282 .symlink = nfs4_proc_symlink,
3283 .mkdir = nfs4_proc_mkdir,
3284 .rmdir = nfs4_proc_remove,
3285 .readdir = nfs4_proc_readdir,
3286 .mknod = nfs4_proc_mknod,
3287 .statfs = nfs4_proc_statfs,
3288 .fsinfo = nfs4_proc_fsinfo,
3289 .pathconf = nfs4_proc_pathconf,
3290 .decode_dirent = nfs4_decode_dirent,
3291 .read_setup = nfs4_proc_read_setup,
3292 .write_setup = nfs4_proc_write_setup,
3293 .commit_setup = nfs4_proc_commit_setup,
3294 .file_open = nfs_open,
3295 .file_release = nfs_release,
3296 .lock = nfs4_proc_lock,
3297 .clear_acl_cache = nfs4_zap_acl_attr,