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