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