spnego: add missing OID to oid registry
[sfrench/cifs-2.6.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 {
2635         struct nfs_access_entry cache;
2636         u32 mask, flags;
2637
2638         /* access call failed or for some reason the server doesn't
2639          * support any access modes -- defer access call until later */
2640         if (opendata->o_res.access_supported == 0)
2641                 return 0;
2642
2643         mask = 0;
2644         if (fmode & FMODE_EXEC) {
2645                 /* ONLY check for exec rights */
2646                 if (S_ISDIR(state->inode->i_mode))
2647                         mask = NFS4_ACCESS_LOOKUP;
2648                 else
2649                         mask = NFS4_ACCESS_EXECUTE;
2650         } else if ((fmode & FMODE_READ) && !opendata->file_created)
2651                 mask = NFS4_ACCESS_READ;
2652
2653         nfs_access_set_mask(&cache, opendata->o_res.access_result);
2654         nfs_access_add_cache(state->inode, &cache, cred);
2655
2656         flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2657         if ((mask & ~cache.mask & flags) == 0)
2658                 return 0;
2659
2660         return -EACCES;
2661 }
2662
2663 /*
2664  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2665  */
2666 static int _nfs4_proc_open(struct nfs4_opendata *data,
2667                            struct nfs_open_context *ctx)
2668 {
2669         struct inode *dir = d_inode(data->dir);
2670         struct nfs_server *server = NFS_SERVER(dir);
2671         struct nfs_openargs *o_arg = &data->o_arg;
2672         struct nfs_openres *o_res = &data->o_res;
2673         int status;
2674
2675         status = nfs4_run_open_task(data, ctx);
2676         if (!data->rpc_done)
2677                 return status;
2678         if (status != 0) {
2679                 if (status == -NFS4ERR_BADNAME &&
2680                                 !(o_arg->open_flags & O_CREAT))
2681                         return -ENOENT;
2682                 return status;
2683         }
2684
2685         nfs_fattr_map_and_free_names(server, &data->f_attr);
2686
2687         if (o_arg->open_flags & O_CREAT) {
2688                 if (o_arg->open_flags & O_EXCL)
2689                         data->file_created = true;
2690                 else if (o_res->cinfo.before != o_res->cinfo.after)
2691                         data->file_created = true;
2692                 if (data->file_created ||
2693                     inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2694                         nfs4_update_changeattr(dir, &o_res->cinfo,
2695                                         o_res->f_attr->time_start,
2696                                         NFS_INO_INVALID_DATA);
2697         }
2698         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2699                 server->caps &= ~NFS_CAP_POSIX_LOCK;
2700         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2701                 status = _nfs4_proc_open_confirm(data);
2702                 if (status != 0)
2703                         return status;
2704         }
2705         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2706                 nfs4_sequence_free_slot(&o_res->seq_res);
2707                 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
2708         }
2709         return 0;
2710 }
2711
2712 /*
2713  * OPEN_EXPIRED:
2714  *      reclaim state on the server after a network partition.
2715  *      Assumes caller holds the appropriate lock
2716  */
2717 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2718 {
2719         struct nfs4_opendata *opendata;
2720         int ret;
2721
2722         opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2723         if (IS_ERR(opendata))
2724                 return PTR_ERR(opendata);
2725         /*
2726          * We're not recovering a delegation, so ask for no delegation.
2727          * Otherwise the recovery thread could deadlock with an outstanding
2728          * delegation return.
2729          */
2730         opendata->o_arg.open_flags = O_DIRECT;
2731         ret = nfs4_open_recover(opendata, state);
2732         if (ret == -ESTALE)
2733                 d_drop(ctx->dentry);
2734         nfs4_opendata_put(opendata);
2735         return ret;
2736 }
2737
2738 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2739 {
2740         struct nfs_server *server = NFS_SERVER(state->inode);
2741         struct nfs4_exception exception = { };
2742         int err;
2743
2744         do {
2745                 err = _nfs4_open_expired(ctx, state);
2746                 trace_nfs4_open_expired(ctx, 0, err);
2747                 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2748                         continue;
2749                 switch (err) {
2750                 default:
2751                         goto out;
2752                 case -NFS4ERR_GRACE:
2753                 case -NFS4ERR_DELAY:
2754                         nfs4_handle_exception(server, err, &exception);
2755                         err = 0;
2756                 }
2757         } while (exception.retry);
2758 out:
2759         return err;
2760 }
2761
2762 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2763 {
2764         struct nfs_open_context *ctx;
2765         int ret;
2766
2767         ctx = nfs4_state_find_open_context(state);
2768         if (IS_ERR(ctx))
2769                 return -EAGAIN;
2770         ret = nfs4_do_open_expired(ctx, state);
2771         put_nfs_open_context(ctx);
2772         return ret;
2773 }
2774
2775 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2776                 const nfs4_stateid *stateid)
2777 {
2778         nfs_remove_bad_delegation(state->inode, stateid);
2779         nfs_state_clear_delegation(state);
2780 }
2781
2782 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2783 {
2784         if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2785                 nfs_finish_clear_delegation_stateid(state, NULL);
2786 }
2787
2788 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2789 {
2790         /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2791         nfs40_clear_delegation_stateid(state);
2792         nfs_state_clear_open_state_flags(state);
2793         return nfs4_open_expired(sp, state);
2794 }
2795
2796 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2797                 nfs4_stateid *stateid,
2798                 const struct cred *cred)
2799 {
2800         return -NFS4ERR_BAD_STATEID;
2801 }
2802
2803 #if defined(CONFIG_NFS_V4_1)
2804 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2805                 nfs4_stateid *stateid,
2806                 const struct cred *cred)
2807 {
2808         int status;
2809
2810         switch (stateid->type) {
2811         default:
2812                 break;
2813         case NFS4_INVALID_STATEID_TYPE:
2814         case NFS4_SPECIAL_STATEID_TYPE:
2815                 return -NFS4ERR_BAD_STATEID;
2816         case NFS4_REVOKED_STATEID_TYPE:
2817                 goto out_free;
2818         }
2819
2820         status = nfs41_test_stateid(server, stateid, cred);
2821         switch (status) {
2822         case -NFS4ERR_EXPIRED:
2823         case -NFS4ERR_ADMIN_REVOKED:
2824         case -NFS4ERR_DELEG_REVOKED:
2825                 break;
2826         default:
2827                 return status;
2828         }
2829 out_free:
2830         /* Ack the revoked state to the server */
2831         nfs41_free_stateid(server, stateid, cred, true);
2832         return -NFS4ERR_EXPIRED;
2833 }
2834
2835 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2836 {
2837         struct nfs_server *server = NFS_SERVER(state->inode);
2838         nfs4_stateid stateid;
2839         struct nfs_delegation *delegation;
2840         const struct cred *cred = NULL;
2841         int status, ret = NFS_OK;
2842
2843         /* Get the delegation credential for use by test/free_stateid */
2844         rcu_read_lock();
2845         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2846         if (delegation == NULL) {
2847                 rcu_read_unlock();
2848                 nfs_state_clear_delegation(state);
2849                 return NFS_OK;
2850         }
2851
2852         spin_lock(&delegation->lock);
2853         nfs4_stateid_copy(&stateid, &delegation->stateid);
2854
2855         if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2856                                 &delegation->flags)) {
2857                 spin_unlock(&delegation->lock);
2858                 rcu_read_unlock();
2859                 return NFS_OK;
2860         }
2861
2862         if (delegation->cred)
2863                 cred = get_cred(delegation->cred);
2864         spin_unlock(&delegation->lock);
2865         rcu_read_unlock();
2866         status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2867         trace_nfs4_test_delegation_stateid(state, NULL, status);
2868         if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2869                 nfs_finish_clear_delegation_stateid(state, &stateid);
2870         else
2871                 ret = status;
2872
2873         put_cred(cred);
2874         return ret;
2875 }
2876
2877 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2878 {
2879         nfs4_stateid tmp;
2880
2881         if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2882             nfs4_copy_delegation_stateid(state->inode, state->state,
2883                                 &tmp, NULL) &&
2884             nfs4_stateid_match_other(&state->stateid, &tmp))
2885                 nfs_state_set_delegation(state, &tmp, state->state);
2886         else
2887                 nfs_state_clear_delegation(state);
2888 }
2889
2890 /**
2891  * nfs41_check_expired_locks - possibly free a lock stateid
2892  *
2893  * @state: NFSv4 state for an inode
2894  *
2895  * Returns NFS_OK if recovery for this stateid is now finished.
2896  * Otherwise a negative NFS4ERR value is returned.
2897  */
2898 static int nfs41_check_expired_locks(struct nfs4_state *state)
2899 {
2900         int status, ret = NFS_OK;
2901         struct nfs4_lock_state *lsp, *prev = NULL;
2902         struct nfs_server *server = NFS_SERVER(state->inode);
2903
2904         if (!test_bit(LK_STATE_IN_USE, &state->flags))
2905                 goto out;
2906
2907         spin_lock(&state->state_lock);
2908         list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2909                 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2910                         const struct cred *cred = lsp->ls_state->owner->so_cred;
2911
2912                         refcount_inc(&lsp->ls_count);
2913                         spin_unlock(&state->state_lock);
2914
2915                         nfs4_put_lock_state(prev);
2916                         prev = lsp;
2917
2918                         status = nfs41_test_and_free_expired_stateid(server,
2919                                         &lsp->ls_stateid,
2920                                         cred);
2921                         trace_nfs4_test_lock_stateid(state, lsp, status);
2922                         if (status == -NFS4ERR_EXPIRED ||
2923                             status == -NFS4ERR_BAD_STATEID) {
2924                                 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2925                                 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2926                                 if (!recover_lost_locks)
2927                                         set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2928                         } else if (status != NFS_OK) {
2929                                 ret = status;
2930                                 nfs4_put_lock_state(prev);
2931                                 goto out;
2932                         }
2933                         spin_lock(&state->state_lock);
2934                 }
2935         }
2936         spin_unlock(&state->state_lock);
2937         nfs4_put_lock_state(prev);
2938 out:
2939         return ret;
2940 }
2941
2942 /**
2943  * nfs41_check_open_stateid - possibly free an open stateid
2944  *
2945  * @state: NFSv4 state for an inode
2946  *
2947  * Returns NFS_OK if recovery for this stateid is now finished.
2948  * Otherwise a negative NFS4ERR value is returned.
2949  */
2950 static int nfs41_check_open_stateid(struct nfs4_state *state)
2951 {
2952         struct nfs_server *server = NFS_SERVER(state->inode);
2953         nfs4_stateid *stateid = &state->open_stateid;
2954         const struct cred *cred = state->owner->so_cred;
2955         int status;
2956
2957         if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2958                 return -NFS4ERR_BAD_STATEID;
2959         status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2960         trace_nfs4_test_open_stateid(state, NULL, status);
2961         if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2962                 nfs_state_clear_open_state_flags(state);
2963                 stateid->type = NFS4_INVALID_STATEID_TYPE;
2964                 return status;
2965         }
2966         if (nfs_open_stateid_recover_openmode(state))
2967                 return -NFS4ERR_OPENMODE;
2968         return NFS_OK;
2969 }
2970
2971 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2972 {
2973         int status;
2974
2975         status = nfs41_check_delegation_stateid(state);
2976         if (status != NFS_OK)
2977                 return status;
2978         nfs41_delegation_recover_stateid(state);
2979
2980         status = nfs41_check_expired_locks(state);
2981         if (status != NFS_OK)
2982                 return status;
2983         status = nfs41_check_open_stateid(state);
2984         if (status != NFS_OK)
2985                 status = nfs4_open_expired(sp, state);
2986         return status;
2987 }
2988 #endif
2989
2990 /*
2991  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2992  * fields corresponding to attributes that were used to store the verifier.
2993  * Make sure we clobber those fields in the later setattr call
2994  */
2995 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2996                                 struct iattr *sattr, struct nfs4_label **label)
2997 {
2998         const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2999         __u32 attrset[3];
3000         unsigned ret;
3001         unsigned i;
3002
3003         for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3004                 attrset[i] = opendata->o_res.attrset[i];
3005                 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3006                         attrset[i] &= ~bitmask[i];
3007         }
3008
3009         ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3010                 sattr->ia_valid : 0;
3011
3012         if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3013                 if (sattr->ia_valid & ATTR_ATIME_SET)
3014                         ret |= ATTR_ATIME_SET;
3015                 else
3016                         ret |= ATTR_ATIME;
3017         }
3018
3019         if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3020                 if (sattr->ia_valid & ATTR_MTIME_SET)
3021                         ret |= ATTR_MTIME_SET;
3022                 else
3023                         ret |= ATTR_MTIME;
3024         }
3025
3026         if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3027                 *label = NULL;
3028         return ret;
3029 }
3030
3031 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3032                 struct nfs_open_context *ctx)
3033 {
3034         struct nfs4_state_owner *sp = opendata->owner;
3035         struct nfs_server *server = sp->so_server;
3036         struct dentry *dentry;
3037         struct nfs4_state *state;
3038         fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3039         struct inode *dir = d_inode(opendata->dir);
3040         unsigned long dir_verifier;
3041         unsigned int seq;
3042         int ret;
3043
3044         seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3045         dir_verifier = nfs_save_change_attribute(dir);
3046
3047         ret = _nfs4_proc_open(opendata, ctx);
3048         if (ret != 0)
3049                 goto out;
3050
3051         state = _nfs4_opendata_to_nfs4_state(opendata);
3052         ret = PTR_ERR(state);
3053         if (IS_ERR(state))
3054                 goto out;
3055         ctx->state = state;
3056         if (server->caps & NFS_CAP_POSIX_LOCK)
3057                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3058         if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3059                 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3060         if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3061                 set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3062
3063         dentry = opendata->dentry;
3064         if (d_really_is_negative(dentry)) {
3065                 struct dentry *alias;
3066                 d_drop(dentry);
3067                 alias = d_exact_alias(dentry, state->inode);
3068                 if (!alias)
3069                         alias = d_splice_alias(igrab(state->inode), dentry);
3070                 /* d_splice_alias() can't fail here - it's a non-directory */
3071                 if (alias) {
3072                         dput(ctx->dentry);
3073                         ctx->dentry = dentry = alias;
3074                 }
3075         }
3076
3077         switch(opendata->o_arg.claim) {
3078         default:
3079                 break;
3080         case NFS4_OPEN_CLAIM_NULL:
3081         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3082         case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3083                 if (!opendata->rpc_done)
3084                         break;
3085                 if (opendata->o_res.delegation_type != 0)
3086                         dir_verifier = nfs_save_change_attribute(dir);
3087                 nfs_set_verifier(dentry, dir_verifier);
3088         }
3089
3090         /* Parse layoutget results before we check for access */
3091         pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3092
3093         ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
3094         if (ret != 0)
3095                 goto out;
3096
3097         if (d_inode(dentry) == state->inode) {
3098                 nfs_inode_attach_open_context(ctx);
3099                 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3100                         nfs4_schedule_stateid_recovery(server, state);
3101         }
3102
3103 out:
3104         if (!opendata->cancelled) {
3105                 if (opendata->lgp) {
3106                         nfs4_lgopen_release(opendata->lgp);
3107                         opendata->lgp = NULL;
3108                 }
3109                 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3110         }
3111         return ret;
3112 }
3113
3114 /*
3115  * Returns a referenced nfs4_state
3116  */
3117 static int _nfs4_do_open(struct inode *dir,
3118                         struct nfs_open_context *ctx,
3119                         int flags,
3120                         const struct nfs4_open_createattrs *c,
3121                         int *opened)
3122 {
3123         struct nfs4_state_owner  *sp;
3124         struct nfs4_state     *state = NULL;
3125         struct nfs_server       *server = NFS_SERVER(dir);
3126         struct nfs4_opendata *opendata;
3127         struct dentry *dentry = ctx->dentry;
3128         const struct cred *cred = ctx->cred;
3129         struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3130         fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3131         enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3132         struct iattr *sattr = c->sattr;
3133         struct nfs4_label *label = c->label;
3134         int status;
3135
3136         /* Protect against reboot recovery conflicts */
3137         status = -ENOMEM;
3138         sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3139         if (sp == NULL) {
3140                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3141                 goto out_err;
3142         }
3143         status = nfs4_client_recover_expired_lease(server->nfs_client);
3144         if (status != 0)
3145                 goto err_put_state_owner;
3146         if (d_really_is_positive(dentry))
3147                 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3148         status = -ENOMEM;
3149         if (d_really_is_positive(dentry))
3150                 claim = NFS4_OPEN_CLAIM_FH;
3151         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3152                         c, claim, GFP_KERNEL);
3153         if (opendata == NULL)
3154                 goto err_put_state_owner;
3155
3156         if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3157                 if (!opendata->f_attr.mdsthreshold) {
3158                         opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3159                         if (!opendata->f_attr.mdsthreshold)
3160                                 goto err_opendata_put;
3161                 }
3162                 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3163         }
3164         if (d_really_is_positive(dentry))
3165                 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3166
3167         status = _nfs4_open_and_get_state(opendata, ctx);
3168         if (status != 0)
3169                 goto err_opendata_put;
3170         state = ctx->state;
3171
3172         if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3173             (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3174                 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3175                 /*
3176                  * send create attributes which was not set by open
3177                  * with an extra setattr.
3178                  */
3179                 if (attrs || label) {
3180                         unsigned ia_old = sattr->ia_valid;
3181
3182                         sattr->ia_valid = attrs;
3183                         nfs_fattr_init(opendata->o_res.f_attr);
3184                         status = nfs4_do_setattr(state->inode, cred,
3185                                         opendata->o_res.f_attr, sattr,
3186                                         ctx, label);
3187                         if (status == 0) {
3188                                 nfs_setattr_update_inode(state->inode, sattr,
3189                                                 opendata->o_res.f_attr);
3190                                 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3191                         }
3192                         sattr->ia_valid = ia_old;
3193                 }
3194         }
3195         if (opened && opendata->file_created)
3196                 *opened = 1;
3197
3198         if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3199                 *ctx_th = opendata->f_attr.mdsthreshold;
3200                 opendata->f_attr.mdsthreshold = NULL;
3201         }
3202
3203         nfs4_opendata_put(opendata);
3204         nfs4_put_state_owner(sp);
3205         return 0;
3206 err_opendata_put:
3207         nfs4_opendata_put(opendata);
3208 err_put_state_owner:
3209         nfs4_put_state_owner(sp);
3210 out_err:
3211         return status;
3212 }
3213
3214
3215 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3216                                         struct nfs_open_context *ctx,
3217                                         int flags,
3218                                         struct iattr *sattr,
3219                                         struct nfs4_label *label,
3220                                         int *opened)
3221 {
3222         struct nfs_server *server = NFS_SERVER(dir);
3223         struct nfs4_exception exception = {
3224                 .interruptible = true,
3225         };
3226         struct nfs4_state *res;
3227         struct nfs4_open_createattrs c = {
3228                 .label = label,
3229                 .sattr = sattr,
3230                 .verf = {
3231                         [0] = (__u32)jiffies,
3232                         [1] = (__u32)current->pid,
3233                 },
3234         };
3235         int status;
3236
3237         do {
3238                 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3239                 res = ctx->state;
3240                 trace_nfs4_open_file(ctx, flags, status);
3241                 if (status == 0)
3242                         break;
3243                 /* NOTE: BAD_SEQID means the server and client disagree about the
3244                  * book-keeping w.r.t. state-changing operations
3245                  * (OPEN/CLOSE/LOCK/LOCKU...)
3246                  * It is actually a sign of a bug on the client or on the server.
3247                  *
3248                  * If we receive a BAD_SEQID error in the particular case of
3249                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
3250                  * have unhashed the old state_owner for us, and that we can
3251                  * therefore safely retry using a new one. We should still warn
3252                  * the user though...
3253                  */
3254                 if (status == -NFS4ERR_BAD_SEQID) {
3255                         pr_warn_ratelimited("NFS: v4 server %s "
3256                                         " returned a bad sequence-id error!\n",
3257                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
3258                         exception.retry = 1;
3259                         continue;
3260                 }
3261                 /*
3262                  * BAD_STATEID on OPEN means that the server cancelled our
3263                  * state before it received the OPEN_CONFIRM.
3264                  * Recover by retrying the request as per the discussion
3265                  * on Page 181 of RFC3530.
3266                  */
3267                 if (status == -NFS4ERR_BAD_STATEID) {
3268                         exception.retry = 1;
3269                         continue;
3270                 }
3271                 if (status == -NFS4ERR_EXPIRED) {
3272                         nfs4_schedule_lease_recovery(server->nfs_client);
3273                         exception.retry = 1;
3274                         continue;
3275                 }
3276                 if (status == -EAGAIN) {
3277                         /* We must have found a delegation */
3278                         exception.retry = 1;
3279                         continue;
3280                 }
3281                 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3282                         continue;
3283                 res = ERR_PTR(nfs4_handle_exception(server,
3284                                         status, &exception));
3285         } while (exception.retry);
3286         return res;
3287 }
3288
3289 static int _nfs4_do_setattr(struct inode *inode,
3290                             struct nfs_setattrargs *arg,
3291                             struct nfs_setattrres *res,
3292                             const struct cred *cred,
3293                             struct nfs_open_context *ctx)
3294 {
3295         struct nfs_server *server = NFS_SERVER(inode);
3296         struct rpc_message msg = {
3297                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3298                 .rpc_argp       = arg,
3299                 .rpc_resp       = res,
3300                 .rpc_cred       = cred,
3301         };
3302         const struct cred *delegation_cred = NULL;
3303         unsigned long timestamp = jiffies;
3304         bool truncate;
3305         int status;
3306
3307         nfs_fattr_init(res->fattr);
3308
3309         /* Servers should only apply open mode checks for file size changes */
3310         truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3311         if (!truncate) {
3312                 nfs4_inode_make_writeable(inode);
3313                 goto zero_stateid;
3314         }
3315
3316         if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3317                 /* Use that stateid */
3318         } else if (ctx != NULL && ctx->state) {
3319                 struct nfs_lock_context *l_ctx;
3320                 if (!nfs4_valid_open_stateid(ctx->state))
3321                         return -EBADF;
3322                 l_ctx = nfs_get_lock_context(ctx);
3323                 if (IS_ERR(l_ctx))
3324                         return PTR_ERR(l_ctx);
3325                 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3326                                                 &arg->stateid, &delegation_cred);
3327                 nfs_put_lock_context(l_ctx);
3328                 if (status == -EIO)
3329                         return -EBADF;
3330                 else if (status == -EAGAIN)
3331                         goto zero_stateid;
3332         } else {
3333 zero_stateid:
3334                 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3335         }
3336         if (delegation_cred)
3337                 msg.rpc_cred = delegation_cred;
3338
3339         status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3340
3341         put_cred(delegation_cred);
3342         if (status == 0 && ctx != NULL)
3343                 renew_lease(server, timestamp);
3344         trace_nfs4_setattr(inode, &arg->stateid, status);
3345         return status;
3346 }
3347
3348 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3349                            struct nfs_fattr *fattr, struct iattr *sattr,
3350                            struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3351 {
3352         struct nfs_server *server = NFS_SERVER(inode);
3353         __u32 bitmask[NFS4_BITMASK_SZ];
3354         struct nfs4_state *state = ctx ? ctx->state : NULL;
3355         struct nfs_setattrargs  arg = {
3356                 .fh             = NFS_FH(inode),
3357                 .iap            = sattr,
3358                 .server         = server,
3359                 .bitmask = bitmask,
3360                 .label          = ilabel,
3361         };
3362         struct nfs_setattrres  res = {
3363                 .fattr          = fattr,
3364                 .server         = server,
3365         };
3366         struct nfs4_exception exception = {
3367                 .state = state,
3368                 .inode = inode,
3369                 .stateid = &arg.stateid,
3370         };
3371         unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3372         int err;
3373
3374         if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3375                 adjust_flags |= NFS_INO_INVALID_MODE;
3376         if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3377                 adjust_flags |= NFS_INO_INVALID_OTHER;
3378
3379         do {
3380                 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3381                                         inode, adjust_flags);
3382
3383                 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3384                 switch (err) {
3385                 case -NFS4ERR_OPENMODE:
3386                         if (!(sattr->ia_valid & ATTR_SIZE)) {
3387                                 pr_warn_once("NFSv4: server %s is incorrectly "
3388                                                 "applying open mode checks to "
3389                                                 "a SETATTR that is not "
3390                                                 "changing file size.\n",
3391                                                 server->nfs_client->cl_hostname);
3392                         }
3393                         if (state && !(state->state & FMODE_WRITE)) {
3394                                 err = -EBADF;
3395                                 if (sattr->ia_valid & ATTR_OPEN)
3396                                         err = -EACCES;
3397                                 goto out;
3398                         }
3399                 }
3400                 err = nfs4_handle_exception(server, err, &exception);
3401         } while (exception.retry);
3402 out:
3403         return err;
3404 }
3405
3406 static bool
3407 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3408 {
3409         if (inode == NULL || !nfs_have_layout(inode))
3410                 return false;
3411
3412         return pnfs_wait_on_layoutreturn(inode, task);
3413 }
3414
3415 /*
3416  * Update the seqid of an open stateid
3417  */
3418 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3419                 struct nfs4_state *state)
3420 {
3421         __be32 seqid_open;
3422         u32 dst_seqid;
3423         int seq;
3424
3425         for (;;) {
3426                 if (!nfs4_valid_open_stateid(state))
3427                         break;
3428                 seq = read_seqbegin(&state->seqlock);
3429                 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3430                         nfs4_stateid_copy(dst, &state->open_stateid);
3431                         if (read_seqretry(&state->seqlock, seq))
3432                                 continue;
3433                         break;
3434                 }
3435                 seqid_open = state->open_stateid.seqid;
3436                 if (read_seqretry(&state->seqlock, seq))
3437                         continue;
3438
3439                 dst_seqid = be32_to_cpu(dst->seqid);
3440                 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3441                         dst->seqid = seqid_open;
3442                 break;
3443         }
3444 }
3445
3446 /*
3447  * Update the seqid of an open stateid after receiving
3448  * NFS4ERR_OLD_STATEID
3449  */
3450 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3451                 struct nfs4_state *state)
3452 {
3453         __be32 seqid_open;
3454         u32 dst_seqid;
3455         bool ret;
3456         int seq, status = -EAGAIN;
3457         DEFINE_WAIT(wait);
3458
3459         for (;;) {
3460                 ret = false;
3461                 if (!nfs4_valid_open_stateid(state))
3462                         break;
3463                 seq = read_seqbegin(&state->seqlock);
3464                 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3465                         if (read_seqretry(&state->seqlock, seq))
3466                                 continue;
3467                         break;
3468                 }
3469
3470                 write_seqlock(&state->seqlock);
3471                 seqid_open = state->open_stateid.seqid;
3472
3473                 dst_seqid = be32_to_cpu(dst->seqid);
3474
3475                 /* Did another OPEN bump the state's seqid?  try again: */
3476                 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3477                         dst->seqid = seqid_open;
3478                         write_sequnlock(&state->seqlock);
3479                         ret = true;
3480                         break;
3481                 }
3482
3483                 /* server says we're behind but we haven't seen the update yet */
3484                 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3485                 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3486                 write_sequnlock(&state->seqlock);
3487                 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3488
3489                 if (fatal_signal_pending(current))
3490                         status = -EINTR;
3491                 else
3492                         if (schedule_timeout(5*HZ) != 0)
3493                                 status = 0;
3494
3495                 finish_wait(&state->waitq, &wait);
3496
3497                 if (!status)
3498                         continue;
3499                 if (status == -EINTR)
3500                         break;
3501
3502                 /* we slept the whole 5 seconds, we must have lost a seqid */
3503                 dst->seqid = cpu_to_be32(dst_seqid + 1);
3504                 ret = true;
3505                 break;
3506         }
3507
3508         return ret;
3509 }
3510
3511 struct nfs4_closedata {
3512         struct inode *inode;
3513         struct nfs4_state *state;
3514         struct nfs_closeargs arg;
3515         struct nfs_closeres res;
3516         struct {
3517                 struct nfs4_layoutreturn_args arg;
3518                 struct nfs4_layoutreturn_res res;
3519                 struct nfs4_xdr_opaque_data ld_private;
3520                 u32 roc_barrier;
3521                 bool roc;
3522         } lr;
3523         struct nfs_fattr fattr;
3524         unsigned long timestamp;
3525 };
3526
3527 static void nfs4_free_closedata(void *data)
3528 {
3529         struct nfs4_closedata *calldata = data;
3530         struct nfs4_state_owner *sp = calldata->state->owner;
3531         struct super_block *sb = calldata->state->inode->i_sb;
3532
3533         if (calldata->lr.roc)
3534                 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3535                                 calldata->res.lr_ret);
3536         nfs4_put_open_state(calldata->state);
3537         nfs_free_seqid(calldata->arg.seqid);
3538         nfs4_put_state_owner(sp);
3539         nfs_sb_deactive(sb);
3540         kfree(calldata);
3541 }
3542
3543 static void nfs4_close_done(struct rpc_task *task, void *data)
3544 {
3545         struct nfs4_closedata *calldata = data;
3546         struct nfs4_state *state = calldata->state;
3547         struct nfs_server *server = NFS_SERVER(calldata->inode);
3548         nfs4_stateid *res_stateid = NULL;
3549         struct nfs4_exception exception = {
3550                 .state = state,
3551                 .inode = calldata->inode,
3552                 .stateid = &calldata->arg.stateid,
3553         };
3554
3555         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3556                 return;
3557         trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3558
3559         /* Handle Layoutreturn errors */
3560         if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3561                           &calldata->res.lr_ret) == -EAGAIN)
3562                 goto out_restart;
3563
3564         /* hmm. we are done with the inode, and in the process of freeing
3565          * the state_owner. we keep this around to process errors
3566          */
3567         switch (task->tk_status) {
3568                 case 0:
3569                         res_stateid = &calldata->res.stateid;
3570                         renew_lease(server, calldata->timestamp);
3571                         break;
3572                 case -NFS4ERR_ACCESS:
3573                         if (calldata->arg.bitmask != NULL) {
3574                                 calldata->arg.bitmask = NULL;
3575                                 calldata->res.fattr = NULL;
3576                                 goto out_restart;
3577
3578                         }
3579                         break;
3580                 case -NFS4ERR_OLD_STATEID:
3581                         /* Did we race with OPEN? */
3582                         if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3583                                                 state))
3584                                 goto out_restart;
3585                         goto out_release;
3586                 case -NFS4ERR_ADMIN_REVOKED:
3587                 case -NFS4ERR_STALE_STATEID:
3588                 case -NFS4ERR_EXPIRED:
3589                         nfs4_free_revoked_stateid(server,
3590                                         &calldata->arg.stateid,
3591                                         task->tk_msg.rpc_cred);
3592                         fallthrough;
3593                 case -NFS4ERR_BAD_STATEID:
3594                         if (calldata->arg.fmode == 0)
3595                                 break;
3596                         fallthrough;
3597                 default:
3598                         task->tk_status = nfs4_async_handle_exception(task,
3599                                         server, task->tk_status, &exception);
3600                         if (exception.retry)
3601                                 goto out_restart;
3602         }
3603         nfs_clear_open_stateid(state, &calldata->arg.stateid,
3604                         res_stateid, calldata->arg.fmode);
3605 out_release:
3606         task->tk_status = 0;
3607         nfs_release_seqid(calldata->arg.seqid);
3608         nfs_refresh_inode(calldata->inode, &calldata->fattr);
3609         dprintk("%s: ret = %d\n", __func__, task->tk_status);
3610         return;
3611 out_restart:
3612         task->tk_status = 0;
3613         rpc_restart_call_prepare(task);
3614         goto out_release;
3615 }
3616
3617 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3618 {
3619         struct nfs4_closedata *calldata = data;
3620         struct nfs4_state *state = calldata->state;
3621         struct inode *inode = calldata->inode;
3622         struct nfs_server *server = NFS_SERVER(inode);
3623         struct pnfs_layout_hdr *lo;
3624         bool is_rdonly, is_wronly, is_rdwr;
3625         int call_close = 0;
3626
3627         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3628                 goto out_wait;
3629
3630         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3631         spin_lock(&state->owner->so_lock);
3632         is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3633         is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3634         is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3635         /* Calculate the change in open mode */
3636         calldata->arg.fmode = 0;
3637         if (state->n_rdwr == 0) {
3638                 if (state->n_rdonly == 0)
3639                         call_close |= is_rdonly;
3640                 else if (is_rdonly)
3641                         calldata->arg.fmode |= FMODE_READ;
3642                 if (state->n_wronly == 0)
3643                         call_close |= is_wronly;
3644                 else if (is_wronly)
3645                         calldata->arg.fmode |= FMODE_WRITE;
3646                 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3647                         call_close |= is_rdwr;
3648         } else if (is_rdwr)
3649                 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3650
3651         nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3652         if (!nfs4_valid_open_stateid(state))
3653                 call_close = 0;
3654         spin_unlock(&state->owner->so_lock);
3655
3656         if (!call_close) {
3657                 /* Note: exit _without_ calling nfs4_close_done */
3658                 goto out_no_action;
3659         }
3660
3661         if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3662                 nfs_release_seqid(calldata->arg.seqid);
3663                 goto out_wait;
3664         }
3665
3666         lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3667         if (lo && !pnfs_layout_is_valid(lo)) {
3668                 calldata->arg.lr_args = NULL;
3669                 calldata->res.lr_res = NULL;
3670         }
3671
3672         if (calldata->arg.fmode == 0)
3673                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3674
3675         if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3676                 /* Close-to-open cache consistency revalidation */
3677                 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3678                         nfs4_bitmask_set(calldata->arg.bitmask_store,
3679                                          server->cache_consistency_bitmask,
3680                                          inode, 0);
3681                         calldata->arg.bitmask = calldata->arg.bitmask_store;
3682                 } else
3683                         calldata->arg.bitmask = NULL;
3684         }
3685
3686         calldata->arg.share_access =
3687                 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3688                                 calldata->arg.fmode, 0);
3689
3690         if (calldata->res.fattr == NULL)
3691                 calldata->arg.bitmask = NULL;
3692         else if (calldata->arg.bitmask == NULL)
3693                 calldata->res.fattr = NULL;
3694         calldata->timestamp = jiffies;
3695         if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3696                                 &calldata->arg.seq_args,
3697                                 &calldata->res.seq_res,
3698                                 task) != 0)
3699                 nfs_release_seqid(calldata->arg.seqid);
3700         return;
3701 out_no_action:
3702         task->tk_action = NULL;
3703 out_wait:
3704         nfs4_sequence_done(task, &calldata->res.seq_res);
3705 }
3706
3707 static const struct rpc_call_ops nfs4_close_ops = {
3708         .rpc_call_prepare = nfs4_close_prepare,
3709         .rpc_call_done = nfs4_close_done,
3710         .rpc_release = nfs4_free_closedata,
3711 };
3712
3713 /* 
3714  * It is possible for data to be read/written from a mem-mapped file 
3715  * after the sys_close call (which hits the vfs layer as a flush).
3716  * This means that we can't safely call nfsv4 close on a file until 
3717  * the inode is cleared. This in turn means that we are not good
3718  * NFSv4 citizens - we do not indicate to the server to update the file's 
3719  * share state even when we are done with one of the three share 
3720  * stateid's in the inode.
3721  *
3722  * NOTE: Caller must be holding the sp->so_owner semaphore!
3723  */
3724 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3725 {
3726         struct nfs_server *server = NFS_SERVER(state->inode);
3727         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3728         struct nfs4_closedata *calldata;
3729         struct nfs4_state_owner *sp = state->owner;
3730         struct rpc_task *task;
3731         struct rpc_message msg = {
3732                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3733                 .rpc_cred = state->owner->so_cred,
3734         };
3735         struct rpc_task_setup task_setup_data = {
3736                 .rpc_client = server->client,
3737                 .rpc_message = &msg,
3738                 .callback_ops = &nfs4_close_ops,
3739                 .workqueue = nfsiod_workqueue,
3740                 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3741         };
3742         int status = -ENOMEM;
3743
3744         if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3745                 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3746
3747         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3748                 &task_setup_data.rpc_client, &msg);
3749
3750         calldata = kzalloc(sizeof(*calldata), gfp_mask);
3751         if (calldata == NULL)
3752                 goto out;
3753         nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3754         calldata->inode = state->inode;
3755         calldata->state = state;
3756         calldata->arg.fh = NFS_FH(state->inode);
3757         if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3758                 goto out_free_calldata;
3759         /* Serialization for the sequence id */
3760         alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3761         calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3762         if (IS_ERR(calldata->arg.seqid))
3763                 goto out_free_calldata;
3764         nfs_fattr_init(&calldata->fattr);
3765         calldata->arg.fmode = 0;
3766         calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3767         calldata->res.fattr = &calldata->fattr;
3768         calldata->res.seqid = calldata->arg.seqid;
3769         calldata->res.server = server;
3770         calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3771         calldata->lr.roc = pnfs_roc(state->inode,
3772                         &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3773         if (calldata->lr.roc) {
3774                 calldata->arg.lr_args = &calldata->lr.arg;
3775                 calldata->res.lr_res = &calldata->lr.res;
3776         }
3777         nfs_sb_active(calldata->inode->i_sb);
3778
3779         msg.rpc_argp = &calldata->arg;
3780         msg.rpc_resp = &calldata->res;
3781         task_setup_data.callback_data = calldata;
3782         task = rpc_run_task(&task_setup_data);
3783         if (IS_ERR(task))
3784                 return PTR_ERR(task);
3785         status = 0;
3786         if (wait)
3787                 status = rpc_wait_for_completion_task(task);
3788         rpc_put_task(task);
3789         return status;
3790 out_free_calldata:
3791         kfree(calldata);
3792 out:
3793         nfs4_put_open_state(state);
3794         nfs4_put_state_owner(sp);
3795         return status;
3796 }
3797
3798 static struct inode *
3799 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3800                 int open_flags, struct iattr *attr, int *opened)
3801 {
3802         struct nfs4_state *state;
3803         struct nfs4_label l, *label;
3804
3805         label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3806
3807         /* Protect against concurrent sillydeletes */
3808         state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3809
3810         nfs4_label_release_security(label);
3811
3812         if (IS_ERR(state))
3813                 return ERR_CAST(state);
3814         return state->inode;
3815 }
3816
3817 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3818 {
3819         if (ctx->state == NULL)
3820                 return;
3821         if (is_sync)
3822                 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3823         else
3824                 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3825 }
3826
3827 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3828 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3829 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3830
3831 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3832 {
3833         u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3834         struct nfs4_server_caps_arg args = {
3835                 .fhandle = fhandle,
3836                 .bitmask = bitmask,
3837         };
3838         struct nfs4_server_caps_res res = {};
3839         struct rpc_message msg = {
3840                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3841                 .rpc_argp = &args,
3842                 .rpc_resp = &res,
3843         };
3844         int status;
3845         int i;
3846
3847         bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3848                      FATTR4_WORD0_FH_EXPIRE_TYPE |
3849                      FATTR4_WORD0_LINK_SUPPORT |
3850                      FATTR4_WORD0_SYMLINK_SUPPORT |
3851                      FATTR4_WORD0_ACLSUPPORT |
3852                      FATTR4_WORD0_CASE_INSENSITIVE |
3853                      FATTR4_WORD0_CASE_PRESERVING;
3854         if (minorversion)
3855                 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3856
3857         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3858         if (status == 0) {
3859                 /* Sanity check the server answers */
3860                 switch (minorversion) {
3861                 case 0:
3862                         res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3863                         res.attr_bitmask[2] = 0;
3864                         break;
3865                 case 1:
3866                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3867                         break;
3868                 case 2:
3869                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3870                 }
3871                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3872                 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3873                                   NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3874                 server->fattr_valid = NFS_ATTR_FATTR_V4;
3875                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3876                                 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3877                         server->caps |= NFS_CAP_ACLS;
3878                 if (res.has_links != 0)
3879                         server->caps |= NFS_CAP_HARDLINKS;
3880                 if (res.has_symlinks != 0)
3881                         server->caps |= NFS_CAP_SYMLINKS;
3882                 if (res.case_insensitive)
3883                         server->caps |= NFS_CAP_CASE_INSENSITIVE;
3884                 if (res.case_preserving)
3885                         server->caps |= NFS_CAP_CASE_PRESERVING;
3886 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3887                 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3888                         server->caps |= NFS_CAP_SECURITY_LABEL;
3889 #endif
3890                 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3891                         server->caps |= NFS_CAP_FS_LOCATIONS;
3892                 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3893                         server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3894                 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3895                         server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3896                 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3897                         server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3898                 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3899                         server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3900                                 NFS_ATTR_FATTR_OWNER_NAME);
3901                 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3902                         server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3903                                 NFS_ATTR_FATTR_GROUP_NAME);
3904                 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3905                         server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3906                 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3907                         server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3908                 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3909                         server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3910                 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3911                         server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3912                 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3913                                 sizeof(server->attr_bitmask));
3914                 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3915
3916                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3917                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3918                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3919                 server->cache_consistency_bitmask[2] = 0;
3920
3921                 /* Avoid a regression due to buggy server */
3922                 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3923                         res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3924                 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3925                         sizeof(server->exclcreat_bitmask));
3926
3927                 server->acl_bitmask = res.acl_bitmask;
3928                 server->fh_expire_type = res.fh_expire_type;
3929         }
3930
3931         return status;
3932 }
3933
3934 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3935 {
3936         struct nfs4_exception exception = {
3937                 .interruptible = true,
3938         };
3939         int err;
3940
3941         nfs4_server_set_init_caps(server);
3942         do {
3943                 err = nfs4_handle_exception(server,
3944                                 _nfs4_server_capabilities(server, fhandle),
3945                                 &exception);
3946         } while (exception.retry);
3947         return err;
3948 }
3949
3950 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3951                                           struct nfs_client *clp,
3952                                           struct nfs_server *server)
3953 {
3954         int i;
3955
3956         for (i = 0; i < location->nservers; i++) {
3957                 struct nfs4_string *srv_loc = &location->servers[i];
3958                 struct sockaddr_storage addr;
3959                 size_t addrlen;
3960                 struct xprt_create xprt_args = {
3961                         .ident = 0,
3962                         .net = clp->cl_net,
3963                 };
3964                 struct nfs4_add_xprt_data xprtdata = {
3965                         .clp = clp,
3966                 };
3967                 struct rpc_add_xprt_test rpcdata = {
3968                         .add_xprt_test = clp->cl_mvops->session_trunk,
3969                         .data = &xprtdata,
3970                 };
3971                 char *servername = NULL;
3972
3973                 if (!srv_loc->len)
3974                         continue;
3975
3976                 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3977                                                 &addr, sizeof(addr),
3978                                                 clp->cl_net, server->port);
3979                 if (!addrlen)
3980                         return;
3981                 xprt_args.dstaddr = (struct sockaddr *)&addr;
3982                 xprt_args.addrlen = addrlen;
3983                 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3984                 if (!servername)
3985                         return;
3986                 memcpy(servername, srv_loc->data, srv_loc->len);
3987                 servername[srv_loc->len] = '\0';
3988                 xprt_args.servername = servername;
3989
3990                 xprtdata.cred = nfs4_get_clid_cred(clp);
3991                 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3992                                   rpc_clnt_setup_test_and_add_xprt,
3993                                   &rpcdata);
3994                 if (xprtdata.cred)
3995                         put_cred(xprtdata.cred);
3996                 kfree(servername);
3997         }
3998 }
3999
4000 static int _nfs4_discover_trunking(struct nfs_server *server,
4001                                    struct nfs_fh *fhandle)
4002 {
4003         struct nfs4_fs_locations *locations = NULL;
4004         struct page *page;
4005         const struct cred *cred;
4006         struct nfs_client *clp = server->nfs_client;
4007         const struct nfs4_state_maintenance_ops *ops =
4008                 clp->cl_mvops->state_renewal_ops;
4009         int status = -ENOMEM, i;
4010
4011         cred = ops->get_state_renewal_cred(clp);
4012         if (cred == NULL) {
4013                 cred = nfs4_get_clid_cred(clp);
4014                 if (cred == NULL)
4015                         return -ENOKEY;
4016         }
4017
4018         page = alloc_page(GFP_KERNEL);
4019         if (!page)
4020                 goto out_put_cred;
4021         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4022         if (!locations)
4023                 goto out_free;
4024         locations->fattr = nfs_alloc_fattr();
4025         if (!locations->fattr)
4026                 goto out_free_2;
4027
4028         status = nfs4_proc_get_locations(server, fhandle, locations, page,
4029                                          cred);
4030         if (status)
4031                 goto out_free_3;
4032
4033         for (i = 0; i < locations->nlocations; i++)
4034                 test_fs_location_for_trunking(&locations->locations[i], clp,
4035                                               server);
4036 out_free_3:
4037         kfree(locations->fattr);
4038 out_free_2:
4039         kfree(locations);
4040 out_free:
4041         __free_page(page);
4042 out_put_cred:
4043         put_cred(cred);
4044         return status;
4045 }
4046
4047 static int nfs4_discover_trunking(struct nfs_server *server,
4048                                   struct nfs_fh *fhandle)
4049 {
4050         struct nfs4_exception exception = {
4051                 .interruptible = true,
4052         };
4053         struct nfs_client *clp = server->nfs_client;
4054         int err = 0;
4055
4056         if (!nfs4_has_session(clp))
4057                 goto out;
4058         do {
4059                 err = nfs4_handle_exception(server,
4060                                 _nfs4_discover_trunking(server, fhandle),
4061                                 &exception);
4062         } while (exception.retry);
4063 out:
4064         return err;
4065 }
4066
4067 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4068                 struct nfs_fsinfo *info)
4069 {
4070         u32 bitmask[3];
4071         struct nfs4_lookup_root_arg args = {
4072                 .bitmask = bitmask,
4073         };
4074         struct nfs4_lookup_res res = {
4075                 .server = server,
4076                 .fattr = info->fattr,
4077                 .fh = fhandle,
4078         };
4079         struct rpc_message msg = {
4080                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4081                 .rpc_argp = &args,
4082                 .rpc_resp = &res,
4083         };
4084
4085         bitmask[0] = nfs4_fattr_bitmap[0];
4086         bitmask[1] = nfs4_fattr_bitmap[1];
4087         /*
4088          * Process the label in the upcoming getfattr
4089          */
4090         bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4091
4092         nfs_fattr_init(info->fattr);
4093         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4094 }
4095
4096 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4097                 struct nfs_fsinfo *info)
4098 {
4099         struct nfs4_exception exception = {
4100                 .interruptible = true,
4101         };
4102         int err;
4103         do {
4104                 err = _nfs4_lookup_root(server, fhandle, info);
4105                 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4106                 switch (err) {
4107                 case 0:
4108                 case -NFS4ERR_WRONGSEC:
4109                         goto out;
4110                 default:
4111                         err = nfs4_handle_exception(server, err, &exception);
4112                 }
4113         } while (exception.retry);
4114 out:
4115         return err;
4116 }
4117
4118 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4119                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4120 {
4121         struct rpc_auth_create_args auth_args = {
4122                 .pseudoflavor = flavor,
4123         };
4124         struct rpc_auth *auth;
4125
4126         auth = rpcauth_create(&auth_args, server->client);
4127         if (IS_ERR(auth))
4128                 return -EACCES;
4129         return nfs4_lookup_root(server, fhandle, info);
4130 }
4131
4132 /*
4133  * Retry pseudoroot lookup with various security flavors.  We do this when:
4134  *
4135  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4136  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4137  *
4138  * Returns zero on success, or a negative NFS4ERR value, or a
4139  * negative errno value.
4140  */
4141 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4142                               struct nfs_fsinfo *info)
4143 {
4144         /* Per 3530bis 15.33.5 */
4145         static const rpc_authflavor_t flav_array[] = {
4146                 RPC_AUTH_GSS_KRB5P,
4147                 RPC_AUTH_GSS_KRB5I,
4148                 RPC_AUTH_GSS_KRB5,
4149                 RPC_AUTH_UNIX,                  /* courtesy */
4150                 RPC_AUTH_NULL,
4151         };
4152         int status = -EPERM;
4153         size_t i;
4154
4155         if (server->auth_info.flavor_len > 0) {
4156                 /* try each flavor specified by user */
4157                 for (i = 0; i < server->auth_info.flavor_len; i++) {
4158                         status = nfs4_lookup_root_sec(server, fhandle, info,
4159                                                 server->auth_info.flavors[i]);
4160                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4161                                 continue;
4162                         break;
4163                 }
4164         } else {
4165                 /* no flavors specified by user, try default list */
4166                 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4167                         status = nfs4_lookup_root_sec(server, fhandle, info,
4168                                                       flav_array[i]);
4169                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4170                                 continue;
4171                         break;
4172                 }
4173         }
4174
4175         /*
4176          * -EACCES could mean that the user doesn't have correct permissions
4177          * to access the mount.  It could also mean that we tried to mount
4178          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
4179          * existing mount programs don't handle -EACCES very well so it should
4180          * be mapped to -EPERM instead.
4181          */
4182         if (status == -EACCES)
4183                 status = -EPERM;
4184         return status;
4185 }
4186
4187 /**
4188  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4189  * @server: initialized nfs_server handle
4190  * @fhandle: we fill in the pseudo-fs root file handle
4191  * @info: we fill in an FSINFO struct
4192  * @auth_probe: probe the auth flavours
4193  *
4194  * Returns zero on success, or a negative errno.
4195  */
4196 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4197                          struct nfs_fsinfo *info,
4198                          bool auth_probe)
4199 {
4200         int status = 0;
4201
4202         if (!auth_probe)
4203                 status = nfs4_lookup_root(server, fhandle, info);
4204
4205         if (auth_probe || status == NFS4ERR_WRONGSEC)
4206                 status = server->nfs_client->cl_mvops->find_root_sec(server,
4207                                 fhandle, info);
4208
4209         if (status == 0)
4210                 status = nfs4_server_capabilities(server, fhandle);
4211         if (status == 0)
4212                 status = nfs4_do_fsinfo(server, fhandle, info);
4213
4214         return nfs4_map_errors(status);
4215 }
4216
4217 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4218                               struct nfs_fsinfo *info)
4219 {
4220         int error;
4221         struct nfs_fattr *fattr = info->fattr;
4222
4223         error = nfs4_server_capabilities(server, mntfh);
4224         if (error < 0) {
4225                 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4226                 return error;
4227         }
4228
4229         error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4230         if (error < 0) {
4231                 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4232                 goto out;
4233         }
4234
4235         if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4236             !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4237                 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4238
4239 out:
4240         return error;
4241 }
4242
4243 /*
4244  * Get locations and (maybe) other attributes of a referral.
4245  * Note that we'll actually follow the referral later when
4246  * we detect fsid mismatch in inode revalidation
4247  */
4248 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4249                              const struct qstr *name, struct nfs_fattr *fattr,
4250                              struct nfs_fh *fhandle)
4251 {
4252         int status = -ENOMEM;
4253         struct page *page = NULL;
4254         struct nfs4_fs_locations *locations = NULL;
4255
4256         page = alloc_page(GFP_KERNEL);
4257         if (page == NULL)
4258                 goto out;
4259         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4260         if (locations == NULL)
4261                 goto out;
4262
4263         locations->fattr = fattr;
4264
4265         status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4266         if (status != 0)
4267                 goto out;
4268
4269         /*
4270          * If the fsid didn't change, this is a migration event, not a
4271          * referral.  Cause us to drop into the exception handler, which
4272          * will kick off migration recovery.
4273          */
4274         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4275                 dprintk("%s: server did not return a different fsid for"
4276                         " a referral at %s\n", __func__, name->name);
4277                 status = -NFS4ERR_MOVED;
4278                 goto out;
4279         }
4280         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4281         nfs_fixup_referral_attributes(fattr);
4282         memset(fhandle, 0, sizeof(struct nfs_fh));
4283 out:
4284         if (page)
4285                 __free_page(page);
4286         kfree(locations);
4287         return status;
4288 }
4289
4290 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4291                                 struct nfs_fattr *fattr, struct inode *inode)
4292 {
4293         __u32 bitmask[NFS4_BITMASK_SZ];
4294         struct nfs4_getattr_arg args = {
4295                 .fh = fhandle,
4296                 .bitmask = bitmask,
4297         };
4298         struct nfs4_getattr_res res = {
4299                 .fattr = fattr,
4300                 .server = server,
4301         };
4302         struct rpc_message msg = {
4303                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4304                 .rpc_argp = &args,
4305                 .rpc_resp = &res,
4306         };
4307         unsigned short task_flags = 0;
4308
4309         if (nfs4_has_session(server->nfs_client))
4310                 task_flags = RPC_TASK_MOVEABLE;
4311
4312         /* Is this is an attribute revalidation, subject to softreval? */
4313         if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4314                 task_flags |= RPC_TASK_TIMEOUT;
4315
4316         nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4317         nfs_fattr_init(fattr);
4318         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4319         return nfs4_do_call_sync(server->client, server, &msg,
4320                         &args.seq_args, &res.seq_res, task_flags);
4321 }
4322
4323 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4324                                 struct nfs_fattr *fattr, struct inode *inode)
4325 {
4326         struct nfs4_exception exception = {
4327                 .interruptible = true,
4328         };
4329         int err;
4330         do {
4331                 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4332                 trace_nfs4_getattr(server, fhandle, fattr, err);
4333                 err = nfs4_handle_exception(server, err,
4334                                 &exception);
4335         } while (exception.retry);
4336         return err;
4337 }
4338
4339 /* 
4340  * The file is not closed if it is opened due to the a request to change
4341  * the size of the file. The open call will not be needed once the
4342  * VFS layer lookup-intents are implemented.
4343  *
4344  * Close is called when the inode is destroyed.
4345  * If we haven't opened the file for O_WRONLY, we
4346  * need to in the size_change case to obtain a stateid.
4347  *
4348  * Got race?
4349  * Because OPEN is always done by name in nfsv4, it is
4350  * possible that we opened a different file by the same
4351  * name.  We can recognize this race condition, but we
4352  * can't do anything about it besides returning an error.
4353  *
4354  * This will be fixed with VFS changes (lookup-intent).
4355  */
4356 static int
4357 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4358                   struct iattr *sattr)
4359 {
4360         struct inode *inode = d_inode(dentry);
4361         const struct cred *cred = NULL;
4362         struct nfs_open_context *ctx = NULL;
4363         int status;
4364
4365         if (pnfs_ld_layoutret_on_setattr(inode) &&
4366             sattr->ia_valid & ATTR_SIZE &&
4367             sattr->ia_size < i_size_read(inode))
4368                 pnfs_commit_and_return_layout(inode);
4369
4370         nfs_fattr_init(fattr);
4371         
4372         /* Deal with open(O_TRUNC) */
4373         if (sattr->ia_valid & ATTR_OPEN)
4374                 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4375
4376         /* Optimization: if the end result is no change, don't RPC */
4377         if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4378                 return 0;
4379
4380         /* Search for an existing open(O_WRITE) file */
4381         if (sattr->ia_valid & ATTR_FILE) {
4382
4383                 ctx = nfs_file_open_context(sattr->ia_file);
4384                 if (ctx)
4385                         cred = ctx->cred;
4386         }
4387
4388         /* Return any delegations if we're going to change ACLs */
4389         if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4390                 nfs4_inode_make_writeable(inode);
4391
4392         status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4393         if (status == 0) {
4394                 nfs_setattr_update_inode(inode, sattr, fattr);
4395                 nfs_setsecurity(inode, fattr);
4396         }
4397         return status;
4398 }
4399
4400 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4401                 struct dentry *dentry, struct nfs_fh *fhandle,
4402                 struct nfs_fattr *fattr)
4403 {
4404         struct nfs_server *server = NFS_SERVER(dir);
4405         int                    status;
4406         struct nfs4_lookup_arg args = {
4407                 .bitmask = server->attr_bitmask,
4408                 .dir_fh = NFS_FH(dir),
4409                 .name = &dentry->d_name,
4410         };
4411         struct nfs4_lookup_res res = {
4412                 .server = server,
4413                 .fattr = fattr,
4414                 .fh = fhandle,
4415         };
4416         struct rpc_message msg = {
4417                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4418                 .rpc_argp = &args,
4419                 .rpc_resp = &res,
4420         };
4421         unsigned short task_flags = 0;
4422
4423         if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4424                 task_flags = RPC_TASK_MOVEABLE;
4425
4426         /* Is this is an attribute revalidation, subject to softreval? */
4427         if (nfs_lookup_is_soft_revalidate(dentry))
4428                 task_flags |= RPC_TASK_TIMEOUT;
4429
4430         args.bitmask = nfs4_bitmask(server, fattr->label);
4431
4432         nfs_fattr_init(fattr);
4433
4434         dprintk("NFS call  lookup %pd2\n", dentry);
4435         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4436         status = nfs4_do_call_sync(clnt, server, &msg,
4437                         &args.seq_args, &res.seq_res, task_flags);
4438         dprintk("NFS reply lookup: %d\n", status);
4439         return status;
4440 }
4441
4442 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4443 {
4444         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4445                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4446         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4447         fattr->nlink = 2;
4448 }
4449
4450 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4451                                    struct dentry *dentry, struct nfs_fh *fhandle,
4452                                    struct nfs_fattr *fattr)
4453 {
4454         struct nfs4_exception exception = {
4455                 .interruptible = true,
4456         };
4457         struct rpc_clnt *client = *clnt;
4458         const struct qstr *name = &dentry->d_name;
4459         int err;
4460         do {
4461                 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4462                 trace_nfs4_lookup(dir, name, err);
4463                 switch (err) {
4464                 case -NFS4ERR_BADNAME:
4465                         err = -ENOENT;
4466                         goto out;
4467                 case -NFS4ERR_MOVED:
4468                         err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4469                         if (err == -NFS4ERR_MOVED)
4470                                 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4471                         goto out;
4472                 case -NFS4ERR_WRONGSEC:
4473                         err = -EPERM;
4474                         if (client != *clnt)
4475                                 goto out;
4476                         client = nfs4_negotiate_security(client, dir, name);
4477                         if (IS_ERR(client))
4478                                 return PTR_ERR(client);
4479
4480                         exception.retry = 1;
4481                         break;
4482                 default:
4483                         err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4484                 }
4485         } while (exception.retry);
4486
4487 out:
4488         if (err == 0)
4489                 *clnt = client;
4490         else if (client != *clnt)
4491                 rpc_shutdown_client(client);
4492
4493         return err;
4494 }
4495
4496 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4497                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4498 {
4499         int status;
4500         struct rpc_clnt *client = NFS_CLIENT(dir);
4501
4502         status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4503         if (client != NFS_CLIENT(dir)) {
4504                 rpc_shutdown_client(client);
4505                 nfs_fixup_secinfo_attributes(fattr);
4506         }
4507         return status;
4508 }
4509
4510 struct rpc_clnt *
4511 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4512                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4513 {
4514         struct rpc_clnt *client = NFS_CLIENT(dir);
4515         int status;
4516
4517         status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4518         if (status < 0)
4519                 return ERR_PTR(status);
4520         return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4521 }
4522
4523 static int _nfs4_proc_lookupp(struct inode *inode,
4524                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4525 {
4526         struct rpc_clnt *clnt = NFS_CLIENT(inode);
4527         struct nfs_server *server = NFS_SERVER(inode);
4528         int                    status;
4529         struct nfs4_lookupp_arg args = {
4530                 .bitmask = server->attr_bitmask,
4531                 .fh = NFS_FH(inode),
4532         };
4533         struct nfs4_lookupp_res res = {
4534                 .server = server,
4535                 .fattr = fattr,
4536                 .fh = fhandle,
4537         };
4538         struct rpc_message msg = {
4539                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4540                 .rpc_argp = &args,
4541                 .rpc_resp = &res,
4542         };
4543         unsigned short task_flags = 0;
4544
4545         if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4546                 task_flags |= RPC_TASK_TIMEOUT;
4547
4548         args.bitmask = nfs4_bitmask(server, fattr->label);
4549
4550         nfs_fattr_init(fattr);
4551
4552         dprintk("NFS call  lookupp ino=0x%lx\n", inode->i_ino);
4553         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4554                                 &res.seq_res, task_flags);
4555         dprintk("NFS reply lookupp: %d\n", status);
4556         return status;
4557 }
4558
4559 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4560                              struct nfs_fattr *fattr)
4561 {
4562         struct nfs4_exception exception = {
4563                 .interruptible = true,
4564         };
4565         int err;
4566         do {
4567                 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4568                 trace_nfs4_lookupp(inode, err);
4569                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4570                                 &exception);
4571         } while (exception.retry);
4572         return err;
4573 }
4574
4575 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4576                              const struct cred *cred)
4577 {
4578         struct nfs_server *server = NFS_SERVER(inode);
4579         struct nfs4_accessargs args = {
4580                 .fh = NFS_FH(inode),
4581                 .access = entry->mask,
4582         };
4583         struct nfs4_accessres res = {
4584                 .server = server,
4585         };
4586         struct rpc_message msg = {
4587                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4588                 .rpc_argp = &args,
4589                 .rpc_resp = &res,
4590                 .rpc_cred = cred,
4591         };
4592         int status = 0;
4593
4594         if (!nfs4_have_delegation(inode, FMODE_READ)) {
4595                 res.fattr = nfs_alloc_fattr();
4596                 if (res.fattr == NULL)
4597                         return -ENOMEM;
4598                 args.bitmask = server->cache_consistency_bitmask;
4599         }
4600         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4601         if (!status) {
4602                 nfs_access_set_mask(entry, res.access);
4603                 if (res.fattr)
4604                         nfs_refresh_inode(inode, res.fattr);
4605         }
4606         nfs_free_fattr(res.fattr);
4607         return status;
4608 }
4609
4610 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4611                             const struct cred *cred)
4612 {
4613         struct nfs4_exception exception = {
4614                 .interruptible = true,
4615         };
4616         int err;
4617         do {
4618                 err = _nfs4_proc_access(inode, entry, cred);
4619                 trace_nfs4_access(inode, err);
4620                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4621                                 &exception);
4622         } while (exception.retry);
4623         return err;
4624 }
4625
4626 /*
4627  * TODO: For the time being, we don't try to get any attributes
4628  * along with any of the zero-copy operations READ, READDIR,
4629  * READLINK, WRITE.
4630  *
4631  * In the case of the first three, we want to put the GETATTR
4632  * after the read-type operation -- this is because it is hard
4633  * to predict the length of a GETATTR response in v4, and thus
4634  * align the READ data correctly.  This means that the GETATTR
4635  * may end up partially falling into the page cache, and we should
4636  * shift it into the 'tail' of the xdr_buf before processing.
4637  * To do this efficiently, we need to know the total length
4638  * of data received, which doesn't seem to be available outside
4639  * of the RPC layer.
4640  *
4641  * In the case of WRITE, we also want to put the GETATTR after
4642  * the operation -- in this case because we want to make sure
4643  * we get the post-operation mtime and size.
4644  *
4645  * Both of these changes to the XDR layer would in fact be quite
4646  * minor, but I decided to leave them for a subsequent patch.
4647  */
4648 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4649                 unsigned int pgbase, unsigned int pglen)
4650 {
4651         struct nfs4_readlink args = {
4652                 .fh       = NFS_FH(inode),
4653                 .pgbase   = pgbase,
4654                 .pglen    = pglen,
4655                 .pages    = &page,
4656         };
4657         struct nfs4_readlink_res res;
4658         struct rpc_message msg = {
4659                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4660                 .rpc_argp = &args,
4661                 .rpc_resp = &res,
4662         };
4663
4664         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4665 }
4666
4667 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4668                 unsigned int pgbase, unsigned int pglen)
4669 {
4670         struct nfs4_exception exception = {
4671                 .interruptible = true,
4672         };
4673         int err;
4674         do {
4675                 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4676                 trace_nfs4_readlink(inode, err);
4677                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4678                                 &exception);
4679         } while (exception.retry);
4680         return err;
4681 }
4682
4683 /*
4684  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
4685  */
4686 static int
4687 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4688                  int flags)
4689 {
4690         struct nfs_server *server = NFS_SERVER(dir);
4691         struct nfs4_label l, *ilabel;
4692         struct nfs_open_context *ctx;
4693         struct nfs4_state *state;
4694         int status = 0;
4695
4696         ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4697         if (IS_ERR(ctx))
4698                 return PTR_ERR(ctx);
4699
4700         ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4701
4702         if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4703                 sattr->ia_mode &= ~current_umask();
4704         state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4705         if (IS_ERR(state)) {
4706                 status = PTR_ERR(state);
4707                 goto out;
4708         }
4709 out:
4710         nfs4_label_release_security(ilabel);
4711         put_nfs_open_context(ctx);
4712         return status;
4713 }
4714
4715 static int
4716 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4717 {
4718         struct nfs_server *server = NFS_SERVER(dir);
4719         struct nfs_removeargs args = {
4720                 .fh = NFS_FH(dir),
4721                 .name = *name,
4722         };
4723         struct nfs_removeres res = {
4724                 .server = server,
4725         };
4726         struct rpc_message msg = {
4727                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4728                 .rpc_argp = &args,
4729                 .rpc_resp = &res,
4730         };
4731         unsigned long timestamp = jiffies;
4732         int status;
4733
4734         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4735         if (status == 0) {
4736                 spin_lock(&dir->i_lock);
4737                 /* Removing a directory decrements nlink in the parent */
4738                 if (ftype == NF4DIR && dir->i_nlink > 2)
4739                         nfs4_dec_nlink_locked(dir);
4740                 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4741                                               NFS_INO_INVALID_DATA);
4742                 spin_unlock(&dir->i_lock);
4743         }
4744         return status;
4745 }
4746
4747 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4748 {
4749         struct nfs4_exception exception = {
4750                 .interruptible = true,
4751         };
4752         struct inode *inode = d_inode(dentry);
4753         int err;
4754
4755         if (inode) {
4756                 if (inode->i_nlink == 1)
4757                         nfs4_inode_return_delegation(inode);
4758                 else
4759                         nfs4_inode_make_writeable(inode);
4760         }
4761         do {
4762                 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4763                 trace_nfs4_remove(dir, &dentry->d_name, err);
4764                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4765                                 &exception);
4766         } while (exception.retry);
4767         return err;
4768 }
4769
4770 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4771 {
4772         struct nfs4_exception exception = {
4773                 .interruptible = true,
4774         };
4775         int err;
4776
4777         do {
4778                 err = _nfs4_proc_remove(dir, name, NF4DIR);
4779                 trace_nfs4_remove(dir, name, err);
4780                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4781                                 &exception);
4782         } while (exception.retry);
4783         return err;
4784 }
4785
4786 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4787                 struct dentry *dentry,
4788                 struct inode *inode)
4789 {
4790         struct nfs_removeargs *args = msg->rpc_argp;
4791         struct nfs_removeres *res = msg->rpc_resp;
4792
4793         res->server = NFS_SB(dentry->d_sb);
4794         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4795         nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4796
4797         nfs_fattr_init(res->dir_attr);
4798
4799         if (inode) {
4800                 nfs4_inode_return_delegation(inode);
4801                 nfs_d_prune_case_insensitive_aliases(inode);
4802         }
4803 }
4804
4805 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4806 {
4807         nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4808                         &data->args.seq_args,
4809                         &data->res.seq_res,
4810                         task);
4811 }
4812
4813 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4814 {
4815         struct nfs_unlinkdata *data = task->tk_calldata;
4816         struct nfs_removeres *res = &data->res;
4817
4818         if (!nfs4_sequence_done(task, &res->seq_res))
4819                 return 0;
4820         if (nfs4_async_handle_error(task, res->server, NULL,
4821                                     &data->timeout) == -EAGAIN)
4822                 return 0;
4823         if (task->tk_status == 0)
4824                 nfs4_update_changeattr(dir, &res->cinfo,
4825                                 res->dir_attr->time_start,
4826                                 NFS_INO_INVALID_DATA);
4827         return 1;
4828 }
4829
4830 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4831                 struct dentry *old_dentry,
4832                 struct dentry *new_dentry)
4833 {
4834         struct nfs_renameargs *arg = msg->rpc_argp;
4835         struct nfs_renameres *res = msg->rpc_resp;
4836         struct inode *old_inode = d_inode(old_dentry);
4837         struct inode *new_inode = d_inode(new_dentry);
4838
4839         if (old_inode)
4840                 nfs4_inode_make_writeable(old_inode);
4841         if (new_inode)
4842                 nfs4_inode_return_delegation(new_inode);
4843         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4844         res->server = NFS_SB(old_dentry->d_sb);
4845         nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4846 }
4847
4848 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4849 {
4850         nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4851                         &data->args.seq_args,
4852                         &data->res.seq_res,
4853                         task);
4854 }
4855
4856 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4857                                  struct inode *new_dir)
4858 {
4859         struct nfs_renamedata *data = task->tk_calldata;
4860         struct nfs_renameres *res = &data->res;
4861
4862         if (!nfs4_sequence_done(task, &res->seq_res))
4863                 return 0;
4864         if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4865                 return 0;
4866
4867         if (task->tk_status == 0) {
4868                 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4869                 if (new_dir != old_dir) {
4870                         /* Note: If we moved a directory, nlink will change */
4871                         nfs4_update_changeattr(old_dir, &res->old_cinfo,
4872                                         res->old_fattr->time_start,
4873                                         NFS_INO_INVALID_NLINK |
4874                                             NFS_INO_INVALID_DATA);
4875                         nfs4_update_changeattr(new_dir, &res->new_cinfo,
4876                                         res->new_fattr->time_start,
4877                                         NFS_INO_INVALID_NLINK |
4878                                             NFS_INO_INVALID_DATA);
4879                 } else
4880                         nfs4_update_changeattr(old_dir, &res->old_cinfo,
4881                                         res->old_fattr->time_start,
4882                                         NFS_INO_INVALID_DATA);
4883         }
4884         return 1;
4885 }
4886
4887 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4888 {
4889         struct nfs_server *server = NFS_SERVER(inode);
4890         __u32 bitmask[NFS4_BITMASK_SZ];
4891         struct nfs4_link_arg arg = {
4892                 .fh     = NFS_FH(inode),
4893                 .dir_fh = NFS_FH(dir),
4894                 .name   = name,
4895                 .bitmask = bitmask,
4896         };
4897         struct nfs4_link_res res = {
4898                 .server = server,
4899         };
4900         struct rpc_message msg = {
4901                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4902                 .rpc_argp = &arg,
4903                 .rpc_resp = &res,
4904         };
4905         int status = -ENOMEM;
4906
4907         res.fattr = nfs_alloc_fattr_with_label(server);
4908         if (res.fattr == NULL)
4909                 goto out;
4910
4911         nfs4_inode_make_writeable(inode);
4912         nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4913                                 NFS_INO_INVALID_CHANGE);
4914         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4915         if (!status) {
4916                 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4917                                        NFS_INO_INVALID_DATA);
4918                 nfs4_inc_nlink(inode);
4919                 status = nfs_post_op_update_inode(inode, res.fattr);
4920                 if (!status)
4921                         nfs_setsecurity(inode, res.fattr);
4922         }
4923
4924 out:
4925         nfs_free_fattr(res.fattr);
4926         return status;
4927 }
4928
4929 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4930 {
4931         struct nfs4_exception exception = {
4932                 .interruptible = true,
4933         };
4934         int err;
4935         do {
4936                 err = nfs4_handle_exception(NFS_SERVER(inode),
4937                                 _nfs4_proc_link(inode, dir, name),
4938                                 &exception);
4939         } while (exception.retry);
4940         return err;
4941 }
4942
4943 struct nfs4_createdata {
4944         struct rpc_message msg;
4945         struct nfs4_create_arg arg;
4946         struct nfs4_create_res res;
4947         struct nfs_fh fh;
4948         struct nfs_fattr fattr;
4949 };
4950
4951 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4952                 const struct qstr *name, struct iattr *sattr, u32 ftype)
4953 {
4954         struct nfs4_createdata *data;
4955
4956         data = kzalloc(sizeof(*data), GFP_KERNEL);
4957         if (data != NULL) {
4958                 struct nfs_server *server = NFS_SERVER(dir);
4959
4960                 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4961                 if (IS_ERR(data->fattr.label))
4962                         goto out_free;
4963
4964                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4965                 data->msg.rpc_argp = &data->arg;
4966                 data->msg.rpc_resp = &data->res;
4967                 data->arg.dir_fh = NFS_FH(dir);
4968                 data->arg.server = server;
4969                 data->arg.name = name;
4970                 data->arg.attrs = sattr;
4971                 data->arg.ftype = ftype;
4972                 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4973                 data->arg.umask = current_umask();
4974                 data->res.server = server;
4975                 data->res.fh = &data->fh;
4976                 data->res.fattr = &data->fattr;
4977                 nfs_fattr_init(data->res.fattr);
4978         }
4979         return data;
4980 out_free:
4981         kfree(data);
4982         return NULL;
4983 }
4984
4985 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4986 {
4987         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4988                                     &data->arg.seq_args, &data->res.seq_res, 1);
4989         if (status == 0) {
4990                 spin_lock(&dir->i_lock);
4991                 /* Creating a directory bumps nlink in the parent */
4992                 if (data->arg.ftype == NF4DIR)
4993                         nfs4_inc_nlink_locked(dir);
4994                 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4995                                               data->res.fattr->time_start,
4996                                               NFS_INO_INVALID_DATA);
4997                 spin_unlock(&dir->i_lock);
4998                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
4999         }
5000         return status;
5001 }
5002
5003 static void nfs4_free_createdata(struct nfs4_createdata *data)
5004 {
5005         nfs4_label_free(data->fattr.label);
5006         kfree(data);
5007 }
5008
5009 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5010                 struct page *page, unsigned int len, struct iattr *sattr,
5011                 struct nfs4_label *label)
5012 {
5013         struct nfs4_createdata *data;
5014         int status = -ENAMETOOLONG;
5015
5016         if (len > NFS4_MAXPATHLEN)
5017                 goto out;
5018
5019         status = -ENOMEM;
5020         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5021         if (data == NULL)
5022                 goto out;
5023
5024         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5025         data->arg.u.symlink.pages = &page;
5026         data->arg.u.symlink.len = len;
5027         data->arg.label = label;
5028         
5029         status = nfs4_do_create(dir, dentry, data);
5030
5031         nfs4_free_createdata(data);
5032 out:
5033         return status;
5034 }
5035
5036 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5037                 struct page *page, unsigned int len, struct iattr *sattr)
5038 {
5039         struct nfs4_exception exception = {
5040                 .interruptible = true,
5041         };
5042         struct nfs4_label l, *label;
5043         int err;
5044
5045         label = nfs4_label_init_security(dir, dentry, sattr, &l);
5046
5047         do {
5048                 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5049                 trace_nfs4_symlink(dir, &dentry->d_name, err);
5050                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5051                                 &exception);
5052         } while (exception.retry);
5053
5054         nfs4_label_release_security(label);
5055         return err;
5056 }
5057
5058 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5059                 struct iattr *sattr, struct nfs4_label *label)
5060 {
5061         struct nfs4_createdata *data;
5062         int status = -ENOMEM;
5063
5064         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5065         if (data == NULL)
5066                 goto out;
5067
5068         data->arg.label = label;
5069         status = nfs4_do_create(dir, dentry, data);
5070
5071         nfs4_free_createdata(data);
5072 out:
5073         return status;
5074 }
5075
5076 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5077                 struct iattr *sattr)
5078 {
5079         struct nfs_server *server = NFS_SERVER(dir);
5080         struct nfs4_exception exception = {
5081                 .interruptible = true,
5082         };
5083         struct nfs4_label l, *label;
5084         int err;
5085
5086         label = nfs4_label_init_security(dir, dentry, sattr, &l);
5087
5088         if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5089                 sattr->ia_mode &= ~current_umask();
5090         do {
5091                 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5092                 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5093                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5094                                 &exception);
5095         } while (exception.retry);
5096         nfs4_label_release_security(label);
5097
5098         return err;
5099 }
5100
5101 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5102                               struct nfs_readdir_res *nr_res)
5103 {
5104         struct inode            *dir = d_inode(nr_arg->dentry);
5105         struct nfs_server       *server = NFS_SERVER(dir);
5106         struct nfs4_readdir_arg args = {
5107                 .fh = NFS_FH(dir),
5108                 .pages = nr_arg->pages,
5109                 .pgbase = 0,
5110                 .count = nr_arg->page_len,
5111                 .plus = nr_arg->plus,
5112         };
5113         struct nfs4_readdir_res res;
5114         struct rpc_message msg = {
5115                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5116                 .rpc_argp = &args,
5117                 .rpc_resp = &res,
5118                 .rpc_cred = nr_arg->cred,
5119         };
5120         int                     status;
5121
5122         dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5123                 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5124         if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5125                 args.bitmask = server->attr_bitmask_nl;
5126         else
5127                 args.bitmask = server->attr_bitmask;
5128
5129         nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5130         res.pgbase = args.pgbase;
5131         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5132                         &res.seq_res, 0);
5133         if (status >= 0) {
5134                 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5135                 status += args.pgbase;
5136         }
5137
5138         nfs_invalidate_atime(dir);
5139
5140         dprintk("%s: returns %d\n", __func__, status);
5141         return status;
5142 }
5143
5144 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5145                              struct nfs_readdir_res *res)
5146 {
5147         struct nfs4_exception exception = {
5148                 .interruptible = true,
5149         };
5150         int err;
5151         do {
5152                 err = _nfs4_proc_readdir(arg, res);
5153                 trace_nfs4_readdir(d_inode(arg->dentry), err);
5154                 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5155                                             err, &exception);
5156         } while (exception.retry);
5157         return err;
5158 }
5159
5160 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5161                 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5162 {
5163         struct nfs4_createdata *data;
5164         int mode = sattr->ia_mode;
5165         int status = -ENOMEM;
5166
5167         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5168         if (data == NULL)
5169                 goto out;
5170
5171         if (S_ISFIFO(mode))
5172                 data->arg.ftype = NF4FIFO;
5173         else if (S_ISBLK(mode)) {
5174                 data->arg.ftype = NF4BLK;
5175                 data->arg.u.device.specdata1 = MAJOR(rdev);
5176                 data->arg.u.device.specdata2 = MINOR(rdev);
5177         }
5178         else if (S_ISCHR(mode)) {
5179                 data->arg.ftype = NF4CHR;
5180                 data->arg.u.device.specdata1 = MAJOR(rdev);
5181                 data->arg.u.device.specdata2 = MINOR(rdev);
5182         } else if (!S_ISSOCK(mode)) {
5183                 status = -EINVAL;
5184                 goto out_free;
5185         }
5186
5187         data->arg.label = label;
5188         status = nfs4_do_create(dir, dentry, data);
5189 out_free:
5190         nfs4_free_createdata(data);
5191 out:
5192         return status;
5193 }
5194
5195 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5196                 struct iattr *sattr, dev_t rdev)
5197 {
5198         struct nfs_server *server = NFS_SERVER(dir);
5199         struct nfs4_exception exception = {
5200                 .interruptible = true,
5201         };
5202         struct nfs4_label l, *label;
5203         int err;
5204
5205         label = nfs4_label_init_security(dir, dentry, sattr, &l);
5206
5207         if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5208                 sattr->ia_mode &= ~current_umask();
5209         do {
5210                 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5211                 trace_nfs4_mknod(dir, &dentry->d_name, err);
5212                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5213                                 &exception);
5214         } while (exception.retry);
5215
5216         nfs4_label_release_security(label);
5217
5218         return err;
5219 }
5220
5221 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5222                  struct nfs_fsstat *fsstat)
5223 {
5224         struct nfs4_statfs_arg args = {
5225                 .fh = fhandle,
5226                 .bitmask = server->attr_bitmask,
5227         };
5228         struct nfs4_statfs_res res = {
5229                 .fsstat = fsstat,
5230         };
5231         struct rpc_message msg = {
5232                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5233                 .rpc_argp = &args,
5234                 .rpc_resp = &res,
5235         };
5236
5237         nfs_fattr_init(fsstat->fattr);
5238         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5239 }
5240
5241 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5242 {
5243         struct nfs4_exception exception = {
5244                 .interruptible = true,
5245         };
5246         int err;
5247         do {
5248                 err = nfs4_handle_exception(server,
5249                                 _nfs4_proc_statfs(server, fhandle, fsstat),
5250                                 &exception);
5251         } while (exception.retry);
5252         return err;
5253 }
5254
5255 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5256                 struct nfs_fsinfo *fsinfo)
5257 {
5258         struct nfs4_fsinfo_arg args = {
5259                 .fh = fhandle,
5260                 .bitmask = server->attr_bitmask,
5261         };
5262         struct nfs4_fsinfo_res res = {
5263                 .fsinfo = fsinfo,
5264         };
5265         struct rpc_message msg = {
5266                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5267                 .rpc_argp = &args,
5268                 .rpc_resp = &res,
5269         };
5270
5271         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5272 }
5273
5274 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5275 {
5276         struct nfs4_exception exception = {
5277                 .interruptible = true,
5278         };
5279         int err;
5280
5281         do {
5282                 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5283                 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5284                 if (err == 0) {
5285                         nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5286                         break;
5287                 }
5288                 err = nfs4_handle_exception(server, err, &exception);
5289         } while (exception.retry);
5290         return err;
5291 }
5292
5293 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5294 {
5295         int error;
5296
5297         nfs_fattr_init(fsinfo->fattr);
5298         error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5299         if (error == 0) {
5300                 /* block layout checks this! */
5301                 server->pnfs_blksize = fsinfo->blksize;
5302                 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5303         }
5304
5305         return error;
5306 }
5307
5308 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5309                 struct nfs_pathconf *pathconf)
5310 {
5311         struct nfs4_pathconf_arg args = {
5312                 .fh = fhandle,
5313                 .bitmask = server->attr_bitmask,
5314         };
5315         struct nfs4_pathconf_res res = {
5316                 .pathconf = pathconf,
5317         };
5318         struct rpc_message msg = {
5319                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5320                 .rpc_argp = &args,
5321                 .rpc_resp = &res,
5322         };
5323
5324         /* None of the pathconf attributes are mandatory to implement */
5325         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5326                 memset(pathconf, 0, sizeof(*pathconf));
5327                 return 0;
5328         }
5329
5330         nfs_fattr_init(pathconf->fattr);
5331         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5332 }
5333
5334 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5335                 struct nfs_pathconf *pathconf)
5336 {
5337         struct nfs4_exception exception = {
5338                 .interruptible = true,
5339         };
5340         int err;
5341
5342         do {
5343                 err = nfs4_handle_exception(server,
5344                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
5345                                 &exception);
5346         } while (exception.retry);
5347         return err;
5348 }
5349
5350 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5351                 const struct nfs_open_context *ctx,
5352                 const struct nfs_lock_context *l_ctx,
5353                 fmode_t fmode)
5354 {
5355         return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5356 }
5357 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5358
5359 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5360                 const struct nfs_open_context *ctx,
5361                 const struct nfs_lock_context *l_ctx,
5362                 fmode_t fmode)
5363 {
5364         nfs4_stateid _current_stateid;
5365
5366         /* If the current stateid represents a lost lock, then exit */
5367         if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5368                 return true;
5369         return nfs4_stateid_match(stateid, &_current_stateid);
5370 }
5371
5372 static bool nfs4_error_stateid_expired(int err)
5373 {
5374         switch (err) {
5375         case -NFS4ERR_DELEG_REVOKED:
5376         case -NFS4ERR_ADMIN_REVOKED:
5377         case -NFS4ERR_BAD_STATEID:
5378         case -NFS4ERR_STALE_STATEID:
5379         case -NFS4ERR_OLD_STATEID:
5380         case -NFS4ERR_OPENMODE:
5381         case -NFS4ERR_EXPIRED:
5382                 return true;
5383         }
5384         return false;
5385 }
5386
5387 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5388 {
5389         struct nfs_server *server = NFS_SERVER(hdr->inode);
5390
5391         trace_nfs4_read(hdr, task->tk_status);
5392         if (task->tk_status < 0) {
5393                 struct nfs4_exception exception = {
5394                         .inode = hdr->inode,
5395                         .state = hdr->args.context->state,
5396                         .stateid = &hdr->args.stateid,
5397                 };
5398                 task->tk_status = nfs4_async_handle_exception(task,
5399                                 server, task->tk_status, &exception);
5400                 if (exception.retry) {
5401                         rpc_restart_call_prepare(task);
5402                         return -EAGAIN;
5403                 }
5404         }
5405
5406         if (task->tk_status > 0)
5407                 renew_lease(server, hdr->timestamp);
5408         return 0;
5409 }
5410
5411 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5412                 struct nfs_pgio_args *args)
5413 {
5414
5415         if (!nfs4_error_stateid_expired(task->tk_status) ||
5416                 nfs4_stateid_is_current(&args->stateid,
5417                                 args->context,
5418                                 args->lock_context,
5419                                 FMODE_READ))
5420                 return false;
5421         rpc_restart_call_prepare(task);
5422         return true;
5423 }
5424
5425 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5426                                          struct nfs_pgio_header *hdr)
5427 {
5428         struct nfs_server *server = NFS_SERVER(hdr->inode);
5429         struct rpc_message *msg = &task->tk_msg;
5430
5431         if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5432             server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5433                 server->caps &= ~NFS_CAP_READ_PLUS;
5434                 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5435                 rpc_restart_call_prepare(task);
5436                 return true;
5437         }
5438         return false;
5439 }
5440
5441 static inline void nfs4_read_plus_scratch_free(struct nfs_pgio_header *hdr)
5442 {
5443         if (hdr->res.scratch) {
5444                 kfree(hdr->res.scratch);
5445                 hdr->res.scratch = NULL;
5446         }
5447 }
5448
5449 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5450 {
5451         nfs4_read_plus_scratch_free(hdr);
5452
5453         if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5454                 return -EAGAIN;
5455         if (nfs4_read_stateid_changed(task, &hdr->args))
5456                 return -EAGAIN;
5457         if (nfs4_read_plus_not_supported(task, hdr))
5458                 return -EAGAIN;
5459         if (task->tk_status > 0)
5460                 nfs_invalidate_atime(hdr->inode);
5461         return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5462                                     nfs4_read_done_cb(task, hdr);
5463 }
5464
5465 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5466 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5467                                     struct rpc_message *msg)
5468 {
5469         /* Note: We don't use READ_PLUS with pNFS yet */
5470         if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
5471                 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5472                 hdr->res.scratch = kmalloc(32, GFP_KERNEL);
5473                 return hdr->res.scratch != NULL;
5474         }
5475         return false;
5476 }
5477 #else
5478 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5479                                     struct rpc_message *msg)
5480 {
5481         return false;
5482 }
5483 #endif /* CONFIG_NFS_V4_2 */
5484
5485 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5486                                  struct rpc_message *msg)
5487 {
5488         hdr->timestamp   = jiffies;
5489         if (!hdr->pgio_done_cb)
5490                 hdr->pgio_done_cb = nfs4_read_done_cb;
5491         if (!nfs42_read_plus_support(hdr, msg))
5492                 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5493         nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5494 }
5495
5496 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5497                                       struct nfs_pgio_header *hdr)
5498 {
5499         if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5500                         &hdr->args.seq_args,
5501                         &hdr->res.seq_res,
5502                         task))
5503                 return 0;
5504         if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5505                                 hdr->args.lock_context,
5506                                 hdr->rw_mode) == -EIO)
5507                 return -EIO;
5508         if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5509                 return -EIO;
5510         return 0;
5511 }
5512
5513 static int nfs4_write_done_cb(struct rpc_task *task,
5514                               struct nfs_pgio_header *hdr)
5515 {
5516         struct inode *inode = hdr->inode;
5517
5518         trace_nfs4_write(hdr, task->tk_status);
5519         if (task->tk_status < 0) {
5520                 struct nfs4_exception exception = {
5521                         .inode = hdr->inode,
5522                         .state = hdr->args.context->state,
5523                         .stateid = &hdr->args.stateid,
5524                 };
5525                 task->tk_status = nfs4_async_handle_exception(task,
5526                                 NFS_SERVER(inode), task->tk_status,
5527                                 &exception);
5528                 if (exception.retry) {
5529                         rpc_restart_call_prepare(task);
5530                         return -EAGAIN;
5531                 }
5532         }
5533         if (task->tk_status >= 0) {
5534                 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5535                 nfs_writeback_update_inode(hdr);
5536         }
5537         return 0;
5538 }
5539
5540 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5541                 struct nfs_pgio_args *args)
5542 {
5543
5544         if (!nfs4_error_stateid_expired(task->tk_status) ||
5545                 nfs4_stateid_is_current(&args->stateid,
5546                                 args->context,
5547                                 args->lock_context,
5548                                 FMODE_WRITE))
5549                 return false;
5550         rpc_restart_call_prepare(task);
5551         return true;
5552 }
5553
5554 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5555 {
5556         if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5557                 return -EAGAIN;
5558         if (nfs4_write_stateid_changed(task, &hdr->args))
5559                 return -EAGAIN;
5560         return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5561                 nfs4_write_done_cb(task, hdr);
5562 }
5563
5564 static
5565 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5566 {
5567         /* Don't request attributes for pNFS or O_DIRECT writes */
5568         if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5569                 return false;
5570         /* Otherwise, request attributes if and only if we don't hold
5571          * a delegation
5572          */
5573         return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5574 }
5575
5576 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5577                       struct inode *inode, unsigned long cache_validity)
5578 {
5579         struct nfs_server *server = NFS_SERVER(inode);
5580         unsigned int i;
5581
5582         memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5583         cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5584
5585         if (cache_validity & NFS_INO_INVALID_CHANGE)
5586                 bitmask[0] |= FATTR4_WORD0_CHANGE;
5587         if (cache_validity & NFS_INO_INVALID_ATIME)
5588                 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5589         if (cache_validity & NFS_INO_INVALID_MODE)
5590                 bitmask[1] |= FATTR4_WORD1_MODE;
5591         if (cache_validity & NFS_INO_INVALID_OTHER)
5592                 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5593         if (cache_validity & NFS_INO_INVALID_NLINK)
5594                 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5595         if (cache_validity & NFS_INO_INVALID_CTIME)
5596                 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5597         if (cache_validity & NFS_INO_INVALID_MTIME)
5598                 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5599         if (cache_validity & NFS_INO_INVALID_BLOCKS)
5600                 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5601
5602         if (cache_validity & NFS_INO_INVALID_SIZE)
5603                 bitmask[0] |= FATTR4_WORD0_SIZE;
5604
5605         for (i = 0; i < NFS4_BITMASK_SZ; i++)
5606                 bitmask[i] &= server->attr_bitmask[i];
5607 }
5608
5609 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5610                                   struct rpc_message *msg,
5611                                   struct rpc_clnt **clnt)
5612 {
5613         struct nfs_server *server = NFS_SERVER(hdr->inode);
5614
5615         if (!nfs4_write_need_cache_consistency_data(hdr)) {
5616                 hdr->args.bitmask = NULL;
5617                 hdr->res.fattr = NULL;
5618         } else {
5619                 nfs4_bitmask_set(hdr->args.bitmask_store,
5620                                  server->cache_consistency_bitmask,
5621                                  hdr->inode, NFS_INO_INVALID_BLOCKS);
5622                 hdr->args.bitmask = hdr->args.bitmask_store;
5623         }
5624
5625         if (!hdr->pgio_done_cb)
5626                 hdr->pgio_done_cb = nfs4_write_done_cb;
5627         hdr->res.server = server;
5628         hdr->timestamp   = jiffies;
5629
5630         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5631         nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5632         nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5633 }
5634
5635 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5636 {
5637         nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5638                         &data->args.seq_args,
5639                         &data->res.seq_res,
5640                         task);
5641 }
5642
5643 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5644 {
5645         struct inode *inode = data->inode;
5646
5647         trace_nfs4_commit(data, task->tk_status);
5648         if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5649                                     NULL, NULL) == -EAGAIN) {
5650                 rpc_restart_call_prepare(task);
5651                 return -EAGAIN;
5652         }
5653         return 0;
5654 }
5655
5656 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5657 {
5658         if (!nfs4_sequence_done(task, &data->res.seq_res))
5659                 return -EAGAIN;
5660         return data->commit_done_cb(task, data);
5661 }
5662
5663 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5664                                    struct rpc_clnt **clnt)
5665 {
5666         struct nfs_server *server = NFS_SERVER(data->inode);
5667
5668         if (data->commit_done_cb == NULL)
5669                 data->commit_done_cb = nfs4_commit_done_cb;
5670         data->res.server = server;
5671         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5672         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5673         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5674 }
5675
5676 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5677                                 struct nfs_commitres *res)
5678 {
5679         struct inode *dst_inode = file_inode(dst);
5680         struct nfs_server *server = NFS_SERVER(dst_inode);
5681         struct rpc_message msg = {
5682                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5683                 .rpc_argp = args,
5684                 .rpc_resp = res,
5685         };
5686
5687         args->fh = NFS_FH(dst_inode);
5688         return nfs4_call_sync(server->client, server, &msg,
5689                         &args->seq_args, &res->seq_res, 1);
5690 }
5691
5692 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5693 {
5694         struct nfs_commitargs args = {
5695                 .offset = offset,
5696                 .count = count,
5697         };
5698         struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5699         struct nfs4_exception exception = { };
5700         int status;
5701
5702         do {
5703                 status = _nfs4_proc_commit(dst, &args, res);
5704                 status = nfs4_handle_exception(dst_server, status, &exception);
5705         } while (exception.retry);
5706
5707         return status;
5708 }
5709
5710 struct nfs4_renewdata {
5711         struct nfs_client       *client;
5712         unsigned long           timestamp;
5713 };
5714
5715 /*
5716  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5717  * standalone procedure for queueing an asynchronous RENEW.
5718  */
5719 static void nfs4_renew_release(void *calldata)
5720 {
5721         struct nfs4_renewdata *data = calldata;
5722         struct nfs_client *clp = data->client;
5723
5724         if (refcount_read(&clp->cl_count) > 1)
5725                 nfs4_schedule_state_renewal(clp);
5726         nfs_put_client(clp);
5727         kfree(data);
5728 }
5729
5730 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5731 {
5732         struct nfs4_renewdata *data = calldata;
5733         struct nfs_client *clp = data->client;
5734         unsigned long timestamp = data->timestamp;
5735
5736         trace_nfs4_renew_async(clp, task->tk_status);
5737         switch (task->tk_status) {
5738         case 0:
5739                 break;
5740         case -NFS4ERR_LEASE_MOVED:
5741                 nfs4_schedule_lease_moved_recovery(clp);
5742                 break;
5743         default:
5744                 /* Unless we're shutting down, schedule state recovery! */
5745                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5746                         return;
5747                 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5748                         nfs4_schedule_lease_recovery(clp);
5749                         return;
5750                 }
5751                 nfs4_schedule_path_down_recovery(clp);
5752         }
5753         do_renew_lease(clp, timestamp);
5754 }
5755
5756 static const struct rpc_call_ops nfs4_renew_ops = {
5757         .rpc_call_done = nfs4_renew_done,
5758         .rpc_release = nfs4_renew_release,
5759 };
5760
5761 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5762 {
5763         struct rpc_message msg = {
5764                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5765                 .rpc_argp       = clp,
5766                 .rpc_cred       = cred,
5767         };
5768         struct nfs4_renewdata *data;
5769
5770         if (renew_flags == 0)
5771                 return 0;
5772         if (!refcount_inc_not_zero(&clp->cl_count))
5773                 return -EIO;
5774         data = kmalloc(sizeof(*data), GFP_NOFS);
5775         if (data == NULL) {
5776                 nfs_put_client(clp);
5777                 return -ENOMEM;
5778         }
5779         data->client = clp;
5780         data->timestamp = jiffies;
5781         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5782                         &nfs4_renew_ops, data);
5783 }
5784
5785 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5786 {
5787         struct rpc_message msg = {
5788                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5789                 .rpc_argp       = clp,
5790                 .rpc_cred       = cred,
5791         };
5792         unsigned long now = jiffies;
5793         int status;
5794
5795         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5796         if (status < 0)
5797                 return status;
5798         do_renew_lease(clp, now);
5799         return 0;
5800 }
5801
5802 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5803                                       enum nfs4_acl_type type)
5804 {
5805         switch (type) {
5806         default:
5807                 return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5808         case NFS4ACL_DACL:
5809                 return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5810         case NFS4ACL_SACL:
5811                 return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5812         }
5813 }
5814
5815 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5816  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5817  * the stack.
5818  */
5819 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5820
5821 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5822                 struct page **pages)
5823 {
5824         struct page *newpage, **spages;
5825         int rc = 0;
5826         size_t len;
5827         spages = pages;
5828
5829         do {
5830                 len = min_t(size_t, PAGE_SIZE, buflen);
5831                 newpage = alloc_page(GFP_KERNEL);
5832
5833                 if (newpage == NULL)
5834                         goto unwind;
5835                 memcpy(page_address(newpage), buf, len);
5836                 buf += len;
5837                 buflen -= len;
5838                 *pages++ = newpage;
5839                 rc++;
5840         } while (buflen != 0);
5841
5842         return rc;
5843
5844 unwind:
5845         for(; rc > 0; rc--)
5846                 __free_page(spages[rc-1]);
5847         return -ENOMEM;
5848 }
5849
5850 struct nfs4_cached_acl {
5851         enum nfs4_acl_type type;
5852         int cached;
5853         size_t len;
5854         char data[];
5855 };
5856
5857 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5858 {
5859         struct nfs_inode *nfsi = NFS_I(inode);
5860
5861         spin_lock(&inode->i_lock);
5862         kfree(nfsi->nfs4_acl);
5863         nfsi->nfs4_acl = acl;
5864         spin_unlock(&inode->i_lock);
5865 }
5866
5867 static void nfs4_zap_acl_attr(struct inode *inode)
5868 {
5869         nfs4_set_cached_acl(inode, NULL);
5870 }
5871
5872 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5873                                     size_t buflen, enum nfs4_acl_type type)
5874 {
5875         struct nfs_inode *nfsi = NFS_I(inode);
5876         struct nfs4_cached_acl *acl;
5877         int ret = -ENOENT;
5878
5879         spin_lock(&inode->i_lock);
5880         acl = nfsi->nfs4_acl;
5881         if (acl == NULL)
5882                 goto out;
5883         if (acl->type != type)
5884                 goto out;
5885         if (buf == NULL) /* user is just asking for length */
5886                 goto out_len;
5887         if (acl->cached == 0)
5888                 goto out;
5889         ret = -ERANGE; /* see getxattr(2) man page */
5890         if (acl->len > buflen)
5891                 goto out;
5892         memcpy(buf, acl->data, acl->len);
5893 out_len:
5894         ret = acl->len;
5895 out:
5896         spin_unlock(&inode->i_lock);
5897         return ret;
5898 }
5899
5900 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5901                                   size_t pgbase, size_t acl_len,
5902                                   enum nfs4_acl_type type)
5903 {
5904         struct nfs4_cached_acl *acl;
5905         size_t buflen = sizeof(*acl) + acl_len;
5906
5907         if (buflen <= PAGE_SIZE) {
5908                 acl = kmalloc(buflen, GFP_KERNEL);
5909                 if (acl == NULL)
5910                         goto out;
5911                 acl->cached = 1;
5912                 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5913         } else {
5914                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5915                 if (acl == NULL)
5916                         goto out;
5917                 acl->cached = 0;
5918         }
5919         acl->type = type;
5920         acl->len = acl_len;
5921 out:
5922         nfs4_set_cached_acl(inode, acl);
5923 }
5924
5925 /*
5926  * The getxattr API returns the required buffer length when called with a
5927  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5928  * the required buf.  On a NULL buf, we send a page of data to the server
5929  * guessing that the ACL request can be serviced by a page. If so, we cache
5930  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5931  * the cache. If not so, we throw away the page, and cache the required
5932  * length. The next getxattr call will then produce another round trip to
5933  * the server, this time with the input buf of the required size.
5934  */
5935 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5936                                        size_t buflen, enum nfs4_acl_type type)
5937 {
5938         struct page **pages;
5939         struct nfs_getaclargs args = {
5940                 .fh = NFS_FH(inode),
5941                 .acl_type = type,
5942                 .acl_len = buflen,
5943         };
5944         struct nfs_getaclres res = {
5945                 .acl_type = type,
5946                 .acl_len = buflen,
5947         };
5948         struct rpc_message msg = {
5949                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5950                 .rpc_argp = &args,
5951                 .rpc_resp = &res,
5952         };
5953         unsigned int npages;
5954         int ret = -ENOMEM, i;
5955         struct nfs_server *server = NFS_SERVER(inode);
5956
5957         if (buflen == 0)
5958                 buflen = server->rsize;
5959
5960         npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5961         pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5962         if (!pages)
5963                 return -ENOMEM;
5964
5965         args.acl_pages = pages;
5966
5967         for (i = 0; i < npages; i++) {
5968                 pages[i] = alloc_page(GFP_KERNEL);
5969                 if (!pages[i])
5970                         goto out_free;
5971         }
5972
5973         /* for decoding across pages */
5974         res.acl_scratch = alloc_page(GFP_KERNEL);
5975         if (!res.acl_scratch)
5976                 goto out_free;
5977
5978         args.acl_len = npages * PAGE_SIZE;
5979
5980         dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
5981                 __func__, buf, buflen, npages, args.acl_len);
5982         ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5983                              &msg, &args.seq_args, &res.seq_res, 0);
5984         if (ret)
5985                 goto out_free;
5986
5987         /* Handle the case where the passed-in buffer is too short */
5988         if (res.acl_flags & NFS4_ACL_TRUNC) {
5989                 /* Did the user only issue a request for the acl length? */
5990                 if (buf == NULL)
5991                         goto out_ok;
5992                 ret = -ERANGE;
5993                 goto out_free;
5994         }
5995         nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
5996                               type);
5997         if (buf) {
5998                 if (res.acl_len > buflen) {
5999                         ret = -ERANGE;
6000                         goto out_free;
6001                 }
6002                 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
6003         }
6004 out_ok:
6005         ret = res.acl_len;
6006 out_free:
6007         while (--i >= 0)
6008                 __free_page(pages[i]);
6009         if (res.acl_scratch)
6010                 __free_page(res.acl_scratch);
6011         kfree(pages);
6012         return ret;
6013 }
6014
6015 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6016                                      size_t buflen, enum nfs4_acl_type type)
6017 {
6018         struct nfs4_exception exception = {
6019                 .interruptible = true,
6020         };
6021         ssize_t ret;
6022         do {
6023                 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6024                 trace_nfs4_get_acl(inode, ret);
6025                 if (ret >= 0)
6026                         break;
6027                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6028         } while (exception.retry);
6029         return ret;
6030 }
6031
6032 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6033                                  enum nfs4_acl_type type)
6034 {
6035         struct nfs_server *server = NFS_SERVER(inode);
6036         int ret;
6037
6038         if (!nfs4_server_supports_acls(server, type))
6039                 return -EOPNOTSUPP;
6040         ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6041         if (ret < 0)
6042                 return ret;
6043         if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6044                 nfs_zap_acl_cache(inode);
6045         ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6046         if (ret != -ENOENT)
6047                 /* -ENOENT is returned if there is no ACL or if there is an ACL
6048                  * but no cached acl data, just the acl length */
6049                 return ret;
6050         return nfs4_get_acl_uncached(inode, buf, buflen, type);
6051 }
6052
6053 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6054                                size_t buflen, enum nfs4_acl_type type)
6055 {
6056         struct nfs_server *server = NFS_SERVER(inode);
6057         struct page *pages[NFS4ACL_MAXPAGES];
6058         struct nfs_setaclargs arg = {
6059                 .fh = NFS_FH(inode),
6060                 .acl_type = type,
6061                 .acl_len = buflen,
6062                 .acl_pages = pages,
6063         };
6064         struct nfs_setaclres res;
6065         struct rpc_message msg = {
6066                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6067                 .rpc_argp       = &arg,
6068                 .rpc_resp       = &res,
6069         };
6070         unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6071         int ret, i;
6072
6073         /* You can't remove system.nfs4_acl: */
6074         if (buflen == 0)
6075                 return -EINVAL;
6076         if (!nfs4_server_supports_acls(server, type))
6077                 return -EOPNOTSUPP;
6078         if (npages > ARRAY_SIZE(pages))
6079                 return -ERANGE;
6080         i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6081         if (i < 0)
6082                 return i;
6083         nfs4_inode_make_writeable(inode);
6084         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6085
6086         /*
6087          * Free each page after tx, so the only ref left is
6088          * held by the network stack
6089          */
6090         for (; i > 0; i--)
6091                 put_page(pages[i-1]);
6092
6093         /*
6094          * Acl update can result in inode attribute update.
6095          * so mark the attribute cache invalid.
6096          */
6097         spin_lock(&inode->i_lock);
6098         nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6099                                              NFS_INO_INVALID_CTIME |
6100                                              NFS_INO_REVAL_FORCED);
6101         spin_unlock(&inode->i_lock);
6102         nfs_access_zap_cache(inode);
6103         nfs_zap_acl_cache(inode);
6104         return ret;
6105 }
6106
6107 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6108                              size_t buflen, enum nfs4_acl_type type)
6109 {
6110         struct nfs4_exception exception = { };
6111         int err;
6112         do {
6113                 err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6114                 trace_nfs4_set_acl(inode, err);
6115                 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6116                         /*
6117                          * no need to retry since the kernel
6118                          * isn't involved in encoding the ACEs.
6119                          */
6120                         err = -EINVAL;
6121                         break;
6122                 }
6123                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6124                                 &exception);
6125         } while (exception.retry);
6126         return err;
6127 }
6128
6129 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6130 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6131                                         size_t buflen)
6132 {
6133         struct nfs_server *server = NFS_SERVER(inode);
6134         struct nfs4_label label = {0, 0, buflen, buf};
6135
6136         u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6137         struct nfs_fattr fattr = {
6138                 .label = &label,
6139         };
6140         struct nfs4_getattr_arg arg = {
6141                 .fh             = NFS_FH(inode),
6142                 .bitmask        = bitmask,
6143         };
6144         struct nfs4_getattr_res res = {
6145                 .fattr          = &fattr,
6146                 .server         = server,
6147         };
6148         struct rpc_message msg = {
6149                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6150                 .rpc_argp       = &arg,
6151                 .rpc_resp       = &res,
6152         };
6153         int ret;
6154
6155         nfs_fattr_init(&fattr);
6156
6157         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6158         if (ret)
6159                 return ret;
6160         if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6161                 return -ENOENT;
6162         return label.len;
6163 }
6164
6165 static int nfs4_get_security_label(struct inode *inode, void *buf,
6166                                         size_t buflen)
6167 {
6168         struct nfs4_exception exception = {
6169                 .interruptible = true,
6170         };
6171         int err;
6172
6173         if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6174                 return -EOPNOTSUPP;
6175
6176         do {
6177                 err = _nfs4_get_security_label(inode, buf, buflen);
6178                 trace_nfs4_get_security_label(inode, err);
6179                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6180                                 &exception);
6181         } while (exception.retry);
6182         return err;
6183 }
6184
6185 static int _nfs4_do_set_security_label(struct inode *inode,
6186                 struct nfs4_label *ilabel,
6187                 struct nfs_fattr *fattr)
6188 {
6189
6190         struct iattr sattr = {0};
6191         struct nfs_server *server = NFS_SERVER(inode);
6192         const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6193         struct nfs_setattrargs arg = {
6194                 .fh             = NFS_FH(inode),
6195                 .iap            = &sattr,
6196                 .server         = server,
6197                 .bitmask        = bitmask,
6198                 .label          = ilabel,
6199         };
6200         struct nfs_setattrres res = {
6201                 .fattr          = fattr,
6202                 .server         = server,
6203         };
6204         struct rpc_message msg = {
6205                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6206                 .rpc_argp       = &arg,
6207                 .rpc_resp       = &res,
6208         };
6209         int status;
6210
6211         nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6212
6213         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6214         if (status)
6215                 dprintk("%s failed: %d\n", __func__, status);
6216
6217         return status;
6218 }
6219
6220 static int nfs4_do_set_security_label(struct inode *inode,
6221                 struct nfs4_label *ilabel,
6222                 struct nfs_fattr *fattr)
6223 {
6224         struct nfs4_exception exception = { };
6225         int err;
6226
6227         do {
6228                 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6229                 trace_nfs4_set_security_label(inode, err);
6230                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6231                                 &exception);
6232         } while (exception.retry);
6233         return err;
6234 }
6235
6236 static int
6237 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6238 {
6239         struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6240         struct nfs_fattr *fattr;
6241         int status;
6242
6243         if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6244                 return -EOPNOTSUPP;
6245
6246         fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6247         if (fattr == NULL)
6248                 return -ENOMEM;
6249
6250         status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6251         if (status == 0)
6252                 nfs_setsecurity(inode, fattr);
6253
6254         return status;
6255 }
6256 #endif  /* CONFIG_NFS_V4_SECURITY_LABEL */
6257
6258
6259 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6260                                     nfs4_verifier *bootverf)
6261 {
6262         __be32 verf[2];
6263
6264         if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6265                 /* An impossible timestamp guarantees this value
6266                  * will never match a generated boot time. */
6267                 verf[0] = cpu_to_be32(U32_MAX);
6268                 verf[1] = cpu_to_be32(U32_MAX);
6269         } else {
6270                 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6271                 u64 ns = ktime_to_ns(nn->boot_time);
6272
6273                 verf[0] = cpu_to_be32(ns >> 32);
6274                 verf[1] = cpu_to_be32(ns);
6275         }
6276         memcpy(bootverf->data, verf, sizeof(bootverf->data));
6277 }
6278
6279 static size_t
6280 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6281 {
6282         struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6283         struct nfs_netns_client *nn_clp = nn->nfs_client;
6284         const char *id;
6285
6286         buf[0] = '\0';
6287
6288         if (nn_clp) {
6289                 rcu_read_lock();
6290                 id = rcu_dereference(nn_clp->identifier);
6291                 if (id)
6292                         strscpy(buf, id, buflen);
6293                 rcu_read_unlock();
6294         }
6295
6296         if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6297                 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6298
6299         return strlen(buf);
6300 }
6301
6302 static int
6303 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6304 {
6305         char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6306         size_t buflen;
6307         size_t len;
6308         char *str;
6309
6310         if (clp->cl_owner_id != NULL)
6311                 return 0;
6312
6313         rcu_read_lock();
6314         len = 14 +
6315                 strlen(clp->cl_rpcclient->cl_nodename) +
6316                 1 +
6317                 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6318                 1;
6319         rcu_read_unlock();
6320
6321         buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6322         if (buflen)
6323                 len += buflen + 1;
6324
6325         if (len > NFS4_OPAQUE_LIMIT + 1)
6326                 return -EINVAL;
6327
6328         /*
6329          * Since this string is allocated at mount time, and held until the
6330          * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6331          * about a memory-reclaim deadlock.
6332          */
6333         str = kmalloc(len, GFP_KERNEL);
6334         if (!str)
6335                 return -ENOMEM;
6336
6337         rcu_read_lock();
6338         if (buflen)
6339                 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6340                           clp->cl_rpcclient->cl_nodename, buf,
6341                           rpc_peeraddr2str(clp->cl_rpcclient,
6342                                            RPC_DISPLAY_ADDR));
6343         else
6344                 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6345                           clp->cl_rpcclient->cl_nodename,
6346                           rpc_peeraddr2str(clp->cl_rpcclient,
6347                                            RPC_DISPLAY_ADDR));
6348         rcu_read_unlock();
6349
6350         clp->cl_owner_id = str;
6351         return 0;
6352 }
6353
6354 static int
6355 nfs4_init_uniform_client_string(struct nfs_client *clp)
6356 {
6357         char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6358         size_t buflen;
6359         size_t len;
6360         char *str;
6361
6362         if (clp->cl_owner_id != NULL)
6363                 return 0;
6364
6365         len = 10 + 10 + 1 + 10 + 1 +
6366                 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6367
6368         buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6369         if (buflen)
6370                 len += buflen + 1;
6371
6372         if (len > NFS4_OPAQUE_LIMIT + 1)
6373                 return -EINVAL;
6374
6375         /*
6376          * Since this string is allocated at mount time, and held until the
6377          * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6378          * about a memory-reclaim deadlock.
6379          */
6380         str = kmalloc(len, GFP_KERNEL);
6381         if (!str)
6382                 return -ENOMEM;
6383
6384         if (buflen)
6385                 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6386                           clp->rpc_ops->version, clp->cl_minorversion,
6387                           buf, clp->cl_rpcclient->cl_nodename);
6388         else
6389                 scnprintf(str, len, "Linux NFSv%u.%u %s",
6390                           clp->rpc_ops->version, clp->cl_minorversion,
6391                           clp->cl_rpcclient->cl_nodename);
6392         clp->cl_owner_id = str;
6393         return 0;
6394 }
6395
6396 /*
6397  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6398  * services.  Advertise one based on the address family of the
6399  * clientaddr.
6400  */
6401 static unsigned int
6402 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6403 {
6404         if (strchr(clp->cl_ipaddr, ':') != NULL)
6405                 return scnprintf(buf, len, "tcp6");
6406         else
6407                 return scnprintf(buf, len, "tcp");
6408 }
6409
6410 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6411 {
6412         struct nfs4_setclientid *sc = calldata;
6413
6414         if (task->tk_status == 0)
6415                 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6416 }
6417
6418 static const struct rpc_call_ops nfs4_setclientid_ops = {
6419         .rpc_call_done = nfs4_setclientid_done,
6420 };
6421
6422 /**
6423  * nfs4_proc_setclientid - Negotiate client ID
6424  * @clp: state data structure
6425  * @program: RPC program for NFSv4 callback service
6426  * @port: IP port number for NFS4 callback service
6427  * @cred: credential to use for this call
6428  * @res: where to place the result
6429  *
6430  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6431  */
6432 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6433                 unsigned short port, const struct cred *cred,
6434                 struct nfs4_setclientid_res *res)
6435 {
6436         nfs4_verifier sc_verifier;
6437         struct nfs4_setclientid setclientid = {
6438                 .sc_verifier = &sc_verifier,
6439                 .sc_prog = program,
6440                 .sc_clnt = clp,
6441         };
6442         struct rpc_message msg = {
6443                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6444                 .rpc_argp = &setclientid,
6445                 .rpc_resp = res,
6446                 .rpc_cred = cred,
6447         };
6448         struct rpc_task_setup task_setup_data = {
6449                 .rpc_client = clp->cl_rpcclient,
6450                 .rpc_message = &msg,
6451                 .callback_ops = &nfs4_setclientid_ops,
6452                 .callback_data = &setclientid,
6453                 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6454         };
6455         unsigned long now = jiffies;
6456         int status;
6457
6458         /* nfs_client_id4 */
6459         nfs4_init_boot_verifier(clp, &sc_verifier);
6460
6461         if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6462                 status = nfs4_init_uniform_client_string(clp);
6463         else
6464                 status = nfs4_init_nonuniform_client_string(clp);
6465
6466         if (status)
6467                 goto out;
6468
6469         /* cb_client4 */
6470         setclientid.sc_netid_len =
6471                                 nfs4_init_callback_netid(clp,
6472                                                 setclientid.sc_netid,
6473                                                 sizeof(setclientid.sc_netid));
6474         setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6475                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6476                                 clp->cl_ipaddr, port >> 8, port & 255);
6477
6478         dprintk("NFS call  setclientid auth=%s, '%s'\n",
6479                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6480                 clp->cl_owner_id);
6481
6482         status = nfs4_call_sync_custom(&task_setup_data);
6483         if (setclientid.sc_cred) {
6484                 kfree(clp->cl_acceptor);
6485                 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6486                 put_rpccred(setclientid.sc_cred);
6487         }
6488
6489         if (status == 0)
6490                 do_renew_lease(clp, now);
6491 out:
6492         trace_nfs4_setclientid(clp, status);
6493         dprintk("NFS reply setclientid: %d\n", status);
6494         return status;
6495 }
6496
6497 /**
6498  * nfs4_proc_setclientid_confirm - Confirm client ID
6499  * @clp: state data structure
6500  * @arg: result of a previous SETCLIENTID
6501  * @cred: credential to use for this call
6502  *
6503  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6504  */
6505 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6506                 struct nfs4_setclientid_res *arg,
6507                 const struct cred *cred)
6508 {
6509         struct rpc_message msg = {
6510                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6511                 .rpc_argp = arg,
6512                 .rpc_cred = cred,
6513         };
6514         int status;
6515
6516         dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
6517                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6518                 clp->cl_clientid);
6519         status = rpc_call_sync(clp->cl_rpcclient, &msg,
6520                                RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6521         trace_nfs4_setclientid_confirm(clp, status);
6522         dprintk("NFS reply setclientid_confirm: %d\n", status);
6523         return status;
6524 }
6525
6526 struct nfs4_delegreturndata {
6527         struct nfs4_delegreturnargs args;
6528         struct nfs4_delegreturnres res;
6529         struct nfs_fh fh;
6530         nfs4_stateid stateid;
6531         unsigned long timestamp;
6532         struct {
6533                 struct nfs4_layoutreturn_args arg;
6534                 struct nfs4_layoutreturn_res res;
6535                 struct nfs4_xdr_opaque_data ld_private;
6536                 u32 roc_barrier;
6537                 bool roc;
6538         } lr;
6539         struct nfs_fattr fattr;
6540         int rpc_status;
6541         struct inode *inode;
6542 };
6543
6544 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6545 {
6546         struct nfs4_delegreturndata *data = calldata;
6547         struct nfs4_exception exception = {
6548                 .inode = data->inode,
6549                 .stateid = &data->stateid,
6550                 .task_is_privileged = data->args.seq_args.sa_privileged,
6551         };
6552
6553         if (!nfs4_sequence_done(task, &data->res.seq_res))
6554                 return;
6555
6556         trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6557
6558         /* Handle Layoutreturn errors */
6559         if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6560                           &data->res.lr_ret) == -EAGAIN)
6561                 goto out_restart;
6562
6563         switch (task->tk_status) {
6564         case 0:
6565                 renew_lease(data->res.server, data->timestamp);
6566                 break;
6567         case -NFS4ERR_ADMIN_REVOKED:
6568         case -NFS4ERR_DELEG_REVOKED:
6569         case -NFS4ERR_EXPIRED:
6570                 nfs4_free_revoked_stateid(data->res.server,
6571                                 data->args.stateid,
6572                                 task->tk_msg.rpc_cred);
6573                 fallthrough;
6574         case -NFS4ERR_BAD_STATEID:
6575         case -NFS4ERR_STALE_STATEID:
6576         case -ETIMEDOUT:
6577                 task->tk_status = 0;
6578                 break;
6579         case -NFS4ERR_OLD_STATEID:
6580                 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6581                         nfs4_stateid_seqid_inc(&data->stateid);
6582                 if (data->args.bitmask) {
6583                         data->args.bitmask = NULL;
6584                         data->res.fattr = NULL;
6585                 }
6586                 goto out_restart;
6587         case -NFS4ERR_ACCESS:
6588                 if (data->args.bitmask) {
6589                         data->args.bitmask = NULL;
6590                         data->res.fattr = NULL;
6591                         goto out_restart;
6592                 }
6593                 fallthrough;
6594         default:
6595                 task->tk_status = nfs4_async_handle_exception(task,
6596                                 data->res.server, task->tk_status,
6597                                 &exception);
6598                 if (exception.retry)
6599                         goto out_restart;
6600         }
6601         nfs_delegation_mark_returned(data->inode, data->args.stateid);
6602         data->rpc_status = task->tk_status;
6603         return;
6604 out_restart:
6605         task->tk_status = 0;
6606         rpc_restart_call_prepare(task);
6607 }
6608
6609 static void nfs4_delegreturn_release(void *calldata)
6610 {
6611         struct nfs4_delegreturndata *data = calldata;
6612         struct inode *inode = data->inode;
6613
6614         if (data->lr.roc)
6615                 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6616                                  data->res.lr_ret);
6617         if (inode) {
6618                 nfs4_fattr_set_prechange(&data->fattr,
6619                                          inode_peek_iversion_raw(inode));
6620                 nfs_refresh_inode(inode, &data->fattr);
6621                 nfs_iput_and_deactive(inode);
6622         }
6623         kfree(calldata);
6624 }
6625
6626 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6627 {
6628         struct nfs4_delegreturndata *d_data;
6629         struct pnfs_layout_hdr *lo;
6630
6631         d_data = data;
6632
6633         if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6634                 nfs4_sequence_done(task, &d_data->res.seq_res);
6635                 return;
6636         }
6637
6638         lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6639         if (lo && !pnfs_layout_is_valid(lo)) {
6640                 d_data->args.lr_args = NULL;
6641                 d_data->res.lr_res = NULL;
6642         }
6643
6644         nfs4_setup_sequence(d_data->res.server->nfs_client,
6645                         &d_data->args.seq_args,
6646                         &d_data->res.seq_res,
6647                         task);
6648 }
6649
6650 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6651         .rpc_call_prepare = nfs4_delegreturn_prepare,
6652         .rpc_call_done = nfs4_delegreturn_done,
6653         .rpc_release = nfs4_delegreturn_release,
6654 };
6655
6656 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6657 {
6658         struct nfs4_delegreturndata *data;
6659         struct nfs_server *server = NFS_SERVER(inode);
6660         struct rpc_task *task;
6661         struct rpc_message msg = {
6662                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6663                 .rpc_cred = cred,
6664         };
6665         struct rpc_task_setup task_setup_data = {
6666                 .rpc_client = server->client,
6667                 .rpc_message = &msg,
6668                 .callback_ops = &nfs4_delegreturn_ops,
6669                 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6670         };
6671         int status = 0;
6672
6673         if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6674                 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6675
6676         data = kzalloc(sizeof(*data), GFP_KERNEL);
6677         if (data == NULL)
6678                 return -ENOMEM;
6679
6680         nfs4_state_protect(server->nfs_client,
6681                         NFS_SP4_MACH_CRED_CLEANUP,
6682                         &task_setup_data.rpc_client, &msg);
6683
6684         data->args.fhandle = &data->fh;
6685         data->args.stateid = &data->stateid;
6686         nfs4_bitmask_set(data->args.bitmask_store,
6687                          server->cache_consistency_bitmask, inode, 0);
6688         data->args.bitmask = data->args.bitmask_store;
6689         nfs_copy_fh(&data->fh, NFS_FH(inode));
6690         nfs4_stateid_copy(&data->stateid, stateid);
6691         data->res.fattr = &data->fattr;
6692         data->res.server = server;
6693         data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6694         data->lr.arg.ld_private = &data->lr.ld_private;
6695         nfs_fattr_init(data->res.fattr);
6696         data->timestamp = jiffies;
6697         data->rpc_status = 0;
6698         data->inode = nfs_igrab_and_active(inode);
6699         if (data->inode || issync) {
6700                 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6701                                         cred);
6702                 if (data->lr.roc) {
6703                         data->args.lr_args = &data->lr.arg;
6704                         data->res.lr_res = &data->lr.res;
6705                 }
6706         }
6707
6708         if (!data->inode)
6709                 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6710                                    1);
6711         else
6712                 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6713                                    0);
6714         task_setup_data.callback_data = data;
6715         msg.rpc_argp = &data->args;
6716         msg.rpc_resp = &data->res;
6717         task = rpc_run_task(&task_setup_data);
6718         if (IS_ERR(task))
6719                 return PTR_ERR(task);
6720         if (!issync)
6721                 goto out;
6722         status = rpc_wait_for_completion_task(task);
6723         if (status != 0)
6724                 goto out;
6725         status = data->rpc_status;
6726 out:
6727         rpc_put_task(task);
6728         return status;
6729 }
6730
6731 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6732 {
6733         struct nfs_server *server = NFS_SERVER(inode);
6734         struct nfs4_exception exception = { };
6735         int err;
6736         do {
6737                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6738                 trace_nfs4_delegreturn(inode, stateid, err);
6739                 switch (err) {
6740                         case -NFS4ERR_STALE_STATEID:
6741                         case -NFS4ERR_EXPIRED:
6742                         case 0:
6743                                 return 0;
6744                 }
6745                 err = nfs4_handle_exception(server, err, &exception);
6746         } while (exception.retry);
6747         return err;
6748 }
6749
6750 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6751 {
6752         struct inode *inode = state->inode;
6753         struct nfs_server *server = NFS_SERVER(inode);
6754         struct nfs_client *clp = server->nfs_client;
6755         struct nfs_lockt_args arg = {
6756                 .fh = NFS_FH(inode),
6757                 .fl = request,
6758         };
6759         struct nfs_lockt_res res = {
6760                 .denied = request,
6761         };
6762         struct rpc_message msg = {
6763                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6764                 .rpc_argp       = &arg,
6765                 .rpc_resp       = &res,
6766                 .rpc_cred       = state->owner->so_cred,
6767         };
6768         struct nfs4_lock_state *lsp;
6769         int status;
6770
6771         arg.lock_owner.clientid = clp->cl_clientid;
6772         status = nfs4_set_lock_state(state, request);
6773         if (status != 0)
6774                 goto out;
6775         lsp = request->fl_u.nfs4_fl.owner;
6776         arg.lock_owner.id = lsp->ls_seqid.owner_id;
6777         arg.lock_owner.s_dev = server->s_dev;
6778         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6779         switch (status) {
6780                 case 0:
6781                         request->fl_type = F_UNLCK;
6782                         break;
6783                 case -NFS4ERR_DENIED:
6784                         status = 0;
6785         }
6786         request->fl_ops->fl_release_private(request);
6787         request->fl_ops = NULL;
6788 out:
6789         return status;
6790 }
6791
6792 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6793 {
6794         struct nfs4_exception exception = {
6795                 .interruptible = true,
6796         };
6797         int err;
6798
6799         do {
6800                 err = _nfs4_proc_getlk(state, cmd, request);
6801                 trace_nfs4_get_lock(request, state, cmd, err);
6802                 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6803                                 &exception);
6804         } while (exception.retry);
6805         return err;
6806 }
6807
6808 /*
6809  * Update the seqid of a lock stateid after receiving
6810  * NFS4ERR_OLD_STATEID
6811  */
6812 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6813                 struct nfs4_lock_state *lsp)
6814 {
6815         struct nfs4_state *state = lsp->ls_state;
6816         bool ret = false;
6817
6818         spin_lock(&state->state_lock);
6819         if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6820                 goto out;
6821         if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6822                 nfs4_stateid_seqid_inc(dst);
6823         else
6824                 dst->seqid = lsp->ls_stateid.seqid;
6825         ret = true;
6826 out:
6827         spin_unlock(&state->state_lock);
6828         return ret;
6829 }
6830
6831 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6832                 struct nfs4_lock_state *lsp)
6833 {
6834         struct nfs4_state *state = lsp->ls_state;
6835         bool ret;
6836
6837         spin_lock(&state->state_lock);
6838         ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6839         nfs4_stateid_copy(dst, &lsp->ls_stateid);
6840         spin_unlock(&state->state_lock);
6841         return ret;
6842 }
6843
6844 struct nfs4_unlockdata {
6845         struct nfs_locku_args arg;
6846         struct nfs_locku_res res;
6847         struct nfs4_lock_state *lsp;
6848         struct nfs_open_context *ctx;
6849         struct nfs_lock_context *l_ctx;
6850         struct file_lock fl;
6851         struct nfs_server *server;
6852         unsigned long timestamp;
6853 };
6854
6855 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6856                 struct nfs_open_context *ctx,
6857                 struct nfs4_lock_state *lsp,
6858                 struct nfs_seqid *seqid)
6859 {
6860         struct nfs4_unlockdata *p;
6861         struct nfs4_state *state = lsp->ls_state;
6862         struct inode *inode = state->inode;
6863
6864         p = kzalloc(sizeof(*p), GFP_KERNEL);
6865         if (p == NULL)
6866                 return NULL;
6867         p->arg.fh = NFS_FH(inode);
6868         p->arg.fl = &p->fl;
6869         p->arg.seqid = seqid;
6870         p->res.seqid = seqid;
6871         p->lsp = lsp;
6872         /* Ensure we don't close file until we're done freeing locks! */
6873         p->ctx = get_nfs_open_context(ctx);
6874         p->l_ctx = nfs_get_lock_context(ctx);
6875         locks_init_lock(&p->fl);
6876         locks_copy_lock(&p->fl, fl);
6877         p->server = NFS_SERVER(inode);
6878         spin_lock(&state->state_lock);
6879         nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6880         spin_unlock(&state->state_lock);
6881         return p;
6882 }
6883
6884 static void nfs4_locku_release_calldata(void *data)
6885 {
6886         struct nfs4_unlockdata *calldata = data;
6887         nfs_free_seqid(calldata->arg.seqid);
6888         nfs4_put_lock_state(calldata->lsp);
6889         nfs_put_lock_context(calldata->l_ctx);
6890         put_nfs_open_context(calldata->ctx);
6891         kfree(calldata);
6892 }
6893
6894 static void nfs4_locku_done(struct rpc_task *task, void *data)
6895 {
6896         struct nfs4_unlockdata *calldata = data;
6897         struct nfs4_exception exception = {
6898                 .inode = calldata->lsp->ls_state->inode,
6899                 .stateid = &calldata->arg.stateid,
6900         };
6901
6902         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6903                 return;
6904         switch (task->tk_status) {
6905                 case 0:
6906                         renew_lease(calldata->server, calldata->timestamp);
6907                         locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6908                         if (nfs4_update_lock_stateid(calldata->lsp,
6909                                         &calldata->res.stateid))
6910                                 break;
6911                         fallthrough;
6912                 case -NFS4ERR_ADMIN_REVOKED:
6913                 case -NFS4ERR_EXPIRED:
6914                         nfs4_free_revoked_stateid(calldata->server,
6915                                         &calldata->arg.stateid,
6916                                         task->tk_msg.rpc_cred);
6917                         fallthrough;
6918                 case -NFS4ERR_BAD_STATEID:
6919                 case -NFS4ERR_STALE_STATEID:
6920                         if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6921                                                 calldata->lsp))
6922                                 rpc_restart_call_prepare(task);
6923                         break;
6924                 case -NFS4ERR_OLD_STATEID:
6925                         if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6926                                                 calldata->lsp))
6927                                 rpc_restart_call_prepare(task);
6928                         break;
6929                 default:
6930                         task->tk_status = nfs4_async_handle_exception(task,
6931                                         calldata->server, task->tk_status,
6932                                         &exception);
6933                         if (exception.retry)
6934                                 rpc_restart_call_prepare(task);
6935         }
6936         nfs_release_seqid(calldata->arg.seqid);
6937 }
6938
6939 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6940 {
6941         struct nfs4_unlockdata *calldata = data;
6942
6943         if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6944                 nfs_async_iocounter_wait(task, calldata->l_ctx))
6945                 return;
6946
6947         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6948                 goto out_wait;
6949         if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6950                 /* Note: exit _without_ running nfs4_locku_done */
6951                 goto out_no_action;
6952         }
6953         calldata->timestamp = jiffies;
6954         if (nfs4_setup_sequence(calldata->server->nfs_client,
6955                                 &calldata->arg.seq_args,
6956                                 &calldata->res.seq_res,
6957                                 task) != 0)
6958                 nfs_release_seqid(calldata->arg.seqid);
6959         return;
6960 out_no_action:
6961         task->tk_action = NULL;
6962 out_wait:
6963         nfs4_sequence_done(task, &calldata->res.seq_res);
6964 }
6965
6966 static const struct rpc_call_ops nfs4_locku_ops = {
6967         .rpc_call_prepare = nfs4_locku_prepare,
6968         .rpc_call_done = nfs4_locku_done,
6969         .rpc_release = nfs4_locku_release_calldata,
6970 };
6971
6972 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6973                 struct nfs_open_context *ctx,
6974                 struct nfs4_lock_state *lsp,
6975                 struct nfs_seqid *seqid)
6976 {
6977         struct nfs4_unlockdata *data;
6978         struct rpc_message msg = {
6979                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6980                 .rpc_cred = ctx->cred,
6981         };
6982         struct rpc_task_setup task_setup_data = {
6983                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6984                 .rpc_message = &msg,
6985                 .callback_ops = &nfs4_locku_ops,
6986                 .workqueue = nfsiod_workqueue,
6987                 .flags = RPC_TASK_ASYNC,
6988         };
6989
6990         if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
6991                 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6992
6993         nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6994                 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6995
6996         /* Ensure this is an unlock - when canceling a lock, the
6997          * canceled lock is passed in, and it won't be an unlock.
6998          */
6999         fl->fl_type = F_UNLCK;
7000         if (fl->fl_flags & FL_CLOSE)
7001                 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
7002
7003         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
7004         if (data == NULL) {
7005                 nfs_free_seqid(seqid);
7006                 return ERR_PTR(-ENOMEM);
7007         }
7008
7009         nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
7010         msg.rpc_argp = &data->arg;
7011         msg.rpc_resp = &data->res;
7012         task_setup_data.callback_data = data;
7013         return rpc_run_task(&task_setup_data);
7014 }
7015
7016 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7017 {
7018         struct inode *inode = state->inode;
7019         struct nfs4_state_owner *sp = state->owner;
7020         struct nfs_inode *nfsi = NFS_I(inode);
7021         struct nfs_seqid *seqid;
7022         struct nfs4_lock_state *lsp;
7023         struct rpc_task *task;
7024         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7025         int status = 0;
7026         unsigned char fl_flags = request->fl_flags;
7027
7028         status = nfs4_set_lock_state(state, request);
7029         /* Unlock _before_ we do the RPC call */
7030         request->fl_flags |= FL_EXISTS;
7031         /* Exclude nfs_delegation_claim_locks() */
7032         mutex_lock(&sp->so_delegreturn_mutex);
7033         /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7034         down_read(&nfsi->rwsem);
7035         if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7036                 up_read(&nfsi->rwsem);
7037                 mutex_unlock(&sp->so_delegreturn_mutex);
7038                 goto out;
7039         }
7040         lsp = request->fl_u.nfs4_fl.owner;
7041         set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7042         up_read(&nfsi->rwsem);
7043         mutex_unlock(&sp->so_delegreturn_mutex);
7044         if (status != 0)
7045                 goto out;
7046         /* Is this a delegated lock? */
7047         if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7048                 goto out;
7049         alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7050         seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7051         status = -ENOMEM;
7052         if (IS_ERR(seqid))
7053                 goto out;
7054         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7055         status = PTR_ERR(task);
7056         if (IS_ERR(task))
7057                 goto out;
7058         status = rpc_wait_for_completion_task(task);
7059         rpc_put_task(task);
7060 out:
7061         request->fl_flags = fl_flags;
7062         trace_nfs4_unlock(request, state, F_SETLK, status);
7063         return status;
7064 }
7065
7066 struct nfs4_lockdata {
7067         struct nfs_lock_args arg;
7068         struct nfs_lock_res res;
7069         struct nfs4_lock_state *lsp;
7070         struct nfs_open_context *ctx;
7071         struct file_lock fl;
7072         unsigned long timestamp;
7073         int rpc_status;
7074         int cancelled;
7075         struct nfs_server *server;
7076 };
7077
7078 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7079                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7080                 gfp_t gfp_mask)
7081 {
7082         struct nfs4_lockdata *p;
7083         struct inode *inode = lsp->ls_state->inode;
7084         struct nfs_server *server = NFS_SERVER(inode);
7085         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7086
7087         p = kzalloc(sizeof(*p), gfp_mask);
7088         if (p == NULL)
7089                 return NULL;
7090
7091         p->arg.fh = NFS_FH(inode);
7092         p->arg.fl = &p->fl;
7093         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7094         if (IS_ERR(p->arg.open_seqid))
7095                 goto out_free;
7096         alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7097         p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7098         if (IS_ERR(p->arg.lock_seqid))
7099                 goto out_free_seqid;
7100         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7101         p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7102         p->arg.lock_owner.s_dev = server->s_dev;
7103         p->res.lock_seqid = p->arg.lock_seqid;
7104         p->lsp = lsp;
7105         p->server = server;
7106         p->ctx = get_nfs_open_context(ctx);
7107         locks_init_lock(&p->fl);
7108         locks_copy_lock(&p->fl, fl);
7109         return p;
7110 out_free_seqid:
7111         nfs_free_seqid(p->arg.open_seqid);
7112 out_free:
7113         kfree(p);
7114         return NULL;
7115 }
7116
7117 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7118 {
7119         struct nfs4_lockdata *data = calldata;
7120         struct nfs4_state *state = data->lsp->ls_state;
7121
7122         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7123                 goto out_wait;
7124         /* Do we need to do an open_to_lock_owner? */
7125         if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7126                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7127                         goto out_release_lock_seqid;
7128                 }
7129                 nfs4_stateid_copy(&data->arg.open_stateid,
7130                                 &state->open_stateid);
7131                 data->arg.new_lock_owner = 1;
7132                 data->res.open_seqid = data->arg.open_seqid;
7133         } else {
7134                 data->arg.new_lock_owner = 0;
7135                 nfs4_stateid_copy(&data->arg.lock_stateid,
7136                                 &data->lsp->ls_stateid);
7137         }
7138         if (!nfs4_valid_open_stateid(state)) {
7139                 data->rpc_status = -EBADF;
7140                 task->tk_action = NULL;
7141                 goto out_release_open_seqid;
7142         }
7143         data->timestamp = jiffies;
7144         if (nfs4_setup_sequence(data->server->nfs_client,
7145                                 &data->arg.seq_args,
7146                                 &data->res.seq_res,
7147                                 task) == 0)
7148                 return;
7149 out_release_open_seqid:
7150         nfs_release_seqid(data->arg.open_seqid);
7151 out_release_lock_seqid:
7152         nfs_release_seqid(data->arg.lock_seqid);
7153 out_wait:
7154         nfs4_sequence_done(task, &data->res.seq_res);
7155         dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7156 }
7157
7158 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7159 {
7160         struct nfs4_lockdata *data = calldata;
7161         struct nfs4_lock_state *lsp = data->lsp;
7162
7163         if (!nfs4_sequence_done(task, &data->res.seq_res))
7164                 return;
7165
7166         data->rpc_status = task->tk_status;
7167         switch (task->tk_status) {
7168         case 0:
7169                 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7170                                 data->timestamp);
7171                 if (data->arg.new_lock && !data->cancelled) {
7172                         data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7173                         if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7174                                 goto out_restart;
7175                 }
7176                 if (data->arg.new_lock_owner != 0) {
7177                         nfs_confirm_seqid(&lsp->ls_seqid, 0);
7178                         nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7179                         set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7180                 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7181                         goto out_restart;
7182                 break;
7183         case -NFS4ERR_OLD_STATEID:
7184                 if (data->arg.new_lock_owner != 0 &&
7185                         nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7186                                         lsp->ls_state))
7187                         goto out_restart;
7188                 if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7189                         goto out_restart;
7190                 fallthrough;
7191         case -NFS4ERR_BAD_STATEID:
7192         case -NFS4ERR_STALE_STATEID:
7193         case -NFS4ERR_EXPIRED:
7194                 if (data->arg.new_lock_owner != 0) {
7195                         if (!nfs4_stateid_match(&data->arg.open_stateid,
7196                                                 &lsp->ls_state->open_stateid))
7197                                 goto out_restart;
7198                 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7199                                                 &lsp->ls_stateid))
7200                                 goto out_restart;
7201         }
7202 out_done:
7203         dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7204         return;
7205 out_restart:
7206         if (!data->cancelled)
7207                 rpc_restart_call_prepare(task);
7208         goto out_done;
7209 }
7210
7211 static void nfs4_lock_release(void *calldata)
7212 {
7213         struct nfs4_lockdata *data = calldata;
7214
7215         nfs_free_seqid(data->arg.open_seqid);
7216         if (data->cancelled && data->rpc_status == 0) {
7217                 struct rpc_task *task;
7218                 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7219                                 data->arg.lock_seqid);
7220                 if (!IS_ERR(task))
7221                         rpc_put_task_async(task);
7222                 dprintk("%s: cancelling lock!\n", __func__);
7223         } else
7224                 nfs_free_seqid(data->arg.lock_seqid);
7225         nfs4_put_lock_state(data->lsp);
7226         put_nfs_open_context(data->ctx);
7227         kfree(data);
7228 }
7229
7230 static const struct rpc_call_ops nfs4_lock_ops = {
7231         .rpc_call_prepare = nfs4_lock_prepare,
7232         .rpc_call_done = nfs4_lock_done,
7233         .rpc_release = nfs4_lock_release,
7234 };
7235
7236 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7237 {
7238         switch (error) {
7239         case -NFS4ERR_ADMIN_REVOKED:
7240         case -NFS4ERR_EXPIRED:
7241         case -NFS4ERR_BAD_STATEID:
7242                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7243                 if (new_lock_owner != 0 ||
7244                    test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7245                         nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7246                 break;
7247         case -NFS4ERR_STALE_STATEID:
7248                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7249                 nfs4_schedule_lease_recovery(server->nfs_client);
7250         }
7251 }
7252
7253 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7254 {
7255         struct nfs4_lockdata *data;
7256         struct rpc_task *task;
7257         struct rpc_message msg = {
7258                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7259                 .rpc_cred = state->owner->so_cred,
7260         };
7261         struct rpc_task_setup task_setup_data = {
7262                 .rpc_client = NFS_CLIENT(state->inode),
7263                 .rpc_message = &msg,
7264                 .callback_ops = &nfs4_lock_ops,
7265                 .workqueue = nfsiod_workqueue,
7266                 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7267         };
7268         int ret;
7269
7270         if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7271                 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7272
7273         data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7274                                    fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7275         if (data == NULL)
7276                 return -ENOMEM;
7277         if (IS_SETLKW(cmd))
7278                 data->arg.block = 1;
7279         nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7280                                 recovery_type > NFS_LOCK_NEW);
7281         msg.rpc_argp = &data->arg;
7282         msg.rpc_resp = &data->res;
7283         task_setup_data.callback_data = data;
7284         if (recovery_type > NFS_LOCK_NEW) {
7285                 if (recovery_type == NFS_LOCK_RECLAIM)
7286                         data->arg.reclaim = NFS_LOCK_RECLAIM;
7287         } else
7288                 data->arg.new_lock = 1;
7289         task = rpc_run_task(&task_setup_data);
7290         if (IS_ERR(task))
7291                 return PTR_ERR(task);
7292         ret = rpc_wait_for_completion_task(task);
7293         if (ret == 0) {
7294                 ret = data->rpc_status;
7295                 if (ret)
7296                         nfs4_handle_setlk_error(data->server, data->lsp,
7297                                         data->arg.new_lock_owner, ret);
7298         } else
7299                 data->cancelled = true;
7300         trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7301         rpc_put_task(task);
7302         dprintk("%s: ret = %d\n", __func__, ret);
7303         return ret;
7304 }
7305
7306 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7307 {
7308         struct nfs_server *server = NFS_SERVER(state->inode);
7309         struct nfs4_exception exception = {
7310                 .inode = state->inode,
7311         };
7312         int err;
7313
7314         do {
7315                 /* Cache the lock if possible... */
7316                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7317                         return 0;
7318                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7319                 if (err != -NFS4ERR_DELAY)
7320                         break;
7321                 nfs4_handle_exception(server, err, &exception);
7322         } while (exception.retry);
7323         return err;
7324 }
7325
7326 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7327 {
7328         struct nfs_server *server = NFS_SERVER(state->inode);
7329         struct nfs4_exception exception = {
7330                 .inode = state->inode,
7331         };
7332         int err;
7333
7334         err = nfs4_set_lock_state(state, request);
7335         if (err != 0)
7336                 return err;
7337         if (!recover_lost_locks) {
7338                 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7339                 return 0;
7340         }
7341         do {
7342                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7343                         return 0;
7344                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7345                 switch (err) {
7346                 default:
7347                         goto out;
7348                 case -NFS4ERR_GRACE:
7349                 case -NFS4ERR_DELAY:
7350                         nfs4_handle_exception(server, err, &exception);
7351                         err = 0;
7352                 }
7353         } while (exception.retry);
7354 out:
7355         return err;
7356 }
7357
7358 #if defined(CONFIG_NFS_V4_1)
7359 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7360 {
7361         struct nfs4_lock_state *lsp;
7362         int status;
7363
7364         status = nfs4_set_lock_state(state, request);
7365         if (status != 0)
7366                 return status;
7367         lsp = request->fl_u.nfs4_fl.owner;
7368         if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7369             test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7370                 return 0;
7371         return nfs4_lock_expired(state, request);
7372 }
7373 #endif
7374
7375 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7376 {
7377         struct nfs_inode *nfsi = NFS_I(state->inode);
7378         struct nfs4_state_owner *sp = state->owner;
7379         unsigned char fl_flags = request->fl_flags;
7380         int status;
7381
7382         request->fl_flags |= FL_ACCESS;
7383         status = locks_lock_inode_wait(state->inode, request);
7384         if (status < 0)
7385                 goto out;
7386         mutex_lock(&sp->so_delegreturn_mutex);
7387         down_read(&nfsi->rwsem);
7388         if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7389                 /* Yes: cache locks! */
7390                 /* ...but avoid races with delegation recall... */
7391                 request->fl_flags = fl_flags & ~FL_SLEEP;
7392                 status = locks_lock_inode_wait(state->inode, request);
7393                 up_read(&nfsi->rwsem);
7394                 mutex_unlock(&sp->so_delegreturn_mutex);
7395                 goto out;
7396         }
7397         up_read(&nfsi->rwsem);
7398         mutex_unlock(&sp->so_delegreturn_mutex);
7399         status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7400 out:
7401         request->fl_flags = fl_flags;
7402         return status;
7403 }
7404
7405 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7406 {
7407         struct nfs4_exception exception = {
7408                 .state = state,
7409                 .inode = state->inode,
7410                 .interruptible = true,
7411         };
7412         int err;
7413
7414         do {
7415                 err = _nfs4_proc_setlk(state, cmd, request);
7416                 if (err == -NFS4ERR_DENIED)
7417                         err = -EAGAIN;
7418                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7419                                 err, &exception);
7420         } while (exception.retry);
7421         return err;
7422 }
7423
7424 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7425 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7426
7427 static int
7428 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7429                         struct file_lock *request)
7430 {
7431         int             status = -ERESTARTSYS;
7432         unsigned long   timeout = NFS4_LOCK_MINTIMEOUT;
7433
7434         while(!signalled()) {
7435                 status = nfs4_proc_setlk(state, cmd, request);
7436                 if ((status != -EAGAIN) || IS_SETLK(cmd))
7437                         break;
7438                 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7439                 schedule_timeout(timeout);
7440                 timeout *= 2;
7441                 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7442                 status = -ERESTARTSYS;
7443         }
7444         return status;
7445 }
7446
7447 #ifdef CONFIG_NFS_V4_1
7448 struct nfs4_lock_waiter {
7449         struct inode            *inode;
7450         struct nfs_lowner       owner;
7451         wait_queue_entry_t      wait;
7452 };
7453
7454 static int
7455 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7456 {
7457         struct nfs4_lock_waiter *waiter =
7458                 container_of(wait, struct nfs4_lock_waiter, wait);
7459
7460         /* NULL key means to wake up everyone */
7461         if (key) {
7462                 struct cb_notify_lock_args      *cbnl = key;
7463                 struct nfs_lowner               *lowner = &cbnl->cbnl_owner,
7464                                                 *wowner = &waiter->owner;
7465
7466                 /* Only wake if the callback was for the same owner. */
7467                 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7468                         return 0;
7469
7470                 /* Make sure it's for the right inode */
7471                 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7472                         return 0;
7473         }
7474
7475         return woken_wake_function(wait, mode, flags, key);
7476 }
7477
7478 static int
7479 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7480 {
7481         struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7482         struct nfs_server *server = NFS_SERVER(state->inode);
7483         struct nfs_client *clp = server->nfs_client;
7484         wait_queue_head_t *q = &clp->cl_lock_waitq;
7485         struct nfs4_lock_waiter waiter = {
7486                 .inode = state->inode,
7487                 .owner = { .clientid = clp->cl_clientid,
7488                            .id = lsp->ls_seqid.owner_id,
7489                            .s_dev = server->s_dev },
7490         };
7491         int status;
7492
7493         /* Don't bother with waitqueue if we don't expect a callback */
7494         if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7495                 return nfs4_retry_setlk_simple(state, cmd, request);
7496
7497         init_wait(&waiter.wait);
7498         waiter.wait.func = nfs4_wake_lock_waiter;
7499         add_wait_queue(q, &waiter.wait);
7500
7501         do {
7502                 status = nfs4_proc_setlk(state, cmd, request);
7503                 if (status != -EAGAIN || IS_SETLK(cmd))
7504                         break;
7505
7506                 status = -ERESTARTSYS;
7507                 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7508                            NFS4_LOCK_MAXTIMEOUT);
7509         } while (!signalled());
7510
7511         remove_wait_queue(q, &waiter.wait);
7512
7513         return status;
7514 }
7515 #else /* !CONFIG_NFS_V4_1 */
7516 static inline int
7517 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7518 {
7519         return nfs4_retry_setlk_simple(state, cmd, request);
7520 }
7521 #endif
7522
7523 static int
7524 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7525 {
7526         struct nfs_open_context *ctx;
7527         struct nfs4_state *state;
7528         int status;
7529
7530         /* verify open state */
7531         ctx = nfs_file_open_context(filp);
7532         state = ctx->state;
7533
7534         if (IS_GETLK(cmd)) {
7535                 if (state != NULL)
7536                         return nfs4_proc_getlk(state, F_GETLK, request);
7537                 return 0;
7538         }
7539
7540         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7541                 return -EINVAL;
7542
7543         if (request->fl_type == F_UNLCK) {
7544                 if (state != NULL)
7545                         return nfs4_proc_unlck(state, cmd, request);
7546                 return 0;
7547         }
7548
7549         if (state == NULL)
7550                 return -ENOLCK;
7551
7552         if ((request->fl_flags & FL_POSIX) &&
7553             !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7554                 return -ENOLCK;
7555
7556         /*
7557          * Don't rely on the VFS having checked the file open mode,
7558          * since it won't do this for flock() locks.
7559          */
7560         switch (request->fl_type) {
7561         case F_RDLCK:
7562                 if (!(filp->f_mode & FMODE_READ))
7563                         return -EBADF;
7564                 break;
7565         case F_WRLCK:
7566                 if (!(filp->f_mode & FMODE_WRITE))
7567                         return -EBADF;
7568         }
7569
7570         status = nfs4_set_lock_state(state, request);
7571         if (status != 0)
7572                 return status;
7573
7574         return nfs4_retry_setlk(state, cmd, request);
7575 }
7576
7577 static int nfs4_delete_lease(struct file *file, void **priv)
7578 {
7579         return generic_setlease(file, F_UNLCK, NULL, priv);
7580 }
7581
7582 static int nfs4_add_lease(struct file *file, int arg, struct file_lock **lease,
7583                           void **priv)
7584 {
7585         struct inode *inode = file_inode(file);
7586         fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7587         int ret;
7588
7589         /* No delegation, no lease */
7590         if (!nfs4_have_delegation(inode, type))
7591                 return -EAGAIN;
7592         ret = generic_setlease(file, arg, lease, priv);
7593         if (ret || nfs4_have_delegation(inode, type))
7594                 return ret;
7595         /* We raced with a delegation return */
7596         nfs4_delete_lease(file, priv);
7597         return -EAGAIN;
7598 }
7599
7600 int nfs4_proc_setlease(struct file *file, int arg, struct file_lock **lease,
7601                        void **priv)
7602 {
7603         switch (arg) {
7604         case F_RDLCK:
7605         case F_WRLCK:
7606                 return nfs4_add_lease(file, arg, lease, priv);
7607         case F_UNLCK:
7608                 return nfs4_delete_lease(file, priv);
7609         default:
7610                 return -EINVAL;
7611         }
7612 }
7613
7614 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7615 {
7616         struct nfs_server *server = NFS_SERVER(state->inode);
7617         int err;
7618
7619         err = nfs4_set_lock_state(state, fl);
7620         if (err != 0)
7621                 return err;
7622         do {
7623                 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7624                 if (err != -NFS4ERR_DELAY)
7625                         break;
7626                 ssleep(1);
7627         } while (err == -NFS4ERR_DELAY);
7628         return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7629 }
7630
7631 struct nfs_release_lockowner_data {
7632         struct nfs4_lock_state *lsp;
7633         struct nfs_server *server;
7634         struct nfs_release_lockowner_args args;
7635         struct nfs_release_lockowner_res res;
7636         unsigned long timestamp;
7637 };
7638
7639 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7640 {
7641         struct nfs_release_lockowner_data *data = calldata;
7642         struct nfs_server *server = data->server;
7643         nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7644                            &data->res.seq_res, task);
7645         data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7646         data->timestamp = jiffies;
7647 }
7648
7649 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7650 {
7651         struct nfs_release_lockowner_data *data = calldata;
7652         struct nfs_server *server = data->server;
7653
7654         nfs40_sequence_done(task, &data->res.seq_res);
7655
7656         switch (task->tk_status) {
7657         case 0:
7658                 renew_lease(server, data->timestamp);
7659                 break;
7660         case -NFS4ERR_STALE_CLIENTID:
7661         case -NFS4ERR_EXPIRED:
7662                 nfs4_schedule_lease_recovery(server->nfs_client);
7663                 break;
7664         case -NFS4ERR_LEASE_MOVED:
7665         case -NFS4ERR_DELAY:
7666                 if (nfs4_async_handle_error(task, server,
7667                                             NULL, NULL) == -EAGAIN)
7668                         rpc_restart_call_prepare(task);
7669         }
7670 }
7671
7672 static void nfs4_release_lockowner_release(void *calldata)
7673 {
7674         struct nfs_release_lockowner_data *data = calldata;
7675         nfs4_free_lock_state(data->server, data->lsp);
7676         kfree(calldata);
7677 }
7678
7679 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7680         .rpc_call_prepare = nfs4_release_lockowner_prepare,
7681         .rpc_call_done = nfs4_release_lockowner_done,
7682         .rpc_release = nfs4_release_lockowner_release,
7683 };
7684
7685 static void
7686 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7687 {
7688         struct nfs_release_lockowner_data *data;
7689         struct rpc_message msg = {
7690                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7691         };
7692
7693         if (server->nfs_client->cl_mvops->minor_version != 0)
7694                 return;
7695
7696         data = kmalloc(sizeof(*data), GFP_KERNEL);
7697         if (!data)
7698                 return;
7699         data->lsp = lsp;
7700         data->server = server;
7701         data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7702         data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7703         data->args.lock_owner.s_dev = server->s_dev;
7704
7705         msg.rpc_argp = &data->args;
7706         msg.rpc_resp = &data->res;
7707         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7708         rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7709 }
7710
7711 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7712
7713 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7714                                    struct mnt_idmap *idmap,
7715                                    struct dentry *unused, struct inode *inode,
7716                                    const char *key, const void *buf,
7717                                    size_t buflen, int flags)
7718 {
7719         return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7720 }
7721
7722 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7723                                    struct dentry *unused, struct inode *inode,
7724                                    const char *key, void *buf, size_t buflen)
7725 {
7726         return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7727 }
7728
7729 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7730 {
7731         return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7732 }
7733
7734 #if defined(CONFIG_NFS_V4_1)
7735 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7736
7737 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7738                                     struct mnt_idmap *idmap,
7739                                     struct dentry *unused, struct inode *inode,
7740                                     const char *key, const void *buf,
7741                                     size_t buflen, int flags)
7742 {
7743         return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7744 }
7745
7746 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7747                                     struct dentry *unused, struct inode *inode,
7748                                     const char *key, void *buf, size_t buflen)
7749 {
7750         return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7751 }
7752
7753 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7754 {
7755         return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7756 }
7757
7758 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7759
7760 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7761                                     struct mnt_idmap *idmap,
7762                                     struct dentry *unused, struct inode *inode,
7763                                     const char *key, const void *buf,
7764                                     size_t buflen, int flags)
7765 {
7766         return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7767 }
7768
7769 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7770                                     struct dentry *unused, struct inode *inode,
7771                                     const char *key, void *buf, size_t buflen)
7772 {
7773         return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7774 }
7775
7776 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7777 {
7778         return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7779 }
7780
7781 #endif
7782
7783 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7784
7785 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7786                                      struct mnt_idmap *idmap,
7787                                      struct dentry *unused, struct inode *inode,
7788                                      const char *key, const void *buf,
7789                                      size_t buflen, int flags)
7790 {
7791         if (security_ismaclabel(key))
7792                 return nfs4_set_security_label(inode, buf, buflen);
7793
7794         return -EOPNOTSUPP;
7795 }
7796
7797 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7798                                      struct dentry *unused, struct inode *inode,
7799                                      const char *key, void *buf, size_t buflen)
7800 {
7801         if (security_ismaclabel(key))
7802                 return nfs4_get_security_label(inode, buf, buflen);
7803         return -EOPNOTSUPP;
7804 }
7805
7806 static ssize_t
7807 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7808 {
7809         int len = 0;
7810
7811         if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7812                 len = security_inode_listsecurity(inode, list, list_len);
7813                 if (len >= 0 && list_len && len > list_len)
7814                         return -ERANGE;
7815         }
7816         return len;
7817 }
7818
7819 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7820         .prefix = XATTR_SECURITY_PREFIX,
7821         .get    = nfs4_xattr_get_nfs4_label,
7822         .set    = nfs4_xattr_set_nfs4_label,
7823 };
7824
7825 #else
7826
7827 static ssize_t
7828 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7829 {
7830         return 0;
7831 }
7832
7833 #endif
7834
7835 #ifdef CONFIG_NFS_V4_2
7836 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7837                                     struct mnt_idmap *idmap,
7838                                     struct dentry *unused, struct inode *inode,
7839                                     const char *key, const void *buf,
7840                                     size_t buflen, int flags)
7841 {
7842         u32 mask;
7843         int ret;
7844
7845         if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7846                 return -EOPNOTSUPP;
7847
7848         /*
7849          * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7850          * flags right now. Handling of xattr operations use the normal
7851          * file read/write permissions.
7852          *
7853          * Just in case the server has other ideas (which RFC 8276 allows),
7854          * do a cached access check for the XA* flags to possibly avoid
7855          * doing an RPC and getting EACCES back.
7856          */
7857         if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7858                 if (!(mask & NFS_ACCESS_XAWRITE))
7859                         return -EACCES;
7860         }
7861
7862         if (buf == NULL) {
7863                 ret = nfs42_proc_removexattr(inode, key);
7864                 if (!ret)
7865                         nfs4_xattr_cache_remove(inode, key);
7866         } else {
7867                 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7868                 if (!ret)
7869                         nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7870         }
7871
7872         return ret;
7873 }
7874
7875 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7876                                     struct dentry *unused, struct inode *inode,
7877                                     const char *key, void *buf, size_t buflen)
7878 {
7879         u32 mask;
7880         ssize_t ret;
7881
7882         if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7883                 return -EOPNOTSUPP;
7884
7885         if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7886                 if (!(mask & NFS_ACCESS_XAREAD))
7887                         return -EACCES;
7888         }
7889
7890         ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7891         if (ret)
7892                 return ret;
7893
7894         ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7895         if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7896                 return ret;
7897
7898         ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7899
7900         return ret;
7901 }
7902
7903 static ssize_t
7904 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7905 {
7906         u64 cookie;
7907         bool eof;
7908         ssize_t ret, size;
7909         char *buf;
7910         size_t buflen;
7911         u32 mask;
7912
7913         if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7914                 return 0;
7915
7916         if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7917                 if (!(mask & NFS_ACCESS_XALIST))
7918                         return 0;
7919         }
7920
7921         ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7922         if (ret)
7923                 return ret;
7924
7925         ret = nfs4_xattr_cache_list(inode, list, list_len);
7926         if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7927                 return ret;
7928
7929         cookie = 0;
7930         eof = false;
7931         buflen = list_len ? list_len : XATTR_LIST_MAX;
7932         buf = list_len ? list : NULL;
7933         size = 0;
7934
7935         while (!eof) {
7936                 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7937                     &cookie, &eof);
7938                 if (ret < 0)
7939                         return ret;
7940
7941                 if (list_len) {
7942                         buf += ret;
7943                         buflen -= ret;
7944                 }
7945                 size += ret;
7946         }
7947
7948         if (list_len)
7949                 nfs4_xattr_cache_set_list(inode, list, size);
7950
7951         return size;
7952 }
7953
7954 #else
7955
7956 static ssize_t
7957 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7958 {
7959         return 0;
7960 }
7961 #endif /* CONFIG_NFS_V4_2 */
7962
7963 /*
7964  * nfs_fhget will use either the mounted_on_fileid or the fileid
7965  */
7966 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7967 {
7968         if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7969                (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7970               (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7971               (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7972                 return;
7973
7974         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7975                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7976         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7977         fattr->nlink = 2;
7978 }
7979
7980 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7981                                    const struct qstr *name,
7982                                    struct nfs4_fs_locations *fs_locations,
7983                                    struct page *page)
7984 {
7985         struct nfs_server *server = NFS_SERVER(dir);
7986         u32 bitmask[3];
7987         struct nfs4_fs_locations_arg args = {
7988                 .dir_fh = NFS_FH(dir),
7989                 .name = name,
7990                 .page = page,
7991                 .bitmask = bitmask,
7992         };
7993         struct nfs4_fs_locations_res res = {
7994                 .fs_locations = fs_locations,
7995         };
7996         struct rpc_message msg = {
7997                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7998                 .rpc_argp = &args,
7999                 .rpc_resp = &res,
8000         };
8001         int status;
8002
8003         dprintk("%s: start\n", __func__);
8004
8005         bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
8006         bitmask[1] = nfs4_fattr_bitmap[1];
8007
8008         /* Ask for the fileid of the absent filesystem if mounted_on_fileid
8009          * is not supported */
8010         if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
8011                 bitmask[0] &= ~FATTR4_WORD0_FILEID;
8012         else
8013                 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
8014
8015         nfs_fattr_init(fs_locations->fattr);
8016         fs_locations->server = server;
8017         fs_locations->nlocations = 0;
8018         status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
8019         dprintk("%s: returned status = %d\n", __func__, status);
8020         return status;
8021 }
8022
8023 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8024                            const struct qstr *name,
8025                            struct nfs4_fs_locations *fs_locations,
8026                            struct page *page)
8027 {
8028         struct nfs4_exception exception = {
8029                 .interruptible = true,
8030         };
8031         int err;
8032         do {
8033                 err = _nfs4_proc_fs_locations(client, dir, name,
8034                                 fs_locations, page);
8035                 trace_nfs4_get_fs_locations(dir, name, err);
8036                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8037                                 &exception);
8038         } while (exception.retry);
8039         return err;
8040 }
8041
8042 /*
8043  * This operation also signals the server that this client is
8044  * performing migration recovery.  The server can stop returning
8045  * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
8046  * appended to this compound to identify the client ID which is
8047  * performing recovery.
8048  */
8049 static int _nfs40_proc_get_locations(struct nfs_server *server,
8050                                      struct nfs_fh *fhandle,
8051                                      struct nfs4_fs_locations *locations,
8052                                      struct page *page, const struct cred *cred)
8053 {
8054         struct rpc_clnt *clnt = server->client;
8055         u32 bitmask[2] = {
8056                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8057         };
8058         struct nfs4_fs_locations_arg args = {
8059                 .clientid       = server->nfs_client->cl_clientid,
8060                 .fh             = fhandle,
8061                 .page           = page,
8062                 .bitmask        = bitmask,
8063                 .migration      = 1,            /* skip LOOKUP */
8064                 .renew          = 1,            /* append RENEW */
8065         };
8066         struct nfs4_fs_locations_res res = {
8067                 .fs_locations   = locations,
8068                 .migration      = 1,
8069                 .renew          = 1,
8070         };
8071         struct rpc_message msg = {
8072                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8073                 .rpc_argp       = &args,
8074                 .rpc_resp       = &res,
8075                 .rpc_cred       = cred,
8076         };
8077         unsigned long now = jiffies;
8078         int status;
8079
8080         nfs_fattr_init(locations->fattr);
8081         locations->server = server;
8082         locations->nlocations = 0;
8083
8084         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8085         status = nfs4_call_sync_sequence(clnt, server, &msg,
8086                                         &args.seq_args, &res.seq_res);
8087         if (status)
8088                 return status;
8089
8090         renew_lease(server, now);
8091         return 0;
8092 }
8093
8094 #ifdef CONFIG_NFS_V4_1
8095
8096 /*
8097  * This operation also signals the server that this client is
8098  * performing migration recovery.  The server can stop asserting
8099  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
8100  * performing this operation is identified in the SEQUENCE
8101  * operation in this compound.
8102  *
8103  * When the client supports GETATTR(fs_locations_info), it can
8104  * be plumbed in here.
8105  */
8106 static int _nfs41_proc_get_locations(struct nfs_server *server,
8107                                      struct nfs_fh *fhandle,
8108                                      struct nfs4_fs_locations *locations,
8109                                      struct page *page, const struct cred *cred)
8110 {
8111         struct rpc_clnt *clnt = server->client;
8112         u32 bitmask[2] = {
8113                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8114         };
8115         struct nfs4_fs_locations_arg args = {
8116                 .fh             = fhandle,
8117                 .page           = page,
8118                 .bitmask        = bitmask,
8119                 .migration      = 1,            /* skip LOOKUP */
8120         };
8121         struct nfs4_fs_locations_res res = {
8122                 .fs_locations   = locations,
8123                 .migration      = 1,
8124         };
8125         struct rpc_message msg = {
8126                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8127                 .rpc_argp       = &args,
8128                 .rpc_resp       = &res,
8129                 .rpc_cred       = cred,
8130         };
8131         struct nfs4_call_sync_data data = {
8132                 .seq_server = server,
8133                 .seq_args = &args.seq_args,
8134                 .seq_res = &res.seq_res,
8135         };
8136         struct rpc_task_setup task_setup_data = {
8137                 .rpc_client = clnt,
8138                 .rpc_message = &msg,
8139                 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8140                 .callback_data = &data,
8141                 .flags = RPC_TASK_NO_ROUND_ROBIN,
8142         };
8143         int status;
8144
8145         nfs_fattr_init(locations->fattr);
8146         locations->server = server;
8147         locations->nlocations = 0;
8148
8149         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8150         status = nfs4_call_sync_custom(&task_setup_data);
8151         if (status == NFS4_OK &&
8152             res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8153                 status = -NFS4ERR_LEASE_MOVED;
8154         return status;
8155 }
8156
8157 #endif  /* CONFIG_NFS_V4_1 */
8158
8159 /**
8160  * nfs4_proc_get_locations - discover locations for a migrated FSID
8161  * @server: pointer to nfs_server to process
8162  * @fhandle: pointer to the kernel NFS client file handle
8163  * @locations: result of query
8164  * @page: buffer
8165  * @cred: credential to use for this operation
8166  *
8167  * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8168  * operation failed, or a negative errno if a local error occurred.
8169  *
8170  * On success, "locations" is filled in, but if the server has
8171  * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8172  * asserted.
8173  *
8174  * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8175  * from this client that require migration recovery.
8176  */
8177 int nfs4_proc_get_locations(struct nfs_server *server,
8178                             struct nfs_fh *fhandle,
8179                             struct nfs4_fs_locations *locations,
8180                             struct page *page, const struct cred *cred)
8181 {
8182         struct nfs_client *clp = server->nfs_client;
8183         const struct nfs4_mig_recovery_ops *ops =
8184                                         clp->cl_mvops->mig_recovery_ops;
8185         struct nfs4_exception exception = {
8186                 .interruptible = true,
8187         };
8188         int status;
8189
8190         dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8191                 (unsigned long long)server->fsid.major,
8192                 (unsigned long long)server->fsid.minor,
8193                 clp->cl_hostname);
8194         nfs_display_fhandle(fhandle, __func__);
8195
8196         do {
8197                 status = ops->get_locations(server, fhandle, locations, page,
8198                                             cred);
8199                 if (status != -NFS4ERR_DELAY)
8200                         break;
8201                 nfs4_handle_exception(server, status, &exception);
8202         } while (exception.retry);
8203         return status;
8204 }
8205
8206 /*
8207  * This operation also signals the server that this client is
8208  * performing "lease moved" recovery.  The server can stop
8209  * returning NFS4ERR_LEASE_MOVED to this client.  A RENEW operation
8210  * is appended to this compound to identify the client ID which is
8211  * performing recovery.
8212  */
8213 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8214 {
8215         struct nfs_server *server = NFS_SERVER(inode);
8216         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8217         struct rpc_clnt *clnt = server->client;
8218         struct nfs4_fsid_present_arg args = {
8219                 .fh             = NFS_FH(inode),
8220                 .clientid       = clp->cl_clientid,
8221                 .renew          = 1,            /* append RENEW */
8222         };
8223         struct nfs4_fsid_present_res res = {
8224                 .renew          = 1,
8225         };
8226         struct rpc_message msg = {
8227                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8228                 .rpc_argp       = &args,
8229                 .rpc_resp       = &res,
8230                 .rpc_cred       = cred,
8231         };
8232         unsigned long now = jiffies;
8233         int status;
8234
8235         res.fh = nfs_alloc_fhandle();
8236         if (res.fh == NULL)
8237                 return -ENOMEM;
8238
8239         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8240         status = nfs4_call_sync_sequence(clnt, server, &msg,
8241                                                 &args.seq_args, &res.seq_res);
8242         nfs_free_fhandle(res.fh);
8243         if (status)
8244                 return status;
8245
8246         do_renew_lease(clp, now);
8247         return 0;
8248 }
8249
8250 #ifdef CONFIG_NFS_V4_1
8251
8252 /*
8253  * This operation also signals the server that this client is
8254  * performing "lease moved" recovery.  The server can stop asserting
8255  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID performing
8256  * this operation is identified in the SEQUENCE operation in this
8257  * compound.
8258  */
8259 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8260 {
8261         struct nfs_server *server = NFS_SERVER(inode);
8262         struct rpc_clnt *clnt = server->client;
8263         struct nfs4_fsid_present_arg args = {
8264                 .fh             = NFS_FH(inode),
8265         };
8266         struct nfs4_fsid_present_res res = {
8267         };
8268         struct rpc_message msg = {
8269                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8270                 .rpc_argp       = &args,
8271                 .rpc_resp       = &res,
8272                 .rpc_cred       = cred,
8273         };
8274         int status;
8275
8276         res.fh = nfs_alloc_fhandle();
8277         if (res.fh == NULL)
8278                 return -ENOMEM;
8279
8280         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8281         status = nfs4_call_sync_sequence(clnt, server, &msg,
8282                                                 &args.seq_args, &res.seq_res);
8283         nfs_free_fhandle(res.fh);
8284         if (status == NFS4_OK &&
8285             res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8286                 status = -NFS4ERR_LEASE_MOVED;
8287         return status;
8288 }
8289
8290 #endif  /* CONFIG_NFS_V4_1 */
8291
8292 /**
8293  * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8294  * @inode: inode on FSID to check
8295  * @cred: credential to use for this operation
8296  *
8297  * Server indicates whether the FSID is present, moved, or not
8298  * recognized.  This operation is necessary to clear a LEASE_MOVED
8299  * condition for this client ID.
8300  *
8301  * Returns NFS4_OK if the FSID is present on this server,
8302  * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8303  *  NFS4ERR code if some error occurred on the server, or a
8304  *  negative errno if a local failure occurred.
8305  */
8306 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8307 {
8308         struct nfs_server *server = NFS_SERVER(inode);
8309         struct nfs_client *clp = server->nfs_client;
8310         const struct nfs4_mig_recovery_ops *ops =
8311                                         clp->cl_mvops->mig_recovery_ops;
8312         struct nfs4_exception exception = {
8313                 .interruptible = true,
8314         };
8315         int status;
8316
8317         dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8318                 (unsigned long long)server->fsid.major,
8319                 (unsigned long long)server->fsid.minor,
8320                 clp->cl_hostname);
8321         nfs_display_fhandle(NFS_FH(inode), __func__);
8322
8323         do {
8324                 status = ops->fsid_present(inode, cred);
8325                 if (status != -NFS4ERR_DELAY)
8326                         break;
8327                 nfs4_handle_exception(server, status, &exception);
8328         } while (exception.retry);
8329         return status;
8330 }
8331
8332 /*
8333  * If 'use_integrity' is true and the state managment nfs_client
8334  * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8335  * and the machine credential as per RFC3530bis and RFC5661 Security
8336  * Considerations sections. Otherwise, just use the user cred with the
8337  * filesystem's rpc_client.
8338  */
8339 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8340 {
8341         int status;
8342         struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8343         struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8344         struct nfs4_secinfo_arg args = {
8345                 .dir_fh = NFS_FH(dir),
8346                 .name   = name,
8347         };
8348         struct nfs4_secinfo_res res = {
8349                 .flavors     = flavors,
8350         };
8351         struct rpc_message msg = {
8352                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8353                 .rpc_argp = &args,
8354                 .rpc_resp = &res,
8355         };
8356         struct nfs4_call_sync_data data = {
8357                 .seq_server = NFS_SERVER(dir),
8358                 .seq_args = &args.seq_args,
8359                 .seq_res = &res.seq_res,
8360         };
8361         struct rpc_task_setup task_setup = {
8362                 .rpc_client = clnt,
8363                 .rpc_message = &msg,
8364                 .callback_ops = clp->cl_mvops->call_sync_ops,
8365                 .callback_data = &data,
8366                 .flags = RPC_TASK_NO_ROUND_ROBIN,
8367         };
8368         const struct cred *cred = NULL;
8369
8370         if (use_integrity) {
8371                 clnt = clp->cl_rpcclient;
8372                 task_setup.rpc_client = clnt;
8373
8374                 cred = nfs4_get_clid_cred(clp);
8375                 msg.rpc_cred = cred;
8376         }
8377
8378         dprintk("NFS call  secinfo %s\n", name->name);
8379
8380         nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8381         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8382         status = nfs4_call_sync_custom(&task_setup);
8383
8384         dprintk("NFS reply  secinfo: %d\n", status);
8385
8386         put_cred(cred);
8387         return status;
8388 }
8389
8390 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8391                       struct nfs4_secinfo_flavors *flavors)
8392 {
8393         struct nfs4_exception exception = {
8394                 .interruptible = true,
8395         };
8396         int err;
8397         do {
8398                 err = -NFS4ERR_WRONGSEC;
8399
8400                 /* try to use integrity protection with machine cred */
8401                 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8402                         err = _nfs4_proc_secinfo(dir, name, flavors, true);
8403
8404                 /*
8405                  * if unable to use integrity protection, or SECINFO with
8406                  * integrity protection returns NFS4ERR_WRONGSEC (which is
8407                  * disallowed by spec, but exists in deployed servers) use
8408                  * the current filesystem's rpc_client and the user cred.
8409                  */
8410                 if (err == -NFS4ERR_WRONGSEC)
8411                         err = _nfs4_proc_secinfo(dir, name, flavors, false);
8412
8413                 trace_nfs4_secinfo(dir, name, err);
8414                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8415                                 &exception);
8416         } while (exception.retry);
8417         return err;
8418 }
8419
8420 #ifdef CONFIG_NFS_V4_1
8421 /*
8422  * Check the exchange flags returned by the server for invalid flags, having
8423  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8424  * DS flags set.
8425  */
8426 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8427 {
8428         if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8429                 goto out_inval;
8430         else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8431                 goto out_inval;
8432         if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8433             (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8434                 goto out_inval;
8435         if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8436                 goto out_inval;
8437         return NFS_OK;
8438 out_inval:
8439         return -NFS4ERR_INVAL;
8440 }
8441
8442 static bool
8443 nfs41_same_server_scope(struct nfs41_server_scope *a,
8444                         struct nfs41_server_scope *b)
8445 {
8446         if (a->server_scope_sz != b->server_scope_sz)
8447                 return false;
8448         return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8449 }
8450
8451 static void
8452 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8453 {
8454         struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8455         struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8456         struct nfs_client *clp = args->client;
8457
8458         switch (task->tk_status) {
8459         case -NFS4ERR_BADSESSION:
8460         case -NFS4ERR_DEADSESSION:
8461                 nfs4_schedule_session_recovery(clp->cl_session,
8462                                 task->tk_status);
8463                 return;
8464         }
8465         if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8466                         res->dir != NFS4_CDFS4_BOTH) {
8467                 rpc_task_close_connection(task);
8468                 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8469                         rpc_restart_call(task);
8470         }
8471 }
8472
8473 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8474         .rpc_call_done =  nfs4_bind_one_conn_to_session_done,
8475 };
8476
8477 /*
8478  * nfs4_proc_bind_one_conn_to_session()
8479  *
8480  * The 4.1 client currently uses the same TCP connection for the
8481  * fore and backchannel.
8482  */
8483 static
8484 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8485                 struct rpc_xprt *xprt,
8486                 struct nfs_client *clp,
8487                 const struct cred *cred)
8488 {
8489         int status;
8490         struct nfs41_bind_conn_to_session_args args = {
8491                 .client = clp,
8492                 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8493                 .retries = 0,
8494         };
8495         struct nfs41_bind_conn_to_session_res res;
8496         struct rpc_message msg = {
8497                 .rpc_proc =
8498                         &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8499                 .rpc_argp = &args,
8500                 .rpc_resp = &res,
8501                 .rpc_cred = cred,
8502         };
8503         struct rpc_task_setup task_setup_data = {
8504                 .rpc_client = clnt,
8505                 .rpc_xprt = xprt,
8506                 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8507                 .rpc_message = &msg,
8508                 .flags = RPC_TASK_TIMEOUT,
8509         };
8510         struct rpc_task *task;
8511
8512         nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8513         if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8514                 args.dir = NFS4_CDFC4_FORE;
8515
8516         /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8517         if (xprt != rcu_access_pointer(clnt->cl_xprt))
8518                 args.dir = NFS4_CDFC4_FORE;
8519
8520         task = rpc_run_task(&task_setup_data);
8521         if (!IS_ERR(task)) {
8522                 status = task->tk_status;
8523                 rpc_put_task(task);
8524         } else
8525                 status = PTR_ERR(task);
8526         trace_nfs4_bind_conn_to_session(clp, status);
8527         if (status == 0) {
8528                 if (memcmp(res.sessionid.data,
8529                     clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8530                         dprintk("NFS: %s: Session ID mismatch\n", __func__);
8531                         return -EIO;
8532                 }
8533                 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8534                         dprintk("NFS: %s: Unexpected direction from server\n",
8535                                 __func__);
8536                         return -EIO;
8537                 }
8538                 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8539                         dprintk("NFS: %s: Server returned RDMA mode = true\n",
8540                                 __func__);
8541                         return -EIO;
8542                 }
8543         }
8544
8545         return status;
8546 }
8547
8548 struct rpc_bind_conn_calldata {
8549         struct nfs_client *clp;
8550         const struct cred *cred;
8551 };
8552
8553 static int
8554 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8555                 struct rpc_xprt *xprt,
8556                 void *calldata)
8557 {
8558         struct rpc_bind_conn_calldata *p = calldata;
8559
8560         return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8561 }
8562
8563 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8564 {
8565         struct rpc_bind_conn_calldata data = {
8566                 .clp = clp,
8567                 .cred = cred,
8568         };
8569         return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8570                         nfs4_proc_bind_conn_to_session_callback, &data);
8571 }
8572
8573 /*
8574  * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8575  * and operations we'd like to see to enable certain features in the allow map
8576  */
8577 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8578         .how = SP4_MACH_CRED,
8579         .enforce.u.words = {
8580                 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8581                       1 << (OP_EXCHANGE_ID - 32) |
8582                       1 << (OP_CREATE_SESSION - 32) |
8583                       1 << (OP_DESTROY_SESSION - 32) |
8584                       1 << (OP_DESTROY_CLIENTID - 32)
8585         },
8586         .allow.u.words = {
8587                 [0] = 1 << (OP_CLOSE) |
8588                       1 << (OP_OPEN_DOWNGRADE) |
8589                       1 << (OP_LOCKU) |
8590                       1 << (OP_DELEGRETURN) |
8591                       1 << (OP_COMMIT),
8592                 [1] = 1 << (OP_SECINFO - 32) |
8593                       1 << (OP_SECINFO_NO_NAME - 32) |
8594                       1 << (OP_LAYOUTRETURN - 32) |
8595                       1 << (OP_TEST_STATEID - 32) |
8596                       1 << (OP_FREE_STATEID - 32) |
8597                       1 << (OP_WRITE - 32)
8598         }
8599 };
8600
8601 /*
8602  * Select the state protection mode for client `clp' given the server results
8603  * from exchange_id in `sp'.
8604  *
8605  * Returns 0 on success, negative errno otherwise.
8606  */
8607 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8608                                  struct nfs41_state_protection *sp)
8609 {
8610         static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8611                 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8612                       1 << (OP_EXCHANGE_ID - 32) |
8613                       1 << (OP_CREATE_SESSION - 32) |
8614                       1 << (OP_DESTROY_SESSION - 32) |
8615                       1 << (OP_DESTROY_CLIENTID - 32)
8616         };
8617         unsigned long flags = 0;
8618         unsigned int i;
8619         int ret = 0;
8620
8621         if (sp->how == SP4_MACH_CRED) {
8622                 /* Print state protect result */
8623                 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8624                 for (i = 0; i <= LAST_NFS4_OP; i++) {
8625                         if (test_bit(i, sp->enforce.u.longs))
8626                                 dfprintk(MOUNT, "  enforce op %d\n", i);
8627                         if (test_bit(i, sp->allow.u.longs))
8628                                 dfprintk(MOUNT, "  allow op %d\n", i);
8629                 }
8630
8631                 /* make sure nothing is on enforce list that isn't supported */
8632                 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8633                         if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8634                                 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8635                                 ret = -EINVAL;
8636                                 goto out;
8637                         }
8638                 }
8639
8640                 /*
8641                  * Minimal mode - state operations are allowed to use machine
8642                  * credential.  Note this already happens by default, so the
8643                  * client doesn't have to do anything more than the negotiation.
8644                  *
8645                  * NOTE: we don't care if EXCHANGE_ID is in the list -
8646                  *       we're already using the machine cred for exchange_id
8647                  *       and will never use a different cred.
8648                  */
8649                 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8650                     test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8651                     test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8652                     test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8653                         dfprintk(MOUNT, "sp4_mach_cred:\n");
8654                         dfprintk(MOUNT, "  minimal mode enabled\n");
8655                         __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8656                 } else {
8657                         dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8658                         ret = -EINVAL;
8659                         goto out;
8660                 }
8661
8662                 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8663                     test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8664                     test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8665                     test_bit(OP_LOCKU, sp->allow.u.longs)) {
8666                         dfprintk(MOUNT, "  cleanup mode enabled\n");
8667                         __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8668                 }
8669
8670                 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8671                         dfprintk(MOUNT, "  pnfs cleanup mode enabled\n");
8672                         __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8673                 }
8674
8675                 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8676                     test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8677                         dfprintk(MOUNT, "  secinfo mode enabled\n");
8678                         __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8679                 }
8680
8681                 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8682                     test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8683                         dfprintk(MOUNT, "  stateid mode enabled\n");
8684                         __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8685                 }
8686
8687                 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8688                         dfprintk(MOUNT, "  write mode enabled\n");
8689                         __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8690                 }
8691
8692                 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8693                         dfprintk(MOUNT, "  commit mode enabled\n");
8694                         __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8695                 }
8696         }
8697 out:
8698         clp->cl_sp4_flags = flags;
8699         return ret;
8700 }
8701
8702 struct nfs41_exchange_id_data {
8703         struct nfs41_exchange_id_res res;
8704         struct nfs41_exchange_id_args args;
8705 };
8706
8707 static void nfs4_exchange_id_release(void *data)
8708 {
8709         struct nfs41_exchange_id_data *cdata =
8710                                         (struct nfs41_exchange_id_data *)data;
8711
8712         nfs_put_client(cdata->args.client);
8713         kfree(cdata->res.impl_id);
8714         kfree(cdata->res.server_scope);
8715         kfree(cdata->res.server_owner);
8716         kfree(cdata);
8717 }
8718
8719 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8720         .rpc_release = nfs4_exchange_id_release,
8721 };
8722
8723 /*
8724  * _nfs4_proc_exchange_id()
8725  *
8726  * Wrapper for EXCHANGE_ID operation.
8727  */
8728 static struct rpc_task *
8729 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8730                         u32 sp4_how, struct rpc_xprt *xprt)
8731 {
8732         struct rpc_message msg = {
8733                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8734                 .rpc_cred = cred,
8735         };
8736         struct rpc_task_setup task_setup_data = {
8737                 .rpc_client = clp->cl_rpcclient,
8738                 .callback_ops = &nfs4_exchange_id_call_ops,
8739                 .rpc_message = &msg,
8740                 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8741         };
8742         struct nfs41_exchange_id_data *calldata;
8743         int status;
8744
8745         if (!refcount_inc_not_zero(&clp->cl_count))
8746                 return ERR_PTR(-EIO);
8747
8748         status = -ENOMEM;
8749         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8750         if (!calldata)
8751                 goto out;
8752
8753         nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8754
8755         status = nfs4_init_uniform_client_string(clp);
8756         if (status)
8757                 goto out_calldata;
8758
8759         calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8760                                                 GFP_NOFS);
8761         status = -ENOMEM;
8762         if (unlikely(calldata->res.server_owner == NULL))
8763                 goto out_calldata;
8764
8765         calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8766                                         GFP_NOFS);
8767         if (unlikely(calldata->res.server_scope == NULL))
8768                 goto out_server_owner;
8769
8770         calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8771         if (unlikely(calldata->res.impl_id == NULL))
8772                 goto out_server_scope;
8773
8774         switch (sp4_how) {
8775         case SP4_NONE:
8776                 calldata->args.state_protect.how = SP4_NONE;
8777                 break;
8778
8779         case SP4_MACH_CRED:
8780                 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8781                 break;
8782
8783         default:
8784                 /* unsupported! */
8785                 WARN_ON_ONCE(1);
8786                 status = -EINVAL;
8787                 goto out_impl_id;
8788         }
8789         if (xprt) {
8790                 task_setup_data.rpc_xprt = xprt;
8791                 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8792                 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8793                                 sizeof(calldata->args.verifier.data));
8794         }
8795         calldata->args.client = clp;
8796         calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8797         EXCHGID4_FLAG_BIND_PRINC_STATEID;
8798 #ifdef CONFIG_NFS_V4_1_MIGRATION
8799         calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8800 #endif
8801         msg.rpc_argp = &calldata->args;
8802         msg.rpc_resp = &calldata->res;
8803         task_setup_data.callback_data = calldata;
8804
8805         return rpc_run_task(&task_setup_data);
8806
8807 out_impl_id:
8808         kfree(calldata->res.impl_id);
8809 out_server_scope:
8810         kfree(calldata->res.server_scope);
8811 out_server_owner:
8812         kfree(calldata->res.server_owner);
8813 out_calldata:
8814         kfree(calldata);
8815 out:
8816         nfs_put_client(clp);
8817         return ERR_PTR(status);
8818 }
8819
8820 /*
8821  * _nfs4_proc_exchange_id()
8822  *
8823  * Wrapper for EXCHANGE_ID operation.
8824  */
8825 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8826                         u32 sp4_how)
8827 {
8828         struct rpc_task *task;
8829         struct nfs41_exchange_id_args *argp;
8830         struct nfs41_exchange_id_res *resp;
8831         unsigned long now = jiffies;
8832         int status;
8833
8834         task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8835         if (IS_ERR(task))
8836                 return PTR_ERR(task);
8837
8838         argp = task->tk_msg.rpc_argp;
8839         resp = task->tk_msg.rpc_resp;
8840         status = task->tk_status;
8841         if (status  != 0)
8842                 goto out;
8843
8844         status = nfs4_check_cl_exchange_flags(resp->flags,
8845                         clp->cl_mvops->minor_version);
8846         if (status  != 0)
8847                 goto out;
8848
8849         status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8850         if (status != 0)
8851                 goto out;
8852
8853         do_renew_lease(clp, now);
8854
8855         clp->cl_clientid = resp->clientid;
8856         clp->cl_exchange_flags = resp->flags;
8857         clp->cl_seqid = resp->seqid;
8858         /* Client ID is not confirmed */
8859         if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8860                 clear_bit(NFS4_SESSION_ESTABLISHED,
8861                           &clp->cl_session->session_state);
8862
8863         if (clp->cl_serverscope != NULL &&
8864             !nfs41_same_server_scope(clp->cl_serverscope,
8865                                 resp->server_scope)) {
8866                 dprintk("%s: server_scope mismatch detected\n",
8867                         __func__);
8868                 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8869         }
8870
8871         swap(clp->cl_serverowner, resp->server_owner);
8872         swap(clp->cl_serverscope, resp->server_scope);
8873         swap(clp->cl_implid, resp->impl_id);
8874
8875         /* Save the EXCHANGE_ID verifier session trunk tests */
8876         memcpy(clp->cl_confirm.data, argp->verifier.data,
8877                sizeof(clp->cl_confirm.data));
8878 out:
8879         trace_nfs4_exchange_id(clp, status);
8880         rpc_put_task(task);
8881         return status;
8882 }
8883
8884 /*
8885  * nfs4_proc_exchange_id()
8886  *
8887  * Returns zero, a negative errno, or a negative NFS4ERR status code.
8888  *
8889  * Since the clientid has expired, all compounds using sessions
8890  * associated with the stale clientid will be returning
8891  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8892  * be in some phase of session reset.
8893  *
8894  * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8895  */
8896 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8897 {
8898         rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8899         int status;
8900
8901         /* try SP4_MACH_CRED if krb5i/p */
8902         if (authflavor == RPC_AUTH_GSS_KRB5I ||
8903             authflavor == RPC_AUTH_GSS_KRB5P) {
8904                 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8905                 if (!status)
8906                         return 0;
8907         }
8908
8909         /* try SP4_NONE */
8910         return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8911 }
8912
8913 /**
8914  * nfs4_test_session_trunk
8915  *
8916  * This is an add_xprt_test() test function called from
8917  * rpc_clnt_setup_test_and_add_xprt.
8918  *
8919  * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8920  * and is dereferrenced in nfs4_exchange_id_release
8921  *
8922  * Upon success, add the new transport to the rpc_clnt
8923  *
8924  * @clnt: struct rpc_clnt to get new transport
8925  * @xprt: the rpc_xprt to test
8926  * @data: call data for _nfs4_proc_exchange_id.
8927  */
8928 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8929                             void *data)
8930 {
8931         struct nfs4_add_xprt_data *adata = data;
8932         struct rpc_task *task;
8933         int status;
8934
8935         u32 sp4_how;
8936
8937         dprintk("--> %s try %s\n", __func__,
8938                 xprt->address_strings[RPC_DISPLAY_ADDR]);
8939
8940         sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8941
8942         /* Test connection for session trunking. Async exchange_id call */
8943         task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8944         if (IS_ERR(task))
8945                 return;
8946
8947         status = task->tk_status;
8948         if (status == 0)
8949                 status = nfs4_detect_session_trunking(adata->clp,
8950                                 task->tk_msg.rpc_resp, xprt);
8951
8952         if (status == 0)
8953                 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8954         else if (rpc_clnt_xprt_switch_has_addr(clnt,
8955                                 (struct sockaddr *)&xprt->addr))
8956                 rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
8957
8958         rpc_put_task(task);
8959 }
8960 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8961
8962 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8963                 const struct cred *cred)
8964 {
8965         struct rpc_message msg = {
8966                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8967                 .rpc_argp = clp,
8968                 .rpc_cred = cred,
8969         };
8970         int status;
8971
8972         status = rpc_call_sync(clp->cl_rpcclient, &msg,
8973                                RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8974         trace_nfs4_destroy_clientid(clp, status);
8975         if (status)
8976                 dprintk("NFS: Got error %d from the server %s on "
8977                         "DESTROY_CLIENTID.", status, clp->cl_hostname);
8978         return status;
8979 }
8980
8981 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8982                 const struct cred *cred)
8983 {
8984         unsigned int loop;
8985         int ret;
8986
8987         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8988                 ret = _nfs4_proc_destroy_clientid(clp, cred);
8989                 switch (ret) {
8990                 case -NFS4ERR_DELAY:
8991                 case -NFS4ERR_CLIENTID_BUSY:
8992                         ssleep(1);
8993                         break;
8994                 default:
8995                         return ret;
8996                 }
8997         }
8998         return 0;
8999 }
9000
9001 int nfs4_destroy_clientid(struct nfs_client *clp)
9002 {
9003         const struct cred *cred;
9004         int ret = 0;
9005
9006         if (clp->cl_mvops->minor_version < 1)
9007                 goto out;
9008         if (clp->cl_exchange_flags == 0)
9009                 goto out;
9010         if (clp->cl_preserve_clid)
9011                 goto out;
9012         cred = nfs4_get_clid_cred(clp);
9013         ret = nfs4_proc_destroy_clientid(clp, cred);
9014         put_cred(cred);
9015         switch (ret) {
9016         case 0:
9017         case -NFS4ERR_STALE_CLIENTID:
9018                 clp->cl_exchange_flags = 0;
9019         }
9020 out:
9021         return ret;
9022 }
9023
9024 #endif /* CONFIG_NFS_V4_1 */
9025
9026 struct nfs4_get_lease_time_data {
9027         struct nfs4_get_lease_time_args *args;
9028         struct nfs4_get_lease_time_res *res;
9029         struct nfs_client *clp;
9030 };
9031
9032 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9033                                         void *calldata)
9034 {
9035         struct nfs4_get_lease_time_data *data =
9036                         (struct nfs4_get_lease_time_data *)calldata;
9037
9038         /* just setup sequence, do not trigger session recovery
9039            since we're invoked within one */
9040         nfs4_setup_sequence(data->clp,
9041                         &data->args->la_seq_args,
9042                         &data->res->lr_seq_res,
9043                         task);
9044 }
9045
9046 /*
9047  * Called from nfs4_state_manager thread for session setup, so don't recover
9048  * from sequence operation or clientid errors.
9049  */
9050 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9051 {
9052         struct nfs4_get_lease_time_data *data =
9053                         (struct nfs4_get_lease_time_data *)calldata;
9054
9055         if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9056                 return;
9057         switch (task->tk_status) {
9058         case -NFS4ERR_DELAY:
9059         case -NFS4ERR_GRACE:
9060                 rpc_delay(task, NFS4_POLL_RETRY_MIN);
9061                 task->tk_status = 0;
9062                 fallthrough;
9063         case -NFS4ERR_RETRY_UNCACHED_REP:
9064                 rpc_restart_call_prepare(task);
9065                 return;
9066         }
9067 }
9068
9069 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9070         .rpc_call_prepare = nfs4_get_lease_time_prepare,
9071         .rpc_call_done = nfs4_get_lease_time_done,
9072 };
9073
9074 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9075 {
9076         struct nfs4_get_lease_time_args args;
9077         struct nfs4_get_lease_time_res res = {
9078                 .lr_fsinfo = fsinfo,
9079         };
9080         struct nfs4_get_lease_time_data data = {
9081                 .args = &args,
9082                 .res = &res,
9083                 .clp = clp,
9084         };
9085         struct rpc_message msg = {
9086                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9087                 .rpc_argp = &args,
9088                 .rpc_resp = &res,
9089         };
9090         struct rpc_task_setup task_setup = {
9091                 .rpc_client = clp->cl_rpcclient,
9092                 .rpc_message = &msg,
9093                 .callback_ops = &nfs4_get_lease_time_ops,
9094                 .callback_data = &data,
9095                 .flags = RPC_TASK_TIMEOUT,
9096         };
9097
9098         nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9099         return nfs4_call_sync_custom(&task_setup);
9100 }
9101
9102 #ifdef CONFIG_NFS_V4_1
9103
9104 /*
9105  * Initialize the values to be used by the client in CREATE_SESSION
9106  * If nfs4_init_session set the fore channel request and response sizes,
9107  * use them.
9108  *
9109  * Set the back channel max_resp_sz_cached to zero to force the client to
9110  * always set csa_cachethis to FALSE because the current implementation
9111  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9112  */
9113 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9114                                     struct rpc_clnt *clnt)
9115 {
9116         unsigned int max_rqst_sz, max_resp_sz;
9117         unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9118         unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9119
9120         max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9121         max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9122
9123         /* Fore channel attributes */
9124         args->fc_attrs.max_rqst_sz = max_rqst_sz;
9125         args->fc_attrs.max_resp_sz = max_resp_sz;
9126         args->fc_attrs.max_ops = NFS4_MAX_OPS;
9127         args->fc_attrs.max_reqs = max_session_slots;
9128
9129         dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9130                 "max_ops=%u max_reqs=%u\n",
9131                 __func__,
9132                 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9133                 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9134
9135         /* Back channel attributes */
9136         args->bc_attrs.max_rqst_sz = max_bc_payload;
9137         args->bc_attrs.max_resp_sz = max_bc_payload;
9138         args->bc_attrs.max_resp_sz_cached = 0;
9139         args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9140         args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9141         if (args->bc_attrs.max_reqs > max_bc_slots)
9142                 args->bc_attrs.max_reqs = max_bc_slots;
9143
9144         dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9145                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9146                 __func__,
9147                 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9148                 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9149                 args->bc_attrs.max_reqs);
9150 }
9151
9152 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9153                 struct nfs41_create_session_res *res)
9154 {
9155         struct nfs4_channel_attrs *sent = &args->fc_attrs;
9156         struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9157
9158         if (rcvd->max_resp_sz > sent->max_resp_sz)
9159                 return -EINVAL;
9160         /*
9161          * Our requested max_ops is the minimum we need; we're not
9162          * prepared to break up compounds into smaller pieces than that.
9163          * So, no point even trying to continue if the server won't
9164          * cooperate:
9165          */
9166         if (rcvd->max_ops < sent->max_ops)
9167                 return -EINVAL;
9168         if (rcvd->max_reqs == 0)
9169                 return -EINVAL;
9170         if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9171                 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9172         return 0;
9173 }
9174
9175 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9176                 struct nfs41_create_session_res *res)
9177 {
9178         struct nfs4_channel_attrs *sent = &args->bc_attrs;
9179         struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9180
9181         if (!(res->flags & SESSION4_BACK_CHAN))
9182                 goto out;
9183         if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9184                 return -EINVAL;
9185         if (rcvd->max_resp_sz < sent->max_resp_sz)
9186                 return -EINVAL;
9187         if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9188                 return -EINVAL;
9189         if (rcvd->max_ops > sent->max_ops)
9190                 return -EINVAL;
9191         if (rcvd->max_reqs > sent->max_reqs)
9192                 return -EINVAL;
9193 out:
9194         return 0;
9195 }
9196
9197 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9198                                      struct nfs41_create_session_res *res)
9199 {
9200         int ret;
9201
9202         ret = nfs4_verify_fore_channel_attrs(args, res);
9203         if (ret)
9204                 return ret;
9205         return nfs4_verify_back_channel_attrs(args, res);
9206 }
9207
9208 static void nfs4_update_session(struct nfs4_session *session,
9209                 struct nfs41_create_session_res *res)
9210 {
9211         nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9212         /* Mark client id and session as being confirmed */
9213         session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9214         set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9215         session->flags = res->flags;
9216         memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9217         if (res->flags & SESSION4_BACK_CHAN)
9218                 memcpy(&session->bc_attrs, &res->bc_attrs,
9219                                 sizeof(session->bc_attrs));
9220 }
9221
9222 static int _nfs4_proc_create_session(struct nfs_client *clp,
9223                 const struct cred *cred)
9224 {
9225         struct nfs4_session *session = clp->cl_session;
9226         struct nfs41_create_session_args args = {
9227                 .client = clp,
9228                 .clientid = clp->cl_clientid,
9229                 .seqid = clp->cl_seqid,
9230                 .cb_program = NFS4_CALLBACK,
9231         };
9232         struct nfs41_create_session_res res;
9233
9234         struct rpc_message msg = {
9235                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9236                 .rpc_argp = &args,
9237                 .rpc_resp = &res,
9238                 .rpc_cred = cred,
9239         };
9240         int status;
9241
9242         nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9243         args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9244
9245         status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9246                                RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9247         trace_nfs4_create_session(clp, status);
9248
9249         switch (status) {
9250         case -NFS4ERR_STALE_CLIENTID:
9251         case -NFS4ERR_DELAY:
9252         case -ETIMEDOUT:
9253         case -EACCES:
9254         case -EAGAIN:
9255                 goto out;
9256         }
9257
9258         clp->cl_seqid++;
9259         if (!status) {
9260                 /* Verify the session's negotiated channel_attrs values */
9261                 status = nfs4_verify_channel_attrs(&args, &res);
9262                 /* Increment the clientid slot sequence id */
9263                 if (status)
9264                         goto out;
9265                 nfs4_update_session(session, &res);
9266         }
9267 out:
9268         return status;
9269 }
9270
9271 /*
9272  * Issues a CREATE_SESSION operation to the server.
9273  * It is the responsibility of the caller to verify the session is
9274  * expired before calling this routine.
9275  */
9276 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9277 {
9278         int status;
9279         unsigned *ptr;
9280         struct nfs4_session *session = clp->cl_session;
9281         struct nfs4_add_xprt_data xprtdata = {
9282                 .clp = clp,
9283         };
9284         struct rpc_add_xprt_test rpcdata = {
9285                 .add_xprt_test = clp->cl_mvops->session_trunk,
9286                 .data = &xprtdata,
9287         };
9288
9289         dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9290
9291         status = _nfs4_proc_create_session(clp, cred);
9292         if (status)
9293                 goto out;
9294
9295         /* Init or reset the session slot tables */
9296         status = nfs4_setup_session_slot_tables(session);
9297         dprintk("slot table setup returned %d\n", status);
9298         if (status)
9299                 goto out;
9300
9301         ptr = (unsigned *)&session->sess_id.data[0];
9302         dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9303                 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9304         rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9305 out:
9306         return status;
9307 }
9308
9309 /*
9310  * Issue the over-the-wire RPC DESTROY_SESSION.
9311  * The caller must serialize access to this routine.
9312  */
9313 int nfs4_proc_destroy_session(struct nfs4_session *session,
9314                 const struct cred *cred)
9315 {
9316         struct rpc_message msg = {
9317                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9318                 .rpc_argp = session,
9319                 .rpc_cred = cred,
9320         };
9321         int status = 0;
9322
9323         /* session is still being setup */
9324         if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9325                 return 0;
9326
9327         status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9328                                RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9329         trace_nfs4_destroy_session(session->clp, status);
9330
9331         if (status)
9332                 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9333                         "Session has been destroyed regardless...\n", status);
9334         rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9335         return status;
9336 }
9337
9338 /*
9339  * Renew the cl_session lease.
9340  */
9341 struct nfs4_sequence_data {
9342         struct nfs_client *clp;
9343         struct nfs4_sequence_args args;
9344         struct nfs4_sequence_res res;
9345 };
9346
9347 static void nfs41_sequence_release(void *data)
9348 {
9349         struct nfs4_sequence_data *calldata = data;
9350         struct nfs_client *clp = calldata->clp;
9351
9352         if (refcount_read(&clp->cl_count) > 1)
9353                 nfs4_schedule_state_renewal(clp);
9354         nfs_put_client(clp);
9355         kfree(calldata);
9356 }
9357
9358 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9359 {
9360         switch(task->tk_status) {
9361         case -NFS4ERR_DELAY:
9362                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9363                 return -EAGAIN;
9364         default:
9365                 nfs4_schedule_lease_recovery(clp);
9366         }
9367         return 0;
9368 }
9369
9370 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9371 {
9372         struct nfs4_sequence_data *calldata = data;
9373         struct nfs_client *clp = calldata->clp;
9374
9375         if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9376                 return;
9377
9378         trace_nfs4_sequence(clp, task->tk_status);
9379         if (task->tk_status < 0 && !task->tk_client->cl_shutdown) {
9380                 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9381                 if (refcount_read(&clp->cl_count) == 1)
9382                         return;
9383
9384                 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9385                         rpc_restart_call_prepare(task);
9386                         return;
9387                 }
9388         }
9389         dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9390 }
9391
9392 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9393 {
9394         struct nfs4_sequence_data *calldata = data;
9395         struct nfs_client *clp = calldata->clp;
9396         struct nfs4_sequence_args *args;
9397         struct nfs4_sequence_res *res;
9398
9399         args = task->tk_msg.rpc_argp;
9400         res = task->tk_msg.rpc_resp;
9401
9402         nfs4_setup_sequence(clp, args, res, task);
9403 }
9404
9405 static const struct rpc_call_ops nfs41_sequence_ops = {
9406         .rpc_call_done = nfs41_sequence_call_done,
9407         .rpc_call_prepare = nfs41_sequence_prepare,
9408         .rpc_release = nfs41_sequence_release,
9409 };
9410
9411 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9412                 const struct cred *cred,
9413                 struct nfs4_slot *slot,
9414                 bool is_privileged)
9415 {
9416         struct nfs4_sequence_data *calldata;
9417         struct rpc_message msg = {
9418                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9419                 .rpc_cred = cred,
9420         };
9421         struct rpc_task_setup task_setup_data = {
9422                 .rpc_client = clp->cl_rpcclient,
9423                 .rpc_message = &msg,
9424                 .callback_ops = &nfs41_sequence_ops,
9425                 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9426         };
9427         struct rpc_task *ret;
9428
9429         ret = ERR_PTR(-EIO);
9430         if (!refcount_inc_not_zero(&clp->cl_count))
9431                 goto out_err;
9432
9433         ret = ERR_PTR(-ENOMEM);
9434         calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9435         if (calldata == NULL)
9436                 goto out_put_clp;
9437         nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9438         nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9439         msg.rpc_argp = &calldata->args;
9440         msg.rpc_resp = &calldata->res;
9441         calldata->clp = clp;
9442         task_setup_data.callback_data = calldata;
9443
9444         ret = rpc_run_task(&task_setup_data);
9445         if (IS_ERR(ret))
9446                 goto out_err;
9447         return ret;
9448 out_put_clp:
9449         nfs_put_client(clp);
9450 out_err:
9451         nfs41_release_slot(slot);
9452         return ret;
9453 }
9454
9455 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9456 {
9457         struct rpc_task *task;
9458         int ret = 0;
9459
9460         if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9461                 return -EAGAIN;
9462         task = _nfs41_proc_sequence(clp, cred, NULL, false);
9463         if (IS_ERR(task))
9464                 ret = PTR_ERR(task);
9465         else
9466                 rpc_put_task_async(task);
9467         dprintk("<-- %s status=%d\n", __func__, ret);
9468         return ret;
9469 }
9470
9471 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9472 {
9473         struct rpc_task *task;
9474         int ret;
9475
9476         task = _nfs41_proc_sequence(clp, cred, NULL, true);
9477         if (IS_ERR(task)) {
9478                 ret = PTR_ERR(task);
9479                 goto out;
9480         }
9481         ret = rpc_wait_for_completion_task(task);
9482         if (!ret)
9483                 ret = task->tk_status;
9484         rpc_put_task(task);
9485 out:
9486         dprintk("<-- %s status=%d\n", __func__, ret);
9487         return ret;
9488 }
9489
9490 struct nfs4_reclaim_complete_data {
9491         struct nfs_client *clp;
9492         struct nfs41_reclaim_complete_args arg;
9493         struct nfs41_reclaim_complete_res res;
9494 };
9495
9496 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9497 {
9498         struct nfs4_reclaim_complete_data *calldata = data;
9499
9500         nfs4_setup_sequence(calldata->clp,
9501                         &calldata->arg.seq_args,
9502                         &calldata->res.seq_res,
9503                         task);
9504 }
9505
9506 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9507 {
9508         switch(task->tk_status) {
9509         case 0:
9510                 wake_up_all(&clp->cl_lock_waitq);
9511                 fallthrough;
9512         case -NFS4ERR_COMPLETE_ALREADY:
9513         case -NFS4ERR_WRONG_CRED: /* What to do here? */
9514                 break;
9515         case -NFS4ERR_DELAY:
9516                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9517                 fallthrough;
9518         case -NFS4ERR_RETRY_UNCACHED_REP:
9519         case -EACCES:
9520                 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9521                         __func__, task->tk_status, clp->cl_hostname);
9522                 return -EAGAIN;
9523         case -NFS4ERR_BADSESSION:
9524         case -NFS4ERR_DEADSESSION:
9525         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9526                 break;
9527         default:
9528                 nfs4_schedule_lease_recovery(clp);
9529         }
9530         return 0;
9531 }
9532
9533 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9534 {
9535         struct nfs4_reclaim_complete_data *calldata = data;
9536         struct nfs_client *clp = calldata->clp;
9537         struct nfs4_sequence_res *res = &calldata->res.seq_res;
9538
9539         if (!nfs41_sequence_done(task, res))
9540                 return;
9541
9542         trace_nfs4_reclaim_complete(clp, task->tk_status);
9543         if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9544                 rpc_restart_call_prepare(task);
9545                 return;
9546         }
9547 }
9548
9549 static void nfs4_free_reclaim_complete_data(void *data)
9550 {
9551         struct nfs4_reclaim_complete_data *calldata = data;
9552
9553         kfree(calldata);
9554 }
9555
9556 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9557         .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9558         .rpc_call_done = nfs4_reclaim_complete_done,
9559         .rpc_release = nfs4_free_reclaim_complete_data,
9560 };
9561
9562 /*
9563  * Issue a global reclaim complete.
9564  */
9565 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9566                 const struct cred *cred)
9567 {
9568         struct nfs4_reclaim_complete_data *calldata;
9569         struct rpc_message msg = {
9570                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9571                 .rpc_cred = cred,
9572         };
9573         struct rpc_task_setup task_setup_data = {
9574                 .rpc_client = clp->cl_rpcclient,
9575                 .rpc_message = &msg,
9576                 .callback_ops = &nfs4_reclaim_complete_call_ops,
9577                 .flags = RPC_TASK_NO_ROUND_ROBIN,
9578         };
9579         int status = -ENOMEM;
9580
9581         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9582         if (calldata == NULL)
9583                 goto out;
9584         calldata->clp = clp;
9585         calldata->arg.one_fs = 0;
9586
9587         nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9588         msg.rpc_argp = &calldata->arg;
9589         msg.rpc_resp = &calldata->res;
9590         task_setup_data.callback_data = calldata;
9591         status = nfs4_call_sync_custom(&task_setup_data);
9592 out:
9593         dprintk("<-- %s status=%d\n", __func__, status);
9594         return status;
9595 }
9596
9597 static void
9598 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9599 {
9600         struct nfs4_layoutget *lgp = calldata;
9601         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9602
9603         nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9604                                 &lgp->res.seq_res, task);
9605 }
9606
9607 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9608 {
9609         struct nfs4_layoutget *lgp = calldata;
9610
9611         nfs41_sequence_process(task, &lgp->res.seq_res);
9612 }
9613
9614 static int
9615 nfs4_layoutget_handle_exception(struct rpc_task *task,
9616                 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9617 {
9618         struct inode *inode = lgp->args.inode;
9619         struct nfs_server *server = NFS_SERVER(inode);
9620         struct pnfs_layout_hdr *lo = lgp->lo;
9621         int nfs4err = task->tk_status;
9622         int err, status = 0;
9623         LIST_HEAD(head);
9624
9625         dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9626
9627         nfs4_sequence_free_slot(&lgp->res.seq_res);
9628
9629         switch (nfs4err) {
9630         case 0:
9631                 goto out;
9632
9633         /*
9634          * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9635          * on the file. set tk_status to -ENODATA to tell upper layer to
9636          * retry go inband.
9637          */
9638         case -NFS4ERR_LAYOUTUNAVAILABLE:
9639                 status = -ENODATA;
9640                 goto out;
9641         /*
9642          * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9643          * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9644          */
9645         case -NFS4ERR_BADLAYOUT:
9646                 status = -EOVERFLOW;
9647                 goto out;
9648         /*
9649          * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9650          * (or clients) writing to the same RAID stripe except when
9651          * the minlength argument is 0 (see RFC5661 section 18.43.3).
9652          *
9653          * Treat it like we would RECALLCONFLICT -- we retry for a little
9654          * while, and then eventually give up.
9655          */
9656         case -NFS4ERR_LAYOUTTRYLATER:
9657                 if (lgp->args.minlength == 0) {
9658                         status = -EOVERFLOW;
9659                         goto out;
9660                 }
9661                 status = -EBUSY;
9662                 break;
9663         case -NFS4ERR_RECALLCONFLICT:
9664                 status = -ERECALLCONFLICT;
9665                 break;
9666         case -NFS4ERR_DELEG_REVOKED:
9667         case -NFS4ERR_ADMIN_REVOKED:
9668         case -NFS4ERR_EXPIRED:
9669         case -NFS4ERR_BAD_STATEID:
9670                 exception->timeout = 0;
9671                 spin_lock(&inode->i_lock);
9672                 /* If the open stateid was bad, then recover it. */
9673                 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9674                     !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9675                         spin_unlock(&inode->i_lock);
9676                         exception->state = lgp->args.ctx->state;
9677                         exception->stateid = &lgp->args.stateid;
9678                         break;
9679                 }
9680
9681                 /*
9682                  * Mark the bad layout state as invalid, then retry
9683                  */
9684                 pnfs_mark_layout_stateid_invalid(lo, &head);
9685                 spin_unlock(&inode->i_lock);
9686                 nfs_commit_inode(inode, 0);
9687                 pnfs_free_lseg_list(&head);
9688                 status = -EAGAIN;
9689                 goto out;
9690         }
9691
9692         err = nfs4_handle_exception(server, nfs4err, exception);
9693         if (!status) {
9694                 if (exception->retry)
9695                         status = -EAGAIN;
9696                 else
9697                         status = err;
9698         }
9699 out:
9700         return status;
9701 }
9702
9703 size_t max_response_pages(struct nfs_server *server)
9704 {
9705         u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9706         return nfs_page_array_len(0, max_resp_sz);
9707 }
9708
9709 static void nfs4_layoutget_release(void *calldata)
9710 {
9711         struct nfs4_layoutget *lgp = calldata;
9712
9713         nfs4_sequence_free_slot(&lgp->res.seq_res);
9714         pnfs_layoutget_free(lgp);
9715 }
9716
9717 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9718         .rpc_call_prepare = nfs4_layoutget_prepare,
9719         .rpc_call_done = nfs4_layoutget_done,
9720         .rpc_release = nfs4_layoutget_release,
9721 };
9722
9723 struct pnfs_layout_segment *
9724 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9725 {
9726         struct inode *inode = lgp->args.inode;
9727         struct nfs_server *server = NFS_SERVER(inode);
9728         struct rpc_task *task;
9729         struct rpc_message msg = {
9730                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9731                 .rpc_argp = &lgp->args,
9732                 .rpc_resp = &lgp->res,
9733                 .rpc_cred = lgp->cred,
9734         };
9735         struct rpc_task_setup task_setup_data = {
9736                 .rpc_client = server->client,
9737                 .rpc_message = &msg,
9738                 .callback_ops = &nfs4_layoutget_call_ops,
9739                 .callback_data = lgp,
9740                 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9741                          RPC_TASK_MOVEABLE,
9742         };
9743         struct pnfs_layout_segment *lseg = NULL;
9744         struct nfs4_exception exception = {
9745                 .inode = inode,
9746                 .timeout = *timeout,
9747         };
9748         int status = 0;
9749
9750         nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9751
9752         task = rpc_run_task(&task_setup_data);
9753         if (IS_ERR(task))
9754                 return ERR_CAST(task);
9755
9756         status = rpc_wait_for_completion_task(task);
9757         if (status != 0)
9758                 goto out;
9759
9760         if (task->tk_status < 0) {
9761                 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9762                 *timeout = exception.timeout;
9763         } else if (lgp->res.layoutp->len == 0) {
9764                 status = -EAGAIN;
9765                 *timeout = nfs4_update_delay(&exception.timeout);
9766         } else
9767                 lseg = pnfs_layout_process(lgp);
9768 out:
9769         trace_nfs4_layoutget(lgp->args.ctx,
9770                         &lgp->args.range,
9771                         &lgp->res.range,
9772                         &lgp->res.stateid,
9773                         status);
9774
9775         rpc_put_task(task);
9776         dprintk("<-- %s status=%d\n", __func__, status);
9777         if (status)
9778                 return ERR_PTR(status);
9779         return lseg;
9780 }
9781
9782 static void
9783 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9784 {
9785         struct nfs4_layoutreturn *lrp = calldata;
9786
9787         nfs4_setup_sequence(lrp->clp,
9788                         &lrp->args.seq_args,
9789                         &lrp->res.seq_res,
9790                         task);
9791         if (!pnfs_layout_is_valid(lrp->args.layout))
9792                 rpc_exit(task, 0);
9793 }
9794
9795 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9796 {
9797         struct nfs4_layoutreturn *lrp = calldata;
9798         struct nfs_server *server;
9799
9800         if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9801                 return;
9802
9803         /*
9804          * Was there an RPC level error? Assume the call succeeded,
9805          * and that we need to release the layout
9806          */
9807         if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9808                 lrp->res.lrs_present = 0;
9809                 return;
9810         }
9811
9812         server = NFS_SERVER(lrp->args.inode);
9813         switch (task->tk_status) {
9814         case -NFS4ERR_OLD_STATEID:
9815                 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9816                                         &lrp->args.range,
9817                                         lrp->args.inode))
9818                         goto out_restart;
9819                 fallthrough;
9820         default:
9821                 task->tk_status = 0;
9822                 fallthrough;
9823         case 0:
9824                 break;
9825         case -NFS4ERR_DELAY:
9826                 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9827                         break;
9828                 goto out_restart;
9829         }
9830         return;
9831 out_restart:
9832         task->tk_status = 0;
9833         nfs4_sequence_free_slot(&lrp->res.seq_res);
9834         rpc_restart_call_prepare(task);
9835 }
9836
9837 static void nfs4_layoutreturn_release(void *calldata)
9838 {
9839         struct nfs4_layoutreturn *lrp = calldata;
9840         struct pnfs_layout_hdr *lo = lrp->args.layout;
9841
9842         pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9843                         lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9844         nfs4_sequence_free_slot(&lrp->res.seq_res);
9845         if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9846                 lrp->ld_private.ops->free(&lrp->ld_private);
9847         pnfs_put_layout_hdr(lrp->args.layout);
9848         nfs_iput_and_deactive(lrp->inode);
9849         put_cred(lrp->cred);
9850         kfree(calldata);
9851 }
9852
9853 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9854         .rpc_call_prepare = nfs4_layoutreturn_prepare,
9855         .rpc_call_done = nfs4_layoutreturn_done,
9856         .rpc_release = nfs4_layoutreturn_release,
9857 };
9858
9859 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9860 {
9861         struct rpc_task *task;
9862         struct rpc_message msg = {
9863                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9864                 .rpc_argp = &lrp->args,
9865                 .rpc_resp = &lrp->res,
9866                 .rpc_cred = lrp->cred,
9867         };
9868         struct rpc_task_setup task_setup_data = {
9869                 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9870                 .rpc_message = &msg,
9871                 .callback_ops = &nfs4_layoutreturn_call_ops,
9872                 .callback_data = lrp,
9873                 .flags = RPC_TASK_MOVEABLE,
9874         };
9875         int status = 0;
9876
9877         nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9878                         NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9879                         &task_setup_data.rpc_client, &msg);
9880
9881         lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9882         if (!sync) {
9883                 if (!lrp->inode) {
9884                         nfs4_layoutreturn_release(lrp);
9885                         return -EAGAIN;
9886                 }
9887                 task_setup_data.flags |= RPC_TASK_ASYNC;
9888         }
9889         if (!lrp->inode)
9890                 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9891                                    1);
9892         else
9893                 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9894                                    0);
9895         task = rpc_run_task(&task_setup_data);
9896         if (IS_ERR(task))
9897                 return PTR_ERR(task);
9898         if (sync)
9899                 status = task->tk_status;
9900         trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9901         dprintk("<-- %s status=%d\n", __func__, status);
9902         rpc_put_task(task);
9903         return status;
9904 }
9905
9906 static int
9907 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9908                 struct pnfs_device *pdev,
9909                 const struct cred *cred)
9910 {
9911         struct nfs4_getdeviceinfo_args args = {
9912                 .pdev = pdev,
9913                 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9914                         NOTIFY_DEVICEID4_DELETE,
9915         };
9916         struct nfs4_getdeviceinfo_res res = {
9917                 .pdev = pdev,
9918         };
9919         struct rpc_message msg = {
9920                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9921                 .rpc_argp = &args,
9922                 .rpc_resp = &res,
9923                 .rpc_cred = cred,
9924         };
9925         int status;
9926
9927         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9928         if (res.notification & ~args.notify_types)
9929                 dprintk("%s: unsupported notification\n", __func__);
9930         if (res.notification != args.notify_types)
9931                 pdev->nocache = 1;
9932
9933         trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9934
9935         dprintk("<-- %s status=%d\n", __func__, status);
9936
9937         return status;
9938 }
9939
9940 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9941                 struct pnfs_device *pdev,
9942                 const struct cred *cred)
9943 {
9944         struct nfs4_exception exception = { };
9945         int err;
9946
9947         do {
9948                 err = nfs4_handle_exception(server,
9949                                         _nfs4_proc_getdeviceinfo(server, pdev, cred),
9950                                         &exception);
9951         } while (exception.retry);
9952         return err;
9953 }
9954 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9955
9956 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9957 {
9958         struct nfs4_layoutcommit_data *data = calldata;
9959         struct nfs_server *server = NFS_SERVER(data->args.inode);
9960
9961         nfs4_setup_sequence(server->nfs_client,
9962                         &data->args.seq_args,
9963                         &data->res.seq_res,
9964                         task);
9965 }
9966
9967 static void
9968 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9969 {
9970         struct nfs4_layoutcommit_data *data = calldata;
9971         struct nfs_server *server = NFS_SERVER(data->args.inode);
9972
9973         if (!nfs41_sequence_done(task, &data->res.seq_res))
9974                 return;
9975
9976         switch (task->tk_status) { /* Just ignore these failures */
9977         case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9978         case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
9979         case -NFS4ERR_BADLAYOUT:     /* no layout */
9980         case -NFS4ERR_GRACE:        /* loca_recalim always false */
9981                 task->tk_status = 0;
9982                 break;
9983         case 0:
9984                 break;
9985         default:
9986                 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9987                         rpc_restart_call_prepare(task);
9988                         return;
9989                 }
9990         }
9991 }
9992
9993 static void nfs4_layoutcommit_release(void *calldata)
9994 {
9995         struct nfs4_layoutcommit_data *data = calldata;
9996
9997         pnfs_cleanup_layoutcommit(data);
9998         nfs_post_op_update_inode_force_wcc(data->args.inode,
9999                                            data->res.fattr);
10000         put_cred(data->cred);
10001         nfs_iput_and_deactive(data->inode);
10002         kfree(data);
10003 }
10004
10005 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
10006         .rpc_call_prepare = nfs4_layoutcommit_prepare,
10007         .rpc_call_done = nfs4_layoutcommit_done,
10008         .rpc_release = nfs4_layoutcommit_release,
10009 };
10010
10011 int
10012 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
10013 {
10014         struct rpc_message msg = {
10015                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
10016                 .rpc_argp = &data->args,
10017                 .rpc_resp = &data->res,
10018                 .rpc_cred = data->cred,
10019         };
10020         struct rpc_task_setup task_setup_data = {
10021                 .task = &data->task,
10022                 .rpc_client = NFS_CLIENT(data->args.inode),
10023                 .rpc_message = &msg,
10024                 .callback_ops = &nfs4_layoutcommit_ops,
10025                 .callback_data = data,
10026                 .flags = RPC_TASK_MOVEABLE,
10027         };
10028         struct rpc_task *task;
10029         int status = 0;
10030
10031         dprintk("NFS: initiating layoutcommit call. sync %d "
10032                 "lbw: %llu inode %lu\n", sync,
10033                 data->args.lastbytewritten,
10034                 data->args.inode->i_ino);
10035
10036         if (!sync) {
10037                 data->inode = nfs_igrab_and_active(data->args.inode);
10038                 if (data->inode == NULL) {
10039                         nfs4_layoutcommit_release(data);
10040                         return -EAGAIN;
10041                 }
10042                 task_setup_data.flags = RPC_TASK_ASYNC;
10043         }
10044         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10045         task = rpc_run_task(&task_setup_data);
10046         if (IS_ERR(task))
10047                 return PTR_ERR(task);
10048         if (sync)
10049                 status = task->tk_status;
10050         trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10051         dprintk("%s: status %d\n", __func__, status);
10052         rpc_put_task(task);
10053         return status;
10054 }
10055
10056 /*
10057  * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10058  * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10059  */
10060 static int
10061 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10062                     struct nfs_fsinfo *info,
10063                     struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10064 {
10065         struct nfs41_secinfo_no_name_args args = {
10066                 .style = SECINFO_STYLE_CURRENT_FH,
10067         };
10068         struct nfs4_secinfo_res res = {
10069                 .flavors = flavors,
10070         };
10071         struct rpc_message msg = {
10072                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10073                 .rpc_argp = &args,
10074                 .rpc_resp = &res,
10075         };
10076         struct nfs4_call_sync_data data = {
10077                 .seq_server = server,
10078                 .seq_args = &args.seq_args,
10079                 .seq_res = &res.seq_res,
10080         };
10081         struct rpc_task_setup task_setup = {
10082                 .rpc_client = server->client,
10083                 .rpc_message = &msg,
10084                 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10085                 .callback_data = &data,
10086                 .flags = RPC_TASK_NO_ROUND_ROBIN,
10087         };
10088         const struct cred *cred = NULL;
10089         int status;
10090
10091         if (use_integrity) {
10092                 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10093
10094                 cred = nfs4_get_clid_cred(server->nfs_client);
10095                 msg.rpc_cred = cred;
10096         }
10097
10098         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10099         status = nfs4_call_sync_custom(&task_setup);
10100         dprintk("<-- %s status=%d\n", __func__, status);
10101
10102         put_cred(cred);
10103
10104         return status;
10105 }
10106
10107 static int
10108 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10109                            struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10110 {
10111         struct nfs4_exception exception = {
10112                 .interruptible = true,
10113         };
10114         int err;
10115         do {
10116                 /* first try using integrity protection */
10117                 err = -NFS4ERR_WRONGSEC;
10118
10119                 /* try to use integrity protection with machine cred */
10120                 if (_nfs4_is_integrity_protected(server->nfs_client))
10121                         err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10122                                                           flavors, true);
10123
10124                 /*
10125                  * if unable to use integrity protection, or SECINFO with
10126                  * integrity protection returns NFS4ERR_WRONGSEC (which is
10127                  * disallowed by spec, but exists in deployed servers) use
10128                  * the current filesystem's rpc_client and the user cred.
10129                  */
10130                 if (err == -NFS4ERR_WRONGSEC)
10131                         err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10132                                                           flavors, false);
10133
10134                 switch (err) {
10135                 case 0:
10136                 case -NFS4ERR_WRONGSEC:
10137                 case -ENOTSUPP:
10138                         goto out;
10139                 default:
10140                         err = nfs4_handle_exception(server, err, &exception);
10141                 }
10142         } while (exception.retry);
10143 out:
10144         return err;
10145 }
10146
10147 static int
10148 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10149                     struct nfs_fsinfo *info)
10150 {
10151         int err;
10152         struct page *page;
10153         rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10154         struct nfs4_secinfo_flavors *flavors;
10155         struct nfs4_secinfo4 *secinfo;
10156         int i;
10157
10158         page = alloc_page(GFP_KERNEL);
10159         if (!page) {
10160                 err = -ENOMEM;
10161                 goto out;
10162         }
10163
10164         flavors = page_address(page);
10165         err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10166
10167         /*
10168          * Fall back on "guess and check" method if
10169          * the server doesn't support SECINFO_NO_NAME
10170          */
10171         if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10172                 err = nfs4_find_root_sec(server, fhandle, info);
10173                 goto out_freepage;
10174         }
10175         if (err)
10176                 goto out_freepage;
10177
10178         for (i = 0; i < flavors->num_flavors; i++) {
10179                 secinfo = &flavors->flavors[i];
10180
10181                 switch (secinfo->flavor) {
10182                 case RPC_AUTH_NULL:
10183                 case RPC_AUTH_UNIX:
10184                 case RPC_AUTH_GSS:
10185                         flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10186                                         &secinfo->flavor_info);
10187                         break;
10188                 default:
10189                         flavor = RPC_AUTH_MAXFLAVOR;
10190                         break;
10191                 }
10192
10193                 if (!nfs_auth_info_match(&server->auth_info, flavor))
10194                         flavor = RPC_AUTH_MAXFLAVOR;
10195
10196                 if (flavor != RPC_AUTH_MAXFLAVOR) {
10197                         err = nfs4_lookup_root_sec(server, fhandle,
10198                                                    info, flavor);
10199                         if (!err)
10200                                 break;
10201                 }
10202         }
10203
10204         if (flavor == RPC_AUTH_MAXFLAVOR)
10205                 err = -EPERM;
10206
10207 out_freepage:
10208         put_page(page);
10209         if (err == -EACCES)
10210                 return -EPERM;
10211 out:
10212         return err;
10213 }
10214
10215 static int _nfs41_test_stateid(struct nfs_server *server,
10216                 nfs4_stateid *stateid,
10217                 const struct cred *cred)
10218 {
10219         int status;
10220         struct nfs41_test_stateid_args args = {
10221                 .stateid = stateid,
10222         };
10223         struct nfs41_test_stateid_res res;
10224         struct rpc_message msg = {
10225                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10226                 .rpc_argp = &args,
10227                 .rpc_resp = &res,
10228                 .rpc_cred = cred,
10229         };
10230         struct rpc_clnt *rpc_client = server->client;
10231
10232         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10233                 &rpc_client, &msg);
10234
10235         dprintk("NFS call  test_stateid %p\n", stateid);
10236         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10237         status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10238                         &args.seq_args, &res.seq_res);
10239         if (status != NFS_OK) {
10240                 dprintk("NFS reply test_stateid: failed, %d\n", status);
10241                 return status;
10242         }
10243         dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10244         return -res.status;
10245 }
10246
10247 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10248                 int err, struct nfs4_exception *exception)
10249 {
10250         exception->retry = 0;
10251         switch(err) {
10252         case -NFS4ERR_DELAY:
10253         case -NFS4ERR_RETRY_UNCACHED_REP:
10254                 nfs4_handle_exception(server, err, exception);
10255                 break;
10256         case -NFS4ERR_BADSESSION:
10257         case -NFS4ERR_BADSLOT:
10258         case -NFS4ERR_BAD_HIGH_SLOT:
10259         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10260         case -NFS4ERR_DEADSESSION:
10261                 nfs4_do_handle_exception(server, err, exception);
10262         }
10263 }
10264
10265 /**
10266  * nfs41_test_stateid - perform a TEST_STATEID operation
10267  *
10268  * @server: server / transport on which to perform the operation
10269  * @stateid: state ID to test
10270  * @cred: credential
10271  *
10272  * Returns NFS_OK if the server recognizes that "stateid" is valid.
10273  * Otherwise a negative NFS4ERR value is returned if the operation
10274  * failed or the state ID is not currently valid.
10275  */
10276 static int nfs41_test_stateid(struct nfs_server *server,
10277                 nfs4_stateid *stateid,
10278                 const struct cred *cred)
10279 {
10280         struct nfs4_exception exception = {
10281                 .interruptible = true,
10282         };
10283         int err;
10284         do {
10285                 err = _nfs41_test_stateid(server, stateid, cred);
10286                 nfs4_handle_delay_or_session_error(server, err, &exception);
10287         } while (exception.retry);
10288         return err;
10289 }
10290
10291 struct nfs_free_stateid_data {
10292         struct nfs_server *server;
10293         struct nfs41_free_stateid_args args;
10294         struct nfs41_free_stateid_res res;
10295 };
10296
10297 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10298 {
10299         struct nfs_free_stateid_data *data = calldata;
10300         nfs4_setup_sequence(data->server->nfs_client,
10301                         &data->args.seq_args,
10302                         &data->res.seq_res,
10303                         task);
10304 }
10305
10306 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10307 {
10308         struct nfs_free_stateid_data *data = calldata;
10309
10310         nfs41_sequence_done(task, &data->res.seq_res);
10311
10312         switch (task->tk_status) {
10313         case -NFS4ERR_DELAY:
10314                 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10315                         rpc_restart_call_prepare(task);
10316         }
10317 }
10318
10319 static void nfs41_free_stateid_release(void *calldata)
10320 {
10321         struct nfs_free_stateid_data *data = calldata;
10322         struct nfs_client *clp = data->server->nfs_client;
10323
10324         nfs_put_client(clp);
10325         kfree(calldata);
10326 }
10327
10328 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10329         .rpc_call_prepare = nfs41_free_stateid_prepare,
10330         .rpc_call_done = nfs41_free_stateid_done,
10331         .rpc_release = nfs41_free_stateid_release,
10332 };
10333
10334 /**
10335  * nfs41_free_stateid - perform a FREE_STATEID operation
10336  *
10337  * @server: server / transport on which to perform the operation
10338  * @stateid: state ID to release
10339  * @cred: credential
10340  * @privileged: set to true if this call needs to be privileged
10341  *
10342  * Note: this function is always asynchronous.
10343  */
10344 static int nfs41_free_stateid(struct nfs_server *server,
10345                 const nfs4_stateid *stateid,
10346                 const struct cred *cred,
10347                 bool privileged)
10348 {
10349         struct rpc_message msg = {
10350                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10351                 .rpc_cred = cred,
10352         };
10353         struct rpc_task_setup task_setup = {
10354                 .rpc_client = server->client,
10355                 .rpc_message = &msg,
10356                 .callback_ops = &nfs41_free_stateid_ops,
10357                 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10358         };
10359         struct nfs_free_stateid_data *data;
10360         struct rpc_task *task;
10361         struct nfs_client *clp = server->nfs_client;
10362
10363         if (!refcount_inc_not_zero(&clp->cl_count))
10364                 return -EIO;
10365
10366         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10367                 &task_setup.rpc_client, &msg);
10368
10369         dprintk("NFS call  free_stateid %p\n", stateid);
10370         data = kmalloc(sizeof(*data), GFP_KERNEL);
10371         if (!data)
10372                 return -ENOMEM;
10373         data->server = server;
10374         nfs4_stateid_copy(&data->args.stateid, stateid);
10375
10376         task_setup.callback_data = data;
10377
10378         msg.rpc_argp = &data->args;
10379         msg.rpc_resp = &data->res;
10380         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10381         task = rpc_run_task(&task_setup);
10382         if (IS_ERR(task))
10383                 return PTR_ERR(task);
10384         rpc_put_task(task);
10385         return 0;
10386 }
10387
10388 static void
10389 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10390 {
10391         const struct cred *cred = lsp->ls_state->owner->so_cred;
10392
10393         nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10394         nfs4_free_lock_state(server, lsp);
10395 }
10396
10397 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10398                 const nfs4_stateid *s2)
10399 {
10400         if (s1->type != s2->type)
10401                 return false;
10402
10403         if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10404                 return false;
10405
10406         if (s1->seqid == s2->seqid)
10407                 return true;
10408
10409         return s1->seqid == 0 || s2->seqid == 0;
10410 }
10411
10412 #endif /* CONFIG_NFS_V4_1 */
10413
10414 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10415                 const nfs4_stateid *s2)
10416 {
10417         return nfs4_stateid_match(s1, s2);
10418 }
10419
10420
10421 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10422         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10423         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10424         .recover_open   = nfs4_open_reclaim,
10425         .recover_lock   = nfs4_lock_reclaim,
10426         .establish_clid = nfs4_init_clientid,
10427         .detect_trunking = nfs40_discover_server_trunking,
10428 };
10429
10430 #if defined(CONFIG_NFS_V4_1)
10431 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10432         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10433         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10434         .recover_open   = nfs4_open_reclaim,
10435         .recover_lock   = nfs4_lock_reclaim,
10436         .establish_clid = nfs41_init_clientid,
10437         .reclaim_complete = nfs41_proc_reclaim_complete,
10438         .detect_trunking = nfs41_discover_server_trunking,
10439 };
10440 #endif /* CONFIG_NFS_V4_1 */
10441
10442 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10443         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10444         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10445         .recover_open   = nfs40_open_expired,
10446         .recover_lock   = nfs4_lock_expired,
10447         .establish_clid = nfs4_init_clientid,
10448 };
10449
10450 #if defined(CONFIG_NFS_V4_1)
10451 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10452         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10453         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10454         .recover_open   = nfs41_open_expired,
10455         .recover_lock   = nfs41_lock_expired,
10456         .establish_clid = nfs41_init_clientid,
10457 };
10458 #endif /* CONFIG_NFS_V4_1 */
10459
10460 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10461         .sched_state_renewal = nfs4_proc_async_renew,
10462         .get_state_renewal_cred = nfs4_get_renew_cred,
10463         .renew_lease = nfs4_proc_renew,
10464 };
10465
10466 #if defined(CONFIG_NFS_V4_1)
10467 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10468         .sched_state_renewal = nfs41_proc_async_sequence,
10469         .get_state_renewal_cred = nfs4_get_machine_cred,
10470         .renew_lease = nfs4_proc_sequence,
10471 };
10472 #endif
10473
10474 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10475         .get_locations = _nfs40_proc_get_locations,
10476         .fsid_present = _nfs40_proc_fsid_present,
10477 };
10478
10479 #if defined(CONFIG_NFS_V4_1)
10480 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10481         .get_locations = _nfs41_proc_get_locations,
10482         .fsid_present = _nfs41_proc_fsid_present,
10483 };
10484 #endif  /* CONFIG_NFS_V4_1 */
10485
10486 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10487         .minor_version = 0,
10488         .init_caps = NFS_CAP_READDIRPLUS
10489                 | NFS_CAP_ATOMIC_OPEN
10490                 | NFS_CAP_POSIX_LOCK,
10491         .init_client = nfs40_init_client,
10492         .shutdown_client = nfs40_shutdown_client,
10493         .match_stateid = nfs4_match_stateid,
10494         .find_root_sec = nfs4_find_root_sec,
10495         .free_lock_state = nfs4_release_lockowner,
10496         .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10497         .alloc_seqid = nfs_alloc_seqid,
10498         .call_sync_ops = &nfs40_call_sync_ops,
10499         .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10500         .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10501         .state_renewal_ops = &nfs40_state_renewal_ops,
10502         .mig_recovery_ops = &nfs40_mig_recovery_ops,
10503 };
10504
10505 #if defined(CONFIG_NFS_V4_1)
10506 static struct nfs_seqid *
10507 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10508 {
10509         return NULL;
10510 }
10511
10512 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10513         .minor_version = 1,
10514         .init_caps = NFS_CAP_READDIRPLUS
10515                 | NFS_CAP_ATOMIC_OPEN
10516                 | NFS_CAP_POSIX_LOCK
10517                 | NFS_CAP_STATEID_NFSV41
10518                 | NFS_CAP_ATOMIC_OPEN_V1
10519                 | NFS_CAP_LGOPEN
10520                 | NFS_CAP_MOVEABLE,
10521         .init_client = nfs41_init_client,
10522         .shutdown_client = nfs41_shutdown_client,
10523         .match_stateid = nfs41_match_stateid,
10524         .find_root_sec = nfs41_find_root_sec,
10525         .free_lock_state = nfs41_free_lock_state,
10526         .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10527         .alloc_seqid = nfs_alloc_no_seqid,
10528         .session_trunk = nfs4_test_session_trunk,
10529         .call_sync_ops = &nfs41_call_sync_ops,
10530         .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10531         .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10532         .state_renewal_ops = &nfs41_state_renewal_ops,
10533         .mig_recovery_ops = &nfs41_mig_recovery_ops,
10534 };
10535 #endif
10536
10537 #if defined(CONFIG_NFS_V4_2)
10538 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10539         .minor_version = 2,
10540         .init_caps = NFS_CAP_READDIRPLUS
10541                 | NFS_CAP_ATOMIC_OPEN
10542                 | NFS_CAP_POSIX_LOCK
10543                 | NFS_CAP_STATEID_NFSV41
10544                 | NFS_CAP_ATOMIC_OPEN_V1
10545                 | NFS_CAP_LGOPEN
10546                 | NFS_CAP_ALLOCATE
10547                 | NFS_CAP_COPY
10548                 | NFS_CAP_OFFLOAD_CANCEL
10549                 | NFS_CAP_COPY_NOTIFY
10550                 | NFS_CAP_DEALLOCATE
10551                 | NFS_CAP_SEEK
10552                 | NFS_CAP_LAYOUTSTATS
10553                 | NFS_CAP_CLONE
10554                 | NFS_CAP_LAYOUTERROR
10555                 | NFS_CAP_READ_PLUS
10556                 | NFS_CAP_MOVEABLE,
10557         .init_client = nfs41_init_client,
10558         .shutdown_client = nfs41_shutdown_client,
10559         .match_stateid = nfs41_match_stateid,
10560         .find_root_sec = nfs41_find_root_sec,
10561         .free_lock_state = nfs41_free_lock_state,
10562         .call_sync_ops = &nfs41_call_sync_ops,
10563         .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10564         .alloc_seqid = nfs_alloc_no_seqid,
10565         .session_trunk = nfs4_test_session_trunk,
10566         .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10567         .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10568         .state_renewal_ops = &nfs41_state_renewal_ops,
10569         .mig_recovery_ops = &nfs41_mig_recovery_ops,
10570 };
10571 #endif
10572
10573 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10574         [0] = &nfs_v4_0_minor_ops,
10575 #if defined(CONFIG_NFS_V4_1)
10576         [1] = &nfs_v4_1_minor_ops,
10577 #endif
10578 #if defined(CONFIG_NFS_V4_2)
10579         [2] = &nfs_v4_2_minor_ops,
10580 #endif
10581 };
10582
10583 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10584 {
10585         ssize_t error, error2, error3;
10586
10587         error = generic_listxattr(dentry, list, size);
10588         if (error < 0)
10589                 return error;
10590         if (list) {
10591                 list += error;
10592                 size -= error;
10593         }
10594
10595         error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10596         if (error2 < 0)
10597                 return error2;
10598
10599         if (list) {
10600                 list += error2;
10601                 size -= error2;
10602         }
10603
10604         error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10605         if (error3 < 0)
10606                 return error3;
10607
10608         return error + error2 + error3;
10609 }
10610
10611 static void nfs4_enable_swap(struct inode *inode)
10612 {
10613         /* The state manager thread must always be running.
10614          * It will notice the client is a swapper, and stay put.
10615          */
10616         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10617
10618         nfs4_schedule_state_manager(clp);
10619 }
10620
10621 static void nfs4_disable_swap(struct inode *inode)
10622 {
10623         /* The state manager thread will now exit once it is
10624          * woken.
10625          */
10626         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10627
10628         nfs4_schedule_state_manager(clp);
10629 }
10630
10631 static const struct inode_operations nfs4_dir_inode_operations = {
10632         .create         = nfs_create,
10633         .lookup         = nfs_lookup,
10634         .atomic_open    = nfs_atomic_open,
10635         .link           = nfs_link,
10636         .unlink         = nfs_unlink,
10637         .symlink        = nfs_symlink,
10638         .mkdir          = nfs_mkdir,
10639         .rmdir          = nfs_rmdir,
10640         .mknod          = nfs_mknod,
10641         .rename         = nfs_rename,
10642         .permission     = nfs_permission,
10643         .getattr        = nfs_getattr,
10644         .setattr        = nfs_setattr,
10645         .listxattr      = nfs4_listxattr,
10646 };
10647
10648 static const struct inode_operations nfs4_file_inode_operations = {
10649         .permission     = nfs_permission,
10650         .getattr        = nfs_getattr,
10651         .setattr        = nfs_setattr,
10652         .listxattr      = nfs4_listxattr,
10653 };
10654
10655 const struct nfs_rpc_ops nfs_v4_clientops = {
10656         .version        = 4,                    /* protocol version */
10657         .dentry_ops     = &nfs4_dentry_operations,
10658         .dir_inode_ops  = &nfs4_dir_inode_operations,
10659         .file_inode_ops = &nfs4_file_inode_operations,
10660         .file_ops       = &nfs4_file_operations,
10661         .getroot        = nfs4_proc_get_root,
10662         .submount       = nfs4_submount,
10663         .try_get_tree   = nfs4_try_get_tree,
10664         .getattr        = nfs4_proc_getattr,
10665         .setattr        = nfs4_proc_setattr,
10666         .lookup         = nfs4_proc_lookup,
10667         .lookupp        = nfs4_proc_lookupp,
10668         .access         = nfs4_proc_access,
10669         .readlink       = nfs4_proc_readlink,
10670         .create         = nfs4_proc_create,
10671         .remove         = nfs4_proc_remove,
10672         .unlink_setup   = nfs4_proc_unlink_setup,
10673         .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10674         .unlink_done    = nfs4_proc_unlink_done,
10675         .rename_setup   = nfs4_proc_rename_setup,
10676         .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10677         .rename_done    = nfs4_proc_rename_done,
10678         .link           = nfs4_proc_link,
10679         .symlink        = nfs4_proc_symlink,
10680         .mkdir          = nfs4_proc_mkdir,
10681         .rmdir          = nfs4_proc_rmdir,
10682         .readdir        = nfs4_proc_readdir,
10683         .mknod          = nfs4_proc_mknod,
10684         .statfs         = nfs4_proc_statfs,
10685         .fsinfo         = nfs4_proc_fsinfo,
10686         .pathconf       = nfs4_proc_pathconf,
10687         .set_capabilities = nfs4_server_capabilities,
10688         .decode_dirent  = nfs4_decode_dirent,
10689         .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10690         .read_setup     = nfs4_proc_read_setup,
10691         .read_done      = nfs4_read_done,
10692         .write_setup    = nfs4_proc_write_setup,
10693         .write_done     = nfs4_write_done,
10694         .commit_setup   = nfs4_proc_commit_setup,
10695         .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10696         .commit_done    = nfs4_commit_done,
10697         .lock           = nfs4_proc_lock,
10698         .clear_acl_cache = nfs4_zap_acl_attr,
10699         .close_context  = nfs4_close_context,
10700         .open_context   = nfs4_atomic_open,
10701         .have_delegation = nfs4_have_delegation,
10702         .alloc_client   = nfs4_alloc_client,
10703         .init_client    = nfs4_init_client,
10704         .free_client    = nfs4_free_client,
10705         .create_server  = nfs4_create_server,
10706         .clone_server   = nfs_clone_server,
10707         .discover_trunking = nfs4_discover_trunking,
10708         .enable_swap    = nfs4_enable_swap,
10709         .disable_swap   = nfs4_disable_swap,
10710 };
10711
10712 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10713         .name   = XATTR_NAME_NFSV4_ACL,
10714         .list   = nfs4_xattr_list_nfs4_acl,
10715         .get    = nfs4_xattr_get_nfs4_acl,
10716         .set    = nfs4_xattr_set_nfs4_acl,
10717 };
10718
10719 #if defined(CONFIG_NFS_V4_1)
10720 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10721         .name   = XATTR_NAME_NFSV4_DACL,
10722         .list   = nfs4_xattr_list_nfs4_dacl,
10723         .get    = nfs4_xattr_get_nfs4_dacl,
10724         .set    = nfs4_xattr_set_nfs4_dacl,
10725 };
10726
10727 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10728         .name   = XATTR_NAME_NFSV4_SACL,
10729         .list   = nfs4_xattr_list_nfs4_sacl,
10730         .get    = nfs4_xattr_get_nfs4_sacl,
10731         .set    = nfs4_xattr_set_nfs4_sacl,
10732 };
10733 #endif
10734
10735 #ifdef CONFIG_NFS_V4_2
10736 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10737         .prefix = XATTR_USER_PREFIX,
10738         .get    = nfs4_xattr_get_nfs4_user,
10739         .set    = nfs4_xattr_set_nfs4_user,
10740 };
10741 #endif
10742
10743 const struct xattr_handler *nfs4_xattr_handlers[] = {
10744         &nfs4_xattr_nfs4_acl_handler,
10745 #if defined(CONFIG_NFS_V4_1)
10746         &nfs4_xattr_nfs4_dacl_handler,
10747         &nfs4_xattr_nfs4_sacl_handler,
10748 #endif
10749 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10750         &nfs4_xattr_nfs4_label_handler,
10751 #endif
10752 #ifdef CONFIG_NFS_V4_2
10753         &nfs4_xattr_nfs4_user_handler,
10754 #endif
10755         NULL
10756 };