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