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