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