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