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