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 struct nfs4_opendata {
198 struct nfs_openargs o_arg;
199 struct nfs_openres o_res;
200 struct nfs_fattr f_attr;
201 struct nfs_fattr dir_attr;
202 struct dentry *dentry;
204 struct nfs4_state_owner *owner;
208 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
209 struct nfs4_state_owner *sp, int flags,
210 const struct iattr *attrs)
212 struct dentry *parent = dget_parent(dentry);
213 struct inode *dir = parent->d_inode;
214 struct nfs_server *server = NFS_SERVER(dir);
215 struct nfs4_opendata *p;
217 p = kzalloc(sizeof(*p), GFP_KERNEL);
220 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
221 if (p->o_arg.seqid == NULL)
223 p->dentry = dget(dentry);
226 atomic_inc(&sp->so_count);
227 p->o_arg.fh = NFS_FH(dir);
228 p->o_arg.open_flags = flags,
229 p->o_arg.clientid = server->nfs4_state->cl_clientid;
230 p->o_arg.id = sp->so_id;
231 p->o_arg.name = &dentry->d_name;
232 p->o_arg.server = server;
233 p->o_arg.bitmask = server->attr_bitmask;
234 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
235 p->o_res.f_attr = &p->f_attr;
236 p->o_res.dir_attr = &p->dir_attr;
237 p->o_res.server = server;
238 nfs_fattr_init(&p->f_attr);
239 nfs_fattr_init(&p->dir_attr);
240 if (flags & O_EXCL) {
241 u32 *s = (u32 *) p->o_arg.u.verifier.data;
244 } else if (flags & O_CREAT) {
245 p->o_arg.u.attrs = &p->attrs;
246 memcpy(&p->attrs, attrs, sizeof(p->attrs));
256 static void nfs4_opendata_free(struct nfs4_opendata *p)
259 nfs_free_seqid(p->o_arg.seqid);
260 nfs4_put_state_owner(p->owner);
267 /* Helper for asynchronous RPC calls */
268 static int nfs4_call_async(struct rpc_clnt *clnt,
269 const struct rpc_call_ops *tk_ops, void *calldata)
271 struct rpc_task *task;
273 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
279 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
284 rpc_clnt_sigmask(task->tk_client, &oldset);
285 ret = rpc_wait_for_completion_task(task);
286 rpc_clnt_sigunmask(task->tk_client, &oldset);
290 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
292 struct inode *inode = state->inode;
294 open_flags &= (FMODE_READ|FMODE_WRITE);
295 /* Protect against nfs4_find_state_byowner() */
296 spin_lock(&state->owner->so_lock);
297 spin_lock(&inode->i_lock);
298 memcpy(&state->stateid, stateid, sizeof(state->stateid));
299 if ((open_flags & FMODE_WRITE))
301 if (open_flags & FMODE_READ)
303 nfs4_state_set_mode_locked(state, state->state | open_flags);
304 spin_unlock(&inode->i_lock);
305 spin_unlock(&state->owner->so_lock);
310 * reclaim state on the server after a reboot.
312 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
314 struct inode *inode = state->inode;
315 struct nfs_server *server = NFS_SERVER(inode);
316 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
317 struct nfs_openargs o_arg = {
320 .open_flags = state->state,
321 .clientid = server->nfs4_state->cl_clientid,
322 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
323 .bitmask = server->attr_bitmask,
325 struct nfs_openres o_res = {
326 .server = server, /* Grrr */
328 struct rpc_message msg = {
329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
332 .rpc_cred = sp->so_cred,
336 if (delegation != NULL) {
337 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
338 memcpy(&state->stateid, &delegation->stateid,
339 sizeof(state->stateid));
340 set_bit(NFS_DELEGATED_STATE, &state->flags);
343 o_arg.u.delegation_type = delegation->type;
345 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
346 if (o_arg.seqid == NULL)
348 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
349 /* Confirm the sequence as being established */
350 nfs_confirm_seqid(&sp->so_seqid, status);
351 nfs_increment_open_seqid(status, o_arg.seqid);
353 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
354 if (o_res.delegation_type != 0) {
355 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
356 /* Did the server issue an immediate delegation recall? */
358 nfs_async_inode_return_delegation(inode, &o_res.stateid);
361 nfs_free_seqid(o_arg.seqid);
362 clear_bit(NFS_DELEGATED_STATE, &state->flags);
363 /* Ensure we update the inode attributes */
368 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
370 struct nfs_server *server = NFS_SERVER(state->inode);
371 struct nfs4_exception exception = { };
374 err = _nfs4_open_reclaim(sp, state);
375 if (err != -NFS4ERR_DELAY)
377 nfs4_handle_exception(server, err, &exception);
378 } while (exception.retry);
382 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
384 struct nfs4_state_owner *sp = state->owner;
385 struct inode *inode = dentry->d_inode;
386 struct nfs_server *server = NFS_SERVER(inode);
387 struct rpc_message msg = {
388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
389 .rpc_cred = sp->so_cred,
391 struct nfs4_opendata *opendata;
394 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
396 if (state->state == 0)
398 opendata = nfs4_opendata_alloc(dentry, sp, state->state, NULL);
400 if (opendata == NULL)
402 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
403 msg.rpc_argp = &opendata->o_arg;
404 msg.rpc_resp = &opendata->o_res;
405 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
406 sizeof(opendata->o_arg.u.delegation.data));
407 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
408 nfs_increment_open_seqid(status, opendata->o_arg.seqid);
411 if(opendata->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
412 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
413 sp, &opendata->o_res.stateid, opendata->o_arg.seqid);
417 nfs_confirm_seqid(&sp->so_seqid, 0);
419 memcpy(state->stateid.data, opendata->o_res.stateid.data,
420 sizeof(state->stateid.data));
421 clear_bit(NFS_DELEGATED_STATE, &state->flags);
424 nfs4_opendata_free(opendata);
429 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
431 struct nfs4_exception exception = { };
432 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
435 err = _nfs4_open_delegation_recall(dentry, state);
439 case -NFS4ERR_STALE_CLIENTID:
440 case -NFS4ERR_STALE_STATEID:
441 case -NFS4ERR_EXPIRED:
442 /* Don't recall a delegation if it was lost */
443 nfs4_schedule_state_recovery(server->nfs4_state);
446 err = nfs4_handle_exception(server, err, &exception);
447 } while (exception.retry);
451 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)
453 struct nfs_open_confirmargs arg = {
458 struct nfs_open_confirmres res;
459 struct rpc_message msg = {
460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
463 .rpc_cred = sp->so_cred,
467 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
468 /* Confirm the sequence as being established */
469 nfs_confirm_seqid(&sp->so_seqid, status);
470 nfs_increment_open_seqid(status, seqid);
472 memcpy(stateid, &res.stateid, sizeof(*stateid));
476 static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
478 struct nfs_server *server = NFS_SERVER(dir);
479 struct rpc_message msg = {
480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
483 .rpc_cred = sp->so_cred,
487 /* Update sequence id. The caller must serialize! */
488 o_arg->id = sp->so_id;
489 o_arg->clientid = sp->so_client->cl_clientid;
491 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
493 /* OPEN on anything except a regular file is disallowed in NFSv4 */
494 switch (o_res->f_attr->mode & S_IFMT) {
508 nfs_increment_open_seqid(status, o_arg->seqid);
511 if (o_arg->open_flags & O_CREAT) {
512 update_changeattr(dir, &o_res->cinfo);
513 nfs_post_op_update_inode(dir, o_res->dir_attr);
515 nfs_refresh_inode(dir, o_res->dir_attr);
516 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
517 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
518 sp, &o_res->stateid, o_arg->seqid);
522 nfs_confirm_seqid(&sp->so_seqid, 0);
523 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
524 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
529 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
531 struct nfs_access_entry cache;
535 if (openflags & FMODE_READ)
537 if (openflags & FMODE_WRITE)
539 status = nfs_access_get_cached(inode, cred, &cache);
543 /* Be clever: ask server to check for all possible rights */
544 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
546 cache.jiffies = jiffies;
547 status = _nfs4_proc_access(inode, &cache);
550 nfs_access_add_cache(inode, &cache);
552 if ((cache.mask & mask) == mask)
559 * reclaim state on the server after a network partition.
560 * Assumes caller holds the appropriate lock
562 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
564 struct dentry *parent = dget_parent(dentry);
565 struct inode *dir = parent->d_inode;
566 struct inode *inode = state->inode;
567 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
568 struct nfs4_opendata *opendata;
571 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
572 status = _nfs4_do_access(inode, sp->so_cred, state->state);
575 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
576 set_bit(NFS_DELEGATED_STATE, &state->flags);
580 opendata = nfs4_opendata_alloc(dentry, sp, state->state, NULL);
581 if (opendata == NULL)
583 status = _nfs4_proc_open(dir, sp, &opendata->o_arg, &opendata->o_res);
586 /* Check if files differ */
587 if ((opendata->f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
589 /* Has the file handle changed? */
590 if (nfs_compare_fh(&opendata->o_res.fh, NFS_FH(inode)) != 0) {
591 /* Verify if the change attributes are the same */
592 if (opendata->f_attr.change_attr != NFS_I(inode)->change_attr)
594 if (nfs_size_to_loff_t(opendata->f_attr.size) != inode->i_size)
596 /* Lets just pretend that this is the same file */
597 nfs_copy_fh(NFS_FH(inode), &opendata->o_res.fh);
598 NFS_I(inode)->fileid = opendata->f_attr.fileid;
600 memcpy(&state->stateid, &opendata->o_res.stateid, sizeof(state->stateid));
601 if (opendata->o_res.delegation_type != 0) {
602 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
603 nfs_inode_set_delegation(inode, sp->so_cred,
606 nfs_inode_reclaim_delegation(inode, sp->so_cred,
610 nfs4_opendata_free(opendata);
611 clear_bit(NFS_DELEGATED_STATE, &state->flags);
617 /* Invalidate the state owner so we don't ever use it again */
618 nfs4_drop_state_owner(sp);
620 /* Should we be trying to close that stateid? */
624 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
626 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
627 struct nfs4_exception exception = { };
631 err = _nfs4_open_expired(sp, state, dentry);
632 if (err == -NFS4ERR_DELAY)
633 nfs4_handle_exception(server, err, &exception);
634 } while (exception.retry);
638 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
640 struct nfs_inode *nfsi = NFS_I(state->inode);
641 struct nfs_open_context *ctx;
644 spin_lock(&state->inode->i_lock);
645 list_for_each_entry(ctx, &nfsi->open_files, list) {
646 if (ctx->state != state)
648 get_nfs_open_context(ctx);
649 spin_unlock(&state->inode->i_lock);
650 status = nfs4_do_open_expired(sp, state, ctx->dentry);
651 put_nfs_open_context(ctx);
654 spin_unlock(&state->inode->i_lock);
659 * Returns an nfs4_state + an extra reference to the inode
661 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
663 struct nfs_delegation *delegation;
664 struct nfs_server *server = NFS_SERVER(inode);
665 struct nfs4_client *clp = server->nfs4_state;
666 struct nfs_inode *nfsi = NFS_I(inode);
667 struct nfs4_state_owner *sp = NULL;
668 struct nfs4_state *state = NULL;
669 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
672 /* Protect against reboot recovery - NOTE ORDER! */
673 down_read(&clp->cl_sem);
674 /* Protect against delegation recall */
675 down_read(&nfsi->rwsem);
676 delegation = NFS_I(inode)->delegation;
678 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
681 if (!(sp = nfs4_get_state_owner(server, cred))) {
682 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
685 state = nfs4_get_open_state(inode, sp);
690 if ((state->state & open_flags) == open_flags) {
691 spin_lock(&inode->i_lock);
692 if (open_flags & FMODE_READ)
694 if (open_flags & FMODE_WRITE)
696 spin_unlock(&inode->i_lock);
698 } else if (state->state != 0)
702 err = _nfs4_do_access(inode, cred, open_flags);
706 set_bit(NFS_DELEGATED_STATE, &state->flags);
707 update_open_stateid(state, &delegation->stateid, open_flags);
709 nfs4_put_state_owner(sp);
710 up_read(&nfsi->rwsem);
711 up_read(&clp->cl_sem);
718 nfs4_put_open_state(state);
719 nfs4_put_state_owner(sp);
721 up_read(&nfsi->rwsem);
722 up_read(&clp->cl_sem);
724 nfs_inode_return_delegation(inode);
728 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
730 struct nfs4_exception exception = { };
731 struct nfs4_state *res;
735 err = _nfs4_open_delegated(inode, flags, cred, &res);
738 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
740 } while (exception.retry);
745 * Returns an nfs4_state + an referenced inode
747 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
749 struct nfs4_state_owner *sp;
750 struct nfs4_state *state = NULL;
751 struct nfs_server *server = NFS_SERVER(dir);
752 struct nfs4_client *clp = server->nfs4_state;
753 struct inode *inode = NULL;
754 struct nfs4_opendata *opendata;
757 /* Protect against reboot recovery conflicts */
758 down_read(&clp->cl_sem);
760 if (!(sp = nfs4_get_state_owner(server, cred))) {
761 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
764 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
765 if (opendata == NULL)
766 goto err_put_state_owner;
768 status = _nfs4_proc_open(dir, sp, &opendata->o_arg, &opendata->o_res);
770 goto err_opendata_free;
773 inode = nfs_fhget(dir->i_sb, &opendata->o_res.fh, &opendata->f_attr);
775 goto err_opendata_free;
776 state = nfs4_get_open_state(inode, sp);
778 goto err_opendata_free;
779 update_open_stateid(state, &opendata->o_res.stateid, flags);
780 if (opendata->o_res.delegation_type != 0)
781 nfs_inode_set_delegation(inode, cred, &opendata->o_res);
782 nfs4_opendata_free(opendata);
783 nfs4_put_state_owner(sp);
784 up_read(&clp->cl_sem);
788 nfs4_opendata_free(opendata);
790 nfs4_put_state_owner(sp);
792 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
793 up_read(&clp->cl_sem);
801 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
803 struct nfs4_exception exception = { };
804 struct nfs4_state *res;
808 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
811 /* NOTE: BAD_SEQID means the server and client disagree about the
812 * book-keeping w.r.t. state-changing operations
813 * (OPEN/CLOSE/LOCK/LOCKU...)
814 * It is actually a sign of a bug on the client or on the server.
816 * If we receive a BAD_SEQID error in the particular case of
817 * doing an OPEN, we assume that nfs_increment_open_seqid() will
818 * have unhashed the old state_owner for us, and that we can
819 * therefore safely retry using a new one. We should still warn
822 if (status == -NFS4ERR_BAD_SEQID) {
823 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
828 * BAD_STATEID on OPEN means that the server cancelled our
829 * state before it received the OPEN_CONFIRM.
830 * Recover by retrying the request as per the discussion
831 * on Page 181 of RFC3530.
833 if (status == -NFS4ERR_BAD_STATEID) {
837 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
838 status, &exception));
839 } while (exception.retry);
843 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
844 struct nfs_fh *fhandle, struct iattr *sattr,
845 struct nfs4_state *state)
847 struct nfs_setattrargs arg = {
851 .bitmask = server->attr_bitmask,
853 struct nfs_setattrres res = {
857 struct rpc_message msg = {
858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
864 nfs_fattr_init(fattr);
867 msg.rpc_cred = state->owner->so_cred;
868 nfs4_copy_stateid(&arg.stateid, state, current->files);
870 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
872 status = rpc_call_sync(server->client, &msg, 0);
876 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
877 struct nfs_fh *fhandle, struct iattr *sattr,
878 struct nfs4_state *state)
880 struct nfs4_exception exception = { };
883 err = nfs4_handle_exception(server,
884 _nfs4_do_setattr(server, fattr, fhandle, sattr,
887 } while (exception.retry);
891 struct nfs4_closedata {
893 struct nfs4_state *state;
894 struct nfs_closeargs arg;
895 struct nfs_closeres res;
896 struct nfs_fattr fattr;
899 static void nfs4_free_closedata(void *data)
901 struct nfs4_closedata *calldata = data;
902 struct nfs4_state_owner *sp = calldata->state->owner;
904 nfs4_put_open_state(calldata->state);
905 nfs_free_seqid(calldata->arg.seqid);
906 nfs4_put_state_owner(sp);
910 static void nfs4_close_done(struct rpc_task *task, void *data)
912 struct nfs4_closedata *calldata = data;
913 struct nfs4_state *state = calldata->state;
914 struct nfs_server *server = NFS_SERVER(calldata->inode);
916 if (RPC_ASSASSINATED(task))
918 /* hmm. we are done with the inode, and in the process of freeing
919 * the state_owner. we keep this around to process errors
921 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
922 switch (task->tk_status) {
924 memcpy(&state->stateid, &calldata->res.stateid,
925 sizeof(state->stateid));
927 case -NFS4ERR_STALE_STATEID:
928 case -NFS4ERR_EXPIRED:
929 nfs4_schedule_state_recovery(server->nfs4_state);
932 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
933 rpc_restart_call(task);
937 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
940 static void nfs4_close_prepare(struct rpc_task *task, void *data)
942 struct nfs4_closedata *calldata = data;
943 struct nfs4_state *state = calldata->state;
944 struct rpc_message msg = {
945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
946 .rpc_argp = &calldata->arg,
947 .rpc_resp = &calldata->res,
948 .rpc_cred = state->owner->so_cred,
950 int mode = 0, old_mode;
952 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
954 /* Recalculate the new open mode in case someone reopened the file
955 * while we were waiting in line to be scheduled.
957 spin_lock(&state->owner->so_lock);
958 spin_lock(&calldata->inode->i_lock);
959 mode = old_mode = state->state;
960 if (state->nreaders == 0)
962 if (state->nwriters == 0)
963 mode &= ~FMODE_WRITE;
964 nfs4_state_set_mode_locked(state, mode);
965 spin_unlock(&calldata->inode->i_lock);
966 spin_unlock(&state->owner->so_lock);
967 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
968 /* Note: exit _without_ calling nfs4_close_done */
969 task->tk_action = NULL;
972 nfs_fattr_init(calldata->res.fattr);
974 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
975 calldata->arg.open_flags = mode;
976 rpc_call_setup(task, &msg, 0);
979 static const struct rpc_call_ops nfs4_close_ops = {
980 .rpc_call_prepare = nfs4_close_prepare,
981 .rpc_call_done = nfs4_close_done,
982 .rpc_release = nfs4_free_closedata,
986 * It is possible for data to be read/written from a mem-mapped file
987 * after the sys_close call (which hits the vfs layer as a flush).
988 * This means that we can't safely call nfsv4 close on a file until
989 * the inode is cleared. This in turn means that we are not good
990 * NFSv4 citizens - we do not indicate to the server to update the file's
991 * share state even when we are done with one of the three share
992 * stateid's in the inode.
994 * NOTE: Caller must be holding the sp->so_owner semaphore!
996 int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
998 struct nfs_server *server = NFS_SERVER(inode);
999 struct nfs4_closedata *calldata;
1000 int status = -ENOMEM;
1002 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1003 if (calldata == NULL)
1005 calldata->inode = inode;
1006 calldata->state = state;
1007 calldata->arg.fh = NFS_FH(inode);
1008 calldata->arg.stateid = &state->stateid;
1009 /* Serialization for the sequence id */
1010 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1011 if (calldata->arg.seqid == NULL)
1012 goto out_free_calldata;
1013 calldata->arg.bitmask = server->attr_bitmask;
1014 calldata->res.fattr = &calldata->fattr;
1015 calldata->res.server = server;
1017 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
1021 nfs_free_seqid(calldata->arg.seqid);
1028 static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1032 filp = lookup_instantiate_filp(nd, dentry, NULL);
1033 if (!IS_ERR(filp)) {
1034 struct nfs_open_context *ctx;
1035 ctx = (struct nfs_open_context *)filp->private_data;
1038 nfs4_close_state(state, nd->intent.open.flags);
1042 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1045 struct rpc_cred *cred;
1046 struct nfs4_state *state;
1049 if (nd->flags & LOOKUP_CREATE) {
1050 attr.ia_mode = nd->intent.open.create_mode;
1051 attr.ia_valid = ATTR_MODE;
1052 if (!IS_POSIXACL(dir))
1053 attr.ia_mode &= ~current->fs->umask;
1056 BUG_ON(nd->intent.open.flags & O_CREAT);
1059 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1061 return (struct dentry *)cred;
1062 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1064 if (IS_ERR(state)) {
1065 if (PTR_ERR(state) == -ENOENT)
1066 d_add(dentry, NULL);
1067 return (struct dentry *)state;
1069 res = d_add_unique(dentry, state->inode);
1072 nfs4_intent_set_file(nd, dentry, state);
1077 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1079 struct rpc_cred *cred;
1080 struct nfs4_state *state;
1081 struct inode *inode;
1083 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1085 return PTR_ERR(cred);
1086 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1088 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1090 if (IS_ERR(state)) {
1091 switch (PTR_ERR(state)) {
1097 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1100 if (dentry->d_inode == NULL)
1105 inode = state->inode;
1107 if (inode == dentry->d_inode) {
1108 nfs4_intent_set_file(nd, dentry, state);
1111 nfs4_close_state(state, openflags);
1118 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1120 struct nfs4_server_caps_res res = {};
1121 struct rpc_message msg = {
1122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1123 .rpc_argp = fhandle,
1128 status = rpc_call_sync(server->client, &msg, 0);
1130 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1131 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1132 server->caps |= NFS_CAP_ACLS;
1133 if (res.has_links != 0)
1134 server->caps |= NFS_CAP_HARDLINKS;
1135 if (res.has_symlinks != 0)
1136 server->caps |= NFS_CAP_SYMLINKS;
1137 server->acl_bitmask = res.acl_bitmask;
1142 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1144 struct nfs4_exception exception = { };
1147 err = nfs4_handle_exception(server,
1148 _nfs4_server_capabilities(server, fhandle),
1150 } while (exception.retry);
1154 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1155 struct nfs_fsinfo *info)
1157 struct nfs4_lookup_root_arg args = {
1158 .bitmask = nfs4_fattr_bitmap,
1160 struct nfs4_lookup_res res = {
1162 .fattr = info->fattr,
1165 struct rpc_message msg = {
1166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1170 nfs_fattr_init(info->fattr);
1171 return rpc_call_sync(server->client, &msg, 0);
1174 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1175 struct nfs_fsinfo *info)
1177 struct nfs4_exception exception = { };
1180 err = nfs4_handle_exception(server,
1181 _nfs4_lookup_root(server, fhandle, info),
1183 } while (exception.retry);
1187 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1188 struct nfs_fsinfo *info)
1190 struct nfs_fattr * fattr = info->fattr;
1193 struct nfs4_lookup_arg args = {
1196 .bitmask = nfs4_fattr_bitmap,
1198 struct nfs4_lookup_res res = {
1203 struct rpc_message msg = {
1204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1211 * Now we do a separate LOOKUP for each component of the mount path.
1212 * The LOOKUPs are done separately so that we can conveniently
1213 * catch an ERR_WRONGSEC if it occurs along the way...
1215 status = nfs4_lookup_root(server, fhandle, info);
1219 p = server->mnt_path;
1221 struct nfs4_exception exception = { };
1228 while (*p && (*p != '/'))
1233 nfs_fattr_init(fattr);
1234 status = nfs4_handle_exception(server,
1235 rpc_call_sync(server->client, &msg, 0),
1237 } while (exception.retry);
1240 if (status == -ENOENT) {
1241 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1242 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1247 status = nfs4_server_capabilities(server, fhandle);
1249 status = nfs4_do_fsinfo(server, fhandle, info);
1254 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1256 struct nfs4_getattr_arg args = {
1258 .bitmask = server->attr_bitmask,
1260 struct nfs4_getattr_res res = {
1264 struct rpc_message msg = {
1265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1270 nfs_fattr_init(fattr);
1271 return rpc_call_sync(server->client, &msg, 0);
1274 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1276 struct nfs4_exception exception = { };
1279 err = nfs4_handle_exception(server,
1280 _nfs4_proc_getattr(server, fhandle, fattr),
1282 } while (exception.retry);
1287 * The file is not closed if it is opened due to the a request to change
1288 * the size of the file. The open call will not be needed once the
1289 * VFS layer lookup-intents are implemented.
1291 * Close is called when the inode is destroyed.
1292 * If we haven't opened the file for O_WRONLY, we
1293 * need to in the size_change case to obtain a stateid.
1296 * Because OPEN is always done by name in nfsv4, it is
1297 * possible that we opened a different file by the same
1298 * name. We can recognize this race condition, but we
1299 * can't do anything about it besides returning an error.
1301 * This will be fixed with VFS changes (lookup-intent).
1304 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1305 struct iattr *sattr)
1307 struct rpc_cred *cred;
1308 struct inode *inode = dentry->d_inode;
1309 struct nfs_open_context *ctx;
1310 struct nfs4_state *state = NULL;
1313 nfs_fattr_init(fattr);
1315 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1317 return PTR_ERR(cred);
1319 /* Search for an existing open(O_WRITE) file */
1320 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1324 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1325 NFS_FH(inode), sattr, state);
1327 nfs_setattr_update_inode(inode, sattr);
1329 put_nfs_open_context(ctx);
1334 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1335 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1338 struct nfs_server *server = NFS_SERVER(dir);
1339 struct nfs4_lookup_arg args = {
1340 .bitmask = server->attr_bitmask,
1341 .dir_fh = NFS_FH(dir),
1344 struct nfs4_lookup_res res = {
1349 struct rpc_message msg = {
1350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1355 nfs_fattr_init(fattr);
1357 dprintk("NFS call lookup %s\n", name->name);
1358 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1359 dprintk("NFS reply lookup: %d\n", status);
1363 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1365 struct nfs4_exception exception = { };
1368 err = nfs4_handle_exception(NFS_SERVER(dir),
1369 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1371 } while (exception.retry);
1375 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1377 struct nfs4_accessargs args = {
1378 .fh = NFS_FH(inode),
1380 struct nfs4_accessres res = { 0 };
1381 struct rpc_message msg = {
1382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1385 .rpc_cred = entry->cred,
1387 int mode = entry->mask;
1391 * Determine which access bits we want to ask for...
1393 if (mode & MAY_READ)
1394 args.access |= NFS4_ACCESS_READ;
1395 if (S_ISDIR(inode->i_mode)) {
1396 if (mode & MAY_WRITE)
1397 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1398 if (mode & MAY_EXEC)
1399 args.access |= NFS4_ACCESS_LOOKUP;
1401 if (mode & MAY_WRITE)
1402 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1403 if (mode & MAY_EXEC)
1404 args.access |= NFS4_ACCESS_EXECUTE;
1406 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1409 if (res.access & NFS4_ACCESS_READ)
1410 entry->mask |= MAY_READ;
1411 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1412 entry->mask |= MAY_WRITE;
1413 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1414 entry->mask |= MAY_EXEC;
1419 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1421 struct nfs4_exception exception = { };
1424 err = nfs4_handle_exception(NFS_SERVER(inode),
1425 _nfs4_proc_access(inode, entry),
1427 } while (exception.retry);
1432 * TODO: For the time being, we don't try to get any attributes
1433 * along with any of the zero-copy operations READ, READDIR,
1436 * In the case of the first three, we want to put the GETATTR
1437 * after the read-type operation -- this is because it is hard
1438 * to predict the length of a GETATTR response in v4, and thus
1439 * align the READ data correctly. This means that the GETATTR
1440 * may end up partially falling into the page cache, and we should
1441 * shift it into the 'tail' of the xdr_buf before processing.
1442 * To do this efficiently, we need to know the total length
1443 * of data received, which doesn't seem to be available outside
1446 * In the case of WRITE, we also want to put the GETATTR after
1447 * the operation -- in this case because we want to make sure
1448 * we get the post-operation mtime and size. This means that
1449 * we can't use xdr_encode_pages() as written: we need a variant
1450 * of it which would leave room in the 'tail' iovec.
1452 * Both of these changes to the XDR layer would in fact be quite
1453 * minor, but I decided to leave them for a subsequent patch.
1455 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1456 unsigned int pgbase, unsigned int pglen)
1458 struct nfs4_readlink args = {
1459 .fh = NFS_FH(inode),
1464 struct rpc_message msg = {
1465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1470 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1473 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1474 unsigned int pgbase, unsigned int pglen)
1476 struct nfs4_exception exception = { };
1479 err = nfs4_handle_exception(NFS_SERVER(inode),
1480 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1482 } while (exception.retry);
1486 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1488 int flags = rdata->flags;
1489 struct inode *inode = rdata->inode;
1490 struct nfs_fattr *fattr = rdata->res.fattr;
1491 struct nfs_server *server = NFS_SERVER(inode);
1492 struct rpc_message msg = {
1493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1494 .rpc_argp = &rdata->args,
1495 .rpc_resp = &rdata->res,
1496 .rpc_cred = rdata->cred,
1498 unsigned long timestamp = jiffies;
1501 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1502 (long long) rdata->args.offset);
1504 nfs_fattr_init(fattr);
1505 status = rpc_call_sync(server->client, &msg, flags);
1507 renew_lease(server, timestamp);
1508 dprintk("NFS reply read: %d\n", status);
1512 static int nfs4_proc_read(struct nfs_read_data *rdata)
1514 struct nfs4_exception exception = { };
1517 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1518 _nfs4_proc_read(rdata),
1520 } while (exception.retry);
1524 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1526 int rpcflags = wdata->flags;
1527 struct inode *inode = wdata->inode;
1528 struct nfs_fattr *fattr = wdata->res.fattr;
1529 struct nfs_server *server = NFS_SERVER(inode);
1530 struct rpc_message msg = {
1531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1532 .rpc_argp = &wdata->args,
1533 .rpc_resp = &wdata->res,
1534 .rpc_cred = wdata->cred,
1538 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1539 (long long) wdata->args.offset);
1541 wdata->args.bitmask = server->attr_bitmask;
1542 wdata->res.server = server;
1543 nfs_fattr_init(fattr);
1544 status = rpc_call_sync(server->client, &msg, rpcflags);
1545 dprintk("NFS reply write: %d\n", status);
1548 nfs_post_op_update_inode(inode, fattr);
1549 return wdata->res.count;
1552 static int nfs4_proc_write(struct nfs_write_data *wdata)
1554 struct nfs4_exception exception = { };
1557 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1558 _nfs4_proc_write(wdata),
1560 } while (exception.retry);
1564 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1566 struct inode *inode = cdata->inode;
1567 struct nfs_fattr *fattr = cdata->res.fattr;
1568 struct nfs_server *server = NFS_SERVER(inode);
1569 struct rpc_message msg = {
1570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1571 .rpc_argp = &cdata->args,
1572 .rpc_resp = &cdata->res,
1573 .rpc_cred = cdata->cred,
1577 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1578 (long long) cdata->args.offset);
1580 cdata->args.bitmask = server->attr_bitmask;
1581 cdata->res.server = server;
1582 nfs_fattr_init(fattr);
1583 status = rpc_call_sync(server->client, &msg, 0);
1584 dprintk("NFS reply commit: %d\n", status);
1586 nfs_post_op_update_inode(inode, fattr);
1590 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1592 struct nfs4_exception exception = { };
1595 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1596 _nfs4_proc_commit(cdata),
1598 } while (exception.retry);
1604 * We will need to arrange for the VFS layer to provide an atomic open.
1605 * Until then, this create/open method is prone to inefficiency and race
1606 * conditions due to the lookup, create, and open VFS calls from sys_open()
1607 * placed on the wire.
1609 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1610 * The file will be opened again in the subsequent VFS open call
1611 * (nfs4_proc_file_open).
1613 * The open for read will just hang around to be used by any process that
1614 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1618 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1619 int flags, struct nameidata *nd)
1621 struct nfs4_state *state;
1622 struct rpc_cred *cred;
1625 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1627 status = PTR_ERR(cred);
1630 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1632 if (IS_ERR(state)) {
1633 status = PTR_ERR(state);
1636 d_instantiate(dentry, state->inode);
1637 if (flags & O_EXCL) {
1638 struct nfs_fattr fattr;
1639 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1640 NFS_FH(state->inode), sattr, state);
1642 nfs_setattr_update_inode(state->inode, sattr);
1644 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1645 nfs4_intent_set_file(nd, dentry, state);
1647 nfs4_close_state(state, flags);
1652 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1654 struct nfs_server *server = NFS_SERVER(dir);
1655 struct nfs4_remove_arg args = {
1658 .bitmask = server->attr_bitmask,
1660 struct nfs_fattr dir_attr;
1661 struct nfs4_remove_res res = {
1663 .dir_attr = &dir_attr,
1665 struct rpc_message msg = {
1666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1672 nfs_fattr_init(res.dir_attr);
1673 status = rpc_call_sync(server->client, &msg, 0);
1675 update_changeattr(dir, &res.cinfo);
1676 nfs_post_op_update_inode(dir, res.dir_attr);
1681 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1683 struct nfs4_exception exception = { };
1686 err = nfs4_handle_exception(NFS_SERVER(dir),
1687 _nfs4_proc_remove(dir, name),
1689 } while (exception.retry);
1693 struct unlink_desc {
1694 struct nfs4_remove_arg args;
1695 struct nfs4_remove_res res;
1696 struct nfs_fattr dir_attr;
1699 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1702 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1703 struct unlink_desc *up;
1705 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1709 up->args.fh = NFS_FH(dir->d_inode);
1710 up->args.name = name;
1711 up->args.bitmask = server->attr_bitmask;
1712 up->res.server = server;
1713 up->res.dir_attr = &up->dir_attr;
1715 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1716 msg->rpc_argp = &up->args;
1717 msg->rpc_resp = &up->res;
1721 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1723 struct rpc_message *msg = &task->tk_msg;
1724 struct unlink_desc *up;
1726 if (msg->rpc_resp != NULL) {
1727 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1728 update_changeattr(dir->d_inode, &up->res.cinfo);
1729 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1731 msg->rpc_resp = NULL;
1732 msg->rpc_argp = NULL;
1737 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1738 struct inode *new_dir, struct qstr *new_name)
1740 struct nfs_server *server = NFS_SERVER(old_dir);
1741 struct nfs4_rename_arg arg = {
1742 .old_dir = NFS_FH(old_dir),
1743 .new_dir = NFS_FH(new_dir),
1744 .old_name = old_name,
1745 .new_name = new_name,
1746 .bitmask = server->attr_bitmask,
1748 struct nfs_fattr old_fattr, new_fattr;
1749 struct nfs4_rename_res res = {
1751 .old_fattr = &old_fattr,
1752 .new_fattr = &new_fattr,
1754 struct rpc_message msg = {
1755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1761 nfs_fattr_init(res.old_fattr);
1762 nfs_fattr_init(res.new_fattr);
1763 status = rpc_call_sync(server->client, &msg, 0);
1766 update_changeattr(old_dir, &res.old_cinfo);
1767 nfs_post_op_update_inode(old_dir, res.old_fattr);
1768 update_changeattr(new_dir, &res.new_cinfo);
1769 nfs_post_op_update_inode(new_dir, res.new_fattr);
1774 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1775 struct inode *new_dir, struct qstr *new_name)
1777 struct nfs4_exception exception = { };
1780 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1781 _nfs4_proc_rename(old_dir, old_name,
1784 } while (exception.retry);
1788 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1790 struct nfs_server *server = NFS_SERVER(inode);
1791 struct nfs4_link_arg arg = {
1792 .fh = NFS_FH(inode),
1793 .dir_fh = NFS_FH(dir),
1795 .bitmask = server->attr_bitmask,
1797 struct nfs_fattr fattr, dir_attr;
1798 struct nfs4_link_res res = {
1801 .dir_attr = &dir_attr,
1803 struct rpc_message msg = {
1804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1810 nfs_fattr_init(res.fattr);
1811 nfs_fattr_init(res.dir_attr);
1812 status = rpc_call_sync(server->client, &msg, 0);
1814 update_changeattr(dir, &res.cinfo);
1815 nfs_post_op_update_inode(dir, res.dir_attr);
1816 nfs_refresh_inode(inode, res.fattr);
1822 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1824 struct nfs4_exception exception = { };
1827 err = nfs4_handle_exception(NFS_SERVER(inode),
1828 _nfs4_proc_link(inode, dir, name),
1830 } while (exception.retry);
1834 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1835 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1836 struct nfs_fattr *fattr)
1838 struct nfs_server *server = NFS_SERVER(dir);
1839 struct nfs_fattr dir_fattr;
1840 struct nfs4_create_arg arg = {
1841 .dir_fh = NFS_FH(dir),
1846 .bitmask = server->attr_bitmask,
1848 struct nfs4_create_res res = {
1852 .dir_fattr = &dir_fattr,
1854 struct rpc_message msg = {
1855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1861 if (path->len > NFS4_MAXPATHLEN)
1862 return -ENAMETOOLONG;
1863 arg.u.symlink = path;
1864 nfs_fattr_init(fattr);
1865 nfs_fattr_init(&dir_fattr);
1867 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1869 update_changeattr(dir, &res.dir_cinfo);
1870 nfs_post_op_update_inode(dir, res.dir_fattr);
1874 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1875 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1876 struct nfs_fattr *fattr)
1878 struct nfs4_exception exception = { };
1881 err = nfs4_handle_exception(NFS_SERVER(dir),
1882 _nfs4_proc_symlink(dir, name, path, sattr,
1885 } while (exception.retry);
1889 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1890 struct iattr *sattr)
1892 struct nfs_server *server = NFS_SERVER(dir);
1893 struct nfs_fh fhandle;
1894 struct nfs_fattr fattr, dir_fattr;
1895 struct nfs4_create_arg arg = {
1896 .dir_fh = NFS_FH(dir),
1898 .name = &dentry->d_name,
1901 .bitmask = server->attr_bitmask,
1903 struct nfs4_create_res res = {
1907 .dir_fattr = &dir_fattr,
1909 struct rpc_message msg = {
1910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1916 nfs_fattr_init(&fattr);
1917 nfs_fattr_init(&dir_fattr);
1919 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1921 update_changeattr(dir, &res.dir_cinfo);
1922 nfs_post_op_update_inode(dir, res.dir_fattr);
1923 status = nfs_instantiate(dentry, &fhandle, &fattr);
1928 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1929 struct iattr *sattr)
1931 struct nfs4_exception exception = { };
1934 err = nfs4_handle_exception(NFS_SERVER(dir),
1935 _nfs4_proc_mkdir(dir, dentry, sattr),
1937 } while (exception.retry);
1941 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1942 u64 cookie, struct page *page, unsigned int count, int plus)
1944 struct inode *dir = dentry->d_inode;
1945 struct nfs4_readdir_arg args = {
1950 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1952 struct nfs4_readdir_res res;
1953 struct rpc_message msg = {
1954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1961 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1962 dentry->d_parent->d_name.name,
1963 dentry->d_name.name,
1964 (unsigned long long)cookie);
1966 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1967 res.pgbase = args.pgbase;
1968 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1970 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1972 dprintk("%s: returns %d\n", __FUNCTION__, status);
1976 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1977 u64 cookie, struct page *page, unsigned int count, int plus)
1979 struct nfs4_exception exception = { };
1982 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1983 _nfs4_proc_readdir(dentry, cred, cookie,
1986 } while (exception.retry);
1990 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1991 struct iattr *sattr, dev_t rdev)
1993 struct nfs_server *server = NFS_SERVER(dir);
1995 struct nfs_fattr fattr, dir_fattr;
1996 struct nfs4_create_arg arg = {
1997 .dir_fh = NFS_FH(dir),
1999 .name = &dentry->d_name,
2001 .bitmask = server->attr_bitmask,
2003 struct nfs4_create_res res = {
2007 .dir_fattr = &dir_fattr,
2009 struct rpc_message msg = {
2010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2015 int mode = sattr->ia_mode;
2017 nfs_fattr_init(&fattr);
2018 nfs_fattr_init(&dir_fattr);
2020 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2021 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2023 arg.ftype = NF4FIFO;
2024 else if (S_ISBLK(mode)) {
2026 arg.u.device.specdata1 = MAJOR(rdev);
2027 arg.u.device.specdata2 = MINOR(rdev);
2029 else if (S_ISCHR(mode)) {
2031 arg.u.device.specdata1 = MAJOR(rdev);
2032 arg.u.device.specdata2 = MINOR(rdev);
2035 arg.ftype = NF4SOCK;
2037 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2039 update_changeattr(dir, &res.dir_cinfo);
2040 nfs_post_op_update_inode(dir, res.dir_fattr);
2041 status = nfs_instantiate(dentry, &fh, &fattr);
2046 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2047 struct iattr *sattr, dev_t rdev)
2049 struct nfs4_exception exception = { };
2052 err = nfs4_handle_exception(NFS_SERVER(dir),
2053 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2055 } while (exception.retry);
2059 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2060 struct nfs_fsstat *fsstat)
2062 struct nfs4_statfs_arg args = {
2064 .bitmask = server->attr_bitmask,
2066 struct rpc_message msg = {
2067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2072 nfs_fattr_init(fsstat->fattr);
2073 return rpc_call_sync(server->client, &msg, 0);
2076 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2078 struct nfs4_exception exception = { };
2081 err = nfs4_handle_exception(server,
2082 _nfs4_proc_statfs(server, fhandle, fsstat),
2084 } while (exception.retry);
2088 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2089 struct nfs_fsinfo *fsinfo)
2091 struct nfs4_fsinfo_arg args = {
2093 .bitmask = server->attr_bitmask,
2095 struct rpc_message msg = {
2096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2101 return rpc_call_sync(server->client, &msg, 0);
2104 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2106 struct nfs4_exception exception = { };
2110 err = nfs4_handle_exception(server,
2111 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2113 } while (exception.retry);
2117 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2119 nfs_fattr_init(fsinfo->fattr);
2120 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2123 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2124 struct nfs_pathconf *pathconf)
2126 struct nfs4_pathconf_arg args = {
2128 .bitmask = server->attr_bitmask,
2130 struct rpc_message msg = {
2131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2133 .rpc_resp = pathconf,
2136 /* None of the pathconf attributes are mandatory to implement */
2137 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2138 memset(pathconf, 0, sizeof(*pathconf));
2142 nfs_fattr_init(pathconf->fattr);
2143 return rpc_call_sync(server->client, &msg, 0);
2146 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2147 struct nfs_pathconf *pathconf)
2149 struct nfs4_exception exception = { };
2153 err = nfs4_handle_exception(server,
2154 _nfs4_proc_pathconf(server, fhandle, pathconf),
2156 } while (exception.retry);
2160 static void nfs4_read_done(struct rpc_task *task, void *calldata)
2162 struct nfs_read_data *data = calldata;
2163 struct inode *inode = data->inode;
2165 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2166 rpc_restart_call(task);
2169 if (task->tk_status > 0)
2170 renew_lease(NFS_SERVER(inode), data->timestamp);
2171 /* Call back common NFS readpage processing */
2172 nfs_readpage_result(task, calldata);
2175 static const struct rpc_call_ops nfs4_read_ops = {
2176 .rpc_call_done = nfs4_read_done,
2177 .rpc_release = nfs_readdata_release,
2181 nfs4_proc_read_setup(struct nfs_read_data *data)
2183 struct rpc_task *task = &data->task;
2184 struct rpc_message msg = {
2185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2186 .rpc_argp = &data->args,
2187 .rpc_resp = &data->res,
2188 .rpc_cred = data->cred,
2190 struct inode *inode = data->inode;
2193 data->timestamp = jiffies;
2195 /* N.B. Do we need to test? Never called for swapfile inode */
2196 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2198 /* Finalize the task. */
2199 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
2200 rpc_call_setup(task, &msg, 0);
2203 static void nfs4_write_done(struct rpc_task *task, void *calldata)
2205 struct nfs_write_data *data = calldata;
2206 struct inode *inode = data->inode;
2208 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2209 rpc_restart_call(task);
2212 if (task->tk_status >= 0) {
2213 renew_lease(NFS_SERVER(inode), data->timestamp);
2214 nfs_post_op_update_inode(inode, data->res.fattr);
2216 /* Call back common NFS writeback processing */
2217 nfs_writeback_done(task, calldata);
2220 static const struct rpc_call_ops nfs4_write_ops = {
2221 .rpc_call_done = nfs4_write_done,
2222 .rpc_release = nfs_writedata_release,
2226 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2228 struct rpc_task *task = &data->task;
2229 struct rpc_message msg = {
2230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2231 .rpc_argp = &data->args,
2232 .rpc_resp = &data->res,
2233 .rpc_cred = data->cred,
2235 struct inode *inode = data->inode;
2236 struct nfs_server *server = NFS_SERVER(inode);
2240 if (how & FLUSH_STABLE) {
2241 if (!NFS_I(inode)->ncommit)
2242 stable = NFS_FILE_SYNC;
2244 stable = NFS_DATA_SYNC;
2246 stable = NFS_UNSTABLE;
2247 data->args.stable = stable;
2248 data->args.bitmask = server->attr_bitmask;
2249 data->res.server = server;
2251 data->timestamp = jiffies;
2253 /* Set the initial flags for the task. */
2254 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2256 /* Finalize the task. */
2257 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
2258 rpc_call_setup(task, &msg, 0);
2261 static void nfs4_commit_done(struct rpc_task *task, void *calldata)
2263 struct nfs_write_data *data = calldata;
2264 struct inode *inode = data->inode;
2266 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2267 rpc_restart_call(task);
2270 if (task->tk_status >= 0)
2271 nfs_post_op_update_inode(inode, data->res.fattr);
2272 /* Call back common NFS writeback processing */
2273 nfs_commit_done(task, calldata);
2276 static const struct rpc_call_ops nfs4_commit_ops = {
2277 .rpc_call_done = nfs4_commit_done,
2278 .rpc_release = nfs_commit_release,
2282 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2284 struct rpc_task *task = &data->task;
2285 struct rpc_message msg = {
2286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2287 .rpc_argp = &data->args,
2288 .rpc_resp = &data->res,
2289 .rpc_cred = data->cred,
2291 struct inode *inode = data->inode;
2292 struct nfs_server *server = NFS_SERVER(inode);
2295 data->args.bitmask = server->attr_bitmask;
2296 data->res.server = server;
2298 /* Set the initial flags for the task. */
2299 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2301 /* Finalize the task. */
2302 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
2303 rpc_call_setup(task, &msg, 0);
2307 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2308 * standalone procedure for queueing an asynchronous RENEW.
2310 static void nfs4_renew_done(struct rpc_task *task, void *data)
2312 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2313 unsigned long timestamp = (unsigned long)data;
2315 if (task->tk_status < 0) {
2316 switch (task->tk_status) {
2317 case -NFS4ERR_STALE_CLIENTID:
2318 case -NFS4ERR_EXPIRED:
2319 case -NFS4ERR_CB_PATH_DOWN:
2320 nfs4_schedule_state_recovery(clp);
2324 spin_lock(&clp->cl_lock);
2325 if (time_before(clp->cl_last_renewal,timestamp))
2326 clp->cl_last_renewal = timestamp;
2327 spin_unlock(&clp->cl_lock);
2330 static const struct rpc_call_ops nfs4_renew_ops = {
2331 .rpc_call_done = nfs4_renew_done,
2335 nfs4_proc_async_renew(struct nfs4_client *clp)
2337 struct rpc_message msg = {
2338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2340 .rpc_cred = clp->cl_cred,
2343 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2344 &nfs4_renew_ops, (void *)jiffies);
2348 nfs4_proc_renew(struct nfs4_client *clp)
2350 struct rpc_message msg = {
2351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2353 .rpc_cred = clp->cl_cred,
2355 unsigned long now = jiffies;
2358 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2361 spin_lock(&clp->cl_lock);
2362 if (time_before(clp->cl_last_renewal,now))
2363 clp->cl_last_renewal = now;
2364 spin_unlock(&clp->cl_lock);
2368 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2370 return (server->caps & NFS_CAP_ACLS)
2371 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2372 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2375 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2376 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2379 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2381 static void buf_to_pages(const void *buf, size_t buflen,
2382 struct page **pages, unsigned int *pgbase)
2384 const void *p = buf;
2386 *pgbase = offset_in_page(buf);
2388 while (p < buf + buflen) {
2389 *(pages++) = virt_to_page(p);
2390 p += PAGE_CACHE_SIZE;
2394 struct nfs4_cached_acl {
2400 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2402 struct nfs_inode *nfsi = NFS_I(inode);
2404 spin_lock(&inode->i_lock);
2405 kfree(nfsi->nfs4_acl);
2406 nfsi->nfs4_acl = acl;
2407 spin_unlock(&inode->i_lock);
2410 static void nfs4_zap_acl_attr(struct inode *inode)
2412 nfs4_set_cached_acl(inode, NULL);
2415 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2417 struct nfs_inode *nfsi = NFS_I(inode);
2418 struct nfs4_cached_acl *acl;
2421 spin_lock(&inode->i_lock);
2422 acl = nfsi->nfs4_acl;
2425 if (buf == NULL) /* user is just asking for length */
2427 if (acl->cached == 0)
2429 ret = -ERANGE; /* see getxattr(2) man page */
2430 if (acl->len > buflen)
2432 memcpy(buf, acl->data, acl->len);
2436 spin_unlock(&inode->i_lock);
2440 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2442 struct nfs4_cached_acl *acl;
2444 if (buf && acl_len <= PAGE_SIZE) {
2445 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2449 memcpy(acl->data, buf, acl_len);
2451 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2458 nfs4_set_cached_acl(inode, acl);
2461 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2463 struct page *pages[NFS4ACL_MAXPAGES];
2464 struct nfs_getaclargs args = {
2465 .fh = NFS_FH(inode),
2469 size_t resp_len = buflen;
2471 struct rpc_message msg = {
2472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2474 .rpc_resp = &resp_len,
2476 struct page *localpage = NULL;
2479 if (buflen < PAGE_SIZE) {
2480 /* As long as we're doing a round trip to the server anyway,
2481 * let's be prepared for a page of acl data. */
2482 localpage = alloc_page(GFP_KERNEL);
2483 resp_buf = page_address(localpage);
2484 if (localpage == NULL)
2486 args.acl_pages[0] = localpage;
2487 args.acl_pgbase = 0;
2488 resp_len = args.acl_len = PAGE_SIZE;
2491 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2493 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2496 if (resp_len > args.acl_len)
2497 nfs4_write_cached_acl(inode, NULL, resp_len);
2499 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2502 if (resp_len > buflen)
2505 memcpy(buf, resp_buf, resp_len);
2510 __free_page(localpage);
2514 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2516 struct nfs_server *server = NFS_SERVER(inode);
2519 if (!nfs4_server_supports_acls(server))
2521 ret = nfs_revalidate_inode(server, inode);
2524 ret = nfs4_read_cached_acl(inode, buf, buflen);
2527 return nfs4_get_acl_uncached(inode, buf, buflen);
2530 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2532 struct nfs_server *server = NFS_SERVER(inode);
2533 struct page *pages[NFS4ACL_MAXPAGES];
2534 struct nfs_setaclargs arg = {
2535 .fh = NFS_FH(inode),
2539 struct rpc_message msg = {
2540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2546 if (!nfs4_server_supports_acls(server))
2548 nfs_inode_return_delegation(inode);
2549 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2550 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2552 nfs4_write_cached_acl(inode, buf, buflen);
2557 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2559 struct nfs4_client *clp = server->nfs4_state;
2561 if (!clp || task->tk_status >= 0)
2563 switch(task->tk_status) {
2564 case -NFS4ERR_STALE_CLIENTID:
2565 case -NFS4ERR_STALE_STATEID:
2566 case -NFS4ERR_EXPIRED:
2567 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2568 nfs4_schedule_state_recovery(clp);
2569 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2570 rpc_wake_up_task(task);
2571 task->tk_status = 0;
2573 case -NFS4ERR_GRACE:
2574 case -NFS4ERR_DELAY:
2575 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2576 task->tk_status = 0;
2578 case -NFS4ERR_OLD_STATEID:
2579 task->tk_status = 0;
2582 task->tk_status = nfs4_map_errors(task->tk_status);
2586 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2590 int interruptible, res = 0;
2594 rpc_clnt_sigmask(clnt, &oldset);
2595 interruptible = TASK_UNINTERRUPTIBLE;
2597 interruptible = TASK_INTERRUPTIBLE;
2598 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2599 nfs4_schedule_state_recovery(clp);
2600 if (clnt->cl_intr && signalled())
2602 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2604 finish_wait(&clp->cl_waitq, &wait);
2605 rpc_clnt_sigunmask(clnt, &oldset);
2609 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2617 *timeout = NFS4_POLL_RETRY_MIN;
2618 if (*timeout > NFS4_POLL_RETRY_MAX)
2619 *timeout = NFS4_POLL_RETRY_MAX;
2620 rpc_clnt_sigmask(clnt, &oldset);
2621 if (clnt->cl_intr) {
2622 schedule_timeout_interruptible(*timeout);
2626 schedule_timeout_uninterruptible(*timeout);
2627 rpc_clnt_sigunmask(clnt, &oldset);
2632 /* This is the error handling routine for processes that are allowed
2635 int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2637 struct nfs4_client *clp = server->nfs4_state;
2638 int ret = errorcode;
2640 exception->retry = 0;
2644 case -NFS4ERR_STALE_CLIENTID:
2645 case -NFS4ERR_STALE_STATEID:
2646 case -NFS4ERR_EXPIRED:
2647 ret = nfs4_wait_clnt_recover(server->client, clp);
2649 exception->retry = 1;
2651 case -NFS4ERR_GRACE:
2652 case -NFS4ERR_DELAY:
2653 ret = nfs4_delay(server->client, &exception->timeout);
2656 case -NFS4ERR_OLD_STATEID:
2657 exception->retry = 1;
2659 /* We failed to handle the error */
2660 return nfs4_map_errors(ret);
2663 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2665 nfs4_verifier sc_verifier;
2666 struct nfs4_setclientid setclientid = {
2667 .sc_verifier = &sc_verifier,
2670 struct rpc_message msg = {
2671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2672 .rpc_argp = &setclientid,
2674 .rpc_cred = clp->cl_cred,
2680 p = (u32*)sc_verifier.data;
2681 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2682 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2685 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2686 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2687 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2688 clp->cl_cred->cr_ops->cr_name,
2689 clp->cl_id_uniquifier);
2690 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2691 sizeof(setclientid.sc_netid), "tcp");
2692 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2693 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2694 clp->cl_ipaddr, port >> 8, port & 255);
2696 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2697 if (status != -NFS4ERR_CLID_INUSE)
2702 ssleep(clp->cl_lease_time + 1);
2704 if (++clp->cl_id_uniquifier == 0)
2711 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2713 struct nfs_fsinfo fsinfo;
2714 struct rpc_message msg = {
2715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2717 .rpc_resp = &fsinfo,
2718 .rpc_cred = clp->cl_cred,
2724 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2726 spin_lock(&clp->cl_lock);
2727 clp->cl_lease_time = fsinfo.lease_time * HZ;
2728 clp->cl_last_renewal = now;
2729 spin_unlock(&clp->cl_lock);
2734 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2736 struct nfs4_delegreturnargs args = {
2737 .fhandle = NFS_FH(inode),
2740 struct rpc_message msg = {
2741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2746 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2749 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2751 struct nfs_server *server = NFS_SERVER(inode);
2752 struct nfs4_exception exception = { };
2755 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2757 case -NFS4ERR_STALE_STATEID:
2758 case -NFS4ERR_EXPIRED:
2759 nfs4_schedule_state_recovery(server->nfs4_state);
2763 err = nfs4_handle_exception(server, err, &exception);
2764 } while (exception.retry);
2768 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2769 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2772 * sleep, with exponential backoff, and retry the LOCK operation.
2774 static unsigned long
2775 nfs4_set_lock_task_retry(unsigned long timeout)
2777 schedule_timeout_interruptible(timeout);
2779 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2780 return NFS4_LOCK_MAXTIMEOUT;
2785 nfs4_lck_type(int cmd, struct file_lock *request)
2788 switch (request->fl_type) {
2790 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2792 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2794 return NFS4_WRITE_LT;
2800 static inline uint64_t
2801 nfs4_lck_length(struct file_lock *request)
2803 if (request->fl_end == OFFSET_MAX)
2804 return ~(uint64_t)0;
2805 return request->fl_end - request->fl_start + 1;
2808 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2810 struct inode *inode = state->inode;
2811 struct nfs_server *server = NFS_SERVER(inode);
2812 struct nfs4_client *clp = server->nfs4_state;
2813 struct nfs_lockargs arg = {
2814 .fh = NFS_FH(inode),
2815 .type = nfs4_lck_type(cmd, request),
2816 .offset = request->fl_start,
2817 .length = nfs4_lck_length(request),
2819 struct nfs_lockres res = {
2822 struct rpc_message msg = {
2823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2826 .rpc_cred = state->owner->so_cred,
2828 struct nfs_lowner nlo;
2829 struct nfs4_lock_state *lsp;
2832 down_read(&clp->cl_sem);
2833 nlo.clientid = clp->cl_clientid;
2834 status = nfs4_set_lock_state(state, request);
2837 lsp = request->fl_u.nfs4_fl.owner;
2838 nlo.id = lsp->ls_id;
2840 status = rpc_call_sync(server->client, &msg, 0);
2842 request->fl_type = F_UNLCK;
2843 } else if (status == -NFS4ERR_DENIED) {
2844 int64_t len, start, end;
2845 start = res.u.denied.offset;
2846 len = res.u.denied.length;
2847 end = start + len - 1;
2848 if (end < 0 || len == 0)
2849 request->fl_end = OFFSET_MAX;
2851 request->fl_end = (loff_t)end;
2852 request->fl_start = (loff_t)start;
2853 request->fl_type = F_WRLCK;
2854 if (res.u.denied.type & 1)
2855 request->fl_type = F_RDLCK;
2856 request->fl_pid = 0;
2860 up_read(&clp->cl_sem);
2864 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2866 struct nfs4_exception exception = { };
2870 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2871 _nfs4_proc_getlk(state, cmd, request),
2873 } while (exception.retry);
2877 static int do_vfs_lock(struct file *file, struct file_lock *fl)
2880 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2882 res = posix_lock_file_wait(file, fl);
2885 res = flock_lock_file_wait(file, fl);
2891 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2896 struct nfs4_unlockdata {
2897 struct nfs_lockargs arg;
2898 struct nfs_locku_opargs luargs;
2899 struct nfs_lockres res;
2900 struct nfs4_lock_state *lsp;
2901 struct nfs_open_context *ctx;
2904 static void nfs4_locku_release_calldata(void *data)
2906 struct nfs4_unlockdata *calldata = data;
2907 nfs_free_seqid(calldata->luargs.seqid);
2908 nfs4_put_lock_state(calldata->lsp);
2909 put_nfs_open_context(calldata->ctx);
2913 static void nfs4_locku_done(struct rpc_task *task, void *data)
2915 struct nfs4_unlockdata *calldata = data;
2917 if (RPC_ASSASSINATED(task))
2919 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2920 switch (task->tk_status) {
2922 memcpy(calldata->lsp->ls_stateid.data,
2923 calldata->res.u.stateid.data,
2924 sizeof(calldata->lsp->ls_stateid.data));
2926 case -NFS4ERR_STALE_STATEID:
2927 case -NFS4ERR_EXPIRED:
2928 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2931 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2932 rpc_restart_call(task);
2937 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
2939 struct nfs4_unlockdata *calldata = data;
2940 struct rpc_message msg = {
2941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2942 .rpc_argp = &calldata->arg,
2943 .rpc_resp = &calldata->res,
2944 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
2948 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
2951 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
2952 /* Note: exit _without_ running nfs4_locku_done */
2953 task->tk_action = NULL;
2956 rpc_call_setup(task, &msg, 0);
2959 static const struct rpc_call_ops nfs4_locku_ops = {
2960 .rpc_call_prepare = nfs4_locku_prepare,
2961 .rpc_call_done = nfs4_locku_done,
2962 .rpc_release = nfs4_locku_release_calldata,
2965 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2967 struct nfs4_unlockdata *calldata;
2968 struct inode *inode = state->inode;
2969 struct nfs_server *server = NFS_SERVER(inode);
2970 struct nfs4_lock_state *lsp;
2971 struct rpc_task *task;
2974 /* Is this a delegated lock? */
2975 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
2978 status = nfs4_set_lock_state(state, request);
2981 lsp = request->fl_u.nfs4_fl.owner;
2982 /* We might have lost the locks! */
2983 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2986 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
2987 if (calldata == NULL)
2989 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2990 if (calldata->luargs.seqid == NULL) {
2994 calldata->luargs.stateid = &lsp->ls_stateid;
2995 calldata->arg.fh = NFS_FH(inode);
2996 calldata->arg.type = nfs4_lck_type(cmd, request);
2997 calldata->arg.offset = request->fl_start;
2998 calldata->arg.length = nfs4_lck_length(request);
2999 calldata->arg.u.locku = &calldata->luargs;
3000 calldata->res.server = server;
3001 calldata->lsp = lsp;
3002 atomic_inc(&lsp->ls_count);
3004 /* Ensure we don't close file until we're done freeing locks! */
3005 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
3007 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_locku_ops, calldata);
3008 if (!IS_ERR(task)) {
3009 status = nfs4_wait_for_completion_rpc_task(task);
3010 rpc_release_task(task);
3012 status = PTR_ERR(task);
3013 nfs4_locku_release_calldata(calldata);
3016 do_vfs_lock(request->fl_file, request);
3020 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
3022 struct inode *inode = state->inode;
3023 struct nfs_server *server = NFS_SERVER(inode);
3024 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
3025 struct nfs_lock_opargs largs = {
3026 .lock_stateid = &lsp->ls_stateid,
3027 .open_stateid = &state->stateid,
3029 .clientid = server->nfs4_state->cl_clientid,
3034 struct nfs_lockargs arg = {
3035 .fh = NFS_FH(inode),
3036 .type = nfs4_lck_type(cmd, request),
3037 .offset = request->fl_start,
3038 .length = nfs4_lck_length(request),
3043 struct nfs_lockres res = {
3046 struct rpc_message msg = {
3047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3050 .rpc_cred = state->owner->so_cred,
3052 int status = -ENOMEM;
3054 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3055 if (largs.lock_seqid == NULL)
3057 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
3058 struct nfs4_state_owner *owner = state->owner;
3060 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
3061 if (largs.open_seqid == NULL)
3063 largs.new_lock_owner = 1;
3064 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3065 /* increment open seqid on success, and seqid mutating errors */
3066 if (largs.new_lock_owner != 0) {
3067 nfs_increment_open_seqid(status, largs.open_seqid);
3069 nfs_confirm_seqid(&lsp->ls_seqid, 0);
3071 nfs_free_seqid(largs.open_seqid);
3073 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3074 /* increment lock seqid on success, and seqid mutating errors*/
3075 nfs_increment_lock_seqid(status, largs.lock_seqid);
3076 /* save the returned stateid. */
3078 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
3079 sizeof(lsp->ls_stateid.data));
3080 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3081 } else if (status == -NFS4ERR_DENIED)
3084 nfs_free_seqid(largs.lock_seqid);
3088 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3090 struct nfs_server *server = NFS_SERVER(state->inode);
3091 struct nfs4_exception exception = { };
3094 /* Cache the lock if possible... */
3095 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3098 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3099 if (err != -NFS4ERR_DELAY)
3101 nfs4_handle_exception(server, err, &exception);
3102 } while (exception.retry);
3106 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3108 struct nfs_server *server = NFS_SERVER(state->inode);
3109 struct nfs4_exception exception = { };
3112 err = nfs4_set_lock_state(state, request);
3116 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3117 if (err != -NFS4ERR_DELAY)
3119 nfs4_handle_exception(server, err, &exception);
3120 } while (exception.retry);
3124 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3126 struct nfs4_client *clp = state->owner->so_client;
3129 /* Is this a delegated open? */
3130 if (NFS_I(state->inode)->delegation_state != 0) {
3131 /* Yes: cache locks! */
3132 status = do_vfs_lock(request->fl_file, request);
3133 /* ...but avoid races with delegation recall... */
3134 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
3137 down_read(&clp->cl_sem);
3138 status = nfs4_set_lock_state(state, request);
3141 status = _nfs4_do_setlk(state, cmd, request, 0);
3144 /* Note: we always want to sleep here! */
3145 request->fl_flags |= FL_SLEEP;
3146 if (do_vfs_lock(request->fl_file, request) < 0)
3147 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3149 up_read(&clp->cl_sem);
3153 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3155 struct nfs4_exception exception = { };
3159 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3160 _nfs4_proc_setlk(state, cmd, request),
3162 } while (exception.retry);
3167 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3169 struct nfs_open_context *ctx;
3170 struct nfs4_state *state;
3171 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3174 /* verify open state */
3175 ctx = (struct nfs_open_context *)filp->private_data;
3178 if (request->fl_start < 0 || request->fl_end < 0)
3182 return nfs4_proc_getlk(state, F_GETLK, request);
3184 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3187 if (request->fl_type == F_UNLCK)
3188 return nfs4_proc_unlck(state, cmd, request);
3191 status = nfs4_proc_setlk(state, cmd, request);
3192 if ((status != -EAGAIN) || IS_SETLK(cmd))
3194 timeout = nfs4_set_lock_task_retry(timeout);
3195 status = -ERESTARTSYS;
3198 } while(status < 0);
3202 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3204 struct nfs_server *server = NFS_SERVER(state->inode);
3205 struct nfs4_exception exception = { };
3208 err = nfs4_set_lock_state(state, fl);
3212 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3213 if (err != -NFS4ERR_DELAY)
3215 err = nfs4_handle_exception(server, err, &exception);
3216 } while (exception.retry);
3221 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3223 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3224 size_t buflen, int flags)
3226 struct inode *inode = dentry->d_inode;
3228 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3231 if (!S_ISREG(inode->i_mode) &&
3232 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3235 return nfs4_proc_set_acl(inode, buf, buflen);
3238 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3239 * and that's what we'll do for e.g. user attributes that haven't been set.
3240 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3241 * attributes in kernel-managed attribute namespaces. */
3242 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3245 struct inode *inode = dentry->d_inode;
3247 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3250 return nfs4_proc_get_acl(inode, buf, buflen);
3253 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3255 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3257 if (buf && buflen < len)
3260 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3264 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3265 .recover_open = nfs4_open_reclaim,
3266 .recover_lock = nfs4_lock_reclaim,
3269 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3270 .recover_open = nfs4_open_expired,
3271 .recover_lock = nfs4_lock_expired,
3274 static struct inode_operations nfs4_file_inode_operations = {
3275 .permission = nfs_permission,
3276 .getattr = nfs_getattr,
3277 .setattr = nfs_setattr,
3278 .getxattr = nfs4_getxattr,
3279 .setxattr = nfs4_setxattr,
3280 .listxattr = nfs4_listxattr,
3283 struct nfs_rpc_ops nfs_v4_clientops = {
3284 .version = 4, /* protocol version */
3285 .dentry_ops = &nfs4_dentry_operations,
3286 .dir_inode_ops = &nfs4_dir_inode_operations,
3287 .file_inode_ops = &nfs4_file_inode_operations,
3288 .getroot = nfs4_proc_get_root,
3289 .getattr = nfs4_proc_getattr,
3290 .setattr = nfs4_proc_setattr,
3291 .lookup = nfs4_proc_lookup,
3292 .access = nfs4_proc_access,
3293 .readlink = nfs4_proc_readlink,
3294 .read = nfs4_proc_read,
3295 .write = nfs4_proc_write,
3296 .commit = nfs4_proc_commit,
3297 .create = nfs4_proc_create,
3298 .remove = nfs4_proc_remove,
3299 .unlink_setup = nfs4_proc_unlink_setup,
3300 .unlink_done = nfs4_proc_unlink_done,
3301 .rename = nfs4_proc_rename,
3302 .link = nfs4_proc_link,
3303 .symlink = nfs4_proc_symlink,
3304 .mkdir = nfs4_proc_mkdir,
3305 .rmdir = nfs4_proc_remove,
3306 .readdir = nfs4_proc_readdir,
3307 .mknod = nfs4_proc_mknod,
3308 .statfs = nfs4_proc_statfs,
3309 .fsinfo = nfs4_proc_fsinfo,
3310 .pathconf = nfs4_proc_pathconf,
3311 .decode_dirent = nfs4_decode_dirent,
3312 .read_setup = nfs4_proc_read_setup,
3313 .write_setup = nfs4_proc_write_setup,
3314 .commit_setup = nfs4_proc_commit_setup,
3315 .file_open = nfs_open,
3316 .file_release = nfs_release,
3317 .lock = nfs4_proc_lock,
3318 .clear_acl_cache = nfs4_zap_acl_attr,