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