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