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 inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
302 switch (open_flags) {
309 case FMODE_READ|FMODE_WRITE:
314 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
316 struct inode *inode = state->inode;
318 open_flags &= (FMODE_READ|FMODE_WRITE);
319 /* Protect against nfs4_find_state_byowner() */
320 spin_lock(&state->owner->so_lock);
321 spin_lock(&inode->i_lock);
322 memcpy(&state->stateid, stateid, sizeof(state->stateid));
323 update_open_stateflags(state, open_flags);
324 nfs4_state_set_mode_locked(state, state->state | open_flags);
325 spin_unlock(&inode->i_lock);
326 spin_unlock(&state->owner->so_lock);
329 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
332 struct nfs4_state *state = NULL;
334 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
336 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
339 state = nfs4_get_open_state(inode, data->owner);
342 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
351 * reclaim state on the server after a reboot.
353 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
355 struct inode *inode = state->inode;
356 struct nfs_server *server = NFS_SERVER(inode);
357 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
358 struct nfs_openargs o_arg = {
361 .open_flags = state->state,
362 .clientid = server->nfs4_state->cl_clientid,
363 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
364 .bitmask = server->attr_bitmask,
366 struct nfs_openres o_res = {
367 .server = server, /* Grrr */
369 struct rpc_message msg = {
370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
373 .rpc_cred = sp->so_cred,
377 if (delegation != NULL) {
378 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
379 memcpy(&state->stateid, &delegation->stateid,
380 sizeof(state->stateid));
381 set_bit(NFS_DELEGATED_STATE, &state->flags);
384 o_arg.u.delegation_type = delegation->type;
386 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
387 if (o_arg.seqid == NULL)
389 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
390 /* Confirm the sequence as being established */
391 nfs_confirm_seqid(&sp->so_seqid, status);
392 nfs_increment_open_seqid(status, o_arg.seqid);
394 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
395 if (o_res.delegation_type != 0) {
396 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
397 /* Did the server issue an immediate delegation recall? */
399 nfs_async_inode_return_delegation(inode, &o_res.stateid);
402 nfs_free_seqid(o_arg.seqid);
403 clear_bit(NFS_DELEGATED_STATE, &state->flags);
404 /* Ensure we update the inode attributes */
409 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
411 struct nfs_server *server = NFS_SERVER(state->inode);
412 struct nfs4_exception exception = { };
415 err = _nfs4_open_reclaim(sp, state);
416 if (err != -NFS4ERR_DELAY)
418 nfs4_handle_exception(server, err, &exception);
419 } while (exception.retry);
423 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
425 struct nfs4_state_owner *sp = state->owner;
426 struct inode *inode = dentry->d_inode;
427 struct nfs_server *server = NFS_SERVER(inode);
428 struct rpc_message msg = {
429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
430 .rpc_cred = sp->so_cred,
432 struct nfs4_opendata *opendata;
435 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
437 if (state->state == 0)
439 opendata = nfs4_opendata_alloc(dentry, sp, state->state, NULL);
441 if (opendata == NULL)
443 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
444 msg.rpc_argp = &opendata->o_arg;
445 msg.rpc_resp = &opendata->o_res;
446 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
447 sizeof(opendata->o_arg.u.delegation.data));
448 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
449 nfs_increment_open_seqid(status, opendata->o_arg.seqid);
452 if(opendata->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
453 status = _nfs4_proc_open_confirm(opendata);
457 nfs_confirm_seqid(&sp->so_seqid, 0);
459 memcpy(state->stateid.data, opendata->o_res.stateid.data,
460 sizeof(state->stateid.data));
461 clear_bit(NFS_DELEGATED_STATE, &state->flags);
464 nfs4_opendata_free(opendata);
469 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
471 struct nfs4_exception exception = { };
472 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
475 err = _nfs4_open_delegation_recall(dentry, state);
479 case -NFS4ERR_STALE_CLIENTID:
480 case -NFS4ERR_STALE_STATEID:
481 case -NFS4ERR_EXPIRED:
482 /* Don't recall a delegation if it was lost */
483 nfs4_schedule_state_recovery(server->nfs4_state);
486 err = nfs4_handle_exception(server, err, &exception);
487 } while (exception.retry);
491 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
493 struct nfs4_opendata *data = calldata;
494 struct rpc_message msg = {
495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
496 .rpc_argp = &data->c_arg,
497 .rpc_resp = &data->c_res,
498 .rpc_cred = data->owner->so_cred,
500 rpc_call_setup(task, &msg, 0);
503 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
505 struct nfs4_opendata *data = calldata;
507 data->rpc_status = task->tk_status;
508 if (RPC_ASSASSINATED(task))
510 if (data->rpc_status == 0)
511 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
512 sizeof(data->o_res.stateid.data));
513 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
514 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
517 static void nfs4_open_confirm_release(void *calldata)
519 struct nfs4_opendata *data = calldata;
520 struct nfs4_state *state = NULL;
522 /* If this request hasn't been cancelled, do nothing */
523 if (data->cancelled == 0)
525 /* In case of error, no cleanup! */
526 if (data->rpc_status != 0)
528 nfs_confirm_seqid(&data->owner->so_seqid, 0);
529 state = nfs4_opendata_to_nfs4_state(data);
531 nfs4_close_state(state, data->o_arg.open_flags);
533 nfs4_opendata_free(data);
536 static const struct rpc_call_ops nfs4_open_confirm_ops = {
537 .rpc_call_prepare = nfs4_open_confirm_prepare,
538 .rpc_call_done = nfs4_open_confirm_done,
539 .rpc_release = nfs4_open_confirm_release,
543 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
545 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
547 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
548 struct rpc_task *task;
551 atomic_inc(&data->count);
552 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
554 nfs4_opendata_free(data);
555 return PTR_ERR(task);
557 status = nfs4_wait_for_completion_rpc_task(task);
562 status = data->rpc_status;
563 rpc_release_task(task);
567 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
569 struct nfs4_opendata *data = calldata;
570 struct nfs4_state_owner *sp = data->owner;
571 struct rpc_message msg = {
572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
573 .rpc_argp = &data->o_arg,
574 .rpc_resp = &data->o_res,
575 .rpc_cred = sp->so_cred,
578 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
580 /* Update sequence id. */
581 data->o_arg.id = sp->so_id;
582 data->o_arg.clientid = sp->so_client->cl_clientid;
583 rpc_call_setup(task, &msg, 0);
586 static void nfs4_open_done(struct rpc_task *task, void *calldata)
588 struct nfs4_opendata *data = calldata;
590 data->rpc_status = task->tk_status;
591 if (RPC_ASSASSINATED(task))
593 if (task->tk_status == 0) {
594 switch (data->o_res.f_attr->mode & S_IFMT) {
598 data->rpc_status = -ELOOP;
601 data->rpc_status = -EISDIR;
604 data->rpc_status = -ENOTDIR;
607 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
610 static void nfs4_open_release(void *calldata)
612 struct nfs4_opendata *data = calldata;
613 struct nfs4_state *state = NULL;
615 /* If this request hasn't been cancelled, do nothing */
616 if (data->cancelled == 0)
618 /* In case of error, no cleanup! */
619 if (data->rpc_status != 0)
621 /* In case we need an open_confirm, no cleanup! */
622 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
624 nfs_confirm_seqid(&data->owner->so_seqid, 0);
625 state = nfs4_opendata_to_nfs4_state(data);
627 nfs4_close_state(state, data->o_arg.open_flags);
629 nfs4_opendata_free(data);
632 static const struct rpc_call_ops nfs4_open_ops = {
633 .rpc_call_prepare = nfs4_open_prepare,
634 .rpc_call_done = nfs4_open_done,
635 .rpc_release = nfs4_open_release,
639 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
641 static int _nfs4_proc_open(struct nfs4_opendata *data)
643 struct inode *dir = data->dir->d_inode;
644 struct nfs_server *server = NFS_SERVER(dir);
645 struct nfs_openargs *o_arg = &data->o_arg;
646 struct nfs_openres *o_res = &data->o_res;
647 struct rpc_task *task;
650 atomic_inc(&data->count);
651 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
653 nfs4_opendata_free(data);
654 return PTR_ERR(task);
656 status = nfs4_wait_for_completion_rpc_task(task);
661 status = data->rpc_status;
662 rpc_release_task(task);
666 if (o_arg->open_flags & O_CREAT) {
667 update_changeattr(dir, &o_res->cinfo);
668 nfs_post_op_update_inode(dir, o_res->dir_attr);
670 nfs_refresh_inode(dir, o_res->dir_attr);
671 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
672 status = _nfs4_proc_open_confirm(data);
676 nfs_confirm_seqid(&data->owner->so_seqid, 0);
677 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
678 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
682 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
684 struct nfs_access_entry cache;
688 if (openflags & FMODE_READ)
690 if (openflags & FMODE_WRITE)
692 status = nfs_access_get_cached(inode, cred, &cache);
696 /* Be clever: ask server to check for all possible rights */
697 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
699 cache.jiffies = jiffies;
700 status = _nfs4_proc_access(inode, &cache);
703 nfs_access_add_cache(inode, &cache);
705 if ((cache.mask & mask) == mask)
712 * reclaim state on the server after a network partition.
713 * Assumes caller holds the appropriate lock
715 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
717 struct dentry *parent = dget_parent(dentry);
718 struct inode *inode = state->inode;
719 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
720 struct nfs4_opendata *opendata;
721 struct nfs4_state *newstate;
722 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
725 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
726 status = _nfs4_do_access(inode, sp->so_cred, openflags);
729 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
730 set_bit(NFS_DELEGATED_STATE, &state->flags);
734 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
735 if (opendata == NULL)
737 status = _nfs4_proc_open(opendata);
740 newstate = nfs4_opendata_to_nfs4_state(opendata);
741 if (newstate != state)
743 if (opendata->o_res.delegation_type != 0) {
744 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
745 nfs_inode_set_delegation(inode, sp->so_cred,
748 nfs_inode_reclaim_delegation(inode, sp->so_cred,
752 nfs4_close_state(newstate, openflags);
754 nfs4_opendata_free(opendata);
755 clear_bit(NFS_DELEGATED_STATE, &state->flags);
761 /* Invalidate the state owner so we don't ever use it again */
762 nfs4_drop_state_owner(sp);
764 /* Should we be trying to close that stateid? */
765 goto out_close_state;
768 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
770 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
771 struct nfs4_exception exception = { };
775 err = _nfs4_open_expired(sp, state, dentry);
776 if (err == -NFS4ERR_DELAY)
777 nfs4_handle_exception(server, err, &exception);
778 } while (exception.retry);
782 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
784 struct nfs_inode *nfsi = NFS_I(state->inode);
785 struct nfs_open_context *ctx;
788 spin_lock(&state->inode->i_lock);
789 list_for_each_entry(ctx, &nfsi->open_files, list) {
790 if (ctx->state != state)
792 get_nfs_open_context(ctx);
793 spin_unlock(&state->inode->i_lock);
794 status = nfs4_do_open_expired(sp, state, ctx->dentry);
795 put_nfs_open_context(ctx);
798 spin_unlock(&state->inode->i_lock);
803 * Returns a referenced nfs4_state if there is an open delegation on the file
805 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
807 struct nfs_delegation *delegation;
808 struct nfs_server *server = NFS_SERVER(inode);
809 struct nfs4_client *clp = server->nfs4_state;
810 struct nfs_inode *nfsi = NFS_I(inode);
811 struct nfs4_state_owner *sp = NULL;
812 struct nfs4_state *state = NULL;
813 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
816 /* Protect against reboot recovery - NOTE ORDER! */
817 down_read(&clp->cl_sem);
818 /* Protect against delegation recall */
819 down_read(&nfsi->rwsem);
820 delegation = NFS_I(inode)->delegation;
822 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
825 if (!(sp = nfs4_get_state_owner(server, cred))) {
826 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
829 state = nfs4_get_open_state(inode, sp);
834 if ((state->state & open_flags) == open_flags) {
835 spin_lock(&inode->i_lock);
836 update_open_stateflags(state, open_flags);
837 spin_unlock(&inode->i_lock);
839 } else if (state->state != 0)
843 err = _nfs4_do_access(inode, cred, open_flags);
847 set_bit(NFS_DELEGATED_STATE, &state->flags);
848 update_open_stateid(state, &delegation->stateid, open_flags);
850 nfs4_put_state_owner(sp);
851 up_read(&nfsi->rwsem);
852 up_read(&clp->cl_sem);
858 nfs4_put_open_state(state);
859 nfs4_put_state_owner(sp);
861 up_read(&nfsi->rwsem);
862 up_read(&clp->cl_sem);
864 nfs_inode_return_delegation(inode);
868 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
870 struct nfs4_exception exception = { };
871 struct nfs4_state *res;
875 err = _nfs4_open_delegated(inode, flags, cred, &res);
878 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
880 } while (exception.retry);
885 * Returns a referenced nfs4_state
887 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
889 struct nfs4_state_owner *sp;
890 struct nfs4_state *state = NULL;
891 struct nfs_server *server = NFS_SERVER(dir);
892 struct nfs4_client *clp = server->nfs4_state;
893 struct nfs4_opendata *opendata;
896 /* Protect against reboot recovery conflicts */
897 down_read(&clp->cl_sem);
899 if (!(sp = nfs4_get_state_owner(server, cred))) {
900 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
903 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
904 if (opendata == NULL)
905 goto err_put_state_owner;
907 status = _nfs4_proc_open(opendata);
909 goto err_opendata_free;
912 state = nfs4_opendata_to_nfs4_state(opendata);
914 goto err_opendata_free;
915 if (opendata->o_res.delegation_type != 0)
916 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
917 nfs4_opendata_free(opendata);
918 nfs4_put_state_owner(sp);
919 up_read(&clp->cl_sem);
923 nfs4_opendata_free(opendata);
925 nfs4_put_state_owner(sp);
927 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
928 up_read(&clp->cl_sem);
934 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
936 struct nfs4_exception exception = { };
937 struct nfs4_state *res;
941 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
944 /* NOTE: BAD_SEQID means the server and client disagree about the
945 * book-keeping w.r.t. state-changing operations
946 * (OPEN/CLOSE/LOCK/LOCKU...)
947 * It is actually a sign of a bug on the client or on the server.
949 * If we receive a BAD_SEQID error in the particular case of
950 * doing an OPEN, we assume that nfs_increment_open_seqid() will
951 * have unhashed the old state_owner for us, and that we can
952 * therefore safely retry using a new one. We should still warn
955 if (status == -NFS4ERR_BAD_SEQID) {
956 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
961 * BAD_STATEID on OPEN means that the server cancelled our
962 * state before it received the OPEN_CONFIRM.
963 * Recover by retrying the request as per the discussion
964 * on Page 181 of RFC3530.
966 if (status == -NFS4ERR_BAD_STATEID) {
970 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
971 status, &exception));
972 } while (exception.retry);
976 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
977 struct nfs_fh *fhandle, struct iattr *sattr,
978 struct nfs4_state *state)
980 struct nfs_setattrargs arg = {
984 .bitmask = server->attr_bitmask,
986 struct nfs_setattrres res = {
990 struct rpc_message msg = {
991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
997 nfs_fattr_init(fattr);
1000 msg.rpc_cred = state->owner->so_cred;
1001 nfs4_copy_stateid(&arg.stateid, state, current->files);
1003 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1005 status = rpc_call_sync(server->client, &msg, 0);
1009 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1010 struct nfs_fh *fhandle, struct iattr *sattr,
1011 struct nfs4_state *state)
1013 struct nfs4_exception exception = { };
1016 err = nfs4_handle_exception(server,
1017 _nfs4_do_setattr(server, fattr, fhandle, sattr,
1020 } while (exception.retry);
1024 struct nfs4_closedata {
1025 struct inode *inode;
1026 struct nfs4_state *state;
1027 struct nfs_closeargs arg;
1028 struct nfs_closeres res;
1029 struct nfs_fattr fattr;
1032 static void nfs4_free_closedata(void *data)
1034 struct nfs4_closedata *calldata = data;
1035 struct nfs4_state_owner *sp = calldata->state->owner;
1037 nfs4_put_open_state(calldata->state);
1038 nfs_free_seqid(calldata->arg.seqid);
1039 nfs4_put_state_owner(sp);
1043 static void nfs4_close_done(struct rpc_task *task, void *data)
1045 struct nfs4_closedata *calldata = data;
1046 struct nfs4_state *state = calldata->state;
1047 struct nfs_server *server = NFS_SERVER(calldata->inode);
1049 if (RPC_ASSASSINATED(task))
1051 /* hmm. we are done with the inode, and in the process of freeing
1052 * the state_owner. we keep this around to process errors
1054 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1055 switch (task->tk_status) {
1057 memcpy(&state->stateid, &calldata->res.stateid,
1058 sizeof(state->stateid));
1060 case -NFS4ERR_STALE_STATEID:
1061 case -NFS4ERR_EXPIRED:
1062 nfs4_schedule_state_recovery(server->nfs4_state);
1065 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1066 rpc_restart_call(task);
1070 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1073 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1075 struct nfs4_closedata *calldata = data;
1076 struct nfs4_state *state = calldata->state;
1077 struct rpc_message msg = {
1078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1079 .rpc_argp = &calldata->arg,
1080 .rpc_resp = &calldata->res,
1081 .rpc_cred = state->owner->so_cred,
1083 int mode = 0, old_mode;
1085 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1087 /* Recalculate the new open mode in case someone reopened the file
1088 * while we were waiting in line to be scheduled.
1090 spin_lock(&state->owner->so_lock);
1091 spin_lock(&calldata->inode->i_lock);
1092 mode = old_mode = state->state;
1093 if (state->n_rdwr == 0) {
1094 if (state->n_rdonly == 0)
1095 mode &= ~FMODE_READ;
1096 if (state->n_wronly == 0)
1097 mode &= ~FMODE_WRITE;
1099 nfs4_state_set_mode_locked(state, mode);
1100 spin_unlock(&calldata->inode->i_lock);
1101 spin_unlock(&state->owner->so_lock);
1102 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1103 /* Note: exit _without_ calling nfs4_close_done */
1104 task->tk_action = NULL;
1107 nfs_fattr_init(calldata->res.fattr);
1109 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1110 calldata->arg.open_flags = mode;
1111 rpc_call_setup(task, &msg, 0);
1114 static const struct rpc_call_ops nfs4_close_ops = {
1115 .rpc_call_prepare = nfs4_close_prepare,
1116 .rpc_call_done = nfs4_close_done,
1117 .rpc_release = nfs4_free_closedata,
1121 * It is possible for data to be read/written from a mem-mapped file
1122 * after the sys_close call (which hits the vfs layer as a flush).
1123 * This means that we can't safely call nfsv4 close on a file until
1124 * the inode is cleared. This in turn means that we are not good
1125 * NFSv4 citizens - we do not indicate to the server to update the file's
1126 * share state even when we are done with one of the three share
1127 * stateid's in the inode.
1129 * NOTE: Caller must be holding the sp->so_owner semaphore!
1131 int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
1133 struct nfs_server *server = NFS_SERVER(inode);
1134 struct nfs4_closedata *calldata;
1135 int status = -ENOMEM;
1137 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1138 if (calldata == NULL)
1140 calldata->inode = inode;
1141 calldata->state = state;
1142 calldata->arg.fh = NFS_FH(inode);
1143 calldata->arg.stateid = &state->stateid;
1144 /* Serialization for the sequence id */
1145 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1146 if (calldata->arg.seqid == NULL)
1147 goto out_free_calldata;
1148 calldata->arg.bitmask = server->attr_bitmask;
1149 calldata->res.fattr = &calldata->fattr;
1150 calldata->res.server = server;
1152 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
1156 nfs_free_seqid(calldata->arg.seqid);
1163 static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1167 filp = lookup_instantiate_filp(nd, dentry, NULL);
1168 if (!IS_ERR(filp)) {
1169 struct nfs_open_context *ctx;
1170 ctx = (struct nfs_open_context *)filp->private_data;
1173 nfs4_close_state(state, nd->intent.open.flags);
1177 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1180 struct rpc_cred *cred;
1181 struct nfs4_state *state;
1184 if (nd->flags & LOOKUP_CREATE) {
1185 attr.ia_mode = nd->intent.open.create_mode;
1186 attr.ia_valid = ATTR_MODE;
1187 if (!IS_POSIXACL(dir))
1188 attr.ia_mode &= ~current->fs->umask;
1191 BUG_ON(nd->intent.open.flags & O_CREAT);
1194 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1196 return (struct dentry *)cred;
1197 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1199 if (IS_ERR(state)) {
1200 if (PTR_ERR(state) == -ENOENT)
1201 d_add(dentry, NULL);
1202 return (struct dentry *)state;
1204 res = d_add_unique(dentry, igrab(state->inode));
1207 nfs4_intent_set_file(nd, dentry, state);
1212 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1214 struct rpc_cred *cred;
1215 struct nfs4_state *state;
1217 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1219 return PTR_ERR(cred);
1220 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1222 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1224 if (IS_ERR(state)) {
1225 switch (PTR_ERR(state)) {
1231 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1234 if (dentry->d_inode == NULL)
1239 if (state->inode == dentry->d_inode) {
1240 nfs4_intent_set_file(nd, dentry, state);
1243 nfs4_close_state(state, openflags);
1250 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1252 struct nfs4_server_caps_res res = {};
1253 struct rpc_message msg = {
1254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1255 .rpc_argp = fhandle,
1260 status = rpc_call_sync(server->client, &msg, 0);
1262 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1263 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1264 server->caps |= NFS_CAP_ACLS;
1265 if (res.has_links != 0)
1266 server->caps |= NFS_CAP_HARDLINKS;
1267 if (res.has_symlinks != 0)
1268 server->caps |= NFS_CAP_SYMLINKS;
1269 server->acl_bitmask = res.acl_bitmask;
1274 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1276 struct nfs4_exception exception = { };
1279 err = nfs4_handle_exception(server,
1280 _nfs4_server_capabilities(server, fhandle),
1282 } while (exception.retry);
1286 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1287 struct nfs_fsinfo *info)
1289 struct nfs4_lookup_root_arg args = {
1290 .bitmask = nfs4_fattr_bitmap,
1292 struct nfs4_lookup_res res = {
1294 .fattr = info->fattr,
1297 struct rpc_message msg = {
1298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1302 nfs_fattr_init(info->fattr);
1303 return rpc_call_sync(server->client, &msg, 0);
1306 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1307 struct nfs_fsinfo *info)
1309 struct nfs4_exception exception = { };
1312 err = nfs4_handle_exception(server,
1313 _nfs4_lookup_root(server, fhandle, info),
1315 } while (exception.retry);
1319 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1320 struct nfs_fsinfo *info)
1322 struct nfs_fattr * fattr = info->fattr;
1325 struct nfs4_lookup_arg args = {
1328 .bitmask = nfs4_fattr_bitmap,
1330 struct nfs4_lookup_res res = {
1335 struct rpc_message msg = {
1336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1343 * Now we do a separate LOOKUP for each component of the mount path.
1344 * The LOOKUPs are done separately so that we can conveniently
1345 * catch an ERR_WRONGSEC if it occurs along the way...
1347 status = nfs4_lookup_root(server, fhandle, info);
1351 p = server->mnt_path;
1353 struct nfs4_exception exception = { };
1360 while (*p && (*p != '/'))
1365 nfs_fattr_init(fattr);
1366 status = nfs4_handle_exception(server,
1367 rpc_call_sync(server->client, &msg, 0),
1369 } while (exception.retry);
1372 if (status == -ENOENT) {
1373 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1374 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1379 status = nfs4_server_capabilities(server, fhandle);
1381 status = nfs4_do_fsinfo(server, fhandle, info);
1386 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1388 struct nfs4_getattr_arg args = {
1390 .bitmask = server->attr_bitmask,
1392 struct nfs4_getattr_res res = {
1396 struct rpc_message msg = {
1397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1402 nfs_fattr_init(fattr);
1403 return rpc_call_sync(server->client, &msg, 0);
1406 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1408 struct nfs4_exception exception = { };
1411 err = nfs4_handle_exception(server,
1412 _nfs4_proc_getattr(server, fhandle, fattr),
1414 } while (exception.retry);
1419 * The file is not closed if it is opened due to the a request to change
1420 * the size of the file. The open call will not be needed once the
1421 * VFS layer lookup-intents are implemented.
1423 * Close is called when the inode is destroyed.
1424 * If we haven't opened the file for O_WRONLY, we
1425 * need to in the size_change case to obtain a stateid.
1428 * Because OPEN is always done by name in nfsv4, it is
1429 * possible that we opened a different file by the same
1430 * name. We can recognize this race condition, but we
1431 * can't do anything about it besides returning an error.
1433 * This will be fixed with VFS changes (lookup-intent).
1436 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1437 struct iattr *sattr)
1439 struct rpc_cred *cred;
1440 struct inode *inode = dentry->d_inode;
1441 struct nfs_open_context *ctx;
1442 struct nfs4_state *state = NULL;
1445 nfs_fattr_init(fattr);
1447 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1449 return PTR_ERR(cred);
1451 /* Search for an existing open(O_WRITE) file */
1452 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1456 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1457 NFS_FH(inode), sattr, state);
1459 nfs_setattr_update_inode(inode, sattr);
1461 put_nfs_open_context(ctx);
1466 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1467 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1470 struct nfs_server *server = NFS_SERVER(dir);
1471 struct nfs4_lookup_arg args = {
1472 .bitmask = server->attr_bitmask,
1473 .dir_fh = NFS_FH(dir),
1476 struct nfs4_lookup_res res = {
1481 struct rpc_message msg = {
1482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1487 nfs_fattr_init(fattr);
1489 dprintk("NFS call lookup %s\n", name->name);
1490 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1491 dprintk("NFS reply lookup: %d\n", status);
1495 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1497 struct nfs4_exception exception = { };
1500 err = nfs4_handle_exception(NFS_SERVER(dir),
1501 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1503 } while (exception.retry);
1507 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1509 struct nfs4_accessargs args = {
1510 .fh = NFS_FH(inode),
1512 struct nfs4_accessres res = { 0 };
1513 struct rpc_message msg = {
1514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1517 .rpc_cred = entry->cred,
1519 int mode = entry->mask;
1523 * Determine which access bits we want to ask for...
1525 if (mode & MAY_READ)
1526 args.access |= NFS4_ACCESS_READ;
1527 if (S_ISDIR(inode->i_mode)) {
1528 if (mode & MAY_WRITE)
1529 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1530 if (mode & MAY_EXEC)
1531 args.access |= NFS4_ACCESS_LOOKUP;
1533 if (mode & MAY_WRITE)
1534 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1535 if (mode & MAY_EXEC)
1536 args.access |= NFS4_ACCESS_EXECUTE;
1538 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1541 if (res.access & NFS4_ACCESS_READ)
1542 entry->mask |= MAY_READ;
1543 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1544 entry->mask |= MAY_WRITE;
1545 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1546 entry->mask |= MAY_EXEC;
1551 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1553 struct nfs4_exception exception = { };
1556 err = nfs4_handle_exception(NFS_SERVER(inode),
1557 _nfs4_proc_access(inode, entry),
1559 } while (exception.retry);
1564 * TODO: For the time being, we don't try to get any attributes
1565 * along with any of the zero-copy operations READ, READDIR,
1568 * In the case of the first three, we want to put the GETATTR
1569 * after the read-type operation -- this is because it is hard
1570 * to predict the length of a GETATTR response in v4, and thus
1571 * align the READ data correctly. This means that the GETATTR
1572 * may end up partially falling into the page cache, and we should
1573 * shift it into the 'tail' of the xdr_buf before processing.
1574 * To do this efficiently, we need to know the total length
1575 * of data received, which doesn't seem to be available outside
1578 * In the case of WRITE, we also want to put the GETATTR after
1579 * the operation -- in this case because we want to make sure
1580 * we get the post-operation mtime and size. This means that
1581 * we can't use xdr_encode_pages() as written: we need a variant
1582 * of it which would leave room in the 'tail' iovec.
1584 * Both of these changes to the XDR layer would in fact be quite
1585 * minor, but I decided to leave them for a subsequent patch.
1587 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1588 unsigned int pgbase, unsigned int pglen)
1590 struct nfs4_readlink args = {
1591 .fh = NFS_FH(inode),
1596 struct rpc_message msg = {
1597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1602 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1605 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1606 unsigned int pgbase, unsigned int pglen)
1608 struct nfs4_exception exception = { };
1611 err = nfs4_handle_exception(NFS_SERVER(inode),
1612 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1614 } while (exception.retry);
1618 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1620 int flags = rdata->flags;
1621 struct inode *inode = rdata->inode;
1622 struct nfs_fattr *fattr = rdata->res.fattr;
1623 struct nfs_server *server = NFS_SERVER(inode);
1624 struct rpc_message msg = {
1625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1626 .rpc_argp = &rdata->args,
1627 .rpc_resp = &rdata->res,
1628 .rpc_cred = rdata->cred,
1630 unsigned long timestamp = jiffies;
1633 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1634 (long long) rdata->args.offset);
1636 nfs_fattr_init(fattr);
1637 status = rpc_call_sync(server->client, &msg, flags);
1639 renew_lease(server, timestamp);
1640 dprintk("NFS reply read: %d\n", status);
1644 static int nfs4_proc_read(struct nfs_read_data *rdata)
1646 struct nfs4_exception exception = { };
1649 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1650 _nfs4_proc_read(rdata),
1652 } while (exception.retry);
1656 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1658 int rpcflags = wdata->flags;
1659 struct inode *inode = wdata->inode;
1660 struct nfs_fattr *fattr = wdata->res.fattr;
1661 struct nfs_server *server = NFS_SERVER(inode);
1662 struct rpc_message msg = {
1663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1664 .rpc_argp = &wdata->args,
1665 .rpc_resp = &wdata->res,
1666 .rpc_cred = wdata->cred,
1670 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1671 (long long) wdata->args.offset);
1673 wdata->args.bitmask = server->attr_bitmask;
1674 wdata->res.server = server;
1675 nfs_fattr_init(fattr);
1676 status = rpc_call_sync(server->client, &msg, rpcflags);
1677 dprintk("NFS reply write: %d\n", status);
1680 nfs_post_op_update_inode(inode, fattr);
1681 return wdata->res.count;
1684 static int nfs4_proc_write(struct nfs_write_data *wdata)
1686 struct nfs4_exception exception = { };
1689 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1690 _nfs4_proc_write(wdata),
1692 } while (exception.retry);
1696 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1698 struct inode *inode = cdata->inode;
1699 struct nfs_fattr *fattr = cdata->res.fattr;
1700 struct nfs_server *server = NFS_SERVER(inode);
1701 struct rpc_message msg = {
1702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1703 .rpc_argp = &cdata->args,
1704 .rpc_resp = &cdata->res,
1705 .rpc_cred = cdata->cred,
1709 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1710 (long long) cdata->args.offset);
1712 cdata->args.bitmask = server->attr_bitmask;
1713 cdata->res.server = server;
1714 nfs_fattr_init(fattr);
1715 status = rpc_call_sync(server->client, &msg, 0);
1716 dprintk("NFS reply commit: %d\n", status);
1718 nfs_post_op_update_inode(inode, fattr);
1722 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1724 struct nfs4_exception exception = { };
1727 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1728 _nfs4_proc_commit(cdata),
1730 } while (exception.retry);
1736 * We will need to arrange for the VFS layer to provide an atomic open.
1737 * Until then, this create/open method is prone to inefficiency and race
1738 * conditions due to the lookup, create, and open VFS calls from sys_open()
1739 * placed on the wire.
1741 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1742 * The file will be opened again in the subsequent VFS open call
1743 * (nfs4_proc_file_open).
1745 * The open for read will just hang around to be used by any process that
1746 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1750 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1751 int flags, struct nameidata *nd)
1753 struct nfs4_state *state;
1754 struct rpc_cred *cred;
1757 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1759 status = PTR_ERR(cred);
1762 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1764 if (IS_ERR(state)) {
1765 status = PTR_ERR(state);
1768 d_instantiate(dentry, igrab(state->inode));
1769 if (flags & O_EXCL) {
1770 struct nfs_fattr fattr;
1771 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1772 NFS_FH(state->inode), sattr, state);
1774 nfs_setattr_update_inode(state->inode, sattr);
1776 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1777 nfs4_intent_set_file(nd, dentry, state);
1779 nfs4_close_state(state, flags);
1784 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1786 struct nfs_server *server = NFS_SERVER(dir);
1787 struct nfs4_remove_arg args = {
1790 .bitmask = server->attr_bitmask,
1792 struct nfs_fattr dir_attr;
1793 struct nfs4_remove_res res = {
1795 .dir_attr = &dir_attr,
1797 struct rpc_message msg = {
1798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1804 nfs_fattr_init(res.dir_attr);
1805 status = rpc_call_sync(server->client, &msg, 0);
1807 update_changeattr(dir, &res.cinfo);
1808 nfs_post_op_update_inode(dir, res.dir_attr);
1813 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1815 struct nfs4_exception exception = { };
1818 err = nfs4_handle_exception(NFS_SERVER(dir),
1819 _nfs4_proc_remove(dir, name),
1821 } while (exception.retry);
1825 struct unlink_desc {
1826 struct nfs4_remove_arg args;
1827 struct nfs4_remove_res res;
1828 struct nfs_fattr dir_attr;
1831 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1834 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1835 struct unlink_desc *up;
1837 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1841 up->args.fh = NFS_FH(dir->d_inode);
1842 up->args.name = name;
1843 up->args.bitmask = server->attr_bitmask;
1844 up->res.server = server;
1845 up->res.dir_attr = &up->dir_attr;
1847 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1848 msg->rpc_argp = &up->args;
1849 msg->rpc_resp = &up->res;
1853 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1855 struct rpc_message *msg = &task->tk_msg;
1856 struct unlink_desc *up;
1858 if (msg->rpc_resp != NULL) {
1859 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1860 update_changeattr(dir->d_inode, &up->res.cinfo);
1861 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1863 msg->rpc_resp = NULL;
1864 msg->rpc_argp = NULL;
1869 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1870 struct inode *new_dir, struct qstr *new_name)
1872 struct nfs_server *server = NFS_SERVER(old_dir);
1873 struct nfs4_rename_arg arg = {
1874 .old_dir = NFS_FH(old_dir),
1875 .new_dir = NFS_FH(new_dir),
1876 .old_name = old_name,
1877 .new_name = new_name,
1878 .bitmask = server->attr_bitmask,
1880 struct nfs_fattr old_fattr, new_fattr;
1881 struct nfs4_rename_res res = {
1883 .old_fattr = &old_fattr,
1884 .new_fattr = &new_fattr,
1886 struct rpc_message msg = {
1887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1893 nfs_fattr_init(res.old_fattr);
1894 nfs_fattr_init(res.new_fattr);
1895 status = rpc_call_sync(server->client, &msg, 0);
1898 update_changeattr(old_dir, &res.old_cinfo);
1899 nfs_post_op_update_inode(old_dir, res.old_fattr);
1900 update_changeattr(new_dir, &res.new_cinfo);
1901 nfs_post_op_update_inode(new_dir, res.new_fattr);
1906 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1907 struct inode *new_dir, struct qstr *new_name)
1909 struct nfs4_exception exception = { };
1912 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1913 _nfs4_proc_rename(old_dir, old_name,
1916 } while (exception.retry);
1920 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1922 struct nfs_server *server = NFS_SERVER(inode);
1923 struct nfs4_link_arg arg = {
1924 .fh = NFS_FH(inode),
1925 .dir_fh = NFS_FH(dir),
1927 .bitmask = server->attr_bitmask,
1929 struct nfs_fattr fattr, dir_attr;
1930 struct nfs4_link_res res = {
1933 .dir_attr = &dir_attr,
1935 struct rpc_message msg = {
1936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1942 nfs_fattr_init(res.fattr);
1943 nfs_fattr_init(res.dir_attr);
1944 status = rpc_call_sync(server->client, &msg, 0);
1946 update_changeattr(dir, &res.cinfo);
1947 nfs_post_op_update_inode(dir, res.dir_attr);
1948 nfs_refresh_inode(inode, res.fattr);
1954 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1956 struct nfs4_exception exception = { };
1959 err = nfs4_handle_exception(NFS_SERVER(inode),
1960 _nfs4_proc_link(inode, dir, name),
1962 } while (exception.retry);
1966 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1967 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1968 struct nfs_fattr *fattr)
1970 struct nfs_server *server = NFS_SERVER(dir);
1971 struct nfs_fattr dir_fattr;
1972 struct nfs4_create_arg arg = {
1973 .dir_fh = NFS_FH(dir),
1978 .bitmask = server->attr_bitmask,
1980 struct nfs4_create_res res = {
1984 .dir_fattr = &dir_fattr,
1986 struct rpc_message msg = {
1987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1993 if (path->len > NFS4_MAXPATHLEN)
1994 return -ENAMETOOLONG;
1995 arg.u.symlink = path;
1996 nfs_fattr_init(fattr);
1997 nfs_fattr_init(&dir_fattr);
1999 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2001 update_changeattr(dir, &res.dir_cinfo);
2002 nfs_post_op_update_inode(dir, res.dir_fattr);
2006 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2007 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2008 struct nfs_fattr *fattr)
2010 struct nfs4_exception exception = { };
2013 err = nfs4_handle_exception(NFS_SERVER(dir),
2014 _nfs4_proc_symlink(dir, name, path, sattr,
2017 } while (exception.retry);
2021 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2022 struct iattr *sattr)
2024 struct nfs_server *server = NFS_SERVER(dir);
2025 struct nfs_fh fhandle;
2026 struct nfs_fattr fattr, dir_fattr;
2027 struct nfs4_create_arg arg = {
2028 .dir_fh = NFS_FH(dir),
2030 .name = &dentry->d_name,
2033 .bitmask = server->attr_bitmask,
2035 struct nfs4_create_res res = {
2039 .dir_fattr = &dir_fattr,
2041 struct rpc_message msg = {
2042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2048 nfs_fattr_init(&fattr);
2049 nfs_fattr_init(&dir_fattr);
2051 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2053 update_changeattr(dir, &res.dir_cinfo);
2054 nfs_post_op_update_inode(dir, res.dir_fattr);
2055 status = nfs_instantiate(dentry, &fhandle, &fattr);
2060 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2061 struct iattr *sattr)
2063 struct nfs4_exception exception = { };
2066 err = nfs4_handle_exception(NFS_SERVER(dir),
2067 _nfs4_proc_mkdir(dir, dentry, sattr),
2069 } while (exception.retry);
2073 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2074 u64 cookie, struct page *page, unsigned int count, int plus)
2076 struct inode *dir = dentry->d_inode;
2077 struct nfs4_readdir_arg args = {
2082 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2084 struct nfs4_readdir_res res;
2085 struct rpc_message msg = {
2086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2093 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2094 dentry->d_parent->d_name.name,
2095 dentry->d_name.name,
2096 (unsigned long long)cookie);
2098 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2099 res.pgbase = args.pgbase;
2100 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2102 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2104 dprintk("%s: returns %d\n", __FUNCTION__, status);
2108 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2109 u64 cookie, struct page *page, unsigned int count, int plus)
2111 struct nfs4_exception exception = { };
2114 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2115 _nfs4_proc_readdir(dentry, cred, cookie,
2118 } while (exception.retry);
2122 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2123 struct iattr *sattr, dev_t rdev)
2125 struct nfs_server *server = NFS_SERVER(dir);
2127 struct nfs_fattr fattr, dir_fattr;
2128 struct nfs4_create_arg arg = {
2129 .dir_fh = NFS_FH(dir),
2131 .name = &dentry->d_name,
2133 .bitmask = server->attr_bitmask,
2135 struct nfs4_create_res res = {
2139 .dir_fattr = &dir_fattr,
2141 struct rpc_message msg = {
2142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2147 int mode = sattr->ia_mode;
2149 nfs_fattr_init(&fattr);
2150 nfs_fattr_init(&dir_fattr);
2152 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2153 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2155 arg.ftype = NF4FIFO;
2156 else if (S_ISBLK(mode)) {
2158 arg.u.device.specdata1 = MAJOR(rdev);
2159 arg.u.device.specdata2 = MINOR(rdev);
2161 else if (S_ISCHR(mode)) {
2163 arg.u.device.specdata1 = MAJOR(rdev);
2164 arg.u.device.specdata2 = MINOR(rdev);
2167 arg.ftype = NF4SOCK;
2169 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2171 update_changeattr(dir, &res.dir_cinfo);
2172 nfs_post_op_update_inode(dir, res.dir_fattr);
2173 status = nfs_instantiate(dentry, &fh, &fattr);
2178 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2179 struct iattr *sattr, dev_t rdev)
2181 struct nfs4_exception exception = { };
2184 err = nfs4_handle_exception(NFS_SERVER(dir),
2185 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2187 } while (exception.retry);
2191 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2192 struct nfs_fsstat *fsstat)
2194 struct nfs4_statfs_arg args = {
2196 .bitmask = server->attr_bitmask,
2198 struct rpc_message msg = {
2199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2204 nfs_fattr_init(fsstat->fattr);
2205 return rpc_call_sync(server->client, &msg, 0);
2208 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2210 struct nfs4_exception exception = { };
2213 err = nfs4_handle_exception(server,
2214 _nfs4_proc_statfs(server, fhandle, fsstat),
2216 } while (exception.retry);
2220 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2221 struct nfs_fsinfo *fsinfo)
2223 struct nfs4_fsinfo_arg args = {
2225 .bitmask = server->attr_bitmask,
2227 struct rpc_message msg = {
2228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2233 return rpc_call_sync(server->client, &msg, 0);
2236 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2238 struct nfs4_exception exception = { };
2242 err = nfs4_handle_exception(server,
2243 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2245 } while (exception.retry);
2249 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2251 nfs_fattr_init(fsinfo->fattr);
2252 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2255 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2256 struct nfs_pathconf *pathconf)
2258 struct nfs4_pathconf_arg args = {
2260 .bitmask = server->attr_bitmask,
2262 struct rpc_message msg = {
2263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2265 .rpc_resp = pathconf,
2268 /* None of the pathconf attributes are mandatory to implement */
2269 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2270 memset(pathconf, 0, sizeof(*pathconf));
2274 nfs_fattr_init(pathconf->fattr);
2275 return rpc_call_sync(server->client, &msg, 0);
2278 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2279 struct nfs_pathconf *pathconf)
2281 struct nfs4_exception exception = { };
2285 err = nfs4_handle_exception(server,
2286 _nfs4_proc_pathconf(server, fhandle, pathconf),
2288 } while (exception.retry);
2292 static void nfs4_read_done(struct rpc_task *task, void *calldata)
2294 struct nfs_read_data *data = calldata;
2295 struct inode *inode = data->inode;
2297 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2298 rpc_restart_call(task);
2301 if (task->tk_status > 0)
2302 renew_lease(NFS_SERVER(inode), data->timestamp);
2303 /* Call back common NFS readpage processing */
2304 nfs_readpage_result(task, calldata);
2307 static const struct rpc_call_ops nfs4_read_ops = {
2308 .rpc_call_done = nfs4_read_done,
2309 .rpc_release = nfs_readdata_release,
2313 nfs4_proc_read_setup(struct nfs_read_data *data)
2315 struct rpc_task *task = &data->task;
2316 struct rpc_message msg = {
2317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2318 .rpc_argp = &data->args,
2319 .rpc_resp = &data->res,
2320 .rpc_cred = data->cred,
2322 struct inode *inode = data->inode;
2325 data->timestamp = jiffies;
2327 /* N.B. Do we need to test? Never called for swapfile inode */
2328 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2330 /* Finalize the task. */
2331 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
2332 rpc_call_setup(task, &msg, 0);
2335 static void nfs4_write_done(struct rpc_task *task, void *calldata)
2337 struct nfs_write_data *data = calldata;
2338 struct inode *inode = data->inode;
2340 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2341 rpc_restart_call(task);
2344 if (task->tk_status >= 0) {
2345 renew_lease(NFS_SERVER(inode), data->timestamp);
2346 nfs_post_op_update_inode(inode, data->res.fattr);
2348 /* Call back common NFS writeback processing */
2349 nfs_writeback_done(task, calldata);
2352 static const struct rpc_call_ops nfs4_write_ops = {
2353 .rpc_call_done = nfs4_write_done,
2354 .rpc_release = nfs_writedata_release,
2358 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2360 struct rpc_task *task = &data->task;
2361 struct rpc_message msg = {
2362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2363 .rpc_argp = &data->args,
2364 .rpc_resp = &data->res,
2365 .rpc_cred = data->cred,
2367 struct inode *inode = data->inode;
2368 struct nfs_server *server = NFS_SERVER(inode);
2372 if (how & FLUSH_STABLE) {
2373 if (!NFS_I(inode)->ncommit)
2374 stable = NFS_FILE_SYNC;
2376 stable = NFS_DATA_SYNC;
2378 stable = NFS_UNSTABLE;
2379 data->args.stable = stable;
2380 data->args.bitmask = server->attr_bitmask;
2381 data->res.server = server;
2383 data->timestamp = jiffies;
2385 /* Set the initial flags for the task. */
2386 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2388 /* Finalize the task. */
2389 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
2390 rpc_call_setup(task, &msg, 0);
2393 static void nfs4_commit_done(struct rpc_task *task, void *calldata)
2395 struct nfs_write_data *data = calldata;
2396 struct inode *inode = data->inode;
2398 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2399 rpc_restart_call(task);
2402 if (task->tk_status >= 0)
2403 nfs_post_op_update_inode(inode, data->res.fattr);
2404 /* Call back common NFS writeback processing */
2405 nfs_commit_done(task, calldata);
2408 static const struct rpc_call_ops nfs4_commit_ops = {
2409 .rpc_call_done = nfs4_commit_done,
2410 .rpc_release = nfs_commit_release,
2414 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2416 struct rpc_task *task = &data->task;
2417 struct rpc_message msg = {
2418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2419 .rpc_argp = &data->args,
2420 .rpc_resp = &data->res,
2421 .rpc_cred = data->cred,
2423 struct inode *inode = data->inode;
2424 struct nfs_server *server = NFS_SERVER(inode);
2427 data->args.bitmask = server->attr_bitmask;
2428 data->res.server = server;
2430 /* Set the initial flags for the task. */
2431 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2433 /* Finalize the task. */
2434 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
2435 rpc_call_setup(task, &msg, 0);
2439 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2440 * standalone procedure for queueing an asynchronous RENEW.
2442 static void nfs4_renew_done(struct rpc_task *task, void *data)
2444 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2445 unsigned long timestamp = (unsigned long)data;
2447 if (task->tk_status < 0) {
2448 switch (task->tk_status) {
2449 case -NFS4ERR_STALE_CLIENTID:
2450 case -NFS4ERR_EXPIRED:
2451 case -NFS4ERR_CB_PATH_DOWN:
2452 nfs4_schedule_state_recovery(clp);
2456 spin_lock(&clp->cl_lock);
2457 if (time_before(clp->cl_last_renewal,timestamp))
2458 clp->cl_last_renewal = timestamp;
2459 spin_unlock(&clp->cl_lock);
2462 static const struct rpc_call_ops nfs4_renew_ops = {
2463 .rpc_call_done = nfs4_renew_done,
2467 nfs4_proc_async_renew(struct nfs4_client *clp)
2469 struct rpc_message msg = {
2470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2472 .rpc_cred = clp->cl_cred,
2475 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2476 &nfs4_renew_ops, (void *)jiffies);
2480 nfs4_proc_renew(struct nfs4_client *clp)
2482 struct rpc_message msg = {
2483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2485 .rpc_cred = clp->cl_cred,
2487 unsigned long now = jiffies;
2490 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2493 spin_lock(&clp->cl_lock);
2494 if (time_before(clp->cl_last_renewal,now))
2495 clp->cl_last_renewal = now;
2496 spin_unlock(&clp->cl_lock);
2500 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2502 return (server->caps & NFS_CAP_ACLS)
2503 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2504 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2507 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2508 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2511 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2513 static void buf_to_pages(const void *buf, size_t buflen,
2514 struct page **pages, unsigned int *pgbase)
2516 const void *p = buf;
2518 *pgbase = offset_in_page(buf);
2520 while (p < buf + buflen) {
2521 *(pages++) = virt_to_page(p);
2522 p += PAGE_CACHE_SIZE;
2526 struct nfs4_cached_acl {
2532 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2534 struct nfs_inode *nfsi = NFS_I(inode);
2536 spin_lock(&inode->i_lock);
2537 kfree(nfsi->nfs4_acl);
2538 nfsi->nfs4_acl = acl;
2539 spin_unlock(&inode->i_lock);
2542 static void nfs4_zap_acl_attr(struct inode *inode)
2544 nfs4_set_cached_acl(inode, NULL);
2547 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2549 struct nfs_inode *nfsi = NFS_I(inode);
2550 struct nfs4_cached_acl *acl;
2553 spin_lock(&inode->i_lock);
2554 acl = nfsi->nfs4_acl;
2557 if (buf == NULL) /* user is just asking for length */
2559 if (acl->cached == 0)
2561 ret = -ERANGE; /* see getxattr(2) man page */
2562 if (acl->len > buflen)
2564 memcpy(buf, acl->data, acl->len);
2568 spin_unlock(&inode->i_lock);
2572 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2574 struct nfs4_cached_acl *acl;
2576 if (buf && acl_len <= PAGE_SIZE) {
2577 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2581 memcpy(acl->data, buf, acl_len);
2583 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2590 nfs4_set_cached_acl(inode, acl);
2593 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2595 struct page *pages[NFS4ACL_MAXPAGES];
2596 struct nfs_getaclargs args = {
2597 .fh = NFS_FH(inode),
2601 size_t resp_len = buflen;
2603 struct rpc_message msg = {
2604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2606 .rpc_resp = &resp_len,
2608 struct page *localpage = NULL;
2611 if (buflen < PAGE_SIZE) {
2612 /* As long as we're doing a round trip to the server anyway,
2613 * let's be prepared for a page of acl data. */
2614 localpage = alloc_page(GFP_KERNEL);
2615 resp_buf = page_address(localpage);
2616 if (localpage == NULL)
2618 args.acl_pages[0] = localpage;
2619 args.acl_pgbase = 0;
2620 resp_len = args.acl_len = PAGE_SIZE;
2623 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2625 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2628 if (resp_len > args.acl_len)
2629 nfs4_write_cached_acl(inode, NULL, resp_len);
2631 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2634 if (resp_len > buflen)
2637 memcpy(buf, resp_buf, resp_len);
2642 __free_page(localpage);
2646 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2648 struct nfs_server *server = NFS_SERVER(inode);
2651 if (!nfs4_server_supports_acls(server))
2653 ret = nfs_revalidate_inode(server, inode);
2656 ret = nfs4_read_cached_acl(inode, buf, buflen);
2659 return nfs4_get_acl_uncached(inode, buf, buflen);
2662 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2664 struct nfs_server *server = NFS_SERVER(inode);
2665 struct page *pages[NFS4ACL_MAXPAGES];
2666 struct nfs_setaclargs arg = {
2667 .fh = NFS_FH(inode),
2671 struct rpc_message msg = {
2672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2678 if (!nfs4_server_supports_acls(server))
2680 nfs_inode_return_delegation(inode);
2681 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2682 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2684 nfs4_write_cached_acl(inode, buf, buflen);
2689 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2691 struct nfs4_client *clp = server->nfs4_state;
2693 if (!clp || task->tk_status >= 0)
2695 switch(task->tk_status) {
2696 case -NFS4ERR_STALE_CLIENTID:
2697 case -NFS4ERR_STALE_STATEID:
2698 case -NFS4ERR_EXPIRED:
2699 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2700 nfs4_schedule_state_recovery(clp);
2701 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2702 rpc_wake_up_task(task);
2703 task->tk_status = 0;
2705 case -NFS4ERR_GRACE:
2706 case -NFS4ERR_DELAY:
2707 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2708 task->tk_status = 0;
2710 case -NFS4ERR_OLD_STATEID:
2711 task->tk_status = 0;
2714 task->tk_status = nfs4_map_errors(task->tk_status);
2718 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2722 int interruptible, res = 0;
2726 rpc_clnt_sigmask(clnt, &oldset);
2727 interruptible = TASK_UNINTERRUPTIBLE;
2729 interruptible = TASK_INTERRUPTIBLE;
2730 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2731 nfs4_schedule_state_recovery(clp);
2732 if (clnt->cl_intr && signalled())
2734 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2736 finish_wait(&clp->cl_waitq, &wait);
2737 rpc_clnt_sigunmask(clnt, &oldset);
2741 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2749 *timeout = NFS4_POLL_RETRY_MIN;
2750 if (*timeout > NFS4_POLL_RETRY_MAX)
2751 *timeout = NFS4_POLL_RETRY_MAX;
2752 rpc_clnt_sigmask(clnt, &oldset);
2753 if (clnt->cl_intr) {
2754 schedule_timeout_interruptible(*timeout);
2758 schedule_timeout_uninterruptible(*timeout);
2759 rpc_clnt_sigunmask(clnt, &oldset);
2764 /* This is the error handling routine for processes that are allowed
2767 int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2769 struct nfs4_client *clp = server->nfs4_state;
2770 int ret = errorcode;
2772 exception->retry = 0;
2776 case -NFS4ERR_STALE_CLIENTID:
2777 case -NFS4ERR_STALE_STATEID:
2778 case -NFS4ERR_EXPIRED:
2779 ret = nfs4_wait_clnt_recover(server->client, clp);
2781 exception->retry = 1;
2783 case -NFS4ERR_GRACE:
2784 case -NFS4ERR_DELAY:
2785 ret = nfs4_delay(server->client, &exception->timeout);
2788 case -NFS4ERR_OLD_STATEID:
2789 exception->retry = 1;
2791 /* We failed to handle the error */
2792 return nfs4_map_errors(ret);
2795 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2797 nfs4_verifier sc_verifier;
2798 struct nfs4_setclientid setclientid = {
2799 .sc_verifier = &sc_verifier,
2802 struct rpc_message msg = {
2803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2804 .rpc_argp = &setclientid,
2806 .rpc_cred = clp->cl_cred,
2812 p = (u32*)sc_verifier.data;
2813 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2814 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2817 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2818 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2819 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2820 clp->cl_cred->cr_ops->cr_name,
2821 clp->cl_id_uniquifier);
2822 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2823 sizeof(setclientid.sc_netid), "tcp");
2824 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2825 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2826 clp->cl_ipaddr, port >> 8, port & 255);
2828 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2829 if (status != -NFS4ERR_CLID_INUSE)
2834 ssleep(clp->cl_lease_time + 1);
2836 if (++clp->cl_id_uniquifier == 0)
2843 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2845 struct nfs_fsinfo fsinfo;
2846 struct rpc_message msg = {
2847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2849 .rpc_resp = &fsinfo,
2850 .rpc_cred = clp->cl_cred,
2856 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2858 spin_lock(&clp->cl_lock);
2859 clp->cl_lease_time = fsinfo.lease_time * HZ;
2860 clp->cl_last_renewal = now;
2861 spin_unlock(&clp->cl_lock);
2866 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2868 struct nfs4_delegreturnargs args = {
2869 .fhandle = NFS_FH(inode),
2872 struct rpc_message msg = {
2873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2878 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2881 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2883 struct nfs_server *server = NFS_SERVER(inode);
2884 struct nfs4_exception exception = { };
2887 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2889 case -NFS4ERR_STALE_STATEID:
2890 case -NFS4ERR_EXPIRED:
2891 nfs4_schedule_state_recovery(server->nfs4_state);
2895 err = nfs4_handle_exception(server, err, &exception);
2896 } while (exception.retry);
2900 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2901 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2904 * sleep, with exponential backoff, and retry the LOCK operation.
2906 static unsigned long
2907 nfs4_set_lock_task_retry(unsigned long timeout)
2909 schedule_timeout_interruptible(timeout);
2911 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2912 return NFS4_LOCK_MAXTIMEOUT;
2917 nfs4_lck_type(int cmd, struct file_lock *request)
2920 switch (request->fl_type) {
2922 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2924 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2926 return NFS4_WRITE_LT;
2932 static inline uint64_t
2933 nfs4_lck_length(struct file_lock *request)
2935 if (request->fl_end == OFFSET_MAX)
2936 return ~(uint64_t)0;
2937 return request->fl_end - request->fl_start + 1;
2940 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2942 struct inode *inode = state->inode;
2943 struct nfs_server *server = NFS_SERVER(inode);
2944 struct nfs4_client *clp = server->nfs4_state;
2945 struct nfs_lockargs arg = {
2946 .fh = NFS_FH(inode),
2947 .type = nfs4_lck_type(cmd, request),
2948 .offset = request->fl_start,
2949 .length = nfs4_lck_length(request),
2951 struct nfs_lockres res = {
2954 struct rpc_message msg = {
2955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2958 .rpc_cred = state->owner->so_cred,
2960 struct nfs_lowner nlo;
2961 struct nfs4_lock_state *lsp;
2964 down_read(&clp->cl_sem);
2965 nlo.clientid = clp->cl_clientid;
2966 status = nfs4_set_lock_state(state, request);
2969 lsp = request->fl_u.nfs4_fl.owner;
2970 nlo.id = lsp->ls_id;
2972 status = rpc_call_sync(server->client, &msg, 0);
2974 request->fl_type = F_UNLCK;
2975 } else if (status == -NFS4ERR_DENIED) {
2976 int64_t len, start, end;
2977 start = res.u.denied.offset;
2978 len = res.u.denied.length;
2979 end = start + len - 1;
2980 if (end < 0 || len == 0)
2981 request->fl_end = OFFSET_MAX;
2983 request->fl_end = (loff_t)end;
2984 request->fl_start = (loff_t)start;
2985 request->fl_type = F_WRLCK;
2986 if (res.u.denied.type & 1)
2987 request->fl_type = F_RDLCK;
2988 request->fl_pid = 0;
2992 up_read(&clp->cl_sem);
2996 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2998 struct nfs4_exception exception = { };
3002 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3003 _nfs4_proc_getlk(state, cmd, request),
3005 } while (exception.retry);
3009 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3012 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3014 res = posix_lock_file_wait(file, fl);
3017 res = flock_lock_file_wait(file, fl);
3023 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
3028 struct nfs4_unlockdata {
3029 struct nfs_lockargs arg;
3030 struct nfs_locku_opargs luargs;
3031 struct nfs_lockres res;
3032 struct nfs4_lock_state *lsp;
3033 struct nfs_open_context *ctx;
3036 static void nfs4_locku_release_calldata(void *data)
3038 struct nfs4_unlockdata *calldata = data;
3039 nfs_free_seqid(calldata->luargs.seqid);
3040 nfs4_put_lock_state(calldata->lsp);
3041 put_nfs_open_context(calldata->ctx);
3045 static void nfs4_locku_done(struct rpc_task *task, void *data)
3047 struct nfs4_unlockdata *calldata = data;
3049 if (RPC_ASSASSINATED(task))
3051 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
3052 switch (task->tk_status) {
3054 memcpy(calldata->lsp->ls_stateid.data,
3055 calldata->res.u.stateid.data,
3056 sizeof(calldata->lsp->ls_stateid.data));
3058 case -NFS4ERR_STALE_STATEID:
3059 case -NFS4ERR_EXPIRED:
3060 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
3063 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
3064 rpc_restart_call(task);
3069 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3071 struct nfs4_unlockdata *calldata = data;
3072 struct rpc_message msg = {
3073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3074 .rpc_argp = &calldata->arg,
3075 .rpc_resp = &calldata->res,
3076 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
3080 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
3083 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3084 /* Note: exit _without_ running nfs4_locku_done */
3085 task->tk_action = NULL;
3088 rpc_call_setup(task, &msg, 0);
3091 static const struct rpc_call_ops nfs4_locku_ops = {
3092 .rpc_call_prepare = nfs4_locku_prepare,
3093 .rpc_call_done = nfs4_locku_done,
3094 .rpc_release = nfs4_locku_release_calldata,
3097 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3099 struct nfs4_unlockdata *calldata;
3100 struct inode *inode = state->inode;
3101 struct nfs_server *server = NFS_SERVER(inode);
3102 struct nfs4_lock_state *lsp;
3103 struct rpc_task *task;
3106 /* Is this a delegated lock? */
3107 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3110 status = nfs4_set_lock_state(state, request);
3113 lsp = request->fl_u.nfs4_fl.owner;
3114 /* We might have lost the locks! */
3115 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
3118 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
3119 if (calldata == NULL)
3121 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3122 if (calldata->luargs.seqid == NULL) {
3126 calldata->luargs.stateid = &lsp->ls_stateid;
3127 calldata->arg.fh = NFS_FH(inode);
3128 calldata->arg.type = nfs4_lck_type(cmd, request);
3129 calldata->arg.offset = request->fl_start;
3130 calldata->arg.length = nfs4_lck_length(request);
3131 calldata->arg.u.locku = &calldata->luargs;
3132 calldata->res.server = server;
3133 calldata->lsp = lsp;
3134 atomic_inc(&lsp->ls_count);
3136 /* Ensure we don't close file until we're done freeing locks! */
3137 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
3139 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_locku_ops, calldata);
3140 if (!IS_ERR(task)) {
3141 status = nfs4_wait_for_completion_rpc_task(task);
3142 rpc_release_task(task);
3144 status = PTR_ERR(task);
3145 nfs4_locku_release_calldata(calldata);
3148 do_vfs_lock(request->fl_file, request);
3152 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
3154 struct inode *inode = state->inode;
3155 struct nfs_server *server = NFS_SERVER(inode);
3156 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
3157 struct nfs_lock_opargs largs = {
3158 .lock_stateid = &lsp->ls_stateid,
3159 .open_stateid = &state->stateid,
3161 .clientid = server->nfs4_state->cl_clientid,
3166 struct nfs_lockargs arg = {
3167 .fh = NFS_FH(inode),
3168 .type = nfs4_lck_type(cmd, request),
3169 .offset = request->fl_start,
3170 .length = nfs4_lck_length(request),
3175 struct nfs_lockres res = {
3178 struct rpc_message msg = {
3179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3182 .rpc_cred = state->owner->so_cred,
3184 int status = -ENOMEM;
3186 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3187 if (largs.lock_seqid == NULL)
3189 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
3190 struct nfs4_state_owner *owner = state->owner;
3192 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
3193 if (largs.open_seqid == NULL)
3195 largs.new_lock_owner = 1;
3196 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3197 /* increment open seqid on success, and seqid mutating errors */
3198 if (largs.new_lock_owner != 0) {
3199 nfs_increment_open_seqid(status, largs.open_seqid);
3201 nfs_confirm_seqid(&lsp->ls_seqid, 0);
3203 nfs_free_seqid(largs.open_seqid);
3205 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3206 /* increment lock seqid on success, and seqid mutating errors*/
3207 nfs_increment_lock_seqid(status, largs.lock_seqid);
3208 /* save the returned stateid. */
3210 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
3211 sizeof(lsp->ls_stateid.data));
3212 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3213 } else if (status == -NFS4ERR_DENIED)
3216 nfs_free_seqid(largs.lock_seqid);
3220 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3222 struct nfs_server *server = NFS_SERVER(state->inode);
3223 struct nfs4_exception exception = { };
3226 /* Cache the lock if possible... */
3227 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3230 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3231 if (err != -NFS4ERR_DELAY)
3233 nfs4_handle_exception(server, err, &exception);
3234 } while (exception.retry);
3238 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3240 struct nfs_server *server = NFS_SERVER(state->inode);
3241 struct nfs4_exception exception = { };
3244 err = nfs4_set_lock_state(state, request);
3248 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3249 if (err != -NFS4ERR_DELAY)
3251 nfs4_handle_exception(server, err, &exception);
3252 } while (exception.retry);
3256 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3258 struct nfs4_client *clp = state->owner->so_client;
3261 /* Is this a delegated open? */
3262 if (NFS_I(state->inode)->delegation_state != 0) {
3263 /* Yes: cache locks! */
3264 status = do_vfs_lock(request->fl_file, request);
3265 /* ...but avoid races with delegation recall... */
3266 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
3269 down_read(&clp->cl_sem);
3270 status = nfs4_set_lock_state(state, request);
3273 status = _nfs4_do_setlk(state, cmd, request, 0);
3276 /* Note: we always want to sleep here! */
3277 request->fl_flags |= FL_SLEEP;
3278 if (do_vfs_lock(request->fl_file, request) < 0)
3279 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3281 up_read(&clp->cl_sem);
3285 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3287 struct nfs4_exception exception = { };
3291 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3292 _nfs4_proc_setlk(state, cmd, request),
3294 } while (exception.retry);
3299 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3301 struct nfs_open_context *ctx;
3302 struct nfs4_state *state;
3303 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3306 /* verify open state */
3307 ctx = (struct nfs_open_context *)filp->private_data;
3310 if (request->fl_start < 0 || request->fl_end < 0)
3314 return nfs4_proc_getlk(state, F_GETLK, request);
3316 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3319 if (request->fl_type == F_UNLCK)
3320 return nfs4_proc_unlck(state, cmd, request);
3323 status = nfs4_proc_setlk(state, cmd, request);
3324 if ((status != -EAGAIN) || IS_SETLK(cmd))
3326 timeout = nfs4_set_lock_task_retry(timeout);
3327 status = -ERESTARTSYS;
3330 } while(status < 0);
3334 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3336 struct nfs_server *server = NFS_SERVER(state->inode);
3337 struct nfs4_exception exception = { };
3340 err = nfs4_set_lock_state(state, fl);
3344 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3345 if (err != -NFS4ERR_DELAY)
3347 err = nfs4_handle_exception(server, err, &exception);
3348 } while (exception.retry);
3353 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3355 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3356 size_t buflen, int flags)
3358 struct inode *inode = dentry->d_inode;
3360 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3363 if (!S_ISREG(inode->i_mode) &&
3364 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3367 return nfs4_proc_set_acl(inode, buf, buflen);
3370 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3371 * and that's what we'll do for e.g. user attributes that haven't been set.
3372 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3373 * attributes in kernel-managed attribute namespaces. */
3374 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3377 struct inode *inode = dentry->d_inode;
3379 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3382 return nfs4_proc_get_acl(inode, buf, buflen);
3385 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3387 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3389 if (buf && buflen < len)
3392 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3396 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3397 .recover_open = nfs4_open_reclaim,
3398 .recover_lock = nfs4_lock_reclaim,
3401 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3402 .recover_open = nfs4_open_expired,
3403 .recover_lock = nfs4_lock_expired,
3406 static struct inode_operations nfs4_file_inode_operations = {
3407 .permission = nfs_permission,
3408 .getattr = nfs_getattr,
3409 .setattr = nfs_setattr,
3410 .getxattr = nfs4_getxattr,
3411 .setxattr = nfs4_setxattr,
3412 .listxattr = nfs4_listxattr,
3415 struct nfs_rpc_ops nfs_v4_clientops = {
3416 .version = 4, /* protocol version */
3417 .dentry_ops = &nfs4_dentry_operations,
3418 .dir_inode_ops = &nfs4_dir_inode_operations,
3419 .file_inode_ops = &nfs4_file_inode_operations,
3420 .getroot = nfs4_proc_get_root,
3421 .getattr = nfs4_proc_getattr,
3422 .setattr = nfs4_proc_setattr,
3423 .lookup = nfs4_proc_lookup,
3424 .access = nfs4_proc_access,
3425 .readlink = nfs4_proc_readlink,
3426 .read = nfs4_proc_read,
3427 .write = nfs4_proc_write,
3428 .commit = nfs4_proc_commit,
3429 .create = nfs4_proc_create,
3430 .remove = nfs4_proc_remove,
3431 .unlink_setup = nfs4_proc_unlink_setup,
3432 .unlink_done = nfs4_proc_unlink_done,
3433 .rename = nfs4_proc_rename,
3434 .link = nfs4_proc_link,
3435 .symlink = nfs4_proc_symlink,
3436 .mkdir = nfs4_proc_mkdir,
3437 .rmdir = nfs4_proc_remove,
3438 .readdir = nfs4_proc_readdir,
3439 .mknod = nfs4_proc_mknod,
3440 .statfs = nfs4_proc_statfs,
3441 .fsinfo = nfs4_proc_fsinfo,
3442 .pathconf = nfs4_proc_pathconf,
3443 .decode_dirent = nfs4_decode_dirent,
3444 .read_setup = nfs4_proc_read_setup,
3445 .write_setup = nfs4_proc_write_setup,
3446 .commit_setup = nfs4_proc_commit_setup,
3447 .file_open = nfs_open,
3448 .file_release = nfs_release,
3449 .lock = nfs4_proc_lock,
3450 .clear_acl_cache = nfs4_zap_acl_attr,