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