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