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 #define NFSDBG_FACILITY NFSDBG_PROC
52 #define NFS4_POLL_RETRY_TIME (15*HZ)
54 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
55 static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *);
56 extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
57 extern struct rpc_procinfo nfs4_procedures[];
59 extern nfs4_stateid zero_stateid;
61 /* Prevent leaks of NFSv4 errors into userland */
62 static inline int nfs4_map_errors(int err)
65 printk(KERN_WARNING "%s could not handle NFSv4 error %d\n",
73 * This is our standard bitmap for GETATTR requests.
75 const u32 nfs4_fattr_bitmap[2] = {
80 | FATTR4_WORD0_FILEID,
82 | FATTR4_WORD1_NUMLINKS
84 | FATTR4_WORD1_OWNER_GROUP
86 | FATTR4_WORD1_SPACE_USED
87 | FATTR4_WORD1_TIME_ACCESS
88 | FATTR4_WORD1_TIME_METADATA
89 | FATTR4_WORD1_TIME_MODIFY
92 const u32 nfs4_statfs_bitmap[2] = {
93 FATTR4_WORD0_FILES_AVAIL
94 | FATTR4_WORD0_FILES_FREE
95 | FATTR4_WORD0_FILES_TOTAL,
96 FATTR4_WORD1_SPACE_AVAIL
97 | FATTR4_WORD1_SPACE_FREE
98 | FATTR4_WORD1_SPACE_TOTAL
101 u32 nfs4_pathconf_bitmap[2] = {
103 | FATTR4_WORD0_MAXNAME,
107 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
108 | FATTR4_WORD0_MAXREAD
109 | FATTR4_WORD0_MAXWRITE
110 | FATTR4_WORD0_LEASE_TIME,
114 static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
115 struct nfs4_readdir_arg *readdir)
119 BUG_ON(readdir->count < 80);
121 readdir->cookie = (cookie > 2) ? cookie : 0;
122 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
127 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
132 * NFSv4 servers do not return entries for '.' and '..'
133 * Therefore, we fake these entries here. We let '.'
134 * have cookie 0 and '..' have cookie 1. Note that
135 * when talking to the server, we always send cookie 0
138 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
141 *p++ = xdr_one; /* next */
142 *p++ = xdr_zero; /* cookie, first word */
143 *p++ = xdr_one; /* cookie, second word */
144 *p++ = xdr_one; /* entry len */
145 memcpy(p, ".\0\0\0", 4); /* entry */
147 *p++ = xdr_one; /* bitmap length */
148 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
149 *p++ = htonl(8); /* attribute buffer length */
150 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
153 *p++ = xdr_one; /* next */
154 *p++ = xdr_zero; /* cookie, first word */
155 *p++ = xdr_two; /* cookie, second word */
156 *p++ = xdr_two; /* entry len */
157 memcpy(p, "..\0\0", 4); /* entry */
159 *p++ = xdr_one; /* bitmap length */
160 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
161 *p++ = htonl(8); /* attribute buffer length */
162 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
164 readdir->pgbase = (char *)p - (char *)start;
165 readdir->count -= readdir->pgbase;
166 kunmap_atomic(start, KM_USER0);
170 renew_lease(struct nfs_server *server, unsigned long timestamp)
172 struct nfs4_client *clp = server->nfs4_state;
173 spin_lock(&clp->cl_lock);
174 if (time_before(clp->cl_last_renewal,timestamp))
175 clp->cl_last_renewal = timestamp;
176 spin_unlock(&clp->cl_lock);
179 static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
181 struct nfs_inode *nfsi = NFS_I(inode);
183 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
184 nfsi->change_attr = cinfo->after;
189 * reclaim state on the server after a reboot.
190 * Assumes caller is holding the sp->so_sem
193 nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
195 struct inode *inode = state->inode;
196 struct nfs_server *server = NFS_SERVER(inode);
197 struct nfs_fattr fattr = {
200 struct nfs_open_reclaimargs o_arg = {
202 .seqid = sp->so_seqid,
204 .share_access = state->state,
205 .clientid = server->nfs4_state->cl_clientid,
206 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
207 .bitmask = server->attr_bitmask,
209 struct nfs_openres o_res = {
211 .server = server, /* Grrr */
213 struct rpc_message msg = {
214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_RECLAIM],
217 .rpc_cred = sp->so_cred,
221 status = rpc_call_sync(server->client, &msg, 0);
222 nfs4_increment_seqid(status, sp);
224 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
225 /* Update the inode attributes */
226 nfs_refresh_inode(inode, &fattr);
231 * Returns an nfs4_state + an referenced inode
234 nfs4_do_open(struct inode *dir, struct qstr *name, int flags, struct iattr *sattr, struct rpc_cred *cred)
236 struct nfs4_state_owner *sp;
237 struct nfs4_state *state = NULL;
238 struct nfs_server *server = NFS_SERVER(dir);
239 struct inode *inode = NULL;
241 struct nfs_fattr f_attr = {
244 struct nfs_openargs o_arg = {
246 .share_access = flags & (FMODE_READ|FMODE_WRITE),
247 .opentype = (flags & O_CREAT) ? NFS4_OPEN_CREATE : NFS4_OPEN_NOCREATE,
248 .createmode = (flags & O_EXCL) ? NFS4_CREATE_EXCLUSIVE : NFS4_CREATE_UNCHECKED,
251 .bitmask = server->attr_bitmask,
253 struct nfs_openres o_res = {
257 struct rpc_message msg = {
258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
266 if (!(sp = nfs4_get_state_owner(NFS_SERVER(dir), cred))) {
267 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
270 if (o_arg.createmode & NFS4_CREATE_EXCLUSIVE){
271 u32 *p = (u32 *) o_arg.u.verifier.data;
274 } else if (o_arg.createmode == NFS4_CREATE_UNCHECKED) {
275 o_arg.u.attrs = sattr;
277 /* Serialization for the sequence id */
279 o_arg.seqid = sp->so_seqid;
280 o_arg.id = sp->so_id;
281 o_arg.clientid = NFS_SERVER(dir)->nfs4_state->cl_clientid,
283 status = rpc_call_sync(server->client, &msg, 0);
284 nfs4_increment_seqid(status, sp);
287 update_changeattr(dir, &o_res.cinfo);
290 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
293 state = nfs4_get_open_state(inode, sp);
297 if(o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
298 struct nfs_open_confirmargs oc_arg = {
300 .seqid = sp->so_seqid,
302 struct nfs_open_confirmres oc_res;
303 struct rpc_message msg = {
304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
310 memcpy(&oc_arg.stateid, &o_res.stateid, sizeof(oc_arg.stateid));
311 status = rpc_call_sync(server->client, &msg, 0);
312 nfs4_increment_seqid(status, sp);
315 memcpy(&state->stateid, &oc_res.stateid, sizeof(state->stateid));
317 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
318 spin_lock(&inode->i_lock);
319 if (flags & FMODE_READ)
321 if (flags & FMODE_WRITE)
323 state->state |= flags & (FMODE_READ|FMODE_WRITE);
324 spin_unlock(&inode->i_lock);
327 nfs4_put_state_owner(sp);
332 nfs4_put_state_owner(sp);
334 nfs4_put_open_state(state);
341 /* NOTE: BAD_SEQID means the server and client disagree about the
342 * book-keeping w.r.t. state-changing operations
343 * (OPEN/CLOSE/LOCK/LOCKU...)
344 * It is actually a sign of a bug on the client or on the server.
346 * If we receive a BAD_SEQID error in the particular case of
347 * doing an OPEN, we assume that nfs4_increment_seqid() will
348 * have unhashed the old state_owner for us, and that we can
349 * therefore safely retry using a new one. We should still warn
352 if (status == -NFS4ERR_BAD_SEQID) {
353 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
356 status = nfs4_handle_error(server, status);
359 BUG_ON(status < -1000 || status > 0);
361 return ERR_PTR(status);
365 nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
366 struct nfs_fh *fhandle, struct iattr *sattr,
367 struct nfs4_state *state)
369 struct nfs_setattrargs arg = {
373 .bitmask = server->attr_bitmask,
375 struct nfs_setattrres res = {
379 struct rpc_message msg = {
380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
389 if (sattr->ia_valid & ATTR_SIZE)
390 nfs4_copy_stateid(&arg.stateid, state, 0);
392 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
394 status = rpc_call_sync(server->client, &msg, 0);
396 status = nfs4_handle_error(server, status);
404 * It is possible for data to be read/written from a mem-mapped file
405 * after the sys_close call (which hits the vfs layer as a flush).
406 * This means that we can't safely call nfsv4 close on a file until
407 * the inode is cleared. This in turn means that we are not good
408 * NFSv4 citizens - we do not indicate to the server to update the file's
409 * share state even when we are done with one of the three share
410 * stateid's in the inode.
412 * NOTE: Caller must be holding the sp->so_owner semaphore!
415 nfs4_do_close(struct inode *inode, struct nfs4_state *state)
417 struct nfs4_state_owner *sp = state->owner;
419 struct nfs_closeargs arg = {
422 struct nfs_closeres res;
423 struct rpc_message msg = {
424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
429 memcpy(&arg.stateid, &state->stateid, sizeof(arg.stateid));
430 /* Serialization for the sequence id */
431 arg.seqid = sp->so_seqid,
432 status = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
434 /* hmm. we are done with the inode, and in the process of freeing
435 * the state_owner. we keep this around to process errors
437 nfs4_increment_seqid(status, sp);
439 memcpy(&state->stateid, &res.stateid, sizeof(state->stateid));
445 nfs4_do_downgrade(struct inode *inode, struct nfs4_state *state, mode_t mode)
447 struct nfs4_state_owner *sp = state->owner;
449 struct nfs_closeargs arg = {
451 .seqid = sp->so_seqid,
452 .share_access = mode,
454 struct nfs_closeres res;
455 struct rpc_message msg = {
456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE],
461 memcpy(&arg.stateid, &state->stateid, sizeof(arg.stateid));
462 status = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
463 nfs4_increment_seqid(status, sp);
465 memcpy(&state->stateid, &res.stateid, sizeof(state->stateid));
471 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
474 struct rpc_cred *cred;
475 struct nfs4_state *state;
477 if (nd->flags & LOOKUP_CREATE) {
478 attr.ia_mode = nd->intent.open.create_mode;
479 attr.ia_valid = ATTR_MODE;
480 if (!IS_POSIXACL(dir))
481 attr.ia_mode &= ~current->fs->umask;
484 BUG_ON(nd->intent.open.flags & O_CREAT);
487 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
488 state = nfs4_do_open(dir, &dentry->d_name, nd->intent.open.flags, &attr, cred);
491 return (struct inode *)state;
496 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
498 struct rpc_cred *cred;
499 struct nfs4_state *state;
502 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
503 state = nfs4_do_open(dir, &dentry->d_name, openflags, NULL, cred);
505 if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
509 inode = state->inode;
510 if (inode == dentry->d_inode) {
515 nfs4_close_state(state, openflags);
521 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
523 struct nfs4_server_caps_res res = {};
524 struct rpc_message msg = {
525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
531 status = rpc_call_sync(server->client, &msg, 0);
533 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
534 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
535 server->caps |= NFS_CAP_ACLS;
536 if (res.has_links != 0)
537 server->caps |= NFS_CAP_HARDLINKS;
538 if (res.has_symlinks != 0)
539 server->caps |= NFS_CAP_SYMLINKS;
540 server->acl_bitmask = res.acl_bitmask;
545 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
546 struct nfs_fsinfo *info)
548 struct nfs_fattr * fattr = info->fattr;
549 struct nfs4_lookup_root_arg args = {
550 .bitmask = nfs4_fattr_bitmap,
552 struct nfs4_lookup_res res = {
557 struct rpc_message msg = {
558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
563 return rpc_call_sync(server->client, &msg, 0);
566 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
567 struct nfs_fsinfo *info)
569 struct nfs_fattr * fattr = info->fattr;
572 struct nfs4_lookup_arg args = {
575 .bitmask = nfs4_fattr_bitmap,
577 struct nfs4_lookup_res res = {
582 struct rpc_message msg = {
583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
590 * Now we do a separate LOOKUP for each component of the mount path.
591 * The LOOKUPs are done separately so that we can conveniently
592 * catch an ERR_WRONGSEC if it occurs along the way...
594 status = nfs4_lookup_root(server, fhandle, info);
598 p = server->mnt_path;
605 while (*p && (*p != '/'))
610 status = rpc_call_sync(server->client, &msg, 0);
613 if (status == -ENOENT) {
614 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
615 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
620 status = nfs4_server_capabilities(server, fhandle);
622 status = nfs4_do_fsinfo(server, fhandle, info);
624 return nfs4_map_errors(status);
627 static int nfs4_proc_getattr(struct inode *inode, struct nfs_fattr *fattr)
629 struct nfs_server *server = NFS_SERVER(inode);
630 struct nfs4_getattr_arg args = {
632 .bitmask = server->attr_bitmask,
634 struct nfs4_getattr_res res = {
638 struct rpc_message msg = {
639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
646 return nfs4_map_errors(rpc_call_sync(NFS_CLIENT(inode), &msg, 0));
650 * The file is not closed if it is opened due to the a request to change
651 * the size of the file. The open call will not be needed once the
652 * VFS layer lookup-intents are implemented.
654 * Close is called when the inode is destroyed.
655 * If we haven't opened the file for O_WRONLY, we
656 * need to in the size_change case to obtain a stateid.
659 * Because OPEN is always done by name in nfsv4, it is
660 * possible that we opened a different file by the same
661 * name. We can recognize this race condition, but we
662 * can't do anything about it besides returning an error.
664 * This will be fixed with VFS changes (lookup-intent).
667 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
670 struct inode * inode = dentry->d_inode;
671 int size_change = sattr->ia_valid & ATTR_SIZE;
672 struct nfs4_state *state = NULL;
679 struct rpc_cred *cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
680 state = nfs4_find_state(inode, cred, FMODE_WRITE);
682 state = nfs4_do_open(dentry->d_parent->d_inode,
683 &dentry->d_name, FMODE_WRITE, NULL, cred);
688 return PTR_ERR(state);
690 if (state->inode != inode) {
691 printk(KERN_WARNING "nfs: raced in setattr (%p != %p), returning -EIO\n", inode, state->inode);
696 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
697 NFS_FH(inode), sattr, state);
700 inode = state->inode;
701 nfs4_close_state(state, FMODE_WRITE);
708 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
709 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
712 struct nfs_server *server = NFS_SERVER(dir);
713 struct nfs4_lookup_arg args = {
714 .bitmask = server->attr_bitmask,
715 .dir_fh = NFS_FH(dir),
718 struct nfs4_lookup_res res = {
723 struct rpc_message msg = {
724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
731 dprintk("NFS call lookup %s\n", name->name);
732 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
733 dprintk("NFS reply lookup: %d\n", status);
734 return nfs4_map_errors(status);
737 static int nfs4_proc_access(struct inode *inode, struct rpc_cred *cred, int mode)
740 struct nfs4_accessargs args = {
743 struct nfs4_accessres res = { 0 };
744 struct rpc_message msg = {
745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
752 * Determine which access bits we want to ask for...
755 args.access |= NFS4_ACCESS_READ;
756 if (S_ISDIR(inode->i_mode)) {
757 if (mode & MAY_WRITE)
758 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
760 args.access |= NFS4_ACCESS_LOOKUP;
763 if (mode & MAY_WRITE)
764 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
766 args.access |= NFS4_ACCESS_EXECUTE;
768 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
770 if (args.access != res.supported) {
771 printk(KERN_NOTICE "NFS: server didn't support all access bits!\n");
773 } else if ((args.access & res.access) != args.access)
776 return nfs4_map_errors(status);
780 * TODO: For the time being, we don't try to get any attributes
781 * along with any of the zero-copy operations READ, READDIR,
784 * In the case of the first three, we want to put the GETATTR
785 * after the read-type operation -- this is because it is hard
786 * to predict the length of a GETATTR response in v4, and thus
787 * align the READ data correctly. This means that the GETATTR
788 * may end up partially falling into the page cache, and we should
789 * shift it into the 'tail' of the xdr_buf before processing.
790 * To do this efficiently, we need to know the total length
791 * of data received, which doesn't seem to be available outside
794 * In the case of WRITE, we also want to put the GETATTR after
795 * the operation -- in this case because we want to make sure
796 * we get the post-operation mtime and size. This means that
797 * we can't use xdr_encode_pages() as written: we need a variant
798 * of it which would leave room in the 'tail' iovec.
800 * Both of these changes to the XDR layer would in fact be quite
801 * minor, but I decided to leave them for a subsequent patch.
803 static int nfs4_proc_readlink(struct inode *inode, struct page *page)
805 struct nfs4_readlink args = {
807 .count = PAGE_CACHE_SIZE,
810 struct rpc_message msg = {
811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
816 return nfs4_map_errors(rpc_call_sync(NFS_CLIENT(inode), &msg, 0));
820 nfs4_proc_read(struct nfs_read_data *rdata, struct file *filp)
822 int flags = rdata->flags;
823 struct inode *inode = rdata->inode;
824 struct nfs_fattr *fattr = rdata->res.fattr;
825 struct nfs_server *server = NFS_SERVER(inode);
826 struct rpc_message msg = {
827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
828 .rpc_argp = &rdata->args,
829 .rpc_resp = &rdata->res,
831 unsigned long timestamp = jiffies;
834 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
835 (long long) rdata->args.offset);
838 * Try first to use O_RDONLY, then O_RDWR stateid.
841 struct nfs4_state *state;
842 state = (struct nfs4_state *)filp->private_data;
843 rdata->args.state = state;
844 msg.rpc_cred = state->owner->so_cred;
846 rdata->args.state = NULL;
847 msg.rpc_cred = NFS_I(inode)->mm_cred;
851 status = rpc_call_sync(server->client, &msg, flags);
853 renew_lease(server, timestamp);
854 dprintk("NFS reply read: %d\n", status);
855 return nfs4_map_errors(status);
859 nfs4_proc_write(struct nfs_write_data *wdata, struct file *filp)
861 int rpcflags = wdata->flags;
862 struct inode *inode = wdata->inode;
863 struct nfs_fattr *fattr = wdata->res.fattr;
864 struct nfs_server *server = NFS_SERVER(inode);
865 struct rpc_message msg = {
866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
867 .rpc_argp = &wdata->args,
868 .rpc_resp = &wdata->res,
872 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
873 (long long) wdata->args.offset);
876 * Try first to use O_WRONLY, then O_RDWR stateid.
879 struct nfs4_state *state;
880 state = (struct nfs4_state *)filp->private_data;
881 wdata->args.state = state;
882 msg.rpc_cred = state->owner->so_cred;
884 wdata->args.state = NULL;
885 msg.rpc_cred = NFS_I(inode)->mm_cred;
889 status = rpc_call_sync(server->client, &msg, rpcflags);
890 dprintk("NFS reply write: %d\n", status);
891 return nfs4_map_errors(status);
895 nfs4_proc_commit(struct nfs_write_data *cdata, struct file *filp)
897 struct inode *inode = cdata->inode;
898 struct nfs_fattr *fattr = cdata->res.fattr;
899 struct nfs_server *server = NFS_SERVER(inode);
900 struct rpc_message msg = {
901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
902 .rpc_argp = &cdata->args,
903 .rpc_resp = &cdata->res,
907 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
908 (long long) cdata->args.offset);
911 * Try first to use O_WRONLY, then O_RDWR stateid.
914 msg.rpc_cred = ((struct nfs4_state *)filp->private_data)->owner->so_cred;
916 msg.rpc_cred = NFS_I(inode)->mm_cred;
919 status = rpc_call_sync(server->client, &msg, 0);
920 dprintk("NFS reply commit: %d\n", status);
921 return nfs4_map_errors(status);
926 * We will need to arrange for the VFS layer to provide an atomic open.
927 * Until then, this create/open method is prone to inefficiency and race
928 * conditions due to the lookup, create, and open VFS calls from sys_open()
929 * placed on the wire.
931 * Given the above sorry state of affairs, I'm simply sending an OPEN.
932 * The file will be opened again in the subsequent VFS open call
933 * (nfs4_proc_file_open).
935 * The open for read will just hang around to be used by any process that
936 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
939 static struct inode *
940 nfs4_proc_create(struct inode *dir, struct qstr *name, struct iattr *sattr,
944 struct nfs4_state *state = NULL;
945 struct rpc_cred *cred;
947 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
948 state = nfs4_do_open(dir, name, flags, sattr, cred);
950 if (!IS_ERR(state)) {
951 inode = state->inode;
952 if (flags & O_EXCL) {
953 struct nfs_fattr fattr;
955 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
956 NFS_FH(inode), sattr, state);
958 nfs4_close_state(state, flags);
960 inode = ERR_PTR(status);
964 inode = (struct inode *)state;
968 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
970 struct nfs4_remove_arg args = {
974 struct nfs4_change_info res;
975 struct rpc_message msg = {
976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
982 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
984 update_changeattr(dir, &res);
985 return nfs4_map_errors(status);
989 struct nfs4_remove_arg args;
990 struct nfs4_change_info res;
993 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
996 struct unlink_desc *up;
998 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1002 up->args.fh = NFS_FH(dir->d_inode);
1003 up->args.name = name;
1005 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1006 msg->rpc_argp = &up->args;
1007 msg->rpc_resp = &up->res;
1011 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1013 struct rpc_message *msg = &task->tk_msg;
1014 struct unlink_desc *up;
1016 if (msg->rpc_resp != NULL) {
1017 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1018 update_changeattr(dir->d_inode, &up->res);
1020 msg->rpc_resp = NULL;
1021 msg->rpc_argp = NULL;
1026 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1027 struct inode *new_dir, struct qstr *new_name)
1029 struct nfs4_rename_arg arg = {
1030 .old_dir = NFS_FH(old_dir),
1031 .new_dir = NFS_FH(new_dir),
1032 .old_name = old_name,
1033 .new_name = new_name,
1035 struct nfs4_rename_res res = { };
1036 struct rpc_message msg = {
1037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1043 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1046 update_changeattr(old_dir, &res.old_cinfo);
1047 update_changeattr(new_dir, &res.new_cinfo);
1049 return nfs4_map_errors(status);
1052 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1054 struct nfs4_link_arg arg = {
1055 .fh = NFS_FH(inode),
1056 .dir_fh = NFS_FH(dir),
1059 struct nfs4_change_info cinfo = { };
1060 struct rpc_message msg = {
1061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1067 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1069 update_changeattr(dir, &cinfo);
1071 return nfs4_map_errors(status);
1074 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1075 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1076 struct nfs_fattr *fattr)
1078 struct nfs_server *server = NFS_SERVER(dir);
1079 struct nfs4_create_arg arg = {
1080 .dir_fh = NFS_FH(dir),
1085 .bitmask = server->attr_bitmask,
1087 struct nfs4_create_res res = {
1092 struct rpc_message msg = {
1093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1099 arg.u.symlink = path;
1102 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1104 update_changeattr(dir, &res.dir_cinfo);
1105 return nfs4_map_errors(status);
1108 static int nfs4_proc_mkdir(struct inode *dir, struct qstr *name,
1109 struct iattr *sattr, struct nfs_fh *fhandle,
1110 struct nfs_fattr *fattr)
1112 struct nfs_server *server = NFS_SERVER(dir);
1113 struct nfs4_create_arg arg = {
1114 .dir_fh = NFS_FH(dir),
1119 .bitmask = server->attr_bitmask,
1121 struct nfs4_create_res res = {
1126 struct rpc_message msg = {
1127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1135 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1137 update_changeattr(dir, &res.dir_cinfo);
1138 return nfs4_map_errors(status);
1141 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1142 u64 cookie, struct page *page, unsigned int count, int plus)
1144 struct inode *dir = dentry->d_inode;
1145 struct nfs4_readdir_arg args = {
1151 struct nfs4_readdir_res res;
1152 struct rpc_message msg = {
1153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1161 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1162 res.pgbase = args.pgbase;
1163 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1165 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1167 return nfs4_map_errors(status);
1170 static int nfs4_proc_mknod(struct inode *dir, struct qstr *name,
1171 struct iattr *sattr, dev_t rdev, struct nfs_fh *fh,
1172 struct nfs_fattr *fattr)
1174 struct nfs_server *server = NFS_SERVER(dir);
1175 struct nfs4_create_arg arg = {
1176 .dir_fh = NFS_FH(dir),
1180 .bitmask = server->attr_bitmask,
1182 struct nfs4_create_res res = {
1187 struct rpc_message msg = {
1188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1193 int mode = sattr->ia_mode;
1197 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1198 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1200 arg.ftype = NF4FIFO;
1201 else if (S_ISBLK(mode)) {
1203 arg.u.device.specdata1 = MAJOR(rdev);
1204 arg.u.device.specdata2 = MINOR(rdev);
1206 else if (S_ISCHR(mode)) {
1208 arg.u.device.specdata1 = MAJOR(rdev);
1209 arg.u.device.specdata2 = MINOR(rdev);
1212 arg.ftype = NF4SOCK;
1214 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1216 update_changeattr(dir, &res.dir_cinfo);
1217 return nfs4_map_errors(status);
1220 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1221 struct nfs_fsstat *fsstat)
1223 struct nfs4_statfs_arg args = {
1225 .bitmask = server->attr_bitmask,
1227 struct rpc_message msg = {
1228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1233 fsstat->fattr->valid = 0;
1234 return nfs4_map_errors(rpc_call_sync(server->client, &msg, 0));
1237 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1238 struct nfs_fsinfo *fsinfo)
1240 struct nfs4_fsinfo_arg args = {
1242 .bitmask = server->attr_bitmask,
1244 struct rpc_message msg = {
1245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1250 return nfs4_map_errors(rpc_call_sync(server->client, &msg, 0));
1253 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1255 fsinfo->fattr->valid = 0;
1256 return nfs4_map_errors(nfs4_do_fsinfo(server, fhandle, fsinfo));
1259 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1260 struct nfs_pathconf *pathconf)
1262 struct nfs4_pathconf_arg args = {
1264 .bitmask = server->attr_bitmask,
1266 struct rpc_message msg = {
1267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1269 .rpc_resp = pathconf,
1272 /* None of the pathconf attributes are mandatory to implement */
1273 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
1274 memset(pathconf, 0, sizeof(*pathconf));
1278 pathconf->fattr->valid = 0;
1279 return nfs4_map_errors(rpc_call_sync(server->client, &msg, 0));
1283 nfs4_read_done(struct rpc_task *task)
1285 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
1286 struct inode *inode = data->inode;
1288 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1289 rpc_restart_call(task);
1292 if (task->tk_status > 0)
1293 renew_lease(NFS_SERVER(inode), data->timestamp);
1294 /* Call back common NFS readpage processing */
1295 nfs_readpage_result(task);
1299 nfs4_proc_read_setup(struct nfs_read_data *data)
1301 struct rpc_task *task = &data->task;
1302 struct rpc_message msg = {
1303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1304 .rpc_argp = &data->args,
1305 .rpc_resp = &data->res,
1306 .rpc_cred = data->cred,
1308 struct inode *inode = data->inode;
1311 data->timestamp = jiffies;
1313 /* N.B. Do we need to test? Never called for swapfile inode */
1314 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
1316 /* Finalize the task. */
1317 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
1318 rpc_call_setup(task, &msg, 0);
1322 nfs4_write_done(struct rpc_task *task)
1324 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1325 struct inode *inode = data->inode;
1327 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1328 rpc_restart_call(task);
1331 if (task->tk_status >= 0)
1332 renew_lease(NFS_SERVER(inode), data->timestamp);
1333 /* Call back common NFS writeback processing */
1334 nfs_writeback_done(task);
1338 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
1340 struct rpc_task *task = &data->task;
1341 struct rpc_message msg = {
1342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1343 .rpc_argp = &data->args,
1344 .rpc_resp = &data->res,
1345 .rpc_cred = data->cred,
1347 struct inode *inode = data->inode;
1351 if (how & FLUSH_STABLE) {
1352 if (!NFS_I(inode)->ncommit)
1353 stable = NFS_FILE_SYNC;
1355 stable = NFS_DATA_SYNC;
1357 stable = NFS_UNSTABLE;
1358 data->args.stable = stable;
1360 data->timestamp = jiffies;
1362 /* Set the initial flags for the task. */
1363 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1365 /* Finalize the task. */
1366 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
1367 rpc_call_setup(task, &msg, 0);
1371 nfs4_commit_done(struct rpc_task *task)
1373 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1374 struct inode *inode = data->inode;
1376 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1377 rpc_restart_call(task);
1380 /* Call back common NFS writeback processing */
1381 nfs_commit_done(task);
1385 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
1387 struct rpc_task *task = &data->task;
1388 struct rpc_message msg = {
1389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1390 .rpc_argp = &data->args,
1391 .rpc_resp = &data->res,
1392 .rpc_cred = data->cred,
1394 struct inode *inode = data->inode;
1397 /* Set the initial flags for the task. */
1398 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1400 /* Finalize the task. */
1401 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
1402 rpc_call_setup(task, &msg, 0);
1406 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
1407 * standalone procedure for queueing an asynchronous RENEW.
1410 renew_done(struct rpc_task *task)
1412 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
1413 unsigned long timestamp = (unsigned long)task->tk_calldata;
1415 if (task->tk_status < 0) {
1416 switch (task->tk_status) {
1417 case -NFS4ERR_STALE_CLIENTID:
1418 nfs4_schedule_state_recovery(clp);
1422 spin_lock(&clp->cl_lock);
1423 if (time_before(clp->cl_last_renewal,timestamp))
1424 clp->cl_last_renewal = timestamp;
1425 spin_unlock(&clp->cl_lock);
1429 nfs4_proc_async_renew(struct nfs4_client *clp)
1431 struct rpc_message msg = {
1432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
1434 .rpc_cred = clp->cl_cred,
1437 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
1438 renew_done, (void *)jiffies);
1442 nfs4_proc_renew(struct nfs4_client *clp)
1444 struct rpc_message msg = {
1445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
1447 .rpc_cred = clp->cl_cred,
1449 unsigned long now = jiffies;
1452 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
1453 spin_lock(&clp->cl_lock);
1454 if (time_before(clp->cl_last_renewal,now))
1455 clp->cl_last_renewal = now;
1456 spin_unlock(&clp->cl_lock);
1461 * We will need to arrange for the VFS layer to provide an atomic open.
1462 * Until then, this open method is prone to inefficiency and race conditions
1463 * due to the lookup, potential create, and open VFS calls from sys_open()
1464 * placed on the wire.
1467 nfs4_proc_file_open(struct inode *inode, struct file *filp)
1469 struct dentry *dentry = filp->f_dentry;
1470 struct nfs4_state *state;
1471 struct rpc_cred *cred;
1473 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
1474 (int)dentry->d_parent->d_name.len,
1475 dentry->d_parent->d_name.name,
1476 (int)dentry->d_name.len, dentry->d_name.name);
1479 /* Find our open stateid */
1480 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1481 state = nfs4_find_state(inode, cred, filp->f_mode);
1483 if (state == NULL) {
1484 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
1485 return -EIO; /* ERACE actually */
1487 nfs4_close_state(state, filp->f_mode);
1488 if (filp->f_mode & FMODE_WRITE) {
1490 nfs_set_mmcred(inode, state->owner->so_cred);
1491 nfs_begin_data_update(inode);
1494 filp->private_data = state;
1502 nfs4_proc_file_release(struct inode *inode, struct file *filp)
1504 struct nfs4_state *state = (struct nfs4_state *)filp->private_data;
1507 nfs4_close_state(state, filp->f_mode);
1508 if (filp->f_mode & FMODE_WRITE) {
1510 nfs_end_data_update(inode);
1517 * Set up the nfspage struct with the right state info and credentials
1520 nfs4_request_init(struct nfs_page *req, struct file *filp)
1522 struct nfs4_state *state;
1525 req->wb_cred = get_rpccred(NFS_I(req->wb_inode)->mm_cred);
1526 req->wb_state = NULL;
1529 state = (struct nfs4_state *)filp->private_data;
1530 req->wb_state = state;
1531 req->wb_cred = get_rpccred(state->owner->so_cred);
1532 req->wb_lockowner = current->files;
1536 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
1538 struct nfs4_client *clp = server->nfs4_state;
1540 if (!clp || task->tk_status >= 0)
1542 switch(task->tk_status) {
1543 case -NFS4ERR_STALE_CLIENTID:
1544 case -NFS4ERR_STALE_STATEID:
1545 case -NFS4ERR_EXPIRED:
1546 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
1547 nfs4_schedule_state_recovery(clp);
1548 task->tk_status = 0;
1550 case -NFS4ERR_GRACE:
1551 case -NFS4ERR_DELAY:
1552 rpc_delay(task, NFS4_POLL_RETRY_TIME);
1553 task->tk_status = 0;
1555 case -NFS4ERR_OLD_STATEID:
1556 task->tk_status = 0;
1559 task->tk_status = nfs4_map_errors(task->tk_status);
1564 nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
1568 int interruptible, res;
1572 rpc_clnt_sigmask(clnt, &oldset);
1573 interruptible = TASK_UNINTERRUPTIBLE;
1575 interruptible = TASK_INTERRUPTIBLE;
1578 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
1579 nfs4_schedule_state_recovery(clp);
1580 if (test_bit(NFS4CLNT_OK, &clp->cl_state) &&
1581 !test_bit(NFS4CLNT_SETUP_STATE, &clp->cl_state))
1583 if (clnt->cl_intr && signalled()) {
1588 } while(!test_bit(NFS4CLNT_OK, &clp->cl_state));
1589 finish_wait(&clp->cl_waitq, &wait);
1590 rpc_clnt_sigunmask(clnt, &oldset);
1595 nfs4_delay(struct rpc_clnt *clnt)
1602 rpc_clnt_sigmask(clnt, &oldset);
1603 if (clnt->cl_intr) {
1604 set_current_state(TASK_INTERRUPTIBLE);
1605 schedule_timeout(NFS4_POLL_RETRY_TIME);
1609 set_current_state(TASK_UNINTERRUPTIBLE);
1610 schedule_timeout(NFS4_POLL_RETRY_TIME);
1612 rpc_clnt_sigunmask(clnt, &oldset);
1616 /* This is the error handling routine for processes that are allowed
1620 nfs4_handle_error(struct nfs_server *server, int errorcode)
1622 struct nfs4_client *clp = server->nfs4_state;
1623 int ret = errorcode;
1626 case -NFS4ERR_STALE_CLIENTID:
1627 case -NFS4ERR_STALE_STATEID:
1628 case -NFS4ERR_EXPIRED:
1629 ret = nfs4_wait_clnt_recover(server->client, clp);
1631 case -NFS4ERR_GRACE:
1632 case -NFS4ERR_DELAY:
1633 ret = nfs4_delay(server->client);
1635 case -NFS4ERR_OLD_STATEID:
1638 /* We failed to handle the error */
1639 return nfs4_map_errors(ret);
1644 nfs4_request_compatible(struct nfs_page *req, struct file *filp, struct page *page)
1646 struct nfs4_state *state = NULL;
1647 struct rpc_cred *cred = NULL;
1649 if (req->wb_file != filp)
1651 if (req->wb_page != page)
1653 state = (struct nfs4_state *)filp->private_data;
1654 if (req->wb_state != state)
1656 if (req->wb_lockowner != current->files)
1658 cred = state->owner->so_cred;
1659 if (req->wb_cred != cred)
1665 nfs4_proc_setclientid(struct nfs4_client *clp,
1666 u32 program, unsigned short port)
1669 struct nfs4_setclientid setclientid;
1671 struct rpc_message msg = {
1672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
1673 .rpc_argp = &setclientid,
1675 .rpc_cred = clp->cl_cred,
1679 p = (u32*)setclientid.sc_verifier.data;
1680 *p++ = (u32)tv.tv_sec;
1681 *p = (u32)tv.tv_nsec;
1682 setclientid.sc_name = clp->cl_ipaddr;
1683 sprintf(setclientid.sc_netid, "tcp");
1684 sprintf(setclientid.sc_uaddr, "%s.%d.%d", clp->cl_ipaddr, port >> 8, port & 255);
1685 setclientid.sc_prog = htonl(program);
1686 setclientid.sc_cb_ident = 0;
1688 return rpc_call_sync(clp->cl_rpcclient, &msg, 0);
1692 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
1694 struct nfs_fsinfo fsinfo;
1695 struct rpc_message msg = {
1696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
1698 .rpc_resp = &fsinfo,
1699 .rpc_cred = clp->cl_cred,
1705 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
1707 spin_lock(&clp->cl_lock);
1708 clp->cl_lease_time = fsinfo.lease_time * HZ;
1709 clp->cl_last_renewal = now;
1710 spin_unlock(&clp->cl_lock);
1715 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
1716 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
1719 * sleep, with exponential backoff, and retry the LOCK operation.
1721 static unsigned long
1722 nfs4_set_lock_task_retry(unsigned long timeout)
1724 current->state = TASK_INTERRUPTIBLE;
1725 schedule_timeout(timeout);
1727 if (timeout > NFS4_LOCK_MAXTIMEOUT)
1728 return NFS4_LOCK_MAXTIMEOUT;
1733 nfs4_lck_type(int cmd, struct file_lock *request)
1736 switch (request->fl_type) {
1738 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
1740 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
1742 return NFS4_WRITE_LT;
1748 static inline uint64_t
1749 nfs4_lck_length(struct file_lock *request)
1751 if (request->fl_end == OFFSET_MAX)
1752 return ~(uint64_t)0;
1753 return request->fl_end - request->fl_start + 1;
1757 nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
1759 struct inode *inode = state->inode;
1760 struct nfs_server *server = NFS_SERVER(inode);
1761 struct nfs4_client *clp = server->nfs4_state;
1762 struct nfs_lockargs arg = {
1763 .fh = NFS_FH(inode),
1764 .type = nfs4_lck_type(cmd, request),
1765 .offset = request->fl_start,
1766 .length = nfs4_lck_length(request),
1768 struct nfs_lockres res = {
1771 struct rpc_message msg = {
1772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
1775 .rpc_cred = state->owner->so_cred,
1777 struct nfs_lowner nlo;
1778 struct nfs4_lock_state *lsp;
1781 nlo.clientid = clp->cl_clientid;
1782 down(&state->lock_sema);
1783 lsp = nfs4_find_lock_state(state, request->fl_owner);
1785 nlo.id = lsp->ls_id;
1787 spin_lock(&clp->cl_lock);
1788 nlo.id = nfs4_alloc_lockowner_id(clp);
1789 spin_unlock(&clp->cl_lock);
1792 status = rpc_call_sync(server->client, &msg, 0);
1794 request->fl_type = F_UNLCK;
1795 } else if (status == -NFS4ERR_DENIED) {
1796 int64_t len, start, end;
1797 start = res.u.denied.offset;
1798 len = res.u.denied.length;
1799 end = start + len - 1;
1800 if (end < 0 || len == 0)
1801 request->fl_end = OFFSET_MAX;
1803 request->fl_end = (loff_t)end;
1804 request->fl_start = (loff_t)start;
1805 request->fl_type = F_WRLCK;
1806 if (res.u.denied.type & 1)
1807 request->fl_type = F_RDLCK;
1808 request->fl_pid = 0;
1812 nfs4_put_lock_state(lsp);
1813 up(&state->lock_sema);
1814 return nfs4_map_errors(status);
1818 nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
1820 struct inode *inode = state->inode;
1821 struct nfs_server *server = NFS_SERVER(inode);
1822 struct nfs_lockargs arg = {
1823 .fh = NFS_FH(inode),
1824 .type = nfs4_lck_type(cmd, request),
1825 .offset = request->fl_start,
1826 .length = nfs4_lck_length(request),
1828 struct nfs_lockres res = {
1831 struct rpc_message msg = {
1832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
1835 .rpc_cred = state->owner->so_cred,
1837 struct nfs4_lock_state *lsp;
1838 struct nfs_locku_opargs luargs;
1841 down(&state->lock_sema);
1842 lsp = nfs4_find_lock_state(state, request->fl_owner);
1845 luargs.seqid = lsp->ls_seqid;
1846 memcpy(&luargs.stateid, &lsp->ls_stateid, sizeof(luargs.stateid));
1847 arg.u.locku = &luargs;
1848 status = rpc_call_sync(server->client, &msg, 0);
1849 nfs4_increment_lock_seqid(status, lsp);
1852 memcpy(&lsp->ls_stateid, &res.u.stateid,
1853 sizeof(lsp->ls_stateid));
1854 nfs4_notify_unlck(inode, request, lsp);
1856 nfs4_put_lock_state(lsp);
1858 up(&state->lock_sema);
1859 return nfs4_map_errors(status);
1863 nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
1865 struct inode *inode = state->inode;
1866 struct nfs_server *server = NFS_SERVER(inode);
1867 struct nfs4_lock_state *lsp;
1868 struct nfs_lockargs arg = {
1869 .fh = NFS_FH(inode),
1870 .type = nfs4_lck_type(cmd, request),
1871 .offset = request->fl_start,
1872 .length = nfs4_lck_length(request),
1874 struct nfs_lockres res = {
1877 struct rpc_message msg = {
1878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
1881 .rpc_cred = state->owner->so_cred,
1883 struct nfs_lock_opargs largs = {
1884 .new_lock_owner = 0,
1888 down(&state->lock_sema);
1889 lsp = nfs4_find_lock_state(state, request->fl_owner);
1891 struct nfs4_state_owner *owner = state->owner;
1892 struct nfs_open_to_lock otl = {
1894 .clientid = server->nfs4_state->cl_clientid,
1898 lsp = nfs4_alloc_lock_state(state, request->fl_owner);
1901 otl.lock_seqid = lsp->ls_seqid;
1902 otl.lock_owner.id = lsp->ls_id;
1903 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
1904 largs.u.open_lock = &otl;
1905 largs.new_lock_owner = 1;
1906 arg.u.lock = &largs;
1907 down(&owner->so_sema);
1908 otl.open_seqid = owner->so_seqid;
1909 status = rpc_call_sync(server->client, &msg, 0);
1910 /* increment open_owner seqid on success, and
1911 * seqid mutating errors */
1912 nfs4_increment_seqid(status, owner);
1913 up(&owner->so_sema);
1915 struct nfs_exist_lock el = {
1916 .seqid = lsp->ls_seqid,
1918 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
1919 largs.u.exist_lock = ⪙
1920 largs.new_lock_owner = 0;
1921 arg.u.lock = &largs;
1922 status = rpc_call_sync(server->client, &msg, 0);
1924 /* increment seqid on success, and * seqid mutating errors*/
1925 nfs4_increment_lock_seqid(status, lsp);
1926 /* save the returned stateid. */
1928 memcpy(&lsp->ls_stateid, &res.u.stateid, sizeof(nfs4_stateid));
1929 nfs4_notify_setlk(inode, request, lsp);
1930 } else if (status == -NFS4ERR_DENIED)
1932 nfs4_put_lock_state(lsp);
1934 up(&state->lock_sema);
1935 return nfs4_map_errors(status);
1939 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
1941 struct nfs4_state *state;
1942 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
1945 /* verify open state */
1946 state = (struct nfs4_state *)filp->private_data;
1949 if (request->fl_start < 0 || request->fl_end < 0)
1953 return nfs4_proc_getlk(state, F_GETLK, request);
1955 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
1958 if (request->fl_type == F_UNLCK)
1959 return nfs4_proc_unlck(state, cmd, request);
1962 status = nfs4_proc_setlk(state, cmd, request);
1963 if ((status != -EAGAIN) || IS_SETLK(cmd))
1965 timeout = nfs4_set_lock_task_retry(timeout);
1966 status = -ERESTARTSYS;
1969 } while(status < 0);
1974 struct nfs_rpc_ops nfs_v4_clientops = {
1975 .version = 4, /* protocol version */
1976 .dentry_ops = &nfs4_dentry_operations,
1977 .dir_inode_ops = &nfs4_dir_inode_operations,
1978 .getroot = nfs4_proc_get_root,
1979 .getattr = nfs4_proc_getattr,
1980 .setattr = nfs4_proc_setattr,
1981 .lookup = nfs4_proc_lookup,
1982 .access = nfs4_proc_access,
1983 .readlink = nfs4_proc_readlink,
1984 .read = nfs4_proc_read,
1985 .write = nfs4_proc_write,
1986 .commit = nfs4_proc_commit,
1987 .create = nfs4_proc_create,
1988 .remove = nfs4_proc_remove,
1989 .unlink_setup = nfs4_proc_unlink_setup,
1990 .unlink_done = nfs4_proc_unlink_done,
1991 .rename = nfs4_proc_rename,
1992 .link = nfs4_proc_link,
1993 .symlink = nfs4_proc_symlink,
1994 .mkdir = nfs4_proc_mkdir,
1995 .rmdir = nfs4_proc_remove,
1996 .readdir = nfs4_proc_readdir,
1997 .mknod = nfs4_proc_mknod,
1998 .statfs = nfs4_proc_statfs,
1999 .fsinfo = nfs4_proc_fsinfo,
2000 .pathconf = nfs4_proc_pathconf,
2001 .decode_dirent = nfs4_decode_dirent,
2002 .read_setup = nfs4_proc_read_setup,
2003 .write_setup = nfs4_proc_write_setup,
2004 .commit_setup = nfs4_proc_commit_setup,
2005 .file_open = nfs4_proc_file_open,
2006 .file_release = nfs4_proc_file_release,
2007 .request_init = nfs4_request_init,
2008 .request_compatible = nfs4_request_compatible,
2009 .lock = nfs4_proc_lock,