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