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)
61 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data);
62 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
63 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
64 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
65 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
66 extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
67 extern struct rpc_procinfo nfs4_procedures[];
69 /* Prevent leaks of NFSv4 errors into userland */
70 int nfs4_map_errors(int err)
73 dprintk("%s could not handle NFSv4 error %d\n",
81 * This is our standard bitmap for GETATTR requests.
83 const u32 nfs4_fattr_bitmap[2] = {
88 | FATTR4_WORD0_FILEID,
90 | FATTR4_WORD1_NUMLINKS
92 | FATTR4_WORD1_OWNER_GROUP
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
100 const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
109 const u32 nfs4_pathconf_bitmap[2] = {
111 | FATTR4_WORD0_MAXNAME,
115 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
122 static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
123 struct nfs4_readdir_arg *readdir)
127 BUG_ON(readdir->count < 80);
129 readdir->cookie = cookie;
130 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
135 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
140 * NFSv4 servers do not return entries for '.' and '..'
141 * Therefore, we fake these entries here. We let '.'
142 * have cookie 0 and '..' have cookie 1. Note that
143 * when talking to the server, we always send cookie 0
146 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
149 *p++ = xdr_one; /* next */
150 *p++ = xdr_zero; /* cookie, first word */
151 *p++ = xdr_one; /* cookie, second word */
152 *p++ = xdr_one; /* entry len */
153 memcpy(p, ".\0\0\0", 4); /* entry */
155 *p++ = xdr_one; /* bitmap length */
156 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
157 *p++ = htonl(8); /* attribute buffer length */
158 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
161 *p++ = xdr_one; /* next */
162 *p++ = xdr_zero; /* cookie, first word */
163 *p++ = xdr_two; /* cookie, second word */
164 *p++ = xdr_two; /* entry len */
165 memcpy(p, "..\0\0", 4); /* entry */
167 *p++ = xdr_one; /* bitmap length */
168 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
169 *p++ = htonl(8); /* attribute buffer length */
170 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
172 readdir->pgbase = (char *)p - (char *)start;
173 readdir->count -= readdir->pgbase;
174 kunmap_atomic(start, KM_USER0);
178 renew_lease(struct nfs_server *server, unsigned long timestamp)
180 struct nfs4_client *clp = server->nfs4_state;
181 spin_lock(&clp->cl_lock);
182 if (time_before(clp->cl_last_renewal,timestamp))
183 clp->cl_last_renewal = timestamp;
184 spin_unlock(&clp->cl_lock);
187 static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
189 struct nfs_inode *nfsi = NFS_I(inode);
191 spin_lock(&inode->i_lock);
192 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
193 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
194 nfsi->change_attr = cinfo->after;
195 spin_unlock(&inode->i_lock);
198 struct nfs4_opendata {
200 struct nfs_openargs o_arg;
201 struct nfs_openres o_res;
202 struct nfs_open_confirmargs c_arg;
203 struct nfs_open_confirmres c_res;
204 struct nfs_fattr f_attr;
205 struct nfs_fattr dir_attr;
206 struct dentry *dentry;
208 struct nfs4_state_owner *owner;
214 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
215 struct nfs4_state_owner *sp, int flags,
216 const struct iattr *attrs)
218 struct dentry *parent = dget_parent(dentry);
219 struct inode *dir = parent->d_inode;
220 struct nfs_server *server = NFS_SERVER(dir);
221 struct nfs4_opendata *p;
223 p = kzalloc(sizeof(*p), GFP_KERNEL);
226 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
227 if (p->o_arg.seqid == NULL)
229 atomic_set(&p->count, 1);
230 p->dentry = dget(dentry);
233 atomic_inc(&sp->so_count);
234 p->o_arg.fh = NFS_FH(dir);
235 p->o_arg.open_flags = flags,
236 p->o_arg.clientid = server->nfs4_state->cl_clientid;
237 p->o_arg.id = sp->so_id;
238 p->o_arg.name = &dentry->d_name;
239 p->o_arg.server = server;
240 p->o_arg.bitmask = server->attr_bitmask;
241 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
242 p->o_res.f_attr = &p->f_attr;
243 p->o_res.dir_attr = &p->dir_attr;
244 p->o_res.server = server;
245 nfs_fattr_init(&p->f_attr);
246 nfs_fattr_init(&p->dir_attr);
247 if (flags & O_EXCL) {
248 u32 *s = (u32 *) p->o_arg.u.verifier.data;
251 } else if (flags & O_CREAT) {
252 p->o_arg.u.attrs = &p->attrs;
253 memcpy(&p->attrs, attrs, sizeof(p->attrs));
255 p->c_arg.fh = &p->o_res.fh;
256 p->c_arg.stateid = &p->o_res.stateid;
257 p->c_arg.seqid = p->o_arg.seqid;
266 static void nfs4_opendata_free(struct nfs4_opendata *p)
268 if (p != NULL && atomic_dec_and_test(&p->count)) {
269 nfs_free_seqid(p->o_arg.seqid);
270 nfs4_put_state_owner(p->owner);
277 /* Helper for asynchronous RPC calls */
278 static int nfs4_call_async(struct rpc_clnt *clnt,
279 const struct rpc_call_ops *tk_ops, void *calldata)
281 struct rpc_task *task;
283 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
289 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
294 rpc_clnt_sigmask(task->tk_client, &oldset);
295 ret = rpc_wait_for_completion_task(task);
296 rpc_clnt_sigunmask(task->tk_client, &oldset);
300 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
302 struct inode *inode = state->inode;
304 open_flags &= (FMODE_READ|FMODE_WRITE);
305 /* Protect against nfs4_find_state_byowner() */
306 spin_lock(&state->owner->so_lock);
307 spin_lock(&inode->i_lock);
308 memcpy(&state->stateid, stateid, sizeof(state->stateid));
309 if ((open_flags & FMODE_WRITE))
311 if (open_flags & FMODE_READ)
313 nfs4_state_set_mode_locked(state, state->state | open_flags);
314 spin_unlock(&inode->i_lock);
315 spin_unlock(&state->owner->so_lock);
318 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
321 struct nfs4_state *state = NULL;
323 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
325 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
328 state = nfs4_get_open_state(inode, data->owner);
331 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
340 * reclaim state on the server after a reboot.
342 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
344 struct inode *inode = state->inode;
345 struct nfs_server *server = NFS_SERVER(inode);
346 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
347 struct nfs_openargs o_arg = {
350 .open_flags = state->state,
351 .clientid = server->nfs4_state->cl_clientid,
352 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
353 .bitmask = server->attr_bitmask,
355 struct nfs_openres o_res = {
356 .server = server, /* Grrr */
358 struct rpc_message msg = {
359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
362 .rpc_cred = sp->so_cred,
366 if (delegation != NULL) {
367 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
368 memcpy(&state->stateid, &delegation->stateid,
369 sizeof(state->stateid));
370 set_bit(NFS_DELEGATED_STATE, &state->flags);
373 o_arg.u.delegation_type = delegation->type;
375 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
376 if (o_arg.seqid == NULL)
378 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
379 /* Confirm the sequence as being established */
380 nfs_confirm_seqid(&sp->so_seqid, status);
381 nfs_increment_open_seqid(status, o_arg.seqid);
383 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
384 if (o_res.delegation_type != 0) {
385 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
386 /* Did the server issue an immediate delegation recall? */
388 nfs_async_inode_return_delegation(inode, &o_res.stateid);
391 nfs_free_seqid(o_arg.seqid);
392 clear_bit(NFS_DELEGATED_STATE, &state->flags);
393 /* Ensure we update the inode attributes */
398 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
400 struct nfs_server *server = NFS_SERVER(state->inode);
401 struct nfs4_exception exception = { };
404 err = _nfs4_open_reclaim(sp, state);
405 if (err != -NFS4ERR_DELAY)
407 nfs4_handle_exception(server, err, &exception);
408 } while (exception.retry);
412 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
414 struct nfs4_state_owner *sp = state->owner;
415 struct inode *inode = dentry->d_inode;
416 struct nfs_server *server = NFS_SERVER(inode);
417 struct rpc_message msg = {
418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
419 .rpc_cred = sp->so_cred,
421 struct nfs4_opendata *opendata;
424 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
426 if (state->state == 0)
428 opendata = nfs4_opendata_alloc(dentry, sp, state->state, NULL);
430 if (opendata == NULL)
432 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
433 msg.rpc_argp = &opendata->o_arg;
434 msg.rpc_resp = &opendata->o_res;
435 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
436 sizeof(opendata->o_arg.u.delegation.data));
437 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
438 nfs_increment_open_seqid(status, opendata->o_arg.seqid);
441 if(opendata->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
442 status = _nfs4_proc_open_confirm(opendata);
446 nfs_confirm_seqid(&sp->so_seqid, 0);
448 memcpy(state->stateid.data, opendata->o_res.stateid.data,
449 sizeof(state->stateid.data));
450 clear_bit(NFS_DELEGATED_STATE, &state->flags);
453 nfs4_opendata_free(opendata);
458 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
460 struct nfs4_exception exception = { };
461 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
464 err = _nfs4_open_delegation_recall(dentry, state);
468 case -NFS4ERR_STALE_CLIENTID:
469 case -NFS4ERR_STALE_STATEID:
470 case -NFS4ERR_EXPIRED:
471 /* Don't recall a delegation if it was lost */
472 nfs4_schedule_state_recovery(server->nfs4_state);
475 err = nfs4_handle_exception(server, err, &exception);
476 } while (exception.retry);
480 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
482 struct nfs4_opendata *data = calldata;
483 struct rpc_message msg = {
484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
485 .rpc_argp = &data->c_arg,
486 .rpc_resp = &data->c_res,
487 .rpc_cred = data->owner->so_cred,
489 rpc_call_setup(task, &msg, 0);
492 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
494 struct nfs4_opendata *data = calldata;
496 data->rpc_status = task->tk_status;
497 if (RPC_ASSASSINATED(task))
499 if (data->rpc_status == 0)
500 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
501 sizeof(data->o_res.stateid.data));
502 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
503 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
506 static void nfs4_open_confirm_release(void *calldata)
508 struct nfs4_opendata *data = calldata;
509 struct nfs4_state *state = NULL;
511 /* If this request hasn't been cancelled, do nothing */
512 if (data->cancelled == 0)
514 /* In case of error, no cleanup! */
515 if (data->rpc_status != 0)
517 nfs_confirm_seqid(&data->owner->so_seqid, 0);
518 state = nfs4_opendata_to_nfs4_state(data);
520 nfs4_close_state(state, data->o_arg.open_flags);
522 nfs4_opendata_free(data);
525 static const struct rpc_call_ops nfs4_open_confirm_ops = {
526 .rpc_call_prepare = nfs4_open_confirm_prepare,
527 .rpc_call_done = nfs4_open_confirm_done,
528 .rpc_release = nfs4_open_confirm_release,
532 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
534 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
536 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
537 struct rpc_task *task;
540 atomic_inc(&data->count);
541 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
543 nfs4_opendata_free(data);
544 return PTR_ERR(task);
546 status = nfs4_wait_for_completion_rpc_task(task);
551 status = data->rpc_status;
552 rpc_release_task(task);
556 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
558 struct nfs4_opendata *data = calldata;
559 struct nfs4_state_owner *sp = data->owner;
560 struct rpc_message msg = {
561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
562 .rpc_argp = &data->o_arg,
563 .rpc_resp = &data->o_res,
564 .rpc_cred = sp->so_cred,
567 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
569 /* Update sequence id. */
570 data->o_arg.id = sp->so_id;
571 data->o_arg.clientid = sp->so_client->cl_clientid;
572 rpc_call_setup(task, &msg, 0);
575 static void nfs4_open_done(struct rpc_task *task, void *calldata)
577 struct nfs4_opendata *data = calldata;
579 data->rpc_status = task->tk_status;
580 if (RPC_ASSASSINATED(task))
582 if (task->tk_status == 0) {
583 switch (data->o_res.f_attr->mode & S_IFMT) {
587 data->rpc_status = -ELOOP;
590 data->rpc_status = -EISDIR;
593 data->rpc_status = -ENOTDIR;
596 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
599 static void nfs4_open_release(void *calldata)
601 struct nfs4_opendata *data = calldata;
602 struct nfs4_state *state = NULL;
604 /* If this request hasn't been cancelled, do nothing */
605 if (data->cancelled == 0)
607 /* In case of error, no cleanup! */
608 if (data->rpc_status != 0)
610 /* In case we need an open_confirm, no cleanup! */
611 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
613 nfs_confirm_seqid(&data->owner->so_seqid, 0);
614 state = nfs4_opendata_to_nfs4_state(data);
616 nfs4_close_state(state, data->o_arg.open_flags);
618 nfs4_opendata_free(data);
621 static const struct rpc_call_ops nfs4_open_ops = {
622 .rpc_call_prepare = nfs4_open_prepare,
623 .rpc_call_done = nfs4_open_done,
624 .rpc_release = nfs4_open_release,
628 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
630 static int _nfs4_proc_open(struct nfs4_opendata *data)
632 struct inode *dir = data->dir->d_inode;
633 struct nfs_server *server = NFS_SERVER(dir);
634 struct nfs_openargs *o_arg = &data->o_arg;
635 struct nfs_openres *o_res = &data->o_res;
636 struct rpc_task *task;
639 atomic_inc(&data->count);
640 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
642 nfs4_opendata_free(data);
643 return PTR_ERR(task);
645 status = nfs4_wait_for_completion_rpc_task(task);
650 status = data->rpc_status;
651 rpc_release_task(task);
655 if (o_arg->open_flags & O_CREAT) {
656 update_changeattr(dir, &o_res->cinfo);
657 nfs_post_op_update_inode(dir, o_res->dir_attr);
659 nfs_refresh_inode(dir, o_res->dir_attr);
660 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
661 status = _nfs4_proc_open_confirm(data);
665 nfs_confirm_seqid(&data->owner->so_seqid, 0);
666 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
667 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
671 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
673 struct nfs_access_entry cache;
677 if (openflags & FMODE_READ)
679 if (openflags & FMODE_WRITE)
681 status = nfs_access_get_cached(inode, cred, &cache);
685 /* Be clever: ask server to check for all possible rights */
686 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
688 cache.jiffies = jiffies;
689 status = _nfs4_proc_access(inode, &cache);
692 nfs_access_add_cache(inode, &cache);
694 if ((cache.mask & mask) == mask)
701 * reclaim state on the server after a network partition.
702 * Assumes caller holds the appropriate lock
704 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
706 struct dentry *parent = dget_parent(dentry);
707 struct inode *inode = state->inode;
708 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
709 struct nfs4_opendata *opendata;
710 struct nfs4_state *newstate;
711 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
714 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
715 status = _nfs4_do_access(inode, sp->so_cred, openflags);
718 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
719 set_bit(NFS_DELEGATED_STATE, &state->flags);
723 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
724 if (opendata == NULL)
726 status = _nfs4_proc_open(opendata);
729 newstate = nfs4_opendata_to_nfs4_state(opendata);
730 if (newstate != state)
732 if (opendata->o_res.delegation_type != 0) {
733 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
734 nfs_inode_set_delegation(inode, sp->so_cred,
737 nfs_inode_reclaim_delegation(inode, sp->so_cred,
741 nfs4_close_state(newstate, openflags);
743 nfs4_opendata_free(opendata);
744 clear_bit(NFS_DELEGATED_STATE, &state->flags);
750 /* Invalidate the state owner so we don't ever use it again */
751 nfs4_drop_state_owner(sp);
753 /* Should we be trying to close that stateid? */
754 goto out_close_state;
757 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
759 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
760 struct nfs4_exception exception = { };
764 err = _nfs4_open_expired(sp, state, dentry);
765 if (err == -NFS4ERR_DELAY)
766 nfs4_handle_exception(server, err, &exception);
767 } while (exception.retry);
771 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
773 struct nfs_inode *nfsi = NFS_I(state->inode);
774 struct nfs_open_context *ctx;
777 spin_lock(&state->inode->i_lock);
778 list_for_each_entry(ctx, &nfsi->open_files, list) {
779 if (ctx->state != state)
781 get_nfs_open_context(ctx);
782 spin_unlock(&state->inode->i_lock);
783 status = nfs4_do_open_expired(sp, state, ctx->dentry);
784 put_nfs_open_context(ctx);
787 spin_unlock(&state->inode->i_lock);
792 * Returns a referenced nfs4_state if there is an open delegation on the file
794 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
796 struct nfs_delegation *delegation;
797 struct nfs_server *server = NFS_SERVER(inode);
798 struct nfs4_client *clp = server->nfs4_state;
799 struct nfs_inode *nfsi = NFS_I(inode);
800 struct nfs4_state_owner *sp = NULL;
801 struct nfs4_state *state = NULL;
802 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
805 /* Protect against reboot recovery - NOTE ORDER! */
806 down_read(&clp->cl_sem);
807 /* Protect against delegation recall */
808 down_read(&nfsi->rwsem);
809 delegation = NFS_I(inode)->delegation;
811 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
814 if (!(sp = nfs4_get_state_owner(server, cred))) {
815 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
818 state = nfs4_get_open_state(inode, sp);
823 if ((state->state & open_flags) == open_flags) {
824 spin_lock(&inode->i_lock);
825 if (open_flags & FMODE_READ)
827 if (open_flags & FMODE_WRITE)
829 spin_unlock(&inode->i_lock);
831 } else if (state->state != 0)
835 err = _nfs4_do_access(inode, cred, open_flags);
839 set_bit(NFS_DELEGATED_STATE, &state->flags);
840 update_open_stateid(state, &delegation->stateid, open_flags);
842 nfs4_put_state_owner(sp);
843 up_read(&nfsi->rwsem);
844 up_read(&clp->cl_sem);
850 nfs4_put_open_state(state);
851 nfs4_put_state_owner(sp);
853 up_read(&nfsi->rwsem);
854 up_read(&clp->cl_sem);
856 nfs_inode_return_delegation(inode);
860 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
862 struct nfs4_exception exception = { };
863 struct nfs4_state *res;
867 err = _nfs4_open_delegated(inode, flags, cred, &res);
870 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
872 } while (exception.retry);
877 * Returns a referenced nfs4_state
879 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
881 struct nfs4_state_owner *sp;
882 struct nfs4_state *state = NULL;
883 struct nfs_server *server = NFS_SERVER(dir);
884 struct nfs4_client *clp = server->nfs4_state;
885 struct nfs4_opendata *opendata;
888 /* Protect against reboot recovery conflicts */
889 down_read(&clp->cl_sem);
891 if (!(sp = nfs4_get_state_owner(server, cred))) {
892 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
895 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
896 if (opendata == NULL)
897 goto err_put_state_owner;
899 status = _nfs4_proc_open(opendata);
901 goto err_opendata_free;
904 state = nfs4_opendata_to_nfs4_state(opendata);
906 goto err_opendata_free;
907 if (opendata->o_res.delegation_type != 0)
908 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
909 nfs4_opendata_free(opendata);
910 nfs4_put_state_owner(sp);
911 up_read(&clp->cl_sem);
915 nfs4_opendata_free(opendata);
917 nfs4_put_state_owner(sp);
919 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
920 up_read(&clp->cl_sem);
926 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
928 struct nfs4_exception exception = { };
929 struct nfs4_state *res;
933 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
936 /* NOTE: BAD_SEQID means the server and client disagree about the
937 * book-keeping w.r.t. state-changing operations
938 * (OPEN/CLOSE/LOCK/LOCKU...)
939 * It is actually a sign of a bug on the client or on the server.
941 * If we receive a BAD_SEQID error in the particular case of
942 * doing an OPEN, we assume that nfs_increment_open_seqid() will
943 * have unhashed the old state_owner for us, and that we can
944 * therefore safely retry using a new one. We should still warn
947 if (status == -NFS4ERR_BAD_SEQID) {
948 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
953 * BAD_STATEID on OPEN means that the server cancelled our
954 * state before it received the OPEN_CONFIRM.
955 * Recover by retrying the request as per the discussion
956 * on Page 181 of RFC3530.
958 if (status == -NFS4ERR_BAD_STATEID) {
962 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
963 status, &exception));
964 } while (exception.retry);
968 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
969 struct nfs_fh *fhandle, struct iattr *sattr,
970 struct nfs4_state *state)
972 struct nfs_setattrargs arg = {
976 .bitmask = server->attr_bitmask,
978 struct nfs_setattrres res = {
982 struct rpc_message msg = {
983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
989 nfs_fattr_init(fattr);
992 msg.rpc_cred = state->owner->so_cred;
993 nfs4_copy_stateid(&arg.stateid, state, current->files);
995 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
997 status = rpc_call_sync(server->client, &msg, 0);
1001 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1002 struct nfs_fh *fhandle, struct iattr *sattr,
1003 struct nfs4_state *state)
1005 struct nfs4_exception exception = { };
1008 err = nfs4_handle_exception(server,
1009 _nfs4_do_setattr(server, fattr, fhandle, sattr,
1012 } while (exception.retry);
1016 struct nfs4_closedata {
1017 struct inode *inode;
1018 struct nfs4_state *state;
1019 struct nfs_closeargs arg;
1020 struct nfs_closeres res;
1021 struct nfs_fattr fattr;
1024 static void nfs4_free_closedata(void *data)
1026 struct nfs4_closedata *calldata = data;
1027 struct nfs4_state_owner *sp = calldata->state->owner;
1029 nfs4_put_open_state(calldata->state);
1030 nfs_free_seqid(calldata->arg.seqid);
1031 nfs4_put_state_owner(sp);
1035 static void nfs4_close_done(struct rpc_task *task, void *data)
1037 struct nfs4_closedata *calldata = data;
1038 struct nfs4_state *state = calldata->state;
1039 struct nfs_server *server = NFS_SERVER(calldata->inode);
1041 if (RPC_ASSASSINATED(task))
1043 /* hmm. we are done with the inode, and in the process of freeing
1044 * the state_owner. we keep this around to process errors
1046 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1047 switch (task->tk_status) {
1049 memcpy(&state->stateid, &calldata->res.stateid,
1050 sizeof(state->stateid));
1052 case -NFS4ERR_STALE_STATEID:
1053 case -NFS4ERR_EXPIRED:
1054 nfs4_schedule_state_recovery(server->nfs4_state);
1057 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1058 rpc_restart_call(task);
1062 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1065 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1067 struct nfs4_closedata *calldata = data;
1068 struct nfs4_state *state = calldata->state;
1069 struct rpc_message msg = {
1070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1071 .rpc_argp = &calldata->arg,
1072 .rpc_resp = &calldata->res,
1073 .rpc_cred = state->owner->so_cred,
1075 int mode = 0, old_mode;
1077 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1079 /* Recalculate the new open mode in case someone reopened the file
1080 * while we were waiting in line to be scheduled.
1082 spin_lock(&state->owner->so_lock);
1083 spin_lock(&calldata->inode->i_lock);
1084 mode = old_mode = state->state;
1085 if (state->nreaders == 0)
1086 mode &= ~FMODE_READ;
1087 if (state->nwriters == 0)
1088 mode &= ~FMODE_WRITE;
1089 nfs4_state_set_mode_locked(state, mode);
1090 spin_unlock(&calldata->inode->i_lock);
1091 spin_unlock(&state->owner->so_lock);
1092 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1093 /* Note: exit _without_ calling nfs4_close_done */
1094 task->tk_action = NULL;
1097 nfs_fattr_init(calldata->res.fattr);
1099 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1100 calldata->arg.open_flags = mode;
1101 rpc_call_setup(task, &msg, 0);
1104 static const struct rpc_call_ops nfs4_close_ops = {
1105 .rpc_call_prepare = nfs4_close_prepare,
1106 .rpc_call_done = nfs4_close_done,
1107 .rpc_release = nfs4_free_closedata,
1111 * It is possible for data to be read/written from a mem-mapped file
1112 * after the sys_close call (which hits the vfs layer as a flush).
1113 * This means that we can't safely call nfsv4 close on a file until
1114 * the inode is cleared. This in turn means that we are not good
1115 * NFSv4 citizens - we do not indicate to the server to update the file's
1116 * share state even when we are done with one of the three share
1117 * stateid's in the inode.
1119 * NOTE: Caller must be holding the sp->so_owner semaphore!
1121 int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
1123 struct nfs_server *server = NFS_SERVER(inode);
1124 struct nfs4_closedata *calldata;
1125 int status = -ENOMEM;
1127 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1128 if (calldata == NULL)
1130 calldata->inode = inode;
1131 calldata->state = state;
1132 calldata->arg.fh = NFS_FH(inode);
1133 calldata->arg.stateid = &state->stateid;
1134 /* Serialization for the sequence id */
1135 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1136 if (calldata->arg.seqid == NULL)
1137 goto out_free_calldata;
1138 calldata->arg.bitmask = server->attr_bitmask;
1139 calldata->res.fattr = &calldata->fattr;
1140 calldata->res.server = server;
1142 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
1146 nfs_free_seqid(calldata->arg.seqid);
1153 static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1157 filp = lookup_instantiate_filp(nd, dentry, NULL);
1158 if (!IS_ERR(filp)) {
1159 struct nfs_open_context *ctx;
1160 ctx = (struct nfs_open_context *)filp->private_data;
1163 nfs4_close_state(state, nd->intent.open.flags);
1167 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1170 struct rpc_cred *cred;
1171 struct nfs4_state *state;
1174 if (nd->flags & LOOKUP_CREATE) {
1175 attr.ia_mode = nd->intent.open.create_mode;
1176 attr.ia_valid = ATTR_MODE;
1177 if (!IS_POSIXACL(dir))
1178 attr.ia_mode &= ~current->fs->umask;
1181 BUG_ON(nd->intent.open.flags & O_CREAT);
1184 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1186 return (struct dentry *)cred;
1187 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1189 if (IS_ERR(state)) {
1190 if (PTR_ERR(state) == -ENOENT)
1191 d_add(dentry, NULL);
1192 return (struct dentry *)state;
1194 res = d_add_unique(dentry, igrab(state->inode));
1197 nfs4_intent_set_file(nd, dentry, state);
1202 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1204 struct rpc_cred *cred;
1205 struct nfs4_state *state;
1207 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1209 return PTR_ERR(cred);
1210 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1212 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1214 if (IS_ERR(state)) {
1215 switch (PTR_ERR(state)) {
1221 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1224 if (dentry->d_inode == NULL)
1229 if (state->inode == dentry->d_inode) {
1230 nfs4_intent_set_file(nd, dentry, state);
1233 nfs4_close_state(state, openflags);
1240 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1242 struct nfs4_server_caps_res res = {};
1243 struct rpc_message msg = {
1244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1245 .rpc_argp = fhandle,
1250 status = rpc_call_sync(server->client, &msg, 0);
1252 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1253 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1254 server->caps |= NFS_CAP_ACLS;
1255 if (res.has_links != 0)
1256 server->caps |= NFS_CAP_HARDLINKS;
1257 if (res.has_symlinks != 0)
1258 server->caps |= NFS_CAP_SYMLINKS;
1259 server->acl_bitmask = res.acl_bitmask;
1264 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1266 struct nfs4_exception exception = { };
1269 err = nfs4_handle_exception(server,
1270 _nfs4_server_capabilities(server, fhandle),
1272 } while (exception.retry);
1276 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1277 struct nfs_fsinfo *info)
1279 struct nfs4_lookup_root_arg args = {
1280 .bitmask = nfs4_fattr_bitmap,
1282 struct nfs4_lookup_res res = {
1284 .fattr = info->fattr,
1287 struct rpc_message msg = {
1288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1292 nfs_fattr_init(info->fattr);
1293 return rpc_call_sync(server->client, &msg, 0);
1296 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1297 struct nfs_fsinfo *info)
1299 struct nfs4_exception exception = { };
1302 err = nfs4_handle_exception(server,
1303 _nfs4_lookup_root(server, fhandle, info),
1305 } while (exception.retry);
1309 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1310 struct nfs_fsinfo *info)
1312 struct nfs_fattr * fattr = info->fattr;
1315 struct nfs4_lookup_arg args = {
1318 .bitmask = nfs4_fattr_bitmap,
1320 struct nfs4_lookup_res res = {
1325 struct rpc_message msg = {
1326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1333 * Now we do a separate LOOKUP for each component of the mount path.
1334 * The LOOKUPs are done separately so that we can conveniently
1335 * catch an ERR_WRONGSEC if it occurs along the way...
1337 status = nfs4_lookup_root(server, fhandle, info);
1341 p = server->mnt_path;
1343 struct nfs4_exception exception = { };
1350 while (*p && (*p != '/'))
1355 nfs_fattr_init(fattr);
1356 status = nfs4_handle_exception(server,
1357 rpc_call_sync(server->client, &msg, 0),
1359 } while (exception.retry);
1362 if (status == -ENOENT) {
1363 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1364 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1369 status = nfs4_server_capabilities(server, fhandle);
1371 status = nfs4_do_fsinfo(server, fhandle, info);
1376 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1378 struct nfs4_getattr_arg args = {
1380 .bitmask = server->attr_bitmask,
1382 struct nfs4_getattr_res res = {
1386 struct rpc_message msg = {
1387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1392 nfs_fattr_init(fattr);
1393 return rpc_call_sync(server->client, &msg, 0);
1396 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1398 struct nfs4_exception exception = { };
1401 err = nfs4_handle_exception(server,
1402 _nfs4_proc_getattr(server, fhandle, fattr),
1404 } while (exception.retry);
1409 * The file is not closed if it is opened due to the a request to change
1410 * the size of the file. The open call will not be needed once the
1411 * VFS layer lookup-intents are implemented.
1413 * Close is called when the inode is destroyed.
1414 * If we haven't opened the file for O_WRONLY, we
1415 * need to in the size_change case to obtain a stateid.
1418 * Because OPEN is always done by name in nfsv4, it is
1419 * possible that we opened a different file by the same
1420 * name. We can recognize this race condition, but we
1421 * can't do anything about it besides returning an error.
1423 * This will be fixed with VFS changes (lookup-intent).
1426 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1427 struct iattr *sattr)
1429 struct rpc_cred *cred;
1430 struct inode *inode = dentry->d_inode;
1431 struct nfs_open_context *ctx;
1432 struct nfs4_state *state = NULL;
1435 nfs_fattr_init(fattr);
1437 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1439 return PTR_ERR(cred);
1441 /* Search for an existing open(O_WRITE) file */
1442 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1446 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1447 NFS_FH(inode), sattr, state);
1449 nfs_setattr_update_inode(inode, sattr);
1451 put_nfs_open_context(ctx);
1456 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1457 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1460 struct nfs_server *server = NFS_SERVER(dir);
1461 struct nfs4_lookup_arg args = {
1462 .bitmask = server->attr_bitmask,
1463 .dir_fh = NFS_FH(dir),
1466 struct nfs4_lookup_res res = {
1471 struct rpc_message msg = {
1472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1477 nfs_fattr_init(fattr);
1479 dprintk("NFS call lookup %s\n", name->name);
1480 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1481 dprintk("NFS reply lookup: %d\n", status);
1485 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1487 struct nfs4_exception exception = { };
1490 err = nfs4_handle_exception(NFS_SERVER(dir),
1491 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1493 } while (exception.retry);
1497 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1499 struct nfs4_accessargs args = {
1500 .fh = NFS_FH(inode),
1502 struct nfs4_accessres res = { 0 };
1503 struct rpc_message msg = {
1504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1507 .rpc_cred = entry->cred,
1509 int mode = entry->mask;
1513 * Determine which access bits we want to ask for...
1515 if (mode & MAY_READ)
1516 args.access |= NFS4_ACCESS_READ;
1517 if (S_ISDIR(inode->i_mode)) {
1518 if (mode & MAY_WRITE)
1519 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1520 if (mode & MAY_EXEC)
1521 args.access |= NFS4_ACCESS_LOOKUP;
1523 if (mode & MAY_WRITE)
1524 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1525 if (mode & MAY_EXEC)
1526 args.access |= NFS4_ACCESS_EXECUTE;
1528 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1531 if (res.access & NFS4_ACCESS_READ)
1532 entry->mask |= MAY_READ;
1533 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1534 entry->mask |= MAY_WRITE;
1535 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1536 entry->mask |= MAY_EXEC;
1541 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1543 struct nfs4_exception exception = { };
1546 err = nfs4_handle_exception(NFS_SERVER(inode),
1547 _nfs4_proc_access(inode, entry),
1549 } while (exception.retry);
1554 * TODO: For the time being, we don't try to get any attributes
1555 * along with any of the zero-copy operations READ, READDIR,
1558 * In the case of the first three, we want to put the GETATTR
1559 * after the read-type operation -- this is because it is hard
1560 * to predict the length of a GETATTR response in v4, and thus
1561 * align the READ data correctly. This means that the GETATTR
1562 * may end up partially falling into the page cache, and we should
1563 * shift it into the 'tail' of the xdr_buf before processing.
1564 * To do this efficiently, we need to know the total length
1565 * of data received, which doesn't seem to be available outside
1568 * In the case of WRITE, we also want to put the GETATTR after
1569 * the operation -- in this case because we want to make sure
1570 * we get the post-operation mtime and size. This means that
1571 * we can't use xdr_encode_pages() as written: we need a variant
1572 * of it which would leave room in the 'tail' iovec.
1574 * Both of these changes to the XDR layer would in fact be quite
1575 * minor, but I decided to leave them for a subsequent patch.
1577 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1578 unsigned int pgbase, unsigned int pglen)
1580 struct nfs4_readlink args = {
1581 .fh = NFS_FH(inode),
1586 struct rpc_message msg = {
1587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1592 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1595 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1596 unsigned int pgbase, unsigned int pglen)
1598 struct nfs4_exception exception = { };
1601 err = nfs4_handle_exception(NFS_SERVER(inode),
1602 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1604 } while (exception.retry);
1608 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1610 int flags = rdata->flags;
1611 struct inode *inode = rdata->inode;
1612 struct nfs_fattr *fattr = rdata->res.fattr;
1613 struct nfs_server *server = NFS_SERVER(inode);
1614 struct rpc_message msg = {
1615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1616 .rpc_argp = &rdata->args,
1617 .rpc_resp = &rdata->res,
1618 .rpc_cred = rdata->cred,
1620 unsigned long timestamp = jiffies;
1623 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1624 (long long) rdata->args.offset);
1626 nfs_fattr_init(fattr);
1627 status = rpc_call_sync(server->client, &msg, flags);
1629 renew_lease(server, timestamp);
1630 dprintk("NFS reply read: %d\n", status);
1634 static int nfs4_proc_read(struct nfs_read_data *rdata)
1636 struct nfs4_exception exception = { };
1639 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1640 _nfs4_proc_read(rdata),
1642 } while (exception.retry);
1646 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1648 int rpcflags = wdata->flags;
1649 struct inode *inode = wdata->inode;
1650 struct nfs_fattr *fattr = wdata->res.fattr;
1651 struct nfs_server *server = NFS_SERVER(inode);
1652 struct rpc_message msg = {
1653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1654 .rpc_argp = &wdata->args,
1655 .rpc_resp = &wdata->res,
1656 .rpc_cred = wdata->cred,
1660 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1661 (long long) wdata->args.offset);
1663 wdata->args.bitmask = server->attr_bitmask;
1664 wdata->res.server = server;
1665 nfs_fattr_init(fattr);
1666 status = rpc_call_sync(server->client, &msg, rpcflags);
1667 dprintk("NFS reply write: %d\n", status);
1670 nfs_post_op_update_inode(inode, fattr);
1671 return wdata->res.count;
1674 static int nfs4_proc_write(struct nfs_write_data *wdata)
1676 struct nfs4_exception exception = { };
1679 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1680 _nfs4_proc_write(wdata),
1682 } while (exception.retry);
1686 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1688 struct inode *inode = cdata->inode;
1689 struct nfs_fattr *fattr = cdata->res.fattr;
1690 struct nfs_server *server = NFS_SERVER(inode);
1691 struct rpc_message msg = {
1692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1693 .rpc_argp = &cdata->args,
1694 .rpc_resp = &cdata->res,
1695 .rpc_cred = cdata->cred,
1699 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1700 (long long) cdata->args.offset);
1702 cdata->args.bitmask = server->attr_bitmask;
1703 cdata->res.server = server;
1704 nfs_fattr_init(fattr);
1705 status = rpc_call_sync(server->client, &msg, 0);
1706 dprintk("NFS reply commit: %d\n", status);
1708 nfs_post_op_update_inode(inode, fattr);
1712 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1714 struct nfs4_exception exception = { };
1717 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1718 _nfs4_proc_commit(cdata),
1720 } while (exception.retry);
1726 * We will need to arrange for the VFS layer to provide an atomic open.
1727 * Until then, this create/open method is prone to inefficiency and race
1728 * conditions due to the lookup, create, and open VFS calls from sys_open()
1729 * placed on the wire.
1731 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1732 * The file will be opened again in the subsequent VFS open call
1733 * (nfs4_proc_file_open).
1735 * The open for read will just hang around to be used by any process that
1736 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1740 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1741 int flags, struct nameidata *nd)
1743 struct nfs4_state *state;
1744 struct rpc_cred *cred;
1747 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1749 status = PTR_ERR(cred);
1752 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1754 if (IS_ERR(state)) {
1755 status = PTR_ERR(state);
1758 d_instantiate(dentry, igrab(state->inode));
1759 if (flags & O_EXCL) {
1760 struct nfs_fattr fattr;
1761 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1762 NFS_FH(state->inode), sattr, state);
1764 nfs_setattr_update_inode(state->inode, sattr);
1766 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1767 nfs4_intent_set_file(nd, dentry, state);
1769 nfs4_close_state(state, flags);
1774 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1776 struct nfs_server *server = NFS_SERVER(dir);
1777 struct nfs4_remove_arg args = {
1780 .bitmask = server->attr_bitmask,
1782 struct nfs_fattr dir_attr;
1783 struct nfs4_remove_res res = {
1785 .dir_attr = &dir_attr,
1787 struct rpc_message msg = {
1788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1794 nfs_fattr_init(res.dir_attr);
1795 status = rpc_call_sync(server->client, &msg, 0);
1797 update_changeattr(dir, &res.cinfo);
1798 nfs_post_op_update_inode(dir, res.dir_attr);
1803 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1805 struct nfs4_exception exception = { };
1808 err = nfs4_handle_exception(NFS_SERVER(dir),
1809 _nfs4_proc_remove(dir, name),
1811 } while (exception.retry);
1815 struct unlink_desc {
1816 struct nfs4_remove_arg args;
1817 struct nfs4_remove_res res;
1818 struct nfs_fattr dir_attr;
1821 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1824 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1825 struct unlink_desc *up;
1827 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1831 up->args.fh = NFS_FH(dir->d_inode);
1832 up->args.name = name;
1833 up->args.bitmask = server->attr_bitmask;
1834 up->res.server = server;
1835 up->res.dir_attr = &up->dir_attr;
1837 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1838 msg->rpc_argp = &up->args;
1839 msg->rpc_resp = &up->res;
1843 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1845 struct rpc_message *msg = &task->tk_msg;
1846 struct unlink_desc *up;
1848 if (msg->rpc_resp != NULL) {
1849 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1850 update_changeattr(dir->d_inode, &up->res.cinfo);
1851 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1853 msg->rpc_resp = NULL;
1854 msg->rpc_argp = NULL;
1859 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1860 struct inode *new_dir, struct qstr *new_name)
1862 struct nfs_server *server = NFS_SERVER(old_dir);
1863 struct nfs4_rename_arg arg = {
1864 .old_dir = NFS_FH(old_dir),
1865 .new_dir = NFS_FH(new_dir),
1866 .old_name = old_name,
1867 .new_name = new_name,
1868 .bitmask = server->attr_bitmask,
1870 struct nfs_fattr old_fattr, new_fattr;
1871 struct nfs4_rename_res res = {
1873 .old_fattr = &old_fattr,
1874 .new_fattr = &new_fattr,
1876 struct rpc_message msg = {
1877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1883 nfs_fattr_init(res.old_fattr);
1884 nfs_fattr_init(res.new_fattr);
1885 status = rpc_call_sync(server->client, &msg, 0);
1888 update_changeattr(old_dir, &res.old_cinfo);
1889 nfs_post_op_update_inode(old_dir, res.old_fattr);
1890 update_changeattr(new_dir, &res.new_cinfo);
1891 nfs_post_op_update_inode(new_dir, res.new_fattr);
1896 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1897 struct inode *new_dir, struct qstr *new_name)
1899 struct nfs4_exception exception = { };
1902 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1903 _nfs4_proc_rename(old_dir, old_name,
1906 } while (exception.retry);
1910 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1912 struct nfs_server *server = NFS_SERVER(inode);
1913 struct nfs4_link_arg arg = {
1914 .fh = NFS_FH(inode),
1915 .dir_fh = NFS_FH(dir),
1917 .bitmask = server->attr_bitmask,
1919 struct nfs_fattr fattr, dir_attr;
1920 struct nfs4_link_res res = {
1923 .dir_attr = &dir_attr,
1925 struct rpc_message msg = {
1926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1932 nfs_fattr_init(res.fattr);
1933 nfs_fattr_init(res.dir_attr);
1934 status = rpc_call_sync(server->client, &msg, 0);
1936 update_changeattr(dir, &res.cinfo);
1937 nfs_post_op_update_inode(dir, res.dir_attr);
1938 nfs_refresh_inode(inode, res.fattr);
1944 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1946 struct nfs4_exception exception = { };
1949 err = nfs4_handle_exception(NFS_SERVER(inode),
1950 _nfs4_proc_link(inode, dir, name),
1952 } while (exception.retry);
1956 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1957 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1958 struct nfs_fattr *fattr)
1960 struct nfs_server *server = NFS_SERVER(dir);
1961 struct nfs_fattr dir_fattr;
1962 struct nfs4_create_arg arg = {
1963 .dir_fh = NFS_FH(dir),
1968 .bitmask = server->attr_bitmask,
1970 struct nfs4_create_res res = {
1974 .dir_fattr = &dir_fattr,
1976 struct rpc_message msg = {
1977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1983 if (path->len > NFS4_MAXPATHLEN)
1984 return -ENAMETOOLONG;
1985 arg.u.symlink = path;
1986 nfs_fattr_init(fattr);
1987 nfs_fattr_init(&dir_fattr);
1989 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1991 update_changeattr(dir, &res.dir_cinfo);
1992 nfs_post_op_update_inode(dir, res.dir_fattr);
1996 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1997 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1998 struct nfs_fattr *fattr)
2000 struct nfs4_exception exception = { };
2003 err = nfs4_handle_exception(NFS_SERVER(dir),
2004 _nfs4_proc_symlink(dir, name, path, sattr,
2007 } while (exception.retry);
2011 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2012 struct iattr *sattr)
2014 struct nfs_server *server = NFS_SERVER(dir);
2015 struct nfs_fh fhandle;
2016 struct nfs_fattr fattr, dir_fattr;
2017 struct nfs4_create_arg arg = {
2018 .dir_fh = NFS_FH(dir),
2020 .name = &dentry->d_name,
2023 .bitmask = server->attr_bitmask,
2025 struct nfs4_create_res res = {
2029 .dir_fattr = &dir_fattr,
2031 struct rpc_message msg = {
2032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2038 nfs_fattr_init(&fattr);
2039 nfs_fattr_init(&dir_fattr);
2041 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2043 update_changeattr(dir, &res.dir_cinfo);
2044 nfs_post_op_update_inode(dir, res.dir_fattr);
2045 status = nfs_instantiate(dentry, &fhandle, &fattr);
2050 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2051 struct iattr *sattr)
2053 struct nfs4_exception exception = { };
2056 err = nfs4_handle_exception(NFS_SERVER(dir),
2057 _nfs4_proc_mkdir(dir, dentry, sattr),
2059 } while (exception.retry);
2063 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2064 u64 cookie, struct page *page, unsigned int count, int plus)
2066 struct inode *dir = dentry->d_inode;
2067 struct nfs4_readdir_arg args = {
2072 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2074 struct nfs4_readdir_res res;
2075 struct rpc_message msg = {
2076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2083 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2084 dentry->d_parent->d_name.name,
2085 dentry->d_name.name,
2086 (unsigned long long)cookie);
2088 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2089 res.pgbase = args.pgbase;
2090 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2092 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2094 dprintk("%s: returns %d\n", __FUNCTION__, status);
2098 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2099 u64 cookie, struct page *page, unsigned int count, int plus)
2101 struct nfs4_exception exception = { };
2104 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2105 _nfs4_proc_readdir(dentry, cred, cookie,
2108 } while (exception.retry);
2112 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2113 struct iattr *sattr, dev_t rdev)
2115 struct nfs_server *server = NFS_SERVER(dir);
2117 struct nfs_fattr fattr, dir_fattr;
2118 struct nfs4_create_arg arg = {
2119 .dir_fh = NFS_FH(dir),
2121 .name = &dentry->d_name,
2123 .bitmask = server->attr_bitmask,
2125 struct nfs4_create_res res = {
2129 .dir_fattr = &dir_fattr,
2131 struct rpc_message msg = {
2132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2137 int mode = sattr->ia_mode;
2139 nfs_fattr_init(&fattr);
2140 nfs_fattr_init(&dir_fattr);
2142 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2143 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2145 arg.ftype = NF4FIFO;
2146 else if (S_ISBLK(mode)) {
2148 arg.u.device.specdata1 = MAJOR(rdev);
2149 arg.u.device.specdata2 = MINOR(rdev);
2151 else if (S_ISCHR(mode)) {
2153 arg.u.device.specdata1 = MAJOR(rdev);
2154 arg.u.device.specdata2 = MINOR(rdev);
2157 arg.ftype = NF4SOCK;
2159 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2161 update_changeattr(dir, &res.dir_cinfo);
2162 nfs_post_op_update_inode(dir, res.dir_fattr);
2163 status = nfs_instantiate(dentry, &fh, &fattr);
2168 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2169 struct iattr *sattr, dev_t rdev)
2171 struct nfs4_exception exception = { };
2174 err = nfs4_handle_exception(NFS_SERVER(dir),
2175 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2177 } while (exception.retry);
2181 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2182 struct nfs_fsstat *fsstat)
2184 struct nfs4_statfs_arg args = {
2186 .bitmask = server->attr_bitmask,
2188 struct rpc_message msg = {
2189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2194 nfs_fattr_init(fsstat->fattr);
2195 return rpc_call_sync(server->client, &msg, 0);
2198 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2200 struct nfs4_exception exception = { };
2203 err = nfs4_handle_exception(server,
2204 _nfs4_proc_statfs(server, fhandle, fsstat),
2206 } while (exception.retry);
2210 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2211 struct nfs_fsinfo *fsinfo)
2213 struct nfs4_fsinfo_arg args = {
2215 .bitmask = server->attr_bitmask,
2217 struct rpc_message msg = {
2218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2223 return rpc_call_sync(server->client, &msg, 0);
2226 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2228 struct nfs4_exception exception = { };
2232 err = nfs4_handle_exception(server,
2233 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2235 } while (exception.retry);
2239 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2241 nfs_fattr_init(fsinfo->fattr);
2242 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2245 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2246 struct nfs_pathconf *pathconf)
2248 struct nfs4_pathconf_arg args = {
2250 .bitmask = server->attr_bitmask,
2252 struct rpc_message msg = {
2253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2255 .rpc_resp = pathconf,
2258 /* None of the pathconf attributes are mandatory to implement */
2259 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2260 memset(pathconf, 0, sizeof(*pathconf));
2264 nfs_fattr_init(pathconf->fattr);
2265 return rpc_call_sync(server->client, &msg, 0);
2268 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2269 struct nfs_pathconf *pathconf)
2271 struct nfs4_exception exception = { };
2275 err = nfs4_handle_exception(server,
2276 _nfs4_proc_pathconf(server, fhandle, pathconf),
2278 } while (exception.retry);
2282 static void nfs4_read_done(struct rpc_task *task, void *calldata)
2284 struct nfs_read_data *data = calldata;
2285 struct inode *inode = data->inode;
2287 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2288 rpc_restart_call(task);
2291 if (task->tk_status > 0)
2292 renew_lease(NFS_SERVER(inode), data->timestamp);
2293 /* Call back common NFS readpage processing */
2294 nfs_readpage_result(task, calldata);
2297 static const struct rpc_call_ops nfs4_read_ops = {
2298 .rpc_call_done = nfs4_read_done,
2299 .rpc_release = nfs_readdata_release,
2303 nfs4_proc_read_setup(struct nfs_read_data *data)
2305 struct rpc_task *task = &data->task;
2306 struct rpc_message msg = {
2307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2308 .rpc_argp = &data->args,
2309 .rpc_resp = &data->res,
2310 .rpc_cred = data->cred,
2312 struct inode *inode = data->inode;
2315 data->timestamp = jiffies;
2317 /* N.B. Do we need to test? Never called for swapfile inode */
2318 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2320 /* Finalize the task. */
2321 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
2322 rpc_call_setup(task, &msg, 0);
2325 static void nfs4_write_done(struct rpc_task *task, void *calldata)
2327 struct nfs_write_data *data = calldata;
2328 struct inode *inode = data->inode;
2330 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2331 rpc_restart_call(task);
2334 if (task->tk_status >= 0) {
2335 renew_lease(NFS_SERVER(inode), data->timestamp);
2336 nfs_post_op_update_inode(inode, data->res.fattr);
2338 /* Call back common NFS writeback processing */
2339 nfs_writeback_done(task, calldata);
2342 static const struct rpc_call_ops nfs4_write_ops = {
2343 .rpc_call_done = nfs4_write_done,
2344 .rpc_release = nfs_writedata_release,
2348 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2350 struct rpc_task *task = &data->task;
2351 struct rpc_message msg = {
2352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2353 .rpc_argp = &data->args,
2354 .rpc_resp = &data->res,
2355 .rpc_cred = data->cred,
2357 struct inode *inode = data->inode;
2358 struct nfs_server *server = NFS_SERVER(inode);
2362 if (how & FLUSH_STABLE) {
2363 if (!NFS_I(inode)->ncommit)
2364 stable = NFS_FILE_SYNC;
2366 stable = NFS_DATA_SYNC;
2368 stable = NFS_UNSTABLE;
2369 data->args.stable = stable;
2370 data->args.bitmask = server->attr_bitmask;
2371 data->res.server = server;
2373 data->timestamp = jiffies;
2375 /* Set the initial flags for the task. */
2376 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2378 /* Finalize the task. */
2379 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
2380 rpc_call_setup(task, &msg, 0);
2383 static void nfs4_commit_done(struct rpc_task *task, void *calldata)
2385 struct nfs_write_data *data = calldata;
2386 struct inode *inode = data->inode;
2388 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2389 rpc_restart_call(task);
2392 if (task->tk_status >= 0)
2393 nfs_post_op_update_inode(inode, data->res.fattr);
2394 /* Call back common NFS writeback processing */
2395 nfs_commit_done(task, calldata);
2398 static const struct rpc_call_ops nfs4_commit_ops = {
2399 .rpc_call_done = nfs4_commit_done,
2400 .rpc_release = nfs_commit_release,
2404 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2406 struct rpc_task *task = &data->task;
2407 struct rpc_message msg = {
2408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2409 .rpc_argp = &data->args,
2410 .rpc_resp = &data->res,
2411 .rpc_cred = data->cred,
2413 struct inode *inode = data->inode;
2414 struct nfs_server *server = NFS_SERVER(inode);
2417 data->args.bitmask = server->attr_bitmask;
2418 data->res.server = server;
2420 /* Set the initial flags for the task. */
2421 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2423 /* Finalize the task. */
2424 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
2425 rpc_call_setup(task, &msg, 0);
2429 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2430 * standalone procedure for queueing an asynchronous RENEW.
2432 static void nfs4_renew_done(struct rpc_task *task, void *data)
2434 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2435 unsigned long timestamp = (unsigned long)data;
2437 if (task->tk_status < 0) {
2438 switch (task->tk_status) {
2439 case -NFS4ERR_STALE_CLIENTID:
2440 case -NFS4ERR_EXPIRED:
2441 case -NFS4ERR_CB_PATH_DOWN:
2442 nfs4_schedule_state_recovery(clp);
2446 spin_lock(&clp->cl_lock);
2447 if (time_before(clp->cl_last_renewal,timestamp))
2448 clp->cl_last_renewal = timestamp;
2449 spin_unlock(&clp->cl_lock);
2452 static const struct rpc_call_ops nfs4_renew_ops = {
2453 .rpc_call_done = nfs4_renew_done,
2457 nfs4_proc_async_renew(struct nfs4_client *clp)
2459 struct rpc_message msg = {
2460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2462 .rpc_cred = clp->cl_cred,
2465 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2466 &nfs4_renew_ops, (void *)jiffies);
2470 nfs4_proc_renew(struct nfs4_client *clp)
2472 struct rpc_message msg = {
2473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2475 .rpc_cred = clp->cl_cred,
2477 unsigned long now = jiffies;
2480 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2483 spin_lock(&clp->cl_lock);
2484 if (time_before(clp->cl_last_renewal,now))
2485 clp->cl_last_renewal = now;
2486 spin_unlock(&clp->cl_lock);
2490 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2492 return (server->caps & NFS_CAP_ACLS)
2493 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2494 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2497 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2498 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2501 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2503 static void buf_to_pages(const void *buf, size_t buflen,
2504 struct page **pages, unsigned int *pgbase)
2506 const void *p = buf;
2508 *pgbase = offset_in_page(buf);
2510 while (p < buf + buflen) {
2511 *(pages++) = virt_to_page(p);
2512 p += PAGE_CACHE_SIZE;
2516 struct nfs4_cached_acl {
2522 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2524 struct nfs_inode *nfsi = NFS_I(inode);
2526 spin_lock(&inode->i_lock);
2527 kfree(nfsi->nfs4_acl);
2528 nfsi->nfs4_acl = acl;
2529 spin_unlock(&inode->i_lock);
2532 static void nfs4_zap_acl_attr(struct inode *inode)
2534 nfs4_set_cached_acl(inode, NULL);
2537 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2539 struct nfs_inode *nfsi = NFS_I(inode);
2540 struct nfs4_cached_acl *acl;
2543 spin_lock(&inode->i_lock);
2544 acl = nfsi->nfs4_acl;
2547 if (buf == NULL) /* user is just asking for length */
2549 if (acl->cached == 0)
2551 ret = -ERANGE; /* see getxattr(2) man page */
2552 if (acl->len > buflen)
2554 memcpy(buf, acl->data, acl->len);
2558 spin_unlock(&inode->i_lock);
2562 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2564 struct nfs4_cached_acl *acl;
2566 if (buf && acl_len <= PAGE_SIZE) {
2567 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2571 memcpy(acl->data, buf, acl_len);
2573 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2580 nfs4_set_cached_acl(inode, acl);
2583 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2585 struct page *pages[NFS4ACL_MAXPAGES];
2586 struct nfs_getaclargs args = {
2587 .fh = NFS_FH(inode),
2591 size_t resp_len = buflen;
2593 struct rpc_message msg = {
2594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2596 .rpc_resp = &resp_len,
2598 struct page *localpage = NULL;
2601 if (buflen < PAGE_SIZE) {
2602 /* As long as we're doing a round trip to the server anyway,
2603 * let's be prepared for a page of acl data. */
2604 localpage = alloc_page(GFP_KERNEL);
2605 resp_buf = page_address(localpage);
2606 if (localpage == NULL)
2608 args.acl_pages[0] = localpage;
2609 args.acl_pgbase = 0;
2610 resp_len = args.acl_len = PAGE_SIZE;
2613 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2615 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2618 if (resp_len > args.acl_len)
2619 nfs4_write_cached_acl(inode, NULL, resp_len);
2621 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2624 if (resp_len > buflen)
2627 memcpy(buf, resp_buf, resp_len);
2632 __free_page(localpage);
2636 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2638 struct nfs_server *server = NFS_SERVER(inode);
2641 if (!nfs4_server_supports_acls(server))
2643 ret = nfs_revalidate_inode(server, inode);
2646 ret = nfs4_read_cached_acl(inode, buf, buflen);
2649 return nfs4_get_acl_uncached(inode, buf, buflen);
2652 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2654 struct nfs_server *server = NFS_SERVER(inode);
2655 struct page *pages[NFS4ACL_MAXPAGES];
2656 struct nfs_setaclargs arg = {
2657 .fh = NFS_FH(inode),
2661 struct rpc_message msg = {
2662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2668 if (!nfs4_server_supports_acls(server))
2670 nfs_inode_return_delegation(inode);
2671 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2672 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2674 nfs4_write_cached_acl(inode, buf, buflen);
2679 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2681 struct nfs4_client *clp = server->nfs4_state;
2683 if (!clp || task->tk_status >= 0)
2685 switch(task->tk_status) {
2686 case -NFS4ERR_STALE_CLIENTID:
2687 case -NFS4ERR_STALE_STATEID:
2688 case -NFS4ERR_EXPIRED:
2689 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2690 nfs4_schedule_state_recovery(clp);
2691 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2692 rpc_wake_up_task(task);
2693 task->tk_status = 0;
2695 case -NFS4ERR_GRACE:
2696 case -NFS4ERR_DELAY:
2697 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2698 task->tk_status = 0;
2700 case -NFS4ERR_OLD_STATEID:
2701 task->tk_status = 0;
2704 task->tk_status = nfs4_map_errors(task->tk_status);
2708 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2712 int interruptible, res = 0;
2716 rpc_clnt_sigmask(clnt, &oldset);
2717 interruptible = TASK_UNINTERRUPTIBLE;
2719 interruptible = TASK_INTERRUPTIBLE;
2720 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2721 nfs4_schedule_state_recovery(clp);
2722 if (clnt->cl_intr && signalled())
2724 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2726 finish_wait(&clp->cl_waitq, &wait);
2727 rpc_clnt_sigunmask(clnt, &oldset);
2731 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2739 *timeout = NFS4_POLL_RETRY_MIN;
2740 if (*timeout > NFS4_POLL_RETRY_MAX)
2741 *timeout = NFS4_POLL_RETRY_MAX;
2742 rpc_clnt_sigmask(clnt, &oldset);
2743 if (clnt->cl_intr) {
2744 schedule_timeout_interruptible(*timeout);
2748 schedule_timeout_uninterruptible(*timeout);
2749 rpc_clnt_sigunmask(clnt, &oldset);
2754 /* This is the error handling routine for processes that are allowed
2757 int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2759 struct nfs4_client *clp = server->nfs4_state;
2760 int ret = errorcode;
2762 exception->retry = 0;
2766 case -NFS4ERR_STALE_CLIENTID:
2767 case -NFS4ERR_STALE_STATEID:
2768 case -NFS4ERR_EXPIRED:
2769 ret = nfs4_wait_clnt_recover(server->client, clp);
2771 exception->retry = 1;
2773 case -NFS4ERR_GRACE:
2774 case -NFS4ERR_DELAY:
2775 ret = nfs4_delay(server->client, &exception->timeout);
2778 case -NFS4ERR_OLD_STATEID:
2779 exception->retry = 1;
2781 /* We failed to handle the error */
2782 return nfs4_map_errors(ret);
2785 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2787 nfs4_verifier sc_verifier;
2788 struct nfs4_setclientid setclientid = {
2789 .sc_verifier = &sc_verifier,
2792 struct rpc_message msg = {
2793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2794 .rpc_argp = &setclientid,
2796 .rpc_cred = clp->cl_cred,
2802 p = (u32*)sc_verifier.data;
2803 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2804 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2807 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2808 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2809 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2810 clp->cl_cred->cr_ops->cr_name,
2811 clp->cl_id_uniquifier);
2812 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2813 sizeof(setclientid.sc_netid), "tcp");
2814 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2815 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2816 clp->cl_ipaddr, port >> 8, port & 255);
2818 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2819 if (status != -NFS4ERR_CLID_INUSE)
2824 ssleep(clp->cl_lease_time + 1);
2826 if (++clp->cl_id_uniquifier == 0)
2833 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2835 struct nfs_fsinfo fsinfo;
2836 struct rpc_message msg = {
2837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2839 .rpc_resp = &fsinfo,
2840 .rpc_cred = clp->cl_cred,
2846 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2848 spin_lock(&clp->cl_lock);
2849 clp->cl_lease_time = fsinfo.lease_time * HZ;
2850 clp->cl_last_renewal = now;
2851 spin_unlock(&clp->cl_lock);
2856 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2858 struct nfs4_delegreturnargs args = {
2859 .fhandle = NFS_FH(inode),
2862 struct rpc_message msg = {
2863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2868 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2871 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2873 struct nfs_server *server = NFS_SERVER(inode);
2874 struct nfs4_exception exception = { };
2877 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2879 case -NFS4ERR_STALE_STATEID:
2880 case -NFS4ERR_EXPIRED:
2881 nfs4_schedule_state_recovery(server->nfs4_state);
2885 err = nfs4_handle_exception(server, err, &exception);
2886 } while (exception.retry);
2890 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2891 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2894 * sleep, with exponential backoff, and retry the LOCK operation.
2896 static unsigned long
2897 nfs4_set_lock_task_retry(unsigned long timeout)
2899 schedule_timeout_interruptible(timeout);
2901 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2902 return NFS4_LOCK_MAXTIMEOUT;
2907 nfs4_lck_type(int cmd, struct file_lock *request)
2910 switch (request->fl_type) {
2912 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2914 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2916 return NFS4_WRITE_LT;
2922 static inline uint64_t
2923 nfs4_lck_length(struct file_lock *request)
2925 if (request->fl_end == OFFSET_MAX)
2926 return ~(uint64_t)0;
2927 return request->fl_end - request->fl_start + 1;
2930 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2932 struct inode *inode = state->inode;
2933 struct nfs_server *server = NFS_SERVER(inode);
2934 struct nfs4_client *clp = server->nfs4_state;
2935 struct nfs_lockargs arg = {
2936 .fh = NFS_FH(inode),
2937 .type = nfs4_lck_type(cmd, request),
2938 .offset = request->fl_start,
2939 .length = nfs4_lck_length(request),
2941 struct nfs_lockres res = {
2944 struct rpc_message msg = {
2945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2948 .rpc_cred = state->owner->so_cred,
2950 struct nfs_lowner nlo;
2951 struct nfs4_lock_state *lsp;
2954 down_read(&clp->cl_sem);
2955 nlo.clientid = clp->cl_clientid;
2956 status = nfs4_set_lock_state(state, request);
2959 lsp = request->fl_u.nfs4_fl.owner;
2960 nlo.id = lsp->ls_id;
2962 status = rpc_call_sync(server->client, &msg, 0);
2964 request->fl_type = F_UNLCK;
2965 } else if (status == -NFS4ERR_DENIED) {
2966 int64_t len, start, end;
2967 start = res.u.denied.offset;
2968 len = res.u.denied.length;
2969 end = start + len - 1;
2970 if (end < 0 || len == 0)
2971 request->fl_end = OFFSET_MAX;
2973 request->fl_end = (loff_t)end;
2974 request->fl_start = (loff_t)start;
2975 request->fl_type = F_WRLCK;
2976 if (res.u.denied.type & 1)
2977 request->fl_type = F_RDLCK;
2978 request->fl_pid = 0;
2982 up_read(&clp->cl_sem);
2986 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2988 struct nfs4_exception exception = { };
2992 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2993 _nfs4_proc_getlk(state, cmd, request),
2995 } while (exception.retry);
2999 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3002 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3004 res = posix_lock_file_wait(file, fl);
3007 res = flock_lock_file_wait(file, fl);
3013 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
3018 struct nfs4_unlockdata {
3019 struct nfs_lockargs arg;
3020 struct nfs_locku_opargs luargs;
3021 struct nfs_lockres res;
3022 struct nfs4_lock_state *lsp;
3023 struct nfs_open_context *ctx;
3026 static void nfs4_locku_release_calldata(void *data)
3028 struct nfs4_unlockdata *calldata = data;
3029 nfs_free_seqid(calldata->luargs.seqid);
3030 nfs4_put_lock_state(calldata->lsp);
3031 put_nfs_open_context(calldata->ctx);
3035 static void nfs4_locku_done(struct rpc_task *task, void *data)
3037 struct nfs4_unlockdata *calldata = data;
3039 if (RPC_ASSASSINATED(task))
3041 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
3042 switch (task->tk_status) {
3044 memcpy(calldata->lsp->ls_stateid.data,
3045 calldata->res.u.stateid.data,
3046 sizeof(calldata->lsp->ls_stateid.data));
3048 case -NFS4ERR_STALE_STATEID:
3049 case -NFS4ERR_EXPIRED:
3050 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
3053 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
3054 rpc_restart_call(task);
3059 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3061 struct nfs4_unlockdata *calldata = data;
3062 struct rpc_message msg = {
3063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3064 .rpc_argp = &calldata->arg,
3065 .rpc_resp = &calldata->res,
3066 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
3070 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
3073 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3074 /* Note: exit _without_ running nfs4_locku_done */
3075 task->tk_action = NULL;
3078 rpc_call_setup(task, &msg, 0);
3081 static const struct rpc_call_ops nfs4_locku_ops = {
3082 .rpc_call_prepare = nfs4_locku_prepare,
3083 .rpc_call_done = nfs4_locku_done,
3084 .rpc_release = nfs4_locku_release_calldata,
3087 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3089 struct nfs4_unlockdata *calldata;
3090 struct inode *inode = state->inode;
3091 struct nfs_server *server = NFS_SERVER(inode);
3092 struct nfs4_lock_state *lsp;
3093 struct rpc_task *task;
3096 /* Is this a delegated lock? */
3097 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3100 status = nfs4_set_lock_state(state, request);
3103 lsp = request->fl_u.nfs4_fl.owner;
3104 /* We might have lost the locks! */
3105 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
3108 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
3109 if (calldata == NULL)
3111 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3112 if (calldata->luargs.seqid == NULL) {
3116 calldata->luargs.stateid = &lsp->ls_stateid;
3117 calldata->arg.fh = NFS_FH(inode);
3118 calldata->arg.type = nfs4_lck_type(cmd, request);
3119 calldata->arg.offset = request->fl_start;
3120 calldata->arg.length = nfs4_lck_length(request);
3121 calldata->arg.u.locku = &calldata->luargs;
3122 calldata->res.server = server;
3123 calldata->lsp = lsp;
3124 atomic_inc(&lsp->ls_count);
3126 /* Ensure we don't close file until we're done freeing locks! */
3127 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
3129 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_locku_ops, calldata);
3130 if (!IS_ERR(task)) {
3131 status = nfs4_wait_for_completion_rpc_task(task);
3132 rpc_release_task(task);
3134 status = PTR_ERR(task);
3135 nfs4_locku_release_calldata(calldata);
3138 do_vfs_lock(request->fl_file, request);
3142 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
3144 struct inode *inode = state->inode;
3145 struct nfs_server *server = NFS_SERVER(inode);
3146 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
3147 struct nfs_lock_opargs largs = {
3148 .lock_stateid = &lsp->ls_stateid,
3149 .open_stateid = &state->stateid,
3151 .clientid = server->nfs4_state->cl_clientid,
3156 struct nfs_lockargs arg = {
3157 .fh = NFS_FH(inode),
3158 .type = nfs4_lck_type(cmd, request),
3159 .offset = request->fl_start,
3160 .length = nfs4_lck_length(request),
3165 struct nfs_lockres res = {
3168 struct rpc_message msg = {
3169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3172 .rpc_cred = state->owner->so_cred,
3174 int status = -ENOMEM;
3176 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3177 if (largs.lock_seqid == NULL)
3179 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
3180 struct nfs4_state_owner *owner = state->owner;
3182 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
3183 if (largs.open_seqid == NULL)
3185 largs.new_lock_owner = 1;
3186 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3187 /* increment open seqid on success, and seqid mutating errors */
3188 if (largs.new_lock_owner != 0) {
3189 nfs_increment_open_seqid(status, largs.open_seqid);
3191 nfs_confirm_seqid(&lsp->ls_seqid, 0);
3193 nfs_free_seqid(largs.open_seqid);
3195 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3196 /* increment lock seqid on success, and seqid mutating errors*/
3197 nfs_increment_lock_seqid(status, largs.lock_seqid);
3198 /* save the returned stateid. */
3200 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
3201 sizeof(lsp->ls_stateid.data));
3202 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3203 } else if (status == -NFS4ERR_DENIED)
3206 nfs_free_seqid(largs.lock_seqid);
3210 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3212 struct nfs_server *server = NFS_SERVER(state->inode);
3213 struct nfs4_exception exception = { };
3216 /* Cache the lock if possible... */
3217 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3220 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3221 if (err != -NFS4ERR_DELAY)
3223 nfs4_handle_exception(server, err, &exception);
3224 } while (exception.retry);
3228 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3230 struct nfs_server *server = NFS_SERVER(state->inode);
3231 struct nfs4_exception exception = { };
3234 err = nfs4_set_lock_state(state, request);
3238 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3239 if (err != -NFS4ERR_DELAY)
3241 nfs4_handle_exception(server, err, &exception);
3242 } while (exception.retry);
3246 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3248 struct nfs4_client *clp = state->owner->so_client;
3251 /* Is this a delegated open? */
3252 if (NFS_I(state->inode)->delegation_state != 0) {
3253 /* Yes: cache locks! */
3254 status = do_vfs_lock(request->fl_file, request);
3255 /* ...but avoid races with delegation recall... */
3256 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
3259 down_read(&clp->cl_sem);
3260 status = nfs4_set_lock_state(state, request);
3263 status = _nfs4_do_setlk(state, cmd, request, 0);
3266 /* Note: we always want to sleep here! */
3267 request->fl_flags |= FL_SLEEP;
3268 if (do_vfs_lock(request->fl_file, request) < 0)
3269 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3271 up_read(&clp->cl_sem);
3275 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3277 struct nfs4_exception exception = { };
3281 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3282 _nfs4_proc_setlk(state, cmd, request),
3284 } while (exception.retry);
3289 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3291 struct nfs_open_context *ctx;
3292 struct nfs4_state *state;
3293 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3296 /* verify open state */
3297 ctx = (struct nfs_open_context *)filp->private_data;
3300 if (request->fl_start < 0 || request->fl_end < 0)
3304 return nfs4_proc_getlk(state, F_GETLK, request);
3306 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3309 if (request->fl_type == F_UNLCK)
3310 return nfs4_proc_unlck(state, cmd, request);
3313 status = nfs4_proc_setlk(state, cmd, request);
3314 if ((status != -EAGAIN) || IS_SETLK(cmd))
3316 timeout = nfs4_set_lock_task_retry(timeout);
3317 status = -ERESTARTSYS;
3320 } while(status < 0);
3324 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3326 struct nfs_server *server = NFS_SERVER(state->inode);
3327 struct nfs4_exception exception = { };
3330 err = nfs4_set_lock_state(state, fl);
3334 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3335 if (err != -NFS4ERR_DELAY)
3337 err = nfs4_handle_exception(server, err, &exception);
3338 } while (exception.retry);
3343 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3345 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3346 size_t buflen, int flags)
3348 struct inode *inode = dentry->d_inode;
3350 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3353 if (!S_ISREG(inode->i_mode) &&
3354 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3357 return nfs4_proc_set_acl(inode, buf, buflen);
3360 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3361 * and that's what we'll do for e.g. user attributes that haven't been set.
3362 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3363 * attributes in kernel-managed attribute namespaces. */
3364 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3367 struct inode *inode = dentry->d_inode;
3369 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3372 return nfs4_proc_get_acl(inode, buf, buflen);
3375 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3377 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3379 if (buf && buflen < len)
3382 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3386 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3387 .recover_open = nfs4_open_reclaim,
3388 .recover_lock = nfs4_lock_reclaim,
3391 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3392 .recover_open = nfs4_open_expired,
3393 .recover_lock = nfs4_lock_expired,
3396 static struct inode_operations nfs4_file_inode_operations = {
3397 .permission = nfs_permission,
3398 .getattr = nfs_getattr,
3399 .setattr = nfs_setattr,
3400 .getxattr = nfs4_getxattr,
3401 .setxattr = nfs4_setxattr,
3402 .listxattr = nfs4_listxattr,
3405 struct nfs_rpc_ops nfs_v4_clientops = {
3406 .version = 4, /* protocol version */
3407 .dentry_ops = &nfs4_dentry_operations,
3408 .dir_inode_ops = &nfs4_dir_inode_operations,
3409 .file_inode_ops = &nfs4_file_inode_operations,
3410 .getroot = nfs4_proc_get_root,
3411 .getattr = nfs4_proc_getattr,
3412 .setattr = nfs4_proc_setattr,
3413 .lookup = nfs4_proc_lookup,
3414 .access = nfs4_proc_access,
3415 .readlink = nfs4_proc_readlink,
3416 .read = nfs4_proc_read,
3417 .write = nfs4_proc_write,
3418 .commit = nfs4_proc_commit,
3419 .create = nfs4_proc_create,
3420 .remove = nfs4_proc_remove,
3421 .unlink_setup = nfs4_proc_unlink_setup,
3422 .unlink_done = nfs4_proc_unlink_done,
3423 .rename = nfs4_proc_rename,
3424 .link = nfs4_proc_link,
3425 .symlink = nfs4_proc_symlink,
3426 .mkdir = nfs4_proc_mkdir,
3427 .rmdir = nfs4_proc_remove,
3428 .readdir = nfs4_proc_readdir,
3429 .mknod = nfs4_proc_mknod,
3430 .statfs = nfs4_proc_statfs,
3431 .fsinfo = nfs4_proc_fsinfo,
3432 .pathconf = nfs4_proc_pathconf,
3433 .decode_dirent = nfs4_decode_dirent,
3434 .read_setup = nfs4_proc_read_setup,
3435 .write_setup = nfs4_proc_write_setup,
3436 .commit_setup = nfs4_proc_commit_setup,
3437 .file_open = nfs_open,
3438 .file_release = nfs_release,
3439 .lock = nfs4_proc_lock,
3440 .clear_acl_cache = nfs4_zap_acl_attr,