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