ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / nfs / nfs4proc.c
1 /*
2  *  fs/nfs/nfs4proc.c
3  *
4  *  Client-side procedure declarations for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
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.
24  *
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.
36  */
37
38 #include <linux/mm.h>
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>
49
50 #define NFSDBG_FACILITY         NFSDBG_PROC
51
52 #define NFS4_POLL_RETRY_TIME    (15*HZ)
53
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[];
58
59 extern nfs4_stateid zero_stateid;
60
61 /* Prevent leaks of NFSv4 errors into userland */
62 static inline int nfs4_map_errors(int err)
63 {
64         if (err < -1000) {
65                 printk(KERN_WARNING "%s could not handle NFSv4 error %d\n",
66                                 __FUNCTION__, -err);
67                 return -EIO;
68         }
69         return err;
70 }
71
72 /*
73  * This is our standard bitmap for GETATTR requests.
74  */
75 const u32 nfs4_fattr_bitmap[2] = {
76         FATTR4_WORD0_TYPE
77         | FATTR4_WORD0_CHANGE
78         | FATTR4_WORD0_SIZE
79         | FATTR4_WORD0_FSID
80         | FATTR4_WORD0_FILEID,
81         FATTR4_WORD1_MODE
82         | FATTR4_WORD1_NUMLINKS
83         | FATTR4_WORD1_OWNER
84         | FATTR4_WORD1_OWNER_GROUP
85         | FATTR4_WORD1_RAWDEV
86         | FATTR4_WORD1_SPACE_USED
87         | FATTR4_WORD1_TIME_ACCESS
88         | FATTR4_WORD1_TIME_METADATA
89         | FATTR4_WORD1_TIME_MODIFY
90 };
91
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
99 };
100
101 u32 nfs4_pathconf_bitmap[2] = {
102         FATTR4_WORD0_MAXLINK
103         | FATTR4_WORD0_MAXNAME,
104         0
105 };
106
107 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
108                         | FATTR4_WORD0_MAXREAD
109                         | FATTR4_WORD0_MAXWRITE
110                         | FATTR4_WORD0_LEASE_TIME,
111                         0
112 };
113
114 static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
115                 struct nfs4_readdir_arg *readdir)
116 {
117         u32 *start, *p;
118
119         BUG_ON(readdir->count < 80);
120         if (cookie > 2) {
121                 readdir->cookie = (cookie > 2) ? cookie : 0;
122                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
123                 return;
124         }
125
126         readdir->cookie = 0;
127         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
128         if (cookie == 2)
129                 return;
130         
131         /*
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
136          * instead of 1 or 2.
137          */
138         start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
139         
140         if (cookie == 0) {
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 */
146                 p++;
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);
151         }
152         
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 */
158         p++;
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);
163
164         readdir->pgbase = (char *)p - (char *)start;
165         readdir->count -= readdir->pgbase;
166         kunmap_atomic(start, KM_USER0);
167 }
168
169 static void
170 renew_lease(struct nfs_server *server, unsigned long timestamp)
171 {
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);
177 }
178
179 static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
180 {
181         struct nfs_inode *nfsi = NFS_I(inode);
182
183         if (cinfo->before == nfsi->change_attr && cinfo->atomic)
184                 nfsi->change_attr = cinfo->after;
185 }
186
187 /*
188  * OPEN_RECLAIM:
189  *      reclaim state on the server after a reboot.
190  *      Assumes caller is holding the sp->so_sem
191  */
192 int
193 nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
194 {
195         struct inode *inode = state->inode;
196         struct nfs_server *server = NFS_SERVER(inode);
197         struct nfs_fattr fattr = {
198                 .valid = 0,
199         };
200         struct nfs_open_reclaimargs o_arg = {
201                 .fh = NFS_FH(inode),
202                 .seqid = sp->so_seqid,
203                 .id = sp->so_id,
204                 .share_access = state->state,
205                 .clientid = server->nfs4_state->cl_clientid,
206                 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
207                 .bitmask = server->attr_bitmask,
208         };
209         struct nfs_openres o_res = {
210                 .f_attr = &fattr,
211                 .server = server,       /* Grrr */
212         };
213         struct rpc_message msg = {
214                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN_RECLAIM],
215                 .rpc_argp       = &o_arg,
216                 .rpc_resp       = &o_res,
217                 .rpc_cred       = sp->so_cred,
218         };
219         int status;
220
221         status = rpc_call_sync(server->client, &msg, 0);
222         nfs4_increment_seqid(status, sp);
223         if (status == 0)
224                 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
225         /* Update the inode attributes */
226         nfs_refresh_inode(inode, &fattr);
227         return status;
228 }
229
230 /*
231  * Returns an nfs4_state + an referenced inode
232  */
233 struct nfs4_state *
234 nfs4_do_open(struct inode *dir, struct qstr *name, int flags, struct iattr *sattr, struct rpc_cred *cred)
235 {
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;
240         int                     status;
241         struct nfs_fattr        f_attr = {
242                 .valid          = 0,
243         };
244         struct nfs_openargs o_arg = {
245                 .fh             = NFS_FH(dir),
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,
249                 .name           = name,
250                 .server         = server,
251                 .bitmask = server->attr_bitmask,
252         };
253         struct nfs_openres o_res = {
254                 .f_attr         = &f_attr,
255                 .server         = server,
256         };
257         struct rpc_message msg = {
258                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
259                 .rpc_argp       = &o_arg,
260                 .rpc_resp       = &o_res,
261                 .rpc_cred       = cred,
262         };
263
264 retry:
265         status = -ENOMEM;
266         if (!(sp = nfs4_get_state_owner(NFS_SERVER(dir), cred))) {
267                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
268                 goto out;
269         }
270         if (o_arg.createmode & NFS4_CREATE_EXCLUSIVE){
271                 u32 *p = (u32 *) o_arg.u.verifier.data;
272                 p[0] = jiffies;
273                 p[1] = current->pid;
274         } else if (o_arg.createmode == NFS4_CREATE_UNCHECKED) {
275                 o_arg.u.attrs = sattr;
276         }
277         /* Serialization for the sequence id */
278         down(&sp->so_sema);
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,
282
283         status = rpc_call_sync(server->client, &msg, 0);
284         nfs4_increment_seqid(status, sp);
285         if (status)
286                 goto out_up;
287         update_changeattr(dir, &o_res.cinfo);
288
289         status = -ENOMEM;
290         inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
291         if (!inode)
292                 goto out_up;
293         state = nfs4_get_open_state(inode, sp);
294         if (!state)
295                 goto out_up;
296
297         if(o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
298                 struct nfs_open_confirmargs oc_arg = {
299                         .fh             = &o_res.fh,
300                         .seqid          = sp->so_seqid,
301                 };
302                 struct nfs_open_confirmres oc_res;
303                 struct  rpc_message msg = {
304                         .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
305                         .rpc_argp       = &oc_arg,
306                         .rpc_resp       = &oc_res,
307                         .rpc_cred       = cred,
308                 };
309
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);
313                 if (status)
314                         goto out_up;
315                 memcpy(&state->stateid, &oc_res.stateid, sizeof(state->stateid));
316         } else
317                 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
318         spin_lock(&inode->i_lock);
319         if (flags & FMODE_READ)
320                 state->nreaders++;
321         if (flags & FMODE_WRITE)
322                 state->nwriters++;
323         state->state |= flags & (FMODE_READ|FMODE_WRITE);
324         spin_unlock(&inode->i_lock);
325
326         up(&sp->so_sema);
327         nfs4_put_state_owner(sp);
328         return state;
329
330 out_up:
331         up(&sp->so_sema);
332         nfs4_put_state_owner(sp);
333         if (state) {
334                 nfs4_put_open_state(state);
335                 state = NULL;
336         }
337         if (inode) {
338                 iput(inode);
339                 inode = NULL;
340         }
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.
345          *
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
350          * the user though...
351          */
352         if (status == -NFS4ERR_BAD_SEQID) {
353                 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
354                 goto retry;
355         }
356         status = nfs4_handle_error(server, status);
357         if (!status)
358                 goto retry;
359         BUG_ON(status < -1000 || status > 0);
360 out:
361         return ERR_PTR(status);
362 }
363
364 int
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)
368 {
369         struct nfs_setattrargs  arg = {
370                 .fh             = fhandle,
371                 .iap            = sattr,
372                 .server         = server,
373                 .bitmask = server->attr_bitmask,
374         };
375         struct nfs_setattrres  res = {
376                 .fattr          = fattr,
377                 .server         = server,
378         };
379         struct rpc_message msg = {
380                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
381                 .rpc_argp       = &arg,
382                 .rpc_resp       = &res,
383         };
384         int status;
385
386 retry:
387         fattr->valid = 0;
388
389         if (sattr->ia_valid & ATTR_SIZE)
390                 nfs4_copy_stateid(&arg.stateid, state, 0);
391         else
392                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
393
394         status = rpc_call_sync(server->client, &msg, 0);
395         if (status) {
396                 status = nfs4_handle_error(server, status);
397                 if (!status)
398                         goto retry;
399         }
400         return status;
401 }
402
403 /* 
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.
411  *
412  * NOTE: Caller must be holding the sp->so_owner semaphore!
413  */
414 int
415 nfs4_do_close(struct inode *inode, struct nfs4_state *state) 
416 {
417         struct nfs4_state_owner *sp = state->owner;
418         int status = 0;
419         struct nfs_closeargs arg = {
420                 .fh             = NFS_FH(inode),
421         };
422         struct nfs_closeres res;
423         struct rpc_message msg = {
424                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
425                 .rpc_argp       = &arg,
426                 .rpc_resp       = &res,
427         };
428
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);
433
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
436          */
437         nfs4_increment_seqid(status, sp);
438         if (!status)
439                 memcpy(&state->stateid, &res.stateid, sizeof(state->stateid));
440
441         return status;
442 }
443
444 int
445 nfs4_do_downgrade(struct inode *inode, struct nfs4_state *state, mode_t mode) 
446 {
447         struct nfs4_state_owner *sp = state->owner;
448         int status = 0;
449         struct nfs_closeargs arg = {
450                 .fh             = NFS_FH(inode),
451                 .seqid          = sp->so_seqid,
452                 .share_access   = mode,
453         };
454         struct nfs_closeres res;
455         struct rpc_message msg = {
456                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE],
457                 .rpc_argp       = &arg,
458                 .rpc_resp       = &res,
459         };
460
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);
464         if (!status)
465                 memcpy(&state->stateid, &res.stateid, sizeof(state->stateid));
466
467         return status;
468 }
469
470 struct inode *
471 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
472 {
473         struct iattr attr;
474         struct rpc_cred *cred;
475         struct nfs4_state *state;
476
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;
482         } else {
483                 attr.ia_valid = 0;
484                 BUG_ON(nd->intent.open.flags & O_CREAT);
485         }
486
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);
489         put_rpccred(cred);
490         if (IS_ERR(state))
491                 return (struct inode *)state;
492         return state->inode;
493 }
494
495 int
496 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
497 {
498         struct rpc_cred *cred;
499         struct nfs4_state *state;
500         struct inode *inode;
501
502         cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
503         state = nfs4_do_open(dir, &dentry->d_name, openflags, NULL, cred);
504         put_rpccred(cred);
505         if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
506                 return 1;
507         if (IS_ERR(state))
508                 return 0;
509         inode = state->inode;
510         if (inode == dentry->d_inode) {
511                 iput(inode);
512                 return 1;
513         }
514         d_drop(dentry);
515         nfs4_close_state(state, openflags);
516         iput(inode);
517         return 0;
518 }
519
520
521 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
522 {
523         struct nfs4_server_caps_res res = {};
524         struct rpc_message msg = {
525                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
526                 .rpc_argp = fhandle,
527                 .rpc_resp = &res,
528         };
529         int status;
530
531         status = rpc_call_sync(server->client, &msg, 0);
532         if (status == 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;
541         }
542         return status;
543 }
544
545 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
546                 struct nfs_fsinfo *info)
547 {
548         struct nfs_fattr *      fattr = info->fattr;
549         struct nfs4_lookup_root_arg args = {
550                 .bitmask = nfs4_fattr_bitmap,
551         };
552         struct nfs4_lookup_res res = {
553                 .server = server,
554                 .fattr = fattr,
555                 .fh = fhandle,
556         };
557         struct rpc_message msg = {
558                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
559                 .rpc_argp = &args,
560                 .rpc_resp = &res,
561         };
562         fattr->valid = 0;
563         return rpc_call_sync(server->client, &msg, 0);
564 }
565
566 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
567                 struct nfs_fsinfo *info)
568 {
569         struct nfs_fattr *      fattr = info->fattr;
570         unsigned char *         p;
571         struct qstr             q;
572         struct nfs4_lookup_arg args = {
573                 .dir_fh = fhandle,
574                 .name = &q,
575                 .bitmask = nfs4_fattr_bitmap,
576         };
577         struct nfs4_lookup_res res = {
578                 .server = server,
579                 .fattr = fattr,
580                 .fh = fhandle,
581         };
582         struct rpc_message msg = {
583                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
584                 .rpc_argp = &args,
585                 .rpc_resp = &res,
586         };
587         int status;
588
589         /*
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...
593          */
594         status = nfs4_lookup_root(server, fhandle, info);
595         if (status)
596                 goto out;
597
598         p = server->mnt_path;
599         for (;;) {
600                 while (*p == '/')
601                         p++;
602                 if (!*p)
603                         break;
604                 q.name = p;
605                 while (*p && (*p != '/'))
606                         p++;
607                 q.len = p - q.name;
608
609                 fattr->valid = 0;
610                 status = rpc_call_sync(server->client, &msg, 0);
611                 if (!status)
612                         continue;
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");
616                 }
617                 break;
618         }
619         if (status == 0)
620                 status = nfs4_server_capabilities(server, fhandle);
621         if (status == 0)
622                 status = nfs4_do_fsinfo(server, fhandle, info);
623 out:
624         return nfs4_map_errors(status);
625 }
626
627 static int nfs4_proc_getattr(struct inode *inode, struct nfs_fattr *fattr)
628 {
629         struct nfs_server *server = NFS_SERVER(inode);
630         struct nfs4_getattr_arg args = {
631                 .fh = NFS_FH(inode),
632                 .bitmask = server->attr_bitmask,
633         };
634         struct nfs4_getattr_res res = {
635                 .fattr = fattr,
636                 .server = server,
637         };
638         struct rpc_message msg = {
639                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
640                 .rpc_argp = &args,
641                 .rpc_resp = &res,
642         };
643         
644         fattr->valid = 0;
645
646         return nfs4_map_errors(rpc_call_sync(NFS_CLIENT(inode), &msg, 0));
647 }
648
649 /* 
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.
653  *
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.
657  *
658  * Got race?
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.
663  *
664  * This will be fixed with VFS changes (lookup-intent).
665  */
666 static int
667 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
668                   struct iattr *sattr)
669 {
670         struct inode *          inode = dentry->d_inode;
671         int                     size_change = sattr->ia_valid & ATTR_SIZE;
672         struct nfs4_state       *state = NULL;
673         int need_iput = 0;
674         int status;
675
676         fattr->valid = 0;
677         
678         if (size_change) {
679                 struct rpc_cred *cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
680                 state = nfs4_find_state(inode, cred, FMODE_WRITE);
681                 if (!state) {
682                         state = nfs4_do_open(dentry->d_parent->d_inode, 
683                                 &dentry->d_name, FMODE_WRITE, NULL, cred);
684                         need_iput = 1;
685                 }
686                 put_rpccred(cred);
687                 if (IS_ERR(state))
688                         return PTR_ERR(state);
689
690                 if (state->inode != inode) {
691                         printk(KERN_WARNING "nfs: raced in setattr (%p != %p), returning -EIO\n", inode, state->inode);
692                         status = -EIO;
693                         goto out;
694                 }
695         }
696         status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
697                         NFS_FH(inode), sattr, state);
698 out:
699         if (state) {
700                 inode = state->inode;
701                 nfs4_close_state(state, FMODE_WRITE);
702                 if (need_iput)
703                         iput(inode);
704         }
705         return status;
706 }
707
708 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
709                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
710 {
711         int                    status;
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),
716                 .name = name,
717         };
718         struct nfs4_lookup_res res = {
719                 .server = server,
720                 .fattr = fattr,
721                 .fh = fhandle,
722         };
723         struct rpc_message msg = {
724                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
725                 .rpc_argp = &args,
726                 .rpc_resp = &res,
727         };
728         
729         fattr->valid = 0;
730         
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);
735 }
736
737 static int nfs4_proc_access(struct inode *inode, struct rpc_cred *cred, int mode)
738 {
739         int                     status;
740         struct nfs4_accessargs args = {
741                 .fh = NFS_FH(inode),
742         };
743         struct nfs4_accessres res = { 0 };
744         struct rpc_message msg = {
745                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
746                 .rpc_argp = &args,
747                 .rpc_resp = &res,
748                 .rpc_cred = cred,
749         };
750
751         /*
752          * Determine which access bits we want to ask for...
753          */
754         if (mode & MAY_READ)
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;
759                 if (mode & MAY_EXEC)
760                         args.access |= NFS4_ACCESS_LOOKUP;
761         }
762         else {
763                 if (mode & MAY_WRITE)
764                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
765                 if (mode & MAY_EXEC)
766                         args.access |= NFS4_ACCESS_EXECUTE;
767         }
768         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
769         if (!status) {
770                 if (args.access != res.supported) {
771                         printk(KERN_NOTICE "NFS: server didn't support all access bits!\n");
772                         status = -ENOTSUPP;
773                 } else if ((args.access & res.access) != args.access)
774                         status = -EACCES;
775         }
776         return nfs4_map_errors(status);
777 }
778
779 /*
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,
782  * READLINK, WRITE.
783  *
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
792  * of the RPC layer.
793  *
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.
799  *
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.
802  */
803 static int nfs4_proc_readlink(struct inode *inode, struct page *page)
804 {
805         struct nfs4_readlink args = {
806                 .fh       = NFS_FH(inode),
807                 .count    = PAGE_CACHE_SIZE,
808                 .pages    = &page,
809         };
810         struct rpc_message msg = {
811                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
812                 .rpc_argp = &args,
813                 .rpc_resp = NULL,
814         };
815
816         return nfs4_map_errors(rpc_call_sync(NFS_CLIENT(inode), &msg, 0));
817 }
818
819 static int
820 nfs4_proc_read(struct nfs_read_data *rdata, struct file *filp)
821 {
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,
830         };
831         unsigned long timestamp = jiffies;
832         int status;
833
834         dprintk("NFS call  read %d @ %Ld\n", rdata->args.count,
835                         (long long) rdata->args.offset);
836
837         /*
838          * Try first to use O_RDONLY, then O_RDWR stateid.
839          */
840         if (filp) {
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;
845         } else {
846                 rdata->args.state = NULL;
847                 msg.rpc_cred = NFS_I(inode)->mm_cred;
848         }
849
850         fattr->valid = 0;
851         status = rpc_call_sync(server->client, &msg, flags);
852         if (!status)
853                 renew_lease(server, timestamp);
854         dprintk("NFS reply read: %d\n", status);
855         return nfs4_map_errors(status);
856 }
857
858 static int
859 nfs4_proc_write(struct nfs_write_data *wdata, struct file *filp)
860 {
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,
869         };
870         int status;
871
872         dprintk("NFS call  write %d @ %Ld\n", wdata->args.count,
873                         (long long) wdata->args.offset);
874
875         /*
876          * Try first to use O_WRONLY, then O_RDWR stateid.
877          */
878         if (filp) {
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;
883         } else {
884                 wdata->args.state = NULL;
885                 msg.rpc_cred = NFS_I(inode)->mm_cred;
886         }
887
888         fattr->valid = 0;
889         status = rpc_call_sync(server->client, &msg, rpcflags);
890         dprintk("NFS reply write: %d\n", status);
891         return nfs4_map_errors(status);
892 }
893
894 static int
895 nfs4_proc_commit(struct nfs_write_data *cdata, struct file *filp)
896 {
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,
904         };
905         int status;
906
907         dprintk("NFS call  commit %d @ %Ld\n", cdata->args.count,
908                         (long long) cdata->args.offset);
909
910         /*
911          * Try first to use O_WRONLY, then O_RDWR stateid.
912          */
913         if (filp)
914                 msg.rpc_cred = ((struct nfs4_state *)filp->private_data)->owner->so_cred;
915         else
916                 msg.rpc_cred = NFS_I(inode)->mm_cred;
917
918         fattr->valid = 0;
919         status = rpc_call_sync(server->client, &msg, 0);
920         dprintk("NFS reply commit: %d\n", status);
921         return nfs4_map_errors(status);
922 }
923
924 /*
925  * Got race?
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.
930  *
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).
934  *
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.
937  */
938
939 static struct inode *
940 nfs4_proc_create(struct inode *dir, struct qstr *name, struct iattr *sattr,
941                  int flags)
942 {
943         struct inode *inode;
944         struct nfs4_state *state = NULL;
945         struct rpc_cred *cred;
946
947         cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
948         state = nfs4_do_open(dir, name, flags, sattr, cred);
949         put_rpccred(cred);
950         if (!IS_ERR(state)) {
951                 inode = state->inode;
952                 if (flags & O_EXCL) {
953                         struct nfs_fattr fattr;
954                         int status;
955                         status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
956                                              NFS_FH(inode), sattr, state);
957                         if (status != 0) {
958                                 nfs4_close_state(state, flags);
959                                 iput(inode);
960                                 inode = ERR_PTR(status);
961                         }
962                 }
963         } else
964                 inode = (struct inode *)state;
965         return inode;
966 }
967
968 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
969 {
970         struct nfs4_remove_arg args = {
971                 .fh = NFS_FH(dir),
972                 .name = name,
973         };
974         struct nfs4_change_info res;
975         struct rpc_message msg = {
976                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
977                 .rpc_argp       = &args,
978                 .rpc_resp       = &res,
979         };
980         int                     status;
981
982         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
983         if (status == 0)
984                 update_changeattr(dir, &res);
985         return nfs4_map_errors(status);
986 }
987
988 struct unlink_desc {
989         struct nfs4_remove_arg  args;
990         struct nfs4_change_info res;
991 };
992
993 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
994                 struct qstr *name)
995 {
996         struct unlink_desc *up;
997
998         up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
999         if (!up)
1000                 return -ENOMEM;
1001         
1002         up->args.fh = NFS_FH(dir->d_inode);
1003         up->args.name = name;
1004         
1005         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1006         msg->rpc_argp = &up->args;
1007         msg->rpc_resp = &up->res;
1008         return 0;
1009 }
1010
1011 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1012 {
1013         struct rpc_message *msg = &task->tk_msg;
1014         struct unlink_desc *up;
1015         
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);
1019                 kfree(up);
1020                 msg->rpc_resp = NULL;
1021                 msg->rpc_argp = NULL;
1022         }
1023         return 0;
1024 }
1025
1026 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1027                 struct inode *new_dir, struct qstr *new_name)
1028 {
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,
1034         };
1035         struct nfs4_rename_res res = { };
1036         struct rpc_message msg = {
1037                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1038                 .rpc_argp = &arg,
1039                 .rpc_resp = &res,
1040         };
1041         int                     status;
1042         
1043         status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1044
1045         if (!status) {
1046                 update_changeattr(old_dir, &res.old_cinfo);
1047                 update_changeattr(new_dir, &res.new_cinfo);
1048         }
1049         return nfs4_map_errors(status);
1050 }
1051
1052 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1053 {
1054         struct nfs4_link_arg arg = {
1055                 .fh     = NFS_FH(inode),
1056                 .dir_fh = NFS_FH(dir),
1057                 .name   = name,
1058         };
1059         struct nfs4_change_info cinfo = { };
1060         struct rpc_message msg = {
1061                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1062                 .rpc_argp = &arg,
1063                 .rpc_resp = &cinfo,
1064         };
1065         int                     status;
1066
1067         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1068         if (!status)
1069                 update_changeattr(dir, &cinfo);
1070
1071         return nfs4_map_errors(status);
1072 }
1073
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)
1077 {
1078         struct nfs_server *server = NFS_SERVER(dir);
1079         struct nfs4_create_arg arg = {
1080                 .dir_fh = NFS_FH(dir),
1081                 .server = server,
1082                 .name = name,
1083                 .attrs = sattr,
1084                 .ftype = NF4LNK,
1085                 .bitmask = server->attr_bitmask,
1086         };
1087         struct nfs4_create_res res = {
1088                 .server = server,
1089                 .fh = fhandle,
1090                 .fattr = fattr,
1091         };
1092         struct rpc_message msg = {
1093                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1094                 .rpc_argp = &arg,
1095                 .rpc_resp = &res,
1096         };
1097         int                     status;
1098
1099         arg.u.symlink = path;
1100         fattr->valid = 0;
1101         
1102         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1103         if (!status)
1104                 update_changeattr(dir, &res.dir_cinfo);
1105         return nfs4_map_errors(status);
1106 }
1107
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)
1111 {
1112         struct nfs_server *server = NFS_SERVER(dir);
1113         struct nfs4_create_arg arg = {
1114                 .dir_fh = NFS_FH(dir),
1115                 .server = server,
1116                 .name = name,
1117                 .attrs = sattr,
1118                 .ftype = NF4DIR,
1119                 .bitmask = server->attr_bitmask,
1120         };
1121         struct nfs4_create_res res = {
1122                 .server = server,
1123                 .fh = fhandle,
1124                 .fattr = fattr,
1125         };
1126         struct rpc_message msg = {
1127                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1128                 .rpc_argp = &arg,
1129                 .rpc_resp = &res,
1130         };
1131         int                     status;
1132
1133         fattr->valid = 0;
1134         
1135         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1136         if (!status)
1137                 update_changeattr(dir, &res.dir_cinfo);
1138         return nfs4_map_errors(status);
1139 }
1140
1141 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1142                   u64 cookie, struct page *page, unsigned int count, int plus)
1143 {
1144         struct inode            *dir = dentry->d_inode;
1145         struct nfs4_readdir_arg args = {
1146                 .fh = NFS_FH(dir),
1147                 .pages = &page,
1148                 .pgbase = 0,
1149                 .count = count,
1150         };
1151         struct nfs4_readdir_res res;
1152         struct rpc_message msg = {
1153                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1154                 .rpc_argp = &args,
1155                 .rpc_resp = &res,
1156                 .rpc_cred = cred,
1157         };
1158         int                     status;
1159
1160         lock_kernel();
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);
1164         if (status == 0)
1165                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1166         unlock_kernel();
1167         return nfs4_map_errors(status);
1168 }
1169
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)
1173 {
1174         struct nfs_server *server = NFS_SERVER(dir);
1175         struct nfs4_create_arg arg = {
1176                 .dir_fh = NFS_FH(dir),
1177                 .server = server,
1178                 .name = name,
1179                 .attrs = sattr,
1180                 .bitmask = server->attr_bitmask,
1181         };
1182         struct nfs4_create_res res = {
1183                 .server = server,
1184                 .fh = fh,
1185                 .fattr = fattr,
1186         };
1187         struct rpc_message msg = {
1188                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1189                 .rpc_argp = &arg,
1190                 .rpc_resp = &res,
1191         };
1192         int                     status;
1193         int                     mode = sattr->ia_mode;
1194
1195         fattr->valid = 0;
1196
1197         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1198         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1199         if (S_ISFIFO(mode))
1200                 arg.ftype = NF4FIFO;
1201         else if (S_ISBLK(mode)) {
1202                 arg.ftype = NF4BLK;
1203                 arg.u.device.specdata1 = MAJOR(rdev);
1204                 arg.u.device.specdata2 = MINOR(rdev);
1205         }
1206         else if (S_ISCHR(mode)) {
1207                 arg.ftype = NF4CHR;
1208                 arg.u.device.specdata1 = MAJOR(rdev);
1209                 arg.u.device.specdata2 = MINOR(rdev);
1210         }
1211         else
1212                 arg.ftype = NF4SOCK;
1213         
1214         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1215         if (!status)
1216                 update_changeattr(dir, &res.dir_cinfo);
1217         return nfs4_map_errors(status);
1218 }
1219
1220 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1221                  struct nfs_fsstat *fsstat)
1222 {
1223         struct nfs4_statfs_arg args = {
1224                 .fh = fhandle,
1225                 .bitmask = server->attr_bitmask,
1226         };
1227         struct rpc_message msg = {
1228                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1229                 .rpc_argp = &args,
1230                 .rpc_resp = fsstat,
1231         };
1232
1233         fsstat->fattr->valid = 0;
1234         return nfs4_map_errors(rpc_call_sync(server->client, &msg, 0));
1235 }
1236
1237 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1238                 struct nfs_fsinfo *fsinfo)
1239 {
1240         struct nfs4_fsinfo_arg args = {
1241                 .fh = fhandle,
1242                 .bitmask = server->attr_bitmask,
1243         };
1244         struct rpc_message msg = {
1245                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1246                 .rpc_argp = &args,
1247                 .rpc_resp = fsinfo,
1248         };
1249
1250         return nfs4_map_errors(rpc_call_sync(server->client, &msg, 0));
1251 }
1252
1253 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1254 {
1255         fsinfo->fattr->valid = 0;
1256         return nfs4_map_errors(nfs4_do_fsinfo(server, fhandle, fsinfo));
1257 }
1258
1259 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1260                 struct nfs_pathconf *pathconf)
1261 {
1262         struct nfs4_pathconf_arg args = {
1263                 .fh = fhandle,
1264                 .bitmask = server->attr_bitmask,
1265         };
1266         struct rpc_message msg = {
1267                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1268                 .rpc_argp = &args,
1269                 .rpc_resp = pathconf,
1270         };
1271
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));
1275                 return 0;
1276         }
1277
1278         pathconf->fattr->valid = 0;
1279         return nfs4_map_errors(rpc_call_sync(server->client, &msg, 0));
1280 }
1281
1282 static void
1283 nfs4_read_done(struct rpc_task *task)
1284 {
1285         struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
1286         struct inode *inode = data->inode;
1287
1288         if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1289                 rpc_restart_call(task);
1290                 return;
1291         }
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);
1296 }
1297
1298 static void
1299 nfs4_proc_read_setup(struct nfs_read_data *data)
1300 {
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,
1307         };
1308         struct inode *inode = data->inode;
1309         int flags;
1310
1311         data->timestamp   = jiffies;
1312
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);
1315
1316         /* Finalize the task. */
1317         rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
1318         rpc_call_setup(task, &msg, 0);
1319 }
1320
1321 static void
1322 nfs4_write_done(struct rpc_task *task)
1323 {
1324         struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1325         struct inode *inode = data->inode;
1326         
1327         if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1328                 rpc_restart_call(task);
1329                 return;
1330         }
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);
1335 }
1336
1337 static void
1338 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
1339 {
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,
1346         };
1347         struct inode *inode = data->inode;
1348         int stable;
1349         int flags;
1350         
1351         if (how & FLUSH_STABLE) {
1352                 if (!NFS_I(inode)->ncommit)
1353                         stable = NFS_FILE_SYNC;
1354                 else
1355                         stable = NFS_DATA_SYNC;
1356         } else
1357                 stable = NFS_UNSTABLE;
1358         data->args.stable = stable;
1359
1360         data->timestamp   = jiffies;
1361
1362         /* Set the initial flags for the task.  */
1363         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1364
1365         /* Finalize the task. */
1366         rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
1367         rpc_call_setup(task, &msg, 0);
1368 }
1369
1370 static void
1371 nfs4_commit_done(struct rpc_task *task)
1372 {
1373         struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1374         struct inode *inode = data->inode;
1375         
1376         if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1377                 rpc_restart_call(task);
1378                 return;
1379         }
1380         /* Call back common NFS writeback processing */
1381         nfs_commit_done(task);
1382 }
1383
1384 static void
1385 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
1386 {
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,
1393         };      
1394         struct inode *inode = data->inode;
1395         int flags;
1396         
1397         /* Set the initial flags for the task.  */
1398         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1399
1400         /* Finalize the task. */
1401         rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
1402         rpc_call_setup(task, &msg, 0);  
1403 }
1404
1405 /*
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.
1408  */
1409 static void
1410 renew_done(struct rpc_task *task)
1411 {
1412         struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
1413         unsigned long timestamp = (unsigned long)task->tk_calldata;
1414
1415         if (task->tk_status < 0) {
1416                 switch (task->tk_status) {
1417                         case -NFS4ERR_STALE_CLIENTID:
1418                                 nfs4_schedule_state_recovery(clp);
1419                                 return;
1420                 }
1421         }
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);
1426 }
1427
1428 int
1429 nfs4_proc_async_renew(struct nfs4_client *clp)
1430 {
1431         struct rpc_message msg = {
1432                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
1433                 .rpc_argp       = clp,
1434                 .rpc_cred       = clp->cl_cred,
1435         };
1436
1437         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
1438                         renew_done, (void *)jiffies);
1439 }
1440
1441 int
1442 nfs4_proc_renew(struct nfs4_client *clp)
1443 {
1444         struct rpc_message msg = {
1445                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
1446                 .rpc_argp       = clp,
1447                 .rpc_cred       = clp->cl_cred,
1448         };
1449         unsigned long now = jiffies;
1450         int status;
1451
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);
1457         return status;
1458 }
1459
1460 /*
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.
1465  */
1466 static int
1467 nfs4_proc_file_open(struct inode *inode, struct file *filp)
1468 {
1469         struct dentry *dentry = filp->f_dentry;
1470         struct nfs4_state *state;
1471         struct rpc_cred *cred;
1472
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);
1477
1478
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);
1482         put_rpccred(cred);
1483         if (state == NULL) {
1484                 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
1485                 return -EIO; /* ERACE actually */
1486         }
1487         nfs4_close_state(state, filp->f_mode);
1488         if (filp->f_mode & FMODE_WRITE) {
1489                 lock_kernel();
1490                 nfs_set_mmcred(inode, state->owner->so_cred);
1491                 nfs_begin_data_update(inode);
1492                 unlock_kernel();
1493         }
1494         filp->private_data = state;
1495         return 0;
1496 }
1497
1498 /*
1499  * Release our state
1500  */
1501 static int
1502 nfs4_proc_file_release(struct inode *inode, struct file *filp)
1503 {
1504         struct nfs4_state *state = (struct nfs4_state *)filp->private_data;
1505
1506         if (state)
1507                 nfs4_close_state(state, filp->f_mode);
1508         if (filp->f_mode & FMODE_WRITE) {
1509                 lock_kernel();
1510                 nfs_end_data_update(inode);
1511                 unlock_kernel();
1512         }
1513         return 0;
1514 }
1515
1516 /*
1517  * Set up the nfspage struct with the right state info and credentials
1518  */
1519 static void
1520 nfs4_request_init(struct nfs_page *req, struct file *filp)
1521 {
1522         struct nfs4_state *state;
1523
1524         if (!filp) {
1525                 req->wb_cred = get_rpccred(NFS_I(req->wb_inode)->mm_cred);
1526                 req->wb_state = NULL;
1527                 return;
1528         }
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;
1533 }
1534
1535 static int
1536 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
1537 {
1538         struct nfs4_client *clp = server->nfs4_state;
1539
1540         if (!clp || task->tk_status >= 0)
1541                 return 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;
1549                         return -EAGAIN;
1550                 case -NFS4ERR_GRACE:
1551                 case -NFS4ERR_DELAY:
1552                         rpc_delay(task, NFS4_POLL_RETRY_TIME);
1553                         task->tk_status = 0;
1554                         return -EAGAIN;
1555                 case -NFS4ERR_OLD_STATEID:
1556                         task->tk_status = 0;
1557                         return -EAGAIN;
1558         }
1559         task->tk_status = nfs4_map_errors(task->tk_status);
1560         return 0;
1561 }
1562
1563 int
1564 nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
1565 {
1566         DEFINE_WAIT(wait);
1567         sigset_t oldset;
1568         int interruptible, res;
1569
1570         might_sleep();
1571
1572         rpc_clnt_sigmask(clnt, &oldset);
1573         interruptible = TASK_UNINTERRUPTIBLE;
1574         if (clnt->cl_intr)
1575                 interruptible = TASK_INTERRUPTIBLE;
1576         do {
1577                 res = 0;
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))
1582                         break;
1583                 if (clnt->cl_intr && signalled()) {
1584                         res = -ERESTARTSYS;
1585                         break;
1586                 }
1587                 schedule();
1588         } while(!test_bit(NFS4CLNT_OK, &clp->cl_state));
1589         finish_wait(&clp->cl_waitq, &wait);
1590         rpc_clnt_sigunmask(clnt, &oldset);
1591         return res;
1592 }
1593
1594 static int
1595 nfs4_delay(struct rpc_clnt *clnt)
1596 {
1597         sigset_t oldset;
1598         int res = 0;
1599
1600         might_sleep();
1601
1602         rpc_clnt_sigmask(clnt, &oldset);
1603         if (clnt->cl_intr) {
1604                 set_current_state(TASK_INTERRUPTIBLE);
1605                 schedule_timeout(NFS4_POLL_RETRY_TIME);
1606                 if (signalled())
1607                         res = -ERESTARTSYS;
1608         } else {
1609                 set_current_state(TASK_UNINTERRUPTIBLE);
1610                 schedule_timeout(NFS4_POLL_RETRY_TIME);
1611         }
1612         rpc_clnt_sigunmask(clnt, &oldset);
1613         return res;
1614 }
1615
1616 /* This is the error handling routine for processes that are allowed
1617  * to sleep.
1618  */
1619 int
1620 nfs4_handle_error(struct nfs_server *server, int errorcode)
1621 {
1622         struct nfs4_client *clp = server->nfs4_state;
1623         int ret = errorcode;
1624
1625         switch(errorcode) {
1626                 case -NFS4ERR_STALE_CLIENTID:
1627                 case -NFS4ERR_STALE_STATEID:
1628                 case -NFS4ERR_EXPIRED:
1629                         ret = nfs4_wait_clnt_recover(server->client, clp);
1630                         break;
1631                 case -NFS4ERR_GRACE:
1632                 case -NFS4ERR_DELAY:
1633                         ret = nfs4_delay(server->client);
1634                         break;
1635                 case -NFS4ERR_OLD_STATEID:
1636                         ret = 0;
1637         }
1638         /* We failed to handle the error */
1639         return nfs4_map_errors(ret);
1640 }
1641
1642
1643 static int
1644 nfs4_request_compatible(struct nfs_page *req, struct file *filp, struct page *page)
1645 {
1646         struct nfs4_state *state = NULL;
1647         struct rpc_cred *cred = NULL;
1648
1649         if (req->wb_file != filp)
1650                 return 0;
1651         if (req->wb_page != page)
1652                 return 0;
1653         state = (struct nfs4_state *)filp->private_data;
1654         if (req->wb_state != state)
1655                 return 0;
1656         if (req->wb_lockowner != current->files)
1657                 return 0;
1658         cred = state->owner->so_cred;
1659         if (req->wb_cred != cred)
1660                 return 0;
1661         return 1;
1662 }
1663
1664 int
1665 nfs4_proc_setclientid(struct nfs4_client *clp,
1666                 u32 program, unsigned short port)
1667 {
1668         u32 *p;
1669         struct nfs4_setclientid setclientid;
1670         struct timespec tv;
1671         struct rpc_message msg = {
1672                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
1673                 .rpc_argp = &setclientid,
1674                 .rpc_resp = clp,
1675                 .rpc_cred = clp->cl_cred,
1676         };
1677
1678         tv = CURRENT_TIME;
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;
1687
1688         return rpc_call_sync(clp->cl_rpcclient, &msg, 0);
1689 }
1690
1691 int
1692 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
1693 {
1694         struct nfs_fsinfo fsinfo;
1695         struct rpc_message msg = {
1696                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
1697                 .rpc_argp = clp,
1698                 .rpc_resp = &fsinfo,
1699                 .rpc_cred = clp->cl_cred,
1700         };
1701         unsigned long now;
1702         int status;
1703
1704         now = jiffies;
1705         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
1706         if (status == 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);
1711         }
1712         return status;
1713 }
1714
1715 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
1716 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
1717
1718 /* 
1719  * sleep, with exponential backoff, and retry the LOCK operation. 
1720  */
1721 static unsigned long
1722 nfs4_set_lock_task_retry(unsigned long timeout)
1723 {
1724         current->state = TASK_INTERRUPTIBLE;
1725         schedule_timeout(timeout);
1726         timeout <<= 1;
1727         if (timeout > NFS4_LOCK_MAXTIMEOUT)
1728                 return NFS4_LOCK_MAXTIMEOUT;
1729         return timeout;
1730 }
1731
1732 static inline int
1733 nfs4_lck_type(int cmd, struct file_lock *request)
1734 {
1735         /* set lock type */
1736         switch (request->fl_type) {
1737                 case F_RDLCK:
1738                         return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
1739                 case F_WRLCK:
1740                         return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
1741                 case F_UNLCK:
1742                         return NFS4_WRITE_LT; 
1743         }
1744         BUG();
1745         return 0;
1746 }
1747
1748 static inline uint64_t
1749 nfs4_lck_length(struct file_lock *request)
1750 {
1751         if (request->fl_end == OFFSET_MAX)
1752                 return ~(uint64_t)0;
1753         return request->fl_end - request->fl_start + 1;
1754 }
1755
1756 int
1757 nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
1758 {
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),
1767         };
1768         struct nfs_lockres res = {
1769                 .server = server,
1770         };
1771         struct rpc_message msg = {
1772                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
1773                 .rpc_argp       = &arg,
1774                 .rpc_resp       = &res,
1775                 .rpc_cred       = state->owner->so_cred,
1776         };
1777         struct nfs_lowner nlo;
1778         struct nfs4_lock_state *lsp;
1779         int status;
1780
1781         nlo.clientid = clp->cl_clientid;
1782         down(&state->lock_sema);
1783         lsp = nfs4_find_lock_state(state, request->fl_owner);
1784         if (lsp)
1785                 nlo.id = lsp->ls_id; 
1786         else {
1787                 spin_lock(&clp->cl_lock);
1788                 nlo.id = nfs4_alloc_lockowner_id(clp);
1789                 spin_unlock(&clp->cl_lock);
1790         }
1791         arg.u.lockt = &nlo;
1792         status = rpc_call_sync(server->client, &msg, 0);
1793         if (!status) {
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;
1802                 else
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;
1809                 status = 0;
1810         }
1811         if (lsp)
1812                 nfs4_put_lock_state(lsp);
1813         up(&state->lock_sema);
1814         return nfs4_map_errors(status);
1815 }
1816
1817 int
1818 nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
1819 {
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),
1827         };
1828         struct nfs_lockres res = {
1829                 .server = server,
1830         };
1831         struct rpc_message msg = {
1832                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
1833                 .rpc_argp       = &arg,
1834                 .rpc_resp       = &res,
1835                 .rpc_cred       = state->owner->so_cred,
1836         };
1837         struct nfs4_lock_state *lsp;
1838         struct nfs_locku_opargs luargs;
1839         int status = 0;
1840                         
1841         down(&state->lock_sema);
1842         lsp = nfs4_find_lock_state(state, request->fl_owner);
1843         if (!lsp)
1844                 goto out;
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);
1850
1851         if (status == 0) {
1852                 memcpy(&lsp->ls_stateid,  &res.u.stateid, 
1853                                 sizeof(lsp->ls_stateid));
1854                 nfs4_notify_unlck(inode, request, lsp);
1855         }
1856         nfs4_put_lock_state(lsp);
1857 out:
1858         up(&state->lock_sema);
1859         return nfs4_map_errors(status);
1860 }
1861
1862 static int
1863 nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
1864 {
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),
1873         };
1874         struct nfs_lockres res = {
1875                 .server = server,
1876         };
1877         struct rpc_message msg = {
1878                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
1879                 .rpc_argp       = &arg,
1880                 .rpc_resp       = &res,
1881                 .rpc_cred       = state->owner->so_cred,
1882         };
1883         struct nfs_lock_opargs largs = {
1884                 .new_lock_owner = 0,
1885         };
1886         int status;
1887
1888         down(&state->lock_sema);
1889         lsp = nfs4_find_lock_state(state, request->fl_owner);
1890         if (lsp == NULL) {
1891                 struct nfs4_state_owner *owner = state->owner;
1892                 struct nfs_open_to_lock otl = {
1893                         .lock_owner = {
1894                                 .clientid = server->nfs4_state->cl_clientid,
1895                         },
1896                 };
1897                 status = -ENOMEM;
1898                 lsp = nfs4_alloc_lock_state(state, request->fl_owner);
1899                 if (!lsp)
1900                         goto out;
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);
1914         } else {
1915                 struct nfs_exist_lock el = {
1916                         .seqid = lsp->ls_seqid,
1917                 };
1918                 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
1919                 largs.u.exist_lock = &el;
1920                 largs.new_lock_owner = 0;
1921                 arg.u.lock = &largs;
1922                 status = rpc_call_sync(server->client, &msg, 0);
1923         }
1924         /* increment seqid on success, and * seqid mutating errors*/
1925         nfs4_increment_lock_seqid(status, lsp);
1926         /* save the returned stateid. */
1927         if (status == 0) {
1928                 memcpy(&lsp->ls_stateid, &res.u.stateid, sizeof(nfs4_stateid));
1929                 nfs4_notify_setlk(inode, request, lsp);
1930         } else if (status == -NFS4ERR_DENIED)
1931                 status = -EAGAIN;
1932         nfs4_put_lock_state(lsp);
1933 out:
1934         up(&state->lock_sema);
1935         return nfs4_map_errors(status);
1936 }
1937
1938 static int
1939 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
1940 {
1941         struct nfs4_state *state;
1942         unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
1943         int status;
1944
1945         /* verify open state */
1946         state = (struct nfs4_state *)filp->private_data;
1947         BUG_ON(!state);
1948
1949         if (request->fl_start < 0 || request->fl_end < 0)
1950                 return -EINVAL;
1951
1952         if (IS_GETLK(cmd))
1953                 return nfs4_proc_getlk(state, F_GETLK, request);
1954
1955         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
1956                 return -EINVAL;
1957
1958         if (request->fl_type == F_UNLCK)
1959                 return nfs4_proc_unlck(state, cmd, request);
1960
1961         do {
1962                 status = nfs4_proc_setlk(state, cmd, request);
1963                 if ((status != -EAGAIN) || IS_SETLK(cmd))
1964                         break;
1965                 timeout = nfs4_set_lock_task_retry(timeout);
1966                 status = -ERESTARTSYS;
1967                 if (signalled())
1968                         break;
1969         } while(status < 0);
1970
1971         return status;
1972 }
1973
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,
2010 };
2011
2012 /*
2013  * Local variables:
2014  *  c-basic-offset: 8
2015  * End:
2016  */