NFSv4: Remove nfs4_client->cl_sem from close() path
[safe/jmp/linux-2.6] / 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/delay.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/smp_lock.h>
49 #include <linux/namei.h>
50
51 #include "nfs4_fs.h"
52 #include "delegation.h"
53
54 #define NFSDBG_FACILITY         NFSDBG_PROC
55
56 #define NFS4_POLL_RETRY_MIN     (1*HZ)
57 #define NFS4_POLL_RETRY_MAX     (15*HZ)
58
59 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
60 static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *);
61 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
62 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
63 extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
64 extern struct rpc_procinfo nfs4_procedures[];
65
66 /* Prevent leaks of NFSv4 errors into userland */
67 int nfs4_map_errors(int err)
68 {
69         if (err < -1000) {
70                 dprintk("%s could not handle NFSv4 error %d\n",
71                                 __FUNCTION__, -err);
72                 return -EIO;
73         }
74         return err;
75 }
76
77 /*
78  * This is our standard bitmap for GETATTR requests.
79  */
80 const u32 nfs4_fattr_bitmap[2] = {
81         FATTR4_WORD0_TYPE
82         | FATTR4_WORD0_CHANGE
83         | FATTR4_WORD0_SIZE
84         | FATTR4_WORD0_FSID
85         | FATTR4_WORD0_FILEID,
86         FATTR4_WORD1_MODE
87         | FATTR4_WORD1_NUMLINKS
88         | FATTR4_WORD1_OWNER
89         | FATTR4_WORD1_OWNER_GROUP
90         | FATTR4_WORD1_RAWDEV
91         | FATTR4_WORD1_SPACE_USED
92         | FATTR4_WORD1_TIME_ACCESS
93         | FATTR4_WORD1_TIME_METADATA
94         | FATTR4_WORD1_TIME_MODIFY
95 };
96
97 const u32 nfs4_statfs_bitmap[2] = {
98         FATTR4_WORD0_FILES_AVAIL
99         | FATTR4_WORD0_FILES_FREE
100         | FATTR4_WORD0_FILES_TOTAL,
101         FATTR4_WORD1_SPACE_AVAIL
102         | FATTR4_WORD1_SPACE_FREE
103         | FATTR4_WORD1_SPACE_TOTAL
104 };
105
106 const u32 nfs4_pathconf_bitmap[2] = {
107         FATTR4_WORD0_MAXLINK
108         | FATTR4_WORD0_MAXNAME,
109         0
110 };
111
112 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
113                         | FATTR4_WORD0_MAXREAD
114                         | FATTR4_WORD0_MAXWRITE
115                         | FATTR4_WORD0_LEASE_TIME,
116                         0
117 };
118
119 static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
120                 struct nfs4_readdir_arg *readdir)
121 {
122         u32 *start, *p;
123
124         BUG_ON(readdir->count < 80);
125         if (cookie > 2) {
126                 readdir->cookie = cookie;
127                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
128                 return;
129         }
130
131         readdir->cookie = 0;
132         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
133         if (cookie == 2)
134                 return;
135         
136         /*
137          * NFSv4 servers do not return entries for '.' and '..'
138          * Therefore, we fake these entries here.  We let '.'
139          * have cookie 0 and '..' have cookie 1.  Note that
140          * when talking to the server, we always send cookie 0
141          * instead of 1 or 2.
142          */
143         start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
144         
145         if (cookie == 0) {
146                 *p++ = xdr_one;                                  /* next */
147                 *p++ = xdr_zero;                   /* cookie, first word */
148                 *p++ = xdr_one;                   /* cookie, second word */
149                 *p++ = xdr_one;                             /* entry len */
150                 memcpy(p, ".\0\0\0", 4);                        /* entry */
151                 p++;
152                 *p++ = xdr_one;                         /* bitmap length */
153                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
154                 *p++ = htonl(8);              /* attribute buffer length */
155                 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
156         }
157         
158         *p++ = xdr_one;                                  /* next */
159         *p++ = xdr_zero;                   /* cookie, first word */
160         *p++ = xdr_two;                   /* cookie, second word */
161         *p++ = xdr_two;                             /* entry len */
162         memcpy(p, "..\0\0", 4);                         /* entry */
163         p++;
164         *p++ = xdr_one;                         /* bitmap length */
165         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
166         *p++ = htonl(8);              /* attribute buffer length */
167         p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
168
169         readdir->pgbase = (char *)p - (char *)start;
170         readdir->count -= readdir->pgbase;
171         kunmap_atomic(start, KM_USER0);
172 }
173
174 static void
175 renew_lease(struct nfs_server *server, unsigned long timestamp)
176 {
177         struct nfs4_client *clp = server->nfs4_state;
178         spin_lock(&clp->cl_lock);
179         if (time_before(clp->cl_last_renewal,timestamp))
180                 clp->cl_last_renewal = timestamp;
181         spin_unlock(&clp->cl_lock);
182 }
183
184 static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
185 {
186         struct nfs_inode *nfsi = NFS_I(inode);
187
188         if (cinfo->before == nfsi->change_attr && cinfo->atomic)
189                 nfsi->change_attr = cinfo->after;
190 }
191
192 /* Helper for asynchronous RPC calls */
193 static int nfs4_call_async(struct rpc_clnt *clnt, rpc_action tk_begin,
194                 rpc_action tk_exit, void *calldata)
195 {
196         struct rpc_task *task;
197
198         if (!(task = rpc_new_task(clnt, tk_exit, RPC_TASK_ASYNC)))
199                 return -ENOMEM;
200
201         task->tk_calldata = calldata;
202         task->tk_action = tk_begin;
203         rpc_execute(task);
204         return 0;
205 }
206
207 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
208 {
209         struct inode *inode = state->inode;
210
211         open_flags &= (FMODE_READ|FMODE_WRITE);
212         /* Protect against nfs4_find_state() */
213         spin_lock(&inode->i_lock);
214         state->state |= open_flags;
215         /* NB! List reordering - see the reclaim code for why.  */
216         if ((open_flags & FMODE_WRITE) && 0 == state->nwriters++)
217                 list_move(&state->open_states, &state->owner->so_states);
218         if (open_flags & FMODE_READ)
219                 state->nreaders++;
220         memcpy(&state->stateid, stateid, sizeof(state->stateid));
221         spin_unlock(&inode->i_lock);
222 }
223
224 /*
225  * OPEN_RECLAIM:
226  *      reclaim state on the server after a reboot.
227  */
228 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
229 {
230         struct inode *inode = state->inode;
231         struct nfs_server *server = NFS_SERVER(inode);
232         struct nfs_delegation *delegation = NFS_I(inode)->delegation;
233         struct nfs_openargs o_arg = {
234                 .fh = NFS_FH(inode),
235                 .id = sp->so_id,
236                 .open_flags = state->state,
237                 .clientid = server->nfs4_state->cl_clientid,
238                 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
239                 .bitmask = server->attr_bitmask,
240         };
241         struct nfs_openres o_res = {
242                 .server = server,       /* Grrr */
243         };
244         struct rpc_message msg = {
245                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
246                 .rpc_argp       = &o_arg,
247                 .rpc_resp       = &o_res,
248                 .rpc_cred       = sp->so_cred,
249         };
250         int status;
251
252         if (delegation != NULL) {
253                 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
254                         memcpy(&state->stateid, &delegation->stateid,
255                                         sizeof(state->stateid));
256                         set_bit(NFS_DELEGATED_STATE, &state->flags);
257                         return 0;
258                 }
259                 o_arg.u.delegation_type = delegation->type;
260         }
261         o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
262         if (o_arg.seqid == NULL)
263                 return -ENOMEM;
264         status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
265         /* Confirm the sequence as being established */
266         nfs_confirm_seqid(&sp->so_seqid, status);
267         nfs_increment_open_seqid(status, o_arg.seqid);
268         if (status == 0) {
269                 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
270                 if (o_res.delegation_type != 0) {
271                         nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
272                         /* Did the server issue an immediate delegation recall? */
273                         if (o_res.do_recall)
274                                 nfs_async_inode_return_delegation(inode, &o_res.stateid);
275                 }
276         }
277         nfs_free_seqid(o_arg.seqid);
278         clear_bit(NFS_DELEGATED_STATE, &state->flags);
279         /* Ensure we update the inode attributes */
280         NFS_CACHEINV(inode);
281         return status;
282 }
283
284 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
285 {
286         struct nfs_server *server = NFS_SERVER(state->inode);
287         struct nfs4_exception exception = { };
288         int err;
289         do {
290                 err = _nfs4_open_reclaim(sp, state);
291                 if (err != -NFS4ERR_DELAY)
292                         break;
293                 nfs4_handle_exception(server, err, &exception);
294         } while (exception.retry);
295         return err;
296 }
297
298 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
299 {
300         struct nfs4_state_owner  *sp  = state->owner;
301         struct inode *inode = dentry->d_inode;
302         struct nfs_server *server = NFS_SERVER(inode);
303         struct dentry *parent = dget_parent(dentry);
304         struct nfs_openargs arg = {
305                 .fh = NFS_FH(parent->d_inode),
306                 .clientid = server->nfs4_state->cl_clientid,
307                 .name = &dentry->d_name,
308                 .id = sp->so_id,
309                 .server = server,
310                 .bitmask = server->attr_bitmask,
311                 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
312         };
313         struct nfs_openres res = {
314                 .server = server,
315         };
316         struct  rpc_message msg = {
317                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
318                 .rpc_argp       = &arg,
319                 .rpc_resp       = &res,
320                 .rpc_cred       = sp->so_cred,
321         };
322         int status = 0;
323
324         if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
325                 goto out;
326         if (state->state == 0)
327                 goto out;
328         arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
329         status = -ENOMEM;
330         if (arg.seqid == NULL)
331                 goto out;
332         arg.open_flags = state->state;
333         memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
334         status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
335         nfs_increment_open_seqid(status, arg.seqid);
336         if (status >= 0) {
337                 memcpy(state->stateid.data, res.stateid.data,
338                                 sizeof(state->stateid.data));
339                 clear_bit(NFS_DELEGATED_STATE, &state->flags);
340         }
341         nfs_free_seqid(arg.seqid);
342 out:
343         dput(parent);
344         return status;
345 }
346
347 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
348 {
349         struct nfs4_exception exception = { };
350         struct nfs_server *server = NFS_SERVER(dentry->d_inode);
351         int err;
352         do {
353                 err = _nfs4_open_delegation_recall(dentry, state);
354                 switch (err) {
355                         case 0:
356                                 return err;
357                         case -NFS4ERR_STALE_CLIENTID:
358                         case -NFS4ERR_STALE_STATEID:
359                         case -NFS4ERR_EXPIRED:
360                                 /* Don't recall a delegation if it was lost */
361                                 nfs4_schedule_state_recovery(server->nfs4_state);
362                                 return err;
363                 }
364                 err = nfs4_handle_exception(server, err, &exception);
365         } while (exception.retry);
366         return err;
367 }
368
369 static inline int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid)
370 {
371         struct nfs_open_confirmargs arg = {
372                 .fh             = fh,
373                 .seqid          = seqid,
374                 .stateid        = *stateid,
375         };
376         struct nfs_open_confirmres res;
377         struct  rpc_message msg = {
378                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
379                 .rpc_argp       = &arg,
380                 .rpc_resp       = &res,
381                 .rpc_cred       = sp->so_cred,
382         };
383         int status;
384
385         status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
386         /* Confirm the sequence as being established */
387         nfs_confirm_seqid(&sp->so_seqid, status);
388         nfs_increment_open_seqid(status, seqid);
389         if (status >= 0)
390                 memcpy(stateid, &res.stateid, sizeof(*stateid));
391         return status;
392 }
393
394 static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner  *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
395 {
396         struct nfs_server *server = NFS_SERVER(dir);
397         struct rpc_message msg = {
398                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
399                 .rpc_argp = o_arg,
400                 .rpc_resp = o_res,
401                 .rpc_cred = sp->so_cred,
402         };
403         int status;
404
405         /* Update sequence id. The caller must serialize! */
406         o_arg->id = sp->so_id;
407         o_arg->clientid = sp->so_client->cl_clientid;
408
409         status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
410         nfs_increment_open_seqid(status, o_arg->seqid);
411         if (status != 0)
412                 goto out;
413         update_changeattr(dir, &o_res->cinfo);
414         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
415                 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
416                                 sp, &o_res->stateid, o_arg->seqid);
417                 if (status != 0)
418                         goto out;
419         }
420         nfs_confirm_seqid(&sp->so_seqid, 0);
421         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
422                 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
423 out:
424         return status;
425 }
426
427 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
428 {
429         struct nfs_access_entry cache;
430         int mask = 0;
431         int status;
432
433         if (openflags & FMODE_READ)
434                 mask |= MAY_READ;
435         if (openflags & FMODE_WRITE)
436                 mask |= MAY_WRITE;
437         status = nfs_access_get_cached(inode, cred, &cache);
438         if (status == 0)
439                 goto out;
440
441         /* Be clever: ask server to check for all possible rights */
442         cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
443         cache.cred = cred;
444         cache.jiffies = jiffies;
445         status = _nfs4_proc_access(inode, &cache);
446         if (status != 0)
447                 return status;
448         nfs_access_add_cache(inode, &cache);
449 out:
450         if ((cache.mask & mask) == mask)
451                 return 0;
452         return -EACCES;
453 }
454
455 /*
456  * OPEN_EXPIRED:
457  *      reclaim state on the server after a network partition.
458  *      Assumes caller holds the appropriate lock
459  */
460 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
461 {
462         struct dentry *parent = dget_parent(dentry);
463         struct inode *dir = parent->d_inode;
464         struct inode *inode = state->inode;
465         struct nfs_server *server = NFS_SERVER(dir);
466         struct nfs_delegation *delegation = NFS_I(inode)->delegation;
467         struct nfs_fattr        f_attr = {
468                 .valid = 0,
469         };
470         struct nfs_openargs o_arg = {
471                 .fh = NFS_FH(dir),
472                 .open_flags = state->state,
473                 .name = &dentry->d_name,
474                 .bitmask = server->attr_bitmask,
475                 .claim = NFS4_OPEN_CLAIM_NULL,
476         };
477         struct nfs_openres o_res = {
478                 .f_attr = &f_attr,
479                 .server = server,
480         };
481         int status = 0;
482
483         if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
484                 status = _nfs4_do_access(inode, sp->so_cred, state->state);
485                 if (status < 0)
486                         goto out;
487                 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
488                 set_bit(NFS_DELEGATED_STATE, &state->flags);
489                 goto out;
490         }
491         o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
492         status = -ENOMEM;
493         if (o_arg.seqid == NULL)
494                 goto out;
495         status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
496         if (status != 0)
497                 goto out_nodeleg;
498         /* Check if files differ */
499         if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
500                 goto out_stale;
501         /* Has the file handle changed? */
502         if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
503                 /* Verify if the change attributes are the same */
504                 if (f_attr.change_attr != NFS_I(inode)->change_attr)
505                         goto out_stale;
506                 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
507                         goto out_stale;
508                 /* Lets just pretend that this is the same file */
509                 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
510                 NFS_I(inode)->fileid = f_attr.fileid;
511         }
512         memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
513         if (o_res.delegation_type != 0) {
514                 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
515                         nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
516                 else
517                         nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
518         }
519 out_nodeleg:
520         nfs_free_seqid(o_arg.seqid);
521         clear_bit(NFS_DELEGATED_STATE, &state->flags);
522 out:
523         dput(parent);
524         return status;
525 out_stale:
526         status = -ESTALE;
527         /* Invalidate the state owner so we don't ever use it again */
528         nfs4_drop_state_owner(sp);
529         d_drop(dentry);
530         /* Should we be trying to close that stateid? */
531         goto out_nodeleg;
532 }
533
534 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
535 {
536         struct nfs_server *server = NFS_SERVER(dentry->d_inode);
537         struct nfs4_exception exception = { };
538         int err;
539
540         do {
541                 err = _nfs4_open_expired(sp, state, dentry);
542                 if (err == -NFS4ERR_DELAY)
543                         nfs4_handle_exception(server, err, &exception);
544         } while (exception.retry);
545         return err;
546 }
547
548 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
549 {
550         struct nfs_inode *nfsi = NFS_I(state->inode);
551         struct nfs_open_context *ctx;
552         int status;
553
554         spin_lock(&state->inode->i_lock);
555         list_for_each_entry(ctx, &nfsi->open_files, list) {
556                 if (ctx->state != state)
557                         continue;
558                 get_nfs_open_context(ctx);
559                 spin_unlock(&state->inode->i_lock);
560                 status = nfs4_do_open_expired(sp, state, ctx->dentry);
561                 put_nfs_open_context(ctx);
562                 return status;
563         }
564         spin_unlock(&state->inode->i_lock);
565         return -ENOENT;
566 }
567
568 /*
569  * Returns an nfs4_state + an extra reference to the inode
570  */
571 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
572 {
573         struct nfs_delegation *delegation;
574         struct nfs_server *server = NFS_SERVER(inode);
575         struct nfs4_client *clp = server->nfs4_state;
576         struct nfs_inode *nfsi = NFS_I(inode);
577         struct nfs4_state_owner *sp = NULL;
578         struct nfs4_state *state = NULL;
579         int open_flags = flags & (FMODE_READ|FMODE_WRITE);
580         int err;
581
582         /* Protect against reboot recovery - NOTE ORDER! */
583         down_read(&clp->cl_sem);
584         /* Protect against delegation recall */
585         down_read(&nfsi->rwsem);
586         delegation = NFS_I(inode)->delegation;
587         err = -ENOENT;
588         if (delegation == NULL || (delegation->type & open_flags) != open_flags)
589                 goto out_err;
590         err = -ENOMEM;
591         if (!(sp = nfs4_get_state_owner(server, cred))) {
592                 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
593                 goto out_err;
594         }
595         state = nfs4_get_open_state(inode, sp);
596         if (state == NULL)
597                 goto out_err;
598
599         err = -ENOENT;
600         if ((state->state & open_flags) == open_flags) {
601                 spin_lock(&inode->i_lock);
602                 if (open_flags & FMODE_READ)
603                         state->nreaders++;
604                 if (open_flags & FMODE_WRITE)
605                         state->nwriters++;
606                 spin_unlock(&inode->i_lock);
607                 goto out_ok;
608         } else if (state->state != 0)
609                 goto out_err;
610
611         lock_kernel();
612         err = _nfs4_do_access(inode, cred, open_flags);
613         unlock_kernel();
614         if (err != 0)
615                 goto out_err;
616         set_bit(NFS_DELEGATED_STATE, &state->flags);
617         update_open_stateid(state, &delegation->stateid, open_flags);
618 out_ok:
619         nfs4_put_state_owner(sp);
620         up_read(&nfsi->rwsem);
621         up_read(&clp->cl_sem);
622         igrab(inode);
623         *res = state;
624         return 0; 
625 out_err:
626         if (sp != NULL) {
627                 if (state != NULL)
628                         nfs4_put_open_state(state);
629                 nfs4_put_state_owner(sp);
630         }
631         up_read(&nfsi->rwsem);
632         up_read(&clp->cl_sem);
633         return err;
634 }
635
636 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
637 {
638         struct nfs4_exception exception = { };
639         struct nfs4_state *res;
640         int err;
641
642         do {
643                 err = _nfs4_open_delegated(inode, flags, cred, &res);
644                 if (err == 0)
645                         break;
646                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
647                                         err, &exception));
648         } while (exception.retry);
649         return res;
650 }
651
652 /*
653  * Returns an nfs4_state + an referenced inode
654  */
655 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
656 {
657         struct nfs4_state_owner  *sp;
658         struct nfs4_state     *state = NULL;
659         struct nfs_server       *server = NFS_SERVER(dir);
660         struct nfs4_client *clp = server->nfs4_state;
661         struct inode *inode = NULL;
662         int                     status;
663         struct nfs_fattr        f_attr = {
664                 .valid          = 0,
665         };
666         struct nfs_openargs o_arg = {
667                 .fh             = NFS_FH(dir),
668                 .open_flags     = flags,
669                 .name           = &dentry->d_name,
670                 .server         = server,
671                 .bitmask = server->attr_bitmask,
672                 .claim = NFS4_OPEN_CLAIM_NULL,
673         };
674         struct nfs_openres o_res = {
675                 .f_attr         = &f_attr,
676                 .server         = server,
677         };
678
679         /* Protect against reboot recovery conflicts */
680         down_read(&clp->cl_sem);
681         status = -ENOMEM;
682         if (!(sp = nfs4_get_state_owner(server, cred))) {
683                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
684                 goto out_err;
685         }
686         if (flags & O_EXCL) {
687                 u32 *p = (u32 *) o_arg.u.verifier.data;
688                 p[0] = jiffies;
689                 p[1] = current->pid;
690         } else
691                 o_arg.u.attrs = sattr;
692         /* Serialization for the sequence id */
693
694         o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
695         if (o_arg.seqid == NULL)
696                 return -ENOMEM;
697         status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
698         if (status != 0)
699                 goto out_err;
700
701         status = -ENOMEM;
702         inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
703         if (!inode)
704                 goto out_err;
705         state = nfs4_get_open_state(inode, sp);
706         if (!state)
707                 goto out_err;
708         update_open_stateid(state, &o_res.stateid, flags);
709         if (o_res.delegation_type != 0)
710                 nfs_inode_set_delegation(inode, cred, &o_res);
711         nfs_free_seqid(o_arg.seqid);
712         nfs4_put_state_owner(sp);
713         up_read(&clp->cl_sem);
714         *res = state;
715         return 0;
716 out_err:
717         if (sp != NULL) {
718                 if (state != NULL)
719                         nfs4_put_open_state(state);
720                 nfs_free_seqid(o_arg.seqid);
721                 nfs4_put_state_owner(sp);
722         }
723         /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
724         up_read(&clp->cl_sem);
725         if (inode != NULL)
726                 iput(inode);
727         *res = NULL;
728         return status;
729 }
730
731
732 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
733 {
734         struct nfs4_exception exception = { };
735         struct nfs4_state *res;
736         int status;
737
738         do {
739                 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
740                 if (status == 0)
741                         break;
742                 /* NOTE: BAD_SEQID means the server and client disagree about the
743                  * book-keeping w.r.t. state-changing operations
744                  * (OPEN/CLOSE/LOCK/LOCKU...)
745                  * It is actually a sign of a bug on the client or on the server.
746                  *
747                  * If we receive a BAD_SEQID error in the particular case of
748                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
749                  * have unhashed the old state_owner for us, and that we can
750                  * therefore safely retry using a new one. We should still warn
751                  * the user though...
752                  */
753                 if (status == -NFS4ERR_BAD_SEQID) {
754                         printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
755                         exception.retry = 1;
756                         continue;
757                 }
758                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
759                                         status, &exception));
760         } while (exception.retry);
761         return res;
762 }
763
764 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
765                 struct nfs_fh *fhandle, struct iattr *sattr,
766                 struct nfs4_state *state)
767 {
768         struct nfs_setattrargs  arg = {
769                 .fh             = fhandle,
770                 .iap            = sattr,
771                 .server         = server,
772                 .bitmask = server->attr_bitmask,
773         };
774         struct nfs_setattrres  res = {
775                 .fattr          = fattr,
776                 .server         = server,
777         };
778         struct rpc_message msg = {
779                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
780                 .rpc_argp       = &arg,
781                 .rpc_resp       = &res,
782         };
783         int status;
784
785         fattr->valid = 0;
786
787         if (state != NULL) {
788                 msg.rpc_cred = state->owner->so_cred;
789                 nfs4_copy_stateid(&arg.stateid, state, current->files);
790         } else
791                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
792
793         status = rpc_call_sync(server->client, &msg, 0);
794         return status;
795 }
796
797 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
798                 struct nfs_fh *fhandle, struct iattr *sattr,
799                 struct nfs4_state *state)
800 {
801         struct nfs4_exception exception = { };
802         int err;
803         do {
804                 err = nfs4_handle_exception(server,
805                                 _nfs4_do_setattr(server, fattr, fhandle, sattr,
806                                         state),
807                                 &exception);
808         } while (exception.retry);
809         return err;
810 }
811
812 struct nfs4_closedata {
813         struct inode *inode;
814         struct nfs4_state *state;
815         struct nfs_closeargs arg;
816         struct nfs_closeres res;
817 };
818
819 static void nfs4_free_closedata(struct nfs4_closedata *calldata)
820 {
821         struct nfs4_state *state = calldata->state;
822         struct nfs4_state_owner *sp = state->owner;
823
824         nfs4_put_open_state(calldata->state);
825         nfs_free_seqid(calldata->arg.seqid);
826         nfs4_put_state_owner(sp);
827         kfree(calldata);
828 }
829
830 static void nfs4_close_done(struct rpc_task *task)
831 {
832         struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
833         struct nfs4_state *state = calldata->state;
834         struct nfs_server *server = NFS_SERVER(calldata->inode);
835
836         /* hmm. we are done with the inode, and in the process of freeing
837          * the state_owner. we keep this around to process errors
838          */
839         nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
840         switch (task->tk_status) {
841                 case 0:
842                         memcpy(&state->stateid, &calldata->res.stateid,
843                                         sizeof(state->stateid));
844                         break;
845                 case -NFS4ERR_STALE_STATEID:
846                 case -NFS4ERR_EXPIRED:
847                         state->state = calldata->arg.open_flags;
848                         nfs4_schedule_state_recovery(server->nfs4_state);
849                         break;
850                 default:
851                         if (nfs4_async_handle_error(task, server) == -EAGAIN) {
852                                 rpc_restart_call(task);
853                                 return;
854                         }
855         }
856         state->state = calldata->arg.open_flags;
857         nfs4_free_closedata(calldata);
858 }
859
860 static void nfs4_close_begin(struct rpc_task *task)
861 {
862         struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
863         struct nfs4_state *state = calldata->state;
864         struct rpc_message msg = {
865                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
866                 .rpc_argp = &calldata->arg,
867                 .rpc_resp = &calldata->res,
868                 .rpc_cred = state->owner->so_cred,
869         };
870         int mode = 0;
871         int status;
872
873         status = nfs_wait_on_sequence(calldata->arg.seqid, task);
874         if (status != 0)
875                 return;
876         /* Don't reorder reads */
877         smp_rmb();
878         /* Recalculate the new open mode in case someone reopened the file
879          * while we were waiting in line to be scheduled.
880          */
881         if (state->nreaders != 0)
882                 mode |= FMODE_READ;
883         if (state->nwriters != 0)
884                 mode |= FMODE_WRITE;
885         if (test_bit(NFS_DELEGATED_STATE, &state->flags))
886                 state->state = mode;
887         if (mode == state->state) {
888                 nfs4_free_closedata(calldata);
889                 task->tk_exit = NULL;
890                 rpc_exit(task, 0);
891                 return;
892         }
893         if (mode != 0)
894                 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
895         calldata->arg.open_flags = mode;
896         rpc_call_setup(task, &msg, 0);
897 }
898
899 /* 
900  * It is possible for data to be read/written from a mem-mapped file 
901  * after the sys_close call (which hits the vfs layer as a flush).
902  * This means that we can't safely call nfsv4 close on a file until 
903  * the inode is cleared. This in turn means that we are not good
904  * NFSv4 citizens - we do not indicate to the server to update the file's 
905  * share state even when we are done with one of the three share 
906  * stateid's in the inode.
907  *
908  * NOTE: Caller must be holding the sp->so_owner semaphore!
909  */
910 int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode) 
911 {
912         struct nfs4_closedata *calldata;
913         int status = -ENOMEM;
914
915         calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
916         if (calldata == NULL)
917                 goto out;
918         calldata->inode = inode;
919         calldata->state = state;
920         calldata->arg.fh = NFS_FH(inode);
921         calldata->arg.stateid = &state->stateid;
922         /* Serialization for the sequence id */
923         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
924         if (calldata->arg.seqid == NULL)
925                 goto out_free_calldata;
926
927         status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_close_begin,
928                         nfs4_close_done, calldata);
929         if (status == 0)
930                 goto out;
931
932         nfs_free_seqid(calldata->arg.seqid);
933 out_free_calldata:
934         kfree(calldata);
935 out:
936         return status;
937 }
938
939 struct inode *
940 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
941 {
942         struct iattr attr;
943         struct rpc_cred *cred;
944         struct nfs4_state *state;
945
946         if (nd->flags & LOOKUP_CREATE) {
947                 attr.ia_mode = nd->intent.open.create_mode;
948                 attr.ia_valid = ATTR_MODE;
949                 if (!IS_POSIXACL(dir))
950                         attr.ia_mode &= ~current->fs->umask;
951         } else {
952                 attr.ia_valid = 0;
953                 BUG_ON(nd->intent.open.flags & O_CREAT);
954         }
955
956         cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
957         if (IS_ERR(cred))
958                 return (struct inode *)cred;
959         state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
960         put_rpccred(cred);
961         if (IS_ERR(state))
962                 return (struct inode *)state;
963         return state->inode;
964 }
965
966 int
967 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
968 {
969         struct rpc_cred *cred;
970         struct nfs4_state *state;
971         struct inode *inode;
972
973         cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
974         if (IS_ERR(cred))
975                 return PTR_ERR(cred);
976         state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
977         if (IS_ERR(state))
978                 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
979         put_rpccred(cred);
980         if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
981                 return 1;
982         if (IS_ERR(state))
983                 return 0;
984         inode = state->inode;
985         if (inode == dentry->d_inode) {
986                 iput(inode);
987                 return 1;
988         }
989         d_drop(dentry);
990         nfs4_close_state(state, openflags);
991         iput(inode);
992         return 0;
993 }
994
995
996 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
997 {
998         struct nfs4_server_caps_res res = {};
999         struct rpc_message msg = {
1000                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1001                 .rpc_argp = fhandle,
1002                 .rpc_resp = &res,
1003         };
1004         int status;
1005
1006         status = rpc_call_sync(server->client, &msg, 0);
1007         if (status == 0) {
1008                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1009                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1010                         server->caps |= NFS_CAP_ACLS;
1011                 if (res.has_links != 0)
1012                         server->caps |= NFS_CAP_HARDLINKS;
1013                 if (res.has_symlinks != 0)
1014                         server->caps |= NFS_CAP_SYMLINKS;
1015                 server->acl_bitmask = res.acl_bitmask;
1016         }
1017         return status;
1018 }
1019
1020 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1021 {
1022         struct nfs4_exception exception = { };
1023         int err;
1024         do {
1025                 err = nfs4_handle_exception(server,
1026                                 _nfs4_server_capabilities(server, fhandle),
1027                                 &exception);
1028         } while (exception.retry);
1029         return err;
1030 }
1031
1032 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1033                 struct nfs_fsinfo *info)
1034 {
1035         struct nfs_fattr *      fattr = info->fattr;
1036         struct nfs4_lookup_root_arg args = {
1037                 .bitmask = nfs4_fattr_bitmap,
1038         };
1039         struct nfs4_lookup_res res = {
1040                 .server = server,
1041                 .fattr = fattr,
1042                 .fh = fhandle,
1043         };
1044         struct rpc_message msg = {
1045                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1046                 .rpc_argp = &args,
1047                 .rpc_resp = &res,
1048         };
1049         fattr->valid = 0;
1050         return rpc_call_sync(server->client, &msg, 0);
1051 }
1052
1053 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1054                 struct nfs_fsinfo *info)
1055 {
1056         struct nfs4_exception exception = { };
1057         int err;
1058         do {
1059                 err = nfs4_handle_exception(server,
1060                                 _nfs4_lookup_root(server, fhandle, info),
1061                                 &exception);
1062         } while (exception.retry);
1063         return err;
1064 }
1065
1066 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1067                 struct nfs_fsinfo *info)
1068 {
1069         struct nfs_fattr *      fattr = info->fattr;
1070         unsigned char *         p;
1071         struct qstr             q;
1072         struct nfs4_lookup_arg args = {
1073                 .dir_fh = fhandle,
1074                 .name = &q,
1075                 .bitmask = nfs4_fattr_bitmap,
1076         };
1077         struct nfs4_lookup_res res = {
1078                 .server = server,
1079                 .fattr = fattr,
1080                 .fh = fhandle,
1081         };
1082         struct rpc_message msg = {
1083                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1084                 .rpc_argp = &args,
1085                 .rpc_resp = &res,
1086         };
1087         int status;
1088
1089         /*
1090          * Now we do a separate LOOKUP for each component of the mount path.
1091          * The LOOKUPs are done separately so that we can conveniently
1092          * catch an ERR_WRONGSEC if it occurs along the way...
1093          */
1094         status = nfs4_lookup_root(server, fhandle, info);
1095         if (status)
1096                 goto out;
1097
1098         p = server->mnt_path;
1099         for (;;) {
1100                 struct nfs4_exception exception = { };
1101
1102                 while (*p == '/')
1103                         p++;
1104                 if (!*p)
1105                         break;
1106                 q.name = p;
1107                 while (*p && (*p != '/'))
1108                         p++;
1109                 q.len = p - q.name;
1110
1111                 do {
1112                         fattr->valid = 0;
1113                         status = nfs4_handle_exception(server,
1114                                         rpc_call_sync(server->client, &msg, 0),
1115                                         &exception);
1116                 } while (exception.retry);
1117                 if (status == 0)
1118                         continue;
1119                 if (status == -ENOENT) {
1120                         printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1121                         printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1122                 }
1123                 break;
1124         }
1125         if (status == 0)
1126                 status = nfs4_server_capabilities(server, fhandle);
1127         if (status == 0)
1128                 status = nfs4_do_fsinfo(server, fhandle, info);
1129 out:
1130         return status;
1131 }
1132
1133 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1134 {
1135         struct nfs4_getattr_arg args = {
1136                 .fh = fhandle,
1137                 .bitmask = server->attr_bitmask,
1138         };
1139         struct nfs4_getattr_res res = {
1140                 .fattr = fattr,
1141                 .server = server,
1142         };
1143         struct rpc_message msg = {
1144                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1145                 .rpc_argp = &args,
1146                 .rpc_resp = &res,
1147         };
1148         
1149         fattr->valid = 0;
1150         return rpc_call_sync(server->client, &msg, 0);
1151 }
1152
1153 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1154 {
1155         struct nfs4_exception exception = { };
1156         int err;
1157         do {
1158                 err = nfs4_handle_exception(server,
1159                                 _nfs4_proc_getattr(server, fhandle, fattr),
1160                                 &exception);
1161         } while (exception.retry);
1162         return err;
1163 }
1164
1165 /* 
1166  * The file is not closed if it is opened due to the a request to change
1167  * the size of the file. The open call will not be needed once the
1168  * VFS layer lookup-intents are implemented.
1169  *
1170  * Close is called when the inode is destroyed.
1171  * If we haven't opened the file for O_WRONLY, we
1172  * need to in the size_change case to obtain a stateid.
1173  *
1174  * Got race?
1175  * Because OPEN is always done by name in nfsv4, it is
1176  * possible that we opened a different file by the same
1177  * name.  We can recognize this race condition, but we
1178  * can't do anything about it besides returning an error.
1179  *
1180  * This will be fixed with VFS changes (lookup-intent).
1181  */
1182 static int
1183 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1184                   struct iattr *sattr)
1185 {
1186         struct rpc_cred *cred;
1187         struct inode *inode = dentry->d_inode;
1188         struct nfs4_state *state;
1189         int status;
1190
1191         fattr->valid = 0;
1192         
1193         cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1194         if (IS_ERR(cred))
1195                 return PTR_ERR(cred);
1196         /* Search for an existing WRITE delegation first */
1197         state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
1198         if (!IS_ERR(state)) {
1199                 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1200                 iput(inode);
1201         } else {
1202                 /* Search for an existing open(O_WRITE) stateid */
1203                 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1204         }
1205
1206         status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1207                         NFS_FH(inode), sattr, state);
1208         if (status == 0)
1209                 nfs_setattr_update_inode(inode, sattr);
1210         if (state != NULL)
1211                 nfs4_close_state(state, FMODE_WRITE);
1212         put_rpccred(cred);
1213         return status;
1214 }
1215
1216 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1217                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1218 {
1219         int                    status;
1220         struct nfs_server *server = NFS_SERVER(dir);
1221         struct nfs4_lookup_arg args = {
1222                 .bitmask = server->attr_bitmask,
1223                 .dir_fh = NFS_FH(dir),
1224                 .name = name,
1225         };
1226         struct nfs4_lookup_res res = {
1227                 .server = server,
1228                 .fattr = fattr,
1229                 .fh = fhandle,
1230         };
1231         struct rpc_message msg = {
1232                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1233                 .rpc_argp = &args,
1234                 .rpc_resp = &res,
1235         };
1236         
1237         fattr->valid = 0;
1238         
1239         dprintk("NFS call  lookup %s\n", name->name);
1240         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1241         dprintk("NFS reply lookup: %d\n", status);
1242         return status;
1243 }
1244
1245 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1246 {
1247         struct nfs4_exception exception = { };
1248         int err;
1249         do {
1250                 err = nfs4_handle_exception(NFS_SERVER(dir),
1251                                 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1252                                 &exception);
1253         } while (exception.retry);
1254         return err;
1255 }
1256
1257 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1258 {
1259         struct nfs4_accessargs args = {
1260                 .fh = NFS_FH(inode),
1261         };
1262         struct nfs4_accessres res = { 0 };
1263         struct rpc_message msg = {
1264                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1265                 .rpc_argp = &args,
1266                 .rpc_resp = &res,
1267                 .rpc_cred = entry->cred,
1268         };
1269         int mode = entry->mask;
1270         int status;
1271
1272         /*
1273          * Determine which access bits we want to ask for...
1274          */
1275         if (mode & MAY_READ)
1276                 args.access |= NFS4_ACCESS_READ;
1277         if (S_ISDIR(inode->i_mode)) {
1278                 if (mode & MAY_WRITE)
1279                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1280                 if (mode & MAY_EXEC)
1281                         args.access |= NFS4_ACCESS_LOOKUP;
1282         } else {
1283                 if (mode & MAY_WRITE)
1284                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1285                 if (mode & MAY_EXEC)
1286                         args.access |= NFS4_ACCESS_EXECUTE;
1287         }
1288         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1289         if (!status) {
1290                 entry->mask = 0;
1291                 if (res.access & NFS4_ACCESS_READ)
1292                         entry->mask |= MAY_READ;
1293                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1294                         entry->mask |= MAY_WRITE;
1295                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1296                         entry->mask |= MAY_EXEC;
1297         }
1298         return status;
1299 }
1300
1301 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1302 {
1303         struct nfs4_exception exception = { };
1304         int err;
1305         do {
1306                 err = nfs4_handle_exception(NFS_SERVER(inode),
1307                                 _nfs4_proc_access(inode, entry),
1308                                 &exception);
1309         } while (exception.retry);
1310         return err;
1311 }
1312
1313 /*
1314  * TODO: For the time being, we don't try to get any attributes
1315  * along with any of the zero-copy operations READ, READDIR,
1316  * READLINK, WRITE.
1317  *
1318  * In the case of the first three, we want to put the GETATTR
1319  * after the read-type operation -- this is because it is hard
1320  * to predict the length of a GETATTR response in v4, and thus
1321  * align the READ data correctly.  This means that the GETATTR
1322  * may end up partially falling into the page cache, and we should
1323  * shift it into the 'tail' of the xdr_buf before processing.
1324  * To do this efficiently, we need to know the total length
1325  * of data received, which doesn't seem to be available outside
1326  * of the RPC layer.
1327  *
1328  * In the case of WRITE, we also want to put the GETATTR after
1329  * the operation -- in this case because we want to make sure
1330  * we get the post-operation mtime and size.  This means that
1331  * we can't use xdr_encode_pages() as written: we need a variant
1332  * of it which would leave room in the 'tail' iovec.
1333  *
1334  * Both of these changes to the XDR layer would in fact be quite
1335  * minor, but I decided to leave them for a subsequent patch.
1336  */
1337 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1338                 unsigned int pgbase, unsigned int pglen)
1339 {
1340         struct nfs4_readlink args = {
1341                 .fh       = NFS_FH(inode),
1342                 .pgbase   = pgbase,
1343                 .pglen    = pglen,
1344                 .pages    = &page,
1345         };
1346         struct rpc_message msg = {
1347                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1348                 .rpc_argp = &args,
1349                 .rpc_resp = NULL,
1350         };
1351
1352         return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1353 }
1354
1355 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1356                 unsigned int pgbase, unsigned int pglen)
1357 {
1358         struct nfs4_exception exception = { };
1359         int err;
1360         do {
1361                 err = nfs4_handle_exception(NFS_SERVER(inode),
1362                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1363                                 &exception);
1364         } while (exception.retry);
1365         return err;
1366 }
1367
1368 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1369 {
1370         int flags = rdata->flags;
1371         struct inode *inode = rdata->inode;
1372         struct nfs_fattr *fattr = rdata->res.fattr;
1373         struct nfs_server *server = NFS_SERVER(inode);
1374         struct rpc_message msg = {
1375                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_READ],
1376                 .rpc_argp       = &rdata->args,
1377                 .rpc_resp       = &rdata->res,
1378                 .rpc_cred       = rdata->cred,
1379         };
1380         unsigned long timestamp = jiffies;
1381         int status;
1382
1383         dprintk("NFS call  read %d @ %Ld\n", rdata->args.count,
1384                         (long long) rdata->args.offset);
1385
1386         fattr->valid = 0;
1387         status = rpc_call_sync(server->client, &msg, flags);
1388         if (!status)
1389                 renew_lease(server, timestamp);
1390         dprintk("NFS reply read: %d\n", status);
1391         return status;
1392 }
1393
1394 static int nfs4_proc_read(struct nfs_read_data *rdata)
1395 {
1396         struct nfs4_exception exception = { };
1397         int err;
1398         do {
1399                 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1400                                 _nfs4_proc_read(rdata),
1401                                 &exception);
1402         } while (exception.retry);
1403         return err;
1404 }
1405
1406 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1407 {
1408         int rpcflags = wdata->flags;
1409         struct inode *inode = wdata->inode;
1410         struct nfs_fattr *fattr = wdata->res.fattr;
1411         struct nfs_server *server = NFS_SERVER(inode);
1412         struct rpc_message msg = {
1413                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1414                 .rpc_argp       = &wdata->args,
1415                 .rpc_resp       = &wdata->res,
1416                 .rpc_cred       = wdata->cred,
1417         };
1418         int status;
1419
1420         dprintk("NFS call  write %d @ %Ld\n", wdata->args.count,
1421                         (long long) wdata->args.offset);
1422
1423         fattr->valid = 0;
1424         status = rpc_call_sync(server->client, &msg, rpcflags);
1425         dprintk("NFS reply write: %d\n", status);
1426         return status;
1427 }
1428
1429 static int nfs4_proc_write(struct nfs_write_data *wdata)
1430 {
1431         struct nfs4_exception exception = { };
1432         int err;
1433         do {
1434                 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1435                                 _nfs4_proc_write(wdata),
1436                                 &exception);
1437         } while (exception.retry);
1438         return err;
1439 }
1440
1441 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1442 {
1443         struct inode *inode = cdata->inode;
1444         struct nfs_fattr *fattr = cdata->res.fattr;
1445         struct nfs_server *server = NFS_SERVER(inode);
1446         struct rpc_message msg = {
1447                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1448                 .rpc_argp       = &cdata->args,
1449                 .rpc_resp       = &cdata->res,
1450                 .rpc_cred       = cdata->cred,
1451         };
1452         int status;
1453
1454         dprintk("NFS call  commit %d @ %Ld\n", cdata->args.count,
1455                         (long long) cdata->args.offset);
1456
1457         fattr->valid = 0;
1458         status = rpc_call_sync(server->client, &msg, 0);
1459         dprintk("NFS reply commit: %d\n", status);
1460         return status;
1461 }
1462
1463 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1464 {
1465         struct nfs4_exception exception = { };
1466         int err;
1467         do {
1468                 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1469                                 _nfs4_proc_commit(cdata),
1470                                 &exception);
1471         } while (exception.retry);
1472         return err;
1473 }
1474
1475 /*
1476  * Got race?
1477  * We will need to arrange for the VFS layer to provide an atomic open.
1478  * Until then, this create/open method is prone to inefficiency and race
1479  * conditions due to the lookup, create, and open VFS calls from sys_open()
1480  * placed on the wire.
1481  *
1482  * Given the above sorry state of affairs, I'm simply sending an OPEN.
1483  * The file will be opened again in the subsequent VFS open call
1484  * (nfs4_proc_file_open).
1485  *
1486  * The open for read will just hang around to be used by any process that
1487  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1488  */
1489
1490 static int
1491 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1492                  int flags)
1493 {
1494         struct nfs4_state *state;
1495         struct rpc_cred *cred;
1496         int status = 0;
1497
1498         cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1499         if (IS_ERR(cred)) {
1500                 status = PTR_ERR(cred);
1501                 goto out;
1502         }
1503         state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1504         put_rpccred(cred);
1505         if (IS_ERR(state)) {
1506                 status = PTR_ERR(state);
1507                 goto out;
1508         }
1509         d_instantiate(dentry, state->inode);
1510         if (flags & O_EXCL) {
1511                 struct nfs_fattr fattr;
1512                 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1513                                      NFS_FH(state->inode), sattr, state);
1514                 if (status == 0) {
1515                         nfs_setattr_update_inode(state->inode, sattr);
1516                         goto out;
1517                 }
1518         } else if (flags != 0)
1519                 goto out;
1520         nfs4_close_state(state, flags);
1521 out:
1522         return status;
1523 }
1524
1525 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1526 {
1527         struct nfs4_remove_arg args = {
1528                 .fh = NFS_FH(dir),
1529                 .name = name,
1530         };
1531         struct nfs4_change_info res;
1532         struct rpc_message msg = {
1533                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1534                 .rpc_argp       = &args,
1535                 .rpc_resp       = &res,
1536         };
1537         int                     status;
1538
1539         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1540         if (status == 0)
1541                 update_changeattr(dir, &res);
1542         return status;
1543 }
1544
1545 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1546 {
1547         struct nfs4_exception exception = { };
1548         int err;
1549         do {
1550                 err = nfs4_handle_exception(NFS_SERVER(dir),
1551                                 _nfs4_proc_remove(dir, name),
1552                                 &exception);
1553         } while (exception.retry);
1554         return err;
1555 }
1556
1557 struct unlink_desc {
1558         struct nfs4_remove_arg  args;
1559         struct nfs4_change_info res;
1560 };
1561
1562 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1563                 struct qstr *name)
1564 {
1565         struct unlink_desc *up;
1566
1567         up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1568         if (!up)
1569                 return -ENOMEM;
1570         
1571         up->args.fh = NFS_FH(dir->d_inode);
1572         up->args.name = name;
1573         
1574         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1575         msg->rpc_argp = &up->args;
1576         msg->rpc_resp = &up->res;
1577         return 0;
1578 }
1579
1580 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1581 {
1582         struct rpc_message *msg = &task->tk_msg;
1583         struct unlink_desc *up;
1584         
1585         if (msg->rpc_resp != NULL) {
1586                 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1587                 update_changeattr(dir->d_inode, &up->res);
1588                 kfree(up);
1589                 msg->rpc_resp = NULL;
1590                 msg->rpc_argp = NULL;
1591         }
1592         return 0;
1593 }
1594
1595 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1596                 struct inode *new_dir, struct qstr *new_name)
1597 {
1598         struct nfs4_rename_arg arg = {
1599                 .old_dir = NFS_FH(old_dir),
1600                 .new_dir = NFS_FH(new_dir),
1601                 .old_name = old_name,
1602                 .new_name = new_name,
1603         };
1604         struct nfs4_rename_res res = { };
1605         struct rpc_message msg = {
1606                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1607                 .rpc_argp = &arg,
1608                 .rpc_resp = &res,
1609         };
1610         int                     status;
1611         
1612         status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1613
1614         if (!status) {
1615                 update_changeattr(old_dir, &res.old_cinfo);
1616                 update_changeattr(new_dir, &res.new_cinfo);
1617         }
1618         return status;
1619 }
1620
1621 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1622                 struct inode *new_dir, struct qstr *new_name)
1623 {
1624         struct nfs4_exception exception = { };
1625         int err;
1626         do {
1627                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1628                                 _nfs4_proc_rename(old_dir, old_name,
1629                                         new_dir, new_name),
1630                                 &exception);
1631         } while (exception.retry);
1632         return err;
1633 }
1634
1635 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1636 {
1637         struct nfs4_link_arg arg = {
1638                 .fh     = NFS_FH(inode),
1639                 .dir_fh = NFS_FH(dir),
1640                 .name   = name,
1641         };
1642         struct nfs4_change_info cinfo = { };
1643         struct rpc_message msg = {
1644                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1645                 .rpc_argp = &arg,
1646                 .rpc_resp = &cinfo,
1647         };
1648         int                     status;
1649
1650         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1651         if (!status)
1652                 update_changeattr(dir, &cinfo);
1653
1654         return status;
1655 }
1656
1657 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1658 {
1659         struct nfs4_exception exception = { };
1660         int err;
1661         do {
1662                 err = nfs4_handle_exception(NFS_SERVER(inode),
1663                                 _nfs4_proc_link(inode, dir, name),
1664                                 &exception);
1665         } while (exception.retry);
1666         return err;
1667 }
1668
1669 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1670                 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1671                 struct nfs_fattr *fattr)
1672 {
1673         struct nfs_server *server = NFS_SERVER(dir);
1674         struct nfs4_create_arg arg = {
1675                 .dir_fh = NFS_FH(dir),
1676                 .server = server,
1677                 .name = name,
1678                 .attrs = sattr,
1679                 .ftype = NF4LNK,
1680                 .bitmask = server->attr_bitmask,
1681         };
1682         struct nfs4_create_res res = {
1683                 .server = server,
1684                 .fh = fhandle,
1685                 .fattr = fattr,
1686         };
1687         struct rpc_message msg = {
1688                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1689                 .rpc_argp = &arg,
1690                 .rpc_resp = &res,
1691         };
1692         int                     status;
1693
1694         if (path->len > NFS4_MAXPATHLEN)
1695                 return -ENAMETOOLONG;
1696         arg.u.symlink = path;
1697         fattr->valid = 0;
1698         
1699         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1700         if (!status)
1701                 update_changeattr(dir, &res.dir_cinfo);
1702         return status;
1703 }
1704
1705 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1706                 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1707                 struct nfs_fattr *fattr)
1708 {
1709         struct nfs4_exception exception = { };
1710         int err;
1711         do {
1712                 err = nfs4_handle_exception(NFS_SERVER(dir),
1713                                 _nfs4_proc_symlink(dir, name, path, sattr,
1714                                         fhandle, fattr),
1715                                 &exception);
1716         } while (exception.retry);
1717         return err;
1718 }
1719
1720 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1721                 struct iattr *sattr)
1722 {
1723         struct nfs_server *server = NFS_SERVER(dir);
1724         struct nfs_fh fhandle;
1725         struct nfs_fattr fattr;
1726         struct nfs4_create_arg arg = {
1727                 .dir_fh = NFS_FH(dir),
1728                 .server = server,
1729                 .name = &dentry->d_name,
1730                 .attrs = sattr,
1731                 .ftype = NF4DIR,
1732                 .bitmask = server->attr_bitmask,
1733         };
1734         struct nfs4_create_res res = {
1735                 .server = server,
1736                 .fh = &fhandle,
1737                 .fattr = &fattr,
1738         };
1739         struct rpc_message msg = {
1740                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1741                 .rpc_argp = &arg,
1742                 .rpc_resp = &res,
1743         };
1744         int                     status;
1745
1746         fattr.valid = 0;
1747         
1748         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1749         if (!status) {
1750                 update_changeattr(dir, &res.dir_cinfo);
1751                 status = nfs_instantiate(dentry, &fhandle, &fattr);
1752         }
1753         return status;
1754 }
1755
1756 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1757                 struct iattr *sattr)
1758 {
1759         struct nfs4_exception exception = { };
1760         int err;
1761         do {
1762                 err = nfs4_handle_exception(NFS_SERVER(dir),
1763                                 _nfs4_proc_mkdir(dir, dentry, sattr),
1764                                 &exception);
1765         } while (exception.retry);
1766         return err;
1767 }
1768
1769 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1770                   u64 cookie, struct page *page, unsigned int count, int plus)
1771 {
1772         struct inode            *dir = dentry->d_inode;
1773         struct nfs4_readdir_arg args = {
1774                 .fh = NFS_FH(dir),
1775                 .pages = &page,
1776                 .pgbase = 0,
1777                 .count = count,
1778                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1779         };
1780         struct nfs4_readdir_res res;
1781         struct rpc_message msg = {
1782                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1783                 .rpc_argp = &args,
1784                 .rpc_resp = &res,
1785                 .rpc_cred = cred,
1786         };
1787         int                     status;
1788
1789         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1790                         dentry->d_parent->d_name.name,
1791                         dentry->d_name.name,
1792                         (unsigned long long)cookie);
1793         lock_kernel();
1794         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1795         res.pgbase = args.pgbase;
1796         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1797         if (status == 0)
1798                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1799         unlock_kernel();
1800         dprintk("%s: returns %d\n", __FUNCTION__, status);
1801         return status;
1802 }
1803
1804 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1805                   u64 cookie, struct page *page, unsigned int count, int plus)
1806 {
1807         struct nfs4_exception exception = { };
1808         int err;
1809         do {
1810                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1811                                 _nfs4_proc_readdir(dentry, cred, cookie,
1812                                         page, count, plus),
1813                                 &exception);
1814         } while (exception.retry);
1815         return err;
1816 }
1817
1818 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1819                 struct iattr *sattr, dev_t rdev)
1820 {
1821         struct nfs_server *server = NFS_SERVER(dir);
1822         struct nfs_fh fh;
1823         struct nfs_fattr fattr;
1824         struct nfs4_create_arg arg = {
1825                 .dir_fh = NFS_FH(dir),
1826                 .server = server,
1827                 .name = &dentry->d_name,
1828                 .attrs = sattr,
1829                 .bitmask = server->attr_bitmask,
1830         };
1831         struct nfs4_create_res res = {
1832                 .server = server,
1833                 .fh = &fh,
1834                 .fattr = &fattr,
1835         };
1836         struct rpc_message msg = {
1837                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1838                 .rpc_argp = &arg,
1839                 .rpc_resp = &res,
1840         };
1841         int                     status;
1842         int                     mode = sattr->ia_mode;
1843
1844         fattr.valid = 0;
1845
1846         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1847         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1848         if (S_ISFIFO(mode))
1849                 arg.ftype = NF4FIFO;
1850         else if (S_ISBLK(mode)) {
1851                 arg.ftype = NF4BLK;
1852                 arg.u.device.specdata1 = MAJOR(rdev);
1853                 arg.u.device.specdata2 = MINOR(rdev);
1854         }
1855         else if (S_ISCHR(mode)) {
1856                 arg.ftype = NF4CHR;
1857                 arg.u.device.specdata1 = MAJOR(rdev);
1858                 arg.u.device.specdata2 = MINOR(rdev);
1859         }
1860         else
1861                 arg.ftype = NF4SOCK;
1862         
1863         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1864         if (status == 0) {
1865                 update_changeattr(dir, &res.dir_cinfo);
1866                 status = nfs_instantiate(dentry, &fh, &fattr);
1867         }
1868         return status;
1869 }
1870
1871 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1872                 struct iattr *sattr, dev_t rdev)
1873 {
1874         struct nfs4_exception exception = { };
1875         int err;
1876         do {
1877                 err = nfs4_handle_exception(NFS_SERVER(dir),
1878                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1879                                 &exception);
1880         } while (exception.retry);
1881         return err;
1882 }
1883
1884 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1885                  struct nfs_fsstat *fsstat)
1886 {
1887         struct nfs4_statfs_arg args = {
1888                 .fh = fhandle,
1889                 .bitmask = server->attr_bitmask,
1890         };
1891         struct rpc_message msg = {
1892                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1893                 .rpc_argp = &args,
1894                 .rpc_resp = fsstat,
1895         };
1896
1897         fsstat->fattr->valid = 0;
1898         return rpc_call_sync(server->client, &msg, 0);
1899 }
1900
1901 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1902 {
1903         struct nfs4_exception exception = { };
1904         int err;
1905         do {
1906                 err = nfs4_handle_exception(server,
1907                                 _nfs4_proc_statfs(server, fhandle, fsstat),
1908                                 &exception);
1909         } while (exception.retry);
1910         return err;
1911 }
1912
1913 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1914                 struct nfs_fsinfo *fsinfo)
1915 {
1916         struct nfs4_fsinfo_arg args = {
1917                 .fh = fhandle,
1918                 .bitmask = server->attr_bitmask,
1919         };
1920         struct rpc_message msg = {
1921                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1922                 .rpc_argp = &args,
1923                 .rpc_resp = fsinfo,
1924         };
1925
1926         return rpc_call_sync(server->client, &msg, 0);
1927 }
1928
1929 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1930 {
1931         struct nfs4_exception exception = { };
1932         int err;
1933
1934         do {
1935                 err = nfs4_handle_exception(server,
1936                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
1937                                 &exception);
1938         } while (exception.retry);
1939         return err;
1940 }
1941
1942 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1943 {
1944         fsinfo->fattr->valid = 0;
1945         return nfs4_do_fsinfo(server, fhandle, fsinfo);
1946 }
1947
1948 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1949                 struct nfs_pathconf *pathconf)
1950 {
1951         struct nfs4_pathconf_arg args = {
1952                 .fh = fhandle,
1953                 .bitmask = server->attr_bitmask,
1954         };
1955         struct rpc_message msg = {
1956                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1957                 .rpc_argp = &args,
1958                 .rpc_resp = pathconf,
1959         };
1960
1961         /* None of the pathconf attributes are mandatory to implement */
1962         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
1963                 memset(pathconf, 0, sizeof(*pathconf));
1964                 return 0;
1965         }
1966
1967         pathconf->fattr->valid = 0;
1968         return rpc_call_sync(server->client, &msg, 0);
1969 }
1970
1971 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1972                 struct nfs_pathconf *pathconf)
1973 {
1974         struct nfs4_exception exception = { };
1975         int err;
1976
1977         do {
1978                 err = nfs4_handle_exception(server,
1979                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
1980                                 &exception);
1981         } while (exception.retry);
1982         return err;
1983 }
1984
1985 static void
1986 nfs4_read_done(struct rpc_task *task)
1987 {
1988         struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
1989         struct inode *inode = data->inode;
1990
1991         if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1992                 rpc_restart_call(task);
1993                 return;
1994         }
1995         if (task->tk_status > 0)
1996                 renew_lease(NFS_SERVER(inode), data->timestamp);
1997         /* Call back common NFS readpage processing */
1998         nfs_readpage_result(task);
1999 }
2000
2001 static void
2002 nfs4_proc_read_setup(struct nfs_read_data *data)
2003 {
2004         struct rpc_task *task = &data->task;
2005         struct rpc_message msg = {
2006                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2007                 .rpc_argp = &data->args,
2008                 .rpc_resp = &data->res,
2009                 .rpc_cred = data->cred,
2010         };
2011         struct inode *inode = data->inode;
2012         int flags;
2013
2014         data->timestamp   = jiffies;
2015
2016         /* N.B. Do we need to test? Never called for swapfile inode */
2017         flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2018
2019         /* Finalize the task. */
2020         rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
2021         rpc_call_setup(task, &msg, 0);
2022 }
2023
2024 static void
2025 nfs4_write_done(struct rpc_task *task)
2026 {
2027         struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2028         struct inode *inode = data->inode;
2029         
2030         if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2031                 rpc_restart_call(task);
2032                 return;
2033         }
2034         if (task->tk_status >= 0)
2035                 renew_lease(NFS_SERVER(inode), data->timestamp);
2036         /* Call back common NFS writeback processing */
2037         nfs_writeback_done(task);
2038 }
2039
2040 static void
2041 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2042 {
2043         struct rpc_task *task = &data->task;
2044         struct rpc_message msg = {
2045                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2046                 .rpc_argp = &data->args,
2047                 .rpc_resp = &data->res,
2048                 .rpc_cred = data->cred,
2049         };
2050         struct inode *inode = data->inode;
2051         int stable;
2052         int flags;
2053         
2054         if (how & FLUSH_STABLE) {
2055                 if (!NFS_I(inode)->ncommit)
2056                         stable = NFS_FILE_SYNC;
2057                 else
2058                         stable = NFS_DATA_SYNC;
2059         } else
2060                 stable = NFS_UNSTABLE;
2061         data->args.stable = stable;
2062
2063         data->timestamp   = jiffies;
2064
2065         /* Set the initial flags for the task.  */
2066         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2067
2068         /* Finalize the task. */
2069         rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2070         rpc_call_setup(task, &msg, 0);
2071 }
2072
2073 static void
2074 nfs4_commit_done(struct rpc_task *task)
2075 {
2076         struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2077         struct inode *inode = data->inode;
2078         
2079         if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2080                 rpc_restart_call(task);
2081                 return;
2082         }
2083         /* Call back common NFS writeback processing */
2084         nfs_commit_done(task);
2085 }
2086
2087 static void
2088 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2089 {
2090         struct rpc_task *task = &data->task;
2091         struct rpc_message msg = {
2092                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2093                 .rpc_argp = &data->args,
2094                 .rpc_resp = &data->res,
2095                 .rpc_cred = data->cred,
2096         };      
2097         struct inode *inode = data->inode;
2098         int flags;
2099         
2100         /* Set the initial flags for the task.  */
2101         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2102
2103         /* Finalize the task. */
2104         rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2105         rpc_call_setup(task, &msg, 0);  
2106 }
2107
2108 /*
2109  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2110  * standalone procedure for queueing an asynchronous RENEW.
2111  */
2112 static void
2113 renew_done(struct rpc_task *task)
2114 {
2115         struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2116         unsigned long timestamp = (unsigned long)task->tk_calldata;
2117
2118         if (task->tk_status < 0) {
2119                 switch (task->tk_status) {
2120                         case -NFS4ERR_STALE_CLIENTID:
2121                         case -NFS4ERR_EXPIRED:
2122                         case -NFS4ERR_CB_PATH_DOWN:
2123                                 nfs4_schedule_state_recovery(clp);
2124                 }
2125                 return;
2126         }
2127         spin_lock(&clp->cl_lock);
2128         if (time_before(clp->cl_last_renewal,timestamp))
2129                 clp->cl_last_renewal = timestamp;
2130         spin_unlock(&clp->cl_lock);
2131 }
2132
2133 int
2134 nfs4_proc_async_renew(struct nfs4_client *clp)
2135 {
2136         struct rpc_message msg = {
2137                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2138                 .rpc_argp       = clp,
2139                 .rpc_cred       = clp->cl_cred,
2140         };
2141
2142         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2143                         renew_done, (void *)jiffies);
2144 }
2145
2146 int
2147 nfs4_proc_renew(struct nfs4_client *clp)
2148 {
2149         struct rpc_message msg = {
2150                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2151                 .rpc_argp       = clp,
2152                 .rpc_cred       = clp->cl_cred,
2153         };
2154         unsigned long now = jiffies;
2155         int status;
2156
2157         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2158         if (status < 0)
2159                 return status;
2160         spin_lock(&clp->cl_lock);
2161         if (time_before(clp->cl_last_renewal,now))
2162                 clp->cl_last_renewal = now;
2163         spin_unlock(&clp->cl_lock);
2164         return 0;
2165 }
2166
2167 /*
2168  * We will need to arrange for the VFS layer to provide an atomic open.
2169  * Until then, this open method is prone to inefficiency and race conditions
2170  * due to the lookup, potential create, and open VFS calls from sys_open()
2171  * placed on the wire.
2172  */
2173 static int
2174 nfs4_proc_file_open(struct inode *inode, struct file *filp)
2175 {
2176         struct dentry *dentry = filp->f_dentry;
2177         struct nfs_open_context *ctx;
2178         struct nfs4_state *state = NULL;
2179         struct rpc_cred *cred;
2180         int status = -ENOMEM;
2181
2182         dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2183                                (int)dentry->d_parent->d_name.len,
2184                                dentry->d_parent->d_name.name,
2185                                (int)dentry->d_name.len, dentry->d_name.name);
2186
2187
2188         /* Find our open stateid */
2189         cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
2190         if (IS_ERR(cred))
2191                 return PTR_ERR(cred);
2192         ctx = alloc_nfs_open_context(dentry, cred);
2193         put_rpccred(cred);
2194         if (unlikely(ctx == NULL))
2195                 return -ENOMEM;
2196         status = -EIO; /* ERACE actually */
2197         state = nfs4_find_state(inode, cred, filp->f_mode);
2198         if (unlikely(state == NULL))
2199                 goto no_state;
2200         ctx->state = state;
2201         nfs4_close_state(state, filp->f_mode);
2202         ctx->mode = filp->f_mode;
2203         nfs_file_set_open_context(filp, ctx);
2204         put_nfs_open_context(ctx);
2205         if (filp->f_mode & FMODE_WRITE)
2206                 nfs_begin_data_update(inode);
2207         return 0;
2208 no_state:
2209         printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
2210         put_nfs_open_context(ctx);
2211         return status;
2212 }
2213
2214 /*
2215  * Release our state
2216  */
2217 static int
2218 nfs4_proc_file_release(struct inode *inode, struct file *filp)
2219 {
2220         if (filp->f_mode & FMODE_WRITE)
2221                 nfs_end_data_update(inode);
2222         nfs_file_clear_open_context(filp);
2223         return 0;
2224 }
2225
2226 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2227 {
2228         return (server->caps & NFS_CAP_ACLS)
2229                 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2230                 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2231 }
2232
2233 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2234  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2235  * the stack.
2236  */
2237 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2238
2239 static void buf_to_pages(const void *buf, size_t buflen,
2240                 struct page **pages, unsigned int *pgbase)
2241 {
2242         const void *p = buf;
2243
2244         *pgbase = offset_in_page(buf);
2245         p -= *pgbase;
2246         while (p < buf + buflen) {
2247                 *(pages++) = virt_to_page(p);
2248                 p += PAGE_CACHE_SIZE;
2249         }
2250 }
2251
2252 struct nfs4_cached_acl {
2253         int cached;
2254         size_t len;
2255         char data[0];
2256 };
2257
2258 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2259 {
2260         struct nfs_inode *nfsi = NFS_I(inode);
2261
2262         spin_lock(&inode->i_lock);
2263         kfree(nfsi->nfs4_acl);
2264         nfsi->nfs4_acl = acl;
2265         spin_unlock(&inode->i_lock);
2266 }
2267
2268 static void nfs4_zap_acl_attr(struct inode *inode)
2269 {
2270         nfs4_set_cached_acl(inode, NULL);
2271 }
2272
2273 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2274 {
2275         struct nfs_inode *nfsi = NFS_I(inode);
2276         struct nfs4_cached_acl *acl;
2277         int ret = -ENOENT;
2278
2279         spin_lock(&inode->i_lock);
2280         acl = nfsi->nfs4_acl;
2281         if (acl == NULL)
2282                 goto out;
2283         if (buf == NULL) /* user is just asking for length */
2284                 goto out_len;
2285         if (acl->cached == 0)
2286                 goto out;
2287         ret = -ERANGE; /* see getxattr(2) man page */
2288         if (acl->len > buflen)
2289                 goto out;
2290         memcpy(buf, acl->data, acl->len);
2291 out_len:
2292         ret = acl->len;
2293 out:
2294         spin_unlock(&inode->i_lock);
2295         return ret;
2296 }
2297
2298 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2299 {
2300         struct nfs4_cached_acl *acl;
2301
2302         if (buf && acl_len <= PAGE_SIZE) {
2303                 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2304                 if (acl == NULL)
2305                         goto out;
2306                 acl->cached = 1;
2307                 memcpy(acl->data, buf, acl_len);
2308         } else {
2309                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2310                 if (acl == NULL)
2311                         goto out;
2312                 acl->cached = 0;
2313         }
2314         acl->len = acl_len;
2315 out:
2316         nfs4_set_cached_acl(inode, acl);
2317 }
2318
2319 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2320 {
2321         struct page *pages[NFS4ACL_MAXPAGES];
2322         struct nfs_getaclargs args = {
2323                 .fh = NFS_FH(inode),
2324                 .acl_pages = pages,
2325                 .acl_len = buflen,
2326         };
2327         size_t resp_len = buflen;
2328         void *resp_buf;
2329         struct rpc_message msg = {
2330                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2331                 .rpc_argp = &args,
2332                 .rpc_resp = &resp_len,
2333         };
2334         struct page *localpage = NULL;
2335         int ret;
2336
2337         if (buflen < PAGE_SIZE) {
2338                 /* As long as we're doing a round trip to the server anyway,
2339                  * let's be prepared for a page of acl data. */
2340                 localpage = alloc_page(GFP_KERNEL);
2341                 resp_buf = page_address(localpage);
2342                 if (localpage == NULL)
2343                         return -ENOMEM;
2344                 args.acl_pages[0] = localpage;
2345                 args.acl_pgbase = 0;
2346                 args.acl_len = PAGE_SIZE;
2347         } else {
2348                 resp_buf = buf;
2349                 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2350         }
2351         ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2352         if (ret)
2353                 goto out_free;
2354         if (resp_len > args.acl_len)
2355                 nfs4_write_cached_acl(inode, NULL, resp_len);
2356         else
2357                 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2358         if (buf) {
2359                 ret = -ERANGE;
2360                 if (resp_len > buflen)
2361                         goto out_free;
2362                 if (localpage)
2363                         memcpy(buf, resp_buf, resp_len);
2364         }
2365         ret = resp_len;
2366 out_free:
2367         if (localpage)
2368                 __free_page(localpage);
2369         return ret;
2370 }
2371
2372 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2373 {
2374         struct nfs_server *server = NFS_SERVER(inode);
2375         int ret;
2376
2377         if (!nfs4_server_supports_acls(server))
2378                 return -EOPNOTSUPP;
2379         ret = nfs_revalidate_inode(server, inode);
2380         if (ret < 0)
2381                 return ret;
2382         ret = nfs4_read_cached_acl(inode, buf, buflen);
2383         if (ret != -ENOENT)
2384                 return ret;
2385         return nfs4_get_acl_uncached(inode, buf, buflen);
2386 }
2387
2388 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2389 {
2390         struct nfs_server *server = NFS_SERVER(inode);
2391         struct page *pages[NFS4ACL_MAXPAGES];
2392         struct nfs_setaclargs arg = {
2393                 .fh             = NFS_FH(inode),
2394                 .acl_pages      = pages,
2395                 .acl_len        = buflen,
2396         };
2397         struct rpc_message msg = {
2398                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2399                 .rpc_argp       = &arg,
2400                 .rpc_resp       = NULL,
2401         };
2402         int ret;
2403
2404         if (!nfs4_server_supports_acls(server))
2405                 return -EOPNOTSUPP;
2406         buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2407         ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2408         if (ret == 0)
2409                 nfs4_write_cached_acl(inode, buf, buflen);
2410         return ret;
2411 }
2412
2413 static int
2414 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
2415 {
2416         struct nfs4_client *clp = server->nfs4_state;
2417
2418         if (!clp || task->tk_status >= 0)
2419                 return 0;
2420         switch(task->tk_status) {
2421                 case -NFS4ERR_STALE_CLIENTID:
2422                 case -NFS4ERR_STALE_STATEID:
2423                 case -NFS4ERR_EXPIRED:
2424                         rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2425                         nfs4_schedule_state_recovery(clp);
2426                         if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2427                                 rpc_wake_up_task(task);
2428                         task->tk_status = 0;
2429                         return -EAGAIN;
2430                 case -NFS4ERR_GRACE:
2431                 case -NFS4ERR_DELAY:
2432                         rpc_delay(task, NFS4_POLL_RETRY_MAX);
2433                         task->tk_status = 0;
2434                         return -EAGAIN;
2435                 case -NFS4ERR_OLD_STATEID:
2436                         task->tk_status = 0;
2437                         return -EAGAIN;
2438         }
2439         task->tk_status = nfs4_map_errors(task->tk_status);
2440         return 0;
2441 }
2442
2443 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2444 {
2445         DEFINE_WAIT(wait);
2446         sigset_t oldset;
2447         int interruptible, res = 0;
2448
2449         might_sleep();
2450
2451         rpc_clnt_sigmask(clnt, &oldset);
2452         interruptible = TASK_UNINTERRUPTIBLE;
2453         if (clnt->cl_intr)
2454                 interruptible = TASK_INTERRUPTIBLE;
2455         prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2456         nfs4_schedule_state_recovery(clp);
2457         if (clnt->cl_intr && signalled())
2458                 res = -ERESTARTSYS;
2459         else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2460                 schedule();
2461         finish_wait(&clp->cl_waitq, &wait);
2462         rpc_clnt_sigunmask(clnt, &oldset);
2463         return res;
2464 }
2465
2466 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2467 {
2468         sigset_t oldset;
2469         int res = 0;
2470
2471         might_sleep();
2472
2473         if (*timeout <= 0)
2474                 *timeout = NFS4_POLL_RETRY_MIN;
2475         if (*timeout > NFS4_POLL_RETRY_MAX)
2476                 *timeout = NFS4_POLL_RETRY_MAX;
2477         rpc_clnt_sigmask(clnt, &oldset);
2478         if (clnt->cl_intr) {
2479                 schedule_timeout_interruptible(*timeout);
2480                 if (signalled())
2481                         res = -ERESTARTSYS;
2482         } else
2483                 schedule_timeout_uninterruptible(*timeout);
2484         rpc_clnt_sigunmask(clnt, &oldset);
2485         *timeout <<= 1;
2486         return res;
2487 }
2488
2489 /* This is the error handling routine for processes that are allowed
2490  * to sleep.
2491  */
2492 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2493 {
2494         struct nfs4_client *clp = server->nfs4_state;
2495         int ret = errorcode;
2496
2497         exception->retry = 0;
2498         switch(errorcode) {
2499                 case 0:
2500                         return 0;
2501                 case -NFS4ERR_STALE_CLIENTID:
2502                 case -NFS4ERR_STALE_STATEID:
2503                 case -NFS4ERR_EXPIRED:
2504                         ret = nfs4_wait_clnt_recover(server->client, clp);
2505                         if (ret == 0)
2506                                 exception->retry = 1;
2507                         break;
2508                 case -NFS4ERR_GRACE:
2509                 case -NFS4ERR_DELAY:
2510                         ret = nfs4_delay(server->client, &exception->timeout);
2511                         if (ret == 0)
2512                                 exception->retry = 1;
2513                         break;
2514                 case -NFS4ERR_OLD_STATEID:
2515                         if (ret == 0)
2516                                 exception->retry = 1;
2517         }
2518         /* We failed to handle the error */
2519         return nfs4_map_errors(ret);
2520 }
2521
2522 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2523 {
2524         nfs4_verifier sc_verifier;
2525         struct nfs4_setclientid setclientid = {
2526                 .sc_verifier = &sc_verifier,
2527                 .sc_prog = program,
2528         };
2529         struct rpc_message msg = {
2530                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2531                 .rpc_argp = &setclientid,
2532                 .rpc_resp = clp,
2533                 .rpc_cred = clp->cl_cred,
2534         };
2535         u32 *p;
2536         int loop = 0;
2537         int status;
2538
2539         p = (u32*)sc_verifier.data;
2540         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2541         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2542
2543         for(;;) {
2544                 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2545                                 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2546                                 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2547                                 clp->cl_cred->cr_ops->cr_name,
2548                                 clp->cl_id_uniquifier);
2549                 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2550                                 sizeof(setclientid.sc_netid), "tcp");
2551                 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2552                                 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2553                                 clp->cl_ipaddr, port >> 8, port & 255);
2554
2555                 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2556                 if (status != -NFS4ERR_CLID_INUSE)
2557                         break;
2558                 if (signalled())
2559                         break;
2560                 if (loop++ & 1)
2561                         ssleep(clp->cl_lease_time + 1);
2562                 else
2563                         if (++clp->cl_id_uniquifier == 0)
2564                                 break;
2565         }
2566         return status;
2567 }
2568
2569 int
2570 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2571 {
2572         struct nfs_fsinfo fsinfo;
2573         struct rpc_message msg = {
2574                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2575                 .rpc_argp = clp,
2576                 .rpc_resp = &fsinfo,
2577                 .rpc_cred = clp->cl_cred,
2578         };
2579         unsigned long now;
2580         int status;
2581
2582         now = jiffies;
2583         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2584         if (status == 0) {
2585                 spin_lock(&clp->cl_lock);
2586                 clp->cl_lease_time = fsinfo.lease_time * HZ;
2587                 clp->cl_last_renewal = now;
2588                 spin_unlock(&clp->cl_lock);
2589         }
2590         return status;
2591 }
2592
2593 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2594 {
2595         struct nfs4_delegreturnargs args = {
2596                 .fhandle = NFS_FH(inode),
2597                 .stateid = stateid,
2598         };
2599         struct rpc_message msg = {
2600                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2601                 .rpc_argp = &args,
2602                 .rpc_cred = cred,
2603         };
2604
2605         return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2606 }
2607
2608 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2609 {
2610         struct nfs_server *server = NFS_SERVER(inode);
2611         struct nfs4_exception exception = { };
2612         int err;
2613         do {
2614                 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2615                 switch (err) {
2616                         case -NFS4ERR_STALE_STATEID:
2617                         case -NFS4ERR_EXPIRED:
2618                                 nfs4_schedule_state_recovery(server->nfs4_state);
2619                         case 0:
2620                                 return 0;
2621                 }
2622                 err = nfs4_handle_exception(server, err, &exception);
2623         } while (exception.retry);
2624         return err;
2625 }
2626
2627 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2628 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2629
2630 /* 
2631  * sleep, with exponential backoff, and retry the LOCK operation. 
2632  */
2633 static unsigned long
2634 nfs4_set_lock_task_retry(unsigned long timeout)
2635 {
2636         schedule_timeout_interruptible(timeout);
2637         timeout <<= 1;
2638         if (timeout > NFS4_LOCK_MAXTIMEOUT)
2639                 return NFS4_LOCK_MAXTIMEOUT;
2640         return timeout;
2641 }
2642
2643 static inline int
2644 nfs4_lck_type(int cmd, struct file_lock *request)
2645 {
2646         /* set lock type */
2647         switch (request->fl_type) {
2648                 case F_RDLCK:
2649                         return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2650                 case F_WRLCK:
2651                         return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2652                 case F_UNLCK:
2653                         return NFS4_WRITE_LT; 
2654         }
2655         BUG();
2656         return 0;
2657 }
2658
2659 static inline uint64_t
2660 nfs4_lck_length(struct file_lock *request)
2661 {
2662         if (request->fl_end == OFFSET_MAX)
2663                 return ~(uint64_t)0;
2664         return request->fl_end - request->fl_start + 1;
2665 }
2666
2667 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2668 {
2669         struct inode *inode = state->inode;
2670         struct nfs_server *server = NFS_SERVER(inode);
2671         struct nfs4_client *clp = server->nfs4_state;
2672         struct nfs_lockargs arg = {
2673                 .fh = NFS_FH(inode),
2674                 .type = nfs4_lck_type(cmd, request),
2675                 .offset = request->fl_start,
2676                 .length = nfs4_lck_length(request),
2677         };
2678         struct nfs_lockres res = {
2679                 .server = server,
2680         };
2681         struct rpc_message msg = {
2682                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2683                 .rpc_argp       = &arg,
2684                 .rpc_resp       = &res,
2685                 .rpc_cred       = state->owner->so_cred,
2686         };
2687         struct nfs_lowner nlo;
2688         struct nfs4_lock_state *lsp;
2689         int status;
2690
2691         down_read(&clp->cl_sem);
2692         nlo.clientid = clp->cl_clientid;
2693         status = nfs4_set_lock_state(state, request);
2694         if (status != 0)
2695                 goto out;
2696         lsp = request->fl_u.nfs4_fl.owner;
2697         nlo.id = lsp->ls_id; 
2698         arg.u.lockt = &nlo;
2699         status = rpc_call_sync(server->client, &msg, 0);
2700         if (!status) {
2701                 request->fl_type = F_UNLCK;
2702         } else if (status == -NFS4ERR_DENIED) {
2703                 int64_t len, start, end;
2704                 start = res.u.denied.offset;
2705                 len = res.u.denied.length;
2706                 end = start + len - 1;
2707                 if (end < 0 || len == 0)
2708                         request->fl_end = OFFSET_MAX;
2709                 else
2710                         request->fl_end = (loff_t)end;
2711                 request->fl_start = (loff_t)start;
2712                 request->fl_type = F_WRLCK;
2713                 if (res.u.denied.type & 1)
2714                         request->fl_type = F_RDLCK;
2715                 request->fl_pid = 0;
2716                 status = 0;
2717         }
2718 out:
2719         up_read(&clp->cl_sem);
2720         return status;
2721 }
2722
2723 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2724 {
2725         struct nfs4_exception exception = { };
2726         int err;
2727
2728         do {
2729                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2730                                 _nfs4_proc_getlk(state, cmd, request),
2731                                 &exception);
2732         } while (exception.retry);
2733         return err;
2734 }
2735
2736 static int do_vfs_lock(struct file *file, struct file_lock *fl)
2737 {
2738         int res = 0;
2739         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2740                 case FL_POSIX:
2741                         res = posix_lock_file_wait(file, fl);
2742                         break;
2743                 case FL_FLOCK:
2744                         res = flock_lock_file_wait(file, fl);
2745                         break;
2746                 default:
2747                         BUG();
2748         }
2749         if (res < 0)
2750                 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2751                                 __FUNCTION__);
2752         return res;
2753 }
2754
2755 static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2756 {
2757         struct inode *inode = state->inode;
2758         struct nfs_server *server = NFS_SERVER(inode);
2759         struct nfs_lockargs arg = {
2760                 .fh = NFS_FH(inode),
2761                 .type = nfs4_lck_type(cmd, request),
2762                 .offset = request->fl_start,
2763                 .length = nfs4_lck_length(request),
2764         };
2765         struct nfs_lockres res = {
2766                 .server = server,
2767         };
2768         struct rpc_message msg = {
2769                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2770                 .rpc_argp       = &arg,
2771                 .rpc_resp       = &res,
2772                 .rpc_cred       = state->owner->so_cred,
2773         };
2774         struct nfs4_lock_state *lsp;
2775         struct nfs_locku_opargs luargs;
2776         int status;
2777                         
2778         status = nfs4_set_lock_state(state, request);
2779         if (status != 0)
2780                 goto out;
2781         lsp = request->fl_u.nfs4_fl.owner;
2782         /* We might have lost the locks! */
2783         if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2784                 goto out;
2785         luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2786         status = -ENOMEM;
2787         if (luargs.seqid == NULL)
2788                 goto out;
2789         memcpy(luargs.stateid.data, lsp->ls_stateid.data, sizeof(luargs.stateid.data));
2790         arg.u.locku = &luargs;
2791         status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2792         nfs_increment_lock_seqid(status, luargs.seqid);
2793
2794         if (status == 0)
2795                 memcpy(lsp->ls_stateid.data, res.u.stateid.data, 
2796                                 sizeof(lsp->ls_stateid.data));
2797         nfs_free_seqid(luargs.seqid);
2798 out:
2799         if (status == 0)
2800                 do_vfs_lock(request->fl_file, request);
2801         return status;
2802 }
2803
2804 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2805 {
2806         struct nfs4_exception exception = { };
2807         int err;
2808
2809         do {
2810                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2811                                 _nfs4_proc_unlck(state, cmd, request),
2812                                 &exception);
2813         } while (exception.retry);
2814         return err;
2815 }
2816
2817 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2818 {
2819         struct inode *inode = state->inode;
2820         struct nfs_server *server = NFS_SERVER(inode);
2821         struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
2822         struct nfs_lockargs arg = {
2823                 .fh = NFS_FH(inode),
2824                 .type = nfs4_lck_type(cmd, request),
2825                 .offset = request->fl_start,
2826                 .length = nfs4_lck_length(request),
2827         };
2828         struct nfs_lockres res = {
2829                 .server = server,
2830         };
2831         struct rpc_message msg = {
2832                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2833                 .rpc_argp       = &arg,
2834                 .rpc_resp       = &res,
2835                 .rpc_cred       = state->owner->so_cred,
2836         };
2837         struct nfs_lock_opargs largs = {
2838                 .reclaim = reclaim,
2839                 .new_lock_owner = 0,
2840         };
2841         struct nfs_seqid *lock_seqid;
2842         int status = -ENOMEM;
2843
2844         lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2845         if (lock_seqid == NULL)
2846                 return -ENOMEM;
2847         if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
2848                 struct nfs4_state_owner *owner = state->owner;
2849                 struct nfs_open_to_lock otl = {
2850                         .lock_owner = {
2851                                 .clientid = server->nfs4_state->cl_clientid,
2852                         },
2853                 };
2854
2855                 otl.lock_seqid = lock_seqid;
2856                 otl.lock_owner.id = lsp->ls_id;
2857                 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
2858                 largs.u.open_lock = &otl;
2859                 largs.new_lock_owner = 1;
2860                 arg.u.lock = &largs;
2861                 otl.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
2862                 if (otl.open_seqid != NULL) {
2863                         status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2864                         /* increment seqid on success, and seqid mutating errors */
2865                         nfs_increment_open_seqid(status, otl.open_seqid);
2866                         nfs_free_seqid(otl.open_seqid);
2867                 }
2868                 if (status == 0)
2869                         nfs_confirm_seqid(&lsp->ls_seqid, 0);
2870         } else {
2871                 struct nfs_exist_lock el;
2872                 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
2873                 largs.u.exist_lock = &el;
2874                 arg.u.lock = &largs;
2875                 el.seqid = lock_seqid;
2876                 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2877         }
2878         /* increment seqid on success, and seqid mutating errors*/
2879         nfs_increment_lock_seqid(status, lock_seqid);
2880         /* save the returned stateid. */
2881         if (status == 0) {
2882                 memcpy(lsp->ls_stateid.data, res.u.stateid.data, sizeof(lsp->ls_stateid.data));
2883                 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2884         } else if (status == -NFS4ERR_DENIED)
2885                 status = -EAGAIN;
2886         nfs_free_seqid(lock_seqid);
2887         return status;
2888 }
2889
2890 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2891 {
2892         struct nfs_server *server = NFS_SERVER(state->inode);
2893         struct nfs4_exception exception = { };
2894         int err;
2895
2896         do {
2897                 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
2898                 if (err != -NFS4ERR_DELAY)
2899                         break;
2900                 nfs4_handle_exception(server, err, &exception);
2901         } while (exception.retry);
2902         return err;
2903 }
2904
2905 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2906 {
2907         struct nfs_server *server = NFS_SERVER(state->inode);
2908         struct nfs4_exception exception = { };
2909         int err;
2910
2911         do {
2912                 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
2913                 if (err != -NFS4ERR_DELAY)
2914                         break;
2915                 nfs4_handle_exception(server, err, &exception);
2916         } while (exception.retry);
2917         return err;
2918 }
2919
2920 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2921 {
2922         struct nfs4_client *clp = state->owner->so_client;
2923         int status;
2924
2925         down_read(&clp->cl_sem);
2926         status = nfs4_set_lock_state(state, request);
2927         if (status == 0)
2928                 status = _nfs4_do_setlk(state, cmd, request, 0);
2929         if (status == 0) {
2930                 /* Note: we always want to sleep here! */
2931                 request->fl_flags |= FL_SLEEP;
2932                 if (do_vfs_lock(request->fl_file, request) < 0)
2933                         printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
2934         }
2935         up_read(&clp->cl_sem);
2936         return status;
2937 }
2938
2939 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2940 {
2941         struct nfs4_exception exception = { };
2942         int err;
2943
2944         do {
2945                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2946                                 _nfs4_proc_setlk(state, cmd, request),
2947                                 &exception);
2948         } while (exception.retry);
2949         return err;
2950 }
2951
2952 static int
2953 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
2954 {
2955         struct nfs_open_context *ctx;
2956         struct nfs4_state *state;
2957         unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
2958         int status;
2959
2960         /* verify open state */
2961         ctx = (struct nfs_open_context *)filp->private_data;
2962         state = ctx->state;
2963
2964         if (request->fl_start < 0 || request->fl_end < 0)
2965                 return -EINVAL;
2966
2967         if (IS_GETLK(cmd))
2968                 return nfs4_proc_getlk(state, F_GETLK, request);
2969
2970         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
2971                 return -EINVAL;
2972
2973         if (request->fl_type == F_UNLCK)
2974                 return nfs4_proc_unlck(state, cmd, request);
2975
2976         do {
2977                 status = nfs4_proc_setlk(state, cmd, request);
2978                 if ((status != -EAGAIN) || IS_SETLK(cmd))
2979                         break;
2980                 timeout = nfs4_set_lock_task_retry(timeout);
2981                 status = -ERESTARTSYS;
2982                 if (signalled())
2983                         break;
2984         } while(status < 0);
2985         return status;
2986 }
2987
2988
2989 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
2990
2991 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
2992                 size_t buflen, int flags)
2993 {
2994         struct inode *inode = dentry->d_inode;
2995
2996         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
2997                 return -EOPNOTSUPP;
2998
2999         if (!S_ISREG(inode->i_mode) &&
3000             (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3001                 return -EPERM;
3002
3003         return nfs4_proc_set_acl(inode, buf, buflen);
3004 }
3005
3006 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3007  * and that's what we'll do for e.g. user attributes that haven't been set.
3008  * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3009  * attributes in kernel-managed attribute namespaces. */
3010 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3011                 size_t buflen)
3012 {
3013         struct inode *inode = dentry->d_inode;
3014
3015         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3016                 return -EOPNOTSUPP;
3017
3018         return nfs4_proc_get_acl(inode, buf, buflen);
3019 }
3020
3021 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3022 {
3023         size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3024
3025         if (buf && buflen < len)
3026                 return -ERANGE;
3027         if (buf)
3028                 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3029         return len;
3030 }
3031
3032 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3033         .recover_open   = nfs4_open_reclaim,
3034         .recover_lock   = nfs4_lock_reclaim,
3035 };
3036
3037 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3038         .recover_open   = nfs4_open_expired,
3039         .recover_lock   = nfs4_lock_expired,
3040 };
3041
3042 static struct inode_operations nfs4_file_inode_operations = {
3043         .permission     = nfs_permission,
3044         .getattr        = nfs_getattr,
3045         .setattr        = nfs_setattr,
3046         .getxattr       = nfs4_getxattr,
3047         .setxattr       = nfs4_setxattr,
3048         .listxattr      = nfs4_listxattr,
3049 };
3050
3051 struct nfs_rpc_ops      nfs_v4_clientops = {
3052         .version        = 4,                    /* protocol version */
3053         .dentry_ops     = &nfs4_dentry_operations,
3054         .dir_inode_ops  = &nfs4_dir_inode_operations,
3055         .file_inode_ops = &nfs4_file_inode_operations,
3056         .getroot        = nfs4_proc_get_root,
3057         .getattr        = nfs4_proc_getattr,
3058         .setattr        = nfs4_proc_setattr,
3059         .lookup         = nfs4_proc_lookup,
3060         .access         = nfs4_proc_access,
3061         .readlink       = nfs4_proc_readlink,
3062         .read           = nfs4_proc_read,
3063         .write          = nfs4_proc_write,
3064         .commit         = nfs4_proc_commit,
3065         .create         = nfs4_proc_create,
3066         .remove         = nfs4_proc_remove,
3067         .unlink_setup   = nfs4_proc_unlink_setup,
3068         .unlink_done    = nfs4_proc_unlink_done,
3069         .rename         = nfs4_proc_rename,
3070         .link           = nfs4_proc_link,
3071         .symlink        = nfs4_proc_symlink,
3072         .mkdir          = nfs4_proc_mkdir,
3073         .rmdir          = nfs4_proc_remove,
3074         .readdir        = nfs4_proc_readdir,
3075         .mknod          = nfs4_proc_mknod,
3076         .statfs         = nfs4_proc_statfs,
3077         .fsinfo         = nfs4_proc_fsinfo,
3078         .pathconf       = nfs4_proc_pathconf,
3079         .decode_dirent  = nfs4_decode_dirent,
3080         .read_setup     = nfs4_proc_read_setup,
3081         .write_setup    = nfs4_proc_write_setup,
3082         .commit_setup   = nfs4_proc_commit_setup,
3083         .file_open      = nfs4_proc_file_open,
3084         .file_release   = nfs4_proc_file_release,
3085         .lock           = nfs4_proc_lock,
3086         .clear_acl_cache = nfs4_zap_acl_attr,
3087 };
3088
3089 /*
3090  * Local variables:
3091  *  c-basic-offset: 8
3092  * End:
3093  */