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