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