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