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