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