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