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