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/errno.h>
41 #include <linux/string.h>
42 #include <linux/sunrpc/clnt.h>
43 #include <linux/nfs.h>
44 #include <linux/nfs4.h>
45 #include <linux/nfs_fs.h>
46 #include <linux/nfs_page.h>
47 #include <linux/smp_lock.h>
48 #include <linux/namei.h>
50 #include "delegation.h"
52 #define NFSDBG_FACILITY NFSDBG_PROC
54 #define NFS4_POLL_RETRY_MIN (1*HZ)
55 #define NFS4_POLL_RETRY_MAX (15*HZ)
57 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
58 static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *);
59 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
60 extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
61 extern struct rpc_procinfo nfs4_procedures[];
63 extern nfs4_stateid zero_stateid;
65 /* Prevent leaks of NFSv4 errors into userland */
66 static inline int nfs4_map_errors(int err)
69 printk(KERN_WARNING "%s could not handle NFSv4 error %d\n",
77 * This is our standard bitmap for GETATTR requests.
79 const u32 nfs4_fattr_bitmap[2] = {
84 | FATTR4_WORD0_FILEID,
86 | FATTR4_WORD1_NUMLINKS
88 | FATTR4_WORD1_OWNER_GROUP
90 | FATTR4_WORD1_SPACE_USED
91 | FATTR4_WORD1_TIME_ACCESS
92 | FATTR4_WORD1_TIME_METADATA
93 | FATTR4_WORD1_TIME_MODIFY
96 const u32 nfs4_statfs_bitmap[2] = {
97 FATTR4_WORD0_FILES_AVAIL
98 | FATTR4_WORD0_FILES_FREE
99 | FATTR4_WORD0_FILES_TOTAL,
100 FATTR4_WORD1_SPACE_AVAIL
101 | FATTR4_WORD1_SPACE_FREE
102 | FATTR4_WORD1_SPACE_TOTAL
105 u32 nfs4_pathconf_bitmap[2] = {
107 | FATTR4_WORD0_MAXNAME,
111 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
112 | FATTR4_WORD0_MAXREAD
113 | FATTR4_WORD0_MAXWRITE
114 | FATTR4_WORD0_LEASE_TIME,
118 static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
119 struct nfs4_readdir_arg *readdir)
123 BUG_ON(readdir->count < 80);
125 readdir->cookie = (cookie > 2) ? cookie : 0;
126 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
131 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
136 * NFSv4 servers do not return entries for '.' and '..'
137 * Therefore, we fake these entries here. We let '.'
138 * have cookie 0 and '..' have cookie 1. Note that
139 * when talking to the server, we always send cookie 0
142 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
145 *p++ = xdr_one; /* next */
146 *p++ = xdr_zero; /* cookie, first word */
147 *p++ = xdr_one; /* cookie, second word */
148 *p++ = xdr_one; /* entry len */
149 memcpy(p, ".\0\0\0", 4); /* entry */
151 *p++ = xdr_one; /* bitmap length */
152 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
153 *p++ = htonl(8); /* attribute buffer length */
154 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
157 *p++ = xdr_one; /* next */
158 *p++ = xdr_zero; /* cookie, first word */
159 *p++ = xdr_two; /* cookie, second word */
160 *p++ = xdr_two; /* entry len */
161 memcpy(p, "..\0\0", 4); /* entry */
163 *p++ = xdr_one; /* bitmap length */
164 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
165 *p++ = htonl(8); /* attribute buffer length */
166 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
168 readdir->pgbase = (char *)p - (char *)start;
169 readdir->count -= readdir->pgbase;
170 kunmap_atomic(start, KM_USER0);
174 renew_lease(struct nfs_server *server, unsigned long timestamp)
176 struct nfs4_client *clp = server->nfs4_state;
177 spin_lock(&clp->cl_lock);
178 if (time_before(clp->cl_last_renewal,timestamp))
179 clp->cl_last_renewal = timestamp;
180 spin_unlock(&clp->cl_lock);
183 static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
185 struct nfs_inode *nfsi = NFS_I(inode);
187 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
188 nfsi->change_attr = cinfo->after;
193 * reclaim state on the server after a reboot.
194 * Assumes caller is holding the sp->so_sem
196 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
198 struct inode *inode = state->inode;
199 struct nfs_server *server = NFS_SERVER(inode);
200 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
201 struct nfs_openargs o_arg = {
203 .seqid = sp->so_seqid,
205 .open_flags = state->state,
206 .clientid = server->nfs4_state->cl_clientid,
207 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
208 .bitmask = server->attr_bitmask,
210 struct nfs_openres o_res = {
211 .server = server, /* Grrr */
213 struct rpc_message msg = {
214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
217 .rpc_cred = sp->so_cred,
221 if (delegation != NULL) {
222 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
223 memcpy(&state->stateid, &delegation->stateid,
224 sizeof(state->stateid));
225 set_bit(NFS_DELEGATED_STATE, &state->flags);
228 o_arg.u.delegation_type = delegation->type;
230 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
231 nfs4_increment_seqid(status, sp);
233 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
234 if (o_res.delegation_type != 0) {
235 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
236 /* Did the server issue an immediate delegation recall? */
238 nfs_async_inode_return_delegation(inode, &o_res.stateid);
241 clear_bit(NFS_DELEGATED_STATE, &state->flags);
242 /* Ensure we update the inode attributes */
247 int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
249 struct nfs_server *server = NFS_SERVER(state->inode);
250 struct nfs4_exception exception = { };
253 err = _nfs4_open_reclaim(sp, state);
256 case -NFS4ERR_STALE_CLIENTID:
257 case -NFS4ERR_STALE_STATEID:
258 case -NFS4ERR_EXPIRED:
261 err = nfs4_handle_exception(server, err, &exception);
262 } while (exception.retry);
266 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
268 struct nfs4_state_owner *sp = state->owner;
269 struct inode *inode = dentry->d_inode;
270 struct nfs_server *server = NFS_SERVER(inode);
271 struct dentry *parent = dget_parent(dentry);
272 struct nfs_openargs arg = {
273 .fh = NFS_FH(parent->d_inode),
274 .clientid = server->nfs4_state->cl_clientid,
275 .name = &dentry->d_name,
278 .bitmask = server->attr_bitmask,
279 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
281 struct nfs_openres res = {
284 struct rpc_message msg = {
285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
288 .rpc_cred = sp->so_cred,
293 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
295 if (state->state == 0)
297 arg.seqid = sp->so_seqid;
298 arg.open_flags = state->state;
299 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
300 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
301 nfs4_increment_seqid(status, sp);
303 memcpy(state->stateid.data, res.stateid.data,
304 sizeof(state->stateid.data));
305 clear_bit(NFS_DELEGATED_STATE, &state->flags);
313 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
315 struct nfs4_exception exception = { };
316 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
319 err = _nfs4_open_delegation_recall(dentry, state);
323 case -NFS4ERR_STALE_CLIENTID:
324 case -NFS4ERR_STALE_STATEID:
325 case -NFS4ERR_EXPIRED:
326 /* Don't recall a delegation if it was lost */
327 nfs4_schedule_state_recovery(server->nfs4_state);
330 err = nfs4_handle_exception(server, err, &exception);
331 } while (exception.retry);
335 static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid)
337 struct nfs_open_confirmargs arg = {
339 .seqid = sp->so_seqid,
342 struct nfs_open_confirmres res;
343 struct rpc_message msg = {
344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
347 .rpc_cred = sp->so_cred,
351 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
352 nfs4_increment_seqid(status, sp);
354 memcpy(stateid, &res.stateid, sizeof(*stateid));
358 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
360 struct nfs_access_entry cache;
363 status = nfs_access_get_cached(inode, cred, &cache);
367 /* Be clever: ask server to check for all possible rights */
368 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
370 cache.jiffies = jiffies;
371 status = _nfs4_proc_access(inode, &cache);
374 nfs_access_add_cache(inode, &cache);
376 if ((cache.mask & mask) == mask)
382 * Returns an nfs4_state + an extra reference to the inode
384 int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
386 struct nfs_delegation *delegation;
387 struct nfs_server *server = NFS_SERVER(inode);
388 struct nfs4_client *clp = server->nfs4_state;
389 struct nfs_inode *nfsi = NFS_I(inode);
390 struct nfs4_state_owner *sp = NULL;
391 struct nfs4_state *state = NULL;
392 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
396 /* Protect against reboot recovery - NOTE ORDER! */
397 down_read(&clp->cl_sem);
398 /* Protect against delegation recall */
399 down_read(&nfsi->rwsem);
400 delegation = NFS_I(inode)->delegation;
402 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
405 if (!(sp = nfs4_get_state_owner(server, cred))) {
406 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
410 state = nfs4_get_open_state(inode, sp);
415 if ((state->state & open_flags) == open_flags) {
416 spin_lock(&inode->i_lock);
417 if (open_flags & FMODE_READ)
419 if (open_flags & FMODE_WRITE)
421 spin_unlock(&inode->i_lock);
423 } else if (state->state != 0)
427 err = _nfs4_do_access(inode, cred, mask);
431 spin_lock(&inode->i_lock);
432 memcpy(state->stateid.data, delegation->stateid.data,
433 sizeof(state->stateid.data));
434 state->state |= open_flags;
435 if (open_flags & FMODE_READ)
437 if (open_flags & FMODE_WRITE)
439 set_bit(NFS_DELEGATED_STATE, &state->flags);
440 spin_unlock(&inode->i_lock);
443 nfs4_put_state_owner(sp);
444 up_read(&nfsi->rwsem);
445 up_read(&clp->cl_sem);
452 nfs4_put_open_state(state);
454 nfs4_put_state_owner(sp);
456 up_read(&nfsi->rwsem);
457 up_read(&clp->cl_sem);
461 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
463 struct nfs4_exception exception = { };
464 struct nfs4_state *res;
468 err = _nfs4_open_delegated(inode, flags, cred, &res);
471 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
473 } while (exception.retry);
478 * Returns an nfs4_state + an referenced inode
480 static int _nfs4_do_open(struct inode *dir, struct qstr *name, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
482 struct nfs4_state_owner *sp;
483 struct nfs4_state *state = NULL;
484 struct nfs_server *server = NFS_SERVER(dir);
485 struct nfs4_client *clp = server->nfs4_state;
486 struct inode *inode = NULL;
488 struct nfs_fattr f_attr = {
491 struct nfs_openargs o_arg = {
496 .bitmask = server->attr_bitmask,
497 .claim = NFS4_OPEN_CLAIM_NULL,
499 struct nfs_openres o_res = {
503 struct rpc_message msg = {
504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
510 /* Protect against reboot recovery conflicts */
511 down_read(&clp->cl_sem);
513 if (!(sp = nfs4_get_state_owner(server, cred))) {
514 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
517 if (flags & O_EXCL) {
518 u32 *p = (u32 *) o_arg.u.verifier.data;
522 o_arg.u.attrs = sattr;
523 /* Serialization for the sequence id */
525 o_arg.seqid = sp->so_seqid;
526 o_arg.id = sp->so_id;
527 o_arg.clientid = clp->cl_clientid,
529 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
530 nfs4_increment_seqid(status, sp);
533 update_changeattr(dir, &o_res.cinfo);
534 if(o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
535 status = _nfs4_proc_open_confirm(server->client, &o_res.fh,
540 if (!(f_attr.valid & NFS_ATTR_FATTR)) {
541 status = server->rpc_ops->getattr(server, &o_res.fh, &f_attr);
547 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
550 state = nfs4_get_open_state(inode, sp);
553 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
554 spin_lock(&inode->i_lock);
555 if (flags & FMODE_READ)
557 if (flags & FMODE_WRITE)
559 state->state |= flags & (FMODE_READ|FMODE_WRITE);
560 spin_unlock(&inode->i_lock);
561 if (o_res.delegation_type != 0)
562 nfs_inode_set_delegation(inode, cred, &o_res);
564 nfs4_put_state_owner(sp);
565 up_read(&clp->cl_sem);
571 nfs4_put_open_state(state);
573 nfs4_put_state_owner(sp);
575 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
576 up_read(&clp->cl_sem);
584 struct nfs4_state *nfs4_do_open(struct inode *dir, struct qstr *name, int flags, struct iattr *sattr, struct rpc_cred *cred)
586 struct nfs4_exception exception = { };
587 struct nfs4_state *res;
591 status = _nfs4_do_open(dir, name, flags, sattr, cred, &res);
594 /* NOTE: BAD_SEQID means the server and client disagree about the
595 * book-keeping w.r.t. state-changing operations
596 * (OPEN/CLOSE/LOCK/LOCKU...)
597 * It is actually a sign of a bug on the client or on the server.
599 * If we receive a BAD_SEQID error in the particular case of
600 * doing an OPEN, we assume that nfs4_increment_seqid() will
601 * have unhashed the old state_owner for us, and that we can
602 * therefore safely retry using a new one. We should still warn
605 if (status == -NFS4ERR_BAD_SEQID) {
606 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
610 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
611 status, &exception));
612 } while (exception.retry);
616 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
617 struct nfs_fh *fhandle, struct iattr *sattr,
618 struct nfs4_state *state)
620 struct nfs_setattrargs arg = {
624 .bitmask = server->attr_bitmask,
626 struct nfs_setattrres res = {
630 struct rpc_message msg = {
631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
638 if (sattr->ia_valid & ATTR_SIZE)
639 nfs4_copy_stateid(&arg.stateid, state, NULL);
641 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
643 return rpc_call_sync(server->client, &msg, 0);
646 int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
647 struct nfs_fh *fhandle, struct iattr *sattr,
648 struct nfs4_state *state)
650 struct nfs4_exception exception = { };
653 err = nfs4_handle_exception(server,
654 _nfs4_do_setattr(server, fattr, fhandle, sattr,
657 } while (exception.retry);
662 * It is possible for data to be read/written from a mem-mapped file
663 * after the sys_close call (which hits the vfs layer as a flush).
664 * This means that we can't safely call nfsv4 close on a file until
665 * the inode is cleared. This in turn means that we are not good
666 * NFSv4 citizens - we do not indicate to the server to update the file's
667 * share state even when we are done with one of the three share
668 * stateid's in the inode.
670 * NOTE: Caller must be holding the sp->so_owner semaphore!
672 static int _nfs4_do_close(struct inode *inode, struct nfs4_state *state)
674 struct nfs4_state_owner *sp = state->owner;
676 struct nfs_closeargs arg = {
679 struct nfs_closeres res;
680 struct rpc_message msg = {
681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
686 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
688 memcpy(&arg.stateid, &state->stateid, sizeof(arg.stateid));
689 /* Serialization for the sequence id */
690 arg.seqid = sp->so_seqid,
691 status = rpc_call_sync(NFS_SERVER(inode)->client, &msg, RPC_TASK_NOINTR);
693 /* hmm. we are done with the inode, and in the process of freeing
694 * the state_owner. we keep this around to process errors
696 nfs4_increment_seqid(status, sp);
698 memcpy(&state->stateid, &res.stateid, sizeof(state->stateid));
703 int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
705 struct nfs_server *server = NFS_SERVER(state->inode);
706 struct nfs4_exception exception = { };
709 err = _nfs4_do_close(inode, state);
711 case -NFS4ERR_STALE_STATEID:
712 case -NFS4ERR_EXPIRED:
713 nfs4_schedule_state_recovery(server->nfs4_state);
718 err = nfs4_handle_exception(server, err, &exception);
719 } while (exception.retry);
723 static int _nfs4_do_downgrade(struct inode *inode, struct nfs4_state *state, mode_t mode)
725 struct nfs4_state_owner *sp = state->owner;
727 struct nfs_closeargs arg = {
729 .seqid = sp->so_seqid,
732 struct nfs_closeres res;
733 struct rpc_message msg = {
734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE],
739 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
741 memcpy(&arg.stateid, &state->stateid, sizeof(arg.stateid));
742 status = rpc_call_sync(NFS_SERVER(inode)->client, &msg, RPC_TASK_NOINTR);
743 nfs4_increment_seqid(status, sp);
745 memcpy(&state->stateid, &res.stateid, sizeof(state->stateid));
750 int nfs4_do_downgrade(struct inode *inode, struct nfs4_state *state, mode_t mode)
752 struct nfs_server *server = NFS_SERVER(state->inode);
753 struct nfs4_exception exception = { };
756 err = _nfs4_do_downgrade(inode, state, mode);
758 case -NFS4ERR_STALE_STATEID:
759 case -NFS4ERR_EXPIRED:
760 nfs4_schedule_state_recovery(server->nfs4_state);
765 err = nfs4_handle_exception(server, err, &exception);
766 } while (exception.retry);
771 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
774 struct rpc_cred *cred;
775 struct nfs4_state *state;
777 if (nd->flags & LOOKUP_CREATE) {
778 attr.ia_mode = nd->intent.open.create_mode;
779 attr.ia_valid = ATTR_MODE;
780 if (!IS_POSIXACL(dir))
781 attr.ia_mode &= ~current->fs->umask;
784 BUG_ON(nd->intent.open.flags & O_CREAT);
787 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
788 state = nfs4_do_open(dir, &dentry->d_name, nd->intent.open.flags, &attr, cred);
791 return (struct inode *)state;
796 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
798 struct rpc_cred *cred;
799 struct nfs4_state *state;
802 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
803 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
805 state = nfs4_do_open(dir, &dentry->d_name, openflags, NULL, cred);
807 if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
811 inode = state->inode;
812 if (inode == dentry->d_inode) {
817 nfs4_close_state(state, openflags);
823 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
825 struct nfs4_server_caps_res res = {};
826 struct rpc_message msg = {
827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
833 status = rpc_call_sync(server->client, &msg, 0);
835 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
836 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
837 server->caps |= NFS_CAP_ACLS;
838 if (res.has_links != 0)
839 server->caps |= NFS_CAP_HARDLINKS;
840 if (res.has_symlinks != 0)
841 server->caps |= NFS_CAP_SYMLINKS;
842 server->acl_bitmask = res.acl_bitmask;
847 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
849 struct nfs4_exception exception = { };
852 err = nfs4_handle_exception(server,
853 _nfs4_server_capabilities(server, fhandle),
855 } while (exception.retry);
859 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
860 struct nfs_fsinfo *info)
862 struct nfs_fattr * fattr = info->fattr;
863 struct nfs4_lookup_root_arg args = {
864 .bitmask = nfs4_fattr_bitmap,
866 struct nfs4_lookup_res res = {
871 struct rpc_message msg = {
872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
877 return rpc_call_sync(server->client, &msg, 0);
880 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
881 struct nfs_fsinfo *info)
883 struct nfs4_exception exception = { };
886 err = nfs4_handle_exception(server,
887 _nfs4_lookup_root(server, fhandle, info),
889 } while (exception.retry);
893 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
894 struct nfs_fsinfo *info)
896 struct nfs_fattr * fattr = info->fattr;
899 struct nfs4_lookup_arg args = {
902 .bitmask = nfs4_fattr_bitmap,
904 struct nfs4_lookup_res res = {
909 struct rpc_message msg = {
910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
917 * Now we do a separate LOOKUP for each component of the mount path.
918 * The LOOKUPs are done separately so that we can conveniently
919 * catch an ERR_WRONGSEC if it occurs along the way...
921 status = nfs4_lookup_root(server, fhandle, info);
925 p = server->mnt_path;
927 struct nfs4_exception exception = { };
934 while (*p && (*p != '/'))
940 status = nfs4_handle_exception(server,
941 rpc_call_sync(server->client, &msg, 0),
943 } while (exception.retry);
946 if (status == -ENOENT) {
947 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
948 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
953 status = nfs4_server_capabilities(server, fhandle);
955 status = nfs4_do_fsinfo(server, fhandle, info);
960 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
962 struct nfs4_getattr_arg args = {
964 .bitmask = server->attr_bitmask,
966 struct nfs4_getattr_res res = {
970 struct rpc_message msg = {
971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
977 return rpc_call_sync(server->client, &msg, 0);
980 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
982 struct nfs4_exception exception = { };
985 err = nfs4_handle_exception(server,
986 _nfs4_proc_getattr(server, fhandle, fattr),
988 } while (exception.retry);
993 * The file is not closed if it is opened due to the a request to change
994 * the size of the file. The open call will not be needed once the
995 * VFS layer lookup-intents are implemented.
997 * Close is called when the inode is destroyed.
998 * If we haven't opened the file for O_WRONLY, we
999 * need to in the size_change case to obtain a stateid.
1002 * Because OPEN is always done by name in nfsv4, it is
1003 * possible that we opened a different file by the same
1004 * name. We can recognize this race condition, but we
1005 * can't do anything about it besides returning an error.
1007 * This will be fixed with VFS changes (lookup-intent).
1010 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1011 struct iattr *sattr)
1013 struct inode * inode = dentry->d_inode;
1014 int size_change = sattr->ia_valid & ATTR_SIZE;
1015 struct nfs4_state *state = NULL;
1022 struct rpc_cred *cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1023 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1024 if (state == NULL) {
1025 state = nfs4_open_delegated(dentry->d_inode,
1028 state = nfs4_do_open(dentry->d_parent->d_inode,
1029 &dentry->d_name, FMODE_WRITE,
1035 return PTR_ERR(state);
1037 if (state->inode != inode) {
1038 printk(KERN_WARNING "nfs: raced in setattr (%p != %p), returning -EIO\n", inode, state->inode);
1043 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1044 NFS_FH(inode), sattr, state);
1047 inode = state->inode;
1048 nfs4_close_state(state, FMODE_WRITE);
1055 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1056 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1059 struct nfs_server *server = NFS_SERVER(dir);
1060 struct nfs4_lookup_arg args = {
1061 .bitmask = server->attr_bitmask,
1062 .dir_fh = NFS_FH(dir),
1065 struct nfs4_lookup_res res = {
1070 struct rpc_message msg = {
1071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1078 dprintk("NFS call lookup %s\n", name->name);
1079 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1080 dprintk("NFS reply lookup: %d\n", status);
1084 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1086 struct nfs4_exception exception = { };
1089 err = nfs4_handle_exception(NFS_SERVER(dir),
1090 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1092 } while (exception.retry);
1096 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1098 struct nfs4_accessargs args = {
1099 .fh = NFS_FH(inode),
1101 struct nfs4_accessres res = { 0 };
1102 struct rpc_message msg = {
1103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1106 .rpc_cred = entry->cred,
1108 int mode = entry->mask;
1112 * Determine which access bits we want to ask for...
1114 if (mode & MAY_READ)
1115 args.access |= NFS4_ACCESS_READ;
1116 if (S_ISDIR(inode->i_mode)) {
1117 if (mode & MAY_WRITE)
1118 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1119 if (mode & MAY_EXEC)
1120 args.access |= NFS4_ACCESS_LOOKUP;
1122 if (mode & MAY_WRITE)
1123 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1124 if (mode & MAY_EXEC)
1125 args.access |= NFS4_ACCESS_EXECUTE;
1127 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1130 if (res.access & NFS4_ACCESS_READ)
1131 entry->mask |= MAY_READ;
1132 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1133 entry->mask |= MAY_WRITE;
1134 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1135 entry->mask |= MAY_EXEC;
1140 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1142 struct nfs4_exception exception = { };
1145 err = nfs4_handle_exception(NFS_SERVER(inode),
1146 _nfs4_proc_access(inode, entry),
1148 } while (exception.retry);
1153 * TODO: For the time being, we don't try to get any attributes
1154 * along with any of the zero-copy operations READ, READDIR,
1157 * In the case of the first three, we want to put the GETATTR
1158 * after the read-type operation -- this is because it is hard
1159 * to predict the length of a GETATTR response in v4, and thus
1160 * align the READ data correctly. This means that the GETATTR
1161 * may end up partially falling into the page cache, and we should
1162 * shift it into the 'tail' of the xdr_buf before processing.
1163 * To do this efficiently, we need to know the total length
1164 * of data received, which doesn't seem to be available outside
1167 * In the case of WRITE, we also want to put the GETATTR after
1168 * the operation -- in this case because we want to make sure
1169 * we get the post-operation mtime and size. This means that
1170 * we can't use xdr_encode_pages() as written: we need a variant
1171 * of it which would leave room in the 'tail' iovec.
1173 * Both of these changes to the XDR layer would in fact be quite
1174 * minor, but I decided to leave them for a subsequent patch.
1176 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1177 unsigned int pgbase, unsigned int pglen)
1179 struct nfs4_readlink args = {
1180 .fh = NFS_FH(inode),
1185 struct rpc_message msg = {
1186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1191 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1194 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1195 unsigned int pgbase, unsigned int pglen)
1197 struct nfs4_exception exception = { };
1200 err = nfs4_handle_exception(NFS_SERVER(inode),
1201 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1203 } while (exception.retry);
1207 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1209 int flags = rdata->flags;
1210 struct inode *inode = rdata->inode;
1211 struct nfs_fattr *fattr = rdata->res.fattr;
1212 struct nfs_server *server = NFS_SERVER(inode);
1213 struct rpc_message msg = {
1214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1215 .rpc_argp = &rdata->args,
1216 .rpc_resp = &rdata->res,
1217 .rpc_cred = rdata->cred,
1219 unsigned long timestamp = jiffies;
1222 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1223 (long long) rdata->args.offset);
1226 status = rpc_call_sync(server->client, &msg, flags);
1228 renew_lease(server, timestamp);
1229 dprintk("NFS reply read: %d\n", status);
1233 static int nfs4_proc_read(struct nfs_read_data *rdata)
1235 struct nfs4_exception exception = { };
1238 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1239 _nfs4_proc_read(rdata),
1241 } while (exception.retry);
1245 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1247 int rpcflags = wdata->flags;
1248 struct inode *inode = wdata->inode;
1249 struct nfs_fattr *fattr = wdata->res.fattr;
1250 struct nfs_server *server = NFS_SERVER(inode);
1251 struct rpc_message msg = {
1252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1253 .rpc_argp = &wdata->args,
1254 .rpc_resp = &wdata->res,
1255 .rpc_cred = wdata->cred,
1259 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1260 (long long) wdata->args.offset);
1263 status = rpc_call_sync(server->client, &msg, rpcflags);
1264 dprintk("NFS reply write: %d\n", status);
1268 static int nfs4_proc_write(struct nfs_write_data *wdata)
1270 struct nfs4_exception exception = { };
1273 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1274 _nfs4_proc_write(wdata),
1276 } while (exception.retry);
1280 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1282 struct inode *inode = cdata->inode;
1283 struct nfs_fattr *fattr = cdata->res.fattr;
1284 struct nfs_server *server = NFS_SERVER(inode);
1285 struct rpc_message msg = {
1286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1287 .rpc_argp = &cdata->args,
1288 .rpc_resp = &cdata->res,
1289 .rpc_cred = cdata->cred,
1293 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1294 (long long) cdata->args.offset);
1297 status = rpc_call_sync(server->client, &msg, 0);
1298 dprintk("NFS reply commit: %d\n", status);
1302 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1304 struct nfs4_exception exception = { };
1307 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1308 _nfs4_proc_commit(cdata),
1310 } while (exception.retry);
1316 * We will need to arrange for the VFS layer to provide an atomic open.
1317 * Until then, this create/open method is prone to inefficiency and race
1318 * conditions due to the lookup, create, and open VFS calls from sys_open()
1319 * placed on the wire.
1321 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1322 * The file will be opened again in the subsequent VFS open call
1323 * (nfs4_proc_file_open).
1325 * The open for read will just hang around to be used by any process that
1326 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1329 static struct inode *
1330 nfs4_proc_create(struct inode *dir, struct qstr *name, struct iattr *sattr,
1333 struct inode *inode;
1334 struct nfs4_state *state = NULL;
1335 struct rpc_cred *cred;
1337 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1338 state = nfs4_do_open(dir, name, flags, sattr, cred);
1340 if (!IS_ERR(state)) {
1341 inode = state->inode;
1342 if (flags & O_EXCL) {
1343 struct nfs_fattr fattr;
1345 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1346 NFS_FH(inode), sattr, state);
1348 nfs4_close_state(state, flags);
1350 inode = ERR_PTR(status);
1354 inode = (struct inode *)state;
1358 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1360 struct nfs4_remove_arg args = {
1364 struct nfs4_change_info res;
1365 struct rpc_message msg = {
1366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1372 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1374 update_changeattr(dir, &res);
1378 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1380 struct nfs4_exception exception = { };
1383 err = nfs4_handle_exception(NFS_SERVER(dir),
1384 _nfs4_proc_remove(dir, name),
1386 } while (exception.retry);
1390 struct unlink_desc {
1391 struct nfs4_remove_arg args;
1392 struct nfs4_change_info res;
1395 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1398 struct unlink_desc *up;
1400 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1404 up->args.fh = NFS_FH(dir->d_inode);
1405 up->args.name = name;
1407 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1408 msg->rpc_argp = &up->args;
1409 msg->rpc_resp = &up->res;
1413 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1415 struct rpc_message *msg = &task->tk_msg;
1416 struct unlink_desc *up;
1418 if (msg->rpc_resp != NULL) {
1419 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1420 update_changeattr(dir->d_inode, &up->res);
1422 msg->rpc_resp = NULL;
1423 msg->rpc_argp = NULL;
1428 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1429 struct inode *new_dir, struct qstr *new_name)
1431 struct nfs4_rename_arg arg = {
1432 .old_dir = NFS_FH(old_dir),
1433 .new_dir = NFS_FH(new_dir),
1434 .old_name = old_name,
1435 .new_name = new_name,
1437 struct nfs4_rename_res res = { };
1438 struct rpc_message msg = {
1439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1445 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1448 update_changeattr(old_dir, &res.old_cinfo);
1449 update_changeattr(new_dir, &res.new_cinfo);
1454 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1455 struct inode *new_dir, struct qstr *new_name)
1457 struct nfs4_exception exception = { };
1460 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1461 _nfs4_proc_rename(old_dir, old_name,
1464 } while (exception.retry);
1468 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1470 struct nfs4_link_arg arg = {
1471 .fh = NFS_FH(inode),
1472 .dir_fh = NFS_FH(dir),
1475 struct nfs4_change_info cinfo = { };
1476 struct rpc_message msg = {
1477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1483 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1485 update_changeattr(dir, &cinfo);
1490 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1492 struct nfs4_exception exception = { };
1495 err = nfs4_handle_exception(NFS_SERVER(inode),
1496 _nfs4_proc_link(inode, dir, name),
1498 } while (exception.retry);
1502 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1503 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1504 struct nfs_fattr *fattr)
1506 struct nfs_server *server = NFS_SERVER(dir);
1507 struct nfs4_create_arg arg = {
1508 .dir_fh = NFS_FH(dir),
1513 .bitmask = server->attr_bitmask,
1515 struct nfs4_create_res res = {
1520 struct rpc_message msg = {
1521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1527 if (path->len > NFS4_MAXPATHLEN)
1528 return -ENAMETOOLONG;
1529 arg.u.symlink = path;
1532 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1534 update_changeattr(dir, &res.dir_cinfo);
1538 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1539 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1540 struct nfs_fattr *fattr)
1542 struct nfs4_exception exception = { };
1545 err = nfs4_handle_exception(NFS_SERVER(dir),
1546 _nfs4_proc_symlink(dir, name, path, sattr,
1549 } while (exception.retry);
1553 static int _nfs4_proc_mkdir(struct inode *dir, struct qstr *name,
1554 struct iattr *sattr, struct nfs_fh *fhandle,
1555 struct nfs_fattr *fattr)
1557 struct nfs_server *server = NFS_SERVER(dir);
1558 struct nfs4_create_arg arg = {
1559 .dir_fh = NFS_FH(dir),
1564 .bitmask = server->attr_bitmask,
1566 struct nfs4_create_res res = {
1571 struct rpc_message msg = {
1572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1580 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1582 update_changeattr(dir, &res.dir_cinfo);
1586 static int nfs4_proc_mkdir(struct inode *dir, struct qstr *name,
1587 struct iattr *sattr, struct nfs_fh *fhandle,
1588 struct nfs_fattr *fattr)
1590 struct nfs4_exception exception = { };
1593 err = nfs4_handle_exception(NFS_SERVER(dir),
1594 _nfs4_proc_mkdir(dir, name, sattr,
1597 } while (exception.retry);
1601 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1602 u64 cookie, struct page *page, unsigned int count, int plus)
1604 struct inode *dir = dentry->d_inode;
1605 struct nfs4_readdir_arg args = {
1611 struct nfs4_readdir_res res;
1612 struct rpc_message msg = {
1613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1621 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1622 res.pgbase = args.pgbase;
1623 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1625 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1630 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1631 u64 cookie, struct page *page, unsigned int count, int plus)
1633 struct nfs4_exception exception = { };
1636 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1637 _nfs4_proc_readdir(dentry, cred, cookie,
1640 } while (exception.retry);
1644 static int _nfs4_proc_mknod(struct inode *dir, struct qstr *name,
1645 struct iattr *sattr, dev_t rdev, struct nfs_fh *fh,
1646 struct nfs_fattr *fattr)
1648 struct nfs_server *server = NFS_SERVER(dir);
1649 struct nfs4_create_arg arg = {
1650 .dir_fh = NFS_FH(dir),
1654 .bitmask = server->attr_bitmask,
1656 struct nfs4_create_res res = {
1661 struct rpc_message msg = {
1662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1667 int mode = sattr->ia_mode;
1671 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1672 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1674 arg.ftype = NF4FIFO;
1675 else if (S_ISBLK(mode)) {
1677 arg.u.device.specdata1 = MAJOR(rdev);
1678 arg.u.device.specdata2 = MINOR(rdev);
1680 else if (S_ISCHR(mode)) {
1682 arg.u.device.specdata1 = MAJOR(rdev);
1683 arg.u.device.specdata2 = MINOR(rdev);
1686 arg.ftype = NF4SOCK;
1688 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1690 update_changeattr(dir, &res.dir_cinfo);
1694 static int nfs4_proc_mknod(struct inode *dir, struct qstr *name,
1695 struct iattr *sattr, dev_t rdev, struct nfs_fh *fh,
1696 struct nfs_fattr *fattr)
1698 struct nfs4_exception exception = { };
1701 err = nfs4_handle_exception(NFS_SERVER(dir),
1702 _nfs4_proc_mknod(dir, name, sattr, rdev,
1705 } while (exception.retry);
1709 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1710 struct nfs_fsstat *fsstat)
1712 struct nfs4_statfs_arg args = {
1714 .bitmask = server->attr_bitmask,
1716 struct rpc_message msg = {
1717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1722 fsstat->fattr->valid = 0;
1723 return rpc_call_sync(server->client, &msg, 0);
1726 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1728 struct nfs4_exception exception = { };
1731 err = nfs4_handle_exception(server,
1732 _nfs4_proc_statfs(server, fhandle, fsstat),
1734 } while (exception.retry);
1738 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1739 struct nfs_fsinfo *fsinfo)
1741 struct nfs4_fsinfo_arg args = {
1743 .bitmask = server->attr_bitmask,
1745 struct rpc_message msg = {
1746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1751 return rpc_call_sync(server->client, &msg, 0);
1754 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1756 struct nfs4_exception exception = { };
1760 err = nfs4_handle_exception(server,
1761 _nfs4_do_fsinfo(server, fhandle, fsinfo),
1763 } while (exception.retry);
1767 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1769 fsinfo->fattr->valid = 0;
1770 return nfs4_do_fsinfo(server, fhandle, fsinfo);
1773 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1774 struct nfs_pathconf *pathconf)
1776 struct nfs4_pathconf_arg args = {
1778 .bitmask = server->attr_bitmask,
1780 struct rpc_message msg = {
1781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1783 .rpc_resp = pathconf,
1786 /* None of the pathconf attributes are mandatory to implement */
1787 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
1788 memset(pathconf, 0, sizeof(*pathconf));
1792 pathconf->fattr->valid = 0;
1793 return rpc_call_sync(server->client, &msg, 0);
1796 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1797 struct nfs_pathconf *pathconf)
1799 struct nfs4_exception exception = { };
1803 err = nfs4_handle_exception(server,
1804 _nfs4_proc_pathconf(server, fhandle, pathconf),
1806 } while (exception.retry);
1811 nfs4_read_done(struct rpc_task *task)
1813 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
1814 struct inode *inode = data->inode;
1816 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1817 rpc_restart_call(task);
1820 if (task->tk_status > 0)
1821 renew_lease(NFS_SERVER(inode), data->timestamp);
1822 /* Call back common NFS readpage processing */
1823 nfs_readpage_result(task);
1827 nfs4_proc_read_setup(struct nfs_read_data *data)
1829 struct rpc_task *task = &data->task;
1830 struct rpc_message msg = {
1831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1832 .rpc_argp = &data->args,
1833 .rpc_resp = &data->res,
1834 .rpc_cred = data->cred,
1836 struct inode *inode = data->inode;
1839 data->timestamp = jiffies;
1841 /* N.B. Do we need to test? Never called for swapfile inode */
1842 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
1844 /* Finalize the task. */
1845 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
1846 rpc_call_setup(task, &msg, 0);
1850 nfs4_write_done(struct rpc_task *task)
1852 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1853 struct inode *inode = data->inode;
1855 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1856 rpc_restart_call(task);
1859 if (task->tk_status >= 0)
1860 renew_lease(NFS_SERVER(inode), data->timestamp);
1861 /* Call back common NFS writeback processing */
1862 nfs_writeback_done(task);
1866 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
1868 struct rpc_task *task = &data->task;
1869 struct rpc_message msg = {
1870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1871 .rpc_argp = &data->args,
1872 .rpc_resp = &data->res,
1873 .rpc_cred = data->cred,
1875 struct inode *inode = data->inode;
1879 if (how & FLUSH_STABLE) {
1880 if (!NFS_I(inode)->ncommit)
1881 stable = NFS_FILE_SYNC;
1883 stable = NFS_DATA_SYNC;
1885 stable = NFS_UNSTABLE;
1886 data->args.stable = stable;
1888 data->timestamp = jiffies;
1890 /* Set the initial flags for the task. */
1891 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1893 /* Finalize the task. */
1894 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
1895 rpc_call_setup(task, &msg, 0);
1899 nfs4_commit_done(struct rpc_task *task)
1901 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1902 struct inode *inode = data->inode;
1904 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1905 rpc_restart_call(task);
1908 /* Call back common NFS writeback processing */
1909 nfs_commit_done(task);
1913 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
1915 struct rpc_task *task = &data->task;
1916 struct rpc_message msg = {
1917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1918 .rpc_argp = &data->args,
1919 .rpc_resp = &data->res,
1920 .rpc_cred = data->cred,
1922 struct inode *inode = data->inode;
1925 /* Set the initial flags for the task. */
1926 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1928 /* Finalize the task. */
1929 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
1930 rpc_call_setup(task, &msg, 0);
1934 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
1935 * standalone procedure for queueing an asynchronous RENEW.
1938 renew_done(struct rpc_task *task)
1940 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
1941 unsigned long timestamp = (unsigned long)task->tk_calldata;
1943 if (task->tk_status < 0) {
1944 switch (task->tk_status) {
1945 case -NFS4ERR_STALE_CLIENTID:
1946 case -NFS4ERR_EXPIRED:
1947 case -NFS4ERR_CB_PATH_DOWN:
1948 nfs4_schedule_state_recovery(clp);
1952 spin_lock(&clp->cl_lock);
1953 if (time_before(clp->cl_last_renewal,timestamp))
1954 clp->cl_last_renewal = timestamp;
1955 spin_unlock(&clp->cl_lock);
1959 nfs4_proc_async_renew(struct nfs4_client *clp)
1961 struct rpc_message msg = {
1962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
1964 .rpc_cred = clp->cl_cred,
1967 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
1968 renew_done, (void *)jiffies);
1972 nfs4_proc_renew(struct nfs4_client *clp)
1974 struct rpc_message msg = {
1975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
1977 .rpc_cred = clp->cl_cred,
1979 unsigned long now = jiffies;
1982 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
1985 spin_lock(&clp->cl_lock);
1986 if (time_before(clp->cl_last_renewal,now))
1987 clp->cl_last_renewal = now;
1988 spin_unlock(&clp->cl_lock);
1993 * We will need to arrange for the VFS layer to provide an atomic open.
1994 * Until then, this open method is prone to inefficiency and race conditions
1995 * due to the lookup, potential create, and open VFS calls from sys_open()
1996 * placed on the wire.
1999 nfs4_proc_file_open(struct inode *inode, struct file *filp)
2001 struct dentry *dentry = filp->f_dentry;
2002 struct nfs_open_context *ctx;
2003 struct nfs4_state *state = NULL;
2004 struct rpc_cred *cred;
2005 int status = -ENOMEM;
2007 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2008 (int)dentry->d_parent->d_name.len,
2009 dentry->d_parent->d_name.name,
2010 (int)dentry->d_name.len, dentry->d_name.name);
2013 /* Find our open stateid */
2014 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
2015 if (unlikely(cred == NULL))
2017 ctx = alloc_nfs_open_context(dentry, cred);
2019 if (unlikely(ctx == NULL))
2021 status = -EIO; /* ERACE actually */
2022 state = nfs4_find_state(inode, cred, filp->f_mode);
2023 if (unlikely(state == NULL))
2026 nfs4_close_state(state, filp->f_mode);
2027 ctx->mode = filp->f_mode;
2028 nfs_file_set_open_context(filp, ctx);
2029 put_nfs_open_context(ctx);
2030 if (filp->f_mode & FMODE_WRITE)
2031 nfs_begin_data_update(inode);
2034 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
2035 put_nfs_open_context(ctx);
2043 nfs4_proc_file_release(struct inode *inode, struct file *filp)
2045 if (filp->f_mode & FMODE_WRITE)
2046 nfs_end_data_update(inode);
2047 nfs_file_clear_open_context(filp);
2052 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
2054 struct nfs4_client *clp = server->nfs4_state;
2056 if (!clp || task->tk_status >= 0)
2058 switch(task->tk_status) {
2059 case -NFS4ERR_STALE_CLIENTID:
2060 case -NFS4ERR_STALE_STATEID:
2061 case -NFS4ERR_EXPIRED:
2062 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2063 nfs4_schedule_state_recovery(clp);
2064 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2065 rpc_wake_up_task(task);
2066 task->tk_status = 0;
2068 case -NFS4ERR_GRACE:
2069 case -NFS4ERR_DELAY:
2070 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2071 task->tk_status = 0;
2073 case -NFS4ERR_OLD_STATEID:
2074 task->tk_status = 0;
2077 task->tk_status = nfs4_map_errors(task->tk_status);
2081 int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2085 int interruptible, res = 0;
2089 rpc_clnt_sigmask(clnt, &oldset);
2090 interruptible = TASK_UNINTERRUPTIBLE;
2092 interruptible = TASK_INTERRUPTIBLE;
2093 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2094 nfs4_schedule_state_recovery(clp);
2095 if (clnt->cl_intr && signalled())
2097 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2099 finish_wait(&clp->cl_waitq, &wait);
2100 rpc_clnt_sigunmask(clnt, &oldset);
2104 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2112 *timeout = NFS4_POLL_RETRY_MIN;
2113 if (*timeout > NFS4_POLL_RETRY_MAX)
2114 *timeout = NFS4_POLL_RETRY_MAX;
2115 rpc_clnt_sigmask(clnt, &oldset);
2116 if (clnt->cl_intr) {
2117 set_current_state(TASK_INTERRUPTIBLE);
2118 schedule_timeout(*timeout);
2122 set_current_state(TASK_UNINTERRUPTIBLE);
2123 schedule_timeout(*timeout);
2125 rpc_clnt_sigunmask(clnt, &oldset);
2130 /* This is the error handling routine for processes that are allowed
2133 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2135 struct nfs4_client *clp = server->nfs4_state;
2136 int ret = errorcode;
2138 exception->retry = 0;
2142 case -NFS4ERR_STALE_CLIENTID:
2143 case -NFS4ERR_STALE_STATEID:
2144 case -NFS4ERR_EXPIRED:
2145 ret = nfs4_wait_clnt_recover(server->client, clp);
2147 exception->retry = 1;
2149 case -NFS4ERR_GRACE:
2150 case -NFS4ERR_DELAY:
2151 ret = nfs4_delay(server->client, &exception->timeout);
2153 exception->retry = 1;
2155 case -NFS4ERR_OLD_STATEID:
2157 exception->retry = 1;
2159 /* We failed to handle the error */
2160 return nfs4_map_errors(ret);
2163 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2165 static nfs4_verifier sc_verifier;
2166 static int initialized;
2168 struct nfs4_setclientid setclientid = {
2169 .sc_verifier = &sc_verifier,
2172 struct rpc_message msg = {
2173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2174 .rpc_argp = &setclientid,
2176 .rpc_cred = clp->cl_cred,
2180 struct timespec boot_time;
2184 boot_time = CURRENT_TIME;
2185 p = (u32*)sc_verifier.data;
2186 *p++ = htonl((u32)boot_time.tv_sec);
2187 *p = htonl((u32)boot_time.tv_nsec);
2189 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2190 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u",
2191 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr));
2192 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2193 sizeof(setclientid.sc_netid), "tcp");
2194 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2195 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2196 clp->cl_ipaddr, port >> 8, port & 255);
2198 return rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2202 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2204 struct nfs_fsinfo fsinfo;
2205 struct rpc_message msg = {
2206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2208 .rpc_resp = &fsinfo,
2209 .rpc_cred = clp->cl_cred,
2215 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2217 spin_lock(&clp->cl_lock);
2218 clp->cl_lease_time = fsinfo.lease_time * HZ;
2219 clp->cl_last_renewal = now;
2220 spin_unlock(&clp->cl_lock);
2225 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2227 struct nfs4_delegreturnargs args = {
2228 .fhandle = NFS_FH(inode),
2231 struct rpc_message msg = {
2232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2237 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2240 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2242 struct nfs_server *server = NFS_SERVER(inode);
2243 struct nfs4_exception exception = { };
2246 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2248 case -NFS4ERR_STALE_STATEID:
2249 case -NFS4ERR_EXPIRED:
2250 nfs4_schedule_state_recovery(server->nfs4_state);
2254 err = nfs4_handle_exception(server, err, &exception);
2255 } while (exception.retry);
2259 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2260 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2263 * sleep, with exponential backoff, and retry the LOCK operation.
2265 static unsigned long
2266 nfs4_set_lock_task_retry(unsigned long timeout)
2268 current->state = TASK_INTERRUPTIBLE;
2269 schedule_timeout(timeout);
2271 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2272 return NFS4_LOCK_MAXTIMEOUT;
2277 nfs4_lck_type(int cmd, struct file_lock *request)
2280 switch (request->fl_type) {
2282 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2284 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2286 return NFS4_WRITE_LT;
2292 static inline uint64_t
2293 nfs4_lck_length(struct file_lock *request)
2295 if (request->fl_end == OFFSET_MAX)
2296 return ~(uint64_t)0;
2297 return request->fl_end - request->fl_start + 1;
2300 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2302 struct inode *inode = state->inode;
2303 struct nfs_server *server = NFS_SERVER(inode);
2304 struct nfs4_client *clp = server->nfs4_state;
2305 struct nfs_lockargs arg = {
2306 .fh = NFS_FH(inode),
2307 .type = nfs4_lck_type(cmd, request),
2308 .offset = request->fl_start,
2309 .length = nfs4_lck_length(request),
2311 struct nfs_lockres res = {
2314 struct rpc_message msg = {
2315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2318 .rpc_cred = state->owner->so_cred,
2320 struct nfs_lowner nlo;
2321 struct nfs4_lock_state *lsp;
2324 down_read(&clp->cl_sem);
2325 nlo.clientid = clp->cl_clientid;
2326 down(&state->lock_sema);
2327 lsp = nfs4_find_lock_state(state, request->fl_owner);
2329 nlo.id = lsp->ls_id;
2331 spin_lock(&clp->cl_lock);
2332 nlo.id = nfs4_alloc_lockowner_id(clp);
2333 spin_unlock(&clp->cl_lock);
2336 status = rpc_call_sync(server->client, &msg, 0);
2338 request->fl_type = F_UNLCK;
2339 } else if (status == -NFS4ERR_DENIED) {
2340 int64_t len, start, end;
2341 start = res.u.denied.offset;
2342 len = res.u.denied.length;
2343 end = start + len - 1;
2344 if (end < 0 || len == 0)
2345 request->fl_end = OFFSET_MAX;
2347 request->fl_end = (loff_t)end;
2348 request->fl_start = (loff_t)start;
2349 request->fl_type = F_WRLCK;
2350 if (res.u.denied.type & 1)
2351 request->fl_type = F_RDLCK;
2352 request->fl_pid = 0;
2356 nfs4_put_lock_state(lsp);
2357 up(&state->lock_sema);
2358 up_read(&clp->cl_sem);
2362 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2364 struct nfs4_exception exception = { };
2368 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2369 _nfs4_proc_getlk(state, cmd, request),
2371 } while (exception.retry);
2375 static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2377 struct inode *inode = state->inode;
2378 struct nfs_server *server = NFS_SERVER(inode);
2379 struct nfs4_client *clp = server->nfs4_state;
2380 struct nfs_lockargs arg = {
2381 .fh = NFS_FH(inode),
2382 .type = nfs4_lck_type(cmd, request),
2383 .offset = request->fl_start,
2384 .length = nfs4_lck_length(request),
2386 struct nfs_lockres res = {
2389 struct rpc_message msg = {
2390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2393 .rpc_cred = state->owner->so_cred,
2395 struct nfs4_lock_state *lsp;
2396 struct nfs_locku_opargs luargs;
2399 down_read(&clp->cl_sem);
2400 down(&state->lock_sema);
2401 lsp = nfs4_find_lock_state(state, request->fl_owner);
2404 /* We might have lost the locks! */
2405 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) {
2406 luargs.seqid = lsp->ls_seqid;
2407 memcpy(&luargs.stateid, &lsp->ls_stateid, sizeof(luargs.stateid));
2408 arg.u.locku = &luargs;
2409 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2410 nfs4_increment_lock_seqid(status, lsp);
2414 memcpy(&lsp->ls_stateid, &res.u.stateid,
2415 sizeof(lsp->ls_stateid));
2416 nfs4_notify_unlck(state, request, lsp);
2418 nfs4_put_lock_state(lsp);
2420 up(&state->lock_sema);
2422 posix_lock_file(request->fl_file, request);
2423 up_read(&clp->cl_sem);
2427 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2429 struct nfs4_exception exception = { };
2433 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2434 _nfs4_proc_unlck(state, cmd, request),
2436 } while (exception.retry);
2440 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2442 struct inode *inode = state->inode;
2443 struct nfs_server *server = NFS_SERVER(inode);
2444 struct nfs4_lock_state *lsp;
2445 struct nfs_lockargs arg = {
2446 .fh = NFS_FH(inode),
2447 .type = nfs4_lck_type(cmd, request),
2448 .offset = request->fl_start,
2449 .length = nfs4_lck_length(request),
2451 struct nfs_lockres res = {
2454 struct rpc_message msg = {
2455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2458 .rpc_cred = state->owner->so_cred,
2460 struct nfs_lock_opargs largs = {
2462 .new_lock_owner = 0,
2466 lsp = nfs4_get_lock_state(state, request->fl_owner);
2469 if (!(lsp->ls_flags & NFS_LOCK_INITIALIZED)) {
2470 struct nfs4_state_owner *owner = state->owner;
2471 struct nfs_open_to_lock otl = {
2473 .clientid = server->nfs4_state->cl_clientid,
2477 otl.lock_seqid = lsp->ls_seqid;
2478 otl.lock_owner.id = lsp->ls_id;
2479 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
2480 largs.u.open_lock = &otl;
2481 largs.new_lock_owner = 1;
2482 arg.u.lock = &largs;
2483 down(&owner->so_sema);
2484 otl.open_seqid = owner->so_seqid;
2485 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2486 /* increment open_owner seqid on success, and
2487 * seqid mutating errors */
2488 nfs4_increment_seqid(status, owner);
2489 up(&owner->so_sema);
2491 struct nfs_exist_lock el = {
2492 .seqid = lsp->ls_seqid,
2494 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
2495 largs.u.exist_lock = ⪙
2496 largs.new_lock_owner = 0;
2497 arg.u.lock = &largs;
2498 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2500 /* increment seqid on success, and * seqid mutating errors*/
2501 nfs4_increment_lock_seqid(status, lsp);
2502 /* save the returned stateid. */
2504 memcpy(&lsp->ls_stateid, &res.u.stateid, sizeof(nfs4_stateid));
2505 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2507 nfs4_notify_setlk(state, request, lsp);
2508 } else if (status == -NFS4ERR_DENIED)
2510 nfs4_put_lock_state(lsp);
2514 int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2516 return _nfs4_do_setlk(state, F_SETLK, request, 1);
2519 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2521 struct nfs4_client *clp = state->owner->so_client;
2524 down_read(&clp->cl_sem);
2525 down(&state->lock_sema);
2526 status = _nfs4_do_setlk(state, cmd, request, 0);
2527 up(&state->lock_sema);
2529 /* Note: we always want to sleep here! */
2530 request->fl_flags |= FL_SLEEP;
2531 if (posix_lock_file_wait(request->fl_file, request) < 0)
2532 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
2534 up_read(&clp->cl_sem);
2538 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2540 struct nfs4_exception exception = { };
2544 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2545 _nfs4_proc_setlk(state, cmd, request),
2547 } while (exception.retry);
2552 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
2554 struct nfs_open_context *ctx;
2555 struct nfs4_state *state;
2556 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
2559 /* verify open state */
2560 ctx = (struct nfs_open_context *)filp->private_data;
2563 if (request->fl_start < 0 || request->fl_end < 0)
2567 return nfs4_proc_getlk(state, F_GETLK, request);
2569 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
2572 if (request->fl_type == F_UNLCK)
2573 return nfs4_proc_unlck(state, cmd, request);
2576 status = nfs4_proc_setlk(state, cmd, request);
2577 if ((status != -EAGAIN) || IS_SETLK(cmd))
2579 timeout = nfs4_set_lock_task_retry(timeout);
2580 status = -ERESTARTSYS;
2583 } while(status < 0);
2588 struct nfs_rpc_ops nfs_v4_clientops = {
2589 .version = 4, /* protocol version */
2590 .dentry_ops = &nfs4_dentry_operations,
2591 .dir_inode_ops = &nfs4_dir_inode_operations,
2592 .getroot = nfs4_proc_get_root,
2593 .getattr = nfs4_proc_getattr,
2594 .setattr = nfs4_proc_setattr,
2595 .lookup = nfs4_proc_lookup,
2596 .access = nfs4_proc_access,
2597 .readlink = nfs4_proc_readlink,
2598 .read = nfs4_proc_read,
2599 .write = nfs4_proc_write,
2600 .commit = nfs4_proc_commit,
2601 .create = nfs4_proc_create,
2602 .remove = nfs4_proc_remove,
2603 .unlink_setup = nfs4_proc_unlink_setup,
2604 .unlink_done = nfs4_proc_unlink_done,
2605 .rename = nfs4_proc_rename,
2606 .link = nfs4_proc_link,
2607 .symlink = nfs4_proc_symlink,
2608 .mkdir = nfs4_proc_mkdir,
2609 .rmdir = nfs4_proc_remove,
2610 .readdir = nfs4_proc_readdir,
2611 .mknod = nfs4_proc_mknod,
2612 .statfs = nfs4_proc_statfs,
2613 .fsinfo = nfs4_proc_fsinfo,
2614 .pathconf = nfs4_proc_pathconf,
2615 .decode_dirent = nfs4_decode_dirent,
2616 .read_setup = nfs4_proc_read_setup,
2617 .write_setup = nfs4_proc_write_setup,
2618 .commit_setup = nfs4_proc_commit_setup,
2619 .file_open = nfs4_proc_file_open,
2620 .file_release = nfs4_proc_file_release,
2621 .lock = nfs4_proc_lock,