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