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