nfs41: do not zero seqid portion of stateid on close
[linux-flexiantxendom0-natty.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/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/sunrpc/clnt.h>
43 #include <linux/nfs.h>
44 #include <linux/nfs4.h>
45 #include <linux/nfs_fs.h>
46 #include <linux/nfs_page.h>
47 #include <linux/namei.h>
48 #include <linux/mount.h>
49 #include <linux/module.h>
50 #include <linux/sunrpc/bc_xprt.h>
51
52 #include "nfs4_fs.h"
53 #include "delegation.h"
54 #include "internal.h"
55 #include "iostat.h"
56 #include "callback.h"
57
58 #define NFSDBG_FACILITY         NFSDBG_PROC
59
60 #define NFS4_POLL_RETRY_MIN     (HZ/10)
61 #define NFS4_POLL_RETRY_MAX     (15*HZ)
62
63 #define NFS4_MAX_LOOP_ON_RECOVER (10)
64
65 struct nfs4_opendata;
66 static int _nfs4_proc_open(struct nfs4_opendata *data);
67 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
68 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
69 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
70 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
71 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72
73 /* Prevent leaks of NFSv4 errors into userland */
74 static int nfs4_map_errors(int err)
75 {
76         if (err >= -1000)
77                 return err;
78         switch (err) {
79         case -NFS4ERR_RESOURCE:
80                 return -EREMOTEIO;
81         default:
82                 dprintk("%s could not handle NFSv4 error %d\n",
83                                 __func__, -err);
84                 break;
85         }
86         return -EIO;
87 }
88
89 /*
90  * This is our standard bitmap for GETATTR requests.
91  */
92 const u32 nfs4_fattr_bitmap[2] = {
93         FATTR4_WORD0_TYPE
94         | FATTR4_WORD0_CHANGE
95         | FATTR4_WORD0_SIZE
96         | FATTR4_WORD0_FSID
97         | FATTR4_WORD0_FILEID,
98         FATTR4_WORD1_MODE
99         | FATTR4_WORD1_NUMLINKS
100         | FATTR4_WORD1_OWNER
101         | FATTR4_WORD1_OWNER_GROUP
102         | FATTR4_WORD1_RAWDEV
103         | FATTR4_WORD1_SPACE_USED
104         | FATTR4_WORD1_TIME_ACCESS
105         | FATTR4_WORD1_TIME_METADATA
106         | FATTR4_WORD1_TIME_MODIFY
107 };
108
109 const u32 nfs4_statfs_bitmap[2] = {
110         FATTR4_WORD0_FILES_AVAIL
111         | FATTR4_WORD0_FILES_FREE
112         | FATTR4_WORD0_FILES_TOTAL,
113         FATTR4_WORD1_SPACE_AVAIL
114         | FATTR4_WORD1_SPACE_FREE
115         | FATTR4_WORD1_SPACE_TOTAL
116 };
117
118 const u32 nfs4_pathconf_bitmap[2] = {
119         FATTR4_WORD0_MAXLINK
120         | FATTR4_WORD0_MAXNAME,
121         0
122 };
123
124 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
125                         | FATTR4_WORD0_MAXREAD
126                         | FATTR4_WORD0_MAXWRITE
127                         | FATTR4_WORD0_LEASE_TIME,
128                         0
129 };
130
131 const u32 nfs4_fs_locations_bitmap[2] = {
132         FATTR4_WORD0_TYPE
133         | FATTR4_WORD0_CHANGE
134         | FATTR4_WORD0_SIZE
135         | FATTR4_WORD0_FSID
136         | FATTR4_WORD0_FILEID
137         | FATTR4_WORD0_FS_LOCATIONS,
138         FATTR4_WORD1_MODE
139         | FATTR4_WORD1_NUMLINKS
140         | FATTR4_WORD1_OWNER
141         | FATTR4_WORD1_OWNER_GROUP
142         | FATTR4_WORD1_RAWDEV
143         | FATTR4_WORD1_SPACE_USED
144         | FATTR4_WORD1_TIME_ACCESS
145         | FATTR4_WORD1_TIME_METADATA
146         | FATTR4_WORD1_TIME_MODIFY
147         | FATTR4_WORD1_MOUNTED_ON_FILEID
148 };
149
150 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
151                 struct nfs4_readdir_arg *readdir)
152 {
153         __be32 *start, *p;
154
155         BUG_ON(readdir->count < 80);
156         if (cookie > 2) {
157                 readdir->cookie = cookie;
158                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
159                 return;
160         }
161
162         readdir->cookie = 0;
163         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
164         if (cookie == 2)
165                 return;
166         
167         /*
168          * NFSv4 servers do not return entries for '.' and '..'
169          * Therefore, we fake these entries here.  We let '.'
170          * have cookie 0 and '..' have cookie 1.  Note that
171          * when talking to the server, we always send cookie 0
172          * instead of 1 or 2.
173          */
174         start = p = kmap_atomic(*readdir->pages, KM_USER0);
175         
176         if (cookie == 0) {
177                 *p++ = xdr_one;                                  /* next */
178                 *p++ = xdr_zero;                   /* cookie, first word */
179                 *p++ = xdr_one;                   /* cookie, second word */
180                 *p++ = xdr_one;                             /* entry len */
181                 memcpy(p, ".\0\0\0", 4);                        /* entry */
182                 p++;
183                 *p++ = xdr_one;                         /* bitmap length */
184                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
185                 *p++ = htonl(8);              /* attribute buffer length */
186                 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
187         }
188         
189         *p++ = xdr_one;                                  /* next */
190         *p++ = xdr_zero;                   /* cookie, first word */
191         *p++ = xdr_two;                   /* cookie, second word */
192         *p++ = xdr_two;                             /* entry len */
193         memcpy(p, "..\0\0", 4);                         /* entry */
194         p++;
195         *p++ = xdr_one;                         /* bitmap length */
196         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
197         *p++ = htonl(8);              /* attribute buffer length */
198         p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
199
200         readdir->pgbase = (char *)p - (char *)start;
201         readdir->count -= readdir->pgbase;
202         kunmap_atomic(start, KM_USER0);
203 }
204
205 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
206 {
207         int res;
208
209         might_sleep();
210
211         res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
212                         nfs_wait_bit_killable, TASK_KILLABLE);
213         return res;
214 }
215
216 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
217 {
218         int res = 0;
219
220         might_sleep();
221
222         if (*timeout <= 0)
223                 *timeout = NFS4_POLL_RETRY_MIN;
224         if (*timeout > NFS4_POLL_RETRY_MAX)
225                 *timeout = NFS4_POLL_RETRY_MAX;
226         schedule_timeout_killable(*timeout);
227         if (fatal_signal_pending(current))
228                 res = -ERESTARTSYS;
229         *timeout <<= 1;
230         return res;
231 }
232
233 /* This is the error handling routine for processes that are allowed
234  * to sleep.
235  */
236 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
237 {
238         struct nfs_client *clp = server->nfs_client;
239         struct nfs4_state *state = exception->state;
240         int ret = errorcode;
241
242         exception->retry = 0;
243         switch(errorcode) {
244                 case 0:
245                         return 0;
246                 case -NFS4ERR_ADMIN_REVOKED:
247                 case -NFS4ERR_BAD_STATEID:
248                 case -NFS4ERR_OPENMODE:
249                         if (state == NULL)
250                                 break;
251                         nfs4_state_mark_reclaim_nograce(clp, state);
252                 case -NFS4ERR_STALE_CLIENTID:
253                 case -NFS4ERR_STALE_STATEID:
254                 case -NFS4ERR_EXPIRED:
255                         nfs4_schedule_state_recovery(clp);
256                         ret = nfs4_wait_clnt_recover(clp);
257                         if (ret == 0)
258                                 exception->retry = 1;
259 #if !defined(CONFIG_NFS_V4_1)
260                         break;
261 #else /* !defined(CONFIG_NFS_V4_1) */
262                         if (!nfs4_has_session(server->nfs_client))
263                                 break;
264                         /* FALLTHROUGH */
265                 case -NFS4ERR_BADSESSION:
266                 case -NFS4ERR_BADSLOT:
267                 case -NFS4ERR_BAD_HIGH_SLOT:
268                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269                 case -NFS4ERR_DEADSESSION:
270                 case -NFS4ERR_SEQ_FALSE_RETRY:
271                 case -NFS4ERR_SEQ_MISORDERED:
272                         dprintk("%s ERROR: %d Reset session\n", __func__,
273                                 errorcode);
274                         nfs4_schedule_state_recovery(clp);
275                         exception->retry = 1;
276                         break;
277 #endif /* !defined(CONFIG_NFS_V4_1) */
278                 case -NFS4ERR_FILE_OPEN:
279                         if (exception->timeout > HZ) {
280                                 /* We have retried a decent amount, time to
281                                  * fail
282                                  */
283                                 ret = -EBUSY;
284                                 break;
285                         }
286                 case -NFS4ERR_GRACE:
287                 case -NFS4ERR_DELAY:
288                         ret = nfs4_delay(server->client, &exception->timeout);
289                         if (ret != 0)
290                                 break;
291                 case -NFS4ERR_OLD_STATEID:
292                         exception->retry = 1;
293         }
294         /* We failed to handle the error */
295         return nfs4_map_errors(ret);
296 }
297
298
299 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
300 {
301         struct nfs_client *clp = server->nfs_client;
302         spin_lock(&clp->cl_lock);
303         if (time_before(clp->cl_last_renewal,timestamp))
304                 clp->cl_last_renewal = timestamp;
305         spin_unlock(&clp->cl_lock);
306 }
307
308 #if defined(CONFIG_NFS_V4_1)
309
310 /*
311  * nfs4_free_slot - free a slot and efficiently update slot table.
312  *
313  * freeing a slot is trivially done by clearing its respective bit
314  * in the bitmap.
315  * If the freed slotid equals highest_used_slotid we want to update it
316  * so that the server would be able to size down the slot table if needed,
317  * otherwise we know that the highest_used_slotid is still in use.
318  * When updating highest_used_slotid there may be "holes" in the bitmap
319  * so we need to scan down from highest_used_slotid to 0 looking for the now
320  * highest slotid in use.
321  * If none found, highest_used_slotid is set to -1.
322  *
323  * Must be called while holding tbl->slot_tbl_lock
324  */
325 static void
326 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
327 {
328         int slotid = free_slotid;
329
330         /* clear used bit in bitmap */
331         __clear_bit(slotid, tbl->used_slots);
332
333         /* update highest_used_slotid when it is freed */
334         if (slotid == tbl->highest_used_slotid) {
335                 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
336                 if (slotid < tbl->max_slots)
337                         tbl->highest_used_slotid = slotid;
338                 else
339                         tbl->highest_used_slotid = -1;
340         }
341         dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
342                 free_slotid, tbl->highest_used_slotid);
343 }
344
345 /*
346  * Signal state manager thread if session is drained
347  */
348 static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
349 {
350         struct rpc_task *task;
351
352         if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
353                 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
354                 if (task)
355                         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
356                 return;
357         }
358
359         if (ses->fc_slot_table.highest_used_slotid != -1)
360                 return;
361
362         dprintk("%s COMPLETE: Session Drained\n", __func__);
363         complete(&ses->complete);
364 }
365
366 static void nfs41_sequence_free_slot(const struct nfs_client *clp,
367                               struct nfs4_sequence_res *res)
368 {
369         struct nfs4_slot_table *tbl;
370
371         tbl = &clp->cl_session->fc_slot_table;
372         if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
373                 /* just wake up the next guy waiting since
374                  * we may have not consumed a slot after all */
375                 dprintk("%s: No slot\n", __func__);
376                 return;
377         }
378
379         spin_lock(&tbl->slot_tbl_lock);
380         nfs4_free_slot(tbl, res->sr_slotid);
381         nfs41_check_drain_session_complete(clp->cl_session);
382         spin_unlock(&tbl->slot_tbl_lock);
383         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
384 }
385
386 static void nfs41_sequence_done(struct nfs_client *clp,
387                                 struct nfs4_sequence_res *res,
388                                 int rpc_status)
389 {
390         unsigned long timestamp;
391         struct nfs4_slot_table *tbl;
392         struct nfs4_slot *slot;
393
394         /*
395          * sr_status remains 1 if an RPC level error occurred. The server
396          * may or may not have processed the sequence operation..
397          * Proceed as if the server received and processed the sequence
398          * operation.
399          */
400         if (res->sr_status == 1)
401                 res->sr_status = NFS_OK;
402
403         /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
404         if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
405                 goto out;
406
407         /* Check the SEQUENCE operation status */
408         if (res->sr_status == 0) {
409                 tbl = &clp->cl_session->fc_slot_table;
410                 slot = tbl->slots + res->sr_slotid;
411                 /* Update the slot's sequence and clientid lease timer */
412                 ++slot->seq_nr;
413                 timestamp = res->sr_renewal_time;
414                 spin_lock(&clp->cl_lock);
415                 if (time_before(clp->cl_last_renewal, timestamp))
416                         clp->cl_last_renewal = timestamp;
417                 spin_unlock(&clp->cl_lock);
418                 /* Check sequence flags */
419                 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
420         }
421 out:
422         /* The session may be reset by one of the error handlers. */
423         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
424         nfs41_sequence_free_slot(clp, res);
425 }
426
427 /*
428  * nfs4_find_slot - efficiently look for a free slot
429  *
430  * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
431  * If found, we mark the slot as used, update the highest_used_slotid,
432  * and respectively set up the sequence operation args.
433  * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
434  *
435  * Note: must be called with under the slot_tbl_lock.
436  */
437 static u8
438 nfs4_find_slot(struct nfs4_slot_table *tbl)
439 {
440         int slotid;
441         u8 ret_id = NFS4_MAX_SLOT_TABLE;
442         BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
443
444         dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
445                 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
446                 tbl->max_slots);
447         slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
448         if (slotid >= tbl->max_slots)
449                 goto out;
450         __set_bit(slotid, tbl->used_slots);
451         if (slotid > tbl->highest_used_slotid)
452                 tbl->highest_used_slotid = slotid;
453         ret_id = slotid;
454 out:
455         dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
456                 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
457         return ret_id;
458 }
459
460 static int nfs41_setup_sequence(struct nfs4_session *session,
461                                 struct nfs4_sequence_args *args,
462                                 struct nfs4_sequence_res *res,
463                                 int cache_reply,
464                                 struct rpc_task *task)
465 {
466         struct nfs4_slot *slot;
467         struct nfs4_slot_table *tbl;
468         u8 slotid;
469
470         dprintk("--> %s\n", __func__);
471         /* slot already allocated? */
472         if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
473                 return 0;
474
475         memset(res, 0, sizeof(*res));
476         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
477         tbl = &session->fc_slot_table;
478
479         spin_lock(&tbl->slot_tbl_lock);
480         if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
481             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
482                 /*
483                  * The state manager will wait until the slot table is empty.
484                  * Schedule the reset thread
485                  */
486                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
487                 spin_unlock(&tbl->slot_tbl_lock);
488                 dprintk("%s Schedule Session Reset\n", __func__);
489                 return -EAGAIN;
490         }
491
492         if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
493             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
494                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
495                 spin_unlock(&tbl->slot_tbl_lock);
496                 dprintk("%s enforce FIFO order\n", __func__);
497                 return -EAGAIN;
498         }
499
500         slotid = nfs4_find_slot(tbl);
501         if (slotid == NFS4_MAX_SLOT_TABLE) {
502                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
503                 spin_unlock(&tbl->slot_tbl_lock);
504                 dprintk("<-- %s: no free slots\n", __func__);
505                 return -EAGAIN;
506         }
507         spin_unlock(&tbl->slot_tbl_lock);
508
509         rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
510         slot = tbl->slots + slotid;
511         args->sa_session = session;
512         args->sa_slotid = slotid;
513         args->sa_cache_this = cache_reply;
514
515         dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
516
517         res->sr_session = session;
518         res->sr_slotid = slotid;
519         res->sr_renewal_time = jiffies;
520         /*
521          * sr_status is only set in decode_sequence, and so will remain
522          * set to 1 if an rpc level failure occurs.
523          */
524         res->sr_status = 1;
525         return 0;
526 }
527
528 int nfs4_setup_sequence(struct nfs_client *clp,
529                         struct nfs4_sequence_args *args,
530                         struct nfs4_sequence_res *res,
531                         int cache_reply,
532                         struct rpc_task *task)
533 {
534         int ret = 0;
535
536         dprintk("--> %s clp %p session %p sr_slotid %d\n",
537                 __func__, clp, clp->cl_session, res->sr_slotid);
538
539         if (!nfs4_has_session(clp))
540                 goto out;
541         ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
542                                    task);
543         if (ret && ret != -EAGAIN) {
544                 /* terminate rpc task */
545                 task->tk_status = ret;
546                 task->tk_action = NULL;
547         }
548 out:
549         dprintk("<-- %s status=%d\n", __func__, ret);
550         return ret;
551 }
552
553 struct nfs41_call_sync_data {
554         struct nfs_client *clp;
555         struct nfs4_sequence_args *seq_args;
556         struct nfs4_sequence_res *seq_res;
557         int cache_reply;
558 };
559
560 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
561 {
562         struct nfs41_call_sync_data *data = calldata;
563
564         dprintk("--> %s data->clp->cl_session %p\n", __func__,
565                 data->clp->cl_session);
566         if (nfs4_setup_sequence(data->clp, data->seq_args,
567                                 data->seq_res, data->cache_reply, task))
568                 return;
569         rpc_call_start(task);
570 }
571
572 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
573 {
574         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
575         nfs41_call_sync_prepare(task, calldata);
576 }
577
578 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
579 {
580         struct nfs41_call_sync_data *data = calldata;
581
582         nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
583 }
584
585 struct rpc_call_ops nfs41_call_sync_ops = {
586         .rpc_call_prepare = nfs41_call_sync_prepare,
587         .rpc_call_done = nfs41_call_sync_done,
588 };
589
590 struct rpc_call_ops nfs41_call_priv_sync_ops = {
591         .rpc_call_prepare = nfs41_call_priv_sync_prepare,
592         .rpc_call_done = nfs41_call_sync_done,
593 };
594
595 static int nfs4_call_sync_sequence(struct nfs_client *clp,
596                                    struct rpc_clnt *clnt,
597                                    struct rpc_message *msg,
598                                    struct nfs4_sequence_args *args,
599                                    struct nfs4_sequence_res *res,
600                                    int cache_reply,
601                                    int privileged)
602 {
603         int ret;
604         struct rpc_task *task;
605         struct nfs41_call_sync_data data = {
606                 .clp = clp,
607                 .seq_args = args,
608                 .seq_res = res,
609                 .cache_reply = cache_reply,
610         };
611         struct rpc_task_setup task_setup = {
612                 .rpc_client = clnt,
613                 .rpc_message = msg,
614                 .callback_ops = &nfs41_call_sync_ops,
615                 .callback_data = &data
616         };
617
618         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
619         if (privileged)
620                 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
621         task = rpc_run_task(&task_setup);
622         if (IS_ERR(task))
623                 ret = PTR_ERR(task);
624         else {
625                 ret = task->tk_status;
626                 rpc_put_task(task);
627         }
628         return ret;
629 }
630
631 int _nfs4_call_sync_session(struct nfs_server *server,
632                             struct rpc_message *msg,
633                             struct nfs4_sequence_args *args,
634                             struct nfs4_sequence_res *res,
635                             int cache_reply)
636 {
637         return nfs4_call_sync_sequence(server->nfs_client, server->client,
638                                        msg, args, res, cache_reply, 0);
639 }
640
641 #endif /* CONFIG_NFS_V4_1 */
642
643 int _nfs4_call_sync(struct nfs_server *server,
644                     struct rpc_message *msg,
645                     struct nfs4_sequence_args *args,
646                     struct nfs4_sequence_res *res,
647                     int cache_reply)
648 {
649         args->sa_session = res->sr_session = NULL;
650         return rpc_call_sync(server->client, msg, 0);
651 }
652
653 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
654         (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
655                         &(res)->seq_res, (cache_reply))
656
657 static void nfs4_sequence_done(const struct nfs_server *server,
658                                struct nfs4_sequence_res *res, int rpc_status)
659 {
660 #ifdef CONFIG_NFS_V4_1
661         if (nfs4_has_session(server->nfs_client))
662                 nfs41_sequence_done(server->nfs_client, res, rpc_status);
663 #endif /* CONFIG_NFS_V4_1 */
664 }
665
666 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
667 {
668         struct nfs_inode *nfsi = NFS_I(dir);
669
670         spin_lock(&dir->i_lock);
671         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
672         if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
673                 nfs_force_lookup_revalidate(dir);
674         nfsi->change_attr = cinfo->after;
675         spin_unlock(&dir->i_lock);
676 }
677
678 struct nfs4_opendata {
679         struct kref kref;
680         struct nfs_openargs o_arg;
681         struct nfs_openres o_res;
682         struct nfs_open_confirmargs c_arg;
683         struct nfs_open_confirmres c_res;
684         struct nfs_fattr f_attr;
685         struct nfs_fattr dir_attr;
686         struct path path;
687         struct dentry *dir;
688         struct nfs4_state_owner *owner;
689         struct nfs4_state *state;
690         struct iattr attrs;
691         unsigned long timestamp;
692         unsigned int rpc_done : 1;
693         int rpc_status;
694         int cancelled;
695 };
696
697
698 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
699 {
700         p->o_res.f_attr = &p->f_attr;
701         p->o_res.dir_attr = &p->dir_attr;
702         p->o_res.seqid = p->o_arg.seqid;
703         p->c_res.seqid = p->c_arg.seqid;
704         p->o_res.server = p->o_arg.server;
705         nfs_fattr_init(&p->f_attr);
706         nfs_fattr_init(&p->dir_attr);
707         p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
708 }
709
710 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
711                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
712                 const struct iattr *attrs)
713 {
714         struct dentry *parent = dget_parent(path->dentry);
715         struct inode *dir = parent->d_inode;
716         struct nfs_server *server = NFS_SERVER(dir);
717         struct nfs4_opendata *p;
718
719         p = kzalloc(sizeof(*p), GFP_KERNEL);
720         if (p == NULL)
721                 goto err;
722         p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
723         if (p->o_arg.seqid == NULL)
724                 goto err_free;
725         p->path.mnt = mntget(path->mnt);
726         p->path.dentry = dget(path->dentry);
727         p->dir = parent;
728         p->owner = sp;
729         atomic_inc(&sp->so_count);
730         p->o_arg.fh = NFS_FH(dir);
731         p->o_arg.open_flags = flags;
732         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
733         p->o_arg.clientid = server->nfs_client->cl_clientid;
734         p->o_arg.id = sp->so_owner_id.id;
735         p->o_arg.name = &p->path.dentry->d_name;
736         p->o_arg.server = server;
737         p->o_arg.bitmask = server->attr_bitmask;
738         p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
739         if (flags & O_EXCL) {
740                 if (nfs4_has_persistent_session(server->nfs_client)) {
741                         /* GUARDED */
742                         p->o_arg.u.attrs = &p->attrs;
743                         memcpy(&p->attrs, attrs, sizeof(p->attrs));
744                 } else { /* EXCLUSIVE4_1 */
745                         u32 *s = (u32 *) p->o_arg.u.verifier.data;
746                         s[0] = jiffies;
747                         s[1] = current->pid;
748                 }
749         } else if (flags & O_CREAT) {
750                 p->o_arg.u.attrs = &p->attrs;
751                 memcpy(&p->attrs, attrs, sizeof(p->attrs));
752         }
753         p->c_arg.fh = &p->o_res.fh;
754         p->c_arg.stateid = &p->o_res.stateid;
755         p->c_arg.seqid = p->o_arg.seqid;
756         nfs4_init_opendata_res(p);
757         kref_init(&p->kref);
758         return p;
759 err_free:
760         kfree(p);
761 err:
762         dput(parent);
763         return NULL;
764 }
765
766 static void nfs4_opendata_free(struct kref *kref)
767 {
768         struct nfs4_opendata *p = container_of(kref,
769                         struct nfs4_opendata, kref);
770
771         nfs_free_seqid(p->o_arg.seqid);
772         if (p->state != NULL)
773                 nfs4_put_open_state(p->state);
774         nfs4_put_state_owner(p->owner);
775         dput(p->dir);
776         path_put(&p->path);
777         kfree(p);
778 }
779
780 static void nfs4_opendata_put(struct nfs4_opendata *p)
781 {
782         if (p != NULL)
783                 kref_put(&p->kref, nfs4_opendata_free);
784 }
785
786 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
787 {
788         int ret;
789
790         ret = rpc_wait_for_completion_task(task);
791         return ret;
792 }
793
794 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
795 {
796         int ret = 0;
797
798         if (open_mode & O_EXCL)
799                 goto out;
800         switch (mode & (FMODE_READ|FMODE_WRITE)) {
801                 case FMODE_READ:
802                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
803                                 && state->n_rdonly != 0;
804                         break;
805                 case FMODE_WRITE:
806                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
807                                 && state->n_wronly != 0;
808                         break;
809                 case FMODE_READ|FMODE_WRITE:
810                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
811                                 && state->n_rdwr != 0;
812         }
813 out:
814         return ret;
815 }
816
817 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
818 {
819         if ((delegation->type & fmode) != fmode)
820                 return 0;
821         if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
822                 return 0;
823         nfs_mark_delegation_referenced(delegation);
824         return 1;
825 }
826
827 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
828 {
829         switch (fmode) {
830                 case FMODE_WRITE:
831                         state->n_wronly++;
832                         break;
833                 case FMODE_READ:
834                         state->n_rdonly++;
835                         break;
836                 case FMODE_READ|FMODE_WRITE:
837                         state->n_rdwr++;
838         }
839         nfs4_state_set_mode_locked(state, state->state | fmode);
840 }
841
842 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
843 {
844         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
845                 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
846         memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
847         switch (fmode) {
848                 case FMODE_READ:
849                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
850                         break;
851                 case FMODE_WRITE:
852                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
853                         break;
854                 case FMODE_READ|FMODE_WRITE:
855                         set_bit(NFS_O_RDWR_STATE, &state->flags);
856         }
857 }
858
859 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
860 {
861         write_seqlock(&state->seqlock);
862         nfs_set_open_stateid_locked(state, stateid, fmode);
863         write_sequnlock(&state->seqlock);
864 }
865
866 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
867 {
868         /*
869          * Protect the call to nfs4_state_set_mode_locked and
870          * serialise the stateid update
871          */
872         write_seqlock(&state->seqlock);
873         if (deleg_stateid != NULL) {
874                 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
875                 set_bit(NFS_DELEGATED_STATE, &state->flags);
876         }
877         if (open_stateid != NULL)
878                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
879         write_sequnlock(&state->seqlock);
880         spin_lock(&state->owner->so_lock);
881         update_open_stateflags(state, fmode);
882         spin_unlock(&state->owner->so_lock);
883 }
884
885 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
886 {
887         struct nfs_inode *nfsi = NFS_I(state->inode);
888         struct nfs_delegation *deleg_cur;
889         int ret = 0;
890
891         fmode &= (FMODE_READ|FMODE_WRITE);
892
893         rcu_read_lock();
894         deleg_cur = rcu_dereference(nfsi->delegation);
895         if (deleg_cur == NULL)
896                 goto no_delegation;
897
898         spin_lock(&deleg_cur->lock);
899         if (nfsi->delegation != deleg_cur ||
900             (deleg_cur->type & fmode) != fmode)
901                 goto no_delegation_unlock;
902
903         if (delegation == NULL)
904                 delegation = &deleg_cur->stateid;
905         else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
906                 goto no_delegation_unlock;
907
908         nfs_mark_delegation_referenced(deleg_cur);
909         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
910         ret = 1;
911 no_delegation_unlock:
912         spin_unlock(&deleg_cur->lock);
913 no_delegation:
914         rcu_read_unlock();
915
916         if (!ret && open_stateid != NULL) {
917                 __update_open_stateid(state, open_stateid, NULL, fmode);
918                 ret = 1;
919         }
920
921         return ret;
922 }
923
924
925 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
926 {
927         struct nfs_delegation *delegation;
928
929         rcu_read_lock();
930         delegation = rcu_dereference(NFS_I(inode)->delegation);
931         if (delegation == NULL || (delegation->type & fmode) == fmode) {
932                 rcu_read_unlock();
933                 return;
934         }
935         rcu_read_unlock();
936         nfs_inode_return_delegation(inode);
937 }
938
939 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
940 {
941         struct nfs4_state *state = opendata->state;
942         struct nfs_inode *nfsi = NFS_I(state->inode);
943         struct nfs_delegation *delegation;
944         int open_mode = opendata->o_arg.open_flags & O_EXCL;
945         fmode_t fmode = opendata->o_arg.fmode;
946         nfs4_stateid stateid;
947         int ret = -EAGAIN;
948
949         for (;;) {
950                 if (can_open_cached(state, fmode, open_mode)) {
951                         spin_lock(&state->owner->so_lock);
952                         if (can_open_cached(state, fmode, open_mode)) {
953                                 update_open_stateflags(state, fmode);
954                                 spin_unlock(&state->owner->so_lock);
955                                 goto out_return_state;
956                         }
957                         spin_unlock(&state->owner->so_lock);
958                 }
959                 rcu_read_lock();
960                 delegation = rcu_dereference(nfsi->delegation);
961                 if (delegation == NULL ||
962                     !can_open_delegated(delegation, fmode)) {
963                         rcu_read_unlock();
964                         break;
965                 }
966                 /* Save the delegation */
967                 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
968                 rcu_read_unlock();
969                 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
970                 if (ret != 0)
971                         goto out;
972                 ret = -EAGAIN;
973
974                 /* Try to update the stateid using the delegation */
975                 if (update_open_stateid(state, NULL, &stateid, fmode))
976                         goto out_return_state;
977         }
978 out:
979         return ERR_PTR(ret);
980 out_return_state:
981         atomic_inc(&state->count);
982         return state;
983 }
984
985 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
986 {
987         struct inode *inode;
988         struct nfs4_state *state = NULL;
989         struct nfs_delegation *delegation;
990         int ret;
991
992         if (!data->rpc_done) {
993                 state = nfs4_try_open_cached(data);
994                 goto out;
995         }
996
997         ret = -EAGAIN;
998         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
999                 goto err;
1000         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1001         ret = PTR_ERR(inode);
1002         if (IS_ERR(inode))
1003                 goto err;
1004         ret = -ENOMEM;
1005         state = nfs4_get_open_state(inode, data->owner);
1006         if (state == NULL)
1007                 goto err_put_inode;
1008         if (data->o_res.delegation_type != 0) {
1009                 int delegation_flags = 0;
1010
1011                 rcu_read_lock();
1012                 delegation = rcu_dereference(NFS_I(inode)->delegation);
1013                 if (delegation)
1014                         delegation_flags = delegation->flags;
1015                 rcu_read_unlock();
1016                 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1017                         nfs_inode_set_delegation(state->inode,
1018                                         data->owner->so_cred,
1019                                         &data->o_res);
1020                 else
1021                         nfs_inode_reclaim_delegation(state->inode,
1022                                         data->owner->so_cred,
1023                                         &data->o_res);
1024         }
1025
1026         update_open_stateid(state, &data->o_res.stateid, NULL,
1027                         data->o_arg.fmode);
1028         iput(inode);
1029 out:
1030         return state;
1031 err_put_inode:
1032         iput(inode);
1033 err:
1034         return ERR_PTR(ret);
1035 }
1036
1037 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1038 {
1039         struct nfs_inode *nfsi = NFS_I(state->inode);
1040         struct nfs_open_context *ctx;
1041
1042         spin_lock(&state->inode->i_lock);
1043         list_for_each_entry(ctx, &nfsi->open_files, list) {
1044                 if (ctx->state != state)
1045                         continue;
1046                 get_nfs_open_context(ctx);
1047                 spin_unlock(&state->inode->i_lock);
1048                 return ctx;
1049         }
1050         spin_unlock(&state->inode->i_lock);
1051         return ERR_PTR(-ENOENT);
1052 }
1053
1054 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1055 {
1056         struct nfs4_opendata *opendata;
1057
1058         opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
1059         if (opendata == NULL)
1060                 return ERR_PTR(-ENOMEM);
1061         opendata->state = state;
1062         atomic_inc(&state->count);
1063         return opendata;
1064 }
1065
1066 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1067 {
1068         struct nfs4_state *newstate;
1069         int ret;
1070
1071         opendata->o_arg.open_flags = 0;
1072         opendata->o_arg.fmode = fmode;
1073         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1074         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1075         nfs4_init_opendata_res(opendata);
1076         ret = _nfs4_recover_proc_open(opendata);
1077         if (ret != 0)
1078                 return ret; 
1079         newstate = nfs4_opendata_to_nfs4_state(opendata);
1080         if (IS_ERR(newstate))
1081                 return PTR_ERR(newstate);
1082         nfs4_close_state(&opendata->path, newstate, fmode);
1083         *res = newstate;
1084         return 0;
1085 }
1086
1087 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1088 {
1089         struct nfs4_state *newstate;
1090         int ret;
1091
1092         /* memory barrier prior to reading state->n_* */
1093         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1094         smp_rmb();
1095         if (state->n_rdwr != 0) {
1096                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1097                 if (ret != 0)
1098                         return ret;
1099                 if (newstate != state)
1100                         return -ESTALE;
1101         }
1102         if (state->n_wronly != 0) {
1103                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1104                 if (ret != 0)
1105                         return ret;
1106                 if (newstate != state)
1107                         return -ESTALE;
1108         }
1109         if (state->n_rdonly != 0) {
1110                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1111                 if (ret != 0)
1112                         return ret;
1113                 if (newstate != state)
1114                         return -ESTALE;
1115         }
1116         /*
1117          * We may have performed cached opens for all three recoveries.
1118          * Check if we need to update the current stateid.
1119          */
1120         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1121             memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1122                 write_seqlock(&state->seqlock);
1123                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1124                         memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1125                 write_sequnlock(&state->seqlock);
1126         }
1127         return 0;
1128 }
1129
1130 /*
1131  * OPEN_RECLAIM:
1132  *      reclaim state on the server after a reboot.
1133  */
1134 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1135 {
1136         struct nfs_delegation *delegation;
1137         struct nfs4_opendata *opendata;
1138         fmode_t delegation_type = 0;
1139         int status;
1140
1141         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1142         if (IS_ERR(opendata))
1143                 return PTR_ERR(opendata);
1144         opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1145         opendata->o_arg.fh = NFS_FH(state->inode);
1146         rcu_read_lock();
1147         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1148         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1149                 delegation_type = delegation->type;
1150         rcu_read_unlock();
1151         opendata->o_arg.u.delegation_type = delegation_type;
1152         status = nfs4_open_recover(opendata, state);
1153         nfs4_opendata_put(opendata);
1154         return status;
1155 }
1156
1157 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1158 {
1159         struct nfs_server *server = NFS_SERVER(state->inode);
1160         struct nfs4_exception exception = { };
1161         int err;
1162         do {
1163                 err = _nfs4_do_open_reclaim(ctx, state);
1164                 if (err != -NFS4ERR_DELAY)
1165                         break;
1166                 nfs4_handle_exception(server, err, &exception);
1167         } while (exception.retry);
1168         return err;
1169 }
1170
1171 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1172 {
1173         struct nfs_open_context *ctx;
1174         int ret;
1175
1176         ctx = nfs4_state_find_open_context(state);
1177         if (IS_ERR(ctx))
1178                 return PTR_ERR(ctx);
1179         ret = nfs4_do_open_reclaim(ctx, state);
1180         put_nfs_open_context(ctx);
1181         return ret;
1182 }
1183
1184 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1185 {
1186         struct nfs4_opendata *opendata;
1187         int ret;
1188
1189         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1190         if (IS_ERR(opendata))
1191                 return PTR_ERR(opendata);
1192         opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1193         memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1194                         sizeof(opendata->o_arg.u.delegation.data));
1195         ret = nfs4_open_recover(opendata, state);
1196         nfs4_opendata_put(opendata);
1197         return ret;
1198 }
1199
1200 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1201 {
1202         struct nfs4_exception exception = { };
1203         struct nfs_server *server = NFS_SERVER(state->inode);
1204         int err;
1205         do {
1206                 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1207                 switch (err) {
1208                         case 0:
1209                         case -ENOENT:
1210                         case -ESTALE:
1211                                 goto out;
1212                         case -NFS4ERR_BADSESSION:
1213                         case -NFS4ERR_BADSLOT:
1214                         case -NFS4ERR_BAD_HIGH_SLOT:
1215                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1216                         case -NFS4ERR_DEADSESSION:
1217                                 nfs4_schedule_state_recovery(
1218                                         server->nfs_client);
1219                                 goto out;
1220                         case -NFS4ERR_STALE_CLIENTID:
1221                         case -NFS4ERR_STALE_STATEID:
1222                         case -NFS4ERR_EXPIRED:
1223                                 /* Don't recall a delegation if it was lost */
1224                                 nfs4_schedule_state_recovery(server->nfs_client);
1225                                 goto out;
1226                         case -ERESTARTSYS:
1227                                 /*
1228                                  * The show must go on: exit, but mark the
1229                                  * stateid as needing recovery.
1230                                  */
1231                         case -NFS4ERR_ADMIN_REVOKED:
1232                         case -NFS4ERR_BAD_STATEID:
1233                                 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1234                         case -ENOMEM:
1235                                 err = 0;
1236                                 goto out;
1237                 }
1238                 err = nfs4_handle_exception(server, err, &exception);
1239         } while (exception.retry);
1240 out:
1241         return err;
1242 }
1243
1244 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1245 {
1246         struct nfs4_opendata *data = calldata;
1247
1248         data->rpc_status = task->tk_status;
1249         if (RPC_ASSASSINATED(task))
1250                 return;
1251         if (data->rpc_status == 0) {
1252                 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1253                                 sizeof(data->o_res.stateid.data));
1254                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1255                 renew_lease(data->o_res.server, data->timestamp);
1256                 data->rpc_done = 1;
1257         }
1258 }
1259
1260 static void nfs4_open_confirm_release(void *calldata)
1261 {
1262         struct nfs4_opendata *data = calldata;
1263         struct nfs4_state *state = NULL;
1264
1265         /* If this request hasn't been cancelled, do nothing */
1266         if (data->cancelled == 0)
1267                 goto out_free;
1268         /* In case of error, no cleanup! */
1269         if (!data->rpc_done)
1270                 goto out_free;
1271         state = nfs4_opendata_to_nfs4_state(data);
1272         if (!IS_ERR(state))
1273                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1274 out_free:
1275         nfs4_opendata_put(data);
1276 }
1277
1278 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1279         .rpc_call_done = nfs4_open_confirm_done,
1280         .rpc_release = nfs4_open_confirm_release,
1281 };
1282
1283 /*
1284  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1285  */
1286 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1287 {
1288         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1289         struct rpc_task *task;
1290         struct  rpc_message msg = {
1291                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1292                 .rpc_argp = &data->c_arg,
1293                 .rpc_resp = &data->c_res,
1294                 .rpc_cred = data->owner->so_cred,
1295         };
1296         struct rpc_task_setup task_setup_data = {
1297                 .rpc_client = server->client,
1298                 .rpc_message = &msg,
1299                 .callback_ops = &nfs4_open_confirm_ops,
1300                 .callback_data = data,
1301                 .workqueue = nfsiod_workqueue,
1302                 .flags = RPC_TASK_ASYNC,
1303         };
1304         int status;
1305
1306         kref_get(&data->kref);
1307         data->rpc_done = 0;
1308         data->rpc_status = 0;
1309         data->timestamp = jiffies;
1310         task = rpc_run_task(&task_setup_data);
1311         if (IS_ERR(task))
1312                 return PTR_ERR(task);
1313         status = nfs4_wait_for_completion_rpc_task(task);
1314         if (status != 0) {
1315                 data->cancelled = 1;
1316                 smp_wmb();
1317         } else
1318                 status = data->rpc_status;
1319         rpc_put_task(task);
1320         return status;
1321 }
1322
1323 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1324 {
1325         struct nfs4_opendata *data = calldata;
1326         struct nfs4_state_owner *sp = data->owner;
1327
1328         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1329                 return;
1330         /*
1331          * Check if we still need to send an OPEN call, or if we can use
1332          * a delegation instead.
1333          */
1334         if (data->state != NULL) {
1335                 struct nfs_delegation *delegation;
1336
1337                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1338                         goto out_no_action;
1339                 rcu_read_lock();
1340                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1341                 if (delegation != NULL &&
1342                     test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1343                         rcu_read_unlock();
1344                         goto out_no_action;
1345                 }
1346                 rcu_read_unlock();
1347         }
1348         /* Update sequence id. */
1349         data->o_arg.id = sp->so_owner_id.id;
1350         data->o_arg.clientid = sp->so_client->cl_clientid;
1351         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1352                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1353                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1354         }
1355         data->timestamp = jiffies;
1356         if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1357                                 &data->o_arg.seq_args,
1358                                 &data->o_res.seq_res, 1, task))
1359                 return;
1360         rpc_call_start(task);
1361         return;
1362 out_no_action:
1363         task->tk_action = NULL;
1364
1365 }
1366
1367 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1368 {
1369         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1370         nfs4_open_prepare(task, calldata);
1371 }
1372
1373 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1374 {
1375         struct nfs4_opendata *data = calldata;
1376
1377         data->rpc_status = task->tk_status;
1378
1379         nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1380                         task->tk_status);
1381
1382         if (RPC_ASSASSINATED(task))
1383                 return;
1384         if (task->tk_status == 0) {
1385                 switch (data->o_res.f_attr->mode & S_IFMT) {
1386                         case S_IFREG:
1387                                 break;
1388                         case S_IFLNK:
1389                                 data->rpc_status = -ELOOP;
1390                                 break;
1391                         case S_IFDIR:
1392                                 data->rpc_status = -EISDIR;
1393                                 break;
1394                         default:
1395                                 data->rpc_status = -ENOTDIR;
1396                 }
1397                 renew_lease(data->o_res.server, data->timestamp);
1398                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1399                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1400         }
1401         data->rpc_done = 1;
1402 }
1403
1404 static void nfs4_open_release(void *calldata)
1405 {
1406         struct nfs4_opendata *data = calldata;
1407         struct nfs4_state *state = NULL;
1408
1409         /* If this request hasn't been cancelled, do nothing */
1410         if (data->cancelled == 0)
1411                 goto out_free;
1412         /* In case of error, no cleanup! */
1413         if (data->rpc_status != 0 || !data->rpc_done)
1414                 goto out_free;
1415         /* In case we need an open_confirm, no cleanup! */
1416         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1417                 goto out_free;
1418         state = nfs4_opendata_to_nfs4_state(data);
1419         if (!IS_ERR(state))
1420                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1421 out_free:
1422         nfs4_opendata_put(data);
1423 }
1424
1425 static const struct rpc_call_ops nfs4_open_ops = {
1426         .rpc_call_prepare = nfs4_open_prepare,
1427         .rpc_call_done = nfs4_open_done,
1428         .rpc_release = nfs4_open_release,
1429 };
1430
1431 static const struct rpc_call_ops nfs4_recover_open_ops = {
1432         .rpc_call_prepare = nfs4_recover_open_prepare,
1433         .rpc_call_done = nfs4_open_done,
1434         .rpc_release = nfs4_open_release,
1435 };
1436
1437 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1438 {
1439         struct inode *dir = data->dir->d_inode;
1440         struct nfs_server *server = NFS_SERVER(dir);
1441         struct nfs_openargs *o_arg = &data->o_arg;
1442         struct nfs_openres *o_res = &data->o_res;
1443         struct rpc_task *task;
1444         struct rpc_message msg = {
1445                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1446                 .rpc_argp = o_arg,
1447                 .rpc_resp = o_res,
1448                 .rpc_cred = data->owner->so_cred,
1449         };
1450         struct rpc_task_setup task_setup_data = {
1451                 .rpc_client = server->client,
1452                 .rpc_message = &msg,
1453                 .callback_ops = &nfs4_open_ops,
1454                 .callback_data = data,
1455                 .workqueue = nfsiod_workqueue,
1456                 .flags = RPC_TASK_ASYNC,
1457         };
1458         int status;
1459
1460         kref_get(&data->kref);
1461         data->rpc_done = 0;
1462         data->rpc_status = 0;
1463         data->cancelled = 0;
1464         if (isrecover)
1465                 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1466         task = rpc_run_task(&task_setup_data);
1467         if (IS_ERR(task))
1468                 return PTR_ERR(task);
1469         status = nfs4_wait_for_completion_rpc_task(task);
1470         if (status != 0) {
1471                 data->cancelled = 1;
1472                 smp_wmb();
1473         } else
1474                 status = data->rpc_status;
1475         rpc_put_task(task);
1476
1477         return status;
1478 }
1479
1480 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1481 {
1482         struct inode *dir = data->dir->d_inode;
1483         struct nfs_openres *o_res = &data->o_res;
1484         int status;
1485
1486         status = nfs4_run_open_task(data, 1);
1487         if (status != 0 || !data->rpc_done)
1488                 return status;
1489
1490         nfs_refresh_inode(dir, o_res->dir_attr);
1491
1492         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1493                 status = _nfs4_proc_open_confirm(data);
1494                 if (status != 0)
1495                         return status;
1496         }
1497
1498         return status;
1499 }
1500
1501 /*
1502  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1503  */
1504 static int _nfs4_proc_open(struct nfs4_opendata *data)
1505 {
1506         struct inode *dir = data->dir->d_inode;
1507         struct nfs_server *server = NFS_SERVER(dir);
1508         struct nfs_openargs *o_arg = &data->o_arg;
1509         struct nfs_openres *o_res = &data->o_res;
1510         int status;
1511
1512         status = nfs4_run_open_task(data, 0);
1513         if (status != 0 || !data->rpc_done)
1514                 return status;
1515
1516         if (o_arg->open_flags & O_CREAT) {
1517                 update_changeattr(dir, &o_res->cinfo);
1518                 nfs_post_op_update_inode(dir, o_res->dir_attr);
1519         } else
1520                 nfs_refresh_inode(dir, o_res->dir_attr);
1521         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1522                 status = _nfs4_proc_open_confirm(data);
1523                 if (status != 0)
1524                         return status;
1525         }
1526         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1527                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1528         return 0;
1529 }
1530
1531 static int nfs4_recover_expired_lease(struct nfs_server *server)
1532 {
1533         struct nfs_client *clp = server->nfs_client;
1534         unsigned int loop;
1535         int ret;
1536
1537         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1538                 ret = nfs4_wait_clnt_recover(clp);
1539                 if (ret != 0)
1540                         break;
1541                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1542                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1543                         break;
1544                 nfs4_schedule_state_recovery(clp);
1545                 ret = -EIO;
1546         }
1547         return ret;
1548 }
1549
1550 /*
1551  * OPEN_EXPIRED:
1552  *      reclaim state on the server after a network partition.
1553  *      Assumes caller holds the appropriate lock
1554  */
1555 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1556 {
1557         struct nfs4_opendata *opendata;
1558         int ret;
1559
1560         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1561         if (IS_ERR(opendata))
1562                 return PTR_ERR(opendata);
1563         ret = nfs4_open_recover(opendata, state);
1564         if (ret == -ESTALE)
1565                 d_drop(ctx->path.dentry);
1566         nfs4_opendata_put(opendata);
1567         return ret;
1568 }
1569
1570 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1571 {
1572         struct nfs_server *server = NFS_SERVER(state->inode);
1573         struct nfs4_exception exception = { };
1574         int err;
1575
1576         do {
1577                 err = _nfs4_open_expired(ctx, state);
1578                 switch (err) {
1579                 default:
1580                         goto out;
1581                 case -NFS4ERR_GRACE:
1582                 case -NFS4ERR_DELAY:
1583                         nfs4_handle_exception(server, err, &exception);
1584                         err = 0;
1585                 }
1586         } while (exception.retry);
1587 out:
1588         return err;
1589 }
1590
1591 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1592 {
1593         struct nfs_open_context *ctx;
1594         int ret;
1595
1596         ctx = nfs4_state_find_open_context(state);
1597         if (IS_ERR(ctx))
1598                 return PTR_ERR(ctx);
1599         ret = nfs4_do_open_expired(ctx, state);
1600         put_nfs_open_context(ctx);
1601         return ret;
1602 }
1603
1604 /*
1605  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1606  * fields corresponding to attributes that were used to store the verifier.
1607  * Make sure we clobber those fields in the later setattr call
1608  */
1609 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1610 {
1611         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1612             !(sattr->ia_valid & ATTR_ATIME_SET))
1613                 sattr->ia_valid |= ATTR_ATIME;
1614
1615         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1616             !(sattr->ia_valid & ATTR_MTIME_SET))
1617                 sattr->ia_valid |= ATTR_MTIME;
1618 }
1619
1620 /*
1621  * Returns a referenced nfs4_state
1622  */
1623 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)
1624 {
1625         struct nfs4_state_owner  *sp;
1626         struct nfs4_state     *state = NULL;
1627         struct nfs_server       *server = NFS_SERVER(dir);
1628         struct nfs4_opendata *opendata;
1629         int status;
1630
1631         /* Protect against reboot recovery conflicts */
1632         status = -ENOMEM;
1633         if (!(sp = nfs4_get_state_owner(server, cred))) {
1634                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1635                 goto out_err;
1636         }
1637         status = nfs4_recover_expired_lease(server);
1638         if (status != 0)
1639                 goto err_put_state_owner;
1640         if (path->dentry->d_inode != NULL)
1641                 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1642         status = -ENOMEM;
1643         opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1644         if (opendata == NULL)
1645                 goto err_put_state_owner;
1646
1647         if (path->dentry->d_inode != NULL)
1648                 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1649
1650         status = _nfs4_proc_open(opendata);
1651         if (status != 0)
1652                 goto err_opendata_put;
1653
1654         if (opendata->o_arg.open_flags & O_EXCL)
1655                 nfs4_exclusive_attrset(opendata, sattr);
1656
1657         state = nfs4_opendata_to_nfs4_state(opendata);
1658         status = PTR_ERR(state);
1659         if (IS_ERR(state))
1660                 goto err_opendata_put;
1661         nfs4_opendata_put(opendata);
1662         nfs4_put_state_owner(sp);
1663         *res = state;
1664         return 0;
1665 err_opendata_put:
1666         nfs4_opendata_put(opendata);
1667 err_put_state_owner:
1668         nfs4_put_state_owner(sp);
1669 out_err:
1670         *res = NULL;
1671         return status;
1672 }
1673
1674
1675 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)
1676 {
1677         struct nfs4_exception exception = { };
1678         struct nfs4_state *res;
1679         int status;
1680
1681         do {
1682                 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1683                 if (status == 0)
1684                         break;
1685                 /* NOTE: BAD_SEQID means the server and client disagree about the
1686                  * book-keeping w.r.t. state-changing operations
1687                  * (OPEN/CLOSE/LOCK/LOCKU...)
1688                  * It is actually a sign of a bug on the client or on the server.
1689                  *
1690                  * If we receive a BAD_SEQID error in the particular case of
1691                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
1692                  * have unhashed the old state_owner for us, and that we can
1693                  * therefore safely retry using a new one. We should still warn
1694                  * the user though...
1695                  */
1696                 if (status == -NFS4ERR_BAD_SEQID) {
1697                         printk(KERN_WARNING "NFS: v4 server %s "
1698                                         " returned a bad sequence-id error!\n",
1699                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
1700                         exception.retry = 1;
1701                         continue;
1702                 }
1703                 /*
1704                  * BAD_STATEID on OPEN means that the server cancelled our
1705                  * state before it received the OPEN_CONFIRM.
1706                  * Recover by retrying the request as per the discussion
1707                  * on Page 181 of RFC3530.
1708                  */
1709                 if (status == -NFS4ERR_BAD_STATEID) {
1710                         exception.retry = 1;
1711                         continue;
1712                 }
1713                 if (status == -EAGAIN) {
1714                         /* We must have found a delegation */
1715                         exception.retry = 1;
1716                         continue;
1717                 }
1718                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1719                                         status, &exception));
1720         } while (exception.retry);
1721         return res;
1722 }
1723
1724 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1725                             struct nfs_fattr *fattr, struct iattr *sattr,
1726                             struct nfs4_state *state)
1727 {
1728         struct nfs_server *server = NFS_SERVER(inode);
1729         struct nfs_setattrargs  arg = {
1730                 .fh             = NFS_FH(inode),
1731                 .iap            = sattr,
1732                 .server         = server,
1733                 .bitmask = server->attr_bitmask,
1734         };
1735         struct nfs_setattrres  res = {
1736                 .fattr          = fattr,
1737                 .server         = server,
1738         };
1739         struct rpc_message msg = {
1740                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1741                 .rpc_argp       = &arg,
1742                 .rpc_resp       = &res,
1743                 .rpc_cred       = cred,
1744         };
1745         unsigned long timestamp = jiffies;
1746         int status;
1747
1748         nfs_fattr_init(fattr);
1749
1750         if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1751                 /* Use that stateid */
1752         } else if (state != NULL) {
1753                 nfs4_copy_stateid(&arg.stateid, state, current->files);
1754         } else
1755                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1756
1757         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1758         if (status == 0 && state != NULL)
1759                 renew_lease(server, timestamp);
1760         return status;
1761 }
1762
1763 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1764                            struct nfs_fattr *fattr, struct iattr *sattr,
1765                            struct nfs4_state *state)
1766 {
1767         struct nfs_server *server = NFS_SERVER(inode);
1768         struct nfs4_exception exception = { };
1769         int err;
1770         do {
1771                 err = nfs4_handle_exception(server,
1772                                 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1773                                 &exception);
1774         } while (exception.retry);
1775         return err;
1776 }
1777
1778 struct nfs4_closedata {
1779         struct path path;
1780         struct inode *inode;
1781         struct nfs4_state *state;
1782         struct nfs_closeargs arg;
1783         struct nfs_closeres res;
1784         struct nfs_fattr fattr;
1785         unsigned long timestamp;
1786 };
1787
1788 static void nfs4_free_closedata(void *data)
1789 {
1790         struct nfs4_closedata *calldata = data;
1791         struct nfs4_state_owner *sp = calldata->state->owner;
1792
1793         nfs4_put_open_state(calldata->state);
1794         nfs_free_seqid(calldata->arg.seqid);
1795         nfs4_put_state_owner(sp);
1796         path_put(&calldata->path);
1797         kfree(calldata);
1798 }
1799
1800 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1801                 fmode_t fmode)
1802 {
1803         spin_lock(&state->owner->so_lock);
1804         if (!(fmode & FMODE_READ))
1805                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1806         if (!(fmode & FMODE_WRITE))
1807                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1808         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1809         spin_unlock(&state->owner->so_lock);
1810 }
1811
1812 static void nfs4_close_done(struct rpc_task *task, void *data)
1813 {
1814         struct nfs4_closedata *calldata = data;
1815         struct nfs4_state *state = calldata->state;
1816         struct nfs_server *server = NFS_SERVER(calldata->inode);
1817
1818         nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1819         if (RPC_ASSASSINATED(task))
1820                 return;
1821         /* hmm. we are done with the inode, and in the process of freeing
1822          * the state_owner. we keep this around to process errors
1823          */
1824         switch (task->tk_status) {
1825                 case 0:
1826                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1827                         renew_lease(server, calldata->timestamp);
1828                         nfs4_close_clear_stateid_flags(state,
1829                                         calldata->arg.fmode);
1830                         break;
1831                 case -NFS4ERR_STALE_STATEID:
1832                 case -NFS4ERR_OLD_STATEID:
1833                 case -NFS4ERR_BAD_STATEID:
1834                 case -NFS4ERR_EXPIRED:
1835                         if (calldata->arg.fmode == 0)
1836                                 break;
1837                 default:
1838                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1839                                 nfs_restart_rpc(task, server->nfs_client);
1840                                 return;
1841                         }
1842         }
1843         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1844 }
1845
1846 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1847 {
1848         struct nfs4_closedata *calldata = data;
1849         struct nfs4_state *state = calldata->state;
1850         int call_close = 0;
1851
1852         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1853                 return;
1854
1855         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1856         calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1857         spin_lock(&state->owner->so_lock);
1858         /* Calculate the change in open mode */
1859         if (state->n_rdwr == 0) {
1860                 if (state->n_rdonly == 0) {
1861                         call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1862                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1863                         calldata->arg.fmode &= ~FMODE_READ;
1864                 }
1865                 if (state->n_wronly == 0) {
1866                         call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1867                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1868                         calldata->arg.fmode &= ~FMODE_WRITE;
1869                 }
1870         }
1871         spin_unlock(&state->owner->so_lock);
1872
1873         if (!call_close) {
1874                 /* Note: exit _without_ calling nfs4_close_done */
1875                 task->tk_action = NULL;
1876                 return;
1877         }
1878
1879         if (calldata->arg.fmode == 0)
1880                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1881
1882         nfs_fattr_init(calldata->res.fattr);
1883         calldata->timestamp = jiffies;
1884         if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1885                                 &calldata->arg.seq_args, &calldata->res.seq_res,
1886                                 1, task))
1887                 return;
1888         rpc_call_start(task);
1889 }
1890
1891 static const struct rpc_call_ops nfs4_close_ops = {
1892         .rpc_call_prepare = nfs4_close_prepare,
1893         .rpc_call_done = nfs4_close_done,
1894         .rpc_release = nfs4_free_closedata,
1895 };
1896
1897 /* 
1898  * It is possible for data to be read/written from a mem-mapped file 
1899  * after the sys_close call (which hits the vfs layer as a flush).
1900  * This means that we can't safely call nfsv4 close on a file until 
1901  * the inode is cleared. This in turn means that we are not good
1902  * NFSv4 citizens - we do not indicate to the server to update the file's 
1903  * share state even when we are done with one of the three share 
1904  * stateid's in the inode.
1905  *
1906  * NOTE: Caller must be holding the sp->so_owner semaphore!
1907  */
1908 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1909 {
1910         struct nfs_server *server = NFS_SERVER(state->inode);
1911         struct nfs4_closedata *calldata;
1912         struct nfs4_state_owner *sp = state->owner;
1913         struct rpc_task *task;
1914         struct rpc_message msg = {
1915                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1916                 .rpc_cred = state->owner->so_cred,
1917         };
1918         struct rpc_task_setup task_setup_data = {
1919                 .rpc_client = server->client,
1920                 .rpc_message = &msg,
1921                 .callback_ops = &nfs4_close_ops,
1922                 .workqueue = nfsiod_workqueue,
1923                 .flags = RPC_TASK_ASYNC,
1924         };
1925         int status = -ENOMEM;
1926
1927         calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1928         if (calldata == NULL)
1929                 goto out;
1930         calldata->inode = state->inode;
1931         calldata->state = state;
1932         calldata->arg.fh = NFS_FH(state->inode);
1933         calldata->arg.stateid = &state->open_stateid;
1934         /* Serialization for the sequence id */
1935         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1936         if (calldata->arg.seqid == NULL)
1937                 goto out_free_calldata;
1938         calldata->arg.fmode = 0;
1939         calldata->arg.bitmask = server->cache_consistency_bitmask;
1940         calldata->res.fattr = &calldata->fattr;
1941         calldata->res.seqid = calldata->arg.seqid;
1942         calldata->res.server = server;
1943         calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1944         calldata->path.mnt = mntget(path->mnt);
1945         calldata->path.dentry = dget(path->dentry);
1946
1947         msg.rpc_argp = &calldata->arg,
1948         msg.rpc_resp = &calldata->res,
1949         task_setup_data.callback_data = calldata;
1950         task = rpc_run_task(&task_setup_data);
1951         if (IS_ERR(task))
1952                 return PTR_ERR(task);
1953         status = 0;
1954         if (wait)
1955                 status = rpc_wait_for_completion_task(task);
1956         rpc_put_task(task);
1957         return status;
1958 out_free_calldata:
1959         kfree(calldata);
1960 out:
1961         nfs4_put_open_state(state);
1962         nfs4_put_state_owner(sp);
1963         return status;
1964 }
1965
1966 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1967 {
1968         struct file *filp;
1969         int ret;
1970
1971         /* If the open_intent is for execute, we have an extra check to make */
1972         if (fmode & FMODE_EXEC) {
1973                 ret = nfs_may_open(state->inode,
1974                                 state->owner->so_cred,
1975                                 nd->intent.open.flags);
1976                 if (ret < 0)
1977                         goto out_close;
1978         }
1979         filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1980         if (!IS_ERR(filp)) {
1981                 struct nfs_open_context *ctx;
1982                 ctx = nfs_file_open_context(filp);
1983                 ctx->state = state;
1984                 return 0;
1985         }
1986         ret = PTR_ERR(filp);
1987 out_close:
1988         nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1989         return ret;
1990 }
1991
1992 struct dentry *
1993 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1994 {
1995         struct path path = {
1996                 .mnt = nd->path.mnt,
1997                 .dentry = dentry,
1998         };
1999         struct dentry *parent;
2000         struct iattr attr;
2001         struct rpc_cred *cred;
2002         struct nfs4_state *state;
2003         struct dentry *res;
2004         fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
2005
2006         if (nd->flags & LOOKUP_CREATE) {
2007                 attr.ia_mode = nd->intent.open.create_mode;
2008                 attr.ia_valid = ATTR_MODE;
2009                 if (!IS_POSIXACL(dir))
2010                         attr.ia_mode &= ~current_umask();
2011         } else {
2012                 attr.ia_valid = 0;
2013                 BUG_ON(nd->intent.open.flags & O_CREAT);
2014         }
2015
2016         cred = rpc_lookup_cred();
2017         if (IS_ERR(cred))
2018                 return (struct dentry *)cred;
2019         parent = dentry->d_parent;
2020         /* Protect against concurrent sillydeletes */
2021         nfs_block_sillyrename(parent);
2022         state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
2023         put_rpccred(cred);
2024         if (IS_ERR(state)) {
2025                 if (PTR_ERR(state) == -ENOENT) {
2026                         d_add(dentry, NULL);
2027                         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2028                 }
2029                 nfs_unblock_sillyrename(parent);
2030                 return (struct dentry *)state;
2031         }
2032         res = d_add_unique(dentry, igrab(state->inode));
2033         if (res != NULL)
2034                 path.dentry = res;
2035         nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
2036         nfs_unblock_sillyrename(parent);
2037         nfs4_intent_set_file(nd, &path, state, fmode);
2038         return res;
2039 }
2040
2041 int
2042 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
2043 {
2044         struct path path = {
2045                 .mnt = nd->path.mnt,
2046                 .dentry = dentry,
2047         };
2048         struct rpc_cred *cred;
2049         struct nfs4_state *state;
2050         fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
2051
2052         cred = rpc_lookup_cred();
2053         if (IS_ERR(cred))
2054                 return PTR_ERR(cred);
2055         state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
2056         put_rpccred(cred);
2057         if (IS_ERR(state)) {
2058                 switch (PTR_ERR(state)) {
2059                         case -EPERM:
2060                         case -EACCES:
2061                         case -EDQUOT:
2062                         case -ENOSPC:
2063                         case -EROFS:
2064                                 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
2065                                 return 1;
2066                         default:
2067                                 goto out_drop;
2068                 }
2069         }
2070         if (state->inode == dentry->d_inode) {
2071                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2072                 nfs4_intent_set_file(nd, &path, state, fmode);
2073                 return 1;
2074         }
2075         nfs4_close_sync(&path, state, fmode);
2076 out_drop:
2077         d_drop(dentry);
2078         return 0;
2079 }
2080
2081 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2082 {
2083         if (ctx->state == NULL)
2084                 return;
2085         if (is_sync)
2086                 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2087         else
2088                 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2089 }
2090
2091 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2092 {
2093         struct nfs4_server_caps_arg args = {
2094                 .fhandle = fhandle,
2095         };
2096         struct nfs4_server_caps_res res = {};
2097         struct rpc_message msg = {
2098                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2099                 .rpc_argp = &args,
2100                 .rpc_resp = &res,
2101         };
2102         int status;
2103
2104         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2105         if (status == 0) {
2106                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2107                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2108                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2109                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2110                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2111                                 NFS_CAP_CTIME|NFS_CAP_MTIME);
2112                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2113                         server->caps |= NFS_CAP_ACLS;
2114                 if (res.has_links != 0)
2115                         server->caps |= NFS_CAP_HARDLINKS;
2116                 if (res.has_symlinks != 0)
2117                         server->caps |= NFS_CAP_SYMLINKS;
2118                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2119                         server->caps |= NFS_CAP_FILEID;
2120                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2121                         server->caps |= NFS_CAP_MODE;
2122                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2123                         server->caps |= NFS_CAP_NLINK;
2124                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2125                         server->caps |= NFS_CAP_OWNER;
2126                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2127                         server->caps |= NFS_CAP_OWNER_GROUP;
2128                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2129                         server->caps |= NFS_CAP_ATIME;
2130                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2131                         server->caps |= NFS_CAP_CTIME;
2132                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2133                         server->caps |= NFS_CAP_MTIME;
2134
2135                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2136                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2137                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2138                 server->acl_bitmask = res.acl_bitmask;
2139         }
2140
2141         return status;
2142 }
2143
2144 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2145 {
2146         struct nfs4_exception exception = { };
2147         int err;
2148         do {
2149                 err = nfs4_handle_exception(server,
2150                                 _nfs4_server_capabilities(server, fhandle),
2151                                 &exception);
2152         } while (exception.retry);
2153         return err;
2154 }
2155
2156 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2157                 struct nfs_fsinfo *info)
2158 {
2159         struct nfs4_lookup_root_arg args = {
2160                 .bitmask = nfs4_fattr_bitmap,
2161         };
2162         struct nfs4_lookup_res res = {
2163                 .server = server,
2164                 .fattr = info->fattr,
2165                 .fh = fhandle,
2166         };
2167         struct rpc_message msg = {
2168                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2169                 .rpc_argp = &args,
2170                 .rpc_resp = &res,
2171         };
2172
2173         nfs_fattr_init(info->fattr);
2174         return nfs4_call_sync(server, &msg, &args, &res, 0);
2175 }
2176
2177 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2178                 struct nfs_fsinfo *info)
2179 {
2180         struct nfs4_exception exception = { };
2181         int err;
2182         do {
2183                 err = nfs4_handle_exception(server,
2184                                 _nfs4_lookup_root(server, fhandle, info),
2185                                 &exception);
2186         } while (exception.retry);
2187         return err;
2188 }
2189
2190 /*
2191  * get the file handle for the "/" directory on the server
2192  */
2193 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2194                               struct nfs_fsinfo *info)
2195 {
2196         int status;
2197
2198         status = nfs4_lookup_root(server, fhandle, info);
2199         if (status == 0)
2200                 status = nfs4_server_capabilities(server, fhandle);
2201         if (status == 0)
2202                 status = nfs4_do_fsinfo(server, fhandle, info);
2203         return nfs4_map_errors(status);
2204 }
2205
2206 /*
2207  * Get locations and (maybe) other attributes of a referral.
2208  * Note that we'll actually follow the referral later when
2209  * we detect fsid mismatch in inode revalidation
2210  */
2211 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2212 {
2213         int status = -ENOMEM;
2214         struct page *page = NULL;
2215         struct nfs4_fs_locations *locations = NULL;
2216
2217         page = alloc_page(GFP_KERNEL);
2218         if (page == NULL)
2219                 goto out;
2220         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2221         if (locations == NULL)
2222                 goto out;
2223
2224         status = nfs4_proc_fs_locations(dir, name, locations, page);
2225         if (status != 0)
2226                 goto out;
2227         /* Make sure server returned a different fsid for the referral */
2228         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2229                 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2230                 status = -EIO;
2231                 goto out;
2232         }
2233
2234         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2235         fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2236         if (!fattr->mode)
2237                 fattr->mode = S_IFDIR;
2238         memset(fhandle, 0, sizeof(struct nfs_fh));
2239 out:
2240         if (page)
2241                 __free_page(page);
2242         if (locations)
2243                 kfree(locations);
2244         return status;
2245 }
2246
2247 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2248 {
2249         struct nfs4_getattr_arg args = {
2250                 .fh = fhandle,
2251                 .bitmask = server->attr_bitmask,
2252         };
2253         struct nfs4_getattr_res res = {
2254                 .fattr = fattr,
2255                 .server = server,
2256         };
2257         struct rpc_message msg = {
2258                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2259                 .rpc_argp = &args,
2260                 .rpc_resp = &res,
2261         };
2262         
2263         nfs_fattr_init(fattr);
2264         return nfs4_call_sync(server, &msg, &args, &res, 0);
2265 }
2266
2267 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2268 {
2269         struct nfs4_exception exception = { };
2270         int err;
2271         do {
2272                 err = nfs4_handle_exception(server,
2273                                 _nfs4_proc_getattr(server, fhandle, fattr),
2274                                 &exception);
2275         } while (exception.retry);
2276         return err;
2277 }
2278
2279 /* 
2280  * The file is not closed if it is opened due to the a request to change
2281  * the size of the file. The open call will not be needed once the
2282  * VFS layer lookup-intents are implemented.
2283  *
2284  * Close is called when the inode is destroyed.
2285  * If we haven't opened the file for O_WRONLY, we
2286  * need to in the size_change case to obtain a stateid.
2287  *
2288  * Got race?
2289  * Because OPEN is always done by name in nfsv4, it is
2290  * possible that we opened a different file by the same
2291  * name.  We can recognize this race condition, but we
2292  * can't do anything about it besides returning an error.
2293  *
2294  * This will be fixed with VFS changes (lookup-intent).
2295  */
2296 static int
2297 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2298                   struct iattr *sattr)
2299 {
2300         struct inode *inode = dentry->d_inode;
2301         struct rpc_cred *cred = NULL;
2302         struct nfs4_state *state = NULL;
2303         int status;
2304
2305         nfs_fattr_init(fattr);
2306         
2307         /* Search for an existing open(O_WRITE) file */
2308         if (sattr->ia_valid & ATTR_FILE) {
2309                 struct nfs_open_context *ctx;
2310
2311                 ctx = nfs_file_open_context(sattr->ia_file);
2312                 if (ctx) {
2313                         cred = ctx->cred;
2314                         state = ctx->state;
2315                 }
2316         }
2317
2318         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2319         if (status == 0)
2320                 nfs_setattr_update_inode(inode, sattr);
2321         return status;
2322 }
2323
2324 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2325                 const struct qstr *name, struct nfs_fh *fhandle,
2326                 struct nfs_fattr *fattr)
2327 {
2328         int                    status;
2329         struct nfs4_lookup_arg args = {
2330                 .bitmask = server->attr_bitmask,
2331                 .dir_fh = dirfh,
2332                 .name = name,
2333         };
2334         struct nfs4_lookup_res res = {
2335                 .server = server,
2336                 .fattr = fattr,
2337                 .fh = fhandle,
2338         };
2339         struct rpc_message msg = {
2340                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2341                 .rpc_argp = &args,
2342                 .rpc_resp = &res,
2343         };
2344
2345         nfs_fattr_init(fattr);
2346
2347         dprintk("NFS call  lookupfh %s\n", name->name);
2348         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2349         dprintk("NFS reply lookupfh: %d\n", status);
2350         return status;
2351 }
2352
2353 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2354                               struct qstr *name, struct nfs_fh *fhandle,
2355                               struct nfs_fattr *fattr)
2356 {
2357         struct nfs4_exception exception = { };
2358         int err;
2359         do {
2360                 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2361                 /* FIXME: !!!! */
2362                 if (err == -NFS4ERR_MOVED) {
2363                         err = -EREMOTE;
2364                         break;
2365                 }
2366                 err = nfs4_handle_exception(server, err, &exception);
2367         } while (exception.retry);
2368         return err;
2369 }
2370
2371 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2372                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2373 {
2374         int status;
2375         
2376         dprintk("NFS call  lookup %s\n", name->name);
2377         status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2378         if (status == -NFS4ERR_MOVED)
2379                 status = nfs4_get_referral(dir, name, fattr, fhandle);
2380         dprintk("NFS reply lookup: %d\n", status);
2381         return status;
2382 }
2383
2384 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2385 {
2386         struct nfs4_exception exception = { };
2387         int err;
2388         do {
2389                 err = nfs4_handle_exception(NFS_SERVER(dir),
2390                                 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2391                                 &exception);
2392         } while (exception.retry);
2393         return err;
2394 }
2395
2396 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2397 {
2398         struct nfs_server *server = NFS_SERVER(inode);
2399         struct nfs_fattr fattr;
2400         struct nfs4_accessargs args = {
2401                 .fh = NFS_FH(inode),
2402                 .bitmask = server->attr_bitmask,
2403         };
2404         struct nfs4_accessres res = {
2405                 .server = server,
2406                 .fattr = &fattr,
2407         };
2408         struct rpc_message msg = {
2409                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2410                 .rpc_argp = &args,
2411                 .rpc_resp = &res,
2412                 .rpc_cred = entry->cred,
2413         };
2414         int mode = entry->mask;
2415         int status;
2416
2417         /*
2418          * Determine which access bits we want to ask for...
2419          */
2420         if (mode & MAY_READ)
2421                 args.access |= NFS4_ACCESS_READ;
2422         if (S_ISDIR(inode->i_mode)) {
2423                 if (mode & MAY_WRITE)
2424                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2425                 if (mode & MAY_EXEC)
2426                         args.access |= NFS4_ACCESS_LOOKUP;
2427         } else {
2428                 if (mode & MAY_WRITE)
2429                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2430                 if (mode & MAY_EXEC)
2431                         args.access |= NFS4_ACCESS_EXECUTE;
2432         }
2433         nfs_fattr_init(&fattr);
2434         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2435         if (!status) {
2436                 entry->mask = 0;
2437                 if (res.access & NFS4_ACCESS_READ)
2438                         entry->mask |= MAY_READ;
2439                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2440                         entry->mask |= MAY_WRITE;
2441                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2442                         entry->mask |= MAY_EXEC;
2443                 nfs_refresh_inode(inode, &fattr);
2444         }
2445         return status;
2446 }
2447
2448 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2449 {
2450         struct nfs4_exception exception = { };
2451         int err;
2452         do {
2453                 err = nfs4_handle_exception(NFS_SERVER(inode),
2454                                 _nfs4_proc_access(inode, entry),
2455                                 &exception);
2456         } while (exception.retry);
2457         return err;
2458 }
2459
2460 /*
2461  * TODO: For the time being, we don't try to get any attributes
2462  * along with any of the zero-copy operations READ, READDIR,
2463  * READLINK, WRITE.
2464  *
2465  * In the case of the first three, we want to put the GETATTR
2466  * after the read-type operation -- this is because it is hard
2467  * to predict the length of a GETATTR response in v4, and thus
2468  * align the READ data correctly.  This means that the GETATTR
2469  * may end up partially falling into the page cache, and we should
2470  * shift it into the 'tail' of the xdr_buf before processing.
2471  * To do this efficiently, we need to know the total length
2472  * of data received, which doesn't seem to be available outside
2473  * of the RPC layer.
2474  *
2475  * In the case of WRITE, we also want to put the GETATTR after
2476  * the operation -- in this case because we want to make sure
2477  * we get the post-operation mtime and size.  This means that
2478  * we can't use xdr_encode_pages() as written: we need a variant
2479  * of it which would leave room in the 'tail' iovec.
2480  *
2481  * Both of these changes to the XDR layer would in fact be quite
2482  * minor, but I decided to leave them for a subsequent patch.
2483  */
2484 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2485                 unsigned int pgbase, unsigned int pglen)
2486 {
2487         struct nfs4_readlink args = {
2488                 .fh       = NFS_FH(inode),
2489                 .pgbase   = pgbase,
2490                 .pglen    = pglen,
2491                 .pages    = &page,
2492         };
2493         struct nfs4_readlink_res res;
2494         struct rpc_message msg = {
2495                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2496                 .rpc_argp = &args,
2497                 .rpc_resp = &res,
2498         };
2499
2500         return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2501 }
2502
2503 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2504                 unsigned int pgbase, unsigned int pglen)
2505 {
2506         struct nfs4_exception exception = { };
2507         int err;
2508         do {
2509                 err = nfs4_handle_exception(NFS_SERVER(inode),
2510                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2511                                 &exception);
2512         } while (exception.retry);
2513         return err;
2514 }
2515
2516 /*
2517  * Got race?
2518  * We will need to arrange for the VFS layer to provide an atomic open.
2519  * Until then, this create/open method is prone to inefficiency and race
2520  * conditions due to the lookup, create, and open VFS calls from sys_open()
2521  * placed on the wire.
2522  *
2523  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2524  * The file will be opened again in the subsequent VFS open call
2525  * (nfs4_proc_file_open).
2526  *
2527  * The open for read will just hang around to be used by any process that
2528  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2529  */
2530
2531 static int
2532 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2533                  int flags, struct nameidata *nd)
2534 {
2535         struct path path = {
2536                 .mnt = nd->path.mnt,
2537                 .dentry = dentry,
2538         };
2539         struct nfs4_state *state;
2540         struct rpc_cred *cred;
2541         fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2542         int status = 0;
2543
2544         cred = rpc_lookup_cred();
2545         if (IS_ERR(cred)) {
2546                 status = PTR_ERR(cred);
2547                 goto out;
2548         }
2549         state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2550         d_drop(dentry);
2551         if (IS_ERR(state)) {
2552                 status = PTR_ERR(state);
2553                 goto out_putcred;
2554         }
2555         d_add(dentry, igrab(state->inode));
2556         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2557         if (flags & O_EXCL) {
2558                 struct nfs_fattr fattr;
2559                 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2560                 if (status == 0)
2561                         nfs_setattr_update_inode(state->inode, sattr);
2562                 nfs_post_op_update_inode(state->inode, &fattr);
2563         }
2564         if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2565                 status = nfs4_intent_set_file(nd, &path, state, fmode);
2566         else
2567                 nfs4_close_sync(&path, state, fmode);
2568 out_putcred:
2569         put_rpccred(cred);
2570 out:
2571         return status;
2572 }
2573
2574 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2575 {
2576         struct nfs_server *server = NFS_SERVER(dir);
2577         struct nfs_removeargs args = {
2578                 .fh = NFS_FH(dir),
2579                 .name.len = name->len,
2580                 .name.name = name->name,
2581                 .bitmask = server->attr_bitmask,
2582         };
2583         struct nfs_removeres res = {
2584                 .server = server,
2585         };
2586         struct rpc_message msg = {
2587                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2588                 .rpc_argp = &args,
2589                 .rpc_resp = &res,
2590         };
2591         int                     status;
2592
2593         nfs_fattr_init(&res.dir_attr);
2594         status = nfs4_call_sync(server, &msg, &args, &res, 1);
2595         if (status == 0) {
2596                 update_changeattr(dir, &res.cinfo);
2597                 nfs_post_op_update_inode(dir, &res.dir_attr);
2598         }
2599         return status;
2600 }
2601
2602 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2603 {
2604         struct nfs4_exception exception = { };
2605         int err;
2606         do {
2607                 err = nfs4_handle_exception(NFS_SERVER(dir),
2608                                 _nfs4_proc_remove(dir, name),
2609                                 &exception);
2610         } while (exception.retry);
2611         return err;
2612 }
2613
2614 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2615 {
2616         struct nfs_server *server = NFS_SERVER(dir);
2617         struct nfs_removeargs *args = msg->rpc_argp;
2618         struct nfs_removeres *res = msg->rpc_resp;
2619
2620         args->bitmask = server->cache_consistency_bitmask;
2621         res->server = server;
2622         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2623 }
2624
2625 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2626 {
2627         struct nfs_removeres *res = task->tk_msg.rpc_resp;
2628
2629         nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2630         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2631                 return 0;
2632         update_changeattr(dir, &res->cinfo);
2633         nfs_post_op_update_inode(dir, &res->dir_attr);
2634         return 1;
2635 }
2636
2637 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2638                 struct inode *new_dir, struct qstr *new_name)
2639 {
2640         struct nfs_server *server = NFS_SERVER(old_dir);
2641         struct nfs4_rename_arg arg = {
2642                 .old_dir = NFS_FH(old_dir),
2643                 .new_dir = NFS_FH(new_dir),
2644                 .old_name = old_name,
2645                 .new_name = new_name,
2646                 .bitmask = server->attr_bitmask,
2647         };
2648         struct nfs_fattr old_fattr, new_fattr;
2649         struct nfs4_rename_res res = {
2650                 .server = server,
2651                 .old_fattr = &old_fattr,
2652                 .new_fattr = &new_fattr,
2653         };
2654         struct rpc_message msg = {
2655                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2656                 .rpc_argp = &arg,
2657                 .rpc_resp = &res,
2658         };
2659         int                     status;
2660         
2661         nfs_fattr_init(res.old_fattr);
2662         nfs_fattr_init(res.new_fattr);
2663         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2664
2665         if (!status) {
2666                 update_changeattr(old_dir, &res.old_cinfo);
2667                 nfs_post_op_update_inode(old_dir, res.old_fattr);
2668                 update_changeattr(new_dir, &res.new_cinfo);
2669                 nfs_post_op_update_inode(new_dir, res.new_fattr);
2670         }
2671         return status;
2672 }
2673
2674 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2675                 struct inode *new_dir, struct qstr *new_name)
2676 {
2677         struct nfs4_exception exception = { };
2678         int err;
2679         do {
2680                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2681                                 _nfs4_proc_rename(old_dir, old_name,
2682                                         new_dir, new_name),
2683                                 &exception);
2684         } while (exception.retry);
2685         return err;
2686 }
2687
2688 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2689 {
2690         struct nfs_server *server = NFS_SERVER(inode);
2691         struct nfs4_link_arg arg = {
2692                 .fh     = NFS_FH(inode),
2693                 .dir_fh = NFS_FH(dir),
2694                 .name   = name,
2695                 .bitmask = server->attr_bitmask,
2696         };
2697         struct nfs_fattr fattr, dir_attr;
2698         struct nfs4_link_res res = {
2699                 .server = server,
2700                 .fattr = &fattr,
2701                 .dir_attr = &dir_attr,
2702         };
2703         struct rpc_message msg = {
2704                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2705                 .rpc_argp = &arg,
2706                 .rpc_resp = &res,
2707         };
2708         int                     status;
2709
2710         nfs_fattr_init(res.fattr);
2711         nfs_fattr_init(res.dir_attr);
2712         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2713         if (!status) {
2714                 update_changeattr(dir, &res.cinfo);
2715                 nfs_post_op_update_inode(dir, res.dir_attr);
2716                 nfs_post_op_update_inode(inode, res.fattr);
2717         }
2718
2719         return status;
2720 }
2721
2722 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2723 {
2724         struct nfs4_exception exception = { };
2725         int err;
2726         do {
2727                 err = nfs4_handle_exception(NFS_SERVER(inode),
2728                                 _nfs4_proc_link(inode, dir, name),
2729                                 &exception);
2730         } while (exception.retry);
2731         return err;
2732 }
2733
2734 struct nfs4_createdata {
2735         struct rpc_message msg;
2736         struct nfs4_create_arg arg;
2737         struct nfs4_create_res res;
2738         struct nfs_fh fh;
2739         struct nfs_fattr fattr;
2740         struct nfs_fattr dir_fattr;
2741 };
2742
2743 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2744                 struct qstr *name, struct iattr *sattr, u32 ftype)
2745 {
2746         struct nfs4_createdata *data;
2747
2748         data = kzalloc(sizeof(*data), GFP_KERNEL);
2749         if (data != NULL) {
2750                 struct nfs_server *server = NFS_SERVER(dir);
2751
2752                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2753                 data->msg.rpc_argp = &data->arg;
2754                 data->msg.rpc_resp = &data->res;
2755                 data->arg.dir_fh = NFS_FH(dir);
2756                 data->arg.server = server;
2757                 data->arg.name = name;
2758                 data->arg.attrs = sattr;
2759                 data->arg.ftype = ftype;
2760                 data->arg.bitmask = server->attr_bitmask;
2761                 data->res.server = server;
2762                 data->res.fh = &data->fh;
2763                 data->res.fattr = &data->fattr;
2764                 data->res.dir_fattr = &data->dir_fattr;
2765                 nfs_fattr_init(data->res.fattr);
2766                 nfs_fattr_init(data->res.dir_fattr);
2767         }
2768         return data;
2769 }
2770
2771 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2772 {
2773         int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2774                                     &data->arg, &data->res, 1);
2775         if (status == 0) {
2776                 update_changeattr(dir, &data->res.dir_cinfo);
2777                 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2778                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2779         }
2780         return status;
2781 }
2782
2783 static void nfs4_free_createdata(struct nfs4_createdata *data)
2784 {
2785         kfree(data);
2786 }
2787
2788 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2789                 struct page *page, unsigned int len, struct iattr *sattr)
2790 {
2791         struct nfs4_createdata *data;
2792         int status = -ENAMETOOLONG;
2793
2794         if (len > NFS4_MAXPATHLEN)
2795                 goto out;
2796
2797         status = -ENOMEM;
2798         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2799         if (data == NULL)
2800                 goto out;
2801
2802         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2803         data->arg.u.symlink.pages = &page;
2804         data->arg.u.symlink.len = len;
2805         
2806         status = nfs4_do_create(dir, dentry, data);
2807
2808         nfs4_free_createdata(data);
2809 out:
2810         return status;
2811 }
2812
2813 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2814                 struct page *page, unsigned int len, struct iattr *sattr)
2815 {
2816         struct nfs4_exception exception = { };
2817         int err;
2818         do {
2819                 err = nfs4_handle_exception(NFS_SERVER(dir),
2820                                 _nfs4_proc_symlink(dir, dentry, page,
2821                                                         len, sattr),
2822                                 &exception);
2823         } while (exception.retry);
2824         return err;
2825 }
2826
2827 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2828                 struct iattr *sattr)
2829 {
2830         struct nfs4_createdata *data;
2831         int status = -ENOMEM;
2832
2833         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2834         if (data == NULL)
2835                 goto out;
2836
2837         status = nfs4_do_create(dir, dentry, data);
2838
2839         nfs4_free_createdata(data);
2840 out:
2841         return status;
2842 }
2843
2844 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2845                 struct iattr *sattr)
2846 {
2847         struct nfs4_exception exception = { };
2848         int err;
2849         do {
2850                 err = nfs4_handle_exception(NFS_SERVER(dir),
2851                                 _nfs4_proc_mkdir(dir, dentry, sattr),
2852                                 &exception);
2853         } while (exception.retry);
2854         return err;
2855 }
2856
2857 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2858                   u64 cookie, struct page *page, unsigned int count, int plus)
2859 {
2860         struct inode            *dir = dentry->d_inode;
2861         struct nfs4_readdir_arg args = {
2862                 .fh = NFS_FH(dir),
2863                 .pages = &page,
2864                 .pgbase = 0,
2865                 .count = count,
2866                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2867         };
2868         struct nfs4_readdir_res res;
2869         struct rpc_message msg = {
2870                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2871                 .rpc_argp = &args,
2872                 .rpc_resp = &res,
2873                 .rpc_cred = cred,
2874         };
2875         int                     status;
2876
2877         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2878                         dentry->d_parent->d_name.name,
2879                         dentry->d_name.name,
2880                         (unsigned long long)cookie);
2881         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2882         res.pgbase = args.pgbase;
2883         status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2884         if (status == 0)
2885                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2886
2887         nfs_invalidate_atime(dir);
2888
2889         dprintk("%s: returns %d\n", __func__, status);
2890         return status;
2891 }
2892
2893 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2894                   u64 cookie, struct page *page, unsigned int count, int plus)
2895 {
2896         struct nfs4_exception exception = { };
2897         int err;
2898         do {
2899                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2900                                 _nfs4_proc_readdir(dentry, cred, cookie,
2901                                         page, count, plus),
2902                                 &exception);
2903         } while (exception.retry);
2904         return err;
2905 }
2906
2907 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2908                 struct iattr *sattr, dev_t rdev)
2909 {
2910         struct nfs4_createdata *data;
2911         int mode = sattr->ia_mode;
2912         int status = -ENOMEM;
2913
2914         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2915         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2916
2917         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2918         if (data == NULL)
2919                 goto out;
2920
2921         if (S_ISFIFO(mode))
2922                 data->arg.ftype = NF4FIFO;
2923         else if (S_ISBLK(mode)) {
2924                 data->arg.ftype = NF4BLK;
2925                 data->arg.u.device.specdata1 = MAJOR(rdev);
2926                 data->arg.u.device.specdata2 = MINOR(rdev);
2927         }
2928         else if (S_ISCHR(mode)) {
2929                 data->arg.ftype = NF4CHR;
2930                 data->arg.u.device.specdata1 = MAJOR(rdev);
2931                 data->arg.u.device.specdata2 = MINOR(rdev);
2932         }
2933         
2934         status = nfs4_do_create(dir, dentry, data);
2935
2936         nfs4_free_createdata(data);
2937 out:
2938         return status;
2939 }
2940
2941 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2942                 struct iattr *sattr, dev_t rdev)
2943 {
2944         struct nfs4_exception exception = { };
2945         int err;
2946         do {
2947                 err = nfs4_handle_exception(NFS_SERVER(dir),
2948                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2949                                 &exception);
2950         } while (exception.retry);
2951         return err;
2952 }
2953
2954 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2955                  struct nfs_fsstat *fsstat)
2956 {
2957         struct nfs4_statfs_arg args = {
2958                 .fh = fhandle,
2959                 .bitmask = server->attr_bitmask,
2960         };
2961         struct nfs4_statfs_res res = {
2962                 .fsstat = fsstat,
2963         };
2964         struct rpc_message msg = {
2965                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2966                 .rpc_argp = &args,
2967                 .rpc_resp = &res,
2968         };
2969
2970         nfs_fattr_init(fsstat->fattr);
2971         return  nfs4_call_sync(server, &msg, &args, &res, 0);
2972 }
2973
2974 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2975 {
2976         struct nfs4_exception exception = { };
2977         int err;
2978         do {
2979                 err = nfs4_handle_exception(server,
2980                                 _nfs4_proc_statfs(server, fhandle, fsstat),
2981                                 &exception);
2982         } while (exception.retry);
2983         return err;
2984 }
2985
2986 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2987                 struct nfs_fsinfo *fsinfo)
2988 {
2989         struct nfs4_fsinfo_arg args = {
2990                 .fh = fhandle,
2991                 .bitmask = server->attr_bitmask,
2992         };
2993         struct nfs4_fsinfo_res res = {
2994                 .fsinfo = fsinfo,
2995         };
2996         struct rpc_message msg = {
2997                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2998                 .rpc_argp = &args,
2999                 .rpc_resp = &res,
3000         };
3001
3002         return nfs4_call_sync(server, &msg, &args, &res, 0);
3003 }
3004
3005 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3006 {
3007         struct nfs4_exception exception = { };
3008         int err;
3009
3010         do {
3011                 err = nfs4_handle_exception(server,
3012                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3013                                 &exception);
3014         } while (exception.retry);
3015         return err;
3016 }
3017
3018 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3019 {
3020         nfs_fattr_init(fsinfo->fattr);
3021         return nfs4_do_fsinfo(server, fhandle, fsinfo);
3022 }
3023
3024 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3025                 struct nfs_pathconf *pathconf)
3026 {
3027         struct nfs4_pathconf_arg args = {
3028                 .fh = fhandle,
3029                 .bitmask = server->attr_bitmask,
3030         };
3031         struct nfs4_pathconf_res res = {
3032                 .pathconf = pathconf,
3033         };
3034         struct rpc_message msg = {
3035                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3036                 .rpc_argp = &args,
3037                 .rpc_resp = &res,
3038         };
3039
3040         /* None of the pathconf attributes are mandatory to implement */
3041         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3042                 memset(pathconf, 0, sizeof(*pathconf));
3043                 return 0;
3044         }
3045
3046         nfs_fattr_init(pathconf->fattr);
3047         return nfs4_call_sync(server, &msg, &args, &res, 0);
3048 }
3049
3050 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3051                 struct nfs_pathconf *pathconf)
3052 {
3053         struct nfs4_exception exception = { };
3054         int err;
3055
3056         do {
3057                 err = nfs4_handle_exception(server,
3058                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
3059                                 &exception);
3060         } while (exception.retry);
3061         return err;
3062 }
3063
3064 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3065 {
3066         struct nfs_server *server = NFS_SERVER(data->inode);
3067
3068         dprintk("--> %s\n", __func__);
3069
3070         nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3071
3072         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3073                 nfs_restart_rpc(task, server->nfs_client);
3074                 return -EAGAIN;
3075         }
3076
3077         nfs_invalidate_atime(data->inode);
3078         if (task->tk_status > 0)
3079                 renew_lease(server, data->timestamp);
3080         return 0;
3081 }
3082
3083 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3084 {
3085         data->timestamp   = jiffies;
3086         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3087 }
3088
3089 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3090 {
3091         struct inode *inode = data->inode;
3092         
3093         nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3094                            task->tk_status);
3095
3096         if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3097                 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3098                 return -EAGAIN;
3099         }
3100         if (task->tk_status >= 0) {
3101                 renew_lease(NFS_SERVER(inode), data->timestamp);
3102                 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3103         }
3104         return 0;
3105 }
3106
3107 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3108 {
3109         struct nfs_server *server = NFS_SERVER(data->inode);
3110
3111         data->args.bitmask = server->cache_consistency_bitmask;
3112         data->res.server = server;
3113         data->timestamp   = jiffies;
3114
3115         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3116 }
3117
3118 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3119 {
3120         struct inode *inode = data->inode;
3121         
3122         nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3123                            task->tk_status);
3124         if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3125                 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3126                 return -EAGAIN;
3127         }
3128         nfs_refresh_inode(inode, data->res.fattr);
3129         return 0;
3130 }
3131
3132 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3133 {
3134         struct nfs_server *server = NFS_SERVER(data->inode);
3135         
3136         data->args.bitmask = server->cache_consistency_bitmask;
3137         data->res.server = server;
3138         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3139 }
3140
3141 /*
3142  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3143  * standalone procedure for queueing an asynchronous RENEW.
3144  */
3145 static void nfs4_renew_done(struct rpc_task *task, void *data)
3146 {
3147         struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
3148         unsigned long timestamp = (unsigned long)data;
3149
3150         if (task->tk_status < 0) {
3151                 /* Unless we're shutting down, schedule state recovery! */
3152                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3153                         nfs4_schedule_state_recovery(clp);
3154                 return;
3155         }
3156         spin_lock(&clp->cl_lock);
3157         if (time_before(clp->cl_last_renewal,timestamp))
3158                 clp->cl_last_renewal = timestamp;
3159         spin_unlock(&clp->cl_lock);
3160 }
3161
3162 static const struct rpc_call_ops nfs4_renew_ops = {
3163         .rpc_call_done = nfs4_renew_done,
3164 };
3165
3166 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3167 {
3168         struct rpc_message msg = {
3169                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3170                 .rpc_argp       = clp,
3171                 .rpc_cred       = cred,
3172         };
3173
3174         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3175                         &nfs4_renew_ops, (void *)jiffies);
3176 }
3177
3178 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3179 {
3180         struct rpc_message msg = {
3181                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3182                 .rpc_argp       = clp,
3183                 .rpc_cred       = cred,
3184         };
3185         unsigned long now = jiffies;
3186         int status;
3187
3188         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3189         if (status < 0)
3190                 return status;
3191         spin_lock(&clp->cl_lock);
3192         if (time_before(clp->cl_last_renewal,now))
3193                 clp->cl_last_renewal = now;
3194         spin_unlock(&clp->cl_lock);
3195         return 0;
3196 }
3197
3198 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3199 {
3200         return (server->caps & NFS_CAP_ACLS)
3201                 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3202                 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3203 }
3204
3205 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3206  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3207  * the stack.
3208  */
3209 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3210
3211 static void buf_to_pages(const void *buf, size_t buflen,
3212                 struct page **pages, unsigned int *pgbase)
3213 {
3214         const void *p = buf;
3215
3216         *pgbase = offset_in_page(buf);
3217         p -= *pgbase;
3218         while (p < buf + buflen) {
3219                 *(pages++) = virt_to_page(p);
3220                 p += PAGE_CACHE_SIZE;
3221         }
3222 }
3223
3224 struct nfs4_cached_acl {
3225         int cached;
3226         size_t len;
3227         char data[0];
3228 };
3229
3230 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3231 {
3232         struct nfs_inode *nfsi = NFS_I(inode);
3233
3234         spin_lock(&inode->i_lock);
3235         kfree(nfsi->nfs4_acl);
3236         nfsi->nfs4_acl = acl;
3237         spin_unlock(&inode->i_lock);
3238 }
3239
3240 static void nfs4_zap_acl_attr(struct inode *inode)
3241 {
3242         nfs4_set_cached_acl(inode, NULL);
3243 }
3244
3245 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3246 {
3247         struct nfs_inode *nfsi = NFS_I(inode);
3248         struct nfs4_cached_acl *acl;
3249         int ret = -ENOENT;
3250
3251         spin_lock(&inode->i_lock);
3252         acl = nfsi->nfs4_acl;
3253         if (acl == NULL)
3254                 goto out;
3255         if (buf == NULL) /* user is just asking for length */
3256                 goto out_len;
3257         if (acl->cached == 0)
3258                 goto out;
3259         ret = -ERANGE; /* see getxattr(2) man page */
3260         if (acl->len > buflen)
3261                 goto out;
3262         memcpy(buf, acl->data, acl->len);
3263 out_len:
3264         ret = acl->len;
3265 out:
3266         spin_unlock(&inode->i_lock);
3267         return ret;
3268 }
3269
3270 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3271 {
3272         struct nfs4_cached_acl *acl;
3273
3274         if (buf && acl_len <= PAGE_SIZE) {
3275                 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3276                 if (acl == NULL)
3277                         goto out;
3278                 acl->cached = 1;
3279                 memcpy(acl->data, buf, acl_len);
3280         } else {
3281                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3282                 if (acl == NULL)
3283                         goto out;
3284                 acl->cached = 0;
3285         }
3286         acl->len = acl_len;
3287 out:
3288         nfs4_set_cached_acl(inode, acl);
3289 }
3290
3291 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3292 {
3293         struct page *pages[NFS4ACL_MAXPAGES];
3294         struct nfs_getaclargs args = {
3295                 .fh = NFS_FH(inode),
3296                 .acl_pages = pages,
3297                 .acl_len = buflen,
3298         };
3299         struct nfs_getaclres res = {
3300                 .acl_len = buflen,
3301         };
3302         void *resp_buf;
3303         struct rpc_message msg = {
3304                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3305                 .rpc_argp = &args,
3306                 .rpc_resp = &res,
3307         };
3308         struct page *localpage = NULL;
3309         int ret;
3310
3311         if (buflen < PAGE_SIZE) {
3312                 /* As long as we're doing a round trip to the server anyway,
3313                  * let's be prepared for a page of acl data. */
3314                 localpage = alloc_page(GFP_KERNEL);
3315                 resp_buf = page_address(localpage);
3316                 if (localpage == NULL)
3317                         return -ENOMEM;
3318                 args.acl_pages[0] = localpage;
3319                 args.acl_pgbase = 0;
3320                 args.acl_len = PAGE_SIZE;
3321         } else {
3322                 resp_buf = buf;
3323                 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3324         }
3325         ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3326         if (ret)
3327                 goto out_free;
3328         if (res.acl_len > args.acl_len)
3329                 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3330         else
3331                 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3332         if (buf) {
3333                 ret = -ERANGE;
3334                 if (res.acl_len > buflen)
3335                         goto out_free;
3336                 if (localpage)
3337                         memcpy(buf, resp_buf, res.acl_len);
3338         }
3339         ret = res.acl_len;
3340 out_free:
3341         if (localpage)
3342                 __free_page(localpage);
3343         return ret;
3344 }
3345
3346 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3347 {
3348         struct nfs4_exception exception = { };
3349         ssize_t ret;
3350         do {
3351                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3352                 if (ret >= 0)
3353                         break;
3354                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3355         } while (exception.retry);
3356         return ret;
3357 }
3358
3359 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3360 {
3361         struct nfs_server *server = NFS_SERVER(inode);
3362         int ret;
3363
3364         if (!nfs4_server_supports_acls(server))
3365                 return -EOPNOTSUPP;
3366         ret = nfs_revalidate_inode(server, inode);
3367         if (ret < 0)
3368                 return ret;
3369         ret = nfs4_read_cached_acl(inode, buf, buflen);
3370         if (ret != -ENOENT)
3371                 return ret;
3372         return nfs4_get_acl_uncached(inode, buf, buflen);
3373 }
3374
3375 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3376 {
3377         struct nfs_server *server = NFS_SERVER(inode);
3378         struct page *pages[NFS4ACL_MAXPAGES];
3379         struct nfs_setaclargs arg = {
3380                 .fh             = NFS_FH(inode),
3381                 .acl_pages      = pages,
3382                 .acl_len        = buflen,
3383         };
3384         struct nfs_setaclres res;
3385         struct rpc_message msg = {
3386                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3387                 .rpc_argp       = &arg,
3388                 .rpc_resp       = &res,
3389         };
3390         int ret;
3391
3392         if (!nfs4_server_supports_acls(server))
3393                 return -EOPNOTSUPP;
3394         nfs_inode_return_delegation(inode);
3395         buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3396         ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3397         nfs_access_zap_cache(inode);
3398         nfs_zap_acl_cache(inode);
3399         return ret;
3400 }
3401
3402 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3403 {
3404         struct nfs4_exception exception = { };
3405         int err;
3406         do {
3407                 err = nfs4_handle_exception(NFS_SERVER(inode),
3408                                 __nfs4_proc_set_acl(inode, buf, buflen),
3409                                 &exception);
3410         } while (exception.retry);
3411         return err;
3412 }
3413
3414 static int
3415 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3416 {
3417         if (!clp || task->tk_status >= 0)
3418                 return 0;
3419         switch(task->tk_status) {
3420                 case -NFS4ERR_ADMIN_REVOKED:
3421                 case -NFS4ERR_BAD_STATEID:
3422                 case -NFS4ERR_OPENMODE:
3423                         if (state == NULL)
3424                                 break;
3425                         nfs4_state_mark_reclaim_nograce(clp, state);
3426                 case -NFS4ERR_STALE_CLIENTID:
3427                 case -NFS4ERR_STALE_STATEID:
3428                 case -NFS4ERR_EXPIRED:
3429                         rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3430                         nfs4_schedule_state_recovery(clp);
3431                         if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3432                                 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3433                         task->tk_status = 0;
3434                         return -EAGAIN;
3435 #if defined(CONFIG_NFS_V4_1)
3436                 case -NFS4ERR_BADSESSION:
3437                 case -NFS4ERR_BADSLOT:
3438                 case -NFS4ERR_BAD_HIGH_SLOT:
3439                 case -NFS4ERR_DEADSESSION:
3440                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3441                 case -NFS4ERR_SEQ_FALSE_RETRY:
3442                 case -NFS4ERR_SEQ_MISORDERED:
3443                         dprintk("%s ERROR %d, Reset session\n", __func__,
3444                                 task->tk_status);
3445                         nfs4_schedule_state_recovery(clp);
3446                         task->tk_status = 0;
3447                         return -EAGAIN;
3448 #endif /* CONFIG_NFS_V4_1 */
3449                 case -NFS4ERR_DELAY:
3450                         if (server)
3451                                 nfs_inc_server_stats(server, NFSIOS_DELAY);
3452                 case -NFS4ERR_GRACE:
3453                         rpc_delay(task, NFS4_POLL_RETRY_MAX);
3454                         task->tk_status = 0;
3455                         return -EAGAIN;
3456                 case -NFS4ERR_OLD_STATEID:
3457                         task->tk_status = 0;
3458                         return -EAGAIN;
3459         }
3460         task->tk_status = nfs4_map_errors(task->tk_status);
3461         return 0;
3462 }
3463
3464 static int
3465 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3466 {
3467         return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3468 }
3469
3470 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3471 {
3472         nfs4_verifier sc_verifier;
3473         struct nfs4_setclientid setclientid = {
3474                 .sc_verifier = &sc_verifier,
3475                 .sc_prog = program,
3476         };
3477         struct rpc_message msg = {
3478                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3479                 .rpc_argp = &setclientid,
3480                 .rpc_resp = clp,
3481                 .rpc_cred = cred,
3482         };
3483         __be32 *p;
3484         int loop = 0;
3485         int status;
3486
3487         p = (__be32*)sc_verifier.data;
3488         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3489         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3490
3491         for(;;) {
3492                 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3493                                 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3494                                 clp->cl_ipaddr,
3495                                 rpc_peeraddr2str(clp->cl_rpcclient,
3496                                                         RPC_DISPLAY_ADDR),
3497                                 rpc_peeraddr2str(clp->cl_rpcclient,
3498                                                         RPC_DISPLAY_PROTO),
3499                                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3500                                 clp->cl_id_uniquifier);
3501                 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3502                                 sizeof(setclientid.sc_netid),
3503                                 rpc_peeraddr2str(clp->cl_rpcclient,
3504                                                         RPC_DISPLAY_NETID));
3505                 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3506                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3507                                 clp->cl_ipaddr, port >> 8, port & 255);
3508
3509                 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3510                 if (status != -NFS4ERR_CLID_INUSE)
3511                         break;
3512                 if (signalled())
3513                         break;
3514                 if (loop++ & 1)
3515                         ssleep(clp->cl_lease_time + 1);
3516                 else
3517                         if (++clp->cl_id_uniquifier == 0)
3518                                 break;
3519         }
3520         return status;
3521 }
3522
3523 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3524 {
3525         struct nfs_fsinfo fsinfo;
3526         struct rpc_message msg = {
3527                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3528                 .rpc_argp = clp,
3529                 .rpc_resp = &fsinfo,
3530                 .rpc_cred = cred,
3531         };
3532         unsigned long now;
3533         int status;
3534
3535         now = jiffies;
3536         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3537         if (status == 0) {
3538                 spin_lock(&clp->cl_lock);
3539                 clp->cl_lease_time = fsinfo.lease_time * HZ;
3540                 clp->cl_last_renewal = now;
3541                 spin_unlock(&clp->cl_lock);
3542         }
3543         return status;
3544 }
3545
3546 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3547 {
3548         long timeout = 0;
3549         int err;
3550         do {
3551                 err = _nfs4_proc_setclientid_confirm(clp, cred);
3552                 switch (err) {
3553                         case 0:
3554                                 return err;
3555                         case -NFS4ERR_RESOURCE:
3556                                 /* The IBM lawyers misread another document! */
3557                         case -NFS4ERR_DELAY:
3558                                 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3559                 }
3560         } while (err == 0);
3561         return err;
3562 }
3563
3564 struct nfs4_delegreturndata {
3565         struct nfs4_delegreturnargs args;
3566         struct nfs4_delegreturnres res;
3567         struct nfs_fh fh;
3568         nfs4_stateid stateid;
3569         unsigned long timestamp;
3570         struct nfs_fattr fattr;
3571         int rpc_status;
3572 };
3573
3574 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3575 {
3576         struct nfs4_delegreturndata *data = calldata;
3577
3578         nfs4_sequence_done(data->res.server, &data->res.seq_res,
3579                         task->tk_status);
3580
3581         switch (task->tk_status) {
3582         case -NFS4ERR_STALE_STATEID:
3583         case -NFS4ERR_EXPIRED:
3584         case 0:
3585                 renew_lease(data->res.server, data->timestamp);
3586                 break;
3587         default:
3588                 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3589                                 -EAGAIN) {
3590                         nfs_restart_rpc(task, data->res.server->nfs_client);
3591                         return;
3592                 }
3593         }
3594         data->rpc_status = task->tk_status;
3595 }
3596
3597 static void nfs4_delegreturn_release(void *calldata)
3598 {
3599         kfree(calldata);
3600 }
3601
3602 #if defined(CONFIG_NFS_V4_1)
3603 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3604 {
3605         struct nfs4_delegreturndata *d_data;
3606
3607         d_data = (struct nfs4_delegreturndata *)data;
3608
3609         if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3610                                 &d_data->args.seq_args,
3611                                 &d_data->res.seq_res, 1, task))
3612                 return;
3613         rpc_call_start(task);
3614 }
3615 #endif /* CONFIG_NFS_V4_1 */
3616
3617 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3618 #if defined(CONFIG_NFS_V4_1)
3619         .rpc_call_prepare = nfs4_delegreturn_prepare,
3620 #endif /* CONFIG_NFS_V4_1 */
3621         .rpc_call_done = nfs4_delegreturn_done,
3622         .rpc_release = nfs4_delegreturn_release,
3623 };
3624
3625 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3626 {
3627         struct nfs4_delegreturndata *data;
3628         struct nfs_server *server = NFS_SERVER(inode);
3629         struct rpc_task *task;
3630         struct rpc_message msg = {
3631                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3632                 .rpc_cred = cred,
3633         };
3634         struct rpc_task_setup task_setup_data = {
3635                 .rpc_client = server->client,
3636                 .rpc_message = &msg,
3637                 .callback_ops = &nfs4_delegreturn_ops,
3638                 .flags = RPC_TASK_ASYNC,
3639         };
3640         int status = 0;
3641
3642         data = kzalloc(sizeof(*data), GFP_KERNEL);
3643         if (data == NULL)
3644                 return -ENOMEM;
3645         data->args.fhandle = &data->fh;
3646         data->args.stateid = &data->stateid;
3647         data->args.bitmask = server->attr_bitmask;
3648         nfs_copy_fh(&data->fh, NFS_FH(inode));
3649         memcpy(&data->stateid, stateid, sizeof(data->stateid));
3650         data->res.fattr = &data->fattr;
3651         data->res.server = server;
3652         data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3653         nfs_fattr_init(data->res.fattr);
3654         data->timestamp = jiffies;
3655         data->rpc_status = 0;
3656
3657         task_setup_data.callback_data = data;
3658         msg.rpc_argp = &data->args,
3659         msg.rpc_resp = &data->res,
3660         task = rpc_run_task(&task_setup_data);
3661         if (IS_ERR(task))
3662                 return PTR_ERR(task);
3663         if (!issync)
3664                 goto out;
3665         status = nfs4_wait_for_completion_rpc_task(task);
3666         if (status != 0)
3667                 goto out;
3668         status = data->rpc_status;
3669         if (status != 0)
3670                 goto out;
3671         nfs_refresh_inode(inode, &data->fattr);
3672 out:
3673         rpc_put_task(task);
3674         return status;
3675 }
3676
3677 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3678 {
3679         struct nfs_server *server = NFS_SERVER(inode);
3680         struct nfs4_exception exception = { };
3681         int err;
3682         do {
3683                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3684                 switch (err) {
3685                         case -NFS4ERR_STALE_STATEID:
3686                         case -NFS4ERR_EXPIRED:
3687                         case 0:
3688                                 return 0;
3689                 }
3690                 err = nfs4_handle_exception(server, err, &exception);
3691         } while (exception.retry);
3692         return err;
3693 }
3694
3695 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3696 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3697
3698 /* 
3699  * sleep, with exponential backoff, and retry the LOCK operation. 
3700  */
3701 static unsigned long
3702 nfs4_set_lock_task_retry(unsigned long timeout)
3703 {
3704         schedule_timeout_killable(timeout);
3705         timeout <<= 1;
3706         if (timeout > NFS4_LOCK_MAXTIMEOUT)
3707                 return NFS4_LOCK_MAXTIMEOUT;
3708         return timeout;
3709 }
3710
3711 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3712 {
3713         struct inode *inode = state->inode;
3714         struct nfs_server *server = NFS_SERVER(inode);
3715         struct nfs_client *clp = server->nfs_client;
3716         struct nfs_lockt_args arg = {
3717                 .fh = NFS_FH(inode),
3718                 .fl = request,
3719         };
3720         struct nfs_lockt_res res = {
3721                 .denied = request,
3722         };
3723         struct rpc_message msg = {
3724                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3725                 .rpc_argp       = &arg,
3726                 .rpc_resp       = &res,
3727                 .rpc_cred       = state->owner->so_cred,
3728         };
3729         struct nfs4_lock_state *lsp;
3730         int status;
3731
3732         arg.lock_owner.clientid = clp->cl_clientid;
3733         status = nfs4_set_lock_state(state, request);
3734         if (status != 0)
3735                 goto out;
3736         lsp = request->fl_u.nfs4_fl.owner;
3737         arg.lock_owner.id = lsp->ls_id.id;
3738         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3739         switch (status) {
3740                 case 0:
3741                         request->fl_type = F_UNLCK;
3742                         break;
3743                 case -NFS4ERR_DENIED:
3744                         status = 0;
3745         }
3746         request->fl_ops->fl_release_private(request);
3747 out:
3748         return status;
3749 }
3750
3751 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3752 {
3753         struct nfs4_exception exception = { };
3754         int err;
3755
3756         do {
3757                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3758                                 _nfs4_proc_getlk(state, cmd, request),
3759                                 &exception);
3760         } while (exception.retry);
3761         return err;
3762 }
3763
3764 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3765 {
3766         int res = 0;
3767         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3768                 case FL_POSIX:
3769                         res = posix_lock_file_wait(file, fl);
3770                         break;
3771                 case FL_FLOCK:
3772                         res = flock_lock_file_wait(file, fl);
3773                         break;
3774                 default:
3775                         BUG();
3776         }
3777         return res;
3778 }
3779
3780 struct nfs4_unlockdata {
3781         struct nfs_locku_args arg;
3782         struct nfs_locku_res res;
3783         struct nfs4_lock_state *lsp;
3784         struct nfs_open_context *ctx;
3785         struct file_lock fl;
3786         const struct nfs_server *server;
3787         unsigned long timestamp;
3788 };
3789
3790 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3791                 struct nfs_open_context *ctx,
3792                 struct nfs4_lock_state *lsp,
3793                 struct nfs_seqid *seqid)
3794 {
3795         struct nfs4_unlockdata *p;
3796         struct inode *inode = lsp->ls_state->inode;
3797
3798         p = kzalloc(sizeof(*p), GFP_KERNEL);
3799         if (p == NULL)
3800                 return NULL;
3801         p->arg.fh = NFS_FH(inode);
3802         p->arg.fl = &p->fl;
3803         p->arg.seqid = seqid;
3804         p->res.seqid = seqid;
3805         p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3806         p->arg.stateid = &lsp->ls_stateid;
3807         p->lsp = lsp;
3808         atomic_inc(&lsp->ls_count);
3809         /* Ensure we don't close file until we're done freeing locks! */
3810         p->ctx = get_nfs_open_context(ctx);
3811         memcpy(&p->fl, fl, sizeof(p->fl));
3812         p->server = NFS_SERVER(inode);
3813         return p;
3814 }
3815
3816 static void nfs4_locku_release_calldata(void *data)
3817 {
3818         struct nfs4_unlockdata *calldata = data;
3819         nfs_free_seqid(calldata->arg.seqid);
3820         nfs4_put_lock_state(calldata->lsp);
3821         put_nfs_open_context(calldata->ctx);
3822         kfree(calldata);
3823 }
3824
3825 static void nfs4_locku_done(struct rpc_task *task, void *data)
3826 {
3827         struct nfs4_unlockdata *calldata = data;
3828
3829         nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3830                            task->tk_status);
3831         if (RPC_ASSASSINATED(task))
3832                 return;
3833         switch (task->tk_status) {
3834                 case 0:
3835                         memcpy(calldata->lsp->ls_stateid.data,
3836                                         calldata->res.stateid.data,
3837                                         sizeof(calldata->lsp->ls_stateid.data));
3838                         renew_lease(calldata->server, calldata->timestamp);
3839                         break;
3840                 case -NFS4ERR_BAD_STATEID:
3841                 case -NFS4ERR_OLD_STATEID:
3842                 case -NFS4ERR_STALE_STATEID:
3843                 case -NFS4ERR_EXPIRED:
3844                         break;
3845                 default:
3846                         if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3847                                 nfs_restart_rpc(task,
3848                                                  calldata->server->nfs_client);
3849         }
3850 }
3851
3852 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3853 {
3854         struct nfs4_unlockdata *calldata = data;
3855
3856         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3857                 return;
3858         if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3859                 /* Note: exit _without_ running nfs4_locku_done */
3860                 task->tk_action = NULL;
3861                 return;
3862         }
3863         calldata->timestamp = jiffies;
3864         if (nfs4_setup_sequence(calldata->server->nfs_client,
3865                                 &calldata->arg.seq_args,
3866                                 &calldata->res.seq_res, 1, task))
3867                 return;
3868         rpc_call_start(task);
3869 }
3870
3871 static const struct rpc_call_ops nfs4_locku_ops = {
3872         .rpc_call_prepare = nfs4_locku_prepare,
3873         .rpc_call_done = nfs4_locku_done,
3874         .rpc_release = nfs4_locku_release_calldata,
3875 };
3876
3877 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3878                 struct nfs_open_context *ctx,
3879                 struct nfs4_lock_state *lsp,
3880                 struct nfs_seqid *seqid)
3881 {
3882         struct nfs4_unlockdata *data;
3883         struct rpc_message msg = {
3884                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3885                 .rpc_cred = ctx->cred,
3886         };
3887         struct rpc_task_setup task_setup_data = {
3888                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3889                 .rpc_message = &msg,
3890                 .callback_ops = &nfs4_locku_ops,
3891                 .workqueue = nfsiod_workqueue,
3892                 .flags = RPC_TASK_ASYNC,
3893         };
3894
3895         /* Ensure this is an unlock - when canceling a lock, the
3896          * canceled lock is passed in, and it won't be an unlock.
3897          */
3898         fl->fl_type = F_UNLCK;
3899
3900         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3901         if (data == NULL) {
3902                 nfs_free_seqid(seqid);
3903                 return ERR_PTR(-ENOMEM);
3904         }
3905
3906         msg.rpc_argp = &data->arg,
3907         msg.rpc_resp = &data->res,
3908         task_setup_data.callback_data = data;
3909         return rpc_run_task(&task_setup_data);
3910 }
3911
3912 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3913 {
3914         struct nfs_inode *nfsi = NFS_I(state->inode);
3915         struct nfs_seqid *seqid;
3916         struct nfs4_lock_state *lsp;
3917         struct rpc_task *task;
3918         int status = 0;
3919         unsigned char fl_flags = request->fl_flags;
3920
3921         status = nfs4_set_lock_state(state, request);
3922         /* Unlock _before_ we do the RPC call */
3923         request->fl_flags |= FL_EXISTS;
3924         down_read(&nfsi->rwsem);
3925         if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3926                 up_read(&nfsi->rwsem);
3927                 goto out;
3928         }
3929         up_read(&nfsi->rwsem);
3930         if (status != 0)
3931                 goto out;
3932         /* Is this a delegated lock? */
3933         if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3934                 goto out;
3935         lsp = request->fl_u.nfs4_fl.owner;
3936         seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3937         status = -ENOMEM;
3938         if (seqid == NULL)
3939                 goto out;
3940         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3941         status = PTR_ERR(task);
3942         if (IS_ERR(task))
3943                 goto out;
3944         status = nfs4_wait_for_completion_rpc_task(task);
3945         rpc_put_task(task);
3946 out:
3947         request->fl_flags = fl_flags;
3948         return status;
3949 }
3950
3951 struct nfs4_lockdata {
3952         struct nfs_lock_args arg;
3953         struct nfs_lock_res res;
3954         struct nfs4_lock_state *lsp;
3955         struct nfs_open_context *ctx;
3956         struct file_lock fl;
3957         unsigned long timestamp;
3958         int rpc_status;
3959         int cancelled;
3960         struct nfs_server *server;
3961 };
3962
3963 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3964                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3965 {
3966         struct nfs4_lockdata *p;
3967         struct inode *inode = lsp->ls_state->inode;
3968         struct nfs_server *server = NFS_SERVER(inode);
3969
3970         p = kzalloc(sizeof(*p), GFP_KERNEL);
3971         if (p == NULL)
3972                 return NULL;
3973
3974         p->arg.fh = NFS_FH(inode);
3975         p->arg.fl = &p->fl;
3976         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3977         if (p->arg.open_seqid == NULL)
3978                 goto out_free;
3979         p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3980         if (p->arg.lock_seqid == NULL)
3981                 goto out_free_seqid;
3982         p->arg.lock_stateid = &lsp->ls_stateid;
3983         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3984         p->arg.lock_owner.id = lsp->ls_id.id;
3985         p->res.lock_seqid = p->arg.lock_seqid;
3986         p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3987         p->lsp = lsp;
3988         p->server = server;
3989         atomic_inc(&lsp->ls_count);
3990         p->ctx = get_nfs_open_context(ctx);
3991         memcpy(&p->fl, fl, sizeof(p->fl));
3992         return p;
3993 out_free_seqid:
3994         nfs_free_seqid(p->arg.open_seqid);
3995 out_free:
3996         kfree(p);
3997         return NULL;
3998 }
3999
4000 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4001 {
4002         struct nfs4_lockdata *data = calldata;
4003         struct nfs4_state *state = data->lsp->ls_state;
4004
4005         dprintk("%s: begin!\n", __func__);
4006         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4007                 return;
4008         /* Do we need to do an open_to_lock_owner? */
4009         if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4010                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4011                         return;
4012                 data->arg.open_stateid = &state->stateid;
4013                 data->arg.new_lock_owner = 1;
4014                 data->res.open_seqid = data->arg.open_seqid;
4015         } else
4016                 data->arg.new_lock_owner = 0;
4017         data->timestamp = jiffies;
4018         if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4019                                 &data->res.seq_res, 1, task))
4020                 return;
4021         rpc_call_start(task);
4022         dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4023 }
4024
4025 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4026 {
4027         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4028         nfs4_lock_prepare(task, calldata);
4029 }
4030
4031 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4032 {
4033         struct nfs4_lockdata *data = calldata;
4034
4035         dprintk("%s: begin!\n", __func__);
4036
4037         nfs4_sequence_done(data->server, &data->res.seq_res,
4038                         task->tk_status);
4039
4040         data->rpc_status = task->tk_status;
4041         if (RPC_ASSASSINATED(task))
4042                 goto out;
4043         if (data->arg.new_lock_owner != 0) {
4044                 if (data->rpc_status == 0)
4045                         nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4046                 else
4047                         goto out;
4048         }
4049         if (data->rpc_status == 0) {
4050                 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4051                                         sizeof(data->lsp->ls_stateid.data));
4052                 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4053                 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4054         }
4055 out:
4056         dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4057 }
4058
4059 static void nfs4_lock_release(void *calldata)
4060 {
4061         struct nfs4_lockdata *data = calldata;
4062
4063         dprintk("%s: begin!\n", __func__);
4064         nfs_free_seqid(data->arg.open_seqid);
4065         if (data->cancelled != 0) {
4066                 struct rpc_task *task;
4067                 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4068                                 data->arg.lock_seqid);
4069                 if (!IS_ERR(task))
4070                         rpc_put_task(task);
4071                 dprintk("%s: cancelling lock!\n", __func__);
4072         } else
4073                 nfs_free_seqid(data->arg.lock_seqid);
4074         nfs4_put_lock_state(data->lsp);
4075         put_nfs_open_context(data->ctx);
4076         kfree(data);
4077         dprintk("%s: done!\n", __func__);
4078 }
4079
4080 static const struct rpc_call_ops nfs4_lock_ops = {
4081         .rpc_call_prepare = nfs4_lock_prepare,
4082         .rpc_call_done = nfs4_lock_done,
4083         .rpc_release = nfs4_lock_release,
4084 };
4085
4086 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4087         .rpc_call_prepare = nfs4_recover_lock_prepare,
4088         .rpc_call_done = nfs4_lock_done,
4089         .rpc_release = nfs4_lock_release,
4090 };
4091
4092 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4093 {
4094         struct nfs4_lockdata *data;
4095         struct rpc_task *task;
4096         struct rpc_message msg = {
4097                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4098                 .rpc_cred = state->owner->so_cred,
4099         };
4100         struct rpc_task_setup task_setup_data = {
4101                 .rpc_client = NFS_CLIENT(state->inode),
4102                 .rpc_message = &msg,
4103                 .callback_ops = &nfs4_lock_ops,
4104                 .workqueue = nfsiod_workqueue,
4105                 .flags = RPC_TASK_ASYNC,
4106         };
4107         int ret;
4108
4109         dprintk("%s: begin!\n", __func__);
4110         data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4111                         fl->fl_u.nfs4_fl.owner);
4112         if (data == NULL)
4113                 return -ENOMEM;
4114         if (IS_SETLKW(cmd))
4115                 data->arg.block = 1;
4116         if (recovery_type > NFS_LOCK_NEW) {
4117                 if (recovery_type == NFS_LOCK_RECLAIM)
4118                         data->arg.reclaim = NFS_LOCK_RECLAIM;
4119                 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4120         }
4121         msg.rpc_argp = &data->arg,
4122         msg.rpc_resp = &data->res,
4123         task_setup_data.callback_data = data;
4124         task = rpc_run_task(&task_setup_data);
4125         if (IS_ERR(task))
4126                 return PTR_ERR(task);
4127         ret = nfs4_wait_for_completion_rpc_task(task);
4128         if (ret == 0) {
4129                 ret = data->rpc_status;
4130         } else
4131                 data->cancelled = 1;
4132         rpc_put_task(task);
4133         dprintk("%s: done, ret = %d!\n", __func__, ret);
4134         return ret;
4135 }
4136
4137 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4138 {
4139         struct nfs_server *server = NFS_SERVER(state->inode);
4140         struct nfs4_exception exception = { };
4141         int err;
4142
4143         do {
4144                 /* Cache the lock if possible... */
4145                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4146                         return 0;
4147                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4148                 if (err != -NFS4ERR_DELAY)
4149                         break;
4150                 nfs4_handle_exception(server, err, &exception);
4151         } while (exception.retry);
4152         return err;
4153 }
4154
4155 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4156 {
4157         struct nfs_server *server = NFS_SERVER(state->inode);
4158         struct nfs4_exception exception = { };
4159         int err;
4160
4161         err = nfs4_set_lock_state(state, request);
4162         if (err != 0)
4163                 return err;
4164         do {
4165                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4166                         return 0;
4167                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4168                 switch (err) {
4169                 default:
4170                         goto out;
4171                 case -NFS4ERR_GRACE:
4172                 case -NFS4ERR_DELAY:
4173                         nfs4_handle_exception(server, err, &exception);
4174                         err = 0;
4175                 }
4176         } while (exception.retry);
4177 out:
4178         return err;
4179 }
4180
4181 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4182 {
4183         struct nfs_inode *nfsi = NFS_I(state->inode);
4184         unsigned char fl_flags = request->fl_flags;
4185         int status;
4186
4187         /* Is this a delegated open? */
4188         status = nfs4_set_lock_state(state, request);
4189         if (status != 0)
4190                 goto out;
4191         request->fl_flags |= FL_ACCESS;
4192         status = do_vfs_lock(request->fl_file, request);
4193         if (status < 0)
4194                 goto out;
4195         down_read(&nfsi->rwsem);
4196         if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4197                 /* Yes: cache locks! */
4198                 /* ...but avoid races with delegation recall... */
4199                 request->fl_flags = fl_flags & ~FL_SLEEP;
4200                 status = do_vfs_lock(request->fl_file, request);
4201                 goto out_unlock;
4202         }
4203         status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4204         if (status != 0)
4205                 goto out_unlock;
4206         /* Note: we always want to sleep here! */
4207         request->fl_flags = fl_flags | FL_SLEEP;
4208         if (do_vfs_lock(request->fl_file, request) < 0)
4209                 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4210 out_unlock:
4211         up_read(&nfsi->rwsem);
4212 out:
4213         request->fl_flags = fl_flags;
4214         return status;
4215 }
4216
4217 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4218 {
4219         struct nfs4_exception exception = { };
4220         int err;
4221
4222         do {
4223                 err = _nfs4_proc_setlk(state, cmd, request);
4224                 if (err == -NFS4ERR_DENIED)
4225                         err = -EAGAIN;
4226                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4227                                 err, &exception);
4228         } while (exception.retry);
4229         return err;
4230 }
4231
4232 static int
4233 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4234 {
4235         struct nfs_open_context *ctx;
4236         struct nfs4_state *state;
4237         unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4238         int status;
4239
4240         /* verify open state */
4241         ctx = nfs_file_open_context(filp);
4242         state = ctx->state;
4243
4244         if (request->fl_start < 0 || request->fl_end < 0)
4245                 return -EINVAL;
4246
4247         if (IS_GETLK(cmd)) {
4248                 if (state != NULL)
4249                         return nfs4_proc_getlk(state, F_GETLK, request);
4250                 return 0;
4251         }
4252
4253         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4254                 return -EINVAL;
4255
4256         if (request->fl_type == F_UNLCK) {
4257                 if (state != NULL)
4258                         return nfs4_proc_unlck(state, cmd, request);
4259                 return 0;
4260         }
4261
4262         if (state == NULL)
4263                 return -ENOLCK;
4264         do {
4265                 status = nfs4_proc_setlk(state, cmd, request);
4266                 if ((status != -EAGAIN) || IS_SETLK(cmd))
4267                         break;
4268                 timeout = nfs4_set_lock_task_retry(timeout);
4269                 status = -ERESTARTSYS;
4270                 if (signalled())
4271                         break;
4272         } while(status < 0);
4273         return status;
4274 }
4275
4276 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4277 {
4278         struct nfs_server *server = NFS_SERVER(state->inode);
4279         struct nfs4_exception exception = { };
4280         int err;
4281
4282         err = nfs4_set_lock_state(state, fl);
4283         if (err != 0)
4284                 goto out;
4285         do {
4286                 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4287                 switch (err) {
4288                         default:
4289                                 printk(KERN_ERR "%s: unhandled error %d.\n",
4290                                                 __func__, err);
4291                         case 0:
4292                         case -ESTALE:
4293                                 goto out;
4294                         case -NFS4ERR_EXPIRED:
4295                         case -NFS4ERR_STALE_CLIENTID:
4296                         case -NFS4ERR_STALE_STATEID:
4297                         case -NFS4ERR_BADSESSION:
4298                         case -NFS4ERR_BADSLOT:
4299                         case -NFS4ERR_BAD_HIGH_SLOT:
4300                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4301                         case -NFS4ERR_DEADSESSION:
4302                                 nfs4_schedule_state_recovery(server->nfs_client);
4303                                 goto out;
4304                         case -ERESTARTSYS:
4305                                 /*
4306                                  * The show must go on: exit, but mark the
4307                                  * stateid as needing recovery.
4308                                  */
4309                         case -NFS4ERR_ADMIN_REVOKED:
4310                         case -NFS4ERR_BAD_STATEID:
4311                         case -NFS4ERR_OPENMODE:
4312                                 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4313                                 err = 0;
4314                                 goto out;
4315                         case -ENOMEM:
4316                         case -NFS4ERR_DENIED:
4317                                 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4318                                 err = 0;
4319                                 goto out;
4320                         case -NFS4ERR_DELAY:
4321                                 break;
4322                 }
4323                 err = nfs4_handle_exception(server, err, &exception);
4324         } while (exception.retry);
4325 out:
4326         return err;
4327 }
4328
4329 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4330
4331 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4332                 size_t buflen, int flags)
4333 {
4334         struct inode *inode = dentry->d_inode;
4335
4336         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4337                 return -EOPNOTSUPP;
4338
4339         return nfs4_proc_set_acl(inode, buf, buflen);
4340 }
4341
4342 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4343  * and that's what we'll do for e.g. user attributes that haven't been set.
4344  * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4345  * attributes in kernel-managed attribute namespaces. */
4346 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4347                 size_t buflen)
4348 {
4349         struct inode *inode = dentry->d_inode;
4350
4351         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4352                 return -EOPNOTSUPP;
4353
4354         return nfs4_proc_get_acl(inode, buf, buflen);
4355 }
4356
4357 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4358 {
4359         size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4360
4361         if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4362                 return 0;
4363         if (buf && buflen < len)
4364                 return -ERANGE;
4365         if (buf)
4366                 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4367         return len;
4368 }
4369
4370 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4371 {
4372         if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4373                 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4374                 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4375                 return;
4376
4377         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4378                 NFS_ATTR_FATTR_NLINK;
4379         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4380         fattr->nlink = 2;
4381 }
4382
4383 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4384                 struct nfs4_fs_locations *fs_locations, struct page *page)
4385 {
4386         struct nfs_server *server = NFS_SERVER(dir);
4387         u32 bitmask[2] = {
4388                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4389                 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4390         };
4391         struct nfs4_fs_locations_arg args = {
4392                 .dir_fh = NFS_FH(dir),
4393                 .name = name,
4394                 .page = page,
4395                 .bitmask = bitmask,
4396         };
4397         struct nfs4_fs_locations_res res = {
4398                 .fs_locations = fs_locations,
4399         };
4400         struct rpc_message msg = {
4401                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4402                 .rpc_argp = &args,
4403                 .rpc_resp = &res,
4404         };
4405         int status;
4406
4407         dprintk("%s: start\n", __func__);
4408         nfs_fattr_init(&fs_locations->fattr);
4409         fs_locations->server = server;
4410         fs_locations->nlocations = 0;
4411         status = nfs4_call_sync(server, &msg, &args, &res, 0);
4412         nfs_fixup_referral_attributes(&fs_locations->fattr);
4413         dprintk("%s: returned status = %d\n", __func__, status);
4414         return status;
4415 }
4416
4417 #ifdef CONFIG_NFS_V4_1
4418 /*
4419  * nfs4_proc_exchange_id()
4420  *
4421  * Since the clientid has expired, all compounds using sessions
4422  * associated with the stale clientid will be returning
4423  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4424  * be in some phase of session reset.
4425  */
4426 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4427 {
4428         nfs4_verifier verifier;
4429         struct nfs41_exchange_id_args args = {
4430                 .client = clp,
4431                 .flags = clp->cl_exchange_flags,
4432         };
4433         struct nfs41_exchange_id_res res = {
4434                 .client = clp,
4435         };
4436         int status;
4437         struct rpc_message msg = {
4438                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4439                 .rpc_argp = &args,
4440                 .rpc_resp = &res,
4441                 .rpc_cred = cred,
4442         };
4443         __be32 *p;
4444
4445         dprintk("--> %s\n", __func__);
4446         BUG_ON(clp == NULL);
4447
4448         /* Remove server-only flags */
4449         args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4450
4451         p = (u32 *)verifier.data;
4452         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4453         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4454         args.verifier = &verifier;
4455
4456         while (1) {
4457                 args.id_len = scnprintf(args.id, sizeof(args.id),
4458                                         "%s/%s %u",
4459                                         clp->cl_ipaddr,
4460                                         rpc_peeraddr2str(clp->cl_rpcclient,
4461                                                          RPC_DISPLAY_ADDR),
4462                                         clp->cl_id_uniquifier);
4463
4464                 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4465
4466                 if (status != NFS4ERR_CLID_INUSE)
4467                         break;
4468
4469                 if (signalled())
4470                         break;
4471
4472                 if (++clp->cl_id_uniquifier == 0)
4473                         break;
4474         }
4475
4476         dprintk("<-- %s status= %d\n", __func__, status);
4477         return status;
4478 }
4479
4480 struct nfs4_get_lease_time_data {
4481         struct nfs4_get_lease_time_args *args;
4482         struct nfs4_get_lease_time_res *res;
4483         struct nfs_client *clp;
4484 };
4485
4486 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4487                                         void *calldata)
4488 {
4489         int ret;
4490         struct nfs4_get_lease_time_data *data =
4491                         (struct nfs4_get_lease_time_data *)calldata;
4492
4493         dprintk("--> %s\n", __func__);
4494         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4495         /* just setup sequence, do not trigger session recovery
4496            since we're invoked within one */
4497         ret = nfs41_setup_sequence(data->clp->cl_session,
4498                                    &data->args->la_seq_args,
4499                                    &data->res->lr_seq_res, 0, task);
4500
4501         BUG_ON(ret == -EAGAIN);
4502         rpc_call_start(task);
4503         dprintk("<-- %s\n", __func__);
4504 }
4505
4506 /*
4507  * Called from nfs4_state_manager thread for session setup, so don't recover
4508  * from sequence operation or clientid errors.
4509  */
4510 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4511 {
4512         struct nfs4_get_lease_time_data *data =
4513                         (struct nfs4_get_lease_time_data *)calldata;
4514
4515         dprintk("--> %s\n", __func__);
4516         nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4517         switch (task->tk_status) {
4518         case -NFS4ERR_DELAY:
4519         case -NFS4ERR_GRACE:
4520                 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4521                 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4522                 task->tk_status = 0;
4523                 nfs_restart_rpc(task, data->clp);
4524                 return;
4525         }
4526         dprintk("<-- %s\n", __func__);
4527 }
4528
4529 struct rpc_call_ops nfs4_get_lease_time_ops = {
4530         .rpc_call_prepare = nfs4_get_lease_time_prepare,
4531         .rpc_call_done = nfs4_get_lease_time_done,
4532 };
4533
4534 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4535 {
4536         struct rpc_task *task;
4537         struct nfs4_get_lease_time_args args;
4538         struct nfs4_get_lease_time_res res = {
4539                 .lr_fsinfo = fsinfo,
4540         };
4541         struct nfs4_get_lease_time_data data = {
4542                 .args = &args,
4543                 .res = &res,
4544                 .clp = clp,
4545         };
4546         struct rpc_message msg = {
4547                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4548                 .rpc_argp = &args,
4549                 .rpc_resp = &res,
4550         };
4551         struct rpc_task_setup task_setup = {
4552                 .rpc_client = clp->cl_rpcclient,
4553                 .rpc_message = &msg,
4554                 .callback_ops = &nfs4_get_lease_time_ops,
4555                 .callback_data = &data
4556         };
4557         int status;
4558
4559         res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4560         dprintk("--> %s\n", __func__);
4561         task = rpc_run_task(&task_setup);
4562
4563         if (IS_ERR(task))
4564                 status = PTR_ERR(task);
4565         else {
4566                 status = task->tk_status;
4567                 rpc_put_task(task);
4568         }
4569         dprintk("<-- %s return %d\n", __func__, status);
4570
4571         return status;
4572 }
4573
4574 /*
4575  * Reset a slot table
4576  */
4577 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4578                 int old_max_slots, int ivalue)
4579 {
4580         int i;
4581         int ret = 0;
4582
4583         dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
4584
4585         /*
4586          * Until we have dynamic slot table adjustment, insist
4587          * upon the same slot table size
4588          */
4589         if (max_slots != old_max_slots) {
4590                 dprintk("%s reset slot table does't match old\n",
4591                         __func__);
4592                 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4593                 goto out;
4594         }
4595         spin_lock(&tbl->slot_tbl_lock);
4596         for (i = 0; i < max_slots; ++i)
4597                 tbl->slots[i].seq_nr = ivalue;
4598         spin_unlock(&tbl->slot_tbl_lock);
4599         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4600                 tbl, tbl->slots, tbl->max_slots);
4601 out:
4602         dprintk("<-- %s: return %d\n", __func__, ret);
4603         return ret;
4604 }
4605
4606 /*
4607  * Reset the forechannel and backchannel slot tables
4608  */
4609 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4610 {
4611         int status;
4612
4613         status = nfs4_reset_slot_table(&session->fc_slot_table,
4614                         session->fc_attrs.max_reqs,
4615                         session->fc_slot_table.max_slots,
4616                         1);
4617         if (status)
4618                 return status;
4619
4620         status = nfs4_reset_slot_table(&session->bc_slot_table,
4621                         session->bc_attrs.max_reqs,
4622                         session->bc_slot_table.max_slots,
4623                         0);
4624         return status;
4625 }
4626
4627 /* Destroy the slot table */
4628 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4629 {
4630         if (session->fc_slot_table.slots != NULL) {
4631                 kfree(session->fc_slot_table.slots);
4632                 session->fc_slot_table.slots = NULL;
4633         }
4634         if (session->bc_slot_table.slots != NULL) {
4635                 kfree(session->bc_slot_table.slots);
4636                 session->bc_slot_table.slots = NULL;
4637         }
4638         return;
4639 }
4640
4641 /*
4642  * Initialize slot table
4643  */
4644 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4645                 int max_slots, int ivalue)
4646 {
4647         struct nfs4_slot *slot;
4648         int ret = -ENOMEM;
4649
4650         BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4651
4652         dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4653
4654         slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4655         if (!slot)
4656                 goto out;
4657         ret = 0;
4658
4659         spin_lock(&tbl->slot_tbl_lock);
4660         tbl->max_slots = max_slots;
4661         tbl->slots = slot;
4662         tbl->highest_used_slotid = -1;  /* no slot is currently used */
4663         spin_unlock(&tbl->slot_tbl_lock);
4664         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4665                 tbl, tbl->slots, tbl->max_slots);
4666 out:
4667         dprintk("<-- %s: return %d\n", __func__, ret);
4668         return ret;
4669 }
4670
4671 /*
4672  * Initialize the forechannel and backchannel tables
4673  */
4674 static int nfs4_init_slot_tables(struct nfs4_session *session)
4675 {
4676         struct nfs4_slot_table *tbl;
4677         int status = 0;
4678
4679         tbl = &session->fc_slot_table;
4680         if (tbl->slots == NULL) {
4681                 status = nfs4_init_slot_table(tbl,
4682                                 session->fc_attrs.max_reqs, 1);
4683                 if (status)
4684                         return status;
4685         }
4686
4687         tbl = &session->bc_slot_table;
4688         if (tbl->slots == NULL) {
4689                 status = nfs4_init_slot_table(tbl,
4690                                 session->bc_attrs.max_reqs, 0);
4691                 if (status)
4692                         nfs4_destroy_slot_tables(session);
4693         }
4694
4695         return status;
4696 }
4697
4698 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4699 {
4700         struct nfs4_session *session;
4701         struct nfs4_slot_table *tbl;
4702
4703         session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4704         if (!session)
4705                 return NULL;
4706
4707         /*
4708          * The create session reply races with the server back
4709          * channel probe. Mark the client NFS_CS_SESSION_INITING
4710          * so that the client back channel can find the
4711          * nfs_client struct
4712          */
4713         clp->cl_cons_state = NFS_CS_SESSION_INITING;
4714         init_completion(&session->complete);
4715
4716         tbl = &session->fc_slot_table;
4717         tbl->highest_used_slotid = -1;
4718         spin_lock_init(&tbl->slot_tbl_lock);
4719         rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4720
4721         tbl = &session->bc_slot_table;
4722         tbl->highest_used_slotid = -1;
4723         spin_lock_init(&tbl->slot_tbl_lock);
4724         rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4725
4726         session->clp = clp;
4727         return session;
4728 }
4729
4730 void nfs4_destroy_session(struct nfs4_session *session)
4731 {
4732         nfs4_proc_destroy_session(session);
4733         dprintk("%s Destroy backchannel for xprt %p\n",
4734                 __func__, session->clp->cl_rpcclient->cl_xprt);
4735         xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4736                                 NFS41_BC_MIN_CALLBACKS);
4737         nfs4_destroy_slot_tables(session);
4738         kfree(session);
4739 }
4740
4741 /*
4742  * Initialize the values to be used by the client in CREATE_SESSION
4743  * If nfs4_init_session set the fore channel request and response sizes,
4744  * use them.
4745  *
4746  * Set the back channel max_resp_sz_cached to zero to force the client to
4747  * always set csa_cachethis to FALSE because the current implementation
4748  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4749  */
4750 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4751 {
4752         struct nfs4_session *session = args->client->cl_session;
4753         unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4754                      mxresp_sz = session->fc_attrs.max_resp_sz;
4755
4756         if (mxrqst_sz == 0)
4757                 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4758         if (mxresp_sz == 0)
4759                 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4760         /* Fore channel attributes */
4761         args->fc_attrs.headerpadsz = 0;
4762         args->fc_attrs.max_rqst_sz = mxrqst_sz;
4763         args->fc_attrs.max_resp_sz = mxresp_sz;
4764         args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4765         args->fc_attrs.max_ops = NFS4_MAX_OPS;
4766         args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4767
4768         dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4769                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4770                 __func__,
4771                 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4772                 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4773                 args->fc_attrs.max_reqs);
4774
4775         /* Back channel attributes */
4776         args->bc_attrs.headerpadsz = 0;
4777         args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4778         args->bc_attrs.max_resp_sz = PAGE_SIZE;
4779         args->bc_attrs.max_resp_sz_cached = 0;
4780         args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4781         args->bc_attrs.max_reqs = 1;
4782
4783         dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4784                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4785                 __func__,
4786                 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4787                 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4788                 args->bc_attrs.max_reqs);
4789 }
4790
4791 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4792 {
4793         if (rcvd <= sent)
4794                 return 0;
4795         printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4796                 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4797         return -EINVAL;
4798 }
4799
4800 #define _verify_fore_channel_attr(_name_) \
4801         _verify_channel_attr("fore", #_name_, \
4802                              args->fc_attrs._name_, \
4803                              session->fc_attrs._name_)
4804
4805 #define _verify_back_channel_attr(_name_) \
4806         _verify_channel_attr("back", #_name_, \
4807                              args->bc_attrs._name_, \
4808                              session->bc_attrs._name_)
4809
4810 /*
4811  * The server is not allowed to increase the fore channel header pad size,
4812  * maximum response size, or maximum number of operations.
4813  *
4814  * The back channel attributes are only negotiatied down: We send what the
4815  * (back channel) server insists upon.
4816  */
4817 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4818                                      struct nfs4_session *session)
4819 {
4820         int ret = 0;
4821
4822         ret |= _verify_fore_channel_attr(headerpadsz);
4823         ret |= _verify_fore_channel_attr(max_resp_sz);
4824         ret |= _verify_fore_channel_attr(max_ops);
4825
4826         ret |= _verify_back_channel_attr(headerpadsz);
4827         ret |= _verify_back_channel_attr(max_rqst_sz);
4828         ret |= _verify_back_channel_attr(max_resp_sz);
4829         ret |= _verify_back_channel_attr(max_resp_sz_cached);
4830         ret |= _verify_back_channel_attr(max_ops);
4831         ret |= _verify_back_channel_attr(max_reqs);
4832
4833         return ret;
4834 }
4835
4836 static int _nfs4_proc_create_session(struct nfs_client *clp)
4837 {
4838         struct nfs4_session *session = clp->cl_session;
4839         struct nfs41_create_session_args args = {
4840                 .client = clp,
4841                 .cb_program = NFS4_CALLBACK,
4842         };
4843         struct nfs41_create_session_res res = {
4844                 .client = clp,
4845         };
4846         struct rpc_message msg = {
4847                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4848                 .rpc_argp = &args,
4849                 .rpc_resp = &res,
4850         };
4851         int status;
4852
4853         nfs4_init_channel_attrs(&args);
4854         args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4855
4856         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4857
4858         if (!status)
4859                 /* Verify the session's negotiated channel_attrs values */
4860                 status = nfs4_verify_channel_attrs(&args, session);
4861         if (!status) {
4862                 /* Increment the clientid slot sequence id */
4863                 clp->cl_seqid++;
4864         }
4865
4866         return status;
4867 }
4868
4869 /*
4870  * Issues a CREATE_SESSION operation to the server.
4871  * It is the responsibility of the caller to verify the session is
4872  * expired before calling this routine.
4873  */
4874 int nfs4_proc_create_session(struct nfs_client *clp)
4875 {
4876         int status;
4877         unsigned *ptr;
4878         struct nfs4_session *session = clp->cl_session;
4879
4880         dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4881
4882         status = _nfs4_proc_create_session(clp);
4883         if (status)
4884                 goto out;
4885
4886         /* Init and reset the fore channel */
4887         status = nfs4_init_slot_tables(session);
4888         dprintk("slot table initialization returned %d\n", status);
4889         if (status)
4890                 goto out;
4891         status = nfs4_reset_slot_tables(session);
4892         dprintk("slot table reset returned %d\n", status);
4893         if (status)
4894                 goto out;
4895
4896         ptr = (unsigned *)&session->sess_id.data[0];
4897         dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4898                 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4899 out:
4900         dprintk("<-- %s\n", __func__);
4901         return status;
4902 }
4903
4904 /*
4905  * Issue the over-the-wire RPC DESTROY_SESSION.
4906  * The caller must serialize access to this routine.
4907  */
4908 int nfs4_proc_destroy_session(struct nfs4_session *session)
4909 {
4910         int status = 0;
4911         struct rpc_message msg;
4912
4913         dprintk("--> nfs4_proc_destroy_session\n");
4914
4915         /* session is still being setup */
4916         if (session->clp->cl_cons_state != NFS_CS_READY)
4917                 return status;
4918
4919         msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4920         msg.rpc_argp = session;
4921         msg.rpc_resp = NULL;
4922         msg.rpc_cred = NULL;
4923         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4924
4925         if (status)
4926                 printk(KERN_WARNING
4927                         "Got error %d from the server on DESTROY_SESSION. "
4928                         "Session has been destroyed regardless...\n", status);
4929
4930         dprintk("<-- nfs4_proc_destroy_session\n");
4931         return status;
4932 }
4933
4934 int nfs4_init_session(struct nfs_server *server)
4935 {
4936         struct nfs_client *clp = server->nfs_client;
4937         struct nfs4_session *session;
4938         int ret;
4939
4940         if (!nfs4_has_session(clp))
4941                 return 0;
4942
4943         session = clp->cl_session;
4944         session->fc_attrs.max_rqst_sz = server->wsize + nfs41_maxwrite_overhead;
4945         session->fc_attrs.max_resp_sz = server->rsize + nfs41_maxread_overhead;
4946
4947         ret = nfs4_recover_expired_lease(server);
4948         if (!ret)
4949                 ret = nfs4_check_client_ready(clp);
4950         return ret;
4951 }
4952
4953 /*
4954  * Renew the cl_session lease.
4955  */
4956 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4957 {
4958         struct nfs4_sequence_args args;
4959         struct nfs4_sequence_res res;
4960
4961         struct rpc_message msg = {
4962                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4963                 .rpc_argp = &args,
4964                 .rpc_resp = &res,
4965                 .rpc_cred = cred,
4966         };
4967
4968         args.sa_cache_this = 0;
4969
4970         return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4971                                        &res, args.sa_cache_this, 1);
4972 }
4973
4974 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4975 {
4976         struct nfs_client *clp = (struct nfs_client *)data;
4977
4978         nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4979
4980         if (task->tk_status < 0) {
4981                 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4982
4983                 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4984                                                                 == -EAGAIN) {
4985                         nfs_restart_rpc(task, clp);
4986                         return;
4987                 }
4988         }
4989         dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4990
4991         kfree(task->tk_msg.rpc_argp);
4992         kfree(task->tk_msg.rpc_resp);
4993
4994         dprintk("<-- %s\n", __func__);
4995 }
4996
4997 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4998 {
4999         struct nfs_client *clp;
5000         struct nfs4_sequence_args *args;
5001         struct nfs4_sequence_res *res;
5002
5003         clp = (struct nfs_client *)data;
5004         args = task->tk_msg.rpc_argp;
5005         res = task->tk_msg.rpc_resp;
5006
5007         if (nfs4_setup_sequence(clp, args, res, 0, task))
5008                 return;
5009         rpc_call_start(task);
5010 }
5011
5012 static const struct rpc_call_ops nfs41_sequence_ops = {
5013         .rpc_call_done = nfs41_sequence_call_done,
5014         .rpc_call_prepare = nfs41_sequence_prepare,
5015 };
5016
5017 static int nfs41_proc_async_sequence(struct nfs_client *clp,
5018                                      struct rpc_cred *cred)
5019 {
5020         struct nfs4_sequence_args *args;
5021         struct nfs4_sequence_res *res;
5022         struct rpc_message msg = {
5023                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5024                 .rpc_cred = cred,
5025         };
5026
5027         args = kzalloc(sizeof(*args), GFP_KERNEL);
5028         if (!args)
5029                 return -ENOMEM;
5030         res = kzalloc(sizeof(*res), GFP_KERNEL);
5031         if (!res) {
5032                 kfree(args);
5033                 return -ENOMEM;
5034         }
5035         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5036         msg.rpc_argp = args;
5037         msg.rpc_resp = res;
5038
5039         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5040                               &nfs41_sequence_ops, (void *)clp);
5041 }
5042
5043 struct nfs4_reclaim_complete_data {
5044         struct nfs_client *clp;
5045         struct nfs41_reclaim_complete_args arg;
5046         struct nfs41_reclaim_complete_res res;
5047 };
5048
5049 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5050 {
5051         struct nfs4_reclaim_complete_data *calldata = data;
5052
5053         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5054         if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5055                                 &calldata->res.seq_res, 0, task))
5056                 return;
5057
5058         rpc_call_start(task);
5059 }
5060
5061 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5062 {
5063         struct nfs4_reclaim_complete_data *calldata = data;
5064         struct nfs_client *clp = calldata->clp;
5065         struct nfs4_sequence_res *res = &calldata->res.seq_res;
5066
5067         dprintk("--> %s\n", __func__);
5068         nfs41_sequence_done(clp, res, task->tk_status);
5069         switch (task->tk_status) {
5070         case 0:
5071         case -NFS4ERR_COMPLETE_ALREADY:
5072                 break;
5073         case -NFS4ERR_BADSESSION:
5074         case -NFS4ERR_DEADSESSION:
5075                 /*
5076                  * Handle the session error, but do not retry the operation, as
5077                  * we have no way of telling whether the clientid had to be
5078                  * reset before we got our reply.  If reset, a new wave of
5079                  * reclaim operations will follow, containing their own reclaim
5080                  * complete.  We don't want our retry to get on the way of
5081                  * recovery by incorrectly indicating to the server that we're
5082                  * done reclaiming state since the process had to be restarted.
5083                  */
5084                 _nfs4_async_handle_error(task, NULL, clp, NULL);
5085                 break;
5086         default:
5087                 if (_nfs4_async_handle_error(
5088                                 task, NULL, clp, NULL) == -EAGAIN) {
5089                         rpc_restart_call_prepare(task);
5090                         return;
5091                 }
5092         }
5093
5094         dprintk("<-- %s\n", __func__);
5095 }
5096
5097 static void nfs4_free_reclaim_complete_data(void *data)
5098 {
5099         struct nfs4_reclaim_complete_data *calldata = data;
5100
5101         kfree(calldata);
5102 }
5103
5104 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5105         .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5106         .rpc_call_done = nfs4_reclaim_complete_done,
5107         .rpc_release = nfs4_free_reclaim_complete_data,
5108 };
5109
5110 /*
5111  * Issue a global reclaim complete.
5112  */
5113 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5114 {
5115         struct nfs4_reclaim_complete_data *calldata;
5116         struct rpc_task *task;
5117         struct rpc_message msg = {
5118                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5119         };
5120         struct rpc_task_setup task_setup_data = {
5121                 .rpc_client = clp->cl_rpcclient,
5122                 .rpc_message = &msg,
5123                 .callback_ops = &nfs4_reclaim_complete_call_ops,
5124                 .flags = RPC_TASK_ASYNC,
5125         };
5126         int status = -ENOMEM;
5127
5128         dprintk("--> %s\n", __func__);
5129         calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5130         if (calldata == NULL)
5131                 goto out;
5132         calldata->clp = clp;
5133         calldata->arg.one_fs = 0;
5134         calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5135
5136         msg.rpc_argp = &calldata->arg;
5137         msg.rpc_resp = &calldata->res;
5138         task_setup_data.callback_data = calldata;
5139         task = rpc_run_task(&task_setup_data);
5140         if (IS_ERR(task))
5141                 status = PTR_ERR(task);
5142         rpc_put_task(task);
5143 out:
5144         dprintk("<-- %s status=%d\n", __func__, status);
5145         return status;
5146 }
5147 #endif /* CONFIG_NFS_V4_1 */
5148
5149 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5150         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5151         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5152         .recover_open   = nfs4_open_reclaim,
5153         .recover_lock   = nfs4_lock_reclaim,
5154         .establish_clid = nfs4_init_clientid,
5155         .get_clid_cred  = nfs4_get_setclientid_cred,
5156 };
5157
5158 #if defined(CONFIG_NFS_V4_1)
5159 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5160         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5161         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5162         .recover_open   = nfs4_open_reclaim,
5163         .recover_lock   = nfs4_lock_reclaim,
5164         .establish_clid = nfs41_init_clientid,
5165         .get_clid_cred  = nfs4_get_exchange_id_cred,
5166         .reclaim_complete = nfs41_proc_reclaim_complete,
5167 };
5168 #endif /* CONFIG_NFS_V4_1 */
5169
5170 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5171         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5172         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5173         .recover_open   = nfs4_open_expired,
5174         .recover_lock   = nfs4_lock_expired,
5175         .establish_clid = nfs4_init_clientid,
5176         .get_clid_cred  = nfs4_get_setclientid_cred,
5177 };
5178
5179 #if defined(CONFIG_NFS_V4_1)
5180 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5181         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5182         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5183         .recover_open   = nfs4_open_expired,
5184         .recover_lock   = nfs4_lock_expired,
5185         .establish_clid = nfs41_init_clientid,
5186         .get_clid_cred  = nfs4_get_exchange_id_cred,
5187 };
5188 #endif /* CONFIG_NFS_V4_1 */
5189
5190 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5191         .sched_state_renewal = nfs4_proc_async_renew,
5192         .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5193         .renew_lease = nfs4_proc_renew,
5194 };
5195
5196 #if defined(CONFIG_NFS_V4_1)
5197 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5198         .sched_state_renewal = nfs41_proc_async_sequence,
5199         .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5200         .renew_lease = nfs4_proc_sequence,
5201 };
5202 #endif
5203
5204 /*
5205  * Per minor version reboot and network partition recovery ops
5206  */
5207
5208 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5209         &nfs40_reboot_recovery_ops,
5210 #if defined(CONFIG_NFS_V4_1)
5211         &nfs41_reboot_recovery_ops,
5212 #endif
5213 };
5214
5215 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5216         &nfs40_nograce_recovery_ops,
5217 #if defined(CONFIG_NFS_V4_1)
5218         &nfs41_nograce_recovery_ops,
5219 #endif
5220 };
5221
5222 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5223         &nfs40_state_renewal_ops,
5224 #if defined(CONFIG_NFS_V4_1)
5225         &nfs41_state_renewal_ops,
5226 #endif
5227 };
5228
5229 static const struct inode_operations nfs4_file_inode_operations = {
5230         .permission     = nfs_permission,
5231         .getattr        = nfs_getattr,
5232         .setattr        = nfs_setattr,
5233         .getxattr       = nfs4_getxattr,
5234         .setxattr       = nfs4_setxattr,
5235         .listxattr      = nfs4_listxattr,
5236 };
5237
5238 const struct nfs_rpc_ops nfs_v4_clientops = {
5239         .version        = 4,                    /* protocol version */
5240         .dentry_ops     = &nfs4_dentry_operations,
5241         .dir_inode_ops  = &nfs4_dir_inode_operations,
5242         .file_inode_ops = &nfs4_file_inode_operations,
5243         .getroot        = nfs4_proc_get_root,
5244         .getattr        = nfs4_proc_getattr,
5245         .setattr        = nfs4_proc_setattr,
5246         .lookupfh       = nfs4_proc_lookupfh,
5247         .lookup         = nfs4_proc_lookup,
5248         .access         = nfs4_proc_access,
5249         .readlink       = nfs4_proc_readlink,
5250         .create         = nfs4_proc_create,
5251         .remove         = nfs4_proc_remove,
5252         .unlink_setup   = nfs4_proc_unlink_setup,
5253         .unlink_done    = nfs4_proc_unlink_done,
5254         .rename         = nfs4_proc_rename,
5255         .link           = nfs4_proc_link,
5256         .symlink        = nfs4_proc_symlink,
5257         .mkdir          = nfs4_proc_mkdir,
5258         .rmdir          = nfs4_proc_remove,
5259         .readdir        = nfs4_proc_readdir,
5260         .mknod          = nfs4_proc_mknod,
5261         .statfs         = nfs4_proc_statfs,
5262         .fsinfo         = nfs4_proc_fsinfo,
5263         .pathconf       = nfs4_proc_pathconf,
5264         .set_capabilities = nfs4_server_capabilities,
5265         .decode_dirent  = nfs4_decode_dirent,
5266         .read_setup     = nfs4_proc_read_setup,
5267         .read_done      = nfs4_read_done,
5268         .write_setup    = nfs4_proc_write_setup,
5269         .write_done     = nfs4_write_done,
5270         .commit_setup   = nfs4_proc_commit_setup,
5271         .commit_done    = nfs4_commit_done,
5272         .lock           = nfs4_proc_lock,
5273         .clear_acl_cache = nfs4_zap_acl_attr,
5274         .close_context  = nfs4_close_context,
5275 };
5276
5277 /*
5278  * Local variables:
5279  *  c-basic-offset: 8
5280  * End:
5281  */