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