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