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