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