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