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