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