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