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