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