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